input_default.cpp 31 KB

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