More serde derives and adjust NodeTemplateIter return value

This commit is contained in:
Setzer22 2022-02-24 20:10:25 +01:00
parent 4804b859c1
commit c88aae2110
5 changed files with 16 additions and 20 deletions

View File

@ -2,6 +2,7 @@ slotmap::new_key_type! { pub struct NodeId; }
slotmap::new_key_type! { pub struct InputId; } slotmap::new_key_type! { pub struct InputId; }
slotmap::new_key_type! { pub struct OutputId; } slotmap::new_key_type! { pub struct OutputId; }
#[cfg_attr(feature = "persistence", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum AnyParameterId { pub enum AnyParameterId {
Input(InputId), Input(InputId),

View File

@ -4,6 +4,7 @@ use crate::{color_hex_utils::*, NodeTemplateIter, NodeTemplateTrait};
use egui::*; use egui::*;
#[cfg_attr(feature = "persistence", derive(serde::Serialize, serde::Deserialize))]
pub struct NodeFinder<NodeTemplate> { pub struct NodeFinder<NodeTemplate> {
pub query: String, pub query: String,
/// Reset every frame. When set, the node finder will be moved at that position /// Reset every frame. When set, the node finder will be moved at that position
@ -56,14 +57,13 @@ where
Frame::default().margin(vec2(10.0, 10.0)).show(ui, |ui| { Frame::default().margin(vec2(10.0, 10.0)).show(ui, |ui| {
for kind in all_kinds.all_kinds() { for kind in all_kinds.all_kinds() {
let kind_name = kind.node_finder_label(); let kind_name = kind.node_finder_label().to_string();
if kind_name.contains(self.query.as_str()) { if kind_name.contains(self.query.as_str()) {
if query_submit {
submitted_archetype = Some(kind);
query_submit = false;
}
if ui.selectable_label(false, kind_name).clicked() { if ui.selectable_label(false, kind_name).clicked() {
submitted_archetype = Some(kind); submitted_archetype = Some(kind);
} else if query_submit {
submitted_archetype = Some(kind);
query_submit = false;
} }
} }
} }
@ -71,6 +71,6 @@ where
}); });
}); });
submitted_archetype.cloned() submitted_archetype
} }
} }

View File

@ -50,7 +50,7 @@ where
/// the node finder. /// the node finder.
pub trait NodeTemplateIter { pub trait NodeTemplateIter {
type Item; type Item;
fn all_kinds(&self) -> Box<dyn Iterator<Item = &Self::Item> + '_>; fn all_kinds(&self) -> Vec<Self::Item>;
} }
/// This trait must be implemented by the `NodeTemplate` generic parameter of /// This trait must be implemented by the `NodeTemplate` generic parameter of

View File

@ -10,6 +10,7 @@ pub struct PanZoom {
pub zoom: f32, pub zoom: f32,
} }
#[cfg_attr(feature = "persistence", derive(Serialize, Deserialize))]
pub struct GraphEditorState<NodeData, DataType, ValueType, NodeTemplate, UserState> { pub struct GraphEditorState<NodeData, DataType, ValueType, NodeTemplate, UserState> {
pub graph: Graph<NodeData, DataType, ValueType>, pub graph: Graph<NodeData, DataType, ValueType>,
/// Nodes are drawn in this order. Draw order is important because nodes /// Nodes are drawn in this order. Draw order is important because nodes

View File

@ -195,22 +195,16 @@ pub struct AllMyNodeTemplates;
impl NodeTemplateIter for AllMyNodeTemplates { impl NodeTemplateIter for AllMyNodeTemplates {
type Item = MyNodeTemplate; type Item = MyNodeTemplate;
fn all_kinds(&self) -> Box<dyn Iterator<Item = &Self::Item> + '_> { fn all_kinds(&self) -> Vec<Self::Item> {
// This function must return a list of node kinds, which the node finder // This function must return a list of node kinds, which the node finder
// will use to display it to the user. Crates like strum can reduce the // will use to display it to the user. Crates like strum can reduce the
// boilerplate in enumerating all variants of an enum. // boilerplate in enumerating all variants of an enum.
// vec![
// The Box here is required because traits in Rust cannot be generic MyNodeTemplate::AddScalar,
// over return parameters, so you can't return an iterator. MyNodeTemplate::SubtractScalar,
Box::new( MyNodeTemplate::VectorTimesScalar,
[ MyNodeTemplate::AddVector,
MyNodeTemplate::AddScalar, ]
MyNodeTemplate::SubtractScalar,
MyNodeTemplate::VectorTimesScalar,
MyNodeTemplate::AddVector,
]
.iter(),
)
} }
} }