App: store assembly elements in slab

This commit is contained in:
Aaron Fenyes 2024-09-22 02:21:45 -07:00
parent 78f8ef8215
commit d121385c18
5 changed files with 45 additions and 40 deletions

View File

@ -12,6 +12,7 @@ itertools = "0.13.0"
js-sys = "0.3.70"
nalgebra = "0.33.0"
rustc-hash = "2.0.0"
slab = "0.4.9"
sycamore = "0.9.0-beta.3"
# The `console_error_panic_hook` crate provides better debugging of panics by

View File

@ -1,5 +1,5 @@
use nalgebra::DVector;
use rustc_hash::FxHashMap;
use slab::Slab;
use sycamore::reactive::Signal;
#[derive(Clone, PartialEq)]
@ -7,12 +7,12 @@ pub struct Element {
pub id: String,
pub label: String,
pub color: [f32; 3],
pub rep: DVector<f64>
pub rep: DVector<f64>,
pub key: usize
}
// a complete, view-independent description of an assembly
#[derive(Clone)]
pub struct Assembly {
// the order of the elements is arbitrary, and it could change at any time
pub elements: Signal<FxHashMap<String, Element>>
pub elements: Signal<Slab<Element>>
}

View File

@ -288,17 +288,17 @@ pub fn Display() -> View {
// get the assembly
let elements = state.assembly.elements.get_clone();
let element_iter = (&elements).values();
let element_iter = (&elements).into_iter().map(|(_, elt)| elt);
let reps_world: Vec<_> = element_iter.clone().map(|elt| &assembly_to_world * &elt.rep).collect();
let colors: Vec<_> = element_iter.clone().map(|elt|
if state.selection.with(|sel| sel.contains(&elt.id)) {
if state.selection.with(|sel| sel.contains(&elt.key)) {
elt.color.map(|ch| 0.2 + 0.8*ch)
} else {
elt.color
}
).collect();
let highlights: Vec<_> = element_iter.map(|elt|
if state.selection.with(|sel| sel.contains(&elt.id)) {
if state.selection.with(|sel| sel.contains(&elt.key)) {
1.0_f32
} else {
HIGHLIGHT

View File

@ -3,7 +3,8 @@ mod display;
mod outline;
use nalgebra::DVector;
use rustc_hash::{FxHashMap, FxHashSet};
use rustc_hash::FxHashSet;
use slab::Slab;
use sycamore::prelude::*;
use assembly::{Assembly, Element};
@ -13,50 +14,56 @@ use outline::Outline;
#[derive(Clone)]
struct AppState {
assembly: Assembly,
selection: Signal<FxHashSet<String>>
selection: Signal<FxHashSet<usize>>
}
fn main() {
sycamore::render(|| {
let state = AppState {
assembly: Assembly {
elements: create_signal(FxHashMap::default())
elements: create_signal(Slab::new())
},
selection: create_signal(FxHashSet::default())
};
state.assembly.elements.update(
|elts| elts.insert(
"wing_a".to_string(),
state.assembly.elements.update(|elts| {
let entry = elts.vacant_entry();
let key = entry.key();
entry.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])
rep: DVector::<f64>::from_column_slice(&[0.5, 0.5, 0.0, 0.5, -0.25]),
key: key
}
)
);
state.assembly.elements.update(
|elts| elts.insert(
"wing_b".to_string(),
});
state.assembly.elements.update(|elts| {
let entry = elts.vacant_entry();
let key = entry.key();
entry.insert(
Element {
id: String::from("wing_b"),
label: String::from("Wing B"),
color: [0.00_f32, 0.25_f32, 1.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]),
key: key
},
)
);
state.assembly.elements.update(
|elts| elts.insert(
"central".to_string(),
});
state.assembly.elements.update(|elts| {
let entry = elts.vacant_entry();
let key = entry.key();
entry.insert(
Element {
id: String::from("central"),
label: String::from("Central"),
color: [0.75_f32, 0.75_f32, 0.75_f32],
rep: DVector::<f64>::from_column_slice(&[0.0, 0.0, 0.0, 0.4, -0.625])
rep: DVector::<f64>::from_column_slice(&[0.0, 0.0, 0.0, 0.4, -0.625]),
key: key
}
)
);
});
provide_context(state);
view! {

View File

@ -12,8 +12,8 @@ pub fn Outline() -> View {
state.assembly.elements
.get_clone()
.into_iter()
.sorted_by_key(|(id, _)| id.clone())
.map(|(_, elt)| elt)
.sorted_by_key(|elt| elt.id.clone())
.collect()
});
@ -29,9 +29,8 @@ pub fn Outline() -> View {
view=|elt| {
let state = use_context::<AppState>();
let class = create_memo({
let id = elt.id.clone();
move || {
if state.selection.with(|sel| sel.contains(&id)) {
if state.selection.with(|sel| sel.contains(&elt.key)) {
"selected"
} else {
""
@ -50,37 +49,35 @@ pub fn Outline() -> View {
class=class.get(),
tabindex="0",
on:click={
let id = elt.id.clone();
move |event: MouseEvent| {
if event.shift_key() {
state.selection.update(|sel| {
if !sel.remove(&id) {
sel.insert(id.clone());
if !sel.remove(&elt.key) {
sel.insert(elt.key);
}
});
} else {
state.selection.update(|sel| {
sel.clear();
sel.insert(id.clone());
sel.insert(elt.key);
});
}
event.stop_propagation();
}
},
on:keydown={
let id = elt.id.clone();
move |event: KeyboardEvent| {
if event.key() == "Enter" {
if event.shift_key() {
state.selection.update(|sel| {
if !sel.remove(&id) {
sel.insert(id.clone());
if !sel.remove(&elt.key) {
sel.insert(elt.key);
}
});
} else {
state.selection.update(|sel| {
sel.clear();
sel.insert(id.clone());
sel.insert(elt.key);
});
}
event.prevent_default();
@ -93,7 +90,7 @@ pub fn Outline() -> View {
}
}
},
key=|elt| elt.id.clone()
key=|elt| elt.key
)
}
}