minifb_tile_base/tools/
transform.rs

1use super::dual_trait::Algebra;
2
3#[derive(Clone, Copy, Default)]
4pub struct Transform {
5    pub position: Position,
6    pub rotation: Rotation,
7}
8
9#[derive(Clone, Copy, Default)]
10pub enum Rotation {
11    #[default]
12    UP,
13    DOWN,
14    LEFT,
15    RIGHT,
16}
17
18#[derive(Clone, Copy, Default, Debug)]
19pub struct Position {
20    pub x: usize,
21    pub y: usize,
22}
23
24impl Algebra for Position {
25    type Item = usize;
26
27    fn new(first: Self::Item, last: Self::Item) -> Self {
28        Self { x: first, y: last }
29    }
30
31    fn first(&self) -> Self::Item {
32        self.x
33    }
34
35    fn last(&self) -> Self::Item {
36        self.y
37    }
38
39    fn splat(value: Self::Item) -> Self {
40        Self { x: value, y: value }
41    }
42}
43
44#[derive(Clone, Copy, Default, Debug)]
45pub struct Dimensions {
46    pub width: usize,
47    pub height: usize,
48}
49
50impl Algebra for Dimensions {
51    type Item = usize;
52
53    fn new(first: Self::Item, last: Self::Item) -> Self {
54        Self {
55            width: first,
56            height: last,
57        }
58    }
59
60    fn first(&self) -> Self::Item {
61        self.width
62    }
63
64    fn last(&self) -> Self::Item {
65        self.height
66    }
67
68    fn splat(value: Self::Item) -> Self {
69        Self {
70            width: value,
71            height: value,
72        }
73    }
74}
75
76impl Dimensions {
77    pub fn area(&self) -> usize {
78        self.mul_self()
79    }
80
81    pub fn swap(&mut self) {
82        let hold = self.width;
83        self.width = self.height;
84        self.height = hold
85    }
86}