1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use std::path::PathBuf;
use arcstr::ArcStr;
use derive_new::new;
use getset::{CopyGetters, Getters};
use serde::{Deserialize, Serialize};
use traffloat_types::geometry;
use crate::{IdString, Schema};
pub type Id = crate::Id<Def>;
impl_identifiable!(Def);
#[derive(Debug, Clone, Serialize, Deserialize, Getters, CopyGetters)]
#[cfg_attr(feature = "xy", derive(xylem::Xylem))]
#[cfg_attr(feature = "xy", xylem(derive(Deserialize), process))]
pub struct Def {
#[getset(get_copy = "pub")]
#[cfg_attr(feature = "xy", xylem(args(new = true)))]
id: Id,
#[getset(get = "pub")]
#[cfg_attr(feature = "xy", xylem(serde(default)))]
id_str: IdString<Def>,
#[getset(get = "pub")]
dir: PathBuf,
#[getset(get = "pub")]
variants: Vec<Variant>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Getters, CopyGetters)]
#[cfg_attr(feature = "xy", derive(xylem::Xylem))]
#[cfg_attr(feature = "xy", xylem(derive(Deserialize)))]
pub struct Variant {
#[getset(get = "pub")]
name: ArcStr,
#[getset(get_copy = "pub")]
dimension: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct SpritesheetId(u32);
impl SpritesheetId {
pub fn new(id: u32) -> Self { Self(id) }
pub fn value(&self) -> u32 { self.0 }
}
#[derive(Debug, Clone, Serialize, Deserialize, CopyGetters)]
pub struct IconRef {
#[getset(get_copy = "pub")]
spritesheet_id: SpritesheetId,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Getters, CopyGetters)]
pub struct ModelRef {
#[getset(get_copy = "pub")]
spritesheet_id: SpritesheetId,
#[getset(get_copy = "pub")]
shape: geometry::Unit,
}
#[cfg(feature = "xy")]
pub mod xy {
use std::any::TypeId;
use std::borrow::Borrow;
use std::cmp;
use std::rc::Rc;
use anyhow::Context as _;
use xylem::{Context as _, DefaultContext, IdArgs, NoArgs, Processable, Xylem};
use super::*;
pub type AtlasCreationHook = dyn Fn(&mut Def, &mut DefaultContext) -> anyhow::Result<()>;
#[derive(new)]
pub struct AtlasContext {
pub creation_hook: Rc<AtlasCreationHook>,
}
impl Processable<Schema> for Def {
fn postprocess(&mut self, context: &mut DefaultContext) -> anyhow::Result<()> {
let atlas_context = context
.get::<AtlasContext>(TypeId::of::<()>())
.expect("Context did not initialize atlas creation hook");
let hook = Rc::clone(&atlas_context.creation_hook);
hook(self, context)
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord)]
struct IdName {
id: Id,
name: String,
}
struct IdNameRef<'t> {
id: Id,
name: &'t str,
}
trait AbstractIdName {
fn id(&self) -> Id;
fn name(&self) -> &str;
}
impl<'t> Borrow<dyn AbstractIdName + 't> for IdName {
fn borrow(&self) -> &(dyn AbstractIdName + 't) { self }
}
impl<'t> Borrow<dyn AbstractIdName + 't> for IdNameRef<'t> {
fn borrow(&self) -> &(dyn AbstractIdName + 't) { self }
}
impl AbstractIdName for IdName {
fn id(&self) -> Id { self.id }
fn name(&self) -> &str { self.name.as_str() }
}
impl<'t> AbstractIdName for IdNameRef<'t> {
fn id(&self) -> Id { self.id }
fn name(&self) -> &str { self.name }
}
impl PartialEq for dyn AbstractIdName + '_ {
fn eq(&self, other: &Self) -> bool {
self.id() == other.id() && self.name() == other.name()
}
}
impl Eq for dyn AbstractIdName + '_ {}
impl PartialOrd for dyn AbstractIdName + '_ {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { Some(self.cmp(other)) }
}
impl Ord for dyn AbstractIdName + '_ {
fn cmp(&self, other: &Self) -> cmp::Ordering {
(self.id().cmp(&other.id())).then_with(|| self.name().cmp(other.name()))
}
}
#[derive(Default)]
pub struct IconIndex(std::collections::BTreeMap<IdName, SpritesheetId>);
impl IconIndex {
pub fn add(&mut self, id: Id, name: String, spritesheet_id: SpritesheetId) {
self.0.insert(IdName { id, name }, spritesheet_id);
}
pub fn get(&self, id: Id, name: &str) -> Option<SpritesheetId> {
self.0.get::<dyn AbstractIdName + '_>(&IdNameRef { id, name }).copied()
}
}
#[derive(Serialize, Deserialize)]
pub struct IconRefXylem {
src: String,
name: String,
}
impl Xylem<Schema> for IconRef {
type From = IconRefXylem;
type Args = NoArgs;
fn convert_impl(
from: Self::From,
context: &mut DefaultContext,
_: &NoArgs,
) -> anyhow::Result<Self> {
let src_id = Id::convert(from.src.clone(), context, &IdArgs::default())
.with_context(|| format!("Undefined atlas reference: {}", &from.src))?;
let entry = {
let index = context.get_mut::<IconIndex, _>(TypeId::of::<()>(), Default::default);
index.get(src_id, from.name.as_str())
};
let spritesheet_id = match entry {
Some(value) => value,
None => anyhow::bail!("Undefined icon reference: {}/{}", &from.src, &from.name),
};
Ok(Self { spritesheet_id })
}
}
#[derive(Default)]
pub struct ModelIndex(std::collections::BTreeMap<IdName, ModelRef>);
impl ModelIndex {
pub fn add(
&mut self,
id: Id,
name: String,
spritesheet_id: SpritesheetId,
shape: geometry::Unit,
) {
self.0.insert(IdName { id, name }, ModelRef { spritesheet_id, shape });
}
fn get(&self, id: Id, name: &str) -> Option<ModelRef> {
self.0.get::<dyn AbstractIdName + '_>(&IdNameRef { id, name }).copied()
}
}
#[derive(Serialize, Deserialize)]
pub struct ModelRefXylem {
src: String,
name: ArcStr,
}
impl Xylem<Schema> for ModelRef {
type From = ModelRefXylem;
type Args = NoArgs;
fn convert_impl(
from: Self::From,
context: &mut DefaultContext,
_: &NoArgs,
) -> anyhow::Result<Self> {
let src_id = Id::convert(from.src.clone(), context, &IdArgs::default())
.with_context(|| format!("Undefined atlas reference: {}", &from.src))?;
let entry = {
let index = context.get_mut::<ModelIndex, _>(TypeId::of::<()>(), Default::default);
index.get(src_id, from.name.as_str())
};
let model_ref = match entry {
Some(value) => value,
None => anyhow::bail!("Undefined model reference: {}/{}", &from.src, &from.name),
};
Ok(model_ref)
}
}
}
pub fn to_path(variant: &str, spritesheet_id: SpritesheetId) -> String {
format!("assets/{}/{:08x}.png", variant, spritesheet_id.value())
}