rlvgl_widgets/motion/direction.rs
1// SPDX-License-Identifier: MIT
2//! Cardinal direction enum shared by motion widgets.
3//!
4//! Motion components such as [`crate::motion::crawl::TextCrawl`] scroll
5//! along one of four cardinal directions. The enum lives alongside the
6//! other motion infrastructure so all variants consume the same
7//! vocabulary.
8
9/// One of four cardinal scroll directions.
10///
11/// Combined with a [`crate::motion::MotionRate`], a [`Direction`]
12/// fully specifies how the visible window slides over a jumbo
13/// background buffer: the rate supplies the **speed**, the direction
14/// supplies the **axis** and **sign**.
15#[derive(Copy, Clone, Debug, PartialEq, Eq)]
16pub enum Direction {
17 /// Content scrolls upward (Star Wars crawl default).
18 Up,
19 /// Content scrolls downward.
20 Down,
21 /// Content scrolls to the left (news ticker default).
22 Left,
23 /// Content scrolls to the right.
24 Right,
25}
26
27impl Direction {
28 /// Returns `true` if this direction is vertical (`Up` or `Down`).
29 #[inline]
30 pub const fn is_vertical(self) -> bool {
31 matches!(self, Direction::Up | Direction::Down)
32 }
33
34 /// Returns `true` if this direction is horizontal (`Left` or `Right`).
35 #[inline]
36 pub const fn is_horizontal(self) -> bool {
37 matches!(self, Direction::Left | Direction::Right)
38 }
39
40 /// Sign of the scroll offset along the scroll axis.
41 ///
42 /// `+1` when the scroll accumulator should *grow* per frame
43 /// ([`Direction::Down`] / [`Direction::Right`]) and `-1` when it
44 /// should shrink ([`Direction::Up`] / [`Direction::Left`]).
45 #[inline]
46 pub const fn sign(self) -> i32 {
47 match self {
48 Direction::Down | Direction::Right => 1,
49 Direction::Up | Direction::Left => -1,
50 }
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn vertical_directions() {
60 assert!(Direction::Up.is_vertical());
61 assert!(Direction::Down.is_vertical());
62 assert!(!Direction::Left.is_vertical());
63 assert!(!Direction::Right.is_vertical());
64 }
65
66 #[test]
67 fn horizontal_directions() {
68 assert!(Direction::Left.is_horizontal());
69 assert!(Direction::Right.is_horizontal());
70 assert!(!Direction::Up.is_horizontal());
71 assert!(!Direction::Down.is_horizontal());
72 }
73
74 #[test]
75 fn signs() {
76 assert_eq!(Direction::Up.sign(), -1);
77 assert_eq!(Direction::Down.sign(), 1);
78 assert_eq!(Direction::Left.sign(), -1);
79 assert_eq!(Direction::Right.sign(), 1);
80 }
81}