forked from StudioInfinity/dyna3
feat: Curvature regulators (#80)
Prior to this commit, there's only one kind of regulator: the one that regulates the inversive distance between two spheres (or, more generally, the Lorentz product between two element representation vectors). Adds a new kind of regulator, which regulates the curvature of a sphere (issue #55). In the process, introduces a general framework based on new traits for organizing and sharing code between different kinds of regulators. Co-authored-by: Aaron Fenyes <aaron.fenyes@fareycircles.ooo> Reviewed-on: StudioInfinity/dyna3#80 Co-authored-by: Vectornaut <vectornaut@nobody@nowhere.net> Co-committed-by: Vectornaut <vectornaut@nobody@nowhere.net>
This commit is contained in:
parent
23ba5acad7
commit
360ce12d8b
6 changed files with 640 additions and 331 deletions
|
@ -1,4 +1,5 @@
|
|||
use itertools::Itertools;
|
||||
use std::rc::Rc;
|
||||
use sycamore::prelude::*;
|
||||
use web_sys::{
|
||||
KeyboardEvent,
|
||||
|
@ -9,24 +10,38 @@ use web_sys::{
|
|||
use crate::{
|
||||
AppState,
|
||||
assembly,
|
||||
assembly::{ElementKey, Regulator, RegulatorKey},
|
||||
assembly::{
|
||||
ElementKey,
|
||||
HalfCurvatureRegulator,
|
||||
InversiveDistanceRegulator,
|
||||
Regulator,
|
||||
RegulatorKey
|
||||
},
|
||||
specified::SpecifiedValue
|
||||
};
|
||||
|
||||
// an editable view of a regulator
|
||||
#[component(inline_props)]
|
||||
fn RegulatorInput(regulator: Regulator) -> View {
|
||||
fn RegulatorInput(regulator: Rc<dyn Regulator>) -> View {
|
||||
// get the regulator's measurement and set point signals
|
||||
let measurement = regulator.measurement();
|
||||
let set_point = regulator.set_point();
|
||||
|
||||
// the `valid` signal tracks whether the last entered value is a valid set
|
||||
// point specification
|
||||
let valid = create_signal(true);
|
||||
|
||||
// the `value` signal holds the current set point specification
|
||||
let value = create_signal(
|
||||
regulator.set_point.with_untracked(|set_pt| set_pt.spec.clone())
|
||||
set_point.with_untracked(|set_pt| set_pt.spec.clone())
|
||||
);
|
||||
|
||||
// this closure resets the input value to the regulator's set point
|
||||
// specification
|
||||
// this `reset_value` closure resets the input value to the regulator's set
|
||||
// point specification
|
||||
let reset_value = move || {
|
||||
batch(|| {
|
||||
valid.set(true);
|
||||
value.set(regulator.set_point.with(|set_pt| set_pt.spec.clone()));
|
||||
value.set(set_point.with(|set_pt| set_pt.spec.clone()));
|
||||
})
|
||||
};
|
||||
|
||||
|
@ -39,7 +54,7 @@ fn RegulatorInput(regulator: Regulator) -> View {
|
|||
r#type="text",
|
||||
class=move || {
|
||||
if valid.get() {
|
||||
regulator.set_point.with(|set_pt| {
|
||||
set_point.with(|set_pt| {
|
||||
if set_pt.is_present() {
|
||||
"regulator-input constraint"
|
||||
} else {
|
||||
|
@ -50,13 +65,13 @@ fn RegulatorInput(regulator: Regulator) -> View {
|
|||
"regulator-input invalid"
|
||||
}
|
||||
},
|
||||
placeholder=regulator.measurement.with(|result| result.to_string()),
|
||||
placeholder=measurement.with(|result| result.to_string()),
|
||||
bind:value=value,
|
||||
on:change=move |_| {
|
||||
valid.set(
|
||||
match SpecifiedValue::try_from(value.get_clone_untracked()) {
|
||||
Ok(set_pt) => {
|
||||
regulator.set_point.set(set_pt);
|
||||
set_point.set(set_pt);
|
||||
true
|
||||
}
|
||||
Err(_) => false
|
||||
|
@ -75,26 +90,53 @@ fn RegulatorInput(regulator: Regulator) -> View {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait OutlineItem {
|
||||
fn outline_item(self: Rc<Self>, element_key: ElementKey) -> View;
|
||||
}
|
||||
|
||||
impl OutlineItem for InversiveDistanceRegulator {
|
||||
fn outline_item(self: Rc<Self>, element_key: ElementKey) -> View {
|
||||
let state = use_context::<AppState>();
|
||||
let other_subject = if self.subjects[0] == element_key {
|
||||
self.subjects[1]
|
||||
} else {
|
||||
self.subjects[0]
|
||||
};
|
||||
let other_subject_label = state.assembly.elements.with(
|
||||
|elts| elts[other_subject].label.clone()
|
||||
);
|
||||
view! {
|
||||
li(class="regulator") {
|
||||
div(class="regulator-label") { (other_subject_label) }
|
||||
div(class="regulator-type") { "Inversive distance" }
|
||||
RegulatorInput(regulator=self)
|
||||
div(class="status")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl OutlineItem for HalfCurvatureRegulator {
|
||||
fn outline_item(self: Rc<Self>, _element_key: ElementKey) -> View {
|
||||
view! {
|
||||
li(class="regulator") {
|
||||
div(class="regulator-label") // for spacing
|
||||
div(class="regulator-type") { "Half-curvature" }
|
||||
RegulatorInput(regulator=self)
|
||||
div(class="status")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// a list item that shows a regulator in an outline view of an element
|
||||
#[component(inline_props)]
|
||||
fn RegulatorOutlineItem(regulator_key: RegulatorKey, element_key: ElementKey) -> View {
|
||||
let state = use_context::<AppState>();
|
||||
let assembly = &state.assembly;
|
||||
let regulator = assembly.regulators.with(|regs| regs[regulator_key]);
|
||||
let other_subject = if regulator.subjects.0 == element_key {
|
||||
regulator.subjects.1
|
||||
} else {
|
||||
regulator.subjects.0
|
||||
};
|
||||
let other_subject_label = assembly.elements.with(|elts| elts[other_subject].label.clone());
|
||||
view! {
|
||||
li(class="regulator") {
|
||||
div(class="regulator-label") { (other_subject_label) }
|
||||
div(class="regulator-type") { "Inversive distance" }
|
||||
RegulatorInput(regulator=regulator)
|
||||
div(class="status")
|
||||
}
|
||||
}
|
||||
let regulator = state.assembly.regulators.with(
|
||||
|regs| regs[regulator_key].clone()
|
||||
);
|
||||
regulator.outline_item(element_key)
|
||||
}
|
||||
|
||||
// a list item that shows an element in an outline view of an assembly
|
||||
|
@ -117,7 +159,15 @@ fn ElementOutlineItem(key: ElementKey, element: assembly::Element) -> View {
|
|||
};
|
||||
let regulated = element.regulators.map(|regs| regs.len() > 0);
|
||||
let regulator_list = element.regulators.map(
|
||||
|regs| regs.clone().into_iter().collect()
|
||||
move |elt_reg_keys| elt_reg_keys
|
||||
.clone()
|
||||
.into_iter()
|
||||
.sorted_by_key(
|
||||
|®_key| state.assembly.regulators.with(
|
||||
|regs| regs[reg_key].subjects().len()
|
||||
)
|
||||
)
|
||||
.collect()
|
||||
);
|
||||
let details_node = create_node_ref();
|
||||
view! {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue