dora_ssr/dora/
spine.rs

1/* Copyright (c) 2016-2025 Li Jin <dragon-fly@qq.com>
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8
9extern "C" {
10	fn spine_type() -> i32;
11	fn spine_set_hit_test_enabled(slf: i64, val: i32);
12	fn spine_is_hit_test_enabled(slf: i64) -> i32;
13	fn spine_set_bone_rotation(slf: i64, name: i64, rotation: f32) -> i32;
14	fn spine_contains_point(slf: i64, x: f32, y: f32) -> i64;
15	fn spine_intersects_segment(slf: i64, x_1: f32, y_1: f32, x_2: f32, y_2: f32) -> i64;
16	fn spine_with_files(skel_file: i64, atlas_file: i64) -> i64;
17	fn spine_new(spine_str: i64) -> i64;
18	fn spine_get_looks(spine_str: i64) -> i64;
19	fn spine_get_animations(spine_str: i64) -> i64;
20}
21use crate::dora::IObject;
22use crate::dora::IPlayable;
23impl IPlayable for Spine { }
24use crate::dora::INode;
25impl INode for Spine { }
26/// An implementation of an animation system using the Spine engine.
27pub struct Spine { raw: i64 }
28crate::dora_object!(Spine);
29impl Spine {
30	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
31		(unsafe { spine_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
32			match raw {
33				0 => None,
34				_ => Some(Box::new(Spine { raw: raw }))
35			}
36		})
37	}
38	/// Sets whether hit testing is enabled.
39	pub fn set_hit_test_enabled(&mut self, val: bool) {
40		unsafe { spine_set_hit_test_enabled(self.raw(), if val { 1 } else { 0 }) };
41	}
42	/// Gets whether hit testing is enabled.
43	pub fn is_hit_test_enabled(&self) -> bool {
44		return unsafe { spine_is_hit_test_enabled(self.raw()) != 0 };
45	}
46	/// Sets the rotation of a bone in the Spine skeleton.
47	///
48	/// # Arguments
49	///
50	/// * `name` - The name of the bone to rotate.
51	/// * `rotation` - The amount to rotate the bone, in degrees.
52	///
53	/// # Returns
54	///
55	/// * `bool` - Whether the rotation was successfully set or not.
56	pub fn set_bone_rotation(&mut self, name: &str, rotation: f32) -> bool {
57		unsafe { return spine_set_bone_rotation(self.raw(), crate::dora::from_string(name), rotation) != 0; }
58	}
59	/// Checks if a point in space is inside the boundaries of the Spine skeleton.
60	///
61	/// # Arguments
62	///
63	/// * `x` - The x-coordinate of the point to check.
64	/// * `y` - The y-coordinate of the point to check.
65	///
66	/// # Returns
67	///
68	/// * `Option<String>` - The name of the bone at the point, or `None` if there is no bone at the point.
69	pub fn contains_point(&mut self, x: f32, y: f32) -> String {
70		unsafe { return crate::dora::to_string(spine_contains_point(self.raw(), x, y)); }
71	}
72	/// Checks if a line segment intersects the boundaries of the instance and returns the name of the bone or slot at the intersection point, or `None` if no bone or slot is found.
73	///
74	/// # Arguments
75	///
76	/// * `x1` - The x-coordinate of the start point of the line segment.
77	/// * `y1` - The y-coordinate of the start point of the line segment.
78	/// * `x2` - The x-coordinate of the end point of the line segment.
79	/// * `y2` - The y-coordinate of the end point of the line segment.
80	///
81	/// # Returns
82	///
83	/// * `Option<String>` - The name of the bone or slot at the intersection point, or `None` if no bone or slot is found.
84	pub fn intersects_segment(&mut self, x_1: f32, y_1: f32, x_2: f32, y_2: f32) -> String {
85		unsafe { return crate::dora::to_string(spine_intersects_segment(self.raw(), x_1, y_1, x_2, y_2)); }
86	}
87	/// Creates a new instance of 'Spine' using the specified skeleton file and atlas file.
88	///
89	/// # Arguments
90	///
91	/// * `skel_file` - The filename of the skeleton file to load.
92	/// * `atlas_file` - The filename of the atlas file to load.
93	///
94	/// # Returns
95	///
96	/// * A new instance of 'Spine' with the specified skeleton file and atlas file. Returns `None` if the skeleton file or atlas file could not be loaded.
97	pub fn with_files(skel_file: &str, atlas_file: &str) -> Option<Spine> {
98		unsafe { return Spine::from(spine_with_files(crate::dora::from_string(skel_file), crate::dora::from_string(atlas_file))); }
99	}
100	/// Creates a new instance of 'Spine' using the specified Spine string.
101	///
102	/// # Arguments
103	///
104	/// * `spine_str` - The Spine file string for the new instance. A Spine file string can be a file path with the target file extension like "Spine/item" or file paths with all the related files like "Spine/item.skel|Spine/item.atlas" or "Spine/item.json|Spine/item.atlas".
105	///
106	/// # Returns
107	///
108	/// * A new instance of 'Spine'. Returns `None` if the Spine file could not be loaded.
109	pub fn new(spine_str: &str) -> Option<Spine> {
110		unsafe { return Spine::from(spine_new(crate::dora::from_string(spine_str))); }
111	}
112	/// Returns a list of available looks for the specified Spine2D file string.
113	///
114	/// # Arguments
115	///
116	/// * `spine_str` - The Spine2D file string to get the looks for.
117	///
118	/// # Returns
119	///
120	/// * A `Vec<String>` representing the available looks.
121	pub fn get_looks(spine_str: &str) -> Vec<String> {
122		unsafe { return crate::dora::Vector::to_str(spine_get_looks(crate::dora::from_string(spine_str))); }
123	}
124	/// Returns a list of available animations for the specified Spine2D file string.
125	///
126	/// # Arguments
127	///
128	/// * `spine_str` - The Spine2D file string to get the animations for.
129	///
130	/// # Returns
131	///
132	/// * A `Vec<String>` representing the available animations.
133	pub fn get_animations(spine_str: &str) -> Vec<String> {
134		unsafe { return crate::dora::Vector::to_str(spine_get_animations(crate::dora::from_string(spine_str))); }
135	}
136}