From 4804b859c12b829fe38d5273d5563b72cf1930b9 Mon Sep 17 00:00:00 2001 From: Setzer22 Date: Tue, 22 Feb 2022 15:32:55 +0100 Subject: [PATCH] Move generic parameters to associated types in NodeDataTrait --- egui_node_graph/src/editor_ui.rs | 29 ++++++++++++++++++++--------- egui_node_graph/src/traits.rs | 10 +++++++--- egui_node_graph_example/src/app.rs | 6 ++++-- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/egui_node_graph/src/editor_ui.rs b/egui_node_graph/src/editor_ui.rs index 8fc52fd..33edcc9 100644 --- a/egui_node_graph/src/editor_ui.rs +++ b/egui_node_graph/src/editor_ui.rs @@ -44,10 +44,16 @@ pub struct GraphNodeWidget<'a, NodeData, DataType, ValueType> { impl GraphEditorState where - NodeData: NodeDataTrait, + NodeData: NodeDataTrait< + Response = UserResponse, + UserState = UserState, + DataType = DataType, + ValueType = ValueType, + >, UserResponse: UserResponseTrait, ValueType: WidgetValueTrait, - NodeTemplate: NodeTemplateTrait, + NodeTemplate: + NodeTemplateTrait, DataType: DataTypeTrait, { #[must_use] @@ -241,18 +247,19 @@ where impl<'a, NodeData, DataType, ValueType, UserResponse, UserState> GraphNodeWidget<'a, NodeData, DataType, ValueType> where - NodeData: NodeDataTrait, + NodeData: NodeDataTrait< + Response = UserResponse, + UserState = UserState, + DataType = DataType, + ValueType = ValueType, + >, UserResponse: UserResponseTrait, ValueType: WidgetValueTrait, DataType: DataTypeTrait, { pub const MAX_NODE_SIZE: [f32; 2] = [200.0, 200.0]; - pub fn show( - self, - ui: &mut Ui, - user_state: &UserState, - ) -> Vec> { + pub fn show(self, ui: &mut Ui, user_state: &UserState) -> Vec> { let mut child_ui = ui.child_ui_with_id_source( Rect::from_min_size(*self.position + self.pan, Self::MAX_NODE_SIZE.into()), Layout::default(), @@ -264,7 +271,11 @@ where /// Draws this node. Also fills in the list of port locations with all of its ports. /// Returns responses indicating multiple events. - fn show_graph_node(self, ui: &mut Ui, user_state: &UserState) -> Vec> { + fn show_graph_node( + self, + ui: &mut Ui, + user_state: &UserState, + ) -> Vec> { let margin = egui::vec2(15.0, 5.0); let mut responses = Vec::new(); diff --git a/egui_node_graph/src/traits.rs b/egui_node_graph/src/traits.rs index c6f52d8..571ac77 100644 --- a/egui_node_graph/src/traits.rs +++ b/egui_node_graph/src/traits.rs @@ -28,13 +28,17 @@ where type Response; /// Must be set to the custom user `UserState` type type UserState; + /// Must be set to the custom user `DataType` type + type DataType; + /// Must be set to the custom user `ValueType` type + type ValueType; /// Additional UI elements to draw in the nodes, after the parameters. - fn bottom_ui( + fn bottom_ui( &self, ui: &mut egui::Ui, node_id: NodeId, - graph: &Graph, + graph: &Graph, user_state: &Self::UserState, ) -> Vec> where @@ -82,4 +86,4 @@ pub trait NodeTemplateTrait: Clone { /// The custom user response types when drawing nodes in the graph must /// implement this trait. -pub trait UserResponseTrait: Clone + Copy + std::fmt::Debug + PartialEq + Eq {} \ No newline at end of file +pub trait UserResponseTrait: Clone + Copy + std::fmt::Debug + PartialEq + Eq {} diff --git a/egui_node_graph_example/src/app.rs b/egui_node_graph_example/src/app.rs index 81d92dc..20273b1 100644 --- a/egui_node_graph_example/src/app.rs +++ b/egui_node_graph_example/src/app.rs @@ -242,17 +242,19 @@ impl UserResponseTrait for MyResponse {} impl NodeDataTrait for MyNodeData { type Response = MyResponse; type UserState = MyGraphState; + type DataType = MyDataType; + type ValueType = MyValueType; // This method will be called when drawing each node. This allows adding // extra ui elements inside the nodes. In this case, we create an "active" // button which introduces the concept of having an active node in the // graph. This is done entirely from user code with no modifications to the // node graph library. - fn bottom_ui( + fn bottom_ui( &self, ui: &mut egui::Ui, node_id: NodeId, - _graph: &Graph, + _graph: &Graph, user_state: &Self::UserState, ) -> Vec> where