From 441f7856ee366416907be386800c85d9846b7709 Mon Sep 17 00:00:00 2001 From: nodeSpace <28955991+nodeSpace@users.noreply.github.com> Date: Tue, 5 Apr 2022 23:28:59 -0400 Subject: [PATCH 1/4] Make editor ui work inside windows. Fix for #4 --- egui_node_graph/src/editor_ui.rs | 64 +++++++++++++++++++------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/egui_node_graph/src/editor_ui.rs b/egui_node_graph/src/editor_ui.rs index 2d0d68e..c79e893 100644 --- a/egui_node_graph/src/editor_ui.rs +++ b/egui_node_graph/src/editor_ui.rs @@ -61,10 +61,18 @@ where pub fn draw_graph_editor( &mut self, ctx: &CtxRef, + ui: &mut Ui, all_kinds: impl NodeTemplateIter, ) -> GraphResponse { + // This causes the graph editor to use as much free space as it can. + // (so for windows it will use up to the resizeably set limit + // and for a Panel it will fill it completely) + let editor_rect = ui.max_rect(); + ui.allocate_rect(editor_rect, Sense::hover()); + let mouse = &ctx.input().pointer; let cursor_pos = mouse.hover_pos().unwrap_or(Pos2::ZERO); + let mut cursor_in_editor = editor_rect.contains(cursor_pos); // Gets filled with the port locations as nodes are drawn let mut port_locations = PortLocations::new(); @@ -83,32 +91,30 @@ where inconsistent self. It has either more or less values than the graph." ); - CentralPanel::default().show(ctx, |ui| { - /* Draw nodes */ - for node_id in self.node_order.iter().copied() { - let responses = GraphNodeWidget { - position: self.node_positions.get_mut(node_id).unwrap(), - graph: &mut self.graph, - port_locations: &mut port_locations, - node_id, - ongoing_drag: self.connection_in_progress, - selected: self - .selected_node - .map(|selected| selected == node_id) - .unwrap_or(false), - pan: self.pan_zoom.pan, - } + /* Draw nodes */ + for node_id in self.node_order.iter().copied() { + let responses = GraphNodeWidget { + position: self.node_positions.get_mut(node_id).unwrap(), + graph: &mut self.graph, + port_locations: &mut port_locations, + node_id, + ongoing_drag: self.connection_in_progress, + selected: self + .selected_node + .map(|selected| selected == node_id) + .unwrap_or(false), + pan: self.pan_zoom.pan, + } .show(ui, &self.user_state); - // Actions executed later - delayed_responses.extend(responses); - } + // Actions executed later + delayed_responses.extend(responses); + } - let r = ui.allocate_rect(ui.min_rect(), Sense::click()); - if r.clicked() { - click_on_background = true; - } - }); + let r = ui.allocate_rect(ui.min_rect(), Sense::click()); + if r.clicked() { + click_on_background = true; + } /* Draw the node finder, if open */ let mut should_close_node_finder = false; @@ -131,6 +137,12 @@ where should_close_node_finder = true; delayed_responses.push(NodeResponse::CreatedNode(new_node)); } + let finder_rect = ui.max_rect(); + // If the cursor is not in the main editor, check if the cursor *is* in the finder + // if the cursor is in the finder, then we can consider that also in the editor. + if !cursor_in_editor && finder_rect.contains(cursor_pos){ + cursor_in_editor = true; + } }); } if should_close_node_finder { @@ -228,7 +240,7 @@ where self.connection_in_progress = None; } - if mouse.button_down(PointerButton::Secondary) { + if mouse.button_down(PointerButton::Secondary) && cursor_in_editor{ self.node_finder = Some(NodeFinder::new_at(cursor_pos)); } if ctx.input().key_pressed(Key::Escape) { @@ -239,7 +251,9 @@ where self.pan_zoom.pan += ctx.input().pointer.delta(); } - if click_on_background { + // Deselect and deactivate finder if the editor backround is clicked, + // *or* if the the mouse clicks off the ui + if click_on_background || (mouse.any_click() && !cursor_in_editor){ self.selected_node = None; self.node_finder = None; } From 45adcf7dc973b34f58436e3962cb85e8efc3996d Mon Sep 17 00:00:00 2001 From: nodeSpace <28955991+nodeSpace@users.noreply.github.com> Date: Wed, 6 Apr 2022 01:15:16 -0400 Subject: [PATCH 2/4] Fixed connection lines not displaying properly --- egui_node_graph/src/editor_ui.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/egui_node_graph/src/editor_ui.rs b/egui_node_graph/src/editor_ui.rs index c79e893..727c3e1 100644 --- a/egui_node_graph/src/editor_ui.rs +++ b/egui_node_graph/src/editor_ui.rs @@ -156,16 +156,14 @@ where }; if let Some((_, ref locator)) = self.connection_in_progress { - let painter = ctx.layer_painter(LayerId::background()); let start_pos = port_locations[locator]; - painter.line_segment([start_pos, cursor_pos], connection_stroke) + ui.painter().line_segment([start_pos, cursor_pos], connection_stroke) } for (input, output) in self.graph.iter_connections() { - let painter = ctx.layer_painter(LayerId::background()); let src_pos = port_locations[&AnyParameterId::Output(output)]; let dst_pos = port_locations[&AnyParameterId::Input(input)]; - painter.line_segment([src_pos, dst_pos], connection_stroke); + ui.painter().line_segment([src_pos, dst_pos], connection_stroke); } /* Handle responses from drawing nodes */ From a4730360378c37c90e838c893536fc8474d0ea54 Mon Sep 17 00:00:00 2001 From: nodeSpace <28955991+nodeSpace@users.noreply.github.com> Date: Fri, 15 Apr 2022 01:22:37 -0400 Subject: [PATCH 3/4] Fix for editor contents not moving with window --- egui_node_graph/src/editor_ui.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/egui_node_graph/src/editor_ui.rs b/egui_node_graph/src/editor_ui.rs index 727c3e1..ef63599 100644 --- a/egui_node_graph/src/editor_ui.rs +++ b/egui_node_graph/src/editor_ui.rs @@ -103,7 +103,7 @@ where .selected_node .map(|selected| selected == node_id) .unwrap_or(false), - pan: self.pan_zoom.pan, + pan: self.pan_zoom.pan + editor_rect.min.to_vec2(), } .show(ui, &self.user_state); From 0442d63cb480ab4f063a472a6d7d178f27422f4b Mon Sep 17 00:00:00 2001 From: nodeSpace <28955991+nodeSpace@users.noreply.github.com> Date: Fri, 15 Apr 2022 01:51:00 -0400 Subject: [PATCH 4/4] fix node spawn location bug introduced by last fix --- egui_node_graph/src/editor_ui.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/egui_node_graph/src/editor_ui.rs b/egui_node_graph/src/editor_ui.rs index ef63599..6fb8e1d 100644 --- a/egui_node_graph/src/editor_ui.rs +++ b/egui_node_graph/src/editor_ui.rs @@ -130,8 +130,10 @@ where node_kind.user_data(), |graph, node_id| node_kind.build_node(graph, node_id), ); - self.node_positions - .insert(new_node, cursor_pos - self.pan_zoom.pan); + self.node_positions.insert( + new_node, + cursor_pos - self.pan_zoom.pan - editor_rect.min.to_vec2() + ); self.node_order.push(new_node); should_close_node_finder = true;