Skip to main content

dotzuki_engine/render/
geometry.rs

1/// Tile-grid position (8×8 pixel tiles).
2#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
3#[non_exhaustive]
4pub struct TilePos {
5    /// Column in tile units (0-based).
6    pub tx: u32,
7    /// Row in tile units (0-based).
8    pub ty: u32,
9}
10
11impl TilePos {
12    /// Create a new tile position.
13    #[inline]
14    pub const fn new(tx: u32, ty: u32) -> Self {
15        Self { tx, ty }
16    }
17
18    /// Convert to pixel coordinates (each tile is 8×8 pixels).
19    #[inline]
20    pub const fn to_pixels(self) -> (u32, u32) {
21        (self.tx * 8, self.ty * 8)
22    }
23}
24
25/// Tile-grid rectangle (position + extent in tile units).
26#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
27#[non_exhaustive]
28pub struct TileRect {
29    /// Left column in tile units.
30    pub tx: u32,
31    /// Top row in tile units.
32    pub ty: u32,
33    /// Width in tile units.
34    pub tw: u32,
35    /// Height in tile units.
36    pub th: u32,
37}
38
39impl TileRect {
40    /// Create a new tile rectangle.
41    #[inline]
42    pub const fn new(tx: u32, ty: u32, tw: u32, th: u32) -> Self {
43        Self { tx, ty, tw, th }
44    }
45
46    /// Return the top-left corner as a [`TilePos`].
47    #[inline]
48    pub const fn pos(&self) -> TilePos {
49        TilePos::new(self.tx, self.ty)
50    }
51
52    /// Return a rectangle shifted by `(dx, dy)` tiles.
53    #[inline]
54    pub fn translated(&self, dx: u32, dy: u32) -> Self {
55        Self::new(self.tx + dx, self.ty + dy, self.tw, self.th)
56    }
57}
58
59/// Bitmap of which sides of a bracket box should be drawn.
60#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
61pub struct BracketSides {
62    /// Draw the top edge.
63    pub top: bool,
64    /// Draw the bottom edge.
65    pub bottom: bool,
66    /// Draw the left edge.
67    pub left: bool,
68    /// Draw the right edge.
69    pub right: bool,
70}
71
72impl BracketSides {
73    /// Only the right and bottom edges (corner bracket).
74    pub const RIGHT_BOTTOM: Self = Self { top: false, bottom: true, left: false, right: true };
75    /// All four edges.
76    pub const ALL: Self = Self { top: true, bottom: true, left: true, right: true };
77}