input_event.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  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. case BUTTON_XBUTTON1:
  366. button_index_string = "BUTTON_XBUTTON1";
  367. break;
  368. case BUTTON_XBUTTON2:
  369. button_index_string = "BUTTON_XBUTTON2";
  370. break;
  371. default:
  372. button_index_string = itos(get_button_index());
  373. break;
  374. }
  375. 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");
  376. }
  377. void InputEventMouseButton::_bind_methods() {
  378. ClassDB::bind_method(D_METHOD("set_factor", "factor"), &InputEventMouseButton::set_factor);
  379. ClassDB::bind_method(D_METHOD("get_factor"), &InputEventMouseButton::get_factor);
  380. ClassDB::bind_method(D_METHOD("set_button_index", "button_index"), &InputEventMouseButton::set_button_index);
  381. ClassDB::bind_method(D_METHOD("get_button_index"), &InputEventMouseButton::get_button_index);
  382. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventMouseButton::set_pressed);
  383. // ClassDB::bind_method(D_METHOD("is_pressed"), &InputEventMouseButton::is_pressed);
  384. ClassDB::bind_method(D_METHOD("set_doubleclick", "doubleclick"), &InputEventMouseButton::set_doubleclick);
  385. ClassDB::bind_method(D_METHOD("is_doubleclick"), &InputEventMouseButton::is_doubleclick);
  386. ADD_PROPERTY(PropertyInfo(Variant::REAL, "factor"), "set_factor", "get_factor");
  387. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_index"), "set_button_index", "get_button_index");
  388. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  389. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "doubleclick"), "set_doubleclick", "is_doubleclick");
  390. }
  391. InputEventMouseButton::InputEventMouseButton() {
  392. factor = 1;
  393. button_index = 0;
  394. pressed = false;
  395. doubleclick = false;
  396. }
  397. ////////////////////////////////////////////
  398. void InputEventMouseMotion::set_relative(const Vector2 &p_relative) {
  399. relative = p_relative;
  400. }
  401. Vector2 InputEventMouseMotion::get_relative() const {
  402. return relative;
  403. }
  404. void InputEventMouseMotion::set_speed(const Vector2 &p_speed) {
  405. speed = p_speed;
  406. }
  407. Vector2 InputEventMouseMotion::get_speed() const {
  408. return speed;
  409. }
  410. Ref<InputEvent> InputEventMouseMotion::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  411. Vector2 g = p_xform.xform(get_global_position());
  412. Vector2 l = p_xform.xform(get_position() + p_local_ofs);
  413. Vector2 r = p_xform.basis_xform(get_relative());
  414. Vector2 s = p_xform.basis_xform(get_speed());
  415. Ref<InputEventMouseMotion> mm;
  416. mm.instance();
  417. mm->set_device(get_device());
  418. mm->set_modifiers_from_event(this);
  419. mm->set_position(l);
  420. mm->set_global_position(g);
  421. mm->set_button_mask(get_button_mask());
  422. mm->set_relative(r);
  423. mm->set_speed(s);
  424. return mm;
  425. }
  426. String InputEventMouseMotion::as_text() const {
  427. String button_mask_string = "";
  428. switch (get_button_mask()) {
  429. case BUTTON_MASK_LEFT:
  430. button_mask_string = "BUTTON_MASK_LEFT";
  431. break;
  432. case BUTTON_MASK_MIDDLE:
  433. button_mask_string = "BUTTON_MASK_MIDDLE";
  434. break;
  435. case BUTTON_MASK_RIGHT:
  436. button_mask_string = "BUTTON_MASK_RIGHT";
  437. break;
  438. case BUTTON_MASK_XBUTTON1:
  439. button_mask_string = "BUTTON_MASK_XBUTTON1";
  440. break;
  441. case BUTTON_MASK_XBUTTON2:
  442. button_mask_string = "BUTTON_MASK_XBUTTON2";
  443. break;
  444. default:
  445. button_mask_string = itos(get_button_mask());
  446. break;
  447. }
  448. return "InputEventMouseMotion : button_mask=" + button_mask_string + ", position=(" + String(get_position()) + "), relative=(" + String(get_relative()) + "), speed=(" + String(get_speed()) + ")";
  449. }
  450. void InputEventMouseMotion::_bind_methods() {
  451. ClassDB::bind_method(D_METHOD("set_relative", "relative"), &InputEventMouseMotion::set_relative);
  452. ClassDB::bind_method(D_METHOD("get_relative"), &InputEventMouseMotion::get_relative);
  453. ClassDB::bind_method(D_METHOD("set_speed", "speed"), &InputEventMouseMotion::set_speed);
  454. ClassDB::bind_method(D_METHOD("get_speed"), &InputEventMouseMotion::get_speed);
  455. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "relative"), "set_relative", "get_relative");
  456. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "speed"), "set_speed", "get_speed");
  457. }
  458. InputEventMouseMotion::InputEventMouseMotion() {
  459. }
  460. ////////////////////////////////////////
  461. void InputEventJoypadMotion::set_axis(int p_axis) {
  462. axis = p_axis;
  463. }
  464. int InputEventJoypadMotion::get_axis() const {
  465. return axis;
  466. }
  467. void InputEventJoypadMotion::set_axis_value(float p_value) {
  468. axis_value = p_value;
  469. }
  470. float InputEventJoypadMotion::get_axis_value() const {
  471. return axis_value;
  472. }
  473. bool InputEventJoypadMotion::is_pressed() const {
  474. return Math::abs(axis_value) >= 0.5f;
  475. }
  476. bool InputEventJoypadMotion::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const {
  477. Ref<InputEventJoypadMotion> jm = p_event;
  478. if (jm.is_null())
  479. return false;
  480. bool match = (axis == jm->axis); // Matches even if not in the same direction, but returns a "not pressed" event.
  481. if (match) {
  482. bool same_direction = (((axis_value < 0) == (jm->axis_value < 0)) || jm->axis_value == 0);
  483. bool pressed = same_direction ? Math::abs(jm->get_axis_value()) >= p_deadzone : false;
  484. if (p_pressed != NULL)
  485. *p_pressed = pressed;
  486. if (p_strength != NULL)
  487. *p_strength = pressed ? CLAMP(Math::inverse_lerp(p_deadzone, 1.0f, Math::abs(jm->get_axis_value())), 0.0f, 1.0f) : 0.0f;
  488. }
  489. return match;
  490. }
  491. String InputEventJoypadMotion::as_text() const {
  492. return "InputEventJoypadMotion : axis=" + itos(axis) + ", axis_value=" + String(Variant(axis_value));
  493. }
  494. void InputEventJoypadMotion::_bind_methods() {
  495. ClassDB::bind_method(D_METHOD("set_axis", "axis"), &InputEventJoypadMotion::set_axis);
  496. ClassDB::bind_method(D_METHOD("get_axis"), &InputEventJoypadMotion::get_axis);
  497. ClassDB::bind_method(D_METHOD("set_axis_value", "axis_value"), &InputEventJoypadMotion::set_axis_value);
  498. ClassDB::bind_method(D_METHOD("get_axis_value"), &InputEventJoypadMotion::get_axis_value);
  499. ADD_PROPERTY(PropertyInfo(Variant::INT, "axis"), "set_axis", "get_axis");
  500. ADD_PROPERTY(PropertyInfo(Variant::REAL, "axis_value"), "set_axis_value", "get_axis_value");
  501. }
  502. InputEventJoypadMotion::InputEventJoypadMotion() {
  503. axis = 0;
  504. axis_value = 0;
  505. }
  506. /////////////////////////////////
  507. void InputEventJoypadButton::set_button_index(int p_index) {
  508. button_index = p_index;
  509. }
  510. int InputEventJoypadButton::get_button_index() const {
  511. return button_index;
  512. }
  513. void InputEventJoypadButton::set_pressed(bool p_pressed) {
  514. pressed = p_pressed;
  515. }
  516. bool InputEventJoypadButton::is_pressed() const {
  517. return pressed;
  518. }
  519. void InputEventJoypadButton::set_pressure(float p_pressure) {
  520. pressure = p_pressure;
  521. }
  522. float InputEventJoypadButton::get_pressure() const {
  523. return pressure;
  524. }
  525. bool InputEventJoypadButton::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const {
  526. Ref<InputEventJoypadButton> jb = p_event;
  527. if (jb.is_null())
  528. return false;
  529. bool match = button_index == jb->button_index;
  530. if (match) {
  531. if (p_pressed != NULL)
  532. *p_pressed = jb->is_pressed();
  533. if (p_strength != NULL)
  534. *p_strength = (*p_pressed) ? 1.0f : 0.0f;
  535. }
  536. return match;
  537. }
  538. String InputEventJoypadButton::as_text() const {
  539. return "InputEventJoypadButton : button_index=" + itos(button_index) + ", pressed=" + (pressed ? "true" : "false") + ", pressure=" + String(Variant(pressure));
  540. }
  541. void InputEventJoypadButton::_bind_methods() {
  542. ClassDB::bind_method(D_METHOD("set_button_index", "button_index"), &InputEventJoypadButton::set_button_index);
  543. ClassDB::bind_method(D_METHOD("get_button_index"), &InputEventJoypadButton::get_button_index);
  544. ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventJoypadButton::set_pressure);
  545. ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventJoypadButton::get_pressure);
  546. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventJoypadButton::set_pressed);
  547. // ClassDB::bind_method(D_METHOD("is_pressed"), &InputEventJoypadButton::is_pressed);
  548. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_index"), "set_button_index", "get_button_index");
  549. ADD_PROPERTY(PropertyInfo(Variant::REAL, "pressure"), "set_pressure", "get_pressure");
  550. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  551. }
  552. InputEventJoypadButton::InputEventJoypadButton() {
  553. button_index = 0;
  554. pressure = 0;
  555. pressed = false;
  556. }
  557. //////////////////////////////////////////////
  558. void InputEventScreenTouch::set_index(int p_index) {
  559. index = p_index;
  560. }
  561. int InputEventScreenTouch::get_index() const {
  562. return index;
  563. }
  564. void InputEventScreenTouch::set_position(const Vector2 &p_pos) {
  565. pos = p_pos;
  566. }
  567. Vector2 InputEventScreenTouch::get_position() const {
  568. return pos;
  569. }
  570. void InputEventScreenTouch::set_pressed(bool p_pressed) {
  571. pressed = p_pressed;
  572. }
  573. bool InputEventScreenTouch::is_pressed() const {
  574. return pressed;
  575. }
  576. Ref<InputEvent> InputEventScreenTouch::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  577. Ref<InputEventScreenTouch> st;
  578. st.instance();
  579. st->set_device(get_device());
  580. st->set_index(index);
  581. st->set_position(p_xform.xform(pos + p_local_ofs));
  582. st->set_pressed(pressed);
  583. return st;
  584. }
  585. String InputEventScreenTouch::as_text() const {
  586. return "InputEventScreenTouch : index=" + itos(index) + ", pressed=" + (pressed ? "true" : "false") + ", position=(" + String(get_position()) + ")";
  587. }
  588. void InputEventScreenTouch::_bind_methods() {
  589. ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenTouch::set_index);
  590. ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenTouch::get_index);
  591. ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventScreenTouch::set_position);
  592. ClassDB::bind_method(D_METHOD("get_position"), &InputEventScreenTouch::get_position);
  593. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventScreenTouch::set_pressed);
  594. //ClassDB::bind_method(D_METHOD("is_pressed"),&InputEventScreenTouch::is_pressed);
  595. ADD_PROPERTY(PropertyInfo(Variant::INT, "index"), "set_index", "get_index");
  596. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
  597. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  598. }
  599. InputEventScreenTouch::InputEventScreenTouch() {
  600. index = 0;
  601. pressed = false;
  602. }
  603. /////////////////////////////
  604. void InputEventScreenDrag::set_index(int p_index) {
  605. index = p_index;
  606. }
  607. int InputEventScreenDrag::get_index() const {
  608. return index;
  609. }
  610. void InputEventScreenDrag::set_position(const Vector2 &p_pos) {
  611. pos = p_pos;
  612. }
  613. Vector2 InputEventScreenDrag::get_position() const {
  614. return pos;
  615. }
  616. void InputEventScreenDrag::set_relative(const Vector2 &p_relative) {
  617. relative = p_relative;
  618. }
  619. Vector2 InputEventScreenDrag::get_relative() const {
  620. return relative;
  621. }
  622. void InputEventScreenDrag::set_speed(const Vector2 &p_speed) {
  623. speed = p_speed;
  624. }
  625. Vector2 InputEventScreenDrag::get_speed() const {
  626. return speed;
  627. }
  628. Ref<InputEvent> InputEventScreenDrag::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  629. Ref<InputEventScreenDrag> sd;
  630. sd.instance();
  631. sd->set_device(get_device());
  632. sd->set_index(index);
  633. sd->set_position(p_xform.xform(pos + p_local_ofs));
  634. sd->set_relative(p_xform.basis_xform(relative));
  635. sd->set_speed(p_xform.basis_xform(speed));
  636. return sd;
  637. }
  638. String InputEventScreenDrag::as_text() const {
  639. return "InputEventScreenDrag : index=" + itos(index) + ", position=(" + String(get_position()) + "), relative=(" + String(get_relative()) + "), speed=(" + String(get_speed()) + ")";
  640. }
  641. void InputEventScreenDrag::_bind_methods() {
  642. ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenDrag::set_index);
  643. ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenDrag::get_index);
  644. ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventScreenDrag::set_position);
  645. ClassDB::bind_method(D_METHOD("get_position"), &InputEventScreenDrag::get_position);
  646. ClassDB::bind_method(D_METHOD("set_relative", "relative"), &InputEventScreenDrag::set_relative);
  647. ClassDB::bind_method(D_METHOD("get_relative"), &InputEventScreenDrag::get_relative);
  648. ClassDB::bind_method(D_METHOD("set_speed", "speed"), &InputEventScreenDrag::set_speed);
  649. ClassDB::bind_method(D_METHOD("get_speed"), &InputEventScreenDrag::get_speed);
  650. ADD_PROPERTY(PropertyInfo(Variant::INT, "index"), "set_index", "get_index");
  651. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
  652. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "relative"), "set_relative", "get_relative");
  653. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "speed"), "set_speed", "get_speed");
  654. }
  655. InputEventScreenDrag::InputEventScreenDrag() {
  656. index = 0;
  657. }
  658. /////////////////////////////
  659. void InputEventAction::set_action(const StringName &p_action) {
  660. action = p_action;
  661. }
  662. StringName InputEventAction::get_action() const {
  663. return action;
  664. }
  665. void InputEventAction::set_pressed(bool p_pressed) {
  666. pressed = p_pressed;
  667. }
  668. bool InputEventAction::is_pressed() const {
  669. return pressed;
  670. }
  671. bool InputEventAction::is_action(const StringName &p_action) const {
  672. return action == p_action;
  673. }
  674. String InputEventAction::as_text() const {
  675. return "InputEventAction : action=" + action + ", pressed=(" + (pressed ? "true" : "false");
  676. }
  677. void InputEventAction::_bind_methods() {
  678. ClassDB::bind_method(D_METHOD("set_action", "action"), &InputEventAction::set_action);
  679. ClassDB::bind_method(D_METHOD("get_action"), &InputEventAction::get_action);
  680. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventAction::set_pressed);
  681. //ClassDB::bind_method(D_METHOD("is_pressed"), &InputEventAction::is_pressed);
  682. // ClassDB::bind_method(D_METHOD("is_action", "name"), &InputEventAction::is_action);
  683. ADD_PROPERTY(PropertyInfo(Variant::STRING, "action"), "set_action", "get_action");
  684. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  685. }
  686. InputEventAction::InputEventAction() {
  687. pressed = false;
  688. }
  689. /////////////////////////////
  690. void InputEventGesture::set_position(const Vector2 &p_pos) {
  691. pos = p_pos;
  692. }
  693. void InputEventGesture::_bind_methods() {
  694. ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventGesture::set_position);
  695. ClassDB::bind_method(D_METHOD("get_position"), &InputEventGesture::get_position);
  696. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
  697. }
  698. Vector2 InputEventGesture::get_position() const {
  699. return pos;
  700. }
  701. /////////////////////////////
  702. void InputEventMagnifyGesture::set_factor(real_t p_factor) {
  703. factor = p_factor;
  704. }
  705. real_t InputEventMagnifyGesture::get_factor() const {
  706. return factor;
  707. }
  708. Ref<InputEvent> InputEventMagnifyGesture::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  709. Ref<InputEventMagnifyGesture> ev;
  710. ev.instance();
  711. ev->set_device(get_device());
  712. ev->set_modifiers_from_event(this);
  713. ev->set_position(p_xform.xform(get_position() + p_local_ofs));
  714. ev->set_factor(get_factor());
  715. return ev;
  716. }
  717. String InputEventMagnifyGesture::as_text() const {
  718. return "InputEventMagnifyGesture : factor=" + rtos(get_factor()) + ", position=(" + String(get_position()) + ")";
  719. }
  720. void InputEventMagnifyGesture::_bind_methods() {
  721. ClassDB::bind_method(D_METHOD("set_factor", "factor"), &InputEventMagnifyGesture::set_factor);
  722. ClassDB::bind_method(D_METHOD("get_factor"), &InputEventMagnifyGesture::get_factor);
  723. ADD_PROPERTY(PropertyInfo(Variant::REAL, "factor"), "set_factor", "get_factor");
  724. }
  725. InputEventMagnifyGesture::InputEventMagnifyGesture() {
  726. factor = 1.0;
  727. }
  728. /////////////////////////////
  729. void InputEventPanGesture::set_delta(const Vector2 &p_delta) {
  730. delta = p_delta;
  731. }
  732. Vector2 InputEventPanGesture::get_delta() const {
  733. return delta;
  734. }
  735. Ref<InputEvent> InputEventPanGesture::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  736. Ref<InputEventPanGesture> ev;
  737. ev.instance();
  738. ev->set_device(get_device());
  739. ev->set_modifiers_from_event(this);
  740. ev->set_position(p_xform.xform(get_position() + p_local_ofs));
  741. ev->set_delta(get_delta());
  742. return ev;
  743. }
  744. String InputEventPanGesture::as_text() const {
  745. return "InputEventPanGesture : delta=(" + String(get_delta()) + "), position=(" + String(get_position()) + ")";
  746. }
  747. void InputEventPanGesture::_bind_methods() {
  748. ClassDB::bind_method(D_METHOD("set_delta", "delta"), &InputEventPanGesture::set_delta);
  749. ClassDB::bind_method(D_METHOD("get_delta"), &InputEventPanGesture::get_delta);
  750. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "delta"), "set_delta", "get_delta");
  751. }
  752. InputEventPanGesture::InputEventPanGesture() {
  753. delta = Vector2(0, 0);
  754. }
  755. /////////////////////////////
  756. void InputEventMIDI::set_channel(const int p_channel) {
  757. channel = p_channel;
  758. }
  759. int InputEventMIDI::get_channel() const {
  760. return channel;
  761. }
  762. void InputEventMIDI::set_message(const int p_message) {
  763. message = p_message;
  764. }
  765. int InputEventMIDI::get_message() const {
  766. return message;
  767. }
  768. void InputEventMIDI::set_pitch(const int p_pitch) {
  769. pitch = p_pitch;
  770. }
  771. int InputEventMIDI::get_pitch() const {
  772. return pitch;
  773. }
  774. void InputEventMIDI::set_velocity(const int p_velocity) {
  775. velocity = p_velocity;
  776. }
  777. int InputEventMIDI::get_velocity() const {
  778. return velocity;
  779. }
  780. void InputEventMIDI::set_instrument(const int p_instrument) {
  781. instrument = p_instrument;
  782. }
  783. int InputEventMIDI::get_instrument() const {
  784. return instrument;
  785. }
  786. void InputEventMIDI::set_pressure(const int p_pressure) {
  787. pressure = p_pressure;
  788. }
  789. int InputEventMIDI::get_pressure() const {
  790. return pressure;
  791. }
  792. void InputEventMIDI::set_controller_number(const int p_controller_number) {
  793. controller_number = p_controller_number;
  794. }
  795. int InputEventMIDI::get_controller_number() const {
  796. return controller_number;
  797. }
  798. void InputEventMIDI::set_controller_value(const int p_controller_value) {
  799. controller_value = p_controller_value;
  800. }
  801. int InputEventMIDI::get_controller_value() const {
  802. return controller_value;
  803. }
  804. String InputEventMIDI::as_text() const {
  805. return "InputEventMIDI : channel=(" + itos(get_channel()) + "), message=(" + itos(get_message()) + ")";
  806. }
  807. void InputEventMIDI::_bind_methods() {
  808. ClassDB::bind_method(D_METHOD("set_channel", "channel"), &InputEventMIDI::set_channel);
  809. ClassDB::bind_method(D_METHOD("get_channel"), &InputEventMIDI::get_channel);
  810. ClassDB::bind_method(D_METHOD("set_message", "message"), &InputEventMIDI::set_message);
  811. ClassDB::bind_method(D_METHOD("get_message"), &InputEventMIDI::get_message);
  812. ClassDB::bind_method(D_METHOD("set_pitch", "pitch"), &InputEventMIDI::set_pitch);
  813. ClassDB::bind_method(D_METHOD("get_pitch"), &InputEventMIDI::get_pitch);
  814. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &InputEventMIDI::set_velocity);
  815. ClassDB::bind_method(D_METHOD("get_velocity"), &InputEventMIDI::get_velocity);
  816. ClassDB::bind_method(D_METHOD("set_instrument", "instrument"), &InputEventMIDI::set_instrument);
  817. ClassDB::bind_method(D_METHOD("get_instrument"), &InputEventMIDI::get_instrument);
  818. ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventMIDI::set_pressure);
  819. ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventMIDI::get_pressure);
  820. ClassDB::bind_method(D_METHOD("set_controller_number", "controller_number"), &InputEventMIDI::set_controller_number);
  821. ClassDB::bind_method(D_METHOD("get_controller_number"), &InputEventMIDI::get_controller_number);
  822. ClassDB::bind_method(D_METHOD("set_controller_value", "controller_value"), &InputEventMIDI::set_controller_value);
  823. ClassDB::bind_method(D_METHOD("get_controller_value"), &InputEventMIDI::get_controller_value);
  824. ADD_PROPERTY(PropertyInfo(Variant::INT, "channel"), "set_channel", "get_channel");
  825. ADD_PROPERTY(PropertyInfo(Variant::INT, "message"), "set_message", "get_message");
  826. ADD_PROPERTY(PropertyInfo(Variant::INT, "pitch"), "set_pitch", "get_pitch");
  827. ADD_PROPERTY(PropertyInfo(Variant::INT, "velocity"), "set_velocity", "get_velocity");
  828. ADD_PROPERTY(PropertyInfo(Variant::INT, "instrument"), "set_instrument", "get_instrument");
  829. ADD_PROPERTY(PropertyInfo(Variant::INT, "pressure"), "set_pressure", "get_pressure");
  830. ADD_PROPERTY(PropertyInfo(Variant::INT, "controller_number"), "set_controller_number", "get_controller_number");
  831. ADD_PROPERTY(PropertyInfo(Variant::INT, "controller_value"), "set_controller_value", "get_controller_value");
  832. }
  833. InputEventMIDI::InputEventMIDI() {
  834. channel = 0;
  835. message = 0;
  836. pitch = 0;
  837. velocity = 0;
  838. instrument = 0;
  839. pressure = 0;
  840. controller_number = 0;
  841. controller_value = 0;
  842. }