Change UserState to outside of GraphEditorState

This commit is contained in:
KOKI 2022-08-26 02:40:59 +09:00
parent eeecd63787
commit 84c32c720e
3 changed files with 19 additions and 15 deletions

View File

@ -82,6 +82,7 @@ where
&mut self, &mut self,
ui: &mut Ui, ui: &mut Ui,
all_kinds: impl NodeTemplateIter<Item = NodeTemplate>, all_kinds: impl NodeTemplateIter<Item = NodeTemplate>,
user_state: &UserState,
) -> GraphResponse<UserResponse, NodeData> { ) -> GraphResponse<UserResponse, NodeData> {
// This causes the graph editor to use as much free space as it can. // 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 // (so for windows it will use up to the resizeably set limit
@ -124,7 +125,7 @@ where
.unwrap_or(false), .unwrap_or(false),
pan: self.pan_zoom.pan + editor_rect.min.to_vec2(), pan: self.pan_zoom.pan + editor_rect.min.to_vec2(),
} }
.show(ui, &self.user_state); .show(ui, &user_state);
// Actions executed later // Actions executed later
delayed_responses.extend(responses); delayed_responses.extend(responses);
@ -147,7 +148,7 @@ where
let new_node = self.graph.add_node( let new_node = self.graph.add_node(
node_kind.node_graph_label(), node_kind.node_graph_label(),
node_kind.user_data(), node_kind.user_data(),
|graph, node_id| node_kind.build_node(graph, &self.user_state, node_id), |graph, node_id| node_kind.build_node(graph, &user_state, node_id),
); );
self.node_positions.insert( self.node_positions.insert(
new_node, new_node,
@ -174,7 +175,7 @@ where
/* Draw connections */ /* Draw connections */
if let Some((_, ref locator)) = self.connection_in_progress { if let Some((_, ref locator)) = self.connection_in_progress {
let port_type = self.graph.any_param_type(*locator).unwrap(); let port_type = self.graph.any_param_type(*locator).unwrap();
let connection_color = port_type.data_type_color(&self.user_state); let connection_color = port_type.data_type_color(&user_state);
let start_pos = port_locations[locator]; let start_pos = port_locations[locator];
// Find a port to connect to // Find a port to connect to
@ -213,7 +214,7 @@ where
.graph .graph
.any_param_type(AnyParameterId::Output(output)) .any_param_type(AnyParameterId::Output(output))
.unwrap(); .unwrap();
let connection_color = port_type.data_type_color(&self.user_state); let connection_color = port_type.data_type_color(&user_state);
let src_pos = port_locations[&AnyParameterId::Output(output)]; let src_pos = port_locations[&AnyParameterId::Output(output)];
let dst_pos = port_locations[&AnyParameterId::Input(input)]; let dst_pos = port_locations[&AnyParameterId::Input(input)];
draw_connection(ui.painter(), src_pos, dst_pos, connection_color); draw_connection(ui.painter(), src_pos, dst_pos, connection_color);

View File

@ -1,4 +1,5 @@
use super::*; use super::*;
use std::marker::PhantomData;
#[cfg(feature = "persistence")] #[cfg(feature = "persistence")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -29,13 +30,13 @@ pub struct GraphEditorState<NodeData, DataType, ValueType, NodeTemplate, UserSta
pub node_finder: Option<NodeFinder<NodeTemplate>>, pub node_finder: Option<NodeFinder<NodeTemplate>>,
/// The panning of the graph viewport. /// The panning of the graph viewport.
pub pan_zoom: PanZoom, pub pan_zoom: PanZoom,
pub user_state: UserState, pub _user_state: PhantomData<fn() -> UserState>,
} }
impl<NodeData, DataType, ValueType, NodeKind, UserState> impl<NodeData, DataType, ValueType, NodeKind, UserState>
GraphEditorState<NodeData, DataType, ValueType, NodeKind, UserState> GraphEditorState<NodeData, DataType, ValueType, NodeKind, UserState>
{ {
pub fn new(default_zoom: f32, user_state: UserState) -> Self { pub fn new(default_zoom: f32) -> Self {
Self { Self {
graph: Graph::new(), graph: Graph::new(),
node_order: Vec::new(), node_order: Vec::new(),
@ -47,7 +48,7 @@ impl<NodeData, DataType, ValueType, NodeKind, UserState>
pan: egui::Vec2::ZERO, pan: egui::Vec2::ZERO,
zoom: default_zoom, zoom: default_zoom,
}, },
user_state, _user_state: PhantomData,
} }
} }
} }

View File

@ -340,12 +340,15 @@ pub struct NodeGraphExample {
// The `GraphEditorState` is the top-level object. You "register" all your // The `GraphEditorState` is the top-level object. You "register" all your
// custom types by specifying it as its generic parameters. // custom types by specifying it as its generic parameters.
state: MyEditorState, state: MyEditorState,
user_state: MyGraphState,
} }
impl Default for NodeGraphExample { impl Default for NodeGraphExample {
fn default() -> Self { fn default() -> Self {
Self { Self {
state: GraphEditorState::new(1.0, MyGraphState::default()), state: GraphEditorState::new(1.0),
user_state: MyGraphState::default(),
} }
} }
} }
@ -361,7 +364,8 @@ impl eframe::App for NodeGraphExample {
}); });
let graph_response = egui::CentralPanel::default() let graph_response = egui::CentralPanel::default()
.show(ctx, |ui| { .show(ctx, |ui| {
self.state.draw_graph_editor(ui, AllMyNodeTemplates) self.state
.draw_graph_editor(ui, AllMyNodeTemplates, &self.user_state)
}) })
.inner; .inner;
for node_response in graph_response.node_responses { for node_response in graph_response.node_responses {
@ -370,15 +374,13 @@ impl eframe::App for NodeGraphExample {
// connection is created // connection is created
if let NodeResponse::User(user_event) = node_response { if let NodeResponse::User(user_event) = node_response {
match user_event { match user_event {
MyResponse::SetActiveNode(node) => { MyResponse::SetActiveNode(node) => self.user_state.active_node = Some(node),
self.state.user_state.active_node = Some(node) MyResponse::ClearActiveNode => self.user_state.active_node = None,
}
MyResponse::ClearActiveNode => self.state.user_state.active_node = None,
} }
} }
} }
if let Some(node) = self.state.user_state.active_node { if let Some(node) = self.user_state.active_node {
if self.state.graph.nodes.contains_key(node) { if self.state.graph.nodes.contains_key(node) {
let text = match evaluate_node(&self.state.graph, node, &mut HashMap::new()) { let text = match evaluate_node(&self.state.graph, node, &mut HashMap::new()) {
Ok(value) => format!("The result is: {:?}", value), Ok(value) => format!("The result is: {:?}", value),
@ -392,7 +394,7 @@ impl eframe::App for NodeGraphExample {
egui::Color32::WHITE, egui::Color32::WHITE,
); );
} else { } else {
self.state.user_state.active_node = None; self.user_state.active_node = None;
} }
} }
} }