dora_ssr/dora/controller.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 controller__is_button_down(controller_id: i32, name: i64) -> i32;
11 fn controller__is_button_up(controller_id: i32, name: i64) -> i32;
12 fn controller__is_button_pressed(controller_id: i32, name: i64) -> i32;
13 fn controller__get_axis(controller_id: i32, name: i64) -> f32;
14}
15/// An interface for handling game controller inputs.
16pub struct Controller { }
17impl Controller {
18 /// Checks whether a button on the controller is currently pressed.
19 ///
20 /// # Arguments
21 ///
22 /// * `controller_id` - The ID of the controller to check. Starts from 0.
23 /// * `name` - The name of the button to check.
24 ///
25 /// # Returns
26 ///
27 /// * `bool` - `true` if the button is pressed, `false` otherwise.
28 pub(crate) fn _is_button_down(controller_id: i32, name: &str) -> bool {
29 unsafe { return controller__is_button_down(controller_id, crate::dora::from_string(name)) != 0; }
30 }
31 /// Checks whether a button on the controller is currently released.
32 ///
33 /// # Arguments
34 ///
35 /// * `controller_id` - The ID of the controller to check. Starts from 0.
36 /// * `name` - The name of the button to check.
37 ///
38 /// # Returns
39 ///
40 /// * `bool` - `true` if the button is released, `false` otherwise.
41 pub(crate) fn _is_button_up(controller_id: i32, name: &str) -> bool {
42 unsafe { return controller__is_button_up(controller_id, crate::dora::from_string(name)) != 0; }
43 }
44 /// Checks whether a button on the controller is currently being pressed.
45 ///
46 /// # Arguments
47 ///
48 /// * `controller_id` - The ID of the controller to check. Starts from 0.
49 /// * `name` - The name of the button to check.
50 ///
51 /// # Returns
52 ///
53 /// * `bool` - `true` if the button is being pressed, `false` otherwise.
54 pub(crate) fn _is_button_pressed(controller_id: i32, name: &str) -> bool {
55 unsafe { return controller__is_button_pressed(controller_id, crate::dora::from_string(name)) != 0; }
56 }
57 /// Gets the value of an axis on the controller.
58 ///
59 /// # Arguments
60 ///
61 /// * `controller_id` - The ID of the controller to check. Starts from 0.
62 /// * `name` - The name of the axis to check.
63 ///
64 /// # Returns
65 ///
66 /// * `f32` - The value of the axis. The value is between -1.0 and 1.0.
67 pub(crate) fn _get_axis(controller_id: i32, name: &str) -> f32 {
68 unsafe { return controller__get_axis(controller_id, crate::dora::from_string(name)); }
69 }
70}