aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src/strategy_toolbar
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-02 14:59:45 +0300
committerValentin Popov <valentin@popov.link>2026-12-02 14:59:45 +0300
commit04e58559f76c89a284dfab820efed25a97db2461 (patch)
tree577b57ff01bc731988cfe350f792717aa227040b /crates/fparkan-assets/src/strategy_toolbar
parente631882baadc7a9cb05a56f21ef8de78aa7627aa (diff)
downloadfparkan-04e58559f76c89a284dfab820efed25a97db2461.tar.xz
fparkan-04e58559f76c89a284dfab820efed25a97db2461.zip
feat(ui): fold the strategic toolbar with its original latch
Diffstat (limited to 'crates/fparkan-assets/src/strategy_toolbar')
-rw-r--r--crates/fparkan-assets/src/strategy_toolbar/fold.rs139
1 files changed, 139 insertions, 0 deletions
diff --git a/crates/fparkan-assets/src/strategy_toolbar/fold.rs b/crates/fparkan-assets/src/strategy_toolbar/fold.rs
new file mode 100644
index 00000000..df361658
--- /dev/null
+++ b/crates/fparkan-assets/src/strategy_toolbar/fold.rs
@@ -0,0 +1,139 @@
+use super::{EXTENT, LOCK_TOP};
+
+// iron3d+0x83ad0 reveals one complete control or separator at each step.
+const STRIP_BOTTOMS: [u32; 17] = [
+ 12, 41, 65, 94, 123, 152, 176, 205, 229, 258, 287, 316, 345, 369, 399, 428, LOCK_TOP,
+];
+const LAST_STEP: usize = STRIP_BOTTOMS.len() - 1;
+
+/// Revealed toolbar rows and whether the latch is visible at this step.
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub struct ToolbarLayout {
+ step: usize,
+ moving: bool,
+}
+
+impl Default for ToolbarLayout {
+ fn default() -> Self {
+ Self {
+ step: LAST_STEP,
+ moving: false,
+ }
+ }
+}
+
+impl ToolbarLayout {
+ pub fn sections_visible(self) -> bool {
+ self.step == LAST_STEP
+ }
+
+ pub(super) fn body_bottom(self) -> u32 {
+ STRIP_BOTTOMS[self.step]
+ }
+
+ pub(super) fn lock_top(self) -> Option<u32> {
+ (!self.moving).then_some(self.body_bottom())
+ }
+
+ pub fn button_visible(self, y: f32) -> bool {
+ y.round_ties_even() < self.body_bottom() as f32
+ }
+
+ pub(super) fn over_lock(self, [x, y]: [f32; 2]) -> bool {
+ self.lock_top().is_some_and(|top| {
+ (0.0..EXTENT[0] as f32).contains(&x.round_ties_even())
+ && (top as f32..(top + 24).min(EXTENT[1]) as f32).contains(&y.round_ties_even())
+ })
+ }
+
+ pub fn covers_pointer(self, [x, y]: [f32; 2]) -> bool {
+ self.over_lock([x, y])
+ || ((0.0..EXTENT[0] as f32).contains(&x.round_ties_even())
+ && (0.0..self.body_bottom() as f32).contains(&y.round_ties_even()))
+ }
+}
+
+#[derive(Default)]
+pub struct ToolbarFold {
+ layout: ToolbarLayout,
+ opening: bool,
+ last_step_ms: u32,
+}
+
+impl ToolbarFold {
+ pub fn layout(&self) -> ToolbarLayout {
+ self.layout
+ }
+
+ /// The lower latch starts closing on a click. The upper latch opens on hover.
+ pub fn start_closing(&mut self, pointer: [f32; 2], now_ms: u32) -> bool {
+ if self.layout.step != LAST_STEP || !self.layout.over_lock(pointer) {
+ return false;
+ }
+ self.start(false, now_ms);
+ true
+ }
+
+ pub fn start_opening(&mut self, pointer: [f32; 2], now_ms: u32) -> bool {
+ if self.layout.step != 0 || !self.layout.over_lock(pointer) {
+ return false;
+ }
+ self.start(true, now_ms);
+ true
+ }
+
+ fn start(&mut self, opening: bool, now_ms: u32) {
+ self.opening = opening;
+ self.layout.moving = true;
+ self.last_step_ms = now_ms;
+ }
+
+ /// iron3d+0x84040 advances one strip per due update, without catching up skipped strips.
+ pub fn advance(&mut self, now_ms: u32) {
+ if !self.layout.moving || now_ms.wrapping_sub(self.last_step_ms) <= 50 {
+ return;
+ }
+ self.last_step_ms = now_ms;
+ if self.opening {
+ self.layout.step += 1;
+ } else {
+ self.layout.step -= 1;
+ }
+ self.layout.moving = self.layout.step != 0 && self.layout.step != LAST_STEP;
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn click_folds_each_strip_and_hover_opens_from_the_top() {
+ let mut panel = ToolbarFold::default();
+ assert!(!panel.start_opening([20.0, 470.0], 0));
+ assert!(panel.start_closing([20.0, 470.0], 0));
+ panel.advance(50);
+ assert_eq!(panel.layout().body_bottom(), LOCK_TOP);
+ assert_eq!(panel.layout().lock_top(), None);
+ panel.advance(250);
+ assert_eq!(panel.layout().body_bottom(), 428);
+ assert!(!panel.layout().sections_visible());
+ assert!(!panel.layout().covers_pointer([20.0, 440.0]));
+ assert!(panel.layout().button_visible(400.0));
+ assert!(!panel.layout().button_visible(428.0));
+
+ for step in 1..16 {
+ panel.advance(250 + step * 51);
+ }
+ assert_eq!(panel.layout().lock_top(), Some(12));
+ assert!(panel.layout().covers_pointer([20.0, 35.0]));
+ assert!(!panel.layout().covers_pointer([20.0, 36.0]));
+ assert!(!panel.start_opening([20.0, 470.0], 1100));
+ assert!(panel.start_opening([20.0, 20.0], 1100));
+ assert!(!panel.start_opening([20.0, 20.0], 1101));
+ for step in 1..=16 {
+ panel.advance(1100 + step * 51);
+ }
+ assert_eq!(panel.layout(), ToolbarLayout::default());
+ }
+}