1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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());
}
}
|