rustty/core/
position.rs

1/// A `(x, y)` position on screen.
2pub type Pos = (usize, usize);
3/// A `(cols, rows)` size.
4pub type Size = (usize, usize);
5
6pub trait HasSize {
7    fn size(&self) -> Size;
8}
9
10pub trait HasPosition {
11    fn origin(&self) -> Pos;
12    fn set_origin(&mut self, new_origin: Pos);
13}
14
15/// A cursor position.
16pub struct Cursor {
17    pos: Option<Pos>,
18    last_pos: Option<Pos>,
19}
20
21impl Cursor {
22    pub fn new() -> Cursor {
23        Cursor {
24            pos: None,
25            last_pos: None,
26        }
27    }
28
29    /// Checks whether the current and last coordinates are sequential and returns `true` if they
30    /// are and `false` otherwise.
31    pub fn is_seq(&self) -> bool {
32        if let Some((cx, cy)) = self.pos {
33            if let Some((lx, ly)) = self.last_pos {
34                (lx + 1, ly) == (cx, cy)
35            } else {
36                false
37            }
38        } else {
39            false
40        }
41    }
42
43    pub fn pos(&self) -> Option<Pos> {
44        self.pos
45    }
46
47    pub fn set_pos(&mut self, newpos: Option<Pos>) {
48        self.last_pos = self.pos;
49        self.pos = newpos;
50    }
51
52    pub fn invalidate_last_pos(&mut self) {
53        self.last_pos = None;
54    }
55}