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
use super::Command;

use cgmath::{Vector2};

#[derive(PartialEq, Debug)]
pub enum EventType {
    Sprite,
    Animation,
    Sample,
    Unknown,
}

#[derive(Debug)]
pub enum LayerType {
    Background,
    Fail,
    Pass,
    Foreground,
    Overlay,
    Samples,

    Unknown,
}

#[derive(Debug)]
pub enum OriginType {
    TopLeft,
    TopCentre,
    TopRight,
    CentreLeft,
    Centre,
    CentreRight,
    BottomLeft,
    BottomCentre,
    BottomRight,

    Custom,
}

#[derive(Debug)]
pub enum LoopType {
    LoopForever,
    LoopOnce,
}

#[derive(Debug)]
pub struct Event {
    pub etype: EventType,
    pub layer: LayerType,
    /* Event: Shared */
    pub file_path: String,
    pub pos: Vector2<f32>,
    pub origin: OriginType,
    pub commands: Vec<Command>,

    /* Event: Animation */
    pub frame_count: i32,
    pub frame_delay: f64,
    pub loop_type: LoopType,

    /* Event: Sample */
    pub time: i32,
    pub volume: i32,
}

impl Event {
    pub fn new() -> Event {
        Event {
            etype: EventType::Unknown,
            layer: LayerType::Unknown,
            file_path: String::default(),
            pos: Vector2::<f32>::new(0.0, 0.0),
            origin: OriginType::TopLeft,
            commands: Vec::<Command>::new(),
            frame_count: 0,
            frame_delay: 0.0,
            loop_type: LoopType::LoopOnce,
            time: 0,
            volume: 0,
        }
    }

    pub fn from_string(data: String) -> Event {
        let columns: Vec<&str> = data.split(",").collect();

        let event_type = match columns[0] {
            "Sprite" => EventType::Sprite,
            "Animation" => EventType::Animation,
            "Sample" => EventType::Sample,

            _ => EventType::Unknown,
        };

        let event_layer = match columns[1] {
            "Background" => LayerType::Background,
            "Fail" => LayerType::Fail,
            "Pass" => LayerType::Pass,
            "Foreground" => LayerType::Foreground,
            "Overlay" => LayerType::Overlay,
            "Samples" => LayerType::Samples,

            _ => LayerType::Unknown,
        };

        let event_origin = match columns[2] {
            "TopLeft" => OriginType::TopLeft,
            "TopCentre" => OriginType::TopCentre,
            "TopRight" => OriginType::TopRight,
            "CentreLeft" => OriginType::CentreLeft,
            "Centre" => OriginType::Centre,
            "CentreRight" => OriginType::CentreRight,
            "BottomLeft" => OriginType::BottomLeft,
            "BottomCentre" => OriginType::BottomCentre,
            "BottomRight" => OriginType::BottomRight,

            _ => OriginType::Custom,
        };

        let file_path = columns[3].trim_matches('"');

        let position_x = columns[4].parse::<f32>().unwrap(); // TODO: test for errors
        let position_y = columns[5].parse::<f32>().unwrap(); // TODO: here to.

        let position = Vector2::<f32>::new(position_x, position_y);
        match event_type {
            EventType::Sprite => {
                return Event {
                    etype: event_type,
                    layer: event_layer,

                    /* Event: Shared */
                    file_path: file_path.to_string(),
                    pos: position,
                    origin: event_origin,
                    commands: Vec::<Command>::new(),
                    /* Event: Animation */
                    frame_count: 0,
                    frame_delay: 0.0,
                    loop_type: LoopType::LoopOnce,
                    /* Event: Sample */
                    time: 0,
                    volume: 0,
                };
            }
            EventType::Animation => {
                let mut loop_type = LoopType::LoopForever;
                if columns.len() > 8 {
                    loop_type = match columns[8] {
                        "LoopOnce" => LoopType::LoopOnce,

                        _ => LoopType::LoopForever,
                    }
                }

                return Event {
                    etype: event_type,
                    layer: event_layer,
                    file_path: file_path.to_string(),
                    pos: position,
                    origin: event_origin,
                    commands: Vec::<Command>::new(),

                    /* Event: Animation */
                    frame_count: columns[6].parse::<i32>().unwrap(),
                    frame_delay: columns[7].parse::<f64>().unwrap(),
                    loop_type: loop_type,
                    /* Event: Sample */
                    time: 0,
                    volume: 0,
                };
            }

            _ => println!("Unimplemented Storyboard Event! {:#?}", event_type),
        };

        Event::new() // Return empty event if not implemented
    }
}