dora_ssr/dora/camera_2d.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 camera2d_type() -> i32;
11 fn camera2d_set_rotation(slf: i64, val: f32);
12 fn camera2d_get_rotation(slf: i64) -> f32;
13 fn camera2d_set_zoom(slf: i64, val: f32);
14 fn camera2d_get_zoom(slf: i64) -> f32;
15 fn camera2d_set_position(slf: i64, val: i64);
16 fn camera2d_get_position(slf: i64) -> i64;
17 fn camera2d_new(name: i64) -> i64;
18}
19use crate::dora::IObject;
20use crate::dora::ICamera;
21impl ICamera for Camera2D { }
22/// A struct for 2D camera object in the game engine.
23pub struct Camera2D { raw: i64 }
24crate::dora_object!(Camera2D);
25impl Camera2D {
26 pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
27 (unsafe { camera2d_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
28 match raw {
29 0 => None,
30 _ => Some(Box::new(Camera2D { raw: raw }))
31 }
32 })
33 }
34 /// Sets the rotation angle of the camera in degrees.
35 pub fn set_rotation(&mut self, val: f32) {
36 unsafe { camera2d_set_rotation(self.raw(), val) };
37 }
38 /// Gets the rotation angle of the camera in degrees.
39 pub fn get_rotation(&self) -> f32 {
40 return unsafe { camera2d_get_rotation(self.raw()) };
41 }
42 /// Sets the factor by which to zoom the camera. If set to 1.0, the view is normal sized. If set to 2.0, items will appear double in size.
43 pub fn set_zoom(&mut self, val: f32) {
44 unsafe { camera2d_set_zoom(self.raw(), val) };
45 }
46 /// Gets the factor by which to zoom the camera. If set to 1.0, the view is normal sized. If set to 2.0, items will appear double in size.
47 pub fn get_zoom(&self) -> f32 {
48 return unsafe { camera2d_get_zoom(self.raw()) };
49 }
50 /// Sets the position of the camera in the game world.
51 pub fn set_position(&mut self, val: &crate::dora::Vec2) {
52 unsafe { camera2d_set_position(self.raw(), val.into_i64()) };
53 }
54 /// Gets the position of the camera in the game world.
55 pub fn get_position(&self) -> crate::dora::Vec2 {
56 return unsafe { crate::dora::Vec2::from(camera2d_get_position(self.raw())) };
57 }
58 /// Creates a new Camera2D object with the given name.
59 ///
60 /// # Arguments
61 ///
62 /// * `name` - The name of the Camera2D object.
63 ///
64 /// # Returns
65 ///
66 /// * `Camera2D` - A new instance of the Camera2D object.
67 pub fn new(name: &str) -> Camera2D {
68 unsafe { return Camera2D { raw: camera2d_new(crate::dora::from_string(name)) }; }
69 }
70}