oxygengine_composite_renderer/
tileset_asset_protocol.rs1use crate::math::Rect;
2use core::{
3 assets::{
4 asset::{Asset, AssetId},
5 protocol::{AssetLoadResult, AssetProtocol, AssetVariant, Meta},
6 },
7 Ignite, Scalar,
8};
9use serde::{Deserialize, Serialize};
10use std::{any::Any, str::from_utf8};
11
12#[derive(Ignite, Debug, Clone, Serialize, Deserialize)]
13pub struct TilesetInfo {
14 pub image: String,
15 #[serde(default)]
16 pub cols: usize,
17 #[serde(default)]
18 pub rows: usize,
19 #[serde(default)]
20 #[serde(alias = "tileWidth")]
21 pub tile_width: Scalar,
22 #[serde(default)]
23 #[serde(alias = "tileHeight")]
24 pub tile_height: Scalar,
25 #[serde(default)]
26 #[serde(alias = "paddingCol")]
27 pub padding_col: Scalar,
28 #[serde(default)]
29 #[serde(alias = "paddingRow")]
30 pub padding_row: Scalar,
31 #[serde(default)]
32 #[serde(alias = "marginCol")]
33 pub margin_col: Scalar,
34 #[serde(default)]
35 #[serde(alias = "marginRow")]
36 pub margin_row: Scalar,
37}
38
39impl TilesetInfo {
40 pub fn image_name(&self) -> String {
41 let parts = self.image.split("://").collect::<Vec<_>>();
42 if parts.len() > 1 {
43 parts[1].to_owned()
44 } else {
45 self.image.clone()
46 }
47 }
48
49 pub fn tiles(&self) -> usize {
50 self.cols * self.rows
51 }
52
53 pub fn frame(&self, col: usize, row: usize) -> Option<Rect> {
54 if col >= self.cols || row >= self.rows {
55 return None;
56 }
57 Some(Rect {
58 x: self.margin_col + self.tile_width * col as Scalar + self.padding_col * col as Scalar,
59 y: self.margin_row
60 + self.tile_height * row as Scalar
61 + self.padding_row * row as Scalar,
62 w: self.tile_width,
63 h: self.tile_height,
64 })
65 }
66}
67
68pub struct TilesetAsset {
69 info: TilesetInfo,
70 image_asset: AssetId,
71}
72
73impl TilesetAsset {
74 pub fn info(&self) -> &TilesetInfo {
75 &self.info
76 }
77
78 pub fn image_asset(&self) -> AssetId {
79 self.image_asset
80 }
81}
82
83pub struct TilesetAssetProtocol;
84
85impl AssetProtocol for TilesetAssetProtocol {
86 fn name(&self) -> &str {
87 "tiles"
88 }
89
90 fn on_load(&mut self, data: Vec<u8>) -> AssetLoadResult {
91 let data = from_utf8(&data).unwrap();
92 let info: TilesetInfo = serde_json::from_str(data).unwrap();
93 let image = info.image.clone();
94 AssetLoadResult::Yield(Some(Box::new(info)), vec![("image".to_owned(), image)])
95 }
96
97 fn on_resume(&mut self, payload: Meta, list: &[(&str, &Asset)]) -> AssetLoadResult {
98 let info = *(payload.unwrap() as Box<dyn Any + Send>)
99 .downcast::<TilesetInfo>()
100 .unwrap();
101 let image_asset = list
102 .get(0)
103 .expect("Could not obtain tileset image asset")
104 .1
105 .id();
106 AssetLoadResult::Data(Box::new(TilesetAsset { info, image_asset }))
107 }
108
109 fn on_unload(&mut self, asset: &Asset) -> Option<Vec<AssetVariant>> {
110 asset
111 .get::<TilesetAsset>()
112 .map(|asset| vec![AssetVariant::Id(asset.image_asset)])
113 }
114}