dora_ssr/dora/
playable.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 playable_type() -> i32;
11	fn playable_set_look(slf: i64, val: i64);
12	fn playable_get_look(slf: i64) -> i64;
13	fn playable_set_speed(slf: i64, val: f32);
14	fn playable_get_speed(slf: i64) -> f32;
15	fn playable_set_recovery(slf: i64, val: f32);
16	fn playable_get_recovery(slf: i64) -> f32;
17	fn playable_set_fliped(slf: i64, val: i32);
18	fn playable_is_fliped(slf: i64) -> i32;
19	fn playable_get_current(slf: i64) -> i64;
20	fn playable_get_last_completed(slf: i64) -> i64;
21	fn playable_get_key(slf: i64, name: i64) -> i64;
22	fn playable_play(slf: i64, name: i64, looping: i32) -> f32;
23	fn playable_stop(slf: i64);
24	fn playable_set_slot(slf: i64, name: i64, item: i64);
25	fn playable_get_slot(slf: i64, name: i64) -> i64;
26	fn playable_new(filename: i64) -> i64;
27}
28use crate::dora::IObject;
29use crate::dora::INode;
30impl INode for Playable { }
31/// An interface for an animation model system.
32pub struct Playable { raw: i64 }
33crate::dora_object!(Playable);
34impl IPlayable for Playable { }
35pub trait IPlayable: INode {
36	/// Sets the look of the animation.
37	fn set_look(&mut self, val: &str) {
38		unsafe { playable_set_look(self.raw(), crate::dora::from_string(val)) };
39	}
40	/// Gets the look of the animation.
41	fn get_look(&self) -> String {
42		return unsafe { crate::dora::to_string(playable_get_look(self.raw())) };
43	}
44	/// Sets the play speed of the animation.
45	fn set_speed(&mut self, val: f32) {
46		unsafe { playable_set_speed(self.raw(), val) };
47	}
48	/// Gets the play speed of the animation.
49	fn get_speed(&self) -> f32 {
50		return unsafe { playable_get_speed(self.raw()) };
51	}
52	/// Sets the recovery time of the animation, in seconds.
53	/// Used for doing transitions from one animation to another animation.
54	fn set_recovery(&mut self, val: f32) {
55		unsafe { playable_set_recovery(self.raw(), val) };
56	}
57	/// Gets the recovery time of the animation, in seconds.
58	/// Used for doing transitions from one animation to another animation.
59	fn get_recovery(&self) -> f32 {
60		return unsafe { playable_get_recovery(self.raw()) };
61	}
62	/// Sets whether the animation is flipped horizontally.
63	fn set_fliped(&mut self, val: bool) {
64		unsafe { playable_set_fliped(self.raw(), if val { 1 } else { 0 }) };
65	}
66	/// Gets whether the animation is flipped horizontally.
67	fn is_fliped(&self) -> bool {
68		return unsafe { playable_is_fliped(self.raw()) != 0 };
69	}
70	/// Gets the current playing animation name.
71	fn get_current(&self) -> String {
72		return unsafe { crate::dora::to_string(playable_get_current(self.raw())) };
73	}
74	/// Gets the last completed animation name.
75	fn get_last_completed(&self) -> String {
76		return unsafe { crate::dora::to_string(playable_get_last_completed(self.raw())) };
77	}
78	/// Gets a key point on the animation model by its name.
79	///
80	/// # Arguments
81	///
82	/// * `name` - The name of the key point to get.
83	///
84	/// # Returns
85	///
86	/// * A `Vec2` representing the key point value.
87	fn get_key(&mut self, name: &str) -> crate::dora::Vec2 {
88		unsafe { return crate::dora::Vec2::from(playable_get_key(self.raw(), crate::dora::from_string(name))); }
89	}
90	/// Plays an animation from the model.
91	///
92	/// # Arguments
93	///
94	/// * `name` - The name of the animation to play.
95	/// * `loop` - Whether to loop the animation or not.
96	///
97	/// # Returns
98	///
99	/// * The duration of the animation in seconds.
100	fn play(&mut self, name: &str, looping: bool) -> f32 {
101		unsafe { return playable_play(self.raw(), crate::dora::from_string(name), if looping { 1 } else { 0 }); }
102	}
103	/// Stops the currently playing animation.
104	fn stop(&mut self) {
105		unsafe { playable_stop(self.raw()); }
106	}
107	/// Attaches a child node to a slot on the animation model.
108	///
109	/// # Arguments
110	///
111	/// * `name` - The name of the slot to set.
112	/// * `item` - The node to set the slot to.
113	fn set_slot(&mut self, name: &str, item: &dyn crate::dora::INode) {
114		unsafe { playable_set_slot(self.raw(), crate::dora::from_string(name), item.raw()); }
115	}
116	/// Gets the child node attached to the animation model.
117	///
118	/// # Arguments
119	///
120	/// * `name` - The name of the slot to get.
121	///
122	/// # Returns
123	///
124	/// * The node in the slot, or `None` if there is no node in the slot.
125	fn get_slot(&mut self, name: &str) -> Option<crate::dora::Node> {
126		unsafe { return crate::dora::Node::from(playable_get_slot(self.raw(), crate::dora::from_string(name))); }
127	}
128}
129impl Playable {
130	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
131		(unsafe { playable_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
132			match raw {
133				0 => None,
134				_ => Some(Box::new(Playable { raw: raw }))
135			}
136		})
137	}
138	/// Creates a new instance of 'Playable' from the specified animation file.
139	///
140	/// # Arguments
141	///
142	/// * `filename` - The filename of the animation file to load. Supports DragonBone, Spine2D and Dora Model files.
143	/// Should be one of the formats below:
144	///     * "model:" + modelFile
145	///     * "spine:" + spineStr
146	///     * "bone:" + dragonBoneStr
147	///
148	/// # Returns
149	///
150	/// * A new instance of 'Playable'. If the file could not be loaded, then `None` is returned.
151	pub fn new(filename: &str) -> Option<Playable> {
152		unsafe { return Playable::from(playable_new(crate::dora::from_string(filename))); }
153	}
154}