dora_ssr/dora/
action.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 action_type() -> i32;
11	fn action_get_duration(slf: i64) -> f32;
12	fn action_is_running(slf: i64) -> i32;
13	fn action_is_paused(slf: i64) -> i32;
14	fn action_set_reversed(slf: i64, val: i32);
15	fn action_is_reversed(slf: i64) -> i32;
16	fn action_set_speed(slf: i64, val: f32);
17	fn action_get_speed(slf: i64) -> f32;
18	fn action_pause(slf: i64);
19	fn action_resume(slf: i64);
20	fn action_update_to(slf: i64, elapsed: f32, reversed: i32);
21	fn action_new(def: i64) -> i64;
22}
23use crate::dora::IObject;
24/// Represents an action that can be run on a node.
25pub struct Action { raw: i64 }
26crate::dora_object!(Action);
27impl Action {
28	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
29		(unsafe { action_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
30			match raw {
31				0 => None,
32				_ => Some(Box::new(Action { raw: raw }))
33			}
34		})
35	}
36	/// Gets the duration of the action.
37	pub fn get_duration(&self) -> f32 {
38		return unsafe { action_get_duration(self.raw()) };
39	}
40	/// Gets whether the action is currently running.
41	pub fn is_running(&self) -> bool {
42		return unsafe { action_is_running(self.raw()) != 0 };
43	}
44	/// Gets whether the action is currently paused.
45	pub fn is_paused(&self) -> bool {
46		return unsafe { action_is_paused(self.raw()) != 0 };
47	}
48	/// Sets whether the action should be run in reverse.
49	pub fn set_reversed(&mut self, val: bool) {
50		unsafe { action_set_reversed(self.raw(), if val { 1 } else { 0 }) };
51	}
52	/// Gets whether the action should be run in reverse.
53	pub fn is_reversed(&self) -> bool {
54		return unsafe { action_is_reversed(self.raw()) != 0 };
55	}
56	/// Sets the speed at which the action should be run.
57	/// Set to 1.0 to get normal speed, Set to 2.0 to get two times faster.
58	pub fn set_speed(&mut self, val: f32) {
59		unsafe { action_set_speed(self.raw(), val) };
60	}
61	/// Gets the speed at which the action should be run.
62	/// Set to 1.0 to get normal speed, Set to 2.0 to get two times faster.
63	pub fn get_speed(&self) -> f32 {
64		return unsafe { action_get_speed(self.raw()) };
65	}
66	/// Pauses the action.
67	pub fn pause(&mut self) {
68		unsafe { action_pause(self.raw()); }
69	}
70	/// Resumes the action.
71	pub fn resume(&mut self) {
72		unsafe { action_resume(self.raw()); }
73	}
74	/// Updates the state of the Action.
75	///
76	/// # Arguments
77	///
78	/// * `elapsed` - The amount of time in seconds that has elapsed to update action to.
79	/// * `reversed` - Whether or not to update the Action in reverse.
80	pub fn update_to(&mut self, elapsed: f32, reversed: bool) {
81		unsafe { action_update_to(self.raw(), elapsed, if reversed { 1 } else { 0 }); }
82	}
83	/// Creates a new Action object.
84	///
85	/// # Arguments
86	///
87	/// * `def` - The definition of the action.
88	///
89	/// # Returns
90	///
91	/// * `Action` - A new Action object.
92	pub fn new(def: crate::dora::ActionDef) -> Action {
93		unsafe { return Action { raw: action_new(def.raw()) }; }
94	}
95}