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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! Functions and types for parsing Ogmo projects.


use std::fs;
use std::path::{Path, PathBuf};

use hashbrown::HashMap;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

use crate::{Error, Vec2};

/// An Ogmo project.

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Project {
    /// The name of the Ogmo project.

    pub name: String,

    /// The version of Ogmo used to export this project.

    pub ogmo_version: String,

    /// An array of paths that hold the project's levels.

    pub level_paths: Vec<PathBuf>,

    /// The project's background color.

    pub background_color: String,

    /// The color of the grid displayed in the editor.

    pub grid_color: String,

    /// Whether the project describes angles in radians or degrees.

    pub angles_radians: bool,

    /// The maximum depth that the editor will search for levels in its file tree.

    pub directory_depth: i32,

    /// The default grid size for newly created layers.

    pub layer_grid_default_size: Vec2<i32>,

    /// The default size of newly created levels in the editor.

    pub level_default_size: Vec2<i32>,

    /// The minimum size of a level.

    pub level_min_size: Vec2<i32>,

    /// The maximum size of a level.

    pub level_max_size: Vec2<i32>,

    /// The value templates for the project's levels.

    pub level_values: Vec<ValueTemplate>,

    /// The default exported file type of a level.

    pub default_export_mode: String,

    /// Whether the project's files will be exported from the editor in a compact format.

    pub compact_export: bool,

    /// The tags that can be attached to entities.

    pub entity_tags: Vec<String>,

    /// The project's layer templates.

    pub layers: Vec<LayerTemplate>,

    /// The project's entity templates.

    pub entities: Vec<EntityTemplate>,

    /// The project's tilesets.

    pub tilesets: Vec<Tileset>,
}

impl Project {
    /// Parses an Ogmo project from a JSON string.

    ///

    /// # Errors

    ///

    /// * `Error::Json` will be returned if deserialization fails.

    pub fn from_json(s: &str) -> Result<Project, Error> {
        serde_json::from_str(s).map_err(Error::Json)
    }

    /// Parses an Ogmo project from a file.

    ///

    /// # Errors

    ///

    /// * `Error::Io` will be returned if the file cannot be read.

    /// * `Error::Json` will be returned if deserialization fails.

    pub fn from_file(path: impl AsRef<Path>) -> Result<Project, Error> {
        let json = fs::read_to_string(path).map_err(Error::Io)?;
        Project::from_json(&json)
    }

    /// Writes the Ogmo project to a JSON string.

    ///

    /// # Errors

    ///

    /// * `Error::Json` will be returned if serialization fails.

    pub fn to_json(&self) -> Result<String, Error> {
        serde_json::to_string(self).map_err(Error::Json)
    }

    /// Writes the Ogmo project to a pretty-printed JSON string.

    ///

    /// # Errors

    ///

    /// * `Error::Json` will be returned if serialization fails.

    pub fn to_json_pretty(&self) -> Result<String, Error> {
        serde_json::to_string_pretty(self).map_err(Error::Json)
    }
}

/// A template for a value.

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(tag = "definition")]
pub enum ValueTemplate {
    /// A boolean value template.

    Boolean(BooleanValueTemplate),

    /// A color value template.

    Color(ColorValueTemplate),

    /// An enum value template.

    Enum(EnumValueTemplate),

    /// An integer value template.

    Integer(IntegerValueTemplate),

    /// A float value template.

    Float(FloatValueTemplate),

    /// A string value template.

    String(StringValueTemplate),

    /// A text value template.

    Text(TextValueTemplate),
}

impl ValueTemplate {
    /// Gets the name of the value template.

    pub fn name(&self) -> &str {
        match self {
            ValueTemplate::Boolean(data) => &data.name,
            ValueTemplate::Color(data) => &data.name,
            ValueTemplate::Enum(data) => &data.name,
            ValueTemplate::Integer(data) => &data.name,
            ValueTemplate::Float(data) => &data.name,
            ValueTemplate::String(data) => &data.name,
            ValueTemplate::Text(data) => &data.name,
        }
    }
}

/// A boolean value template.

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BooleanValueTemplate {
    /// The name of the value.

    pub name: String,

    /// The default value.

    pub defaults: bool,
}

/// A color value template.

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ColorValueTemplate {
    /// The name of the value.

    pub name: String,

    /// The default value.

    pub defaults: String,

    /// Whether the alpha component will be included in the color.

    pub include_alpha: bool,
}

/// An enum value template.

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EnumValueTemplate {
    /// The name of the value.

    pub name: String,

    /// The default value.

    pub defaults: i32,

    /// The available choices for the enum.

    pub choices: Vec<String>,
}

/// An integer value template.

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct IntegerValueTemplate {
    /// The name of the value.

    pub name: String,

    /// The default value.

    pub defaults: i32,

    /// Whether the value is bounded with a min and/or max value.

    pub bounded: bool,

    /// The minimum value.

    pub min: i32,

    /// The maximum value.

    pub max: i32,
}

/// A float value template.

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FloatValueTemplate {
    /// The name of the value.

    pub name: String,

    /// The default value.

    pub defaults: f32,

    /// Whether the value is bounded with a min and/or max value.

    pub bounded: bool,

    /// The minimum value.

    pub min: f32,

    /// The maximum value.

    pub max: f32,
}

/// A string value template.

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StringValueTemplate {
    /// The name of the value.

    pub name: String,

    /// The default value.

    pub defaults: String,

    /// The maximum length.

    pub max_length: i32,

    /// Whether whitespace should be trimmed from the beginning and end of the string.

    pub trim_whitespace: bool,
}

/// A text value template.

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TextValueTemplate {
    /// The name of the value.

    pub name: String,

    /// The default value.

    pub defaults: String,
}

/// A template for a layer.

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum LayerTemplate {
    /// A tile layer template.

    Tile(TileLayerTemplate),

    /// A grid layer template.

    Grid(GridLayerTemplate),

    /// An entity layer template.

    Entity(EntityLayerTemplate),

    /// A decal layer template.

    Decal(DecalLayerTemplate),
}

impl LayerTemplate {
    /// Gets the name of the layer template.

    pub fn name(&self) -> &str {
        match self {
            LayerTemplate::Tile(data) => &data.name,
            LayerTemplate::Grid(data) => &data.name,
            LayerTemplate::Entity(data) => &data.name,
            LayerTemplate::Decal(data) => &data.name,
        }
    }
}

/// A tile layer template.

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "definition", rename = "tile")]
pub struct TileLayerTemplate {
    /// The name of the layer.

    pub name: String,

    /// The size of each cell in the layer's grid.

    pub grid_size: Vec2<i32>,

    /// The unique export ID of the layer.

    #[serde(rename = "exportID")]
    pub export_id: String,

    /// Whether the tile data is stored as IDs or co-oords.

    pub export_mode: ExportMode,

    /// Whether the tile data is stored as a 1D array or a 2D array.

    pub array_mode: ArrayMode,

    /// The default tileset for the layer.

    pub default_tileset: String,
}

/// A grid layer template.

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "definition", rename = "grid")]
pub struct GridLayerTemplate {
    /// The name of the layer.

    pub name: String,

    /// The size of each cell in the layer's grid.

    pub grid_size: Vec2<i32>,

    /// The unique export ID of the layer.

    #[serde(rename = "exportID")]
    pub export_id: String,

    /// Whether the tile data is stored as a 1D array or a 2D array.

    pub array_mode: ArrayMode,

    /// Descriptions for the available grid cells.

    pub legend: HashMap<String, String>,
}

/// An entity layer template.

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "definition", rename = "entity")]
pub struct EntityLayerTemplate {
    /// The name of the layer.

    pub name: String,

    /// The size of each cell in the layer's grid.

    pub grid_size: Vec2<i32>,

    /// The unique export ID of the layer.

    #[serde(rename = "exportID")]
    pub export_id: String,

    /// Tags that are required for an entity to be displayed on this layer.

    pub required_tags: Vec<String>,

    /// Tags that must not be present for an entity to be displayed on this layer.

    pub excluded_tags: Vec<String>,
}

/// A decal layer template.

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "definition", rename = "decal")]
pub struct DecalLayerTemplate {
    /// The name of the layer.

    pub name: String,

    /// The size of each cell in the layer's grid.

    pub grid_size: Vec2<i32>,

    /// The unique export ID of the layer.

    #[serde(rename = "exportID")]
    pub export_id: String,

    /// The path to search for decal images, relative to the project

    pub folder: PathBuf,

    /// Whether image sequences are included as available decals.

    pub include_image_sequence: bool,

    /// Whether this layer's decals are scalable.

    pub scaleable: bool,

    /// Whether this layer's decals are rotatable.

    pub rotatable: bool,

    /// Value templates associated with this decal layer.

    pub values: Vec<ValueTemplate>,
}

/// Defines whether tile data is stored as IDs or co-oords.

#[derive(Clone, Debug, Deserialize_repr, Serialize_repr)]
#[repr(u8)]
pub enum ExportMode {
    /// The tile data is represented by IDs (counting left to right, top to bottom).

    Ids = 0,

    /// The tile data is represented by co-ordinates.

    Coords = 1,
}

/// Defines whether tile data is stored as a 1D array or a 2D array.

#[derive(Clone, Debug, Deserialize_repr, Serialize_repr)]
#[repr(u8)]
pub enum ArrayMode {
    /// The tile data is stored in a 1D array.

    One = 0,

    /// The tile data is stored in a 2D array.

    Two = 1,
}

/// A template for an entity.

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EntityTemplate {
    /// The name of the entity.

    pub name: String,

    /// The unique export ID of the entity.

    #[serde(rename = "exportID")]
    pub export_id: String,

    /// The maximum number of instances. 0 to ignore.

    pub limit: i32,

    /// The size of the entity.

    pub size: Vec2<f32>,

    /// The origin of the entity.

    pub origin: Vec2<f32>,

    /// Whether the entity is anchored to the origin.

    pub origin_anchored: bool,

    /// The shape of the entity.

    pub shape: Shape,

    /// The color of the entity's icon.

    pub color: String,

    /// Whether the icon should tile on the X axis.

    pub tile_x: bool,

    /// Whether the icon should tile on the Y axis.

    pub tile_y: bool,

    /// The tiled icon size.

    pub tile_size: Vec2<f32>,

    /// Whether the entity is resizable on the X axis.

    pub resizeable_x: bool,

    /// Whether the entity is resizable on the Y axis.

    pub resizeable_y: bool,

    /// Whether the entity is rotatable

    pub rotatable: bool,

    /// The interval of rotation.

    pub rotation_degrees: f32,

    /// Whether the entity can be flipped on the X axis.

    pub can_flip_x: bool,

    /// Whether the entity can be flipped on the Y axis.

    pub can_flip_y: bool,

    /// Whether the entity's color can be set.

    pub can_set_color: bool,

    /// Whether the entity has nodes.

    pub has_nodes: bool,

    /// The maximum number of nodes. 0 to ignore.

    pub node_limit: i32,

    /// Whether to display nodes.

    pub node_display: i32,

    /// Whether to display ghosts.

    pub node_ghost: bool,

    /// The entity's tags.

    pub tags: Vec<String>,

    /// The entity's custom values.

    pub values: Vec<ValueTemplate>,

    /// The entity's texture.

    #[serde(skip_serializing_if = "Option::is_none")]
    pub texture: Option<String>,

    /// The entity's texture, encoded in base 64.

    #[serde(skip_serializing_if = "Option::is_none")]
    pub texture_image: Option<String>,
}

/// An entity's shape.

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Shape {
    /// The shape's label.

    pub label: String,

    /// The points that make up the shape.

    pub points: Vec<Vec2<f32>>,
}

/// A tileset.

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Tileset {
    /// The name of the tileset.

    pub label: String,

    /// The path to the tileset's image, relative to the project's path.

    pub path: PathBuf,

    /// The tileset's image, encoded in base 64.

    pub image: String,

    /// The width of each tile in the tileset.

    pub tile_width: i32,

    /// The height of each tile in the tileset.

    pub tile_height: i32,

    /// The number of empty pixels that seperate each tile on the X axis in the tileset.

    pub tile_separation_x: i32,

    /// The number of empty pixels that seperate each tile on the Y axis in the tileset.

    pub tile_separation_y: i32,
}

impl Tileset {
    /// Returns an iterator which yields the position of each tile in the tileset.

    ///

    /// As the Ogmo project doesn't store the width and height of the texture (only the

    /// path to it), you must provide these values yourself.

    pub fn tile_coords(
        &self,
        texture_width: i32,
        texture_height: i32,
    ) -> impl Iterator<Item = Vec2<i32>> + '_ {
        let step_x = self.tile_width + self.tile_separation_x;
        let step_y = self.tile_height + self.tile_separation_y;

        let tiles_x = texture_width / step_x;
        let tiles_y = texture_height / step_y;

        (0..tiles_y).flat_map(move |tile_y| {
            (0..tiles_x).map(move |tile_x| {
                let x = tile_x * step_x;
                let y = tile_y * step_y;

                Vec2 { x, y }
            })
        })
    }
}