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
//! Provides [`SkeletonController`], a helper struct for updating and drawing Spine skeletons.
//!
//! ```
//! use std::sync::Arc;
//!
//! use rusty_spine::{controller::*, *};
//!
//! fn main() {
//!     // Load and initialize the atlas and skeleton
//!     let atlas_path = "assets/spineboy/export/spineboy.atlas";
//!     let json_path = "assets/spineboy/export/spineboy-pro.json";
//!     let atlas = Arc::new(Atlas::new_from_file(atlas_path).unwrap());
//!     let skeleton_json = SkeletonJson::new(atlas);
//!     let skeleton_data = Arc::new(skeleton_json.read_skeleton_data_file(json_path).unwrap());
//!     let animation_state_data = Arc::new(AnimationStateData::new(skeleton_data.clone()));
//!
//!     // Create the controller
//!     let mut skeleton_controller =
//!         SkeletonController::new(skeleton_data.clone(), animation_state_data);
//!
//!     // Update for one frame
//!     skeleton_controller.update(0.016);
//!
//!     // Get renderable mesh data
//!     // Note: if slot_index is not important, use the much faster
//!     // `combined_renderables()` method instead
//!     let renderables = skeleton_controller.renderables();
//!     println!("Skeleton:");
//!     println!("");
//!     println!("  Atlas: {}", atlas_path);
//!     println!("  JSON: {}", json_path);
//!     println!("  Version: {}", skeleton_data.version());
//!     println!("  Hash: {}", skeleton_data.hash());
//!     println!("");
//!     println!("Renderables:");
//!     println!("");
//!     for renderable in renderables.iter() {
//!         let slot = skeleton_controller
//!             .skeleton
//!             .slot_at_index(renderable.slot_index)
//!             .unwrap();
//!         println!("  {}", slot.data().name());
//!         println!("    {} Vertices / UVs", renderable.vertices.len());
//!         println!("    {} Indices", renderable.indices.len());
//!         println!("    {:?} Blend Mode", renderable.blend_mode);
//!         println!("    {:?}", renderable.color);
//!         println!(
//!             "    {}",
//!             if renderable.premultiplied_alpha {
//!                 "Premultiplied Alpha"
//!             } else {
//!                 "No Premultiplied Alpha"
//!             }
//!         );
//!         println!("");
//!     }
//! }
//! ```

use std::{mem::take, sync::Arc};

use crate::{
    animation_state::AnimationState,
    animation_state_data::AnimationStateData,
    c::c_void,
    color::Color,
    draw::{ColorSpace, CombinedDrawer, CullDirection, SimpleDrawer},
    skeleton::Skeleton,
    skeleton_clipping::SkeletonClipping,
    skeleton_data::SkeletonData,
    BlendMode,
};

#[derive(Debug)]
pub struct SkeletonController {
    pub skeleton: Skeleton,
    pub animation_state: AnimationState,
    pub clipper: SkeletonClipping,
    pub settings: SkeletonControllerSettings,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SkeletonControllerSettings {
    /// Set to `true` if the textures are expected to have premultiplied alpha.
    pub premultiplied_alpha: bool,
    /// The cull direction to use for the vertices.
    pub cull_direction: CullDirection,
    /// The color space to use for the colors returned in [`SkeletonRenderable`] or  [`SkeletonCombinedRenderable`].
    pub color_space: ColorSpace,
}

impl Default for SkeletonControllerSettings {
    fn default() -> Self {
        Self {
            premultiplied_alpha: false,
            cull_direction: CullDirection::Clockwise,
            color_space: ColorSpace::SRGB,
        }
    }
}

impl SkeletonControllerSettings {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_premultiplied_alpha(self, premultiplied_alpha: bool) -> Self {
        Self {
            premultiplied_alpha,
            ..self
        }
    }

    pub fn with_cull_direction(self, cull_direction: CullDirection) -> Self {
        Self {
            cull_direction,
            ..self
        }
    }

    pub fn with_color_space(self, color_space: ColorSpace) -> Self {
        Self {
            color_space,
            ..self
        }
    }
}

impl SkeletonController {
    /// Creates a new skeleton and animation state instance with the given data.
    pub fn new(
        skeleton_data: Arc<SkeletonData>,
        animation_state_data: Arc<AnimationStateData>,
    ) -> Self {
        let mut skeleton = Skeleton::new(skeleton_data);
        skeleton.set_to_setup_pose();
        skeleton.update_world_transform();
        Self {
            skeleton,
            animation_state: AnimationState::new(animation_state_data),
            clipper: SkeletonClipping::new(),
            settings: SkeletonControllerSettings::default(),
        }
    }

    pub fn with_settings(self, settings: SkeletonControllerSettings) -> Self {
        Self { settings, ..self }
    }

    /// Updates the animation state, applies to the skeleton, and updates world transforms.
    pub fn update(&mut self, delta_seconds: f32) {
        self.animation_state.update(delta_seconds);
        self.animation_state.apply(&mut self.skeleton);
        self.skeleton.update_world_transform();
    }

    /// Render the skeleton using the [`SimpleDrawer`] and returns renderable mesh information.
    ///
    /// In most cases, it is preferable to use [`SkeletonController::combined_renderables`] which
    /// is significantly faster for complex rigs.
    pub fn renderables(&mut self) -> Vec<SkeletonRenderable> {
        let renderables = SimpleDrawer {
            cull_direction: self.settings.cull_direction,
            premultiplied_alpha: self.settings.premultiplied_alpha,
            color_space: self.settings.color_space,
        }
        .draw(&mut self.skeleton, Some(&mut self.clipper));
        renderables
            .into_iter()
            .map(|mut renderable| SkeletonRenderable {
                slot_index: renderable.slot_index,
                vertices: take(&mut renderable.vertices),
                uvs: take(&mut renderable.uvs),
                indices: take(&mut renderable.indices),
                color: renderable.color,
                dark_color: renderable.dark_color,
                blend_mode: renderable.blend_mode,
                premultiplied_alpha: self.settings.premultiplied_alpha,
                attachment_renderer_object: renderable.attachment_renderer_object,
            })
            .collect()
    }

    /// Render the skeleton using the [`CombinedDrawer`] and returns renderable mesh information.
    pub fn combined_renderables(&mut self) -> Vec<SkeletonCombinedRenderable> {
        let renderables = CombinedDrawer {
            cull_direction: self.settings.cull_direction,
            premultiplied_alpha: self.settings.premultiplied_alpha,
            color_space: self.settings.color_space,
        }
        .draw(&mut self.skeleton, Some(&mut self.clipper));
        renderables
            .into_iter()
            .map(|mut renderable| SkeletonCombinedRenderable {
                vertices: take(&mut renderable.vertices),
                uvs: take(&mut renderable.uvs),
                indices: take(&mut renderable.indices),
                colors: renderable.colors,
                dark_colors: renderable.dark_colors,
                blend_mode: renderable.blend_mode,
                premultiplied_alpha: self.settings.premultiplied_alpha,
                attachment_renderer_object: renderable.attachment_renderer_object,
            })
            .collect()
    }
}

#[derive(Debug, Clone)]
pub struct SkeletonRenderable {
    pub slot_index: i32,
    pub vertices: Vec<[f32; 2]>,
    pub uvs: Vec<[f32; 2]>,
    pub indices: Vec<u16>,
    pub color: Color,
    pub dark_color: Color,
    pub blend_mode: BlendMode,
    pub premultiplied_alpha: bool,
    pub attachment_renderer_object: Option<*const c_void>,
}

#[derive(Debug, Clone)]
pub struct SkeletonCombinedRenderable {
    pub vertices: Vec<[f32; 2]>,
    pub uvs: Vec<[f32; 2]>,
    pub indices: Vec<u16>,
    pub colors: Vec<[f32; 4]>,
    pub dark_colors: Vec<[f32; 4]>,
    pub blend_mode: BlendMode,
    pub premultiplied_alpha: bool,
    pub attachment_renderer_object: Option<*const c_void>,
}