summaryrefslogtreecommitdiffstats
path: root/src/ui/grid/cursor.rs
blob: 16f8fcfd7a3dd53336ff99d752e03492b60e17cc (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use crate::ui::color::Color;

#[derive(Default)]
pub struct Animation {
    start: (f64, f64),
    end: (f64, f64),
    start_time: i64,
    end_time: i64,
}

#[derive(Default)]
pub struct Cursor {
    /// Position, (row, col).
    pub pos: Option<(f64, f64)>,
    /// Flag for disabling the movement animation.
    pub disable_animation: bool,
    pub animation: Option<Animation>,

    /// Alpha color. Used to make the cursor blink.
    pub alpha: f64,
    /// The duration of the blink.
    pub blink_on: u64,
    /// Width of the cursor.
    pub cell_percentage: f64,
    /// Color of the cursor.
    pub color: Color,
}

impl Cursor {
    pub fn goto(&mut self, row: f64, col: f64, frame_time: i64) {
        // When we get our first cursor_goto, set the position directly.
        if self.pos.is_none() {
            self.pos = Some((row, col));
        }

        // If cursor animation is disabled, set the position directly. Otherwise, set the animation
        // so that we can animate cursor position change.
        if self.disable_animation {
            self.pos = Some((row, col));
        } else {
            let duration = 200;
            self.animation = Some(Animation {
                start: self.pos.unwrap(),
                end: (row, col),
                start_time: frame_time,
                end_time: frame_time + 1000 * duration,
            });
        }
    }

    pub fn tick(&mut self, frame_time: i64) {
        self.blink();
        self.animate_position(frame_time);
    }

    fn blink(&mut self) {
        // If we dont need to blink, return.
        if self.blink_on == 0 {
            return;
        }

        // Assuming a 60hz framerate
        self.alpha += 100.0 / (6.0 * self.blink_on as f64);

        if self.alpha > 2.0 {
            self.alpha = 0.0;
        }
    }

    fn animate_position(&mut self, frame_time: i64) {
        if let Some(Animation {
            start,
            end,
            start_time,
            end_time,
        }) = self.animation
        {
            let mut pos = self.pos.unwrap_or((0.0, 0.0));

            if frame_time < end_time && pos != end {
                let mut t = (frame_time - start_time) as f64
                    / (end_time - start_time) as f64;
                t = ease_out_cubic(t);
                pos.0 = start.0 + t * (end.0 - start.0);
                pos.1 = start.1 + t * (end.1 - start.1);

                self.pos = Some(pos);
            } else {
                self.pos = Some(end);
                self.animation = None;
            }
        }
    }

    /// Gets the position of the cursor.
    pub fn get_position(&self) -> Option<(f64, f64)> {
        if let Some(ref a) = self.animation {
            // The end position of our animation is the "real" position where
            // the cursor is.
            Some(a.end)
        } else {
            self.pos
        }
    }
}

/// From clutter-easing.c, based on Robert Penner's
/// infamous easing equations, MIT license.
fn ease_out_cubic(t: f64) -> f64 {
    let p = t - 1f64;
    p * p * p + 1f64
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cursor_blink100() {
        let mut cursor = Cursor::default();
        cursor.blink_on = 100;
        cursor.alpha = 1.0;

        cursor.blink();
        assert_eq!(cursor.alpha, 1.1666666666666667);
    }

    #[test]
    fn test_cursor_blink0() {
        let mut cursor = Cursor::default();
        cursor.blink_on = 0;
        cursor.alpha = 1.0;

        cursor.blink();
        assert_eq!(cursor.alpha, 1.0);
    }

    #[test]
    fn test_first_position() {
        let mut cursor = Cursor::default();

        // When we first set the position, it should be set immediately.
        cursor.goto(15.0, 15.0, 1);
        assert_eq!(cursor.pos, Some((15.0, 15.0)));

        // When we've set the position once already, the subsequent goto positions should be set
        // with some delay by the animation.
        cursor.goto(10.0, 10.0, 1);
        assert_eq!(cursor.pos, Some((15.0, 15.0)));
    }

    #[test]
    fn test_animate_position() {
        let mut cursor = Cursor::default();

        // When we first set the position, it should be set immediately.
        cursor.goto(15.0, 15.0, 1);
        assert_eq!(cursor.pos, Some((15.0, 15.0)));

        cursor.goto(10.0, 10.0, 1);
        cursor.tick(25000);
        assert_eq!(cursor.pos, Some((13.349666797203126, 13.349666797203126)));
    }

    #[test]
    fn test_animate_position_animation_disabled() {
        let mut cursor = Cursor::default();
        cursor.disable_animation = true;

        // When we first set the position, it should be set immediately.
        cursor.goto(15.0, 15.0, 1);
        assert_eq!(cursor.pos, Some((15.0, 15.0)));

        // Position animation is disabled, goto should change the position directly and tick
        // shouldn't affect the position value at all.
        cursor.goto(10.0, 10.0, 1);
        assert_eq!(cursor.pos, Some((10.0, 10.0)));
        cursor.tick(25000);
        assert_eq!(cursor.pos, Some((10.0, 10.0)));
    }

    #[test]
    fn test_get_position() {
        let mut cursor = Cursor::default();

        assert_eq!(cursor.get_position(), None);
        cursor.pos = Some((10.0, 10.0));
        assert_eq!(cursor.get_position(), Some((10.0, 10.0)));
        cursor.animation = Some(Animation {
            end: (15.0, 15.0),
            ..Animation::default()
        });
        assert_eq!(cursor.get_position(), Some((15.0, 15.0)));
    }
}