App: store selection in hash map
Switch `Assembly.elements` to a hash map too, since that's probably closer to what we'll want in the future.
This commit is contained in:
parent
96afad0c97
commit
96f8b6b5f3
@ -11,6 +11,7 @@ default = ["console_error_panic_hook"]
|
|||||||
itertools = "0.13.0"
|
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"
|
||||||
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,4 +1,5 @@
|
|||||||
use nalgebra::DVector;
|
use nalgebra::DVector;
|
||||||
|
use rustc_hash::FxHashMap;
|
||||||
use sycamore::reactive::Signal;
|
use sycamore::reactive::Signal;
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(Clone, PartialEq)]
|
||||||
@ -6,16 +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>
|
||||||
|
|
||||||
/* TO DO */
|
|
||||||
// does this belong in the element data?
|
|
||||||
pub selected: Signal<bool>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
// the order of the elements is arbitrary, and it could change at any time
|
||||||
pub elements: Signal<Vec<Element>>
|
pub elements: Signal<FxHashMap<String, Element>>
|
||||||
}
|
}
|
@ -102,8 +102,7 @@ pub fn Display() -> View {
|
|||||||
// change listener
|
// change listener
|
||||||
let scene_changed = create_signal(true);
|
let scene_changed = create_signal(true);
|
||||||
create_effect(move || {
|
create_effect(move || {
|
||||||
let elements = state.assembly.elements.get_clone();
|
state.selection.track();
|
||||||
for elt in elements { elt.selected.track(); }
|
|
||||||
scene_changed.set(true);
|
scene_changed.set(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -289,17 +288,21 @@ 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).into_iter();
|
let element_iter = (&elements).values();
|
||||||
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 elt.selected.get() {
|
if state.selection.with(|sel| sel.contains(&elt.id)) {
|
||||||
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 elt.selected.get() { 1.0_f32 } else { HIGHLIGHT }
|
if state.selection.with(|sel| sel.contains(&elt.id)) {
|
||||||
|
1.0_f32
|
||||||
|
} else {
|
||||||
|
HIGHLIGHT
|
||||||
|
}
|
||||||
).collect();
|
).collect();
|
||||||
|
|
||||||
// set the resolution
|
// set the resolution
|
||||||
|
@ -3,6 +3,7 @@ mod display;
|
|||||||
mod outline;
|
mod outline;
|
||||||
|
|
||||||
use nalgebra::DVector;
|
use nalgebra::DVector;
|
||||||
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
use sycamore::prelude::*;
|
use sycamore::prelude::*;
|
||||||
|
|
||||||
use assembly::{Assembly, Element};
|
use assembly::{Assembly, Element};
|
||||||
@ -11,40 +12,52 @@ use outline::Outline;
|
|||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
assembly: Assembly
|
assembly: Assembly,
|
||||||
|
selection: Signal<FxHashSet<String>>
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
sycamore::render(|| {
|
sycamore::render(|| {
|
||||||
provide_context(
|
let state = AppState {
|
||||||
AppState {
|
|
||||||
assembly: Assembly {
|
assembly: Assembly {
|
||||||
elements: create_signal(vec![
|
elements: create_signal(FxHashMap::default())
|
||||||
|
},
|
||||||
|
selection: create_signal(FxHashSet::default())
|
||||||
|
};
|
||||||
|
state.assembly.elements.update(
|
||||||
|
|elts| elts.insert(
|
||||||
|
"wing_a".to_string(),
|
||||||
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])
|
||||||
selected: create_signal(false)
|
}
|
||||||
},
|
)
|
||||||
|
);
|
||||||
|
state.assembly.elements.update(
|
||||||
|
|elts| elts.insert(
|
||||||
|
"wing_b".to_string(),
|
||||||
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])
|
||||||
selected: create_signal(false)
|
|
||||||
},
|
},
|
||||||
|
)
|
||||||
|
);
|
||||||
|
state.assembly.elements.update(
|
||||||
|
|elts| elts.insert(
|
||||||
|
"central".to_string(),
|
||||||
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])
|
||||||
selected: create_signal(false)
|
|
||||||
}
|
|
||||||
])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
provide_context(state);
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
Outline {}
|
Outline {}
|
||||||
|
@ -6,25 +6,33 @@ use crate::AppState;
|
|||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn Outline() -> View {
|
pub fn Outline() -> View {
|
||||||
let state = use_context::<AppState>();
|
|
||||||
|
|
||||||
// sort the elements alphabetically by ID
|
// sort the elements alphabetically by ID
|
||||||
let elements_sorted = create_memo(move ||
|
let elements_sorted = create_memo(|| {
|
||||||
|
let state = use_context::<AppState>();
|
||||||
state.assembly.elements
|
state.assembly.elements
|
||||||
.get_clone()
|
.get_clone()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.sorted_by_key(|elt| elt.id.clone())
|
.sorted_by_key(|(id, _)| id.clone())
|
||||||
|
.map(|(_, elt)| elt)
|
||||||
.collect()
|
.collect()
|
||||||
);
|
});
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
ul {
|
ul {
|
||||||
Keyed(
|
Keyed(
|
||||||
list=elements_sorted,
|
list=elements_sorted,
|
||||||
view=|elt| {
|
view=|elt| {
|
||||||
let class = create_memo(move ||
|
let state = use_context::<AppState>();
|
||||||
if elt.selected.get() { "selected" } else { "" }
|
let class = create_memo({
|
||||||
);
|
let id = elt.id.clone();
|
||||||
|
move || {
|
||||||
|
if state.selection.with(|sel| sel.contains(&id)) {
|
||||||
|
"selected"
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
let label = elt.label.clone();
|
let label = elt.label.clone();
|
||||||
let rep_components = elt.rep.iter().map(|u| {
|
let rep_components = elt.rep.iter().map(|u| {
|
||||||
let u_coord = u.to_string().replace("-", "\u{2212}");
|
let u_coord = u.to_string().replace("-", "\u{2212}");
|
||||||
@ -36,15 +44,29 @@ pub fn Outline() -> View {
|
|||||||
li(
|
li(
|
||||||
class=class.get(),
|
class=class.get(),
|
||||||
tabindex="0",
|
tabindex="0",
|
||||||
on:click=move |_| {
|
on:click={
|
||||||
elt.selected.set_fn(|sel| !sel);
|
let id = elt.id.clone();
|
||||||
|
move |_| {
|
||||||
|
state.selection.update(|sel| {
|
||||||
|
if !sel.remove(&id) {
|
||||||
|
sel.insert(id.clone());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
on:keydown=move |event: KeyboardEvent| {
|
on:keydown={
|
||||||
|
let id = elt.id.clone();
|
||||||
|
move |event: KeyboardEvent| {
|
||||||
if event.key() == "Enter" {
|
if event.key() == "Enter" {
|
||||||
elt.selected.set_fn(|sel| !sel);
|
state.selection.update(|sel| {
|
||||||
|
if !sel.remove(&id) {
|
||||||
|
sel.insert(id.clone());
|
||||||
|
}
|
||||||
|
});
|
||||||
event.prevent_default();
|
event.prevent_default();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
) {
|
) {
|
||||||
div(class="elt-label") { (label) }
|
div(class="elt-label") { (label) }
|
||||||
div(class="elt-rep") { (rep_components) }
|
div(class="elt-rep") { (rep_components) }
|
||||||
|
Loading…
Reference in New Issue
Block a user