osb 0.3.0

A fast, fully-fledged, scalable and secure implementation of the .osb storyboard file format
Documentation
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
use crate::event::*;
use crate::utils::{Number, Vec2};
use crate::Layer;
use crate::Origin;

struct EventCollection {
    move_: Vec<Move>,
    movex_: Vec<MoveX>,
    movey_: Vec<MoveY>,
    fade_: Vec<Fade>,
    rotate_: Vec<Rotate>,
    scale_: Vec<Scale>,
    scalevec_: Vec<ScaleVec>,
    color_: Vec<Color>,
    hflip_: Vec<HFlip>,
    vflip_: Vec<VFlip>,
    additive_: Vec<Additive>,
}

fn events_to_str<T>(events: &Vec<T>) -> String
where
    T: Event,
{
    events
        .iter()
        .map(|event| event.to_line() + "\n")
        .collect::<Vec<String>>()
        .join("")
}

impl EventCollection {
    pub fn new() -> Self {
        Self {
            move_: Vec::<Move>::new(),
            movex_: Vec::<MoveX>::new(),
            movey_: Vec::<MoveY>::new(),
            fade_: Vec::<Fade>::new(),
            rotate_: Vec::<Rotate>::new(),
            scale_: Vec::<Scale>::new(),
            scalevec_: Vec::<ScaleVec>::new(),
            color_: Vec::<Color>::new(),
            hflip_: Vec::<HFlip>::new(),
            vflip_: Vec::<VFlip>::new(),
            additive_: Vec::<Additive>::new(),
        }
    }

    pub fn to_str(&self) -> String {
        format!(
            "{}{}{}{}{}{}{}{}{}{}{}",
            events_to_str(&self.move_),
            events_to_str(&self.movex_),
            events_to_str(&self.movey_),
            events_to_str(&self.fade_),
            events_to_str(&self.rotate_),
            events_to_str(&self.scale_),
            events_to_str(&self.scalevec_),
            events_to_str(&self.color_),
            events_to_str(&self.hflip_),
            events_to_str(&self.vflip_),
            events_to_str(&self.additive_),
        )
    }
}

/// The struct corresponding to sprites
pub struct Sprite {
    events: EventCollection,
    current_depth: usize,
    path: String,
    pos: Vec2,
    layer: Layer,
    origin: Origin,
    start_time: Option<i32>,
    end_time: Option<i32>,
}

// Adding an event to a sprite
macro_rules! add_event {
    ($sprite:ident, $event:ident, $events:expr) => {
        // Adjusting sprite's start and end values
        let (event_start, event_end) = ($event.get_start_time(), $event.get_end_time());
        match $sprite.start_time {
            Some(sprite_start) => {
                if event_start < sprite_start {
                    $sprite.start_time = Some(event_start)
                }
            }
            None => $sprite.start_time = Some(event_start),
        }

        match $sprite.end_time {
            Some(sprite_end) => {
                if sprite_end < event_end {
                    $sprite.end_time = Some(event_end)
                }
            }
            None => $sprite.end_time = Some(event_end),
        }

        // Pushing it to the events
        $event.set_depth($sprite.current_depth);
        $events.push($event);
    };
}

impl Sprite {
    /// Initializes a new `Sprite`
    ///
    /// See [trait implementations](#trait-implementations) to see how you can create a sprite
    pub fn new<T>(args: T) -> Self
    where
        T: Into<Sprite>,
    {
        args.into()
    }

    /// Performs the event [`Move`] to a `Sprite`
    ///
    /// ```
    /// use osb::{Sprite, Easing, utils::Vec2};
    ///
    /// let mut sprite = Sprite::new("res/sprite.png");
    ///
    /// // There's a `Vec2` type you can use if you wish
    /// sprite.move_((Easing::Out, 0, 1000, Vec2::from(0, 0), Vec2::from(320, 240)));
    /// // But you're not forced to! Giving pairs of integers automatically translates to a `Vec2`
    /// sprite.move_((Easing::QuadInOut, 1000, 2000, 320, 240, 100, 100));
    /// // And of course you can use a static move too
    /// sprite.move_((3000, Vec2::from(320, 240)));
    /// // Please refer to the trait implementations of the event to see everything you can do
    /// ```
    pub fn move_<T>(&mut self, args: T)
    where
        T: Into<Move>,
    {
        let mut event = args.into();
        add_event!(self, event, self.events.move_);
    }

    /// Performs the event [`MoveX`] to a `Sprite`
    ///
    /// ```
    /// use osb::{Sprite, Easing, utils::Vec2};
    ///
    /// let mut sprite = Sprite::new("res/sprite.png");
    /// sprite.movex_((0, 320));
    /// // Please refer to the trait implementations of the event to see everything you can do
    /// ```
    pub fn movex_<T>(&mut self, args: T)
    where
        T: Into<MoveX>,
    {
        let mut event = args.into();
        add_event!(self, event, self.events.movex_);
    }

    /// Performs the event [`MoveY`] to a `Sprite`
    ///
    /// ```
    /// use osb::{Sprite, Easing, utils::Vec2};
    ///
    /// let mut sprite = Sprite::new("res/sprite.png");
    /// sprite.movey_((0, 240));
    /// // Please refer to the trait implementations of the event to see everything you can do
    /// ```
    pub fn movey_<T>(&mut self, args: T)
    where
        T: Into<MoveY>,
    {
        let mut event = args.into();
        add_event!(self, event, self.events.movey_);
    }

    /// Performs the event [`Fade`] to a `Sprite`
    ///
    /// ```
    /// use osb::{Sprite, Easing, utils::Vec2};
    ///
    /// let mut sprite = Sprite::new("res/sprite.png");
    /// sprite.fade_((0, 1));
    /// // Please refer to the trait implementations of the event to see everything you can do
    /// ```
    pub fn fade_<T>(&mut self, args: T)
    where
        T: Into<Fade>,
    {
        let mut event = args.into();
        add_event!(self, event, self.events.fade_);
    }

    /// Performs the event [`Rotate`] to a `Sprite`
    ///
    /// ```
    /// use osb::{Sprite, Easing, utils::Vec2};
    /// use std::f32::consts::PI;
    ///
    /// let mut sprite = Sprite::new("res/sprite.png");
    /// sprite.rotate_((0, PI));
    /// // Please refer to the trait implementations of the event to see everything you can do
    /// ```
    pub fn rotate_<T>(&mut self, args: T)
    where
        T: Into<Rotate>,
    {
        let mut event = args.into();
        add_event!(self, event, self.events.rotate_);
    }

    /// Performs the event [`Scale`] to a `Sprite`
    ///
    /// ```
    /// use osb::{Sprite, Easing, utils::Vec2};
    ///
    /// let mut sprite = Sprite::new("res/sprite.png");
    /// sprite.scale_((0, 1));
    /// // Please refer to the trait implementations of the event to see everything you can do
    /// ```
    pub fn scale_<T>(&mut self, args: T)
    where
        T: Into<Scale>,
    {
        let mut event = args.into();
        add_event!(self, event, self.events.scale_);
    }

    /// Performs the event [`ScaleVec`] to a `Sprite`
    ///
    /// ```
    /// use osb::{Sprite, Easing, utils::Vec2};
    ///
    /// let mut sprite = Sprite::new("res/sprite.png");
    /// // There's a `Vec2` type you can use if you wish
    /// sprite.scalevec_((Easing::Out, 0, 1000, Vec2::from(1, 0), Vec2::from(1, 1)));
    /// // But you're not forced to! Giving pairs of integers automatically translates to a `Vec2`
    /// sprite.scalevec_((Easing::QuadInOut, 1000, 2000, 1, 0, 1, 1));
    /// // And of course you can use a static ScaleVec too
    /// sprite.scalevec_((3000, Vec2::from(1, 0.5)));
    /// // Please refer to the trait implementations of the event to see everything you can do
    /// ```
    pub fn scalevec_<T>(&mut self, args: T)
    where
        T: Into<ScaleVec>,
    {
        let mut event = args.into();
        add_event!(self, event, self.events.scalevec_);
    }

    /// Performs the event [`Color`] to a `Sprite`
    ///
    /// ```
    /// use osb::{Sprite, Easing, utils::Color};
    ///
    /// let mut sprite = Sprite::new("res/sprite.png");
    /// // There's a `Color` type you can use if you wish
    /// sprite.color_((Easing::Out, 0, 1000, Color::white(), Color::red()));
    /// // But you're not forced to! Giving pairs of integers automatically translates to a `Color`
    /// sprite.color_((Easing::QuadInOut, 1000, 2000, 255, 255, 255, 255, 0, 0));
    /// // And of course you can use a static Color too
    /// sprite.color_((3000, Color::green()));
    /// // Please refer to the trait implementations of the event to see everything you can do
    /// ```
    pub fn color_<T>(&mut self, args: T)
    where
        T: Into<Color>,
    {
        let mut event = args.into();
        add_event!(self, event, self.events.color_);
    }

    /// Performs the event [`HFlip`] to a `Sprite`
    ///
    /// ```
    /// use osb::{Sprite, Easing, utils::Vec2};
    ///
    /// let mut sprite = Sprite::new("res/sprite.png");
    /// sprite.hflip_((0, 1000));
    /// // Please refer to the trait implementations of the event to see everything you can do
    /// ```
    pub fn hflip_<T>(&mut self, args: T)
    where
        T: Into<HFlip>,
    {
        let mut event = args.into();
        add_event!(self, event, self.events.hflip_);
    }

    /// Performs the event [`VFlip`] to a `Sprite`
    ///
    /// ```
    /// use osb::{Sprite, Easing, utils::Vec2};
    ///
    /// let mut sprite = Sprite::new("res/sprite.png");
    /// sprite.vflip_((0, 1000));
    /// // Please refer to the trait implementations of the event to see everything you can do
    /// ```
    pub fn vflip_<T>(&mut self, args: T)
    where
        T: Into<VFlip>,
    {
        let mut event = args.into();
        add_event!(self, event, self.events.vflip_);
    }

    /// Performs the event [`Additive`] to a `Sprite`
    ///
    /// ```
    /// use osb::{Sprite, Easing, utils::Vec2};
    ///
    /// let mut sprite = Sprite::new("res/sprite.png");
    /// sprite.additive_((0, 1000));
    /// // Please refer to the trait implementations of the event to see everything you can do
    /// ```
    pub fn additive_<T>(&mut self, args: T)
    where
        T: Into<Additive>,
    {
        let mut event = args.into();
        add_event!(self, event, self.events.additive_);
    }

    /// Returns the initial X position of a `Sprite`
    ///
    /// **Warning**: This does **not** return the X position in a certain time.
    ///
    /// Example:
    /// ```
    /// use osb::{utils::Number, Sprite};
    /// let mut sprite = Sprite::new("res/sprite.png");
    /// sprite.move_((0, 100, 100));
    /// assert_eq!(sprite.get_x(), Number::Int(320));
    /// ```
    pub fn get_x(&self) -> Number {
        self.pos.x
    }

    /// Returns the initial Y position of a `Sprite`
    ///
    /// **Warning**: This does **not** return the Y position in a certain time.
    ///
    /// Example:
    /// ```
    /// use osb::{utils::Number, Sprite};
    /// let mut sprite = Sprite::new("res/sprite.png");
    /// sprite.move_((0, 100, 100));
    /// assert_eq!(sprite.get_y(), Number::Int(240));
    /// ```
    pub fn get_y(&self) -> Number {
        self.pos.y
    }

    /// Returns the start time of the first event of a `Sprite`
    ///
    /// Example:
    /// ```
    /// use osb::Sprite;
    ///
    /// let mut sprite = Sprite::new("res/sprite.png");
    /// assert_eq!(sprite.start_time(), None);
    ///
    /// sprite.move_((100, 200, 0, 0, 320, 240));
    /// assert_eq!(sprite.start_time(), Some(100));
    ///
    /// sprite.fade_((0, 100, 0, 1));
    /// assert_eq!(sprite.start_time(), Some(0));
    /// ```
    pub fn start_time(&self) -> Option<i32> {
        self.start_time
    }

    /// Returns the end time of the first event of a `Sprite`
    ///
    /// Example:
    /// ```
    /// use osb::Sprite;
    ///
    /// let mut sprite = Sprite::new("res/sprite.png");
    /// assert_eq!(sprite.end_time(), None);
    ///
    /// sprite.move_((0, 100, 0, 0, 320, 240));
    /// assert_eq!(sprite.end_time(), Some(100));
    ///
    /// sprite.fade_((100, 200, 1, 0));
    /// assert_eq!(sprite.end_time(), Some(200));
    /// ```
    pub fn end_time(&self) -> Option<i32> {
        self.end_time
    }

    /// Returns the contents of the `Sprite`
    ///
    /// **Warning**: this method is not meant to be used
    pub fn to_str(&self) -> String {
        format!(
            "Sprite,{},{},\"{}\",{},{}\n{}",
            self.layer,
            self.origin,
            self.path,
            self.pos.x,
            self.pos.y,
            self.events.to_str()
        )
    }

    /// Sets the [`Layer`] of the `Sprite`
    ///
    /// **Warning**: this method is not meant to be used
    pub fn set_layer(&mut self, layer: Layer) {
        self.layer = layer;
    }
}

/// Creates a `Sprite` with the path of the file
///
/// Example:
/// ```
/// use osb::Sprite;
/// let path = String::from("res/sprite.png");
/// let mut sprite = Sprite::new(path);
/// ```
impl Into<Sprite> for String {
    fn into(self) -> Sprite {
        Sprite {
            events: EventCollection::new(),
            current_depth: 0,
            path: self,
            pos: Vec2::from(320, 240),
            layer: Layer::Background,
            origin: Origin::Centre,
            start_time: None,
            end_time: None,
        }
    }
}

/// Creates a `Sprite` with the path of the file
///
/// Example:
/// ```
/// use osb::Sprite;
/// let path = "res/sprite.png";
/// let mut sprite = Sprite::new(path);
/// ```
impl Into<Sprite> for &str {
    fn into(self) -> Sprite {
        Sprite {
            events: EventCollection::new(),
            current_depth: 0,
            path: String::from(self),
            pos: Vec2::from(320, 240),
            layer: Layer::Background,
            origin: Origin::Centre,
            start_time: None,
            end_time: None,
        }
    }
}

/// Creates a `Sprite` with the origin and path of the file
///
/// Example:
/// ```
/// use osb::{Origin, Sprite};
/// let origin = Origin::Centre;
/// let path = String::from("res/sprite.png");
/// let mut sprite = Sprite::new((origin, path));
/// ```
impl Into<Sprite> for (Origin, String) {
    fn into(self) -> Sprite {
        Sprite {
            events: EventCollection::new(),
            current_depth: 0,
            path: self.1,
            pos: Vec2::from(320, 240),
            layer: Layer::Background,
            origin: self.0,
            start_time: None,
            end_time: None,
        }
    }
}

/// Creates a `Sprite` with the origin and the path of the file
///
/// Example:
/// ```
/// use osb::{Origin, Sprite};
/// let origin = Origin::Centre;
/// let path = "res/sprite.png";
/// let mut sprite = Sprite::new((origin, path));
/// ```
impl Into<Sprite> for (Origin, &str) {
    fn into(self) -> Sprite {
        Sprite {
            events: EventCollection::new(),
            current_depth: 0,
            path: String::from(self.1),
            pos: Vec2::from(320, 240),
            layer: Layer::Background,
            origin: self.0,
            start_time: None,
            end_time: None,
        }
    }
}

/// Creates a `Sprite` with the path of the file and the original coordinates
///
/// Example:
/// ```
/// use osb::{utils::Vec2, Sprite};
/// let path = String::from("res/sprite.png");
/// let pos = Vec2::from(320, 240);
/// let mut sprite = Sprite::new((path, pos));
/// ```
impl Into<Sprite> for (String, Vec2) {
    fn into(self) -> Sprite {
        Sprite {
            events: EventCollection::new(),
            current_depth: 0,
            path: self.0,
            pos: self.1,
            layer: Layer::Background,
            origin: Origin::Centre,
            start_time: None,
            end_time: None,
        }
    }
}

/// Creates a `Sprite` with the path of the file and the original coordinates
///
/// Example:
/// ```
/// use osb::{utils::Vec2, Sprite};
/// let path = String::from("res/sprite.png");
/// let x = 320;
/// let y = 240;
/// let mut sprite = Sprite::new((path, x, y));
/// ```
impl<T, U> Into<Sprite> for (String, T, U)
where
    T: Into<Number>,
    U: Into<Number>,
{
    fn into(self) -> Sprite {
        Sprite {
            events: EventCollection::new(),
            current_depth: 0,
            path: self.0,
            pos: Vec2::from(self.1, self.2),
            layer: Layer::Background,
            origin: Origin::Centre,
            start_time: None,
            end_time: None,
        }
    }
}

/// Creates a `Sprite` with the path of the file and the original coordinates
///
/// Example:
/// ```
/// use osb::{utils::Vec2, Sprite};
/// let path = "res/sprite.png";
/// let pos = Vec2::from(320, 240);
/// let mut sprite = Sprite::new((path, pos));
/// ```
impl Into<Sprite> for (&str, Vec2) {
    fn into(self) -> Sprite {
        Sprite {
            events: EventCollection::new(),
            current_depth: 0,
            path: String::from(self.0),
            pos: self.1,
            layer: Layer::Background,
            origin: Origin::Centre,
            start_time: None,
            end_time: None,
        }
    }
}

/// Creates a `Sprite` with the path of the file and the original coordinates
///
/// Example:
/// ```
/// use osb::{utils::Vec2, Sprite};
/// let path = "res/sprite.png";
/// let x = 320;
/// let y = 240;
/// let mut sprite = Sprite::new((path, x, y));
/// ```
impl<T, U> Into<Sprite> for (&str, T, U)
where
    T: Into<Number>,
    U: Into<Number>,
{
    fn into(self) -> Sprite {
        Sprite {
            events: EventCollection::new(),
            current_depth: 0,
            path: String::from(self.0),
            pos: Vec2::from(self.1, self.2),
            layer: Layer::Background,
            origin: Origin::Centre,
            start_time: None,
            end_time: None,
        }
    }
}

/// Creates a `Sprite` with the origin, the path of the file and the original coordinates
///
/// Example:
/// ```
/// use osb::{utils::Vec2, Origin, Sprite};
/// let origin = Origin::Centre;
/// let path = String::from("res/sprite.png");
/// let pos = Vec2::from(320, 240);
/// let mut sprite = Sprite::new((origin, path, pos));
/// ```
impl Into<Sprite> for (Origin, String, Vec2) {
    fn into(self) -> Sprite {
        Sprite {
            events: EventCollection::new(),
            current_depth: 0,
            path: self.1,
            pos: self.2,
            layer: Layer::Background,
            origin: self.0,
            start_time: None,
            end_time: None,
        }
    }
}

/// Creates a `Sprite` with the origin, the path of the file and the original coordinates
///
/// Example:
/// ```
/// use osb::{utils::Vec2, Origin, Sprite};
/// let origin = Origin::Centre;
/// let path = String::from("res/sprite.png");
/// let x = 320;
/// let y = 240;
/// let mut sprite = Sprite::new((origin, path, x, y));
/// ```
impl<T, U> Into<Sprite> for (Origin, String, T, U)
where
    T: Into<Number>,
    U: Into<Number>,
{
    fn into(self) -> Sprite {
        Sprite {
            events: EventCollection::new(),
            current_depth: 0,
            path: self.1,
            pos: Vec2::from(self.2, self.3),
            layer: Layer::Background,
            origin: self.0,
            start_time: None,
            end_time: None,
        }
    }
}

/// Creates a `Sprite` with the origin, the path of the file and the original coordinates
///
/// Example:
/// ```
/// use osb::{utils::Vec2, Origin, Sprite};
/// let origin = Origin::Centre;
/// let path = "res/sprite.png";
/// let pos = Vec2::from(320, 240);
/// let mut sprite = Sprite::new((origin, path, pos));
/// ```
impl Into<Sprite> for (Origin, &str, Vec2) {
    fn into(self) -> Sprite {
        Sprite {
            events: EventCollection::new(),
            current_depth: 0,
            path: String::from(self.1),
            pos: self.2,
            layer: Layer::Background,
            origin: self.0,
            start_time: None,
            end_time: None,
        }
    }
}

/// Creates a `Sprite` with the origin, the path of the file and the original coordinates
///
/// Example:
/// ```
/// use osb::{utils::Vec2, Origin, Sprite};
/// let origin = Origin::Centre;
/// let path = "res/sprite.png";
/// let x = 320;
/// let y = 240;
/// let mut sprite = Sprite::new((origin, path, x, y));
/// ```
impl<T, U> Into<Sprite> for (Origin, &str, T, U)
where
    T: Into<Number>,
    U: Into<Number>,
{
    fn into(self) -> Sprite {
        Sprite {
            events: EventCollection::new(),
            current_depth: 0,
            path: String::from(self.1),
            pos: Vec2::from(self.2, self.3),
            layer: Layer::Background,
            origin: self.0,
            start_time: None,
            end_time: None,
        }
    }
}