input_event.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. /*************************************************************************/
  2. /* input_event.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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_event.h"
  31. #include "input_map.h"
  32. #include "os/keyboard.h"
  33. void InputEvent::set_device(int p_device) {
  34. device = p_device;
  35. }
  36. int InputEvent::get_device() const {
  37. return device;
  38. }
  39. bool InputEvent::is_action(const StringName &p_action) const {
  40. return InputMap::get_singleton()->event_is_action(Ref<InputEvent>((InputEvent *)this), p_action);
  41. }
  42. bool InputEvent::is_action_pressed(const StringName &p_action) const {
  43. bool pressed;
  44. bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>((InputEvent *)this), p_action, &pressed);
  45. return valid && pressed && !is_echo();
  46. }
  47. bool InputEvent::is_action_released(const StringName &p_action) const {
  48. bool pressed;
  49. bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>((InputEvent *)this), p_action, &pressed);
  50. return valid && !pressed;
  51. }
  52. float InputEvent::get_action_strength(const StringName &p_action) const {
  53. bool pressed;
  54. float strength;
  55. bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>((InputEvent *)this), p_action, &pressed, &strength);
  56. return valid ? strength : 0.0f;
  57. }
  58. bool InputEvent::is_pressed() const {
  59. return false;
  60. }
  61. bool InputEvent::is_echo() const {
  62. return false;
  63. }
  64. Ref<InputEvent> InputEvent::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  65. return Ref<InputEvent>((InputEvent *)this);
  66. }
  67. String InputEvent::as_text() const {
  68. return String();
  69. }
  70. bool InputEvent::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const {
  71. return false;
  72. }
  73. bool InputEvent::shortcut_match(const Ref<InputEvent> &p_event) const {
  74. return false;
  75. }
  76. bool InputEvent::is_action_type() const {
  77. return false;
  78. }
  79. void InputEvent::_bind_methods() {
  80. ClassDB::bind_method(D_METHOD("set_device", "device"), &InputEvent::set_device);
  81. ClassDB::bind_method(D_METHOD("get_device"), &InputEvent::get_device);
  82. ClassDB::bind_method(D_METHOD("is_action", "action"), &InputEvent::is_action);
  83. ClassDB::bind_method(D_METHOD("is_action_pressed", "action"), &InputEvent::is_action_pressed);
  84. ClassDB::bind_method(D_METHOD("is_action_released", "action"), &InputEvent::is_action_released);
  85. ClassDB::bind_method(D_METHOD("get_action_strength", "action"), &InputEvent::get_action_strength);
  86. ClassDB::bind_method(D_METHOD("is_pressed"), &InputEvent::is_pressed);
  87. ClassDB::bind_method(D_METHOD("is_echo"), &InputEvent::is_echo);
  88. ClassDB::bind_method(D_METHOD("as_text"), &InputEvent::as_text);
  89. ClassDB::bind_method(D_METHOD("shortcut_match", "event"), &InputEvent::shortcut_match);
  90. ClassDB::bind_method(D_METHOD("is_action_type"), &InputEvent::is_action_type);
  91. ClassDB::bind_method(D_METHOD("xformed_by", "xform", "local_ofs"), &InputEvent::xformed_by, DEFVAL(Vector2()));
  92. ADD_PROPERTY(PropertyInfo(Variant::INT, "device"), "set_device", "get_device");
  93. }
  94. InputEvent::InputEvent() {
  95. device = 0;
  96. }
  97. //////////////////
  98. void InputEventWithModifiers::set_shift(bool p_enabled) {
  99. shift = p_enabled;
  100. }
  101. bool InputEventWithModifiers::get_shift() const {
  102. return shift;
  103. }
  104. void InputEventWithModifiers::set_alt(bool p_enabled) {
  105. alt = p_enabled;
  106. }
  107. bool InputEventWithModifiers::get_alt() const {
  108. return alt;
  109. }
  110. void InputEventWithModifiers::set_control(bool p_enabled) {
  111. control = p_enabled;
  112. }
  113. bool InputEventWithModifiers::get_control() const {
  114. return control;
  115. }
  116. void InputEventWithModifiers::set_metakey(bool p_enabled) {
  117. meta = p_enabled;
  118. }
  119. bool InputEventWithModifiers::get_metakey() const {
  120. return meta;
  121. }
  122. void InputEventWithModifiers::set_command(bool p_enabled) {
  123. command = p_enabled;
  124. }
  125. bool InputEventWithModifiers::get_command() const {
  126. return command;
  127. }
  128. void InputEventWithModifiers::set_modifiers_from_event(const InputEventWithModifiers *event) {
  129. set_alt(event->get_alt());
  130. set_shift(event->get_shift());
  131. set_control(event->get_control());
  132. set_metakey(event->get_metakey());
  133. }
  134. void InputEventWithModifiers::_bind_methods() {
  135. ClassDB::bind_method(D_METHOD("set_alt", "enable"), &InputEventWithModifiers::set_alt);
  136. ClassDB::bind_method(D_METHOD("get_alt"), &InputEventWithModifiers::get_alt);
  137. ClassDB::bind_method(D_METHOD("set_shift", "enable"), &InputEventWithModifiers::set_shift);
  138. ClassDB::bind_method(D_METHOD("get_shift"), &InputEventWithModifiers::get_shift);
  139. ClassDB::bind_method(D_METHOD("set_control", "enable"), &InputEventWithModifiers::set_control);
  140. ClassDB::bind_method(D_METHOD("get_control"), &InputEventWithModifiers::get_control);
  141. ClassDB::bind_method(D_METHOD("set_metakey", "enable"), &InputEventWithModifiers::set_metakey);
  142. ClassDB::bind_method(D_METHOD("get_metakey"), &InputEventWithModifiers::get_metakey);
  143. ClassDB::bind_method(D_METHOD("set_command", "enable"), &InputEventWithModifiers::set_command);
  144. ClassDB::bind_method(D_METHOD("get_command"), &InputEventWithModifiers::get_command);
  145. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "alt"), "set_alt", "get_alt");
  146. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shift"), "set_shift", "get_shift");
  147. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "control"), "set_control", "get_control");
  148. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meta"), "set_metakey", "get_metakey");
  149. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "command"), "set_command", "get_command");
  150. }
  151. InputEventWithModifiers::InputEventWithModifiers() {
  152. alt = false;
  153. shift = false;
  154. control = false;
  155. meta = false;
  156. }
  157. //////////////////////////////////
  158. void InputEventKey::set_pressed(bool p_pressed) {
  159. pressed = p_pressed;
  160. }
  161. bool InputEventKey::is_pressed() const {
  162. return pressed;
  163. }
  164. void InputEventKey::set_scancode(uint32_t p_scancode) {
  165. scancode = p_scancode;
  166. }
  167. uint32_t InputEventKey::get_scancode() const {
  168. return scancode;
  169. }
  170. void InputEventKey::set_unicode(uint32_t p_unicode) {
  171. unicode = p_unicode;
  172. }
  173. uint32_t InputEventKey::get_unicode() const {
  174. return unicode;
  175. }
  176. void InputEventKey::set_echo(bool p_enable) {
  177. echo = p_enable;
  178. }
  179. bool InputEventKey::is_echo() const {
  180. return echo;
  181. }
  182. uint32_t InputEventKey::get_scancode_with_modifiers() const {
  183. uint32_t sc = scancode;
  184. if (get_control())
  185. sc |= KEY_MASK_CTRL;
  186. if (get_alt())
  187. sc |= KEY_MASK_ALT;
  188. if (get_shift())
  189. sc |= KEY_MASK_SHIFT;
  190. if (get_metakey())
  191. sc |= KEY_MASK_META;
  192. return sc;
  193. }
  194. String InputEventKey::as_text() const {
  195. String kc = keycode_get_string(scancode);
  196. if (kc == String())
  197. return kc;
  198. if (get_metakey()) {
  199. kc = find_keycode_name(KEY_META) + ("+" + kc);
  200. }
  201. if (get_alt()) {
  202. kc = find_keycode_name(KEY_ALT) + ("+" + kc);
  203. }
  204. if (get_shift()) {
  205. kc = find_keycode_name(KEY_SHIFT) + ("+" + kc);
  206. }
  207. if (get_control()) {
  208. kc = find_keycode_name(KEY_CONTROL) + ("+" + kc);
  209. }
  210. return kc;
  211. }
  212. bool InputEventKey::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const {
  213. Ref<InputEventKey> key = p_event;
  214. if (key.is_null())
  215. return false;
  216. uint32_t code = get_scancode_with_modifiers();
  217. uint32_t event_code = key->get_scancode_with_modifiers();
  218. bool match = get_scancode() == key->get_scancode() && (!key->is_pressed() || (code & event_code) == code);
  219. if (match) {
  220. if (p_pressed != NULL)
  221. *p_pressed = key->is_pressed();
  222. if (p_strength != NULL)
  223. *p_strength = (*p_pressed) ? 1.0f : 0.0f;
  224. }
  225. return match;
  226. }
  227. bool InputEventKey::shortcut_match(const Ref<InputEvent> &p_event) const {
  228. Ref<InputEventKey> key = p_event;
  229. if (key.is_null())
  230. return false;
  231. uint32_t code = get_scancode_with_modifiers();
  232. uint32_t event_code = key->get_scancode_with_modifiers();
  233. return code == event_code;
  234. }
  235. void InputEventKey::_bind_methods() {
  236. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventKey::set_pressed);
  237. ClassDB::bind_method(D_METHOD("set_scancode", "scancode"), &InputEventKey::set_scancode);
  238. ClassDB::bind_method(D_METHOD("get_scancode"), &InputEventKey::get_scancode);
  239. ClassDB::bind_method(D_METHOD("set_unicode", "unicode"), &InputEventKey::set_unicode);
  240. ClassDB::bind_method(D_METHOD("get_unicode"), &InputEventKey::get_unicode);
  241. ClassDB::bind_method(D_METHOD("set_echo", "echo"), &InputEventKey::set_echo);
  242. ClassDB::bind_method(D_METHOD("get_scancode_with_modifiers"), &InputEventKey::get_scancode_with_modifiers);
  243. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  244. ADD_PROPERTY(PropertyInfo(Variant::INT, "scancode"), "set_scancode", "get_scancode");
  245. ADD_PROPERTY(PropertyInfo(Variant::INT, "unicode"), "set_unicode", "get_unicode");
  246. ADD_PROPERTY(PropertyInfo(Variant::INT, "echo"), "set_echo", "is_echo");
  247. }
  248. InputEventKey::InputEventKey() {
  249. pressed = false;
  250. scancode = 0;
  251. unicode = 0; ///unicode
  252. echo = false;
  253. }
  254. ////////////////////////////////////////
  255. void InputEventMouse::set_button_mask(int p_mask) {
  256. button_mask = p_mask;
  257. }
  258. int InputEventMouse::get_button_mask() const {
  259. return button_mask;
  260. }
  261. void InputEventMouse::set_position(const Vector2 &p_pos) {
  262. pos = p_pos;
  263. }
  264. Vector2 InputEventMouse::get_position() const {
  265. return pos;
  266. }
  267. void InputEventMouse::set_global_position(const Vector2 &p_global_pos) {
  268. global_pos = p_global_pos;
  269. }
  270. Vector2 InputEventMouse::get_global_position() const {
  271. return global_pos;
  272. }
  273. void InputEventMouse::_bind_methods() {
  274. ClassDB::bind_method(D_METHOD("set_button_mask", "button_mask"), &InputEventMouse::set_button_mask);
  275. ClassDB::bind_method(D_METHOD("get_button_mask"), &InputEventMouse::get_button_mask);
  276. ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventMouse::set_position);
  277. ClassDB::bind_method(D_METHOD("get_position"), &InputEventMouse::get_position);
  278. ClassDB::bind_method(D_METHOD("set_global_position", "global_position"), &InputEventMouse::set_global_position);
  279. ClassDB::bind_method(D_METHOD("get_global_position"), &InputEventMouse::get_global_position);
  280. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask"), "set_button_mask", "get_button_mask");
  281. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
  282. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_position"), "set_global_position", "get_global_position");
  283. }
  284. InputEventMouse::InputEventMouse() {
  285. button_mask = 0;
  286. }
  287. ///////////////////////////////////////
  288. void InputEventMouseButton::set_factor(float p_factor) {
  289. factor = p_factor;
  290. }
  291. float InputEventMouseButton::get_factor() {
  292. return factor;
  293. }
  294. void InputEventMouseButton::set_button_index(int p_index) {
  295. button_index = p_index;
  296. }
  297. int InputEventMouseButton::get_button_index() const {
  298. return button_index;
  299. }
  300. void InputEventMouseButton::set_pressed(bool p_pressed) {
  301. pressed = p_pressed;
  302. }
  303. bool InputEventMouseButton::is_pressed() const {
  304. return pressed;
  305. }
  306. void InputEventMouseButton::set_doubleclick(bool p_doubleclick) {
  307. doubleclick = p_doubleclick;
  308. }
  309. bool InputEventMouseButton::is_doubleclick() const {
  310. return doubleclick;
  311. }
  312. Ref<InputEvent> InputEventMouseButton::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  313. Vector2 g = p_xform.xform(get_global_position());
  314. Vector2 l = p_xform.xform(get_position() + p_local_ofs);
  315. Ref<InputEventMouseButton> mb;
  316. mb.instance();
  317. mb->set_device(get_device());
  318. mb->set_modifiers_from_event(this);
  319. mb->set_position(l);
  320. mb->set_global_position(g);
  321. mb->set_button_mask(get_button_mask());
  322. mb->set_pressed(pressed);
  323. mb->set_doubleclick(doubleclick);
  324. mb->set_factor(factor);
  325. mb->set_button_index(button_index);
  326. return mb;
  327. }
  328. bool InputEventMouseButton::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const {
  329. Ref<InputEventMouseButton> mb = p_event;
  330. if (mb.is_null())
  331. return false;
  332. bool match = mb->button_index == button_index;
  333. if (match) {
  334. if (p_pressed != NULL)
  335. *p_pressed = mb->is_pressed();
  336. if (p_strength != NULL)
  337. *p_strength = (*p_pressed) ? 1.0f : 0.0f;
  338. }
  339. return match;
  340. }
  341. String InputEventMouseButton::as_text() const {
  342. String button_index_string = "";
  343. switch (get_button_index()) {
  344. case BUTTON_LEFT:
  345. button_index_string = "BUTTON_LEFT";
  346. break;
  347. case BUTTON_RIGHT:
  348. button_index_string = "BUTTON_RIGHT";
  349. break;
  350. case BUTTON_MIDDLE:
  351. button_index_string = "BUTTON_MIDDLE";
  352. break;
  353. case BUTTON_WHEEL_UP:
  354. button_index_string = "BUTTON_WHEEL_UP";
  355. break;
  356. case BUTTON_WHEEL_DOWN:
  357. button_index_string = "BUTTON_WHEEL_DOWN";
  358. break;
  359. case BUTTON_WHEEL_LEFT:
  360. button_index_string = "BUTTON_WHEEL_LEFT";
  361. break;
  362. case BUTTON_WHEEL_RIGHT:
  363. button_index_string = "BUTTON_WHEEL_RIGHT";
  364. break;
  365. default:
  366. button_index_string = itos(get_button_index());
  367. break;
  368. }
  369. return "InputEventMouseButton : button_index=" + button_index_string + ", pressed=" + (pressed ? "true" : "false") + ", position=(" + String(get_position()) + "), button_mask=" + itos(get_button_mask()) + ", doubleclick=" + (doubleclick ? "true" : "false");
  370. }
  371. void InputEventMouseButton::_bind_methods() {
  372. ClassDB::bind_method(D_METHOD("set_factor", "factor"), &InputEventMouseButton::set_factor);
  373. ClassDB::bind_method(D_METHOD("get_factor"), &InputEventMouseButton::get_factor);
  374. ClassDB::bind_method(D_METHOD("set_button_index", "button_index"), &InputEventMouseButton::set_button_index);
  375. ClassDB::bind_method(D_METHOD("get_button_index"), &InputEventMouseButton::get_button_index);
  376. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventMouseButton::set_pressed);
  377. // ClassDB::bind_method(D_METHOD("is_pressed"), &InputEventMouseButton::is_pressed);
  378. ClassDB::bind_method(D_METHOD("set_doubleclick", "doubleclick"), &InputEventMouseButton::set_doubleclick);
  379. ClassDB::bind_method(D_METHOD("is_doubleclick"), &InputEventMouseButton::is_doubleclick);
  380. ADD_PROPERTY(PropertyInfo(Variant::REAL, "factor"), "set_factor", "get_factor");
  381. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_index"), "set_button_index", "get_button_index");
  382. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  383. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "doubleclick"), "set_doubleclick", "is_doubleclick");
  384. }
  385. InputEventMouseButton::InputEventMouseButton() {
  386. factor = 1;
  387. button_index = 0;
  388. pressed = false;
  389. doubleclick = false;
  390. }
  391. ////////////////////////////////////////////
  392. void InputEventMouseMotion::set_relative(const Vector2 &p_relative) {
  393. relative = p_relative;
  394. }
  395. Vector2 InputEventMouseMotion::get_relative() const {
  396. return relative;
  397. }
  398. void InputEventMouseMotion::set_speed(const Vector2 &p_speed) {
  399. speed = p_speed;
  400. }
  401. Vector2 InputEventMouseMotion::get_speed() const {
  402. return speed;
  403. }
  404. Ref<InputEvent> InputEventMouseMotion::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  405. Vector2 g = p_xform.xform(get_global_position());
  406. Vector2 l = p_xform.xform(get_position() + p_local_ofs);
  407. Vector2 r = p_xform.basis_xform(get_relative());
  408. Vector2 s = p_xform.basis_xform(get_speed());
  409. Ref<InputEventMouseMotion> mm;
  410. mm.instance();
  411. mm->set_device(get_device());
  412. mm->set_modifiers_from_event(this);
  413. mm->set_position(l);
  414. mm->set_global_position(g);
  415. mm->set_button_mask(get_button_mask());
  416. mm->set_relative(r);
  417. mm->set_speed(s);
  418. return mm;
  419. }
  420. String InputEventMouseMotion::as_text() const {
  421. String button_mask_string = "";
  422. switch (get_button_mask()) {
  423. case BUTTON_MASK_LEFT:
  424. button_mask_string = "BUTTON_MASK_LEFT";
  425. break;
  426. case BUTTON_MASK_MIDDLE:
  427. button_mask_string = "BUTTON_MASK_MIDDLE";
  428. break;
  429. case BUTTON_MASK_RIGHT:
  430. button_mask_string = "BUTTON_MASK_RIGHT";
  431. break;
  432. default:
  433. button_mask_string = itos(get_button_mask());
  434. break;
  435. }
  436. return "InputEventMouseMotion : button_mask=" + button_mask_string + ", position=(" + String(get_position()) + "), relative=(" + String(get_relative()) + "), speed=(" + String(get_speed()) + ")";
  437. }
  438. void InputEventMouseMotion::_bind_methods() {
  439. ClassDB::bind_method(D_METHOD("set_relative", "relative"), &InputEventMouseMotion::set_relative);
  440. ClassDB::bind_method(D_METHOD("get_relative"), &InputEventMouseMotion::get_relative);
  441. ClassDB::bind_method(D_METHOD("set_speed", "speed"), &InputEventMouseMotion::set_speed);
  442. ClassDB::bind_method(D_METHOD("get_speed"), &InputEventMouseMotion::get_speed);
  443. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "relative"), "set_relative", "get_relative");
  444. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "speed"), "set_speed", "get_speed");
  445. }
  446. InputEventMouseMotion::InputEventMouseMotion() {
  447. }
  448. ////////////////////////////////////////
  449. void InputEventJoypadMotion::set_axis(int p_axis) {
  450. axis = p_axis;
  451. }
  452. int InputEventJoypadMotion::get_axis() const {
  453. return axis;
  454. }
  455. void InputEventJoypadMotion::set_axis_value(float p_value) {
  456. axis_value = p_value;
  457. }
  458. float InputEventJoypadMotion::get_axis_value() const {
  459. return axis_value;
  460. }
  461. bool InputEventJoypadMotion::is_pressed() const {
  462. return Math::abs(axis_value) >= 0.5f;
  463. }
  464. bool InputEventJoypadMotion::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const {
  465. Ref<InputEventJoypadMotion> jm = p_event;
  466. if (jm.is_null())
  467. return false;
  468. bool match = (axis == jm->axis); // Matches even if not in the same direction, but returns a "not pressed" event.
  469. if (match) {
  470. bool same_direction = (((axis_value < 0) == (jm->axis_value < 0)) || jm->axis_value == 0);
  471. bool pressed = same_direction ? Math::abs(jm->get_axis_value()) >= p_deadzone : false;
  472. if (p_pressed != NULL)
  473. *p_pressed = pressed;
  474. if (p_strength != NULL)
  475. *p_strength = pressed ? CLAMP(Math::inverse_lerp(p_deadzone, 1.0f, Math::abs(jm->get_axis_value())), 0.0f, 1.0f) : 0.0f;
  476. }
  477. return match;
  478. }
  479. String InputEventJoypadMotion::as_text() const {
  480. return "InputEventJoypadMotion : axis=" + itos(axis) + ", axis_value=" + String(Variant(axis_value));
  481. }
  482. void InputEventJoypadMotion::_bind_methods() {
  483. ClassDB::bind_method(D_METHOD("set_axis", "axis"), &InputEventJoypadMotion::set_axis);
  484. ClassDB::bind_method(D_METHOD("get_axis"), &InputEventJoypadMotion::get_axis);
  485. ClassDB::bind_method(D_METHOD("set_axis_value", "axis_value"), &InputEventJoypadMotion::set_axis_value);
  486. ClassDB::bind_method(D_METHOD("get_axis_value"), &InputEventJoypadMotion::get_axis_value);
  487. ADD_PROPERTY(PropertyInfo(Variant::INT, "axis"), "set_axis", "get_axis");
  488. ADD_PROPERTY(PropertyInfo(Variant::REAL, "axis_value"), "set_axis_value", "get_axis_value");
  489. }
  490. InputEventJoypadMotion::InputEventJoypadMotion() {
  491. axis = 0;
  492. axis_value = 0;
  493. }
  494. /////////////////////////////////
  495. void InputEventJoypadButton::set_button_index(int p_index) {
  496. button_index = p_index;
  497. }
  498. int InputEventJoypadButton::get_button_index() const {
  499. return button_index;
  500. }
  501. void InputEventJoypadButton::set_pressed(bool p_pressed) {
  502. pressed = p_pressed;
  503. }
  504. bool InputEventJoypadButton::is_pressed() const {
  505. return pressed;
  506. }
  507. void InputEventJoypadButton::set_pressure(float p_pressure) {
  508. pressure = p_pressure;
  509. }
  510. float InputEventJoypadButton::get_pressure() const {
  511. return pressure;
  512. }
  513. bool InputEventJoypadButton::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const {
  514. Ref<InputEventJoypadButton> jb = p_event;
  515. if (jb.is_null())
  516. return false;
  517. bool match = button_index == jb->button_index;
  518. if (match) {
  519. if (p_pressed != NULL)
  520. *p_pressed = jb->is_pressed();
  521. if (p_strength != NULL)
  522. *p_strength = (*p_pressed) ? 1.0f : 0.0f;
  523. }
  524. return match;
  525. }
  526. String InputEventJoypadButton::as_text() const {
  527. return "InputEventJoypadButton : button_index=" + itos(button_index) + ", pressed=" + (pressed ? "true" : "false") + ", pressure=" + String(Variant(pressure));
  528. }
  529. void InputEventJoypadButton::_bind_methods() {
  530. ClassDB::bind_method(D_METHOD("set_button_index", "button_index"), &InputEventJoypadButton::set_button_index);
  531. ClassDB::bind_method(D_METHOD("get_button_index"), &InputEventJoypadButton::get_button_index);
  532. ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventJoypadButton::set_pressure);
  533. ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventJoypadButton::get_pressure);
  534. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventJoypadButton::set_pressed);
  535. // ClassDB::bind_method(D_METHOD("is_pressed"), &InputEventJoypadButton::is_pressed);
  536. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_index"), "set_button_index", "get_button_index");
  537. ADD_PROPERTY(PropertyInfo(Variant::REAL, "pressure"), "set_pressure", "get_pressure");
  538. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  539. }
  540. InputEventJoypadButton::InputEventJoypadButton() {
  541. button_index = 0;
  542. pressure = 0;
  543. pressed = false;
  544. }
  545. //////////////////////////////////////////////
  546. void InputEventScreenTouch::set_index(int p_index) {
  547. index = p_index;
  548. }
  549. int InputEventScreenTouch::get_index() const {
  550. return index;
  551. }
  552. void InputEventScreenTouch::set_position(const Vector2 &p_pos) {
  553. pos = p_pos;
  554. }
  555. Vector2 InputEventScreenTouch::get_position() const {
  556. return pos;
  557. }
  558. void InputEventScreenTouch::set_pressed(bool p_pressed) {
  559. pressed = p_pressed;
  560. }
  561. bool InputEventScreenTouch::is_pressed() const {
  562. return pressed;
  563. }
  564. Ref<InputEvent> InputEventScreenTouch::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  565. Ref<InputEventScreenTouch> st;
  566. st.instance();
  567. st->set_device(get_device());
  568. st->set_index(index);
  569. st->set_position(p_xform.xform(pos + p_local_ofs));
  570. st->set_pressed(pressed);
  571. return st;
  572. }
  573. String InputEventScreenTouch::as_text() const {
  574. return "InputEventScreenTouch : index=" + itos(index) + ", pressed=" + (pressed ? "true" : "false") + ", position=(" + String(get_position()) + ")";
  575. }
  576. void InputEventScreenTouch::_bind_methods() {
  577. ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenTouch::set_index);
  578. ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenTouch::get_index);
  579. ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventScreenTouch::set_position);
  580. ClassDB::bind_method(D_METHOD("get_position"), &InputEventScreenTouch::get_position);
  581. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventScreenTouch::set_pressed);
  582. //ClassDB::bind_method(D_METHOD("is_pressed"),&InputEventScreenTouch::is_pressed);
  583. ADD_PROPERTY(PropertyInfo(Variant::INT, "index"), "set_index", "get_index");
  584. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
  585. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  586. }
  587. InputEventScreenTouch::InputEventScreenTouch() {
  588. index = 0;
  589. pressed = false;
  590. }
  591. /////////////////////////////
  592. void InputEventScreenDrag::set_index(int p_index) {
  593. index = p_index;
  594. }
  595. int InputEventScreenDrag::get_index() const {
  596. return index;
  597. }
  598. void InputEventScreenDrag::set_position(const Vector2 &p_pos) {
  599. pos = p_pos;
  600. }
  601. Vector2 InputEventScreenDrag::get_position() const {
  602. return pos;
  603. }
  604. void InputEventScreenDrag::set_relative(const Vector2 &p_relative) {
  605. relative = p_relative;
  606. }
  607. Vector2 InputEventScreenDrag::get_relative() const {
  608. return relative;
  609. }
  610. void InputEventScreenDrag::set_speed(const Vector2 &p_speed) {
  611. speed = p_speed;
  612. }
  613. Vector2 InputEventScreenDrag::get_speed() const {
  614. return speed;
  615. }
  616. Ref<InputEvent> InputEventScreenDrag::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  617. Ref<InputEventScreenDrag> sd;
  618. sd.instance();
  619. sd->set_device(get_device());
  620. sd->set_index(index);
  621. sd->set_position(p_xform.xform(pos + p_local_ofs));
  622. sd->set_relative(p_xform.basis_xform(relative));
  623. sd->set_speed(p_xform.basis_xform(speed));
  624. return sd;
  625. }
  626. String InputEventScreenDrag::as_text() const {
  627. return "InputEventScreenDrag : index=" + itos(index) + ", position=(" + String(get_position()) + "), relative=(" + String(get_relative()) + "), speed=(" + String(get_speed()) + ")";
  628. }
  629. void InputEventScreenDrag::_bind_methods() {
  630. ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenDrag::set_index);
  631. ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenDrag::get_index);
  632. ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventScreenDrag::set_position);
  633. ClassDB::bind_method(D_METHOD("get_position"), &InputEventScreenDrag::get_position);
  634. ClassDB::bind_method(D_METHOD("set_relative", "relative"), &InputEventScreenDrag::set_relative);
  635. ClassDB::bind_method(D_METHOD("get_relative"), &InputEventScreenDrag::get_relative);
  636. ClassDB::bind_method(D_METHOD("set_speed", "speed"), &InputEventScreenDrag::set_speed);
  637. ClassDB::bind_method(D_METHOD("get_speed"), &InputEventScreenDrag::get_speed);
  638. ADD_PROPERTY(PropertyInfo(Variant::INT, "index"), "set_index", "get_index");
  639. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
  640. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "relative"), "set_relative", "get_relative");
  641. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "speed"), "set_speed", "get_speed");
  642. }
  643. InputEventScreenDrag::InputEventScreenDrag() {
  644. index = 0;
  645. }
  646. /////////////////////////////
  647. void InputEventAction::set_action(const StringName &p_action) {
  648. action = p_action;
  649. }
  650. StringName InputEventAction::get_action() const {
  651. return action;
  652. }
  653. void InputEventAction::set_pressed(bool p_pressed) {
  654. pressed = p_pressed;
  655. }
  656. bool InputEventAction::is_pressed() const {
  657. return pressed;
  658. }
  659. bool InputEventAction::shortcut_match(const Ref<InputEvent> &p_event) const {
  660. Ref<InputEventKey> event = p_event;
  661. if (event.is_null())
  662. return false;
  663. return event->is_action(action);
  664. }
  665. bool InputEventAction::is_action(const StringName &p_action) const {
  666. return action == p_action;
  667. }
  668. String InputEventAction::as_text() const {
  669. return "InputEventAction : action=" + action + ", pressed=(" + (pressed ? "true" : "false");
  670. }
  671. void InputEventAction::_bind_methods() {
  672. ClassDB::bind_method(D_METHOD("set_action", "action"), &InputEventAction::set_action);
  673. ClassDB::bind_method(D_METHOD("get_action"), &InputEventAction::get_action);
  674. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventAction::set_pressed);
  675. //ClassDB::bind_method(D_METHOD("is_pressed"), &InputEventAction::is_pressed);
  676. // ClassDB::bind_method(D_METHOD("is_action", "name"), &InputEventAction::is_action);
  677. ADD_PROPERTY(PropertyInfo(Variant::STRING, "action"), "set_action", "get_action");
  678. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  679. }
  680. InputEventAction::InputEventAction() {
  681. pressed = false;
  682. }
  683. /////////////////////////////
  684. void InputEventGesture::set_position(const Vector2 &p_pos) {
  685. pos = p_pos;
  686. }
  687. void InputEventGesture::_bind_methods() {
  688. ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventGesture::set_position);
  689. ClassDB::bind_method(D_METHOD("get_position"), &InputEventGesture::get_position);
  690. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
  691. }
  692. Vector2 InputEventGesture::get_position() const {
  693. return pos;
  694. }
  695. /////////////////////////////
  696. void InputEventMagnifyGesture::set_factor(real_t p_factor) {
  697. factor = p_factor;
  698. }
  699. real_t InputEventMagnifyGesture::get_factor() const {
  700. return factor;
  701. }
  702. Ref<InputEvent> InputEventMagnifyGesture::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  703. Ref<InputEventMagnifyGesture> ev;
  704. ev.instance();
  705. ev->set_device(get_device());
  706. ev->set_modifiers_from_event(this);
  707. ev->set_position(p_xform.xform(get_position() + p_local_ofs));
  708. ev->set_factor(get_factor());
  709. return ev;
  710. }
  711. String InputEventMagnifyGesture::as_text() const {
  712. return "InputEventMagnifyGesture : factor=" + rtos(get_factor()) + ", position=(" + String(get_position()) + ")";
  713. }
  714. void InputEventMagnifyGesture::_bind_methods() {
  715. ClassDB::bind_method(D_METHOD("set_factor", "factor"), &InputEventMagnifyGesture::set_factor);
  716. ClassDB::bind_method(D_METHOD("get_factor"), &InputEventMagnifyGesture::get_factor);
  717. ADD_PROPERTY(PropertyInfo(Variant::REAL, "factor"), "set_factor", "get_factor");
  718. }
  719. InputEventMagnifyGesture::InputEventMagnifyGesture() {
  720. factor = 1.0;
  721. }
  722. /////////////////////////////
  723. void InputEventPanGesture::set_delta(const Vector2 &p_delta) {
  724. delta = p_delta;
  725. }
  726. Vector2 InputEventPanGesture::get_delta() const {
  727. return delta;
  728. }
  729. Ref<InputEvent> InputEventPanGesture::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  730. Ref<InputEventPanGesture> ev;
  731. ev.instance();
  732. ev->set_device(get_device());
  733. ev->set_modifiers_from_event(this);
  734. ev->set_position(p_xform.xform(get_position() + p_local_ofs));
  735. ev->set_delta(get_delta());
  736. return ev;
  737. }
  738. String InputEventPanGesture::as_text() const {
  739. return "InputEventPanGesture : delta=(" + String(get_delta()) + "), position=(" + String(get_position()) + ")";
  740. }
  741. void InputEventPanGesture::_bind_methods() {
  742. ClassDB::bind_method(D_METHOD("set_delta", "delta"), &InputEventPanGesture::set_delta);
  743. ClassDB::bind_method(D_METHOD("get_delta"), &InputEventPanGesture::get_delta);
  744. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "delta"), "set_delta", "get_delta");
  745. }
  746. InputEventPanGesture::InputEventPanGesture() {
  747. delta = Vector2(0, 0);
  748. }