2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* input.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00: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-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "input.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
|
|
|
|
#include "core/input_map.h"
|
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "core/project_settings.h"
|
|
|
|
|
2018-12-18 01:53:54 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
#include "editor/editor_settings.h"
|
|
|
|
#endif
|
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
Input *Input::singleton = nullptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Input *Input::get_singleton() {
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Input::set_mouse_mode(MouseMode p_mode) {
|
2018-10-04 07:17:59 +00:00
|
|
|
ERR_FAIL_INDEX((int)p_mode, 4);
|
2014-02-10 01:10:30 +00: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 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("is_key_pressed", "scancode"), &Input::is_key_pressed);
|
|
|
|
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 14:22:42 +00: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 01:02:18 +00: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 15:44:50 +00: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 16:45:44 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("joy_connection_changed", "device", "connected", "name", "guid"), &Input::joy_connection_changed);
|
2017-03-05 15:44:50 +00: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 11:47:24 +00: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 15:27:29 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("vibrate_handheld", "duration_ms"), &Input::vibrate_handheld, DEFVAL(500));
|
2017-03-05 15:44:50 +00: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 20:09:30 +00: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 15:29:38 +00:00
|
|
|
//ClassDB::bind_method(D_METHOD("get_mouse_position"),&Input::get_mouse_position); - this is not the function you want
|
2017-03-05 15:44:50 +00: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 13:37:49 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("warp_mouse_position", "to"), &Input::warp_mouse_position);
|
2018-11-17 20:45:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("action_press", "action", "strength"), &Input::action_press, DEFVAL(1.f));
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("action_release", "action"), &Input::action_release);
|
2018-04-09 20:48:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_default_cursor_shape", "shape"), &Input::set_default_cursor_shape, DEFVAL(CURSOR_ARROW));
|
2019-04-15 15:30:20 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_current_cursor_shape"), &Input::get_current_cursor_shape);
|
2017-11-10 10:50:11 +00: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 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("parse_input_event", "event"), &Input::parse_input_event);
|
2019-03-03 22:52:18 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_use_accumulated_input", "enable"), &Input::set_use_accumulated_input);
|
2021-10-14 16:58:15 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("flush_buffered_events"), &Input::flush_buffered_events);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2017-08-20 15:45:01 +00: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 15:44:50 +00:00
|
|
|
|
2017-11-10 10:50:11 +00: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 07:09:40 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("joy_connection_changed", PropertyInfo(Variant::INT, "device"), PropertyInfo(Variant::BOOL, "connected")));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Input::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
|
2015-01-03 16:03:13 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
2021-07-23 01:02:18 +00:00
|
|
|
const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\"";
|
2018-12-18 01:53:54 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String pf = p_function;
|
2021-07-23 01:02:18 +00: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 16:03:13 +00:00
|
|
|
List<PropertyInfo> pinfo;
|
2017-07-19 20:00:46 +00:00
|
|
|
ProjectSettings::get_singleton()->get_property_list(&pinfo);
|
2015-01-03 16:03:13 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
|
|
|
|
const PropertyInfo &pi = E->get();
|
2015-01-03 16:03:13 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!pi.name.begins_with("input/")) {
|
2015-01-03 16:03:13 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2015-01-03 16:03:13 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
|
2018-12-18 01:53:54 +00:00
|
|
|
r_options->push_back(quote_style + name + quote_style);
|
2015-01-03 16:03:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Input::Input() {
|
2017-03-05 15:44:50 +00:00
|
|
|
singleton = this;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////
|