From b97a567eb76ad2581b2bf4e56dcf5b5d195aa20d Mon Sep 17 00:00:00 2001 From: KOKI Date: Mon, 1 Aug 2022 16:54:00 +0900 Subject: [PATCH 1/2] Snap Connections and Ports --- egui_node_graph/src/editor_ui.rs | 46 +++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/egui_node_graph/src/editor_ui.rs b/egui_node_graph/src/editor_ui.rs index a64f144..505fdcf 100644 --- a/egui_node_graph/src/editor_ui.rs +++ b/egui_node_graph/src/editor_ui.rs @@ -8,6 +8,7 @@ use egui::epaint::{CubicBezierShape, RectShape}; use egui::*; pub type PortLocations = std::collections::HashMap; +const DISTANCE_TO_CONNECT: f32 = 10.0; /// Nodes communicate certain events to the parent graph when drawn. There is /// one special `User` variant which can be used by users as the return value @@ -176,8 +177,37 @@ where let connection_color = port_type.data_type_color(&self.user_state); let start_pos = port_locations[locator]; let (src_pos, dst_pos) = match locator { - AnyParameterId::Output(_) => (start_pos, cursor_pos), - AnyParameterId::Input(_) => (cursor_pos, start_pos), + AnyParameterId::Output(_) => { + // Find a port to connect to + let port = self.graph.inputs.iter().find_map(|(input_id, _)| { + let port_pos = port_locations[&AnyParameterId::Input(input_id)]; + if port_pos.distance(cursor_pos) < DISTANCE_TO_CONNECT { + Some(port_pos) + } else { + None + } + }); + if let Some(port_pos) = port { + (start_pos, port_pos) + } else { + (start_pos, cursor_pos) + } + } + AnyParameterId::Input(_) => { + let port = self.graph.outputs.iter().find_map(|(output_id, _)| { + let port_pos = port_locations[&AnyParameterId::Output(output_id)]; + if port_pos.distance(cursor_pos) < DISTANCE_TO_CONNECT { + Some(port_pos) + } else { + None + } + }); + if let Some(port_pos) = port { + (port_pos, start_pos) + } else { + (cursor_pos, start_pos) + } + } }; draw_connection(ui.painter(), src_pos, dst_pos, connection_color); } @@ -464,7 +494,15 @@ where }; let resp = ui.allocate_rect(port_rect, sense); - let port_color = if resp.hovered() { + + // Check if the distance between the port and the mouse is the distance to connect + let close_enough = if let Some(pointer_pos) = ui.ctx().pointer_hover_pos() { + port_rect.center().distance(pointer_pos) < DISTANCE_TO_CONNECT + } else { + false + }; + + let port_color = if close_enough { Color32::WHITE } else { port_type.data_type_color(user_state) @@ -491,7 +529,7 @@ where if origin_node != node_id { // Don't allow self-loops if graph.any_param_type(origin_param).unwrap() == port_type - && resp.hovered() + && close_enough && ui.input().pointer.any_released() { match (param_id, origin_param) { From a11c6c623d8c8a2b0df955b29d75258ee96c5a9e Mon Sep 17 00:00:00 2001 From: KOKI Date: Fri, 12 Aug 2022 18:24:54 +0900 Subject: [PATCH 2/2] Encapsulated snap_to_ports --- egui_node_graph/src/editor_ui.rs | 50 +++++++++++++++----------------- egui_node_graph/src/id_type.rs | 12 ++++++++ 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/egui_node_graph/src/editor_ui.rs b/egui_node_graph/src/editor_ui.rs index 505fdcf..d573e48 100644 --- a/egui_node_graph/src/editor_ui.rs +++ b/egui_node_graph/src/editor_ui.rs @@ -176,38 +176,34 @@ where let port_type = self.graph.any_param_type(*locator).unwrap(); let connection_color = port_type.data_type_color(&self.user_state); let start_pos = port_locations[locator]; - let (src_pos, dst_pos) = match locator { - AnyParameterId::Output(_) => { - // Find a port to connect to - let port = self.graph.inputs.iter().find_map(|(input_id, _)| { - let port_pos = port_locations[&AnyParameterId::Input(input_id)]; - if port_pos.distance(cursor_pos) < DISTANCE_TO_CONNECT { - Some(port_pos) - } else { - None - } - }); - if let Some(port_pos) = port { - (start_pos, port_pos) - } else { - (start_pos, cursor_pos) - } - } - AnyParameterId::Input(_) => { - let port = self.graph.outputs.iter().find_map(|(output_id, _)| { - let port_pos = port_locations[&AnyParameterId::Output(output_id)]; + + // Find a port to connect to + fn snap_to_ports, Value>( + ports: &SlotMap, + port_locations: &PortLocations, + cursor_pos: Pos2, + ) -> Pos2 { + ports + .iter() + .find_map(|(port_id, _)| { + let port_pos = port_locations[&port_id.into()]; if port_pos.distance(cursor_pos) < DISTANCE_TO_CONNECT { Some(port_pos) } else { None } - }); - if let Some(port_pos) = port { - (port_pos, start_pos) - } else { - (cursor_pos, start_pos) - } - } + }) + .unwrap_or(cursor_pos) + } + let (src_pos, dst_pos) = match locator { + AnyParameterId::Output(_) => ( + start_pos, + snap_to_ports(&self.graph.inputs, &port_locations, cursor_pos), + ), + AnyParameterId::Input(_) => ( + snap_to_ports(&self.graph.outputs, &port_locations, cursor_pos), + start_pos, + ), }; draw_connection(ui.painter(), src_pos, dst_pos, connection_color); } diff --git a/egui_node_graph/src/id_type.rs b/egui_node_graph/src/id_type.rs index 666ef92..5c272e1 100644 --- a/egui_node_graph/src/id_type.rs +++ b/egui_node_graph/src/id_type.rs @@ -23,3 +23,15 @@ impl AnyParameterId { } } } + +impl From for AnyParameterId { + fn from(output: OutputId) -> Self { + Self::Output(output) + } +} + +impl From for AnyParameterId { + fn from(input: InputId) -> Self { + Self::Input(input) + } +}