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