devotee_backend/
middling.rs

1/// Type trait that can point on a texel reference.
2pub trait TexelDesignatorRef<'a> {
3    /// Texel reference.
4    type TexelRef;
5}
6
7/// Type trait that can point on a mutable texel reference.
8pub trait TexelDesignatorMut<'a> {
9    /// Mutable texel reference.
10    type TexelMut;
11}
12
13/// Helper type to represent texel reference.
14pub type TexelRef<'a, This> = <This as TexelDesignatorRef<'a>>::TexelRef;
15
16/// Helper type to represent mutable texel reference.
17pub type TexelMut<'a, This> = <This as TexelDesignatorMut<'a>>::TexelMut;
18
19/// So-so display surface trait for reuse purposes.
20pub trait Surface: for<'a> TexelDesignatorRef<'a> + for<'a> TexelDesignatorMut<'a> {
21    /// Specific texel type.
22    type Texel;
23
24    /// Get texel reference given its coordinates.
25    fn texel(&self, x: u32, y: u32) -> Option<TexelRef<'_, Self>>;
26
27    /// Get mutable texel reference given its coordinates.
28    fn texel_mut(&mut self, x: u32, y: u32) -> Option<TexelMut<'_, Self>>;
29
30    /// Get texel reference given its coordinates unsafely.
31    ///
32    /// # Safety
33    ///
34    /// - `x` must be in range [`0`, `width - 1`];
35    /// - `y` must be in range [`0`, `height - 1`];
36    unsafe fn unsafe_texel(&self, x: u32, y: u32) -> TexelRef<'_, Self> {
37        self.texel(x, y).unwrap()
38    }
39
40    /// Get mutable texel reference given its coordinates unsafely.
41    ///
42    /// # Safety
43    ///
44    /// - `x` must be in range [`0`, `width - 1`];
45    /// - `y` must be in range [`0`, `height - 1`];
46    unsafe fn unsafe_texel_mut(&mut self, x: u32, y: u32) -> TexelMut<'_, Self> {
47        self.texel_mut(x, y).unwrap()
48    }
49
50    /// Clear the surface with the given color.
51    fn clear(&mut self, value: Self::Texel);
52
53    /// Get surface width.
54    fn width(&self) -> u32;
55
56    /// Get surface height.
57    fn height(&self) -> u32;
58}
59
60/// The surface that can be filled directly.
61pub trait Fill: Surface {
62    /// Fill the surface from the provided `data`.
63    fn fill_from(&mut self, data: &[Self::Texel]);
64}
65
66/// Input handler trait with optional input caching.
67pub trait InputHandler<Event, EventContext> {
68    /// Handle input event, optionally consume it.
69    fn handle_event(&mut self, event: Event, event_context: &EventContext) -> Option<Event>;
70
71    /// Update internal state over tick event.
72    fn update(&mut self);
73}
74
75/// Event context generalization.
76pub trait EventContext<EventSpace> {
77    /// Surface space coordinates representation.
78    type SurfaceSpace;
79
80    /// Recalculate from the event space coordinates into the surface space coordinates.
81    fn estimate_surface_space(&self, event_space: EventSpace) -> Self::SurfaceSpace;
82}