App: insert constraints consistently

Also, write constructors for state objects.
This commit is contained in:
Aaron Fenyes 2024-09-22 14:40:31 -07:00
parent 050e2373a6
commit 4a24a01928
2 changed files with 37 additions and 17 deletions

View File

@ -1,7 +1,7 @@
use nalgebra::DVector; use nalgebra::DVector;
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use slab::Slab; use slab::Slab;
use sycamore::reactive::Signal; use sycamore::prelude::*;
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
pub struct Element { pub struct Element {
@ -23,3 +23,21 @@ pub struct Assembly {
pub elements: Signal<Slab<Element>>, pub elements: Signal<Slab<Element>>,
pub constraints: Signal<Slab<Constraint>> pub constraints: Signal<Slab<Constraint>>
} }
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);
})
}
}

View File

@ -7,7 +7,7 @@ use rustc_hash::FxHashSet;
use slab::Slab; use slab::Slab;
use sycamore::prelude::*; use sycamore::prelude::*;
use assembly::{Assembly, Element}; use assembly::{Assembly, Constraint, Element};
use display::Display; use display::Display;
use outline::Outline; use outline::Outline;
@ -17,32 +17,30 @@ struct AppState {
selection: Signal<FxHashSet<usize>> selection: Signal<FxHashSet<usize>>
} }
impl AppState {
fn new() -> AppState {
AppState {
assembly: Assembly::new(),
selection: create_signal(FxHashSet::default())
}
}
}
fn main() { fn main() {
sycamore::render(|| { sycamore::render(|| {
let state = AppState { let state = AppState::new();
assembly: Assembly { let key_a = state.assembly.elements.update(
elements: create_signal(Slab::new()),
constraints: create_signal(Slab::new())
},
selection: create_signal(FxHashSet::default())
};
state.assembly.elements.update(
|elts| elts.insert( |elts| elts.insert(
Element { Element {
id: String::from("wing_a"), id: String::from("wing_a"),
label: String::from("Wing A"), label: String::from("Wing A"),
color: [1.00_f32, 0.25_f32, 0.00_f32], color: [1.00_f32, 0.25_f32, 0.00_f32],
rep: DVector::<f64>::from_column_slice(&[0.5, 0.5, 0.0, 0.5, -0.25]), rep: DVector::<f64>::from_column_slice(&[0.5, 0.5, 0.0, 0.5, -0.25]),
constraints: { constraints: FxHashSet::default()
let mut set = FxHashSet::default();
set.insert(1);
set.insert(2);
set
}
} }
) )
); );
state.assembly.elements.update( let key_b = state.assembly.elements.update(
|elts| elts.insert( |elts| elts.insert(
Element { Element {
id: String::from("wing_b"), 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); provide_context(state);
view! { view! {