1use clairlune_scene::activity::{Activity, builtin_catalog};
8use clairlune_scene::placeholder::PixelMap;
9use clairlune_scene::{Rgb, SceneGraph, SpriteKind};
10use clairlune_sprites::{Character, builtin_character};
11
12pub trait Pixmap {
14 fn width(&self) -> u32;
15 fn height(&self) -> u32;
16 fn pixel(&self, x: u32, y: u32) -> Option<Rgb>;
18}
19
20impl Pixmap for PixelMap {
21 fn width(&self) -> u32 {
22 self.width
23 }
24 fn height(&self) -> u32 {
25 self.height
26 }
27 fn pixel(&self, x: u32, y: u32) -> Option<Rgb> {
28 PixelMap::pixel(self, x, y)
29 }
30}
31
32#[derive(Clone, Debug, PartialEq, Eq)]
34pub struct OwnedPixmap {
35 pub width: u32,
36 pub height: u32,
37 pub pixels: Vec<Option<Rgb>>,
38}
39
40impl OwnedPixmap {
41 pub fn copy_of(p: &dyn Pixmap) -> Self {
42 let pixels = (0..p.height())
43 .flat_map(|y| (0..p.width()).map(move |x| (x, y)))
44 .map(|(x, y)| p.pixel(x, y))
45 .collect();
46 OwnedPixmap {
47 width: p.width(),
48 height: p.height(),
49 pixels,
50 }
51 }
52}
53
54impl Pixmap for OwnedPixmap {
55 fn width(&self) -> u32 {
56 self.width
57 }
58 fn height(&self) -> u32 {
59 self.height
60 }
61 fn pixel(&self, x: u32, y: u32) -> Option<Rgb> {
62 self.pixels[(y * self.width + x) as usize]
63 }
64}
65
66pub trait SceneArt {
68 fn activity(&self) -> Option<&Activity>;
70 fn character(&self) -> &dyn Character;
71 fn prop(&self, kind: SpriteKind) -> Option<&dyn Pixmap>;
73}
74
75pub struct BuiltinArt {
77 activity: Option<&'static Activity>,
78}
79
80impl BuiltinArt {
81 pub fn for_graph(graph: &SceneGraph) -> Self {
82 BuiltinArt {
83 activity: graph
84 .activity
85 .as_deref()
86 .and_then(|id| builtin_catalog().get(id)),
87 }
88 }
89}
90
91impl SceneArt for BuiltinArt {
92 fn activity(&self) -> Option<&Activity> {
93 self.activity
94 }
95 fn character(&self) -> &dyn Character {
96 builtin_character()
97 }
98 fn prop(&self, kind: SpriteKind) -> Option<&dyn Pixmap> {
99 Some(kind.map())
100 }
101}