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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
use crate::utils;
fn compute_reverse(frames: u64, tick: u64, start: u64) -> u64 {
let leftover = frames - (tick - start)
.clamp(0, frames);
tick - leftover
}
pub enum ModeToggle {
Inactive { start: u64 },
Active { start: u64 },
}
pub struct Mode {
frames: u64,
toggle: ModeToggle,
locked: bool,
}
impl Mode {
pub fn new(frames: u64) -> Self {
Self {
frames,
toggle: ModeToggle::Inactive { start: 0 },
locked: false,
}
}
/// Is the current state active?
pub fn is_active(&self) -> bool {
match self.toggle {
ModeToggle::Inactive {..} => false,
ModeToggle::Active {..} => true,
}
}
pub fn is_locked(&self) -> bool {
self.locked
}
/// Has the current transition finished?
pub fn is_ready(&self, tick: u64) -> bool {
let started = match self.toggle {
ModeToggle::Inactive { start } => start,
ModeToggle::Active { start } => start,
};
tick - started > self.frames
}
pub fn progress(&self, tick: u64) -> f32 {
match self.toggle {
ModeToggle::Inactive { start } => {
1.0 - (((tick - start) as f32) / self.frames as f32)
.clamp(0.0, 1.0)
},
ModeToggle::Active { start } => {
(((tick - start) as f32) / self.frames as f32)
.clamp(0.0, 1.0)
}
}
}
pub fn reset(&mut self) {
self.locked = false;
self.toggle = ModeToggle::Inactive { start: 0 };
}
pub fn reverse(&mut self, tick: u64) -> bool {
if !self.locked {
self.locked = true;
match self.toggle {
ModeToggle::Inactive { start } => {
self.toggle = ModeToggle::Active {
start: compute_reverse(self.frames, tick, start)
};
},
ModeToggle::Active { start } => {
self.toggle = ModeToggle::Inactive {
start: compute_reverse(self.frames, tick, start)
};
},
}
true
} else { false }
}
pub fn lock(&mut self) {
self.locked = true;
}
pub fn unlock(&mut self) {
self.locked = false;
}
}
pub struct Cursor {
pub index: i32,
pub prev_index: i32,
pub change_started: u64,
pub bound: i32,
pub frames: u64,
pub locked: bool,
}
impl Cursor {
pub fn new(bound: i32, frames: u64) -> Self {
Self {
index: 0,
prev_index: 0,
change_started: 0,
bound,
frames,
locked: false,
}
}
pub fn animation_index(&self, tick: u64) -> f32 {
let progress = ((tick - self.change_started) as f32)
/ (self.frames as f32 / 2.0);
utils::lerp(
self.prev_index as f32,
self.index as f32,
progress
)
}
pub fn is_ready(&self, tick: u64) -> bool {
tick - self.change_started > self.frames
}
pub fn set(&mut self, val: i32, tick: u64) -> bool {
if self.is_ready(tick) || !self.locked {
self.change_started = tick;
self.prev_index = self.index;
self.index = val;
self.index %= self.bound;
self.locked = true;
true
} else { false }
}
pub fn increment(&mut self, tick: u64) -> bool {
self.set(self.index + 1, tick)
}
pub fn decrement(&mut self, tick: u64) -> bool {
self.set(self.index + self.bound - 1, tick)
}
pub fn unlock(&mut self) {
self.locked = false;
}
}
|