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
use crate::{
    c::{spEvent, spEventData},
    c_interface::{NewFromPtr, SyncPtr},
    TrackEntry,
};

#[allow(unused_imports)]
use crate::AnimationState;

/// A wrapper for [`Event`] that makes events slightly nicer to work with in Rust.
///
/// To receive events, see [`AnimationState::set_listener`].
pub enum AnimationEvent<'a> {
    Start {
        /// The track this event originated from.
        track_entry: TrackEntry,
    },
    Interrupt {
        /// The track this event originated from.
        track_entry: TrackEntry,
    },
    End {
        /// The track this event originated from.
        track_entry: TrackEntry,
    },
    Complete {
        /// The track this event originated from.
        track_entry: TrackEntry,
    },
    Dispose {
        /// The track this event originated from.
        track_entry: TrackEntry,
    },
    Event {
        /// The track this event originated from.
        track_entry: TrackEntry,
        /// The name of the event, which is unique across all events in the skeleton.
        name: &'a str,
        /// The animation time this event was keyed.
        time: f32,
        /// The event's int value.
        int: i32,
        /// The event's float value.
        float: f32,
        /// The event's string value or an empty string.
        string: &'a str,
        /// The event's audio path or an empty string.
        audio_path: &'a str,
        /// The event's audio volume.
        volume: f32,
        /// The event's audio balance.
        balance: f32,
        /// The raw event data.
        event: Event,
    },
}

/// Events fired from animations.
///
/// [Spine API Reference](http://esotericsoftware.com/spine-api-reference#Event)
///
/// To receive events, see [`AnimationState::set_listener`].
#[derive(Debug)]
pub struct Event {
    c_event: SyncPtr<spEvent>,
}

impl NewFromPtr<spEvent> for Event {
    unsafe fn new_from_ptr(c_event: *const spEvent) -> Self {
        Self {
            c_event: SyncPtr(c_event as *mut spEvent),
        }
    }
}

impl Event {
    c_accessor_tmp_ptr!(data, data_mut, data, EventData, spEventData);
    c_accessor!(time, time, f32);
    c_accessor!(int_value, intValue, i32);
    c_accessor!(float_value, floatValue, f32);
    c_accessor_string!(string_value, stringValue);
    c_accessor!(volume, volume, f32);
    c_accessor!(balance, balance, f32);
    c_ptr!(c_event, spEvent);
}

/// Static event data imported from Spine.
///
/// [Spine API Reference](http://esotericsoftware.com/spine-api-reference#EventData)
#[derive(Debug)]
pub struct EventData {
    c_event_data: SyncPtr<spEventData>,
}

impl NewFromPtr<spEventData> for EventData {
    unsafe fn new_from_ptr(c_event_data: *const spEventData) -> Self {
        Self {
            c_event_data: SyncPtr(c_event_data as *mut spEventData),
        }
    }
}

impl EventData {
    c_accessor_string!(name, name);
    c_accessor!(int_value, intValue, i32);
    c_accessor!(float_value, floatValue, f32);
    c_accessor_string!(string_value, stringValue);
    c_accessor_string!(audio_path, audioPath);
    c_accessor!(volume, volume, f32);
    c_accessor!(balance, balance, f32);
    c_ptr!(c_event_data, spEventData);
}