input.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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. #include "input.h"
  31. #include "core/input/default_controller_mappings.h"
  32. #include "core/input/input_map.h"
  33. #include "core/os/os.h"
  34. #include "core/project_settings.h"
  35. #ifdef TOOLS_ENABLED
  36. #include "editor/editor_settings.h"
  37. #endif
  38. Input *Input::singleton = NULL;
  39. void (*Input::set_mouse_mode_func)(Input::MouseMode) = nullptr;
  40. Input::MouseMode (*Input::get_mouse_mode_func)() = nullptr;
  41. void (*Input::warp_mouse_func)(const Vector2 &p_to_pos) = nullptr;
  42. Input::CursorShape (*Input::get_current_cursor_shape_func)() = nullptr;
  43. void (*Input::set_custom_mouse_cursor_func)(const RES &, Input::CursorShape, const Vector2 &) = nullptr;
  44. Input *Input::get_singleton() {
  45. return singleton;
  46. }
  47. void Input::set_mouse_mode(MouseMode p_mode) {
  48. ERR_FAIL_INDEX((int)p_mode, 4);
  49. set_mouse_mode_func(p_mode);
  50. }
  51. Input::MouseMode Input::get_mouse_mode() const {
  52. return get_mouse_mode_func();
  53. }
  54. void Input::_bind_methods() {
  55. ClassDB::bind_method(D_METHOD("is_key_pressed", "keycode"), &Input::is_key_pressed);
  56. ClassDB::bind_method(D_METHOD("is_mouse_button_pressed", "button"), &Input::is_mouse_button_pressed);
  57. ClassDB::bind_method(D_METHOD("is_joy_button_pressed", "device", "button"), &Input::is_joy_button_pressed);
  58. ClassDB::bind_method(D_METHOD("is_action_pressed", "action"), &Input::is_action_pressed);
  59. ClassDB::bind_method(D_METHOD("is_action_just_pressed", "action"), &Input::is_action_just_pressed);
  60. ClassDB::bind_method(D_METHOD("is_action_just_released", "action"), &Input::is_action_just_released);
  61. ClassDB::bind_method(D_METHOD("get_action_strength", "action"), &Input::get_action_strength);
  62. ClassDB::bind_method(D_METHOD("add_joy_mapping", "mapping", "update_existing"), &Input::add_joy_mapping, DEFVAL(false));
  63. ClassDB::bind_method(D_METHOD("remove_joy_mapping", "guid"), &Input::remove_joy_mapping);
  64. ClassDB::bind_method(D_METHOD("joy_connection_changed", "device", "connected", "name", "guid"), &Input::joy_connection_changed);
  65. ClassDB::bind_method(D_METHOD("is_joy_known", "device"), &Input::is_joy_known);
  66. ClassDB::bind_method(D_METHOD("get_joy_axis", "device", "axis"), &Input::get_joy_axis);
  67. ClassDB::bind_method(D_METHOD("get_joy_name", "device"), &Input::get_joy_name);
  68. ClassDB::bind_method(D_METHOD("get_joy_guid", "device"), &Input::get_joy_guid);
  69. ClassDB::bind_method(D_METHOD("get_connected_joypads"), &Input::get_connected_joypads);
  70. ClassDB::bind_method(D_METHOD("get_joy_vibration_strength", "device"), &Input::get_joy_vibration_strength);
  71. ClassDB::bind_method(D_METHOD("get_joy_vibration_duration", "device"), &Input::get_joy_vibration_duration);
  72. ClassDB::bind_method(D_METHOD("get_joy_button_string", "button_index"), &Input::get_joy_button_string);
  73. ClassDB::bind_method(D_METHOD("get_joy_button_index_from_string", "button"), &Input::get_joy_button_index_from_string);
  74. ClassDB::bind_method(D_METHOD("get_joy_axis_string", "axis_index"), &Input::get_joy_axis_string);
  75. ClassDB::bind_method(D_METHOD("get_joy_axis_index_from_string", "axis"), &Input::get_joy_axis_index_from_string);
  76. ClassDB::bind_method(D_METHOD("start_joy_vibration", "device", "weak_magnitude", "strong_magnitude", "duration"), &Input::start_joy_vibration, DEFVAL(0));
  77. ClassDB::bind_method(D_METHOD("stop_joy_vibration", "device"), &Input::stop_joy_vibration);
  78. ClassDB::bind_method(D_METHOD("vibrate_handheld", "duration_ms"), &Input::vibrate_handheld, DEFVAL(500));
  79. ClassDB::bind_method(D_METHOD("get_gravity"), &Input::get_gravity);
  80. ClassDB::bind_method(D_METHOD("get_accelerometer"), &Input::get_accelerometer);
  81. ClassDB::bind_method(D_METHOD("get_magnetometer"), &Input::get_magnetometer);
  82. ClassDB::bind_method(D_METHOD("get_gyroscope"), &Input::get_gyroscope);
  83. //ClassDB::bind_method(D_METHOD("get_mouse_position"),&Input::get_mouse_position); - this is not the function you want
  84. ClassDB::bind_method(D_METHOD("get_last_mouse_speed"), &Input::get_last_mouse_speed);
  85. ClassDB::bind_method(D_METHOD("get_mouse_button_mask"), &Input::get_mouse_button_mask);
  86. ClassDB::bind_method(D_METHOD("set_mouse_mode", "mode"), &Input::set_mouse_mode);
  87. ClassDB::bind_method(D_METHOD("get_mouse_mode"), &Input::get_mouse_mode);
  88. ClassDB::bind_method(D_METHOD("warp_mouse_position", "to"), &Input::warp_mouse_position);
  89. ClassDB::bind_method(D_METHOD("action_press", "action", "strength"), &Input::action_press, DEFVAL(1.f));
  90. ClassDB::bind_method(D_METHOD("action_release", "action"), &Input::action_release);
  91. ClassDB::bind_method(D_METHOD("set_default_cursor_shape", "shape"), &Input::set_default_cursor_shape, DEFVAL(CURSOR_ARROW));
  92. ClassDB::bind_method(D_METHOD("get_current_cursor_shape"), &Input::get_current_cursor_shape);
  93. ClassDB::bind_method(D_METHOD("set_custom_mouse_cursor", "image", "shape", "hotspot"), &Input::set_custom_mouse_cursor, DEFVAL(CURSOR_ARROW), DEFVAL(Vector2()));
  94. ClassDB::bind_method(D_METHOD("parse_input_event", "event"), &Input::parse_input_event);
  95. ClassDB::bind_method(D_METHOD("set_use_accumulated_input", "enable"), &Input::set_use_accumulated_input);
  96. BIND_ENUM_CONSTANT(MOUSE_MODE_VISIBLE);
  97. BIND_ENUM_CONSTANT(MOUSE_MODE_HIDDEN);
  98. BIND_ENUM_CONSTANT(MOUSE_MODE_CAPTURED);
  99. BIND_ENUM_CONSTANT(MOUSE_MODE_CONFINED);
  100. BIND_ENUM_CONSTANT(CURSOR_ARROW);
  101. BIND_ENUM_CONSTANT(CURSOR_IBEAM);
  102. BIND_ENUM_CONSTANT(CURSOR_POINTING_HAND);
  103. BIND_ENUM_CONSTANT(CURSOR_CROSS);
  104. BIND_ENUM_CONSTANT(CURSOR_WAIT);
  105. BIND_ENUM_CONSTANT(CURSOR_BUSY);
  106. BIND_ENUM_CONSTANT(CURSOR_DRAG);
  107. BIND_ENUM_CONSTANT(CURSOR_CAN_DROP);
  108. BIND_ENUM_CONSTANT(CURSOR_FORBIDDEN);
  109. BIND_ENUM_CONSTANT(CURSOR_VSIZE);
  110. BIND_ENUM_CONSTANT(CURSOR_HSIZE);
  111. BIND_ENUM_CONSTANT(CURSOR_BDIAGSIZE);
  112. BIND_ENUM_CONSTANT(CURSOR_FDIAGSIZE);
  113. BIND_ENUM_CONSTANT(CURSOR_MOVE);
  114. BIND_ENUM_CONSTANT(CURSOR_VSPLIT);
  115. BIND_ENUM_CONSTANT(CURSOR_HSPLIT);
  116. BIND_ENUM_CONSTANT(CURSOR_HELP);
  117. ADD_SIGNAL(MethodInfo("joy_connection_changed", PropertyInfo(Variant::INT, "device"), PropertyInfo(Variant::BOOL, "connected")));
  118. }
  119. void Input::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  120. #ifdef TOOLS_ENABLED
  121. const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", 0) ? "'" : "\"";
  122. String pf = p_function;
  123. 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")) {
  124. List<PropertyInfo> pinfo;
  125. ProjectSettings::get_singleton()->get_property_list(&pinfo);
  126. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  127. const PropertyInfo &pi = E->get();
  128. if (!pi.name.begins_with("input/"))
  129. continue;
  130. String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  131. r_options->push_back(quote_style + name + quote_style);
  132. }
  133. }
  134. #endif
  135. }
  136. void Input::SpeedTrack::update(const Vector2 &p_delta_p) {
  137. uint64_t tick = OS::get_singleton()->get_ticks_usec();
  138. uint32_t tdiff = tick - last_tick;
  139. float delta_t = tdiff / 1000000.0;
  140. last_tick = tick;
  141. accum += p_delta_p;
  142. accum_t += delta_t;
  143. if (accum_t > max_ref_frame * 10)
  144. accum_t = max_ref_frame * 10;
  145. while (accum_t >= min_ref_frame) {
  146. float slice_t = min_ref_frame / accum_t;
  147. Vector2 slice = accum * slice_t;
  148. accum = accum - slice;
  149. accum_t -= min_ref_frame;
  150. speed = (slice / min_ref_frame).linear_interpolate(speed, min_ref_frame / max_ref_frame);
  151. }
  152. }
  153. void Input::SpeedTrack::reset() {
  154. last_tick = OS::get_singleton()->get_ticks_usec();
  155. speed = Vector2();
  156. accum_t = 0;
  157. }
  158. Input::SpeedTrack::SpeedTrack() {
  159. min_ref_frame = 0.1;
  160. max_ref_frame = 0.3;
  161. reset();
  162. }
  163. bool Input::is_key_pressed(int p_keycode) const {
  164. _THREAD_SAFE_METHOD_
  165. return keys_pressed.has(p_keycode);
  166. }
  167. bool Input::is_mouse_button_pressed(int p_button) const {
  168. _THREAD_SAFE_METHOD_
  169. return (mouse_button_mask & (1 << (p_button - 1))) != 0;
  170. }
  171. static int _combine_device(int p_value, int p_device) {
  172. return p_value | (p_device << 20);
  173. }
  174. bool Input::is_joy_button_pressed(int p_device, int p_button) const {
  175. _THREAD_SAFE_METHOD_
  176. return joy_buttons_pressed.has(_combine_device(p_button, p_device));
  177. }
  178. bool Input::is_action_pressed(const StringName &p_action) const {
  179. return action_state.has(p_action) && action_state[p_action].pressed;
  180. }
  181. bool Input::is_action_just_pressed(const StringName &p_action) const {
  182. const Map<StringName, Action>::Element *E = action_state.find(p_action);
  183. if (!E)
  184. return false;
  185. if (Engine::get_singleton()->is_in_physics_frame()) {
  186. return E->get().pressed && E->get().physics_frame == Engine::get_singleton()->get_physics_frames();
  187. } else {
  188. return E->get().pressed && E->get().idle_frame == Engine::get_singleton()->get_idle_frames();
  189. }
  190. }
  191. bool Input::is_action_just_released(const StringName &p_action) const {
  192. const Map<StringName, Action>::Element *E = action_state.find(p_action);
  193. if (!E)
  194. return false;
  195. if (Engine::get_singleton()->is_in_physics_frame()) {
  196. return !E->get().pressed && E->get().physics_frame == Engine::get_singleton()->get_physics_frames();
  197. } else {
  198. return !E->get().pressed && E->get().idle_frame == Engine::get_singleton()->get_idle_frames();
  199. }
  200. }
  201. float Input::get_action_strength(const StringName &p_action) const {
  202. const Map<StringName, Action>::Element *E = action_state.find(p_action);
  203. if (!E)
  204. return 0.0f;
  205. return E->get().strength;
  206. }
  207. float Input::get_joy_axis(int p_device, int p_axis) const {
  208. _THREAD_SAFE_METHOD_
  209. int c = _combine_device(p_axis, p_device);
  210. if (_joy_axis.has(c)) {
  211. return _joy_axis[c];
  212. } else {
  213. return 0;
  214. }
  215. }
  216. String Input::get_joy_name(int p_idx) {
  217. _THREAD_SAFE_METHOD_
  218. return joy_names[p_idx].name;
  219. };
  220. Vector2 Input::get_joy_vibration_strength(int p_device) {
  221. if (joy_vibration.has(p_device)) {
  222. return Vector2(joy_vibration[p_device].weak_magnitude, joy_vibration[p_device].strong_magnitude);
  223. } else {
  224. return Vector2(0, 0);
  225. }
  226. }
  227. uint64_t Input::get_joy_vibration_timestamp(int p_device) {
  228. if (joy_vibration.has(p_device)) {
  229. return joy_vibration[p_device].timestamp;
  230. } else {
  231. return 0;
  232. }
  233. }
  234. float Input::get_joy_vibration_duration(int p_device) {
  235. if (joy_vibration.has(p_device)) {
  236. return joy_vibration[p_device].duration;
  237. } else {
  238. return 0.f;
  239. }
  240. }
  241. static String _hex_str(uint8_t p_byte) {
  242. static const char *dict = "0123456789abcdef";
  243. char ret[3];
  244. ret[2] = 0;
  245. ret[0] = dict[p_byte >> 4];
  246. ret[1] = dict[p_byte & 0xf];
  247. return ret;
  248. };
  249. void Input::joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid) {
  250. _THREAD_SAFE_METHOD_
  251. Joypad js;
  252. js.name = p_connected ? p_name : "";
  253. js.uid = p_connected ? p_guid : "";
  254. if (p_connected) {
  255. String uidname = p_guid;
  256. if (p_guid == "") {
  257. int uidlen = MIN(p_name.length(), 16);
  258. for (int i = 0; i < uidlen; i++) {
  259. uidname = uidname + _hex_str(p_name[i]);
  260. };
  261. };
  262. js.uid = uidname;
  263. js.connected = true;
  264. int mapping = fallback_mapping;
  265. for (int i = 0; i < map_db.size(); i++) {
  266. if (js.uid == map_db[i].uid) {
  267. mapping = i;
  268. js.name = map_db[i].name;
  269. };
  270. };
  271. js.mapping = mapping;
  272. } else {
  273. js.connected = false;
  274. for (int i = 0; i < JOY_BUTTON_MAX; i++) {
  275. if (i < JOY_AXIS_MAX)
  276. set_joy_axis(p_idx, i, 0.0f);
  277. int c = _combine_device(i, p_idx);
  278. joy_buttons_pressed.erase(c);
  279. };
  280. };
  281. joy_names[p_idx] = js;
  282. emit_signal("joy_connection_changed", p_idx, p_connected);
  283. };
  284. Vector3 Input::get_gravity() const {
  285. _THREAD_SAFE_METHOD_
  286. return gravity;
  287. }
  288. Vector3 Input::get_accelerometer() const {
  289. _THREAD_SAFE_METHOD_
  290. return accelerometer;
  291. }
  292. Vector3 Input::get_magnetometer() const {
  293. _THREAD_SAFE_METHOD_
  294. return magnetometer;
  295. }
  296. Vector3 Input::get_gyroscope() const {
  297. _THREAD_SAFE_METHOD_
  298. return gyroscope;
  299. }
  300. void Input::parse_drop_files(const Vector<String> &p_files) {
  301. if (main_loop) {
  302. main_loop->drop_files(p_files);
  303. }
  304. }
  305. void Input::parse_notification(int p_notification) {
  306. if (main_loop) {
  307. main_loop->notification(p_notification);
  308. }
  309. }
  310. void Input::parse_input_event(const Ref<InputEvent> &p_event) {
  311. _parse_input_event_impl(p_event, false);
  312. }
  313. void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated) {
  314. // Notes on mouse-touch emulation:
  315. // - Emulated mouse events are parsed, that is, re-routed to this method, so they make the same effects
  316. // as true mouse events. The only difference is the situation is flagged as emulated so they are not
  317. // emulated back to touch events in an endless loop.
  318. // - Emulated touch events are handed right to the main loop (i.e., the SceneTree) because they don't
  319. // require additional handling by this class.
  320. _THREAD_SAFE_METHOD_
  321. Ref<InputEventKey> k = p_event;
  322. if (k.is_valid() && !k->is_echo() && k->get_keycode() != 0) {
  323. if (k->is_pressed())
  324. keys_pressed.insert(k->get_keycode());
  325. else
  326. keys_pressed.erase(k->get_keycode());
  327. }
  328. Ref<InputEventMouseButton> mb = p_event;
  329. if (mb.is_valid()) {
  330. if (mb->is_pressed()) {
  331. mouse_button_mask |= (1 << (mb->get_button_index() - 1));
  332. } else {
  333. mouse_button_mask &= ~(1 << (mb->get_button_index() - 1));
  334. }
  335. Point2 pos = mb->get_global_position();
  336. if (mouse_pos != pos) {
  337. set_mouse_position(pos);
  338. }
  339. if (main_loop && emulate_touch_from_mouse && !p_is_emulated && mb->get_button_index() == 1) {
  340. Ref<InputEventScreenTouch> touch_event;
  341. touch_event.instance();
  342. touch_event->set_pressed(mb->is_pressed());
  343. touch_event->set_position(mb->get_position());
  344. main_loop->input_event(touch_event);
  345. }
  346. }
  347. Ref<InputEventMouseMotion> mm = p_event;
  348. if (mm.is_valid()) {
  349. Point2 pos = mm->get_global_position();
  350. if (mouse_pos != pos) {
  351. set_mouse_position(pos);
  352. }
  353. if (main_loop && emulate_touch_from_mouse && !p_is_emulated && mm->get_button_mask() & 1) {
  354. Ref<InputEventScreenDrag> drag_event;
  355. drag_event.instance();
  356. drag_event->set_position(mm->get_position());
  357. drag_event->set_relative(mm->get_relative());
  358. drag_event->set_speed(mm->get_speed());
  359. main_loop->input_event(drag_event);
  360. }
  361. }
  362. Ref<InputEventScreenTouch> st = p_event;
  363. if (st.is_valid()) {
  364. if (st->is_pressed()) {
  365. SpeedTrack &track = touch_speed_track[st->get_index()];
  366. track.reset();
  367. } else {
  368. // Since a pointer index may not occur again (OSs may or may not reuse them),
  369. // imperatively remove it from the map to keep no fossil entries in it
  370. touch_speed_track.erase(st->get_index());
  371. }
  372. if (emulate_mouse_from_touch) {
  373. bool translate = false;
  374. if (st->is_pressed()) {
  375. if (mouse_from_touch_index == -1) {
  376. translate = true;
  377. mouse_from_touch_index = st->get_index();
  378. }
  379. } else {
  380. if (st->get_index() == mouse_from_touch_index) {
  381. translate = true;
  382. mouse_from_touch_index = -1;
  383. }
  384. }
  385. if (translate) {
  386. Ref<InputEventMouseButton> button_event;
  387. button_event.instance();
  388. button_event->set_device(InputEvent::DEVICE_ID_TOUCH_MOUSE);
  389. button_event->set_position(st->get_position());
  390. button_event->set_global_position(st->get_position());
  391. button_event->set_pressed(st->is_pressed());
  392. button_event->set_button_index(BUTTON_LEFT);
  393. if (st->is_pressed()) {
  394. button_event->set_button_mask(mouse_button_mask | (1 << (BUTTON_LEFT - 1)));
  395. } else {
  396. button_event->set_button_mask(mouse_button_mask & ~(1 << (BUTTON_LEFT - 1)));
  397. }
  398. _parse_input_event_impl(button_event, true);
  399. }
  400. }
  401. }
  402. Ref<InputEventScreenDrag> sd = p_event;
  403. if (sd.is_valid()) {
  404. SpeedTrack &track = touch_speed_track[sd->get_index()];
  405. track.update(sd->get_relative());
  406. sd->set_speed(track.speed);
  407. if (emulate_mouse_from_touch && sd->get_index() == mouse_from_touch_index) {
  408. Ref<InputEventMouseMotion> motion_event;
  409. motion_event.instance();
  410. motion_event->set_device(InputEvent::DEVICE_ID_TOUCH_MOUSE);
  411. motion_event->set_position(sd->get_position());
  412. motion_event->set_global_position(sd->get_position());
  413. motion_event->set_relative(sd->get_relative());
  414. motion_event->set_speed(sd->get_speed());
  415. motion_event->set_button_mask(mouse_button_mask);
  416. _parse_input_event_impl(motion_event, true);
  417. }
  418. }
  419. Ref<InputEventJoypadButton> jb = p_event;
  420. if (jb.is_valid()) {
  421. int c = _combine_device(jb->get_button_index(), jb->get_device());
  422. if (jb->is_pressed())
  423. joy_buttons_pressed.insert(c);
  424. else
  425. joy_buttons_pressed.erase(c);
  426. }
  427. Ref<InputEventJoypadMotion> jm = p_event;
  428. if (jm.is_valid()) {
  429. set_joy_axis(jm->get_device(), jm->get_axis(), jm->get_axis_value());
  430. }
  431. Ref<InputEventGesture> ge = p_event;
  432. if (ge.is_valid()) {
  433. if (main_loop) {
  434. main_loop->input_event(ge);
  435. }
  436. }
  437. for (const Map<StringName, InputMap::Action>::Element *E = InputMap::get_singleton()->get_action_map().front(); E; E = E->next()) {
  438. if (InputMap::get_singleton()->event_is_action(p_event, E->key())) {
  439. // Save the action's state
  440. if (!p_event->is_echo() && is_action_pressed(E->key()) != p_event->is_action_pressed(E->key())) {
  441. Action action;
  442. action.physics_frame = Engine::get_singleton()->get_physics_frames();
  443. action.idle_frame = Engine::get_singleton()->get_idle_frames();
  444. action.pressed = p_event->is_action_pressed(E->key());
  445. action.strength = 0.f;
  446. action_state[E->key()] = action;
  447. }
  448. action_state[E->key()].strength = p_event->get_action_strength(E->key());
  449. }
  450. }
  451. if (main_loop)
  452. main_loop->input_event(p_event);
  453. }
  454. void Input::set_joy_axis(int p_device, int p_axis, float p_value) {
  455. _THREAD_SAFE_METHOD_
  456. int c = _combine_device(p_axis, p_device);
  457. _joy_axis[c] = p_value;
  458. }
  459. void Input::start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration) {
  460. _THREAD_SAFE_METHOD_
  461. if (p_weak_magnitude < 0.f || p_weak_magnitude > 1.f || p_strong_magnitude < 0.f || p_strong_magnitude > 1.f) {
  462. return;
  463. }
  464. VibrationInfo vibration;
  465. vibration.weak_magnitude = p_weak_magnitude;
  466. vibration.strong_magnitude = p_strong_magnitude;
  467. vibration.duration = p_duration;
  468. vibration.timestamp = OS::get_singleton()->get_ticks_usec();
  469. joy_vibration[p_device] = vibration;
  470. }
  471. void Input::stop_joy_vibration(int p_device) {
  472. _THREAD_SAFE_METHOD_
  473. VibrationInfo vibration;
  474. vibration.weak_magnitude = 0;
  475. vibration.strong_magnitude = 0;
  476. vibration.duration = 0;
  477. vibration.timestamp = OS::get_singleton()->get_ticks_usec();
  478. joy_vibration[p_device] = vibration;
  479. }
  480. void Input::vibrate_handheld(int p_duration_ms) {
  481. OS::get_singleton()->vibrate_handheld(p_duration_ms);
  482. }
  483. void Input::set_gravity(const Vector3 &p_gravity) {
  484. _THREAD_SAFE_METHOD_
  485. gravity = p_gravity;
  486. }
  487. void Input::set_accelerometer(const Vector3 &p_accel) {
  488. _THREAD_SAFE_METHOD_
  489. accelerometer = p_accel;
  490. }
  491. void Input::set_magnetometer(const Vector3 &p_magnetometer) {
  492. _THREAD_SAFE_METHOD_
  493. magnetometer = p_magnetometer;
  494. }
  495. void Input::set_gyroscope(const Vector3 &p_gyroscope) {
  496. _THREAD_SAFE_METHOD_
  497. gyroscope = p_gyroscope;
  498. }
  499. void Input::set_main_loop(MainLoop *p_main_loop) {
  500. main_loop = p_main_loop;
  501. }
  502. void Input::set_mouse_position(const Point2 &p_posf) {
  503. mouse_speed_track.update(p_posf - mouse_pos);
  504. mouse_pos = p_posf;
  505. }
  506. Point2 Input::get_mouse_position() const {
  507. return mouse_pos;
  508. }
  509. Point2 Input::get_last_mouse_speed() const {
  510. return mouse_speed_track.speed;
  511. }
  512. int Input::get_mouse_button_mask() const {
  513. return mouse_button_mask; // do not trust OS implementation, should remove it - OS::get_singleton()->get_mouse_button_state();
  514. }
  515. void Input::warp_mouse_position(const Vector2 &p_to) {
  516. warp_mouse_func(p_to);
  517. }
  518. Point2i Input::warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect) {
  519. // The relative distance reported for the next event after a warp is in the boundaries of the
  520. // size of the rect on that axis, but it may be greater, in which case there's not problem as fmod()
  521. // will warp it, but if the pointer has moved in the opposite direction between the pointer relocation
  522. // and the subsequent event, the reported relative distance will be less than the size of the rect
  523. // and thus fmod() will be disabled for handling the situation.
  524. // And due to this mouse warping mechanism being stateless, we need to apply some heuristics to
  525. // detect the warp: if the relative distance is greater than the half of the size of the relevant rect
  526. // (checked per each axis), it will be considered as the consequence of a former pointer warp.
  527. const Point2i rel_sgn(p_motion->get_relative().x >= 0.0f ? 1 : -1, p_motion->get_relative().y >= 0.0 ? 1 : -1);
  528. const Size2i warp_margin = p_rect.size * 0.5f;
  529. const Point2i rel_warped(
  530. Math::fmod(p_motion->get_relative().x + rel_sgn.x * warp_margin.x, p_rect.size.x) - rel_sgn.x * warp_margin.x,
  531. Math::fmod(p_motion->get_relative().y + rel_sgn.y * warp_margin.y, p_rect.size.y) - rel_sgn.y * warp_margin.y);
  532. const Point2i pos_local = p_motion->get_global_position() - p_rect.position;
  533. const Point2i pos_warped(Math::fposmod(pos_local.x, p_rect.size.x), Math::fposmod(pos_local.y, p_rect.size.y));
  534. if (pos_warped != pos_local) {
  535. warp_mouse_position(pos_warped + p_rect.position);
  536. }
  537. return rel_warped;
  538. }
  539. void Input::iteration(float p_step) {
  540. }
  541. void Input::action_press(const StringName &p_action, float p_strength) {
  542. Action action;
  543. action.physics_frame = Engine::get_singleton()->get_physics_frames();
  544. action.idle_frame = Engine::get_singleton()->get_idle_frames();
  545. action.pressed = true;
  546. action.strength = p_strength;
  547. action_state[p_action] = action;
  548. }
  549. void Input::action_release(const StringName &p_action) {
  550. Action action;
  551. action.physics_frame = Engine::get_singleton()->get_physics_frames();
  552. action.idle_frame = Engine::get_singleton()->get_idle_frames();
  553. action.pressed = false;
  554. action.strength = 0.f;
  555. action_state[p_action] = action;
  556. }
  557. void Input::set_emulate_touch_from_mouse(bool p_emulate) {
  558. emulate_touch_from_mouse = p_emulate;
  559. }
  560. bool Input::is_emulating_touch_from_mouse() const {
  561. return emulate_touch_from_mouse;
  562. }
  563. // Calling this whenever the game window is focused helps unstucking the "touch mouse"
  564. // if the OS or its abstraction class hasn't properly reported that touch pointers raised
  565. void Input::ensure_touch_mouse_raised() {
  566. if (mouse_from_touch_index != -1) {
  567. mouse_from_touch_index = -1;
  568. Ref<InputEventMouseButton> button_event;
  569. button_event.instance();
  570. button_event->set_device(InputEvent::DEVICE_ID_TOUCH_MOUSE);
  571. button_event->set_position(mouse_pos);
  572. button_event->set_global_position(mouse_pos);
  573. button_event->set_pressed(false);
  574. button_event->set_button_index(BUTTON_LEFT);
  575. button_event->set_button_mask(mouse_button_mask & ~(1 << (BUTTON_LEFT - 1)));
  576. _parse_input_event_impl(button_event, true);
  577. }
  578. }
  579. void Input::set_emulate_mouse_from_touch(bool p_emulate) {
  580. emulate_mouse_from_touch = p_emulate;
  581. }
  582. bool Input::is_emulating_mouse_from_touch() const {
  583. return emulate_mouse_from_touch;
  584. }
  585. Input::CursorShape Input::get_default_cursor_shape() const {
  586. return default_shape;
  587. }
  588. void Input::set_default_cursor_shape(CursorShape p_shape) {
  589. if (default_shape == p_shape)
  590. return;
  591. default_shape = p_shape;
  592. // The default shape is set in Viewport::_gui_input_event. To instantly
  593. // see the shape in the viewport we need to trigger a mouse motion event.
  594. Ref<InputEventMouseMotion> mm;
  595. mm.instance();
  596. mm->set_position(mouse_pos);
  597. mm->set_global_position(mouse_pos);
  598. parse_input_event(mm);
  599. }
  600. Input::CursorShape Input::get_current_cursor_shape() const {
  601. return get_current_cursor_shape_func();
  602. }
  603. void Input::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  604. if (Engine::get_singleton()->is_editor_hint())
  605. return;
  606. set_custom_mouse_cursor_func(p_cursor, p_shape, p_hotspot);
  607. }
  608. void Input::accumulate_input_event(const Ref<InputEvent> &p_event) {
  609. ERR_FAIL_COND(p_event.is_null());
  610. if (!use_accumulated_input) {
  611. parse_input_event(p_event);
  612. return;
  613. }
  614. if (!accumulated_events.empty() && accumulated_events.back()->get()->accumulate(p_event)) {
  615. return; //event was accumulated, exit
  616. }
  617. accumulated_events.push_back(p_event);
  618. }
  619. void Input::flush_accumulated_events() {
  620. while (accumulated_events.front()) {
  621. parse_input_event(accumulated_events.front()->get());
  622. accumulated_events.pop_front();
  623. }
  624. }
  625. void Input::set_use_accumulated_input(bool p_enable) {
  626. use_accumulated_input = p_enable;
  627. }
  628. void Input::release_pressed_events() {
  629. flush_accumulated_events(); // this is needed to release actions strengths
  630. keys_pressed.clear();
  631. joy_buttons_pressed.clear();
  632. _joy_axis.clear();
  633. for (Map<StringName, Input::Action>::Element *E = action_state.front(); E; E = E->next()) {
  634. if (E->get().pressed)
  635. action_release(E->key());
  636. }
  637. }
  638. void Input::joy_button(int p_device, int p_button, bool p_pressed) {
  639. _THREAD_SAFE_METHOD_;
  640. Joypad &joy = joy_names[p_device];
  641. //printf("got button %i, mapping is %i\n", p_button, joy.mapping);
  642. if (joy.last_buttons[p_button] == p_pressed) {
  643. return;
  644. }
  645. joy.last_buttons[p_button] = p_pressed;
  646. if (joy.mapping == -1) {
  647. _button_event(p_device, p_button, p_pressed);
  648. return;
  649. }
  650. const Map<int, JoyEvent>::Element *el = map_db[joy.mapping].buttons.find(p_button);
  651. if (!el) {
  652. //don't process un-mapped events for now, it could mess things up badly for devices with additional buttons/axis
  653. //return _button_event(p_last_id, p_device, p_button, p_pressed);
  654. return;
  655. }
  656. JoyEvent map = el->get();
  657. if (map.type == TYPE_BUTTON) {
  658. //fake additional axis event for triggers
  659. if (map.index == JOY_L2 || map.index == JOY_R2) {
  660. float value = p_pressed ? 1.0f : 0.0f;
  661. int axis = map.index == JOY_L2 ? JOY_ANALOG_L2 : JOY_ANALOG_R2;
  662. _axis_event(p_device, axis, value);
  663. }
  664. _button_event(p_device, map.index, p_pressed);
  665. return;
  666. }
  667. if (map.type == TYPE_AXIS) {
  668. _axis_event(p_device, map.index, p_pressed ? 1.0 : 0.0);
  669. }
  670. // no event?
  671. }
  672. void Input::joy_axis(int p_device, int p_axis, const JoyAxis &p_value) {
  673. _THREAD_SAFE_METHOD_;
  674. ERR_FAIL_INDEX(p_axis, JOY_AXIS_MAX);
  675. Joypad &joy = joy_names[p_device];
  676. if (joy.last_axis[p_axis] == p_value.value) {
  677. return;
  678. }
  679. if (p_value.value > joy.last_axis[p_axis]) {
  680. if (p_value.value < joy.last_axis[p_axis] + joy.filter) {
  681. return;
  682. }
  683. } else if (p_value.value > joy.last_axis[p_axis] - joy.filter) {
  684. return;
  685. }
  686. //when changing direction quickly, insert fake event to release pending inputmap actions
  687. float last = joy.last_axis[p_axis];
  688. if (p_value.min == 0 && (last < 0.25 || last > 0.75) && (last - 0.5) * (p_value.value - 0.5) < 0) {
  689. JoyAxis jx;
  690. jx.min = p_value.min;
  691. jx.value = p_value.value < 0.5 ? 0.6 : 0.4;
  692. joy_axis(p_device, p_axis, jx);
  693. } else if (ABS(last) > 0.5 && last * p_value.value < 0) {
  694. JoyAxis jx;
  695. jx.min = p_value.min;
  696. jx.value = p_value.value < 0 ? 0.1 : -0.1;
  697. joy_axis(p_device, p_axis, jx);
  698. }
  699. joy.last_axis[p_axis] = p_value.value;
  700. float val = p_value.min == 0 ? -1.0f + 2.0f * p_value.value : p_value.value;
  701. if (joy.mapping == -1) {
  702. _axis_event(p_device, p_axis, val);
  703. return;
  704. };
  705. const Map<int, JoyEvent>::Element *el = map_db[joy.mapping].axis.find(p_axis);
  706. if (!el) {
  707. //return _axis_event(p_last_id, p_device, p_axis, p_value);
  708. return;
  709. };
  710. JoyEvent map = el->get();
  711. if (map.type == TYPE_BUTTON) {
  712. //send axis event for triggers
  713. if (map.index == JOY_L2 || map.index == JOY_R2) {
  714. float value = p_value.min == 0 ? p_value.value : 0.5f + p_value.value / 2.0f;
  715. int axis = map.index == JOY_L2 ? JOY_ANALOG_L2 : JOY_ANALOG_R2;
  716. _axis_event(p_device, axis, value);
  717. }
  718. if (map.index == JOY_DPAD_UP || map.index == JOY_DPAD_DOWN) {
  719. bool pressed = p_value.value != 0.0f;
  720. int button = p_value.value < 0 ? JOY_DPAD_UP : JOY_DPAD_DOWN;
  721. if (!pressed) {
  722. if (joy_buttons_pressed.has(_combine_device(JOY_DPAD_UP, p_device))) {
  723. _button_event(p_device, JOY_DPAD_UP, false);
  724. }
  725. if (joy_buttons_pressed.has(_combine_device(JOY_DPAD_DOWN, p_device))) {
  726. _button_event(p_device, JOY_DPAD_DOWN, false);
  727. }
  728. }
  729. if (pressed == joy_buttons_pressed.has(_combine_device(button, p_device))) {
  730. return;
  731. }
  732. _button_event(p_device, button, true);
  733. return;
  734. }
  735. if (map.index == JOY_DPAD_LEFT || map.index == JOY_DPAD_RIGHT) {
  736. bool pressed = p_value.value != 0.0f;
  737. int button = p_value.value < 0 ? JOY_DPAD_LEFT : JOY_DPAD_RIGHT;
  738. if (!pressed) {
  739. if (joy_buttons_pressed.has(_combine_device(JOY_DPAD_LEFT, p_device))) {
  740. _button_event(p_device, JOY_DPAD_LEFT, false);
  741. }
  742. if (joy_buttons_pressed.has(_combine_device(JOY_DPAD_RIGHT, p_device))) {
  743. _button_event(p_device, JOY_DPAD_RIGHT, false);
  744. }
  745. }
  746. if (pressed == joy_buttons_pressed.has(_combine_device(button, p_device))) {
  747. return;
  748. }
  749. _button_event(p_device, button, true);
  750. return;
  751. }
  752. float deadzone = p_value.min == 0 ? 0.5f : 0.0f;
  753. bool pressed = p_value.value > deadzone;
  754. if (pressed == joy_buttons_pressed.has(_combine_device(map.index, p_device))) {
  755. // button already pressed or released, this is an axis bounce value
  756. return;
  757. }
  758. _button_event(p_device, map.index, pressed);
  759. return;
  760. }
  761. if (map.type == TYPE_AXIS) {
  762. _axis_event(p_device, map.index, val);
  763. return;
  764. }
  765. //printf("invalid mapping\n");
  766. }
  767. void Input::joy_hat(int p_device, int p_val) {
  768. _THREAD_SAFE_METHOD_;
  769. const Joypad &joy = joy_names[p_device];
  770. const JoyEvent *map;
  771. if (joy.mapping == -1) {
  772. map = hat_map_default;
  773. } else {
  774. map = map_db[joy.mapping].hat;
  775. };
  776. int cur_val = joy_names[p_device].hat_current;
  777. if ((p_val & HAT_MASK_UP) != (cur_val & HAT_MASK_UP)) {
  778. _button_event(p_device, map[HAT_UP].index, p_val & HAT_MASK_UP);
  779. }
  780. if ((p_val & HAT_MASK_RIGHT) != (cur_val & HAT_MASK_RIGHT)) {
  781. _button_event(p_device, map[HAT_RIGHT].index, p_val & HAT_MASK_RIGHT);
  782. }
  783. if ((p_val & HAT_MASK_DOWN) != (cur_val & HAT_MASK_DOWN)) {
  784. _button_event(p_device, map[HAT_DOWN].index, p_val & HAT_MASK_DOWN);
  785. }
  786. if ((p_val & HAT_MASK_LEFT) != (cur_val & HAT_MASK_LEFT)) {
  787. _button_event(p_device, map[HAT_LEFT].index, p_val & HAT_MASK_LEFT);
  788. }
  789. joy_names[p_device].hat_current = p_val;
  790. }
  791. void Input::_button_event(int p_device, int p_index, bool p_pressed) {
  792. Ref<InputEventJoypadButton> ievent;
  793. ievent.instance();
  794. ievent->set_device(p_device);
  795. ievent->set_button_index(p_index);
  796. ievent->set_pressed(p_pressed);
  797. parse_input_event(ievent);
  798. }
  799. void Input::_axis_event(int p_device, int p_axis, float p_value) {
  800. Ref<InputEventJoypadMotion> ievent;
  801. ievent.instance();
  802. ievent->set_device(p_device);
  803. ievent->set_axis(p_axis);
  804. ievent->set_axis_value(p_value);
  805. parse_input_event(ievent);
  806. };
  807. Input::JoyEvent Input::_find_to_event(String p_to) {
  808. // string names of the SDL buttons in the same order as input_event.h godot buttons
  809. static const char *buttons[] = { "a", "b", "x", "y", "leftshoulder", "rightshoulder", "lefttrigger", "righttrigger", "leftstick", "rightstick", "back", "start", "dpup", "dpdown", "dpleft", "dpright", "guide", NULL };
  810. static const char *axis[] = { "leftx", "lefty", "rightx", "righty", NULL };
  811. JoyEvent ret;
  812. ret.type = -1;
  813. ret.index = 0;
  814. int i = 0;
  815. while (buttons[i]) {
  816. if (p_to == buttons[i]) {
  817. ret.type = TYPE_BUTTON;
  818. ret.index = i;
  819. ret.value = 0;
  820. return ret;
  821. };
  822. ++i;
  823. };
  824. i = 0;
  825. while (axis[i]) {
  826. if (p_to == axis[i]) {
  827. ret.type = TYPE_AXIS;
  828. ret.index = i;
  829. ret.value = 0;
  830. return ret;
  831. };
  832. ++i;
  833. };
  834. return ret;
  835. };
  836. void Input::parse_mapping(String p_mapping) {
  837. _THREAD_SAFE_METHOD_;
  838. JoyDeviceMapping mapping;
  839. for (int i = 0; i < HAT_MAX; ++i)
  840. mapping.hat[i].index = 1024 + i;
  841. Vector<String> entry = p_mapping.split(",");
  842. if (entry.size() < 2) {
  843. return;
  844. }
  845. CharString uid;
  846. uid.resize(17);
  847. mapping.uid = entry[0];
  848. mapping.name = entry[1];
  849. int idx = 1;
  850. while (++idx < entry.size()) {
  851. if (entry[idx] == "")
  852. continue;
  853. String from = entry[idx].get_slice(":", 1).replace(" ", "");
  854. String to = entry[idx].get_slice(":", 0).replace(" ", "");
  855. JoyEvent to_event = _find_to_event(to);
  856. if (to_event.type == -1)
  857. continue;
  858. String etype = from.substr(0, 1);
  859. if (etype == "a") {
  860. int aid = from.substr(1, from.length() - 1).to_int();
  861. mapping.axis[aid] = to_event;
  862. } else if (etype == "b") {
  863. int bid = from.substr(1, from.length() - 1).to_int();
  864. mapping.buttons[bid] = to_event;
  865. } else if (etype == "h") {
  866. int hat_value = from.get_slice(".", 1).to_int();
  867. switch (hat_value) {
  868. case 1:
  869. mapping.hat[HAT_UP] = to_event;
  870. break;
  871. case 2:
  872. mapping.hat[HAT_RIGHT] = to_event;
  873. break;
  874. case 4:
  875. mapping.hat[HAT_DOWN] = to_event;
  876. break;
  877. case 8:
  878. mapping.hat[HAT_LEFT] = to_event;
  879. break;
  880. };
  881. };
  882. };
  883. map_db.push_back(mapping);
  884. //printf("added mapping with uuid %ls\n", mapping.uid.c_str());
  885. };
  886. void Input::add_joy_mapping(String p_mapping, bool p_update_existing) {
  887. parse_mapping(p_mapping);
  888. if (p_update_existing) {
  889. Vector<String> entry = p_mapping.split(",");
  890. String uid = entry[0];
  891. for (int i = 0; i < joy_names.size(); i++) {
  892. if (uid == joy_names[i].uid) {
  893. joy_names[i].mapping = map_db.size() - 1;
  894. }
  895. }
  896. }
  897. }
  898. void Input::remove_joy_mapping(String p_guid) {
  899. for (int i = map_db.size() - 1; i >= 0; i--) {
  900. if (p_guid == map_db[i].uid) {
  901. map_db.remove(i);
  902. }
  903. }
  904. for (int i = 0; i < joy_names.size(); i++) {
  905. if (joy_names[i].uid == p_guid) {
  906. joy_names[i].mapping = -1;
  907. }
  908. }
  909. }
  910. void Input::set_fallback_mapping(String p_guid) {
  911. for (int i = 0; i < map_db.size(); i++) {
  912. if (map_db[i].uid == p_guid) {
  913. fallback_mapping = i;
  914. return;
  915. }
  916. }
  917. }
  918. //platforms that use the remapping system can override and call to these ones
  919. bool Input::is_joy_known(int p_device) {
  920. int mapping = joy_names[p_device].mapping;
  921. return mapping != -1 ? (mapping != fallback_mapping) : false;
  922. }
  923. String Input::get_joy_guid(int p_device) const {
  924. ERR_FAIL_COND_V(!joy_names.has(p_device), "");
  925. return joy_names[p_device].uid;
  926. }
  927. Array Input::get_connected_joypads() {
  928. Array ret;
  929. Map<int, Joypad>::Element *elem = joy_names.front();
  930. while (elem) {
  931. if (elem->get().connected) {
  932. ret.push_back(elem->key());
  933. }
  934. elem = elem->next();
  935. }
  936. return ret;
  937. }
  938. static const char *_buttons[JOY_BUTTON_MAX] = {
  939. "Face Button Bottom",
  940. "Face Button Right",
  941. "Face Button Left",
  942. "Face Button Top",
  943. "L",
  944. "R",
  945. "L2",
  946. "R2",
  947. "L3",
  948. "R3",
  949. "Select",
  950. "Start",
  951. "DPAD Up",
  952. "DPAD Down",
  953. "DPAD Left",
  954. "DPAD Right"
  955. };
  956. static const char *_axes[JOY_AXIS_MAX] = {
  957. "Left Stick X",
  958. "Left Stick Y",
  959. "Right Stick X",
  960. "Right Stick Y",
  961. "",
  962. "",
  963. "L2",
  964. "R2",
  965. "",
  966. ""
  967. };
  968. String Input::get_joy_button_string(int p_button) {
  969. ERR_FAIL_INDEX_V(p_button, JOY_BUTTON_MAX, "");
  970. return _buttons[p_button];
  971. }
  972. int Input::get_joy_button_index_from_string(String p_button) {
  973. for (int i = 0; i < JOY_BUTTON_MAX; i++) {
  974. if (p_button == _buttons[i]) {
  975. return i;
  976. }
  977. }
  978. ERR_FAIL_V(-1);
  979. }
  980. int Input::get_unused_joy_id() {
  981. for (int i = 0; i < JOYPADS_MAX; i++) {
  982. if (!joy_names.has(i) || !joy_names[i].connected) {
  983. return i;
  984. }
  985. }
  986. return -1;
  987. }
  988. String Input::get_joy_axis_string(int p_axis) {
  989. ERR_FAIL_INDEX_V(p_axis, JOY_AXIS_MAX, "");
  990. return _axes[p_axis];
  991. }
  992. int Input::get_joy_axis_index_from_string(String p_axis) {
  993. for (int i = 0; i < JOY_AXIS_MAX; i++) {
  994. if (p_axis == _axes[i]) {
  995. return i;
  996. }
  997. }
  998. ERR_FAIL_V(-1);
  999. }
  1000. Input::Input() {
  1001. singleton = this;
  1002. use_accumulated_input = true;
  1003. mouse_button_mask = 0;
  1004. mouse_window = 0;
  1005. emulate_touch_from_mouse = false;
  1006. emulate_mouse_from_touch = false;
  1007. mouse_from_touch_index = -1;
  1008. main_loop = NULL;
  1009. default_shape = CURSOR_ARROW;
  1010. hat_map_default[HAT_UP].type = TYPE_BUTTON;
  1011. hat_map_default[HAT_UP].index = JOY_DPAD_UP;
  1012. hat_map_default[HAT_UP].value = 0;
  1013. hat_map_default[HAT_RIGHT].type = TYPE_BUTTON;
  1014. hat_map_default[HAT_RIGHT].index = JOY_DPAD_RIGHT;
  1015. hat_map_default[HAT_RIGHT].value = 0;
  1016. hat_map_default[HAT_DOWN].type = TYPE_BUTTON;
  1017. hat_map_default[HAT_DOWN].index = JOY_DPAD_DOWN;
  1018. hat_map_default[HAT_DOWN].value = 0;
  1019. hat_map_default[HAT_LEFT].type = TYPE_BUTTON;
  1020. hat_map_default[HAT_LEFT].index = JOY_DPAD_LEFT;
  1021. hat_map_default[HAT_LEFT].value = 0;
  1022. fallback_mapping = -1;
  1023. // Parse default mappings.
  1024. {
  1025. int i = 0;
  1026. while (DefaultControllerMappings::mappings[i]) {
  1027. parse_mapping(DefaultControllerMappings::mappings[i++]);
  1028. }
  1029. }
  1030. // If defined, parse SDL_GAMECONTROLLERCONFIG for possible new mappings/overrides.
  1031. String env_mapping = OS::get_singleton()->get_environment("SDL_GAMECONTROLLERCONFIG");
  1032. if (env_mapping != "") {
  1033. Vector<String> entries = env_mapping.split("\n");
  1034. for (int i = 0; i < entries.size(); i++) {
  1035. if (entries[i] == "")
  1036. continue;
  1037. parse_mapping(entries[i]);
  1038. }
  1039. }
  1040. }
  1041. //////////////////////////////////////////////////////////