2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* input.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2022-01-13 09:45:09 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission 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: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "input.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
|
|
|
|
#include "core/input_map.h"
|
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "core/project_settings.h"
|
|
|
|
|
2018-12-18 02:53:54 +01:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
#include "editor/editor_settings.h"
|
|
|
|
#endif
|
|
|
|
|
2021-05-04 16:00:45 +02:00
|
|
|
Input *Input::singleton = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Input *Input::get_singleton() {
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Input::set_mouse_mode(MouseMode p_mode) {
|
2018-10-04 09:17:59 +02:00
|
|
|
ERR_FAIL_INDEX((int)p_mode, 4);
|
2014-02-10 02:10:30 +01:00
|
|
|
OS::get_singleton()->set_mouse_mode((OS::MouseMode)p_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
Input::MouseMode Input::get_mouse_mode() const {
|
|
|
|
return (MouseMode)OS::get_singleton()->get_mouse_mode();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Input::_bind_methods() {
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("is_key_pressed", "scancode"), &Input::is_key_pressed);
|
2021-11-23 10:14:19 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("is_physical_key_pressed", "scancode"), &Input::is_physical_key_pressed);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("is_mouse_button_pressed", "button"), &Input::is_mouse_button_pressed);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_joy_button_pressed", "device", "button"), &Input::is_joy_button_pressed);
|
Allow checking for exact matches with Action events.
Added additional param to action related methods to test for exactness.
If "p_exact_match" is true, then the action will only be "matched" if the provided input event *exactly* matches with the action event.
Before:
* Action Event = KEY_S
* Input Event = KEY_CONTROL + KEY_S
* Is Action Pressed = True
Now:
You can still do the above, however you can optionally check that the input is exactly what the action event is:
* Action Event = KEY_S
* Input Event = KEY_CONTROL + KEY_S
* p_exact_match = True
* Is Action Pressed = False
* If the Input Event was only KEY_S, then the result would be true.
Usage:
```gdscript
Input.is_action_pressed(action_name: String, exact_match: bool)
Input.is_action_pressed("my_action", true)
InputMap.event_is_action(p_event, "my_action", true)
func _input(event: InputEvent):
event.is_action_pressed("my_action", false, true) # false = "allow_echo", true = "exact_match"
event.is_action("my_action", true)
```
Co-authored-by: Eric M <itsjusteza@gmail.com>
2020-12-13 15:22:42 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("is_action_pressed", "action", "exact"), &Input::is_action_pressed, DEFVAL(false));
|
|
|
|
ClassDB::bind_method(D_METHOD("is_action_just_pressed", "action", "exact"), &Input::is_action_just_pressed, DEFVAL(false));
|
|
|
|
ClassDB::bind_method(D_METHOD("is_action_just_released", "action", "exact"), &Input::is_action_just_released, DEFVAL(false));
|
|
|
|
ClassDB::bind_method(D_METHOD("get_action_strength", "action", "exact"), &Input::get_action_strength, DEFVAL(false));
|
|
|
|
ClassDB::bind_method(D_METHOD("get_action_raw_strength", "action", "exact"), &Input::get_action_raw_strength, DEFVAL(false));
|
2021-07-23 03:02:18 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_axis", "negative_action", "positive_action"), &Input::get_axis);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_vector", "negative_x", "positive_x", "negative_y", "positive_y", "deadzone"), &Input::get_vector, DEFVAL(-1.0f));
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("add_joy_mapping", "mapping", "update_existing"), &Input::add_joy_mapping, DEFVAL(false));
|
|
|
|
ClassDB::bind_method(D_METHOD("remove_joy_mapping", "guid"), &Input::remove_joy_mapping);
|
2017-10-11 18:45:44 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("joy_connection_changed", "device", "connected", "name", "guid"), &Input::joy_connection_changed);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("is_joy_known", "device"), &Input::is_joy_known);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_joy_axis", "device", "axis"), &Input::get_joy_axis);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_joy_name", "device"), &Input::get_joy_name);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_joy_guid", "device"), &Input::get_joy_guid);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_connected_joypads"), &Input::get_connected_joypads);
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_joy_vibration_strength", "device"), &Input::get_joy_vibration_strength);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_joy_vibration_duration", "device"), &Input::get_joy_vibration_duration);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_joy_button_string", "button_index"), &Input::get_joy_button_string);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_joy_button_index_from_string", "button"), &Input::get_joy_button_index_from_string);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_joy_axis_string", "axis_index"), &Input::get_joy_axis_string);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_joy_axis_index_from_string", "axis"), &Input::get_joy_axis_index_from_string);
|
|
|
|
ClassDB::bind_method(D_METHOD("start_joy_vibration", "device", "weak_magnitude", "strong_magnitude", "duration"), &Input::start_joy_vibration, DEFVAL(0));
|
|
|
|
ClassDB::bind_method(D_METHOD("stop_joy_vibration", "device"), &Input::stop_joy_vibration);
|
2019-08-17 17:27:29 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("vibrate_handheld", "duration_ms"), &Input::vibrate_handheld, DEFVAL(500));
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_gravity"), &Input::get_gravity);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_accelerometer"), &Input::get_accelerometer);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_magnetometer"), &Input::get_magnetometer);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_gyroscope"), &Input::get_gyroscope);
|
2021-10-12 22:09:30 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_gravity", "value"), &Input::set_gravity);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_accelerometer", "value"), &Input::set_accelerometer);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_magnetometer", "value"), &Input::set_magnetometer);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_gyroscope", "value"), &Input::set_gyroscope);
|
2017-03-29 17:29:38 +02:00
|
|
|
//ClassDB::bind_method(D_METHOD("get_mouse_position"),&Input::get_mouse_position); - this is not the function you want
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_last_mouse_speed"), &Input::get_last_mouse_speed);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_mouse_button_mask"), &Input::get_mouse_button_mask);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_mouse_mode", "mode"), &Input::set_mouse_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_mouse_mode"), &Input::get_mouse_mode);
|
2017-09-10 15:37:49 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("warp_mouse_position", "to"), &Input::warp_mouse_position);
|
2018-11-17 21:45:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("action_press", "action", "strength"), &Input::action_press, DEFVAL(1.f));
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("action_release", "action"), &Input::action_release);
|
2018-04-09 22:48:24 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_default_cursor_shape", "shape"), &Input::set_default_cursor_shape, DEFVAL(CURSOR_ARROW));
|
2019-04-15 17:30:20 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_current_cursor_shape"), &Input::get_current_cursor_shape);
|
2017-11-10 11:50:11 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_custom_mouse_cursor", "image", "shape", "hotspot"), &Input::set_custom_mouse_cursor, DEFVAL(CURSOR_ARROW), DEFVAL(Vector2()));
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("parse_input_event", "event"), &Input::parse_input_event);
|
2019-03-03 23:52:18 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_use_accumulated_input", "enable"), &Input::set_use_accumulated_input);
|
2022-06-10 11:33:12 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("is_using_accumulated_input"), &Input::is_using_accumulated_input);
|
2021-10-14 18:58:15 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("flush_buffered_events"), &Input::flush_buffered_events);
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2022-06-10 11:33:12 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_mode"), "set_mouse_mode", "get_mouse_mode");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_accumulated_input"), "set_use_accumulated_input", "is_using_accumulated_input");
|
|
|
|
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(MOUSE_MODE_VISIBLE);
|
|
|
|
BIND_ENUM_CONSTANT(MOUSE_MODE_HIDDEN);
|
|
|
|
BIND_ENUM_CONSTANT(MOUSE_MODE_CAPTURED);
|
|
|
|
BIND_ENUM_CONSTANT(MOUSE_MODE_CONFINED);
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2017-11-10 11:50:11 +01:00
|
|
|
BIND_ENUM_CONSTANT(CURSOR_ARROW);
|
|
|
|
BIND_ENUM_CONSTANT(CURSOR_IBEAM);
|
|
|
|
BIND_ENUM_CONSTANT(CURSOR_POINTING_HAND);
|
|
|
|
BIND_ENUM_CONSTANT(CURSOR_CROSS);
|
|
|
|
BIND_ENUM_CONSTANT(CURSOR_WAIT);
|
|
|
|
BIND_ENUM_CONSTANT(CURSOR_BUSY);
|
|
|
|
BIND_ENUM_CONSTANT(CURSOR_DRAG);
|
|
|
|
BIND_ENUM_CONSTANT(CURSOR_CAN_DROP);
|
|
|
|
BIND_ENUM_CONSTANT(CURSOR_FORBIDDEN);
|
|
|
|
BIND_ENUM_CONSTANT(CURSOR_VSIZE);
|
|
|
|
BIND_ENUM_CONSTANT(CURSOR_HSIZE);
|
|
|
|
BIND_ENUM_CONSTANT(CURSOR_BDIAGSIZE);
|
|
|
|
BIND_ENUM_CONSTANT(CURSOR_FDIAGSIZE);
|
|
|
|
BIND_ENUM_CONSTANT(CURSOR_MOVE);
|
|
|
|
BIND_ENUM_CONSTANT(CURSOR_VSPLIT);
|
|
|
|
BIND_ENUM_CONSTANT(CURSOR_HSPLIT);
|
|
|
|
BIND_ENUM_CONSTANT(CURSOR_HELP);
|
|
|
|
|
2018-04-06 09:09:40 +02:00
|
|
|
ADD_SIGNAL(MethodInfo("joy_connection_changed", PropertyInfo(Variant::INT, "device"), PropertyInfo(Variant::BOOL, "connected")));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void Input::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
|
2015-01-03 17:03:13 +01:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
2022-04-27 09:26:46 +02:00
|
|
|
const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
|
2018-12-18 02:53:54 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String pf = p_function;
|
2021-10-28 13:23:24 +02:00
|
|
|
if (p_idx == 0 &&
|
|
|
|
(pf == "is_action_pressed" || pf == "action_press" || pf == "action_release" ||
|
|
|
|
pf == "is_action_just_pressed" || pf == "is_action_just_released" ||
|
|
|
|
pf == "get_action_strength" || pf == "get_action_raw_strength" ||
|
|
|
|
pf == "get_axis" || pf == "get_vector")) {
|
2015-01-03 17:03:13 +01:00
|
|
|
List<PropertyInfo> pinfo;
|
2017-07-19 22:00:46 +02:00
|
|
|
ProjectSettings::get_singleton()->get_property_list(&pinfo);
|
2015-01-03 17:03:13 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
|
|
|
|
const PropertyInfo &pi = E->get();
|
2015-01-03 17:03:13 +01:00
|
|
|
|
2021-05-05 12:44:11 +02:00
|
|
|
if (!pi.name.begins_with("input/")) {
|
2015-01-03 17:03:13 +01:00
|
|
|
continue;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2015-01-03 17:03:13 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
|
2018-12-18 02:53:54 +01:00
|
|
|
r_options->push_back(quote_style + name + quote_style);
|
2015-01-03 17:03:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Input::Input() {
|
2017-03-05 16:44:50 +01:00
|
|
|
singleton = this;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////
|