From 4ce43f5521e87f5a21729e865e690d15e4f017a9 Mon Sep 17 00:00:00 2001 From: Okko Hakola Date: Sat, 22 Apr 2023 16:40:17 +0300 Subject: [PATCH 1/3] Update egui --- .gitignore | 1 + egui_node_graph/Cargo.toml | 4 ++-- egui_node_graph/src/editor_ui.rs | 24 +++++++++++++----------- egui_node_graph/src/node_finder.rs | 4 ++-- egui_node_graph_example/Cargo.toml | 2 +- egui_node_graph_example/src/main.rs | 3 ++- 6 files changed, 21 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 96ef6c0..408b8a5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target Cargo.lock +.idea \ No newline at end of file diff --git a/egui_node_graph/Cargo.toml b/egui_node_graph/Cargo.toml index 25292d4..eb105e6 100644 --- a/egui_node_graph/Cargo.toml +++ b/egui_node_graph/Cargo.toml @@ -15,8 +15,8 @@ workspace = ".." persistence = ["serde", "slotmap/serde", "smallvec/serde", "egui/persistence"] [dependencies] -egui = { version = "0.19.0" } +egui = { version = "0.21.0" } slotmap = { version = "1.0" } -smallvec = { version = "1.7.0" } +smallvec = { version = "1.10.0" } serde = { version = "1.0", optional = true, features = ["derive"] } thiserror = "1.0" diff --git a/egui_node_graph/src/editor_ui.rs b/egui_node_graph/src/editor_ui.rs index dbc61a8..fe5751a 100644 --- a/egui_node_graph/src/editor_ui.rs +++ b/egui_node_graph/src/editor_ui.rs @@ -116,7 +116,9 @@ where let editor_rect = ui.max_rect(); ui.allocate_rect(editor_rect, Sense::hover()); - let cursor_pos = ui.ctx().input().pointer.hover_pos().unwrap_or(Pos2::ZERO); + let cursor_pos = ui + .ctx() + .input(|i| i.pointer.hover_pos().unwrap_or(Pos2::ZERO)); let mut cursor_in_editor = editor_rect.contains(cursor_pos); let mut cursor_in_finder = false; @@ -395,7 +397,7 @@ where /* Mouse input handling */ // This locks the context, so don't hold on to it for too long. - let mouse = &ui.ctx().input().pointer; + let mouse = &ui.ctx().input(|i| i.pointer.clone()); if mouse.any_released() && self.connection_in_progress.is_some() { self.connection_in_progress = None; @@ -404,12 +406,12 @@ where if mouse.secondary_released() && cursor_in_editor && !cursor_in_finder { self.node_finder = Some(NodeFinder::new_at(cursor_pos)); } - if ui.ctx().input().key_pressed(Key::Escape) { + if ui.ctx().input(|i| i.key_pressed(Key::Escape)) { self.node_finder = None; } - if r.dragged() && ui.ctx().input().pointer.middle_down() { - self.pan_zoom.pan += ui.ctx().input().pointer.delta(); + if r.dragged() && ui.ctx().input(|i| i.pointer.middle_down()) { + self.pan_zoom.pan += ui.ctx().input(|i| i.pointer.delta()); } // Deselect and deactivate finder if the editor backround is clicked, @@ -628,7 +630,7 @@ where port_type.data_type_color(user_state) }; ui.painter() - .circle(port_rect.center(), 5.0, port_color, Stroke::none()); + .circle(port_rect.center(), 5.0, port_color, Stroke::NONE); if resp.drag_started() { if is_connected_input { @@ -650,7 +652,7 @@ where // Don't allow self-loops if graph.any_param_type(origin_param).unwrap() == port_type && close_enough - && ui.input().pointer.any_released() + && ui.input(|i| i.pointer.any_released()) { match (param_id, origin_param) { (AnyParameterId::Input(input), AnyParameterId::Output(output)) @@ -734,7 +736,7 @@ where .user_data .titlebar_color(ui, self.node_id, self.graph, user_state) .unwrap_or_else(|| background_color.lighten(0.8)), - stroke: Stroke::none(), + stroke: Stroke::NONE, }); let body_rect = Rect::from_min_size( @@ -745,7 +747,7 @@ where rect: body_rect, rounding: Rounding::none(), fill: background_color, - stroke: Stroke::none(), + stroke: Stroke::NONE, }); let bottom_body_rect = Rect::from_min_size( @@ -756,7 +758,7 @@ where rect: bottom_body_rect, rounding, fill: background_color, - stroke: Stroke::none(), + stroke: Stroke::NONE, }); let node_rect = titlebar_rect.union(body_rect).union(bottom_body_rect); @@ -765,7 +767,7 @@ where rect: node_rect.expand(1.0), rounding, fill: Color32::WHITE.lighten(0.8), - stroke: Stroke::none(), + stroke: Stroke::NONE, }) } else { Shape::Noop diff --git a/egui_node_graph/src/node_finder.rs b/egui_node_graph/src/node_finder.rs index 87a9596..4225f9e 100644 --- a/egui_node_graph/src/node_finder.rs +++ b/egui_node_graph/src/node_finder.rs @@ -63,9 +63,9 @@ where self.just_spawned = false; } - let mut query_submit = resp.lost_focus() && ui.input().key_down(Key::Enter); + let mut query_submit = resp.lost_focus() && ui.input(|i| i.key_pressed(Key::Enter)); - let max_height = ui.input().screen_rect.height() * 0.5; + let max_height = ui.input(|i| i.screen_rect.height()); let scroll_area_width = resp.rect.width() - 30.0; Frame::default() diff --git a/egui_node_graph_example/Cargo.toml b/egui_node_graph_example/Cargo.toml index 7748d93..4094bfe 100644 --- a/egui_node_graph_example/Cargo.toml +++ b/egui_node_graph_example/Cargo.toml @@ -9,7 +9,7 @@ rust-version = "1.56" crate-type = ["cdylib", "rlib"] [dependencies] -eframe = "0.19.0" +eframe = "0.21.0" egui_node_graph = { path = "../egui_node_graph" } anyhow = "1.0" serde = { version = "1.0", optional = true } diff --git a/egui_node_graph_example/src/main.rs b/egui_node_graph_example/src/main.rs index 02d1528..0fe95af 100644 --- a/egui_node_graph_example/src/main.rs +++ b/egui_node_graph_example/src/main.rs @@ -19,5 +19,6 @@ fn main() { #[cfg(not(feature = "persistence"))] Box::new(egui_node_graph_example::NodeGraphExample::default()) }), - ); + ) + .expect("Failed to run native example"); } From 790d9ef5f9c88897e40e715a6fbbc6f1ade4f740 Mon Sep 17 00:00:00 2001 From: Okko Hakola Date: Sun, 23 Apr 2023 01:53:51 +0300 Subject: [PATCH 2/3] Fix node interaction bug --- egui_node_graph/src/editor_ui.rs | 36 +++++++++++++++++++------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/egui_node_graph/src/editor_ui.rs b/egui_node_graph/src/editor_ui.rs index fe5751a..1ef32c8 100644 --- a/egui_node_graph/src/editor_ui.rs +++ b/egui_node_graph/src/editor_ui.rs @@ -144,6 +144,17 @@ where inconsistent self. It has either more or less values than the graph." ); + // Allocate rect before the nodes, otherwise this will block the interaction + // with the nodes. + let r = ui.allocate_rect(ui.min_rect(), Sense::click().union(Sense::drag())); + if r.clicked() { + click_on_background = true; + } else if r.drag_started() { + drag_started_on_background = true; + } else if r.drag_released() { + drag_released_on_background = true; + } + /* Draw nodes */ for node_id in self.node_order.iter().copied() { let responses = GraphNodeWidget { @@ -165,15 +176,6 @@ where delayed_responses.extend(responses); } - let r = ui.allocate_rect(ui.min_rect(), Sense::click().union(Sense::drag())); - if r.clicked() { - click_on_background = true; - } else if r.drag_started() { - drag_started_on_background = true; - } else if r.drag_released() { - drag_released_on_background = true; - } - /* Draw the node finder, if open */ let mut should_close_node_finder = false; if let Some(ref mut node_finder) = self.node_finder { @@ -510,6 +512,16 @@ where let background_shape = ui.painter().add(Shape::Noop); let outer_rect_bounds = ui.available_rect_before_wrap(); + + // Hack?: Call this before the content of the node + // to ensure interaction works after egui 0.19. Otherwise the layers block + // hover interaction + let window_response = ui.interact( + outer_rect_bounds, + Id::new((self.node_id, "window")), + Sense::click_and_drag(), + ); + let mut inner_rect = outer_rect_bounds.shrink2(margin); // Make sure we don't shrink to the negative: @@ -795,12 +807,6 @@ where responses.push(NodeResponse::DeleteNodeUi(self.node_id)); }; - let window_response = ui.interact( - outer_rect, - Id::new((self.node_id, "window")), - Sense::click_and_drag(), - ); - // Movement let drag_delta = window_response.drag_delta(); if drag_delta.length_sq() > 0.0 { From 4cfe39341227cbbc6afb03bb7fd8f27fd6b702c8 Mon Sep 17 00:00:00 2001 From: Okko Hakola Date: Sun, 23 Apr 2023 19:44:32 +0300 Subject: [PATCH 3/3] Implement fix suggestions (interact area on resize) --- egui_node_graph/src/editor_ui.rs | 36 ++++++++++++++++++++++-------- egui_node_graph/src/node_finder.rs | 2 +- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/egui_node_graph/src/editor_ui.rs b/egui_node_graph/src/editor_ui.rs index 1ef32c8..43f6487 100644 --- a/egui_node_graph/src/editor_ui.rs +++ b/egui_node_graph/src/editor_ui.rs @@ -455,6 +455,9 @@ fn draw_connection(painter: &Painter, src_pos: Pos2, dst_pos: Pos2, color: Color painter.add(bezier); } +#[derive(Clone, Copy, Debug)] +struct OuterRectMemory(Rect); + impl<'a, NodeData, DataType, ValueType, UserResponse, UserState> GraphNodeWidget<'a, NodeData, DataType, ValueType> where @@ -513,15 +516,6 @@ where let outer_rect_bounds = ui.available_rect_before_wrap(); - // Hack?: Call this before the content of the node - // to ensure interaction works after egui 0.19. Otherwise the layers block - // hover interaction - let window_response = ui.interact( - outer_rect_bounds, - Id::new((self.node_id, "window")), - Sense::click_and_drag(), - ); - let mut inner_rect = outer_rect_bounds.shrink2(margin); // Make sure we don't shrink to the negative: @@ -529,6 +523,24 @@ where inner_rect.max.y = inner_rect.max.y.max(inner_rect.min.y); let mut child_ui = ui.child_ui(inner_rect, *ui.layout()); + + // Get interaction rect from memory, it may expand after the window response on resize. + let interaction_rect = ui + .ctx() + .memory_mut(|mem| { + mem.data + .get_temp::(child_ui.id()) + .map(|stored| stored.0) + }) + .unwrap_or(outer_rect_bounds); + // After 0.20, layers added over others can block hover interaction. Call this first + // before creating the node content. + let window_response = ui.interact( + interaction_rect, + Id::new((self.node_id, "window")), + Sense::click_and_drag(), + ); + let mut title_height = 0.0; let mut input_port_heights = vec![]; @@ -600,6 +612,12 @@ where let port_left = outer_rect.left(); let port_right = outer_rect.right(); + // Save expanded rect to memory. + ui.ctx().memory_mut(|mem| { + mem.data + .insert_temp(child_ui.id(), OuterRectMemory(outer_rect)) + }); + #[allow(clippy::too_many_arguments)] fn draw_port( ui: &mut Ui, diff --git a/egui_node_graph/src/node_finder.rs b/egui_node_graph/src/node_finder.rs index 4225f9e..f0863f5 100644 --- a/egui_node_graph/src/node_finder.rs +++ b/egui_node_graph/src/node_finder.rs @@ -65,7 +65,7 @@ where let mut query_submit = resp.lost_focus() && ui.input(|i| i.key_pressed(Key::Enter)); - let max_height = ui.input(|i| i.screen_rect.height()); + let max_height = ui.input(|i| i.screen_rect.height() * 0.5); let scroll_area_width = resp.rect.width() - 30.0; Frame::default()