1 /*************************************************************************/
2 /*  input.cpp                                                            */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 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 #include "input.h"
31 #include "globals.h"
32 #include "input_map.h"
33 #include "os/os.h"
34 Input *Input::singleton = NULL;
35 
get_singleton()36 Input *Input::get_singleton() {
37 
38 	return singleton;
39 }
40 
set_mouse_mode(MouseMode p_mode)41 void Input::set_mouse_mode(MouseMode p_mode) {
42 	ERR_FAIL_INDEX(p_mode, 3);
43 	OS::get_singleton()->set_mouse_mode((OS::MouseMode)p_mode);
44 }
45 
get_mouse_mode() const46 Input::MouseMode Input::get_mouse_mode() const {
47 
48 	return (MouseMode)OS::get_singleton()->get_mouse_mode();
49 }
50 
_bind_methods()51 void Input::_bind_methods() {
52 
53 	ObjectTypeDB::bind_method(_MD("is_key_pressed", "scancode"), &Input::is_key_pressed);
54 	ObjectTypeDB::bind_method(_MD("is_mouse_button_pressed", "button"), &Input::is_mouse_button_pressed);
55 	ObjectTypeDB::bind_method(_MD("is_joy_button_pressed", "device", "button"), &Input::is_joy_button_pressed);
56 	ObjectTypeDB::bind_method(_MD("is_action_pressed", "action"), &Input::is_action_pressed);
57 	ObjectTypeDB::bind_method(_MD("add_joy_mapping", "mapping", "update_existing"), &Input::add_joy_mapping, DEFVAL(false));
58 	ObjectTypeDB::bind_method(_MD("remove_joy_mapping", "guid"), &Input::remove_joy_mapping);
59 	ObjectTypeDB::bind_method(_MD("is_joy_known", "device"), &Input::is_joy_known);
60 	ObjectTypeDB::bind_method(_MD("get_joy_axis", "device", "axis"), &Input::get_joy_axis);
61 	ObjectTypeDB::bind_method(_MD("get_joy_name", "device"), &Input::get_joy_name);
62 	ObjectTypeDB::bind_method(_MD("get_joy_guid", "device"), &Input::get_joy_guid);
63 	ObjectTypeDB::bind_method(_MD("get_connected_joysticks"), &Input::get_connected_joysticks);
64 	ObjectTypeDB::bind_method(_MD("get_joy_vibration_strength", "device"), &Input::get_joy_vibration_strength);
65 	ObjectTypeDB::bind_method(_MD("get_joy_vibration_duration", "device"), &Input::get_joy_vibration_duration);
66 	ObjectTypeDB::bind_method(_MD("get_joy_button_string", "button_index"), &Input::get_joy_button_string);
67 	ObjectTypeDB::bind_method(_MD("get_joy_button_index_from_string", "button"), &Input::get_joy_button_index_from_string);
68 	ObjectTypeDB::bind_method(_MD("get_joy_axis_string", "axis_index"), &Input::get_joy_axis_string);
69 	ObjectTypeDB::bind_method(_MD("get_joy_axis_index_from_string", "axis"), &Input::get_joy_axis_index_from_string);
70 	ObjectTypeDB::bind_method(_MD("start_joy_vibration", "device", "weak_magnitude", "strong_magnitude", "duration"), &Input::start_joy_vibration, DEFVAL(0));
71 	ObjectTypeDB::bind_method(_MD("stop_joy_vibration", "device"), &Input::stop_joy_vibration);
72 	ObjectTypeDB::bind_method(_MD("get_gravity"), &Input::get_gravity);
73 	ObjectTypeDB::bind_method(_MD("get_accelerometer"), &Input::get_accelerometer);
74 	ObjectTypeDB::bind_method(_MD("get_magnetometer"), &Input::get_magnetometer);
75 	ObjectTypeDB::bind_method(_MD("get_gyroscope"), &Input::get_gyroscope);
76 	//ObjectTypeDB::bind_method(_MD("get_mouse_pos"),&Input::get_mouse_pos); - this is not the function you want
77 	ObjectTypeDB::bind_method(_MD("get_mouse_speed"), &Input::get_mouse_speed);
78 	ObjectTypeDB::bind_method(_MD("get_mouse_button_mask"), &Input::get_mouse_button_mask);
79 	ObjectTypeDB::bind_method(_MD("set_mouse_mode", "mode"), &Input::set_mouse_mode);
80 	ObjectTypeDB::bind_method(_MD("get_mouse_mode"), &Input::get_mouse_mode);
81 	ObjectTypeDB::bind_method(_MD("warp_mouse_pos", "to"), &Input::warp_mouse_pos);
82 	ObjectTypeDB::bind_method(_MD("action_press", "action"), &Input::action_press);
83 	ObjectTypeDB::bind_method(_MD("action_release", "action"), &Input::action_release);
84 	ObjectTypeDB::bind_method(_MD("set_custom_mouse_cursor", "image:Texture", "shape", "hotspot"), &Input::set_custom_mouse_cursor, DEFVAL(CURSOR_ARROW), DEFVAL(Vector2()));
85 	ObjectTypeDB::bind_method(_MD("parse_input_event", "event"), &Input::parse_input_event);
86 
87 	BIND_CONSTANT(MOUSE_MODE_VISIBLE);
88 	BIND_CONSTANT(MOUSE_MODE_HIDDEN);
89 	BIND_CONSTANT(MOUSE_MODE_CAPTURED);
90 
91 	// Those constants are used to change the mouse texture for given cursor shapes
92 	BIND_CONSTANT(CURSOR_ARROW);
93 	BIND_CONSTANT(CURSOR_IBEAM);
94 	BIND_CONSTANT(CURSOR_POINTING_HAND);
95 	BIND_CONSTANT(CURSOR_CROSS);
96 	BIND_CONSTANT(CURSOR_WAIT);
97 	BIND_CONSTANT(CURSOR_BUSY);
98 	BIND_CONSTANT(CURSOR_DRAG);
99 	BIND_CONSTANT(CURSOR_CAN_DROP);
100 	BIND_CONSTANT(CURSOR_FORBIDDEN);
101 	BIND_CONSTANT(CURSOR_VSIZE);
102 	BIND_CONSTANT(CURSOR_HSIZE);
103 	BIND_CONSTANT(CURSOR_BDIAGSIZE);
104 	BIND_CONSTANT(CURSOR_FDIAGSIZE);
105 	BIND_CONSTANT(CURSOR_MOVE);
106 	BIND_CONSTANT(CURSOR_VSPLIT);
107 	BIND_CONSTANT(CURSOR_HSPLIT);
108 	BIND_CONSTANT(CURSOR_HELP);
109 
110 	ADD_SIGNAL(MethodInfo("joy_connection_changed", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::BOOL, "connected")));
111 }
112 
get_argument_options(const StringName & p_function,int p_idx,List<String> * r_options) const113 void Input::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
114 #ifdef TOOLS_ENABLED
115 
116 	String pf = p_function;
117 	if (p_idx == 0 && (pf == "is_action_pressed" || pf == "action_press" || pf == "action_release")) {
118 
119 		List<PropertyInfo> pinfo;
120 		Globals::get_singleton()->get_property_list(&pinfo);
121 
122 		for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
123 			const PropertyInfo &pi = E->get();
124 
125 			if (!pi.name.begins_with("input/"))
126 				continue;
127 
128 			String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
129 			r_options->push_back("\"" + name + "\"");
130 		}
131 	}
132 #endif
133 }
134 
Input()135 Input::Input() {
136 
137 	singleton = this;
138 }
139 
140 //////////////////////////////////////////////////////////
141