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
use crate::prelude::*;
#[derive(Clone, Serialize, Deserialize)]
#[serde(remote = "Color")]
pub struct ColorDef {
#[serde(rename = "red", alias = "r")]
pub r: f32,
#[serde(rename = "green", alias = "g")]
pub g: f32,
#[serde(rename = "blue", alias = "b")]
pub b: f32,
#[serde(rename = "alpha", alias = "a")]
pub a: f32,
}
impl From<Color> for ColorDef {
fn from(other: Color) -> Self {
ColorDef {
r: other.r,
g: other.g,
b: other.b,
a: other.a,
}
}
}
impl From<ColorDef> for Color {
fn from(other: ColorDef) -> Self {
Color {
r: other.r,
g: other.g,
b: other.b,
a: other.a,
}
}
}
pub mod opt_color {
use super::Color;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
pub fn serialize<S>(value: &Option<Color>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
#[derive(Serialize)]
struct Helper<'a>(#[serde(with = "super::ColorDef")] &'a Color);
value.as_ref().map(Helper).serialize(serializer)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Color>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct Helper(#[serde(with = "super::ColorDef")] Color);
let helper = Option::deserialize(deserializer)?;
Ok(helper.map(|Helper(external)| external))
}
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(remote = "Animation")]
pub struct AnimationDef {
pub name: String,
pub row: u32,
pub frames: u32,
pub fps: u32,
}
impl From<&Animation> for AnimationDef {
fn from(other: &Animation) -> Self {
AnimationDef {
name: other.name.clone(),
row: other.row,
frames: other.frames,
fps: other.fps,
}
}
}
impl From<AnimationDef> for Animation {
fn from(other: AnimationDef) -> Self {
Animation {
name: other.name,
row: other.row,
frames: other.frames,
fps: other.fps,
}
}
}
pub mod vec_animation {
use super::{Animation, AnimationDef};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
pub fn serialize<S>(value: &[Animation], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
#[derive(Serialize)]
struct Helper<'a>(#[serde(with = "AnimationDef")] &'a Animation);
value
.iter()
.map(Helper)
.collect::<Vec<Helper>>()
.serialize(serializer)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<Animation>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct Helper(#[serde(with = "AnimationDef")] Animation);
let helper = Vec::deserialize(deserializer)?;
Ok(helper
.iter()
.map(|Helper(external)| external.clone())
.collect())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(remote = "FilterMode")]
pub enum FilterModeDef {
#[serde(alias = "linear")]
Linear,
#[serde(alias = "nearest_neighbor")]
Nearest,
}
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
#[serde(remote = "UniformType", rename_all = "snake_case")]
pub enum UniformTypeDef {
#[serde(alias = "float")]
Float1,
#[serde(alias = "vec2")]
Float2,
#[serde(alias = "vec3")]
Float3,
#[serde(alias = "vec4")]
Float4,
#[serde(alias = "int")]
Int1,
Int2,
Int3,
Int4,
Mat4,
}