From 4a24a019285fb09ddba459b301c134bbd95d571c Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Sun, 22 Sep 2024 14:40:31 -0700 Subject: [PATCH] App: insert constraints consistently Also, write constructors for state objects. --- app-proto/sketch-outline/src/assembly.rs | 20 +++++++++++++- app-proto/sketch-outline/src/main.rs | 34 +++++++++++++----------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/app-proto/sketch-outline/src/assembly.rs b/app-proto/sketch-outline/src/assembly.rs index 4cd769c..a4c9a0f 100644 --- a/app-proto/sketch-outline/src/assembly.rs +++ b/app-proto/sketch-outline/src/assembly.rs @@ -1,7 +1,7 @@ use nalgebra::DVector; use rustc_hash::FxHashSet; use slab::Slab; -use sycamore::reactive::Signal; +use sycamore::prelude::*; #[derive(Clone, PartialEq)] pub struct Element { @@ -22,4 +22,22 @@ pub struct Constraint { pub struct Assembly { pub elements: Signal>, pub constraints: Signal> +} + +impl Assembly { + pub fn new() -> Assembly { + Assembly { + elements: create_signal(Slab::new()), + constraints: create_signal(Slab::new()) + } + } + + pub fn insert_constraint(&self, constraint: Constraint) { + let args = constraint.args; + let key = self.constraints.update(|csts| csts.insert(constraint)); + self.elements.update(|elts| { + elts[args.0].constraints.insert(key); + elts[args.1].constraints.insert(key); + }) + } } \ No newline at end of file diff --git a/app-proto/sketch-outline/src/main.rs b/app-proto/sketch-outline/src/main.rs index 20ae651..b86f979 100644 --- a/app-proto/sketch-outline/src/main.rs +++ b/app-proto/sketch-outline/src/main.rs @@ -7,7 +7,7 @@ use rustc_hash::FxHashSet; use slab::Slab; use sycamore::prelude::*; -use assembly::{Assembly, Element}; +use assembly::{Assembly, Constraint, Element}; use display::Display; use outline::Outline; @@ -17,32 +17,30 @@ struct AppState { selection: Signal> } +impl AppState { + fn new() -> AppState { + AppState { + assembly: Assembly::new(), + selection: create_signal(FxHashSet::default()) + } + } +} + fn main() { sycamore::render(|| { - let state = AppState { - assembly: Assembly { - elements: create_signal(Slab::new()), - constraints: create_signal(Slab::new()) - }, - selection: create_signal(FxHashSet::default()) - }; - state.assembly.elements.update( + let state = AppState::new(); + let key_a = state.assembly.elements.update( |elts| elts.insert( Element { id: String::from("wing_a"), label: String::from("Wing A"), color: [1.00_f32, 0.25_f32, 0.00_f32], rep: DVector::::from_column_slice(&[0.5, 0.5, 0.0, 0.5, -0.25]), - constraints: { - let mut set = FxHashSet::default(); - set.insert(1); - set.insert(2); - set - } + constraints: FxHashSet::default() } ) ); - state.assembly.elements.update( + let key_b = state.assembly.elements.update( |elts| elts.insert( Element { id: String::from("wing_b"), @@ -64,6 +62,10 @@ fn main() { } ) ); + state.assembly.insert_constraint(Constraint { + args: (key_a, key_b), + rep: 0.5 + }); provide_context(state); view! {