1 /*************************************************************************/
2 /*  input.cpp                                                            */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #include "input.h"
32 
33 #include "core/input_map.h"
34 #include "core/os/os.h"
35 #include "core/project_settings.h"
36 
37 #ifdef TOOLS_ENABLED
38 #include "editor/editor_settings.h"
39 #endif
40 
41 Input *Input::singleton = NULL;
42 
get_singleton()43 Input *Input::get_singleton() {
44 
45 	return singleton;
46 }
47 
set_mouse_mode(MouseMode p_mode)48 void Input::set_mouse_mode(MouseMode p_mode) {
49 	ERR_FAIL_INDEX((int)p_mode, 4);
50 	OS::get_singleton()->set_mouse_mode((OS::MouseMode)p_mode);
51 }
52 
get_mouse_mode() const53 Input::MouseMode Input::get_mouse_mode() const {
54 
55 	return (MouseMode)OS::get_singleton()->get_mouse_mode();
56 }
57 
_bind_methods()58 void Input::_bind_methods() {
59 
60 	ClassDB::bind_method(D_METHOD("is_key_pressed", "scancode"), &Input::is_key_pressed);
61 	ClassDB::bind_method(D_METHOD("is_mouse_button_pressed", "button"), &Input::is_mouse_button_pressed);
62 	ClassDB::bind_method(D_METHOD("is_joy_button_pressed", "device", "button"), &Input::is_joy_button_pressed);
63 	ClassDB::bind_method(D_METHOD("is_action_pressed", "action"), &Input::is_action_pressed);
64 	ClassDB::bind_method(D_METHOD("is_action_just_pressed", "action"), &Input::is_action_just_pressed);
65 	ClassDB::bind_method(D_METHOD("is_action_just_released", "action"), &Input::is_action_just_released);
66 	ClassDB::bind_method(D_METHOD("get_action_strength", "action"), &Input::get_action_strength);
67 	ClassDB::bind_method(D_METHOD("add_joy_mapping", "mapping", "update_existing"), &Input::add_joy_mapping, DEFVAL(false));
68 	ClassDB::bind_method(D_METHOD("remove_joy_mapping", "guid"), &Input::remove_joy_mapping);
69 	ClassDB::bind_method(D_METHOD("joy_connection_changed", "device", "connected", "name", "guid"), &Input::joy_connection_changed);
70 	ClassDB::bind_method(D_METHOD("is_joy_known", "device"), &Input::is_joy_known);
71 	ClassDB::bind_method(D_METHOD("get_joy_axis", "device", "axis"), &Input::get_joy_axis);
72 	ClassDB::bind_method(D_METHOD("get_joy_name", "device"), &Input::get_joy_name);
73 	ClassDB::bind_method(D_METHOD("get_joy_guid", "device"), &Input::get_joy_guid);
74 	ClassDB::bind_method(D_METHOD("get_connected_joypads"), &Input::get_connected_joypads);
75 	ClassDB::bind_method(D_METHOD("get_joy_vibration_strength", "device"), &Input::get_joy_vibration_strength);
76 	ClassDB::bind_method(D_METHOD("get_joy_vibration_duration", "device"), &Input::get_joy_vibration_duration);
77 	ClassDB::bind_method(D_METHOD("get_joy_button_string", "button_index"), &Input::get_joy_button_string);
78 	ClassDB::bind_method(D_METHOD("get_joy_button_index_from_string", "button"), &Input::get_joy_button_index_from_string);
79 	ClassDB::bind_method(D_METHOD("get_joy_axis_string", "axis_index"), &Input::get_joy_axis_string);
80 	ClassDB::bind_method(D_METHOD("get_joy_axis_index_from_string", "axis"), &Input::get_joy_axis_index_from_string);
81 	ClassDB::bind_method(D_METHOD("start_joy_vibration", "device", "weak_magnitude", "strong_magnitude", "duration"), &Input::start_joy_vibration, DEFVAL(0));
82 	ClassDB::bind_method(D_METHOD("stop_joy_vibration", "device"), &Input::stop_joy_vibration);
83 	ClassDB::bind_method(D_METHOD("vibrate_handheld", "duration_ms"), &Input::vibrate_handheld, DEFVAL(500));
84 	ClassDB::bind_method(D_METHOD("get_gravity"), &Input::get_gravity);
85 	ClassDB::bind_method(D_METHOD("get_accelerometer"), &Input::get_accelerometer);
86 	ClassDB::bind_method(D_METHOD("get_magnetometer"), &Input::get_magnetometer);
87 	ClassDB::bind_method(D_METHOD("get_gyroscope"), &Input::get_gyroscope);
88 	//ClassDB::bind_method(D_METHOD("get_mouse_position"),&Input::get_mouse_position); - this is not the function you want
89 	ClassDB::bind_method(D_METHOD("get_last_mouse_speed"), &Input::get_last_mouse_speed);
90 	ClassDB::bind_method(D_METHOD("get_mouse_button_mask"), &Input::get_mouse_button_mask);
91 	ClassDB::bind_method(D_METHOD("set_mouse_mode", "mode"), &Input::set_mouse_mode);
92 	ClassDB::bind_method(D_METHOD("get_mouse_mode"), &Input::get_mouse_mode);
93 	ClassDB::bind_method(D_METHOD("warp_mouse_position", "to"), &Input::warp_mouse_position);
94 	ClassDB::bind_method(D_METHOD("action_press", "action", "strength"), &Input::action_press, DEFVAL(1.f));
95 	ClassDB::bind_method(D_METHOD("action_release", "action"), &Input::action_release);
96 	ClassDB::bind_method(D_METHOD("set_default_cursor_shape", "shape"), &Input::set_default_cursor_shape, DEFVAL(CURSOR_ARROW));
97 	ClassDB::bind_method(D_METHOD("get_current_cursor_shape"), &Input::get_current_cursor_shape);
98 	ClassDB::bind_method(D_METHOD("set_custom_mouse_cursor", "image", "shape", "hotspot"), &Input::set_custom_mouse_cursor, DEFVAL(CURSOR_ARROW), DEFVAL(Vector2()));
99 	ClassDB::bind_method(D_METHOD("parse_input_event", "event"), &Input::parse_input_event);
100 	ClassDB::bind_method(D_METHOD("set_use_accumulated_input", "enable"), &Input::set_use_accumulated_input);
101 
102 	BIND_ENUM_CONSTANT(MOUSE_MODE_VISIBLE);
103 	BIND_ENUM_CONSTANT(MOUSE_MODE_HIDDEN);
104 	BIND_ENUM_CONSTANT(MOUSE_MODE_CAPTURED);
105 	BIND_ENUM_CONSTANT(MOUSE_MODE_CONFINED);
106 
107 	BIND_ENUM_CONSTANT(CURSOR_ARROW);
108 	BIND_ENUM_CONSTANT(CURSOR_IBEAM);
109 	BIND_ENUM_CONSTANT(CURSOR_POINTING_HAND);
110 	BIND_ENUM_CONSTANT(CURSOR_CROSS);
111 	BIND_ENUM_CONSTANT(CURSOR_WAIT);
112 	BIND_ENUM_CONSTANT(CURSOR_BUSY);
113 	BIND_ENUM_CONSTANT(CURSOR_DRAG);
114 	BIND_ENUM_CONSTANT(CURSOR_CAN_DROP);
115 	BIND_ENUM_CONSTANT(CURSOR_FORBIDDEN);
116 	BIND_ENUM_CONSTANT(CURSOR_VSIZE);
117 	BIND_ENUM_CONSTANT(CURSOR_HSIZE);
118 	BIND_ENUM_CONSTANT(CURSOR_BDIAGSIZE);
119 	BIND_ENUM_CONSTANT(CURSOR_FDIAGSIZE);
120 	BIND_ENUM_CONSTANT(CURSOR_MOVE);
121 	BIND_ENUM_CONSTANT(CURSOR_VSPLIT);
122 	BIND_ENUM_CONSTANT(CURSOR_HSPLIT);
123 	BIND_ENUM_CONSTANT(CURSOR_HELP);
124 
125 	ADD_SIGNAL(MethodInfo("joy_connection_changed", PropertyInfo(Variant::INT, "device"), PropertyInfo(Variant::BOOL, "connected")));
126 }
127 
get_argument_options(const StringName & p_function,int p_idx,List<String> * r_options) const128 void Input::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
129 #ifdef TOOLS_ENABLED
130 
131 	const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", 0) ? "'" : "\"";
132 
133 	String pf = p_function;
134 	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")) {
135 
136 		List<PropertyInfo> pinfo;
137 		ProjectSettings::get_singleton()->get_property_list(&pinfo);
138 
139 		for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
140 			const PropertyInfo &pi = E->get();
141 
142 			if (!pi.name.begins_with("input/"))
143 				continue;
144 
145 			String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
146 			r_options->push_back(quote_style + name + quote_style);
147 		}
148 	}
149 #endif
150 }
151 
Input()152 Input::Input() {
153 
154 	singleton = this;
155 }
156 
157 //////////////////////////////////////////////////////////
158