dora_ssr/dora/platformer/
platform_camera.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 platformer_platformcamera_type() -> i32;
11	fn platformer_platformcamera_set_position(slf: i64, val: i64);
12	fn platformer_platformcamera_get_position(slf: i64) -> i64;
13	fn platformer_platformcamera_set_rotation(slf: i64, val: f32);
14	fn platformer_platformcamera_get_rotation(slf: i64) -> f32;
15	fn platformer_platformcamera_set_zoom(slf: i64, val: f32);
16	fn platformer_platformcamera_get_zoom(slf: i64) -> f32;
17	fn platformer_platformcamera_set_boundary(slf: i64, val: i64);
18	fn platformer_platformcamera_get_boundary(slf: i64) -> i64;
19	fn platformer_platformcamera_set_follow_ratio(slf: i64, val: i64);
20	fn platformer_platformcamera_get_follow_ratio(slf: i64) -> i64;
21	fn platformer_platformcamera_set_follow_offset(slf: i64, val: i64);
22	fn platformer_platformcamera_get_follow_offset(slf: i64) -> i64;
23	fn platformer_platformcamera_set_follow_target(slf: i64, val: i64);
24	fn platformer_platformcamera_get_follow_target(slf: i64) -> i64;
25	fn platformer_platformcamera_set_follow_target_null(slf: i64);
26	fn platformer_platformcamera_new(name: i64) -> i64;
27}
28use crate::dora::IObject;
29use crate::dora::ICamera;
30impl ICamera for PlatformCamera { }
31/// A platform camera for 2D platformer games that can track a game unit's movement and keep it within the camera's view.
32pub struct PlatformCamera { raw: i64 }
33crate::dora_object!(PlatformCamera);
34impl PlatformCamera {
35	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
36		(unsafe { platformer_platformcamera_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
37			match raw {
38				0 => None,
39				_ => Some(Box::new(PlatformCamera { raw: raw }))
40			}
41		})
42	}
43	/// Sets The camera's position.
44	pub fn set_position(&mut self, val: &crate::dora::Vec2) {
45		unsafe { platformer_platformcamera_set_position(self.raw(), val.into_i64()) };
46	}
47	/// Gets The camera's position.
48	pub fn get_position(&self) -> crate::dora::Vec2 {
49		return unsafe { crate::dora::Vec2::from(platformer_platformcamera_get_position(self.raw())) };
50	}
51	/// Sets The camera's rotation in degrees.
52	pub fn set_rotation(&mut self, val: f32) {
53		unsafe { platformer_platformcamera_set_rotation(self.raw(), val) };
54	}
55	/// Gets The camera's rotation in degrees.
56	pub fn get_rotation(&self) -> f32 {
57		return unsafe { platformer_platformcamera_get_rotation(self.raw()) };
58	}
59	/// Sets The camera's zoom factor, 1.0 means the normal size, 2.0 mean zoom to doubled size.
60	pub fn set_zoom(&mut self, val: f32) {
61		unsafe { platformer_platformcamera_set_zoom(self.raw(), val) };
62	}
63	/// Gets The camera's zoom factor, 1.0 means the normal size, 2.0 mean zoom to doubled size.
64	pub fn get_zoom(&self) -> f32 {
65		return unsafe { platformer_platformcamera_get_zoom(self.raw()) };
66	}
67	/// Sets The rectangular area within which the camera is allowed to view.
68	pub fn set_boundary(&mut self, val: &crate::dora::Rect) {
69		unsafe { platformer_platformcamera_set_boundary(self.raw(), val.raw()) };
70	}
71	/// Gets The rectangular area within which the camera is allowed to view.
72	pub fn get_boundary(&self) -> crate::dora::Rect {
73		return unsafe { crate::dora::Rect::from(platformer_platformcamera_get_boundary(self.raw())) };
74	}
75	/// Sets the ratio at which the camera should move to keep up with the target's position.
76	/// For example, set to `Vec2(1.0, 1.0)`, then the camera will keep up to the target's position right away.
77	/// Set to Vec2(0.5, 0.5) or smaller value, then the camera will move halfway to the target's position each frame, resulting in a smooth and gradual movement.
78	pub fn set_follow_ratio(&mut self, val: &crate::dora::Vec2) {
79		unsafe { platformer_platformcamera_set_follow_ratio(self.raw(), val.into_i64()) };
80	}
81	/// Gets the ratio at which the camera should move to keep up with the target's position.
82	/// For example, set to `Vec2(1.0, 1.0)`, then the camera will keep up to the target's position right away.
83	/// Set to Vec2(0.5, 0.5) or smaller value, then the camera will move halfway to the target's position each frame, resulting in a smooth and gradual movement.
84	pub fn get_follow_ratio(&self) -> crate::dora::Vec2 {
85		return unsafe { crate::dora::Vec2::from(platformer_platformcamera_get_follow_ratio(self.raw())) };
86	}
87	/// Sets the offset at which the camera should follow the target.
88	pub fn set_follow_offset(&mut self, val: &crate::dora::Vec2) {
89		unsafe { platformer_platformcamera_set_follow_offset(self.raw(), val.into_i64()) };
90	}
91	/// Gets the offset at which the camera should follow the target.
92	pub fn get_follow_offset(&self) -> crate::dora::Vec2 {
93		return unsafe { crate::dora::Vec2::from(platformer_platformcamera_get_follow_offset(self.raw())) };
94	}
95	/// Sets the game unit that the camera should track.
96	pub fn set_follow_target(&mut self, val: &dyn crate::dora::INode) {
97		unsafe { platformer_platformcamera_set_follow_target(self.raw(), val.raw()) };
98	}
99	/// Gets the game unit that the camera should track.
100	pub fn get_follow_target(&self) -> Option<crate::dora::Node> {
101		return unsafe { crate::dora::Node::from(platformer_platformcamera_get_follow_target(self.raw())) };
102	}
103	/// Removes the target that the camera is following.
104	pub fn set_follow_target_null(&mut self) {
105		unsafe { platformer_platformcamera_set_follow_target_null(self.raw()); }
106	}
107	/// Creates a new instance of `PlatformCamera`.
108	///
109	/// # Arguments
110	///
111	/// * `name` - An optional string that specifies the name of the new instance. Default is an empty string.
112	///
113	/// # Returns
114	///
115	/// * A new `PlatformCamera` instance.
116	pub fn new(name: &str) -> PlatformCamera {
117		unsafe { return PlatformCamera { raw: platformer_platformcamera_new(crate::dora::from_string(name)) }; }
118	}
119}