Skip to main content

miracle_plugin/
core.rs

1use super::bindings::{miracle_point_t, miracle_size_t};
2use glam::Mat4;
3
4/// A rectangle defined by a point and size.
5#[derive(Debug, Clone, Copy, PartialEq, Default)]
6pub struct Rect {
7    pub x: f32,
8    pub y: f32,
9    pub width: f32,
10    pub height: f32,
11}
12
13impl Rect {
14    pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
15        Self {
16            x,
17            y,
18            width,
19            height,
20        }
21    }
22
23    pub fn from_array(arr: [f32; 4]) -> Self {
24        Self {
25            x: arr[0],
26            y: arr[1],
27            width: arr[2],
28            height: arr[3],
29        }
30    }
31
32    pub fn to_array(self) -> [f32; 4] {
33        [self.x, self.y, self.width, self.height]
34    }
35}
36
37/// A size with integer dimensions.
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
39pub struct Size {
40    pub width: i32,
41    pub height: i32,
42}
43
44impl Size {
45    pub const fn new(width: i32, height: i32) -> Self {
46        Self { width, height }
47    }
48}
49
50impl From<Size> for miracle_size_t {
51    fn from(value: Size) -> Self {
52        Self {
53            w: value.width,
54            h: value.height,
55        }
56    }
57}
58
59impl From<miracle_size_t> for Size {
60    fn from(value: miracle_size_t) -> Self {
61        Self {
62            width: value.w,
63            height: value.h,
64        }
65    }
66}
67
68/// A 2D point with integer coordinates.
69#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
70pub struct Point {
71    pub x: i32,
72    pub y: i32,
73}
74
75impl Point {
76    pub const fn new(x: i32, y: i32) -> Self {
77        Self { x, y }
78    }
79}
80
81impl From<Point> for miracle_point_t {
82    fn from(value: Point) -> Self {
83        Self {
84            x: value.x,
85            y: value.y,
86        }
87    }
88}
89
90impl From<miracle_point_t> for Point {
91    fn from(value: miracle_point_t) -> Self {
92        Self {
93            x: value.x,
94            y: value.y,
95        }
96    }
97}
98
99/// A rectangle with integer position and dimensions.
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
101pub struct Rectangle {
102    pub x: i32,
103    pub y: i32,
104    pub width: i32,
105    pub height: i32,
106}
107
108impl Rectangle {
109    pub const fn new(x: i32, y: i32, width: i32, height: i32) -> Self {
110        Self {
111            x,
112            y,
113            width,
114            height,
115        }
116    }
117}
118
119pub fn mat4_from_f32_array(arr: [f32; 16]) -> Mat4 {
120    Mat4::from_cols_array(&arr)
121}
122
123pub fn mat4_to_f32_array(mat: Mat4) -> [f32; 16] {
124    mat.to_cols_array()
125}