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 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<Slab<Element>>,
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 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<FxHashSet<usize>>
}
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::<f64>::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! {