dora_ssr/dora/mouse.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 mouse_get_position() -> i64;
11 fn mouse_is_left_button_pressed() -> i32;
12 fn mouse_is_right_button_pressed() -> i32;
13 fn mouse_is_middle_button_pressed() -> i32;
14 fn mouse_get_wheel() -> i64;
15}
16/// An interface for handling mouse inputs.
17pub struct Mouse { }
18impl Mouse {
19 /// The position of the mouse in the visible window.
20 /// You can use `Mouse::get_position() * App::get_device_pixel_ratio()` to get the coordinate in the game world.
21 /// Then use `node.convertToNodeSpace()` to convert the world coordinate to the local coordinate of the node.
22 ///
23 /// # Example
24 ///
25 /// ```
26 /// let worldPos = Mouse::get_position() * App::get_device_pixel_ratio();
27 /// let nodePos = node.convert_to_node_space(&worldPos);
28 /// ```
29 pub fn get_position() -> crate::dora::Vec2 {
30 unsafe { return crate::dora::Vec2::from(mouse_get_position()); }
31 }
32 /// Whether the left mouse button is currently being pressed.
33 pub fn is_left_button_pressed() -> bool {
34 unsafe { return mouse_is_left_button_pressed() != 0; }
35 }
36 /// Whether the right mouse button is currently being pressed.
37 pub fn is_right_button_pressed() -> bool {
38 unsafe { return mouse_is_right_button_pressed() != 0; }
39 }
40 /// Whether the middle mouse button is currently being pressed.
41 pub fn is_middle_button_pressed() -> bool {
42 unsafe { return mouse_is_middle_button_pressed() != 0; }
43 }
44 /// Gets the mouse wheel value.
45 pub fn get_wheel() -> crate::dora::Vec2 {
46 unsafe { return crate::dora::Vec2::from(mouse_get_wheel()); }
47 }
48}