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
use crate::{Color, Position, Scale};
use anim::{animation::Interpolation, transition::Transition};
use core::{prefab::Prefab, Ignite, Scalar};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

pub type CharacterStyle = Transition<String>;

#[derive(Ignite, Debug, Clone, Serialize, Deserialize)]
pub struct Character {
    name: String,
    /// {style name: image name}
    pub styles: HashMap<String, String>,
    #[serde(default)]
    style: CharacterStyle,
    #[serde(default)]
    visibility: Interpolation<Scalar>,
    #[serde(default)]
    name_color: Interpolation<Color>,
    /// position in screen space percentage.
    #[serde(default)]
    position: Interpolation<Position>,
    /// alignment tells where in the image space pivot is located (percentage of image size).
    #[serde(default)]
    alignment: Interpolation<Position>,
    #[serde(default)]
    rotation: Interpolation<Scalar>,
    #[serde(default)]
    scale: Interpolation<Scale>,
    #[serde(skip)]
    pub(crate) dirty: bool,
}

impl Prefab for Character {
    fn post_from_prefab(&mut self) {
        self.dirty = true;
    }
}

impl Default for Character {
    fn default() -> Self {
        Self {
            name: Default::default(),
            styles: Default::default(),
            style: Default::default(),
            visibility: Interpolation::instant(1.0),
            name_color: Interpolation::instant((1.0, 1.0, 1.0).into()),
            position: Default::default(),
            alignment: Default::default(),
            rotation: Default::default(),
            scale: Interpolation::instant((1.0, 1.0).into()),
            dirty: true,
        }
    }
}

impl Character {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_owned(),
            ..Default::default()
        }
    }

    pub fn initialize(&mut self) {
        self.style.end();
        self.visibility.end();
        self.name_color.end();
        self.position.end();
        self.alignment.end();
        self.rotation.end();
        self.scale.end();
    }

    pub fn is_dirty(&self) -> bool {
        self.dirty
            || self.style.in_progress()
            || self.visibility.in_progress()
            || self.name_color.in_progress()
            || self.position.in_progress()
            || self.alignment.in_progress()
            || self.rotation.in_progress()
            || self.scale.in_progress()
    }

    pub fn rebuild(&mut self) {
        self.dirty = true;
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn set_name(&mut self, value: String) {
        self.name = value;
        self.dirty = true;
    }

    /// (style name, image name)
    pub fn styles(&self) -> impl Iterator<Item = (&str, &str)> {
        self.styles.iter().map(|(k, v)| (k.as_str(), v.as_str()))
    }

    /// (from style, phase, to style)
    pub fn style(&self) -> (&str, Scalar, &str) {
        let from = self.style.from().as_str();
        let to = self.style.to().as_str();
        let phase = self.style.phase();
        (from, phase, to)
    }

    pub fn style_transition(&self) -> &CharacterStyle {
        &self.style
    }

    pub fn style_transition_mut(&mut self) -> &mut CharacterStyle {
        &mut self.style
    }

    pub fn set_style(&mut self, value: String) {
        self.style.set(value);
        self.style.playing = true;
    }

    pub fn visibility(&self) -> Scalar {
        self.visibility.value()
    }

    pub fn visibility_anim(&self) -> &Interpolation<Scalar> {
        &self.visibility
    }

    pub fn visibility_anim_mut(&mut self) -> &mut Interpolation<Scalar> {
        &mut self.visibility
    }

    pub fn set_visibility(&mut self, value: Scalar) {
        self.visibility.set(value);
        self.visibility.playing = true;
    }

    pub fn hide(&mut self) {
        self.set_visibility(0.0);
    }

    pub fn show(&mut self) {
        self.set_visibility(1.0);
    }

    pub fn name_color(&self) -> Color {
        self.name_color.value()
    }

    pub fn name_color_anim(&self) -> &Interpolation<Color> {
        &self.name_color
    }

    pub fn name_color_anim_mut(&mut self) -> &mut Interpolation<Color> {
        &mut self.name_color
    }

    pub fn set_name_color(&mut self, value: Color) {
        self.name_color.set(value);
        self.name_color.playing = true;
    }

    pub fn position(&self) -> Position {
        self.position.value()
    }

    pub fn position_anim(&self) -> &Interpolation<Position> {
        &self.position
    }

    pub fn position_anim_mut(&mut self) -> &mut Interpolation<Position> {
        &mut self.position
    }

    pub fn set_position(&mut self, value: Position) {
        self.position.set(value);
        self.position.playing = true;
    }

    pub fn alignment(&self) -> Position {
        self.alignment.value()
    }

    pub fn alignment_anim(&self) -> &Interpolation<Position> {
        &self.alignment
    }

    pub fn alignment_anim_mut(&mut self) -> &mut Interpolation<Position> {
        &mut self.alignment
    }

    pub fn set_alignment(&mut self, value: Position) {
        self.alignment.set(value);
        self.alignment.playing = true;
    }

    pub fn rotation(&self) -> Scalar {
        self.rotation.value()
    }

    pub fn rotation_anim(&self) -> &Interpolation<Scalar> {
        &self.rotation
    }

    pub fn rotation_anim_mut(&mut self) -> &mut Interpolation<Scalar> {
        &mut self.rotation
    }

    pub fn set_rotation(&mut self, value: Scalar) {
        self.rotation.set(value);
        self.rotation.playing = true;
    }

    pub fn scale(&self) -> Scale {
        self.scale.value()
    }

    pub fn scale_anim(&self) -> &Interpolation<Scale> {
        &self.scale
    }

    pub fn scale_anim_mut(&mut self) -> &mut Interpolation<Scale> {
        &mut self.scale
    }

    pub fn set_scale(&mut self, value: Scale) {
        self.scale.set(value);
        self.scale.playing = true;
    }

    pub fn process(&mut self, delta_time: Scalar) {
        self.dirty = false;
        self.style.process(delta_time);
        self.visibility.process(delta_time);
        self.name_color.process(delta_time);
        self.position.process(delta_time);
        self.alignment.process(delta_time);
        self.rotation.process(delta_time);
        self.scale.process(delta_time);
    }
}