Why didn't Response.is_pointer_button_down_on and Response.dragged work in this case?
#7257
-
ContextI'm trying to implement a simple drag-and-drop app and ran into this problem. The list of CommandRUST_LOG=INFO cargo run --releaseCodeuse egui::{Align2, Color32, FontId, Id, LayerId, Sense, show_tooltip};
use log::info;
fn main() -> eframe::Result {
env_logger::init();
let native_options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default()
.with_inner_size([400.0, 300.0])
.with_min_inner_size([300.0, 220.0]),
..Default::default()
};
eframe::run_native(
"DndApp",
native_options,
// Box::new(|cc| Ok(Box::new(Game::new(cc)))),
Box::new(|cc| Ok(Box::new(MyDnDApp {
items: vec![
Potion {
id: 1,
name: "Healing".to_string(),
color: Color32::from_rgb(255, 0, 0),
},
Potion {
id: 2,
name: "Mana".to_string(),
color: Color32::from_rgb(0, 0, 255),
},
Potion {
id: 3,
name: "Stamina".to_string(),
color: Color32::from_rgb(0, 255, 0),
},
],
dropped_potion_name: Some("drop zoneee".to_string()),
}))),
)
}
#[derive(Clone, PartialEq, Eq, Debug)]
struct Potion {
id: u32,
name: String,
color: Color32,
}
#[derive(Default)]
struct MyDnDApp {
items: Vec<Potion>,
dropped_potion_name: Option<String>,
}
impl eframe::App for MyDnDApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.horizontal(|ui| {
ui.vertical(|ui| {
ui.label("Inventory (Drag from here)");
for potion in &self.items {
let item_id = egui::Id::new(("potion", potion.id));
let response = ui
.dnd_drag_source(
item_id,
potion.clone(),
|ui| {
egui::Frame::NONE
.fill(potion.color.linear_multiply(0.5))
.inner_margin(8.0)
.show(ui, |ui| {
ui.label(&potion.name);
});
},
).response;
// if response.is_pointer_button_down_on() {
// if response.dragged() {
if response.contains_pointer() {
info!("Dragging potion: {}", potion.name);
show_tooltip(ctx, LayerId::debug(), ui.auto_id_with("tooltip_id"), |ui| {
ui.label(format!("A magical {} potion", potion.name));
});
}
}
});
ui.vertical(|ui| {
ui.label("Cauldron (Drop here)");
let (inner, drop_payload) = ui.dnd_drop_zone::<Potion, ()>(
egui::Frame::default().inner_margin(16.),
|ui| {
ui.set_min_size(egui::vec2(150., 150.));
if let Some(name) = &self.dropped_potion_name {
ui.label(format!("Dropped: {}", name));
} else {
ui.label("Drop a potion here.");
}
},
);
if let Some(potion_payload) = drop_payload {
self.dropped_potion_name = Some(potion_payload.name.clone());
}
let style = ui.style_mut();
if inner.response.drag_stopped() {
style.visuals.widgets.active.bg_fill = egui::Color32::from_rgb(0, 100, 0);
}
if inner.response.dragged() {
style.visuals.widgets.hovered.bg_fill = egui::Color32::from_rgb(0, 50, 0);
}
});
});
});
}
}Cargo.toml[dependencies]
eframe = "0.31.1"
egui = "0.31.1"
egui_extras = "0.31.1"
env_logger = "0.11.8"
log = "0.4.27"
serde = { version = "1.*", features = ["derive"] }
[profile.release]
opt-level = 2
[profile.dev.package."*"]
opt-level = 2
|
Beta Was this translation helpful? Give feedback.
Answered by
taminhtienhai
Jun 21, 2025
Replies: 1 comment
-
|
Ah, after consulting an AI, it became clear: the newly dragged widget wasn't registering with Drag sense, but its origin was. The correct approach to check if the current widget is being dragged is through // Use the egui context to check drag state, not the response
if ctx.is_being_dragged(item_id) {
info!("Dragging potion: {}", potion.name);
show_tooltip(ctx, LayerId::debug(), ui.auto_id_with("tooltip_id"), |ui| {
ui.label(format!("A magical {} potion", potion.name));
});
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
taminhtienhai
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah, after consulting an AI, it became clear: the newly dragged widget wasn't registering with Drag sense, but its origin was. The correct approach to check if the current widget is being dragged is through
Context.is_being_dragged.