dora_ssr/dora/
director.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 director_set_clear_color(val: i32);
11	fn director_get_clear_color() -> i32;
12	fn director_get_ui() -> i64;
13	fn director_get_ui_3d() -> i64;
14	fn director_get_entry() -> i64;
15	fn director_get_post_node() -> i64;
16	fn director_get_current_camera() -> i64;
17	fn director_set_frustum_culling(val: i32);
18	fn director_is_frustum_culling() -> i32;
19	fn director_schedule(func0: i32, stack0: i64);
20	fn director_schedule_posted(func0: i32, stack0: i64);
21	fn director_push_camera(camera: i64);
22	fn director_pop_camera();
23	fn director_remove_camera(camera: i64) -> i32;
24	fn director_clear_camera();
25	fn director_cleanup();
26}
27/// A struct manages the game scene trees and provides access to root scene nodes for different game uses.
28pub struct Director { }
29impl Director {
30	/// Sets the background color for the game world.
31	pub fn set_clear_color(val: &crate::dora::Color) {
32		unsafe { director_set_clear_color(val.to_argb() as i32) };
33	}
34	/// Gets the background color for the game world.
35	pub fn get_clear_color() -> crate::dora::Color {
36		return unsafe { crate::dora::Color::from(director_get_clear_color()) };
37	}
38	/// Gets the root node for 2D user interface elements like buttons and labels.
39	pub fn get_ui() -> crate::dora::Node {
40		return unsafe { crate::dora::Node::from(director_get_ui()).unwrap() };
41	}
42	/// Gets the root node for 3D user interface elements with 3D projection effect.
43	pub fn get_ui_3d() -> crate::dora::Node {
44		return unsafe { crate::dora::Node::from(director_get_ui_3d()).unwrap() };
45	}
46	/// Gets the root node for the starting point of a game.
47	pub fn get_entry() -> crate::dora::Node {
48		return unsafe { crate::dora::Node::from(director_get_entry()).unwrap() };
49	}
50	/// Gets the root node for post-rendering scene tree.
51	pub fn get_post_node() -> crate::dora::Node {
52		return unsafe { crate::dora::Node::from(director_get_post_node()).unwrap() };
53	}
54	/// Gets the current active camera in Director's camera stack.
55	pub fn get_current_camera() -> crate::dora::Camera {
56		return unsafe { crate::dora::Camera::from(director_get_current_camera()).unwrap() };
57	}
58	/// Sets whether or not to enable frustum culling.
59	pub fn set_frustum_culling(val: bool) {
60		unsafe { director_set_frustum_culling(if val { 1 } else { 0 }) };
61	}
62	/// Gets whether or not to enable frustum culling.
63	pub fn is_frustum_culling() -> bool {
64		return unsafe { director_is_frustum_culling() != 0 };
65	}
66	/// Schedule a function to be called every frame.
67	///
68	/// # Arguments
69	///
70	/// * `updateFunc` - The function to call every frame.
71	pub fn schedule(mut update_func: Box<dyn FnMut(f64) -> bool>) {
72		let mut stack0 = crate::dora::CallStack::new();
73		let stack_raw0 = stack0.raw();
74		let func_id0 = crate::dora::push_function(Box::new(move || {
75			let result = update_func(stack0.pop_f64().unwrap());
76			stack0.push_bool(result);
77		}));
78		unsafe { director_schedule(func_id0, stack_raw0); }
79	}
80	/// Schedule a function to be called every frame for processing post game logic.
81	///
82	/// # Arguments
83	///
84	/// * `func` - The function to call every frame.
85	pub fn schedule_posted(mut update_func: Box<dyn FnMut(f64) -> bool>) {
86		let mut stack0 = crate::dora::CallStack::new();
87		let stack_raw0 = stack0.raw();
88		let func_id0 = crate::dora::push_function(Box::new(move || {
89			let result = update_func(stack0.pop_f64().unwrap());
90			stack0.push_bool(result);
91		}));
92		unsafe { director_schedule_posted(func_id0, stack_raw0); }
93	}
94	/// Adds a new camera to Director's camera stack and sets it to the current camera.
95	///
96	/// # Arguments
97	///
98	/// * `camera` - The camera to add.
99	pub fn push_camera(camera: &dyn crate::dora::ICamera) {
100		unsafe { director_push_camera(camera.raw()); }
101	}
102	/// Removes the current camera from Director's camera stack.
103	pub fn pop_camera() {
104		unsafe { director_pop_camera(); }
105	}
106	/// Removes a specified camera from Director's camera stack.
107	///
108	/// # Arguments
109	///
110	/// * `camera` - The camera to remove.
111	///
112	/// # Returns
113	///
114	/// * `bool` - `true` if the camera was removed, `false` otherwise.
115	pub fn remove_camera(camera: &dyn crate::dora::ICamera) -> bool {
116		unsafe { return director_remove_camera(camera.raw()) != 0; }
117	}
118	/// Removes all cameras from Director's camera stack.
119	pub fn clear_camera() {
120		unsafe { director_clear_camera(); }
121	}
122	/// Cleans up all resources managed by the Director, including scene trees and cameras.
123	pub fn cleanup() {
124		unsafe { director_cleanup(); }
125	}
126}