Skip to content

Commit

Permalink
fix: store node state in NodeInstance instead of adjacently, in the g…
Browse files Browse the repository at this point in the history
…raph
  • Loading branch information
vgarleanu committed May 31, 2024
1 parent dcbf1a3 commit d7e6330
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/model/backend/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub struct IntInput {
pub max: f32,
#[serde(default = "float_step")]
pub step: f32,
#[serde(skip)]
#[serde(default = "float_step")]
pub state: f32,
}

Expand All @@ -222,7 +222,7 @@ pub struct FloatInput {
pub max: f32,
#[serde(default = "float_step")]
pub step: f32,
#[serde(skip)]
#[serde(default = "float_step")]
pub state: f32,
}

Expand All @@ -231,7 +231,7 @@ pub struct StringInput {
#[serde(default = "bool_false")]
pub multiline: bool,
pub default: Option<String>,
#[serde(skip)]
#[serde(default)]
pub state: String,
}

Expand Down
20 changes: 8 additions & 12 deletions src/model/tabs/project/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ use serde::Serialize;
use slint::SharedString;
use std::collections::HashMap;

pub type NodeId = usize;
pub type FieldStates = HashMap<NodeId, Vec<(String, NodeField)>>;

#[derive(Debug, Serialize, Deserialize)]
pub struct Graph {
nodes: Vec<NodeInstance>,
links: Vec<Link>,
zoom: f32,
offset: (f32, f32),
field_states: FieldStates,
}

impl Graph {
Expand All @@ -23,7 +19,6 @@ impl Graph {
links: vec![],
zoom: 2.,
offset: (0., 0.),
field_states: FieldStates::new(),
}
}

Expand All @@ -47,15 +42,15 @@ impl Graph {
self.nodes.push(NodeInstance {
ty: id,
pos: (20. - self.offset.0, 20. - self.offset.1),
state,
image: None,
});

self.field_states.insert(self.nodes.len() - 1, state);
}

pub fn get_state_mut(&mut self, id: NodeId, input: &str) -> Option<&mut NodeField> {
self.field_states
.get_mut(&id)?
pub fn get_state_mut(&mut self, id: usize, input: &str) -> Option<&mut NodeField> {
self.nodes
.get_mut(id)?
.state
.iter_mut()
.find(|(lbl, _)| lbl == input)
.map(|(_, field)| field)
Expand All @@ -79,8 +74,8 @@ impl Graph {
&self.nodes
}

pub fn get_state(&self, id: NodeId) -> Option<&Vec<(String, NodeField)>> {
self.field_states.get(&id)
pub fn get_state(&self, id: usize) -> Option<&Vec<(String, NodeField)>> {
Some(&self.nodes.get(id)?.state)
}

pub fn get_node(&self, idx: usize) -> Option<&NodeInstance> {
Expand All @@ -100,6 +95,7 @@ impl Graph {
pub struct NodeInstance {
pub ty: NodeType,
pub pos: (f32, f32),
pub state: Vec<(String, NodeField)>,
#[serde(skip)]
pub image: Option<image::RgbImage>,
}
Expand Down

0 comments on commit d7e6330

Please sign in to comment.