devotee_backend/middling.rs
1/// So-so display surface trait for reuse purposes.
2pub trait Surface {
3 /// Specific texel type.
4 type Texel;
5
6 /// Get texel given its coordinates.
7 fn texel(&self, x: u32, y: u32) -> Option<Self::Texel>;
8
9 /// Set texel given its coordinates.
10 fn set_texel(&mut self, x: u32, y: u32, value: Self::Texel);
11
12 /// Get texel given its coordinates unsafely.
13 ///
14 /// # Safety
15 ///
16 /// - `x` must be in range [`0`, `width - 1`];
17 /// - `y` must be in range [`0`, `height - 1`];
18 unsafe fn texel_unchecked(&self, x: u32, y: u32) -> Self::Texel {
19 self.texel(x, y).unwrap()
20 }
21
22 /// Set texel value given its coordinates unsafely.
23 ///
24 /// # Safety
25 ///
26 /// - `x` must be in range [`0`, `width - 1`];
27 /// - `y` must be in range [`0`, `height - 1`];
28 unsafe fn set_texel_unchecked(&mut self, x: u32, y: u32, value: Self::Texel) {
29 self.set_texel(x, y, value)
30 }
31
32 /// Clear the surface with the given color.
33 fn clear(&mut self, value: Self::Texel);
34
35 /// Get surface width.
36 fn width(&self) -> u32;
37
38 /// Get surface height.
39 fn height(&self) -> u32;
40}
41
42/// The surface that can be filled directly.
43pub trait Fill: Surface {
44 /// Fill the surface from the provided `data`.
45 fn fill_from(&mut self, data: &[Self::Texel]);
46}
47
48/// Input handler trait with optional input caching.
49pub trait InputHandler<Event, EventContext> {
50 /// Handle input event, optionally consume it.
51 fn handle_event(&mut self, event: Event, event_context: &EventContext) -> Option<Event>;
52
53 /// Update internal state over tick event.
54 fn update(&mut self);
55}
56
57/// Event context generalization.
58pub trait EventContext<EventSpace> {
59 /// Surface space coordinates representation.
60 type SurfaceSpace;
61
62 /// Recalculate from the event space coordinates into the surface space coordinates.
63 fn estimate_surface_space(&self, event_space: EventSpace) -> Self::SurfaceSpace;
64}