input_default.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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_device(InputEvent::DEVICE_ID_TOUCH_MOUSE);
  279. button_event->set_position(st->get_position());
  280. button_event->set_global_position(st->get_position());
  281. button_event->set_pressed(st->is_pressed());
  282. button_event->set_button_index(BUTTON_LEFT);
  283. if (st->is_pressed()) {
  284. button_event->set_button_mask(mouse_button_mask | (1 << (BUTTON_LEFT - 1)));
  285. } else {
  286. button_event->set_button_mask(mouse_button_mask & ~(1 << (BUTTON_LEFT - 1)));
  287. }
  288. _parse_input_event_impl(button_event, true);
  289. }
  290. }
  291. }
  292. Ref<InputEventScreenDrag> sd = p_event;
  293. if (sd.is_valid()) {
  294. SpeedTrack &track = touch_speed_track[sd->get_index()];
  295. track.update(sd->get_relative());
  296. sd->set_speed(track.speed);
  297. if (emulate_mouse_from_touch && sd->get_index() == mouse_from_touch_index) {
  298. Ref<InputEventMouseMotion> motion_event;
  299. motion_event.instance();
  300. motion_event->set_device(InputEvent::DEVICE_ID_TOUCH_MOUSE);
  301. motion_event->set_position(sd->get_position());
  302. motion_event->set_global_position(sd->get_position());
  303. motion_event->set_relative(sd->get_relative());
  304. motion_event->set_speed(sd->get_speed());
  305. motion_event->set_button_mask(mouse_button_mask);
  306. _parse_input_event_impl(motion_event, true);
  307. }
  308. }
  309. Ref<InputEventJoypadButton> jb = p_event;
  310. if (jb.is_valid()) {
  311. int c = _combine_device(jb->get_button_index(), jb->get_device());
  312. if (jb->is_pressed())
  313. joy_buttons_pressed.insert(c);
  314. else
  315. joy_buttons_pressed.erase(c);
  316. }
  317. Ref<InputEventJoypadMotion> jm = p_event;
  318. if (jm.is_valid()) {
  319. set_joy_axis(jm->get_device(), jm->get_axis(), jm->get_axis_value());
  320. }
  321. Ref<InputEventGesture> ge = p_event;
  322. if (ge.is_valid()) {
  323. if (main_loop) {
  324. main_loop->input_event(ge);
  325. }
  326. }
  327. for (const Map<StringName, InputMap::Action>::Element *E = InputMap::get_singleton()->get_action_map().front(); E; E = E->next()) {
  328. if (InputMap::get_singleton()->event_is_action(p_event, E->key())) {
  329. // Save the action's state
  330. if (!p_event->is_echo() && is_action_pressed(E->key()) != p_event->is_action_pressed(E->key())) {
  331. Action action;
  332. action.physics_frame = Engine::get_singleton()->get_physics_frames();
  333. action.idle_frame = Engine::get_singleton()->get_idle_frames();
  334. action.pressed = p_event->is_action_pressed(E->key());
  335. action.strength = 0.f;
  336. action_state[E->key()] = action;
  337. }
  338. action_state[E->key()].strength = p_event->get_action_strength(E->key());
  339. }
  340. }
  341. if (main_loop)
  342. main_loop->input_event(p_event);
  343. }
  344. void InputDefault::set_joy_axis(int p_device, int p_axis, float p_value) {
  345. _THREAD_SAFE_METHOD_
  346. int c = _combine_device(p_axis, p_device);
  347. _joy_axis[c] = p_value;
  348. }
  349. void InputDefault::start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration) {
  350. _THREAD_SAFE_METHOD_
  351. if (p_weak_magnitude < 0.f || p_weak_magnitude > 1.f || p_strong_magnitude < 0.f || p_strong_magnitude > 1.f) {
  352. return;
  353. }
  354. VibrationInfo vibration;
  355. vibration.weak_magnitude = p_weak_magnitude;
  356. vibration.strong_magnitude = p_strong_magnitude;
  357. vibration.duration = p_duration;
  358. vibration.timestamp = OS::get_singleton()->get_ticks_usec();
  359. joy_vibration[p_device] = vibration;
  360. }
  361. void InputDefault::stop_joy_vibration(int p_device) {
  362. _THREAD_SAFE_METHOD_
  363. VibrationInfo vibration;
  364. vibration.weak_magnitude = 0;
  365. vibration.strong_magnitude = 0;
  366. vibration.duration = 0;
  367. vibration.timestamp = OS::get_singleton()->get_ticks_usec();
  368. joy_vibration[p_device] = vibration;
  369. }
  370. void InputDefault::vibrate_handheld(int p_duration_ms) {
  371. OS::get_singleton()->vibrate_handheld(p_duration_ms);
  372. }
  373. void InputDefault::set_gravity(const Vector3 &p_gravity) {
  374. _THREAD_SAFE_METHOD_
  375. gravity = p_gravity;
  376. }
  377. void InputDefault::set_accelerometer(const Vector3 &p_accel) {
  378. _THREAD_SAFE_METHOD_
  379. accelerometer = p_accel;
  380. }
  381. void InputDefault::set_magnetometer(const Vector3 &p_magnetometer) {
  382. _THREAD_SAFE_METHOD_
  383. magnetometer = p_magnetometer;
  384. }
  385. void InputDefault::set_gyroscope(const Vector3 &p_gyroscope) {
  386. _THREAD_SAFE_METHOD_
  387. gyroscope = p_gyroscope;
  388. }
  389. void InputDefault::set_main_loop(MainLoop *p_main_loop) {
  390. main_loop = p_main_loop;
  391. }
  392. void InputDefault::set_mouse_position(const Point2 &p_posf) {
  393. mouse_speed_track.update(p_posf - mouse_pos);
  394. mouse_pos = p_posf;
  395. }
  396. Point2 InputDefault::get_mouse_position() const {
  397. return mouse_pos;
  398. }
  399. Point2 InputDefault::get_last_mouse_speed() const {
  400. return mouse_speed_track.speed;
  401. }
  402. int InputDefault::get_mouse_button_mask() const {
  403. return mouse_button_mask; // do not trust OS implementation, should remove it - OS::get_singleton()->get_mouse_button_state();
  404. }
  405. void InputDefault::warp_mouse_position(const Vector2 &p_to) {
  406. OS::get_singleton()->warp_mouse_position(p_to);
  407. }
  408. Point2i InputDefault::warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect) {
  409. // The relative distance reported for the next event after a warp is in the boundaries of the
  410. // size of the rect on that axis, but it may be greater, in which case there's not problem as fmod()
  411. // will warp it, but if the pointer has moved in the opposite direction between the pointer relocation
  412. // and the subsequent event, the reported relative distance will be less than the size of the rect
  413. // and thus fmod() will be disabled for handling the situation.
  414. // And due to this mouse warping mechanism being stateless, we need to apply some heuristics to
  415. // detect the warp: if the relative distance is greater than the half of the size of the relevant rect
  416. // (checked per each axis), it will be considered as the consequence of a former pointer warp.
  417. const Point2i rel_sgn(p_motion->get_relative().x >= 0.0f ? 1 : -1, p_motion->get_relative().y >= 0.0 ? 1 : -1);
  418. const Size2i warp_margin = p_rect.size * 0.5f;
  419. const Point2i rel_warped(
  420. Math::fmod(p_motion->get_relative().x + rel_sgn.x * warp_margin.x, p_rect.size.x) - rel_sgn.x * warp_margin.x,
  421. Math::fmod(p_motion->get_relative().y + rel_sgn.y * warp_margin.y, p_rect.size.y) - rel_sgn.y * warp_margin.y);
  422. const Point2i pos_local = p_motion->get_global_position() - p_rect.position;
  423. const Point2i pos_warped(Math::fposmod(pos_local.x, p_rect.size.x), Math::fposmod(pos_local.y, p_rect.size.y));
  424. if (pos_warped != pos_local) {
  425. OS::get_singleton()->warp_mouse_position(pos_warped + p_rect.position);
  426. }
  427. return rel_warped;
  428. }
  429. void InputDefault::iteration(float p_step) {
  430. }
  431. void InputDefault::action_press(const StringName &p_action, float p_strength) {
  432. Action action;
  433. action.physics_frame = Engine::get_singleton()->get_physics_frames();
  434. action.idle_frame = Engine::get_singleton()->get_idle_frames();
  435. action.pressed = true;
  436. action.strength = p_strength;
  437. action_state[p_action] = action;
  438. }
  439. void InputDefault::action_release(const StringName &p_action) {
  440. Action action;
  441. action.physics_frame = Engine::get_singleton()->get_physics_frames();
  442. action.idle_frame = Engine::get_singleton()->get_idle_frames();
  443. action.pressed = false;
  444. action.strength = 0.f;
  445. action_state[p_action] = action;
  446. }
  447. void InputDefault::set_emulate_touch_from_mouse(bool p_emulate) {
  448. emulate_touch_from_mouse = p_emulate;
  449. }
  450. bool InputDefault::is_emulating_touch_from_mouse() const {
  451. return emulate_touch_from_mouse;
  452. }
  453. // Calling this whenever the game window is focused helps unstucking the "touch mouse"
  454. // if the OS or its abstraction class hasn't properly reported that touch pointers raised
  455. void InputDefault::ensure_touch_mouse_raised() {
  456. if (mouse_from_touch_index != -1) {
  457. mouse_from_touch_index = -1;
  458. Ref<InputEventMouseButton> button_event;
  459. button_event.instance();
  460. button_event->set_device(InputEvent::DEVICE_ID_TOUCH_MOUSE);
  461. button_event->set_position(mouse_pos);
  462. button_event->set_global_position(mouse_pos);
  463. button_event->set_pressed(false);
  464. button_event->set_button_index(BUTTON_LEFT);
  465. button_event->set_button_mask(mouse_button_mask & ~(1 << (BUTTON_LEFT - 1)));
  466. _parse_input_event_impl(button_event, true);
  467. }
  468. }
  469. void InputDefault::set_emulate_mouse_from_touch(bool p_emulate) {
  470. emulate_mouse_from_touch = p_emulate;
  471. }
  472. bool InputDefault::is_emulating_mouse_from_touch() const {
  473. return emulate_mouse_from_touch;
  474. }
  475. Input::CursorShape InputDefault::get_default_cursor_shape() const {
  476. return default_shape;
  477. }
  478. void InputDefault::set_default_cursor_shape(CursorShape p_shape) {
  479. if (default_shape == p_shape)
  480. return;
  481. default_shape = p_shape;
  482. // The default shape is set in Viewport::_gui_input_event. To instantly
  483. // see the shape in the viewport we need to trigger a mouse motion event.
  484. Ref<InputEventMouseMotion> mm;
  485. mm.instance();
  486. mm->set_position(mouse_pos);
  487. mm->set_global_position(mouse_pos);
  488. parse_input_event(mm);
  489. }
  490. Input::CursorShape InputDefault::get_current_cursor_shape() const {
  491. return (Input::CursorShape)OS::get_singleton()->get_cursor_shape();
  492. }
  493. void InputDefault::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  494. if (Engine::get_singleton()->is_editor_hint())
  495. return;
  496. OS::get_singleton()->set_custom_mouse_cursor(p_cursor, (OS::CursorShape)p_shape, p_hotspot);
  497. }
  498. void InputDefault::accumulate_input_event(const Ref<InputEvent> &p_event) {
  499. ERR_FAIL_COND(p_event.is_null());
  500. if (!use_accumulated_input) {
  501. parse_input_event(p_event);
  502. return;
  503. }
  504. if (!accumulated_events.empty() && accumulated_events.back()->get()->accumulate(p_event)) {
  505. return; //event was accumulated, exit
  506. }
  507. accumulated_events.push_back(p_event);
  508. }
  509. void InputDefault::flush_accumulated_events() {
  510. while (accumulated_events.front()) {
  511. parse_input_event(accumulated_events.front()->get());
  512. accumulated_events.pop_front();
  513. }
  514. }
  515. void InputDefault::set_use_accumulated_input(bool p_enable) {
  516. use_accumulated_input = p_enable;
  517. }
  518. InputDefault::InputDefault() {
  519. use_accumulated_input = true;
  520. mouse_button_mask = 0;
  521. emulate_touch_from_mouse = false;
  522. emulate_mouse_from_touch = false;
  523. mouse_from_touch_index = -1;
  524. main_loop = NULL;
  525. default_shape = CURSOR_ARROW;
  526. hat_map_default[HAT_UP].type = TYPE_BUTTON;
  527. hat_map_default[HAT_UP].index = JOY_DPAD_UP;
  528. hat_map_default[HAT_UP].value = 0;
  529. hat_map_default[HAT_RIGHT].type = TYPE_BUTTON;
  530. hat_map_default[HAT_RIGHT].index = JOY_DPAD_RIGHT;
  531. hat_map_default[HAT_RIGHT].value = 0;
  532. hat_map_default[HAT_DOWN].type = TYPE_BUTTON;
  533. hat_map_default[HAT_DOWN].index = JOY_DPAD_DOWN;
  534. hat_map_default[HAT_DOWN].value = 0;
  535. hat_map_default[HAT_LEFT].type = TYPE_BUTTON;
  536. hat_map_default[HAT_LEFT].index = JOY_DPAD_LEFT;
  537. hat_map_default[HAT_LEFT].value = 0;
  538. fallback_mapping = -1;
  539. String env_mapping = OS::get_singleton()->get_environment("SDL_GAMECONTROLLERCONFIG");
  540. if (env_mapping != "") {
  541. Vector<String> entries = env_mapping.split("\n");
  542. for (int i = 0; i < entries.size(); i++) {
  543. if (entries[i] == "")
  544. continue;
  545. parse_mapping(entries[i]);
  546. };
  547. };
  548. int i = 0;
  549. while (DefaultControllerMappings::mappings[i]) {
  550. parse_mapping(DefaultControllerMappings::mappings[i++]);
  551. };
  552. }
  553. void InputDefault::joy_button(int p_device, int p_button, bool p_pressed) {
  554. _THREAD_SAFE_METHOD_;
  555. Joypad &joy = joy_names[p_device];
  556. //printf("got button %i, mapping is %i\n", p_button, joy.mapping);
  557. if (joy.last_buttons[p_button] == p_pressed) {
  558. return;
  559. }
  560. joy.last_buttons[p_button] = p_pressed;
  561. if (joy.mapping == -1) {
  562. _button_event(p_device, p_button, p_pressed);
  563. return;
  564. };
  565. const Map<int, JoyEvent>::Element *el = map_db[joy.mapping].buttons.find(p_button);
  566. if (!el) {
  567. //don't process un-mapped events for now, it could mess things up badly for devices with additional buttons/axis
  568. //return _button_event(p_last_id, p_device, p_button, p_pressed);
  569. return;
  570. };
  571. JoyEvent map = el->get();
  572. if (map.type == TYPE_BUTTON) {
  573. //fake additional axis event for triggers
  574. if (map.index == JOY_L2 || map.index == JOY_R2) {
  575. float value = p_pressed ? 1.0f : 0.0f;
  576. int axis = map.index == JOY_L2 ? JOY_ANALOG_L2 : JOY_ANALOG_R2;
  577. _axis_event(p_device, axis, value);
  578. }
  579. _button_event(p_device, map.index, p_pressed);
  580. return;
  581. };
  582. if (map.type == TYPE_AXIS) {
  583. _axis_event(p_device, map.index, p_pressed ? 1.0 : 0.0);
  584. };
  585. return; // no event?
  586. };
  587. void InputDefault::joy_axis(int p_device, int p_axis, const JoyAxis &p_value) {
  588. _THREAD_SAFE_METHOD_;
  589. ERR_FAIL_INDEX(p_axis, JOY_AXIS_MAX);
  590. Joypad &joy = joy_names[p_device];
  591. if (joy.last_axis[p_axis] == p_value.value) {
  592. return;
  593. }
  594. if (p_value.value > joy.last_axis[p_axis]) {
  595. if (p_value.value < joy.last_axis[p_axis] + joy.filter) {
  596. return;
  597. }
  598. } else if (p_value.value > joy.last_axis[p_axis] - joy.filter) {
  599. return;
  600. }
  601. //when changing direction quickly, insert fake event to release pending inputmap actions
  602. float last = joy.last_axis[p_axis];
  603. if (p_value.min == 0 && (last < 0.25 || last > 0.75) && (last - 0.5) * (p_value.value - 0.5) < 0) {
  604. JoyAxis jx;
  605. jx.min = p_value.min;
  606. jx.value = p_value.value < 0.5 ? 0.6 : 0.4;
  607. joy_axis(p_device, p_axis, jx);
  608. } else if (ABS(last) > 0.5 && last * p_value.value < 0) {
  609. JoyAxis jx;
  610. jx.min = p_value.min;
  611. jx.value = p_value.value < 0 ? 0.1 : -0.1;
  612. joy_axis(p_device, p_axis, jx);
  613. }
  614. joy.last_axis[p_axis] = p_value.value;
  615. float val = p_value.min == 0 ? -1.0f + 2.0f * p_value.value : p_value.value;
  616. if (joy.mapping == -1) {
  617. _axis_event(p_device, p_axis, val);
  618. return;
  619. };
  620. const Map<int, JoyEvent>::Element *el = map_db[joy.mapping].axis.find(p_axis);
  621. if (!el) {
  622. //return _axis_event(p_last_id, p_device, p_axis, p_value);
  623. return;
  624. };
  625. JoyEvent map = el->get();
  626. if (map.type == TYPE_BUTTON) {
  627. //send axis event for triggers
  628. if (map.index == JOY_L2 || map.index == JOY_R2) {
  629. float value = p_value.min == 0 ? p_value.value : 0.5f + p_value.value / 2.0f;
  630. int axis = map.index == JOY_L2 ? JOY_ANALOG_L2 : JOY_ANALOG_R2;
  631. _axis_event(p_device, axis, value);
  632. }
  633. if (map.index == JOY_DPAD_UP || map.index == JOY_DPAD_DOWN) {
  634. bool pressed = p_value.value != 0.0f;
  635. int button = p_value.value < 0 ? JOY_DPAD_UP : JOY_DPAD_DOWN;
  636. if (!pressed) {
  637. if (joy_buttons_pressed.has(_combine_device(JOY_DPAD_UP, p_device))) {
  638. _button_event(p_device, JOY_DPAD_UP, false);
  639. }
  640. if (joy_buttons_pressed.has(_combine_device(JOY_DPAD_DOWN, p_device))) {
  641. _button_event(p_device, JOY_DPAD_DOWN, false);
  642. }
  643. }
  644. if (pressed == joy_buttons_pressed.has(_combine_device(button, p_device))) {
  645. return;
  646. }
  647. _button_event(p_device, button, true);
  648. return;
  649. }
  650. if (map.index == JOY_DPAD_LEFT || map.index == JOY_DPAD_RIGHT) {
  651. bool pressed = p_value.value != 0.0f;
  652. int button = p_value.value < 0 ? JOY_DPAD_LEFT : JOY_DPAD_RIGHT;
  653. if (!pressed) {
  654. if (joy_buttons_pressed.has(_combine_device(JOY_DPAD_LEFT, p_device))) {
  655. _button_event(p_device, JOY_DPAD_LEFT, false);
  656. }
  657. if (joy_buttons_pressed.has(_combine_device(JOY_DPAD_RIGHT, p_device))) {
  658. _button_event(p_device, JOY_DPAD_RIGHT, false);
  659. }
  660. }
  661. if (pressed == joy_buttons_pressed.has(_combine_device(button, p_device))) {
  662. return;
  663. }
  664. _button_event(p_device, button, true);
  665. return;
  666. }
  667. float deadzone = p_value.min == 0 ? 0.5f : 0.0f;
  668. bool pressed = p_value.value > deadzone ? true : false;
  669. if (pressed == joy_buttons_pressed.has(_combine_device(map.index, p_device))) {
  670. // button already pressed or released, this is an axis bounce value
  671. return;
  672. };
  673. _button_event(p_device, map.index, pressed);
  674. return;
  675. };
  676. if (map.type == TYPE_AXIS) {
  677. _axis_event(p_device, map.index, val);
  678. return;
  679. };
  680. //printf("invalid mapping\n");
  681. return;
  682. };
  683. void InputDefault::joy_hat(int p_device, int p_val) {
  684. _THREAD_SAFE_METHOD_;
  685. const Joypad &joy = joy_names[p_device];
  686. const JoyEvent *map;
  687. if (joy.mapping == -1) {
  688. map = hat_map_default;
  689. } else {
  690. map = map_db[joy.mapping].hat;
  691. };
  692. int cur_val = joy_names[p_device].hat_current;
  693. if ((p_val & HAT_MASK_UP) != (cur_val & HAT_MASK_UP)) {
  694. _button_event(p_device, map[HAT_UP].index, p_val & HAT_MASK_UP);
  695. };
  696. if ((p_val & HAT_MASK_RIGHT) != (cur_val & HAT_MASK_RIGHT)) {
  697. _button_event(p_device, map[HAT_RIGHT].index, p_val & HAT_MASK_RIGHT);
  698. };
  699. if ((p_val & HAT_MASK_DOWN) != (cur_val & HAT_MASK_DOWN)) {
  700. _button_event(p_device, map[HAT_DOWN].index, p_val & HAT_MASK_DOWN);
  701. };
  702. if ((p_val & HAT_MASK_LEFT) != (cur_val & HAT_MASK_LEFT)) {
  703. _button_event(p_device, map[HAT_LEFT].index, p_val & HAT_MASK_LEFT);
  704. };
  705. joy_names[p_device].hat_current = p_val;
  706. };
  707. void InputDefault::_button_event(int p_device, int p_index, bool p_pressed) {
  708. Ref<InputEventJoypadButton> ievent;
  709. ievent.instance();
  710. ievent->set_device(p_device);
  711. ievent->set_button_index(p_index);
  712. ievent->set_pressed(p_pressed);
  713. parse_input_event(ievent);
  714. };
  715. void InputDefault::_axis_event(int p_device, int p_axis, float p_value) {
  716. Ref<InputEventJoypadMotion> ievent;
  717. ievent.instance();
  718. ievent->set_device(p_device);
  719. ievent->set_axis(p_axis);
  720. ievent->set_axis_value(p_value);
  721. parse_input_event(ievent);
  722. };
  723. InputDefault::JoyEvent InputDefault::_find_to_event(String p_to) {
  724. // string names of the SDL buttons in the same order as input_event.h godot buttons
  725. static const char *buttons[] = { "a", "b", "x", "y", "leftshoulder", "rightshoulder", "lefttrigger", "righttrigger", "leftstick", "rightstick", "back", "start", "dpup", "dpdown", "dpleft", "dpright", "guide", NULL };
  726. static const char *axis[] = { "leftx", "lefty", "rightx", "righty", NULL };
  727. JoyEvent ret;
  728. ret.type = -1;
  729. ret.index = 0;
  730. int i = 0;
  731. while (buttons[i]) {
  732. if (p_to == buttons[i]) {
  733. ret.type = TYPE_BUTTON;
  734. ret.index = i;
  735. ret.value = 0;
  736. return ret;
  737. };
  738. ++i;
  739. };
  740. i = 0;
  741. while (axis[i]) {
  742. if (p_to == axis[i]) {
  743. ret.type = TYPE_AXIS;
  744. ret.index = i;
  745. ret.value = 0;
  746. return ret;
  747. };
  748. ++i;
  749. };
  750. return ret;
  751. };
  752. void InputDefault::parse_mapping(String p_mapping) {
  753. _THREAD_SAFE_METHOD_;
  754. JoyDeviceMapping mapping;
  755. for (int i = 0; i < HAT_MAX; ++i)
  756. mapping.hat[i].index = 1024 + i;
  757. Vector<String> entry = p_mapping.split(",");
  758. if (entry.size() < 2) {
  759. return;
  760. }
  761. CharString uid;
  762. uid.resize(17);
  763. mapping.uid = entry[0];
  764. mapping.name = entry[1];
  765. int idx = 1;
  766. while (++idx < entry.size()) {
  767. if (entry[idx] == "")
  768. continue;
  769. String from = entry[idx].get_slice(":", 1).replace(" ", "");
  770. String to = entry[idx].get_slice(":", 0).replace(" ", "");
  771. JoyEvent to_event = _find_to_event(to);
  772. if (to_event.type == -1)
  773. continue;
  774. String etype = from.substr(0, 1);
  775. if (etype == "a") {
  776. int aid = from.substr(1, from.length() - 1).to_int();
  777. mapping.axis[aid] = to_event;
  778. } else if (etype == "b") {
  779. int bid = from.substr(1, from.length() - 1).to_int();
  780. mapping.buttons[bid] = to_event;
  781. } else if (etype == "h") {
  782. int hat_value = from.get_slice(".", 1).to_int();
  783. switch (hat_value) {
  784. case 1:
  785. mapping.hat[HAT_UP] = to_event;
  786. break;
  787. case 2:
  788. mapping.hat[HAT_RIGHT] = to_event;
  789. break;
  790. case 4:
  791. mapping.hat[HAT_DOWN] = to_event;
  792. break;
  793. case 8:
  794. mapping.hat[HAT_LEFT] = to_event;
  795. break;
  796. };
  797. };
  798. };
  799. map_db.push_back(mapping);
  800. //printf("added mapping with uuid %ls\n", mapping.uid.c_str());
  801. };
  802. void InputDefault::add_joy_mapping(String p_mapping, bool p_update_existing) {
  803. parse_mapping(p_mapping);
  804. if (p_update_existing) {
  805. Vector<String> entry = p_mapping.split(",");
  806. String uid = entry[0];
  807. for (int i = 0; i < joy_names.size(); i++) {
  808. if (uid == joy_names[i].uid) {
  809. joy_names[i].mapping = map_db.size() - 1;
  810. }
  811. }
  812. }
  813. }
  814. void InputDefault::remove_joy_mapping(String p_guid) {
  815. for (int i = map_db.size() - 1; i >= 0; i--) {
  816. if (p_guid == map_db[i].uid) {
  817. map_db.remove(i);
  818. }
  819. }
  820. for (int i = 0; i < joy_names.size(); i++) {
  821. if (joy_names[i].uid == p_guid) {
  822. joy_names[i].mapping = -1;
  823. }
  824. }
  825. }
  826. void InputDefault::set_fallback_mapping(String p_guid) {
  827. for (int i = 0; i < map_db.size(); i++) {
  828. if (map_db[i].uid == p_guid) {
  829. fallback_mapping = i;
  830. return;
  831. }
  832. }
  833. }
  834. //Defaults to simple implementation for platforms with a fixed gamepad layout, like consoles.
  835. bool InputDefault::is_joy_known(int p_device) {
  836. return OS::get_singleton()->is_joy_known(p_device);
  837. }
  838. String InputDefault::get_joy_guid(int p_device) const {
  839. return OS::get_singleton()->get_joy_guid(p_device);
  840. }
  841. //platforms that use the remapping system can override and call to these ones
  842. bool InputDefault::is_joy_mapped(int p_device) {
  843. int mapping = joy_names[p_device].mapping;
  844. return mapping != -1 ? (mapping != fallback_mapping) : false;
  845. }
  846. String InputDefault::get_joy_guid_remapped(int p_device) const {
  847. ERR_FAIL_COND_V(!joy_names.has(p_device), "");
  848. return joy_names[p_device].uid;
  849. }
  850. Array InputDefault::get_connected_joypads() {
  851. Array ret;
  852. Map<int, Joypad>::Element *elem = joy_names.front();
  853. while (elem) {
  854. if (elem->get().connected) {
  855. ret.push_back(elem->key());
  856. }
  857. elem = elem->next();
  858. }
  859. return ret;
  860. }
  861. static const char *_buttons[JOY_BUTTON_MAX] = {
  862. "Face Button Bottom",
  863. "Face Button Right",
  864. "Face Button Left",
  865. "Face Button Top",
  866. "L",
  867. "R",
  868. "L2",
  869. "R2",
  870. "L3",
  871. "R3",
  872. "Select",
  873. "Start",
  874. "DPAD Up",
  875. "DPAD Down",
  876. "DPAD Left",
  877. "DPAD Right"
  878. };
  879. static const char *_axes[JOY_AXIS_MAX] = {
  880. "Left Stick X",
  881. "Left Stick Y",
  882. "Right Stick X",
  883. "Right Stick Y",
  884. "",
  885. "",
  886. "L2",
  887. "R2",
  888. "",
  889. ""
  890. };
  891. String InputDefault::get_joy_button_string(int p_button) {
  892. ERR_FAIL_INDEX_V(p_button, JOY_BUTTON_MAX, "");
  893. return _buttons[p_button];
  894. }
  895. int InputDefault::get_joy_button_index_from_string(String p_button) {
  896. for (int i = 0; i < JOY_BUTTON_MAX; i++) {
  897. if (p_button == _buttons[i]) {
  898. return i;
  899. }
  900. }
  901. ERR_FAIL_V(-1);
  902. }
  903. int InputDefault::get_unused_joy_id() {
  904. for (int i = 0; i < JOYPADS_MAX; i++) {
  905. if (!joy_names.has(i) || !joy_names[i].connected) {
  906. return i;
  907. }
  908. }
  909. return -1;
  910. }
  911. String InputDefault::get_joy_axis_string(int p_axis) {
  912. ERR_FAIL_INDEX_V(p_axis, JOY_AXIS_MAX, "");
  913. return _axes[p_axis];
  914. }
  915. int InputDefault::get_joy_axis_index_from_string(String p_axis) {
  916. for (int i = 0; i < JOY_AXIS_MAX; i++) {
  917. if (p_axis == _axes[i]) {
  918. return i;
  919. }
  920. }
  921. ERR_FAIL_V(-1);
  922. }