App: store assembly elements in slab
This commit is contained in:
parent
78f8ef8215
commit
d121385c18
@ -12,6 +12,7 @@ itertools = "0.13.0"
|
|||||||
js-sys = "0.3.70"
|
js-sys = "0.3.70"
|
||||||
nalgebra = "0.33.0"
|
nalgebra = "0.33.0"
|
||||||
rustc-hash = "2.0.0"
|
rustc-hash = "2.0.0"
|
||||||
|
slab = "0.4.9"
|
||||||
sycamore = "0.9.0-beta.3"
|
sycamore = "0.9.0-beta.3"
|
||||||
|
|
||||||
# The `console_error_panic_hook` crate provides better debugging of panics by
|
# The `console_error_panic_hook` crate provides better debugging of panics by
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use nalgebra::DVector;
|
use nalgebra::DVector;
|
||||||
use rustc_hash::FxHashMap;
|
use slab::Slab;
|
||||||
use sycamore::reactive::Signal;
|
use sycamore::reactive::Signal;
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(Clone, PartialEq)]
|
||||||
@ -7,12 +7,12 @@ pub struct Element {
|
|||||||
pub id: String,
|
pub id: String,
|
||||||
pub label: String,
|
pub label: String,
|
||||||
pub color: [f32; 3],
|
pub color: [f32; 3],
|
||||||
pub rep: DVector<f64>
|
pub rep: DVector<f64>,
|
||||||
|
pub key: usize
|
||||||
}
|
}
|
||||||
|
|
||||||
// a complete, view-independent description of an assembly
|
// a complete, view-independent description of an assembly
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Assembly {
|
pub struct Assembly {
|
||||||
// the order of the elements is arbitrary, and it could change at any time
|
pub elements: Signal<Slab<Element>>
|
||||||
pub elements: Signal<FxHashMap<String, Element>>
|
|
||||||
}
|
}
|
@ -288,17 +288,17 @@ pub fn Display() -> View {
|
|||||||
|
|
||||||
// get the assembly
|
// get the assembly
|
||||||
let elements = state.assembly.elements.get_clone();
|
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 reps_world: Vec<_> = element_iter.clone().map(|elt| &assembly_to_world * &elt.rep).collect();
|
||||||
let colors: Vec<_> = element_iter.clone().map(|elt|
|
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)
|
elt.color.map(|ch| 0.2 + 0.8*ch)
|
||||||
} else {
|
} else {
|
||||||
elt.color
|
elt.color
|
||||||
}
|
}
|
||||||
).collect();
|
).collect();
|
||||||
let highlights: Vec<_> = element_iter.map(|elt|
|
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
|
1.0_f32
|
||||||
} else {
|
} else {
|
||||||
HIGHLIGHT
|
HIGHLIGHT
|
||||||
|
@ -3,7 +3,8 @@ mod display;
|
|||||||
mod outline;
|
mod outline;
|
||||||
|
|
||||||
use nalgebra::DVector;
|
use nalgebra::DVector;
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::FxHashSet;
|
||||||
|
use slab::Slab;
|
||||||
use sycamore::prelude::*;
|
use sycamore::prelude::*;
|
||||||
|
|
||||||
use assembly::{Assembly, Element};
|
use assembly::{Assembly, Element};
|
||||||
@ -13,50 +14,56 @@ use outline::Outline;
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
assembly: Assembly,
|
assembly: Assembly,
|
||||||
selection: Signal<FxHashSet<String>>
|
selection: Signal<FxHashSet<usize>>
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
sycamore::render(|| {
|
sycamore::render(|| {
|
||||||
let state = AppState {
|
let state = AppState {
|
||||||
assembly: Assembly {
|
assembly: Assembly {
|
||||||
elements: create_signal(FxHashMap::default())
|
elements: create_signal(Slab::new())
|
||||||
},
|
},
|
||||||
selection: create_signal(FxHashSet::default())
|
selection: create_signal(FxHashSet::default())
|
||||||
};
|
};
|
||||||
state.assembly.elements.update(
|
state.assembly.elements.update(|elts| {
|
||||||
|elts| elts.insert(
|
let entry = elts.vacant_entry();
|
||||||
"wing_a".to_string(),
|
let key = entry.key();
|
||||||
|
entry.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]),
|
||||||
|
key: key
|
||||||
}
|
}
|
||||||
)
|
);
|
||||||
);
|
});
|
||||||
state.assembly.elements.update(
|
state.assembly.elements.update(|elts| {
|
||||||
|elts| elts.insert(
|
let entry = elts.vacant_entry();
|
||||||
"wing_b".to_string(),
|
let key = entry.key();
|
||||||
|
entry.insert(
|
||||||
Element {
|
Element {
|
||||||
id: String::from("wing_b"),
|
id: String::from("wing_b"),
|
||||||
label: String::from("Wing B"),
|
label: String::from("Wing B"),
|
||||||
color: [0.00_f32, 0.25_f32, 1.00_f32],
|
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(
|
state.assembly.elements.update(|elts| {
|
||||||
|elts| elts.insert(
|
let entry = elts.vacant_entry();
|
||||||
"central".to_string(),
|
let key = entry.key();
|
||||||
|
entry.insert(
|
||||||
Element {
|
Element {
|
||||||
id: String::from("central"),
|
id: String::from("central"),
|
||||||
label: String::from("Central"),
|
label: String::from("Central"),
|
||||||
color: [0.75_f32, 0.75_f32, 0.75_f32],
|
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);
|
provide_context(state);
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
|
@ -12,8 +12,8 @@ pub fn Outline() -> View {
|
|||||||
state.assembly.elements
|
state.assembly.elements
|
||||||
.get_clone()
|
.get_clone()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.sorted_by_key(|(id, _)| id.clone())
|
|
||||||
.map(|(_, elt)| elt)
|
.map(|(_, elt)| elt)
|
||||||
|
.sorted_by_key(|elt| elt.id.clone())
|
||||||
.collect()
|
.collect()
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -29,9 +29,8 @@ pub fn Outline() -> View {
|
|||||||
view=|elt| {
|
view=|elt| {
|
||||||
let state = use_context::<AppState>();
|
let state = use_context::<AppState>();
|
||||||
let class = create_memo({
|
let class = create_memo({
|
||||||
let id = elt.id.clone();
|
|
||||||
move || {
|
move || {
|
||||||
if state.selection.with(|sel| sel.contains(&id)) {
|
if state.selection.with(|sel| sel.contains(&elt.key)) {
|
||||||
"selected"
|
"selected"
|
||||||
} else {
|
} else {
|
||||||
""
|
""
|
||||||
@ -50,37 +49,35 @@ pub fn Outline() -> View {
|
|||||||
class=class.get(),
|
class=class.get(),
|
||||||
tabindex="0",
|
tabindex="0",
|
||||||
on:click={
|
on:click={
|
||||||
let id = elt.id.clone();
|
|
||||||
move |event: MouseEvent| {
|
move |event: MouseEvent| {
|
||||||
if event.shift_key() {
|
if event.shift_key() {
|
||||||
state.selection.update(|sel| {
|
state.selection.update(|sel| {
|
||||||
if !sel.remove(&id) {
|
if !sel.remove(&elt.key) {
|
||||||
sel.insert(id.clone());
|
sel.insert(elt.key);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
state.selection.update(|sel| {
|
state.selection.update(|sel| {
|
||||||
sel.clear();
|
sel.clear();
|
||||||
sel.insert(id.clone());
|
sel.insert(elt.key);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
event.stop_propagation();
|
event.stop_propagation();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
on:keydown={
|
on:keydown={
|
||||||
let id = elt.id.clone();
|
|
||||||
move |event: KeyboardEvent| {
|
move |event: KeyboardEvent| {
|
||||||
if event.key() == "Enter" {
|
if event.key() == "Enter" {
|
||||||
if event.shift_key() {
|
if event.shift_key() {
|
||||||
state.selection.update(|sel| {
|
state.selection.update(|sel| {
|
||||||
if !sel.remove(&id) {
|
if !sel.remove(&elt.key) {
|
||||||
sel.insert(id.clone());
|
sel.insert(elt.key);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
state.selection.update(|sel| {
|
state.selection.update(|sel| {
|
||||||
sel.clear();
|
sel.clear();
|
||||||
sel.insert(id.clone());
|
sel.insert(elt.key);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
event.prevent_default();
|
event.prevent_default();
|
||||||
@ -93,7 +90,7 @@ pub fn Outline() -> View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
key=|elt| elt.id.clone()
|
key=|elt| elt.key
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user