input_event.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. /*************************************************************************/
  2. /* input_event.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "core/input_map.h"
  32. #include "core/os/keyboard.h"
  33. const int InputEvent::DEVICE_ID_TOUCH_MOUSE = -1;
  34. const int InputEvent::DEVICE_ID_INTERNAL = -2;
  35. void InputEvent::set_device(int p_device) {
  36. device = p_device;
  37. }
  38. int InputEvent::get_device() const {
  39. return device;
  40. }
  41. bool InputEvent::is_action(const StringName &p_action, bool p_exact_match) const {
  42. return InputMap::get_singleton()->event_is_action(Ref<InputEvent>((InputEvent *)this), p_action, p_exact_match);
  43. }
  44. bool InputEvent::is_action_pressed(const StringName &p_action, bool p_allow_echo, bool p_exact_match) const {
  45. bool pressed;
  46. bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>((InputEvent *)this), p_action, p_exact_match, &pressed, nullptr, nullptr);
  47. return valid && pressed && (p_allow_echo || !is_echo());
  48. }
  49. bool InputEvent::is_action_released(const StringName &p_action, bool p_exact_match) const {
  50. bool pressed;
  51. bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>((InputEvent *)this), p_action, p_exact_match, &pressed, nullptr, nullptr);
  52. return valid && !pressed;
  53. }
  54. float InputEvent::get_action_strength(const StringName &p_action, bool p_exact_match) const {
  55. float strength;
  56. bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>((InputEvent *)this), p_action, p_exact_match, nullptr, &strength, nullptr);
  57. return valid ? strength : 0.0f;
  58. }
  59. float InputEvent::get_action_raw_strength(const StringName &p_action, bool p_exact_match) const {
  60. float raw_strength;
  61. bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>((InputEvent *)this), p_action, p_exact_match, nullptr, nullptr, &raw_strength);
  62. return valid ? raw_strength : 0.0f;
  63. }
  64. bool InputEvent::is_pressed() const {
  65. return false;
  66. }
  67. bool InputEvent::is_echo() const {
  68. return false;
  69. }
  70. Ref<InputEvent> InputEvent::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  71. return Ref<InputEvent>((InputEvent *)this);
  72. }
  73. String InputEvent::as_text() const {
  74. return String();
  75. }
  76. bool InputEvent::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
  77. return false;
  78. }
  79. bool InputEvent::shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
  80. return false;
  81. }
  82. bool InputEvent::is_action_type() const {
  83. return false;
  84. }
  85. void InputEvent::_bind_methods() {
  86. ClassDB::bind_method(D_METHOD("set_device", "device"), &InputEvent::set_device);
  87. ClassDB::bind_method(D_METHOD("get_device"), &InputEvent::get_device);
  88. ClassDB::bind_method(D_METHOD("is_action", "action", "exact_match"), &InputEvent::is_action, DEFVAL(false));
  89. ClassDB::bind_method(D_METHOD("is_action_pressed", "action", "allow_echo", "exact_match"), &InputEvent::is_action_pressed, DEFVAL(false), DEFVAL(false));
  90. ClassDB::bind_method(D_METHOD("is_action_released", "action", "exact_match"), &InputEvent::is_action_released, DEFVAL(false));
  91. ClassDB::bind_method(D_METHOD("get_action_strength", "action", "exact_match"), &InputEvent::get_action_strength, DEFVAL(false));
  92. ClassDB::bind_method(D_METHOD("is_pressed"), &InputEvent::is_pressed);
  93. ClassDB::bind_method(D_METHOD("is_echo"), &InputEvent::is_echo);
  94. ClassDB::bind_method(D_METHOD("as_text"), &InputEvent::as_text);
  95. ClassDB::bind_method(D_METHOD("shortcut_match", "event", "exact_match"), &InputEvent::shortcut_match, DEFVAL(true));
  96. ClassDB::bind_method(D_METHOD("is_action_type"), &InputEvent::is_action_type);
  97. ClassDB::bind_method(D_METHOD("accumulate", "with_event"), &InputEvent::accumulate);
  98. ClassDB::bind_method(D_METHOD("xformed_by", "xform", "local_ofs"), &InputEvent::xformed_by, DEFVAL(Vector2()));
  99. ADD_PROPERTY(PropertyInfo(Variant::INT, "device"), "set_device", "get_device");
  100. }
  101. InputEvent::InputEvent() {
  102. device = 0;
  103. }
  104. //////////////////
  105. void InputEventWithModifiers::set_shift(bool p_enabled) {
  106. shift = p_enabled;
  107. }
  108. bool InputEventWithModifiers::get_shift() const {
  109. return shift;
  110. }
  111. void InputEventWithModifiers::set_alt(bool p_enabled) {
  112. alt = p_enabled;
  113. }
  114. bool InputEventWithModifiers::get_alt() const {
  115. return alt;
  116. }
  117. void InputEventWithModifiers::set_control(bool p_enabled) {
  118. control = p_enabled;
  119. }
  120. bool InputEventWithModifiers::get_control() const {
  121. return control;
  122. }
  123. void InputEventWithModifiers::set_metakey(bool p_enabled) {
  124. meta = p_enabled;
  125. }
  126. bool InputEventWithModifiers::get_metakey() const {
  127. return meta;
  128. }
  129. void InputEventWithModifiers::set_command(bool p_enabled) {
  130. command = p_enabled;
  131. }
  132. bool InputEventWithModifiers::get_command() const {
  133. return command;
  134. }
  135. void InputEventWithModifiers::set_modifiers_from_event(const InputEventWithModifiers *event) {
  136. set_alt(event->get_alt());
  137. set_shift(event->get_shift());
  138. set_control(event->get_control());
  139. set_metakey(event->get_metakey());
  140. }
  141. uint32_t InputEventWithModifiers::get_modifiers_mask() const {
  142. uint32_t mask = 0;
  143. if (get_control()) {
  144. mask |= KEY_MASK_CTRL;
  145. }
  146. if (get_shift()) {
  147. mask |= KEY_MASK_SHIFT;
  148. }
  149. if (get_alt()) {
  150. mask |= KEY_MASK_ALT;
  151. }
  152. if (get_metakey()) {
  153. mask |= KEY_MASK_META;
  154. }
  155. return mask;
  156. }
  157. void InputEventWithModifiers::_bind_methods() {
  158. ClassDB::bind_method(D_METHOD("set_alt", "enable"), &InputEventWithModifiers::set_alt);
  159. ClassDB::bind_method(D_METHOD("get_alt"), &InputEventWithModifiers::get_alt);
  160. ClassDB::bind_method(D_METHOD("set_shift", "enable"), &InputEventWithModifiers::set_shift);
  161. ClassDB::bind_method(D_METHOD("get_shift"), &InputEventWithModifiers::get_shift);
  162. ClassDB::bind_method(D_METHOD("set_control", "enable"), &InputEventWithModifiers::set_control);
  163. ClassDB::bind_method(D_METHOD("get_control"), &InputEventWithModifiers::get_control);
  164. ClassDB::bind_method(D_METHOD("set_metakey", "enable"), &InputEventWithModifiers::set_metakey);
  165. ClassDB::bind_method(D_METHOD("get_metakey"), &InputEventWithModifiers::get_metakey);
  166. ClassDB::bind_method(D_METHOD("set_command", "enable"), &InputEventWithModifiers::set_command);
  167. ClassDB::bind_method(D_METHOD("get_command"), &InputEventWithModifiers::get_command);
  168. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "alt"), "set_alt", "get_alt");
  169. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shift"), "set_shift", "get_shift");
  170. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "control"), "set_control", "get_control");
  171. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meta"), "set_metakey", "get_metakey");
  172. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "command"), "set_command", "get_command");
  173. }
  174. InputEventWithModifiers::InputEventWithModifiers() {
  175. alt = false;
  176. shift = false;
  177. control = false;
  178. meta = false;
  179. }
  180. //////////////////////////////////
  181. void InputEventKey::set_pressed(bool p_pressed) {
  182. pressed = p_pressed;
  183. }
  184. bool InputEventKey::is_pressed() const {
  185. return pressed;
  186. }
  187. void InputEventKey::set_scancode(uint32_t p_scancode) {
  188. scancode = p_scancode;
  189. }
  190. uint32_t InputEventKey::get_scancode() const {
  191. return scancode;
  192. }
  193. void InputEventKey::set_physical_scancode(uint32_t p_scancode) {
  194. physical_scancode = p_scancode;
  195. }
  196. uint32_t InputEventKey::get_physical_scancode() const {
  197. return physical_scancode;
  198. }
  199. void InputEventKey::set_unicode(uint32_t p_unicode) {
  200. unicode = p_unicode;
  201. }
  202. uint32_t InputEventKey::get_unicode() const {
  203. return unicode;
  204. }
  205. void InputEventKey::set_echo(bool p_enable) {
  206. echo = p_enable;
  207. }
  208. bool InputEventKey::is_echo() const {
  209. return echo;
  210. }
  211. uint32_t InputEventKey::get_scancode_with_modifiers() const {
  212. return scancode | get_modifiers_mask();
  213. }
  214. uint32_t InputEventKey::get_physical_scancode_with_modifiers() const {
  215. return physical_scancode | get_modifiers_mask();
  216. }
  217. String InputEventKey::as_text() const {
  218. String kc;
  219. if (scancode == 0) {
  220. kc = keycode_get_string(physical_scancode) + " (" + RTR("Physical") + ")";
  221. } else {
  222. kc = keycode_get_string(scancode);
  223. }
  224. if (kc == String()) {
  225. return kc;
  226. }
  227. if (get_metakey()) {
  228. kc = find_keycode_name(KEY_META) + ("+" + kc);
  229. }
  230. if (get_alt()) {
  231. kc = find_keycode_name(KEY_ALT) + ("+" + kc);
  232. }
  233. if (get_shift()) {
  234. kc = find_keycode_name(KEY_SHIFT) + ("+" + kc);
  235. }
  236. if (get_control()) {
  237. kc = find_keycode_name(KEY_CONTROL) + ("+" + kc);
  238. }
  239. return kc;
  240. }
  241. bool InputEventKey::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
  242. Ref<InputEventKey> key = p_event;
  243. if (key.is_null()) {
  244. return false;
  245. }
  246. bool match;
  247. if (scancode != 0) {
  248. match = scancode == key->scancode;
  249. } else {
  250. match = physical_scancode == key->physical_scancode;
  251. }
  252. uint32_t action_mask = get_modifiers_mask();
  253. uint32_t key_mask = key->get_modifiers_mask();
  254. if (key->is_pressed()) {
  255. match &= (action_mask & key_mask) == action_mask;
  256. }
  257. if (p_exact_match) {
  258. match &= action_mask == key_mask;
  259. }
  260. if (match) {
  261. bool pressed = key->is_pressed();
  262. if (p_pressed != nullptr) {
  263. *p_pressed = pressed;
  264. }
  265. float strength = pressed ? 1.0f : 0.0f;
  266. if (p_strength != nullptr) {
  267. *p_strength = strength;
  268. }
  269. if (p_raw_strength != nullptr) {
  270. *p_raw_strength = strength;
  271. }
  272. }
  273. return match;
  274. }
  275. bool InputEventKey::shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
  276. Ref<InputEventKey> key = p_event;
  277. if (key.is_null()) {
  278. return false;
  279. }
  280. if (scancode == 0) {
  281. return physical_scancode == key->physical_scancode &&
  282. (!p_exact_match || get_modifiers_mask() == key->get_modifiers_mask());
  283. } else {
  284. return scancode == key->scancode &&
  285. (!p_exact_match || get_modifiers_mask() == key->get_modifiers_mask());
  286. }
  287. }
  288. void InputEventKey::_bind_methods() {
  289. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventKey::set_pressed);
  290. ClassDB::bind_method(D_METHOD("set_scancode", "scancode"), &InputEventKey::set_scancode);
  291. ClassDB::bind_method(D_METHOD("get_scancode"), &InputEventKey::get_scancode);
  292. ClassDB::bind_method(D_METHOD("set_physical_scancode", "scancode"), &InputEventKey::set_physical_scancode);
  293. ClassDB::bind_method(D_METHOD("get_physical_scancode"), &InputEventKey::get_physical_scancode);
  294. ClassDB::bind_method(D_METHOD("set_unicode", "unicode"), &InputEventKey::set_unicode);
  295. ClassDB::bind_method(D_METHOD("get_unicode"), &InputEventKey::get_unicode);
  296. ClassDB::bind_method(D_METHOD("set_echo", "echo"), &InputEventKey::set_echo);
  297. ClassDB::bind_method(D_METHOD("get_scancode_with_modifiers"), &InputEventKey::get_scancode_with_modifiers);
  298. ClassDB::bind_method(D_METHOD("get_physical_scancode_with_modifiers"), &InputEventKey::get_physical_scancode_with_modifiers);
  299. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  300. ADD_PROPERTY(PropertyInfo(Variant::INT, "scancode"), "set_scancode", "get_scancode");
  301. ADD_PROPERTY(PropertyInfo(Variant::INT, "physical_scancode"), "set_physical_scancode", "get_physical_scancode");
  302. ADD_PROPERTY(PropertyInfo(Variant::INT, "unicode"), "set_unicode", "get_unicode");
  303. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "echo"), "set_echo", "is_echo");
  304. }
  305. InputEventKey::InputEventKey() {
  306. pressed = false;
  307. scancode = 0;
  308. physical_scancode = 0;
  309. unicode = 0; ///unicode
  310. echo = false;
  311. }
  312. ////////////////////////////////////////
  313. void InputEventMouse::set_button_mask(int p_mask) {
  314. button_mask = p_mask;
  315. }
  316. int InputEventMouse::get_button_mask() const {
  317. return button_mask;
  318. }
  319. void InputEventMouse::set_position(const Vector2 &p_pos) {
  320. pos = p_pos;
  321. }
  322. Vector2 InputEventMouse::get_position() const {
  323. return pos;
  324. }
  325. void InputEventMouse::set_global_position(const Vector2 &p_global_pos) {
  326. global_pos = p_global_pos;
  327. }
  328. Vector2 InputEventMouse::get_global_position() const {
  329. return global_pos;
  330. }
  331. void InputEventMouse::_bind_methods() {
  332. ClassDB::bind_method(D_METHOD("set_button_mask", "button_mask"), &InputEventMouse::set_button_mask);
  333. ClassDB::bind_method(D_METHOD("get_button_mask"), &InputEventMouse::get_button_mask);
  334. ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventMouse::set_position);
  335. ClassDB::bind_method(D_METHOD("get_position"), &InputEventMouse::get_position);
  336. ClassDB::bind_method(D_METHOD("set_global_position", "global_position"), &InputEventMouse::set_global_position);
  337. ClassDB::bind_method(D_METHOD("get_global_position"), &InputEventMouse::get_global_position);
  338. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask"), "set_button_mask", "get_button_mask");
  339. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
  340. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_position"), "set_global_position", "get_global_position");
  341. }
  342. InputEventMouse::InputEventMouse() {
  343. button_mask = 0;
  344. }
  345. ///////////////////////////////////////
  346. void InputEventMouseButton::set_factor(float p_factor) {
  347. factor = p_factor;
  348. }
  349. float InputEventMouseButton::get_factor() const {
  350. return factor;
  351. }
  352. void InputEventMouseButton::set_button_index(int p_index) {
  353. button_index = p_index;
  354. }
  355. int InputEventMouseButton::get_button_index() const {
  356. return button_index;
  357. }
  358. void InputEventMouseButton::set_pressed(bool p_pressed) {
  359. pressed = p_pressed;
  360. }
  361. bool InputEventMouseButton::is_pressed() const {
  362. return pressed;
  363. }
  364. void InputEventMouseButton::set_doubleclick(bool p_doubleclick) {
  365. doubleclick = p_doubleclick;
  366. }
  367. bool InputEventMouseButton::is_doubleclick() const {
  368. return doubleclick;
  369. }
  370. Ref<InputEvent> InputEventMouseButton::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  371. Vector2 g = get_global_position();
  372. Vector2 l = p_xform.xform(get_position() + p_local_ofs);
  373. Ref<InputEventMouseButton> mb;
  374. mb.instance();
  375. mb->set_device(get_device());
  376. mb->set_modifiers_from_event(this);
  377. mb->set_position(l);
  378. mb->set_global_position(g);
  379. mb->set_button_mask(get_button_mask());
  380. mb->set_pressed(pressed);
  381. mb->set_doubleclick(doubleclick);
  382. mb->set_factor(factor);
  383. mb->set_button_index(button_index);
  384. return mb;
  385. }
  386. bool InputEventMouseButton::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
  387. Ref<InputEventMouseButton> mb = p_event;
  388. if (mb.is_null()) {
  389. return false;
  390. }
  391. bool match = mb->button_index == button_index;
  392. uint32_t action_mask = get_modifiers_mask();
  393. uint32_t button_mask = mb->get_modifiers_mask();
  394. if (mb->is_pressed()) {
  395. match &= (action_mask & button_mask) == action_mask;
  396. }
  397. if (p_exact_match) {
  398. match &= action_mask == button_mask;
  399. }
  400. if (match) {
  401. bool pressed = mb->is_pressed();
  402. if (p_pressed != nullptr) {
  403. *p_pressed = pressed;
  404. }
  405. float strength = pressed ? 1.0f : 0.0f;
  406. if (p_strength != nullptr) {
  407. *p_strength = strength;
  408. }
  409. if (p_raw_strength != nullptr) {
  410. *p_raw_strength = strength;
  411. }
  412. }
  413. return match;
  414. }
  415. bool InputEventMouseButton::shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
  416. Ref<InputEventMouseButton> mb = p_event;
  417. if (mb.is_null()) {
  418. return false;
  419. }
  420. return button_index == mb->button_index &&
  421. (!p_exact_match || get_modifiers_mask() == mb->get_modifiers_mask());
  422. }
  423. String InputEventMouseButton::as_text() const {
  424. String button_index_string = "";
  425. switch (get_button_index()) {
  426. case BUTTON_LEFT:
  427. button_index_string = "BUTTON_LEFT";
  428. break;
  429. case BUTTON_RIGHT:
  430. button_index_string = "BUTTON_RIGHT";
  431. break;
  432. case BUTTON_MIDDLE:
  433. button_index_string = "BUTTON_MIDDLE";
  434. break;
  435. case BUTTON_WHEEL_UP:
  436. button_index_string = "BUTTON_WHEEL_UP";
  437. break;
  438. case BUTTON_WHEEL_DOWN:
  439. button_index_string = "BUTTON_WHEEL_DOWN";
  440. break;
  441. case BUTTON_WHEEL_LEFT:
  442. button_index_string = "BUTTON_WHEEL_LEFT";
  443. break;
  444. case BUTTON_WHEEL_RIGHT:
  445. button_index_string = "BUTTON_WHEEL_RIGHT";
  446. break;
  447. case BUTTON_XBUTTON1:
  448. button_index_string = "BUTTON_XBUTTON1";
  449. break;
  450. case BUTTON_XBUTTON2:
  451. button_index_string = "BUTTON_XBUTTON2";
  452. break;
  453. default:
  454. button_index_string = itos(get_button_index());
  455. break;
  456. }
  457. 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");
  458. }
  459. void InputEventMouseButton::_bind_methods() {
  460. ClassDB::bind_method(D_METHOD("set_factor", "factor"), &InputEventMouseButton::set_factor);
  461. ClassDB::bind_method(D_METHOD("get_factor"), &InputEventMouseButton::get_factor);
  462. ClassDB::bind_method(D_METHOD("set_button_index", "button_index"), &InputEventMouseButton::set_button_index);
  463. ClassDB::bind_method(D_METHOD("get_button_index"), &InputEventMouseButton::get_button_index);
  464. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventMouseButton::set_pressed);
  465. // ClassDB::bind_method(D_METHOD("is_pressed"), &InputEventMouseButton::is_pressed);
  466. ClassDB::bind_method(D_METHOD("set_doubleclick", "doubleclick"), &InputEventMouseButton::set_doubleclick);
  467. ClassDB::bind_method(D_METHOD("is_doubleclick"), &InputEventMouseButton::is_doubleclick);
  468. ADD_PROPERTY(PropertyInfo(Variant::REAL, "factor"), "set_factor", "get_factor");
  469. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_index"), "set_button_index", "get_button_index");
  470. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  471. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "doubleclick"), "set_doubleclick", "is_doubleclick");
  472. }
  473. InputEventMouseButton::InputEventMouseButton() {
  474. factor = 1;
  475. button_index = 0;
  476. pressed = false;
  477. doubleclick = false;
  478. }
  479. ////////////////////////////////////////////
  480. void InputEventMouseMotion::set_tilt(const Vector2 &p_tilt) {
  481. tilt = p_tilt;
  482. }
  483. Vector2 InputEventMouseMotion::get_tilt() const {
  484. return tilt;
  485. }
  486. void InputEventMouseMotion::set_pressure(float p_pressure) {
  487. pressure = p_pressure;
  488. }
  489. float InputEventMouseMotion::get_pressure() const {
  490. return pressure;
  491. }
  492. void InputEventMouseMotion::set_relative(const Vector2 &p_relative) {
  493. relative = p_relative;
  494. }
  495. Vector2 InputEventMouseMotion::get_relative() const {
  496. return relative;
  497. }
  498. void InputEventMouseMotion::set_speed(const Vector2 &p_speed) {
  499. speed = p_speed;
  500. }
  501. Vector2 InputEventMouseMotion::get_speed() const {
  502. return speed;
  503. }
  504. Ref<InputEvent> InputEventMouseMotion::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  505. Vector2 g = get_global_position();
  506. Vector2 l = p_xform.xform(get_position() + p_local_ofs);
  507. Vector2 r = p_xform.basis_xform(get_relative());
  508. Vector2 s = p_xform.basis_xform(get_speed());
  509. Ref<InputEventMouseMotion> mm;
  510. mm.instance();
  511. mm->set_device(get_device());
  512. mm->set_modifiers_from_event(this);
  513. mm->set_position(l);
  514. mm->set_pressure(get_pressure());
  515. mm->set_tilt(get_tilt());
  516. mm->set_global_position(g);
  517. mm->set_button_mask(get_button_mask());
  518. mm->set_relative(r);
  519. mm->set_speed(s);
  520. return mm;
  521. }
  522. String InputEventMouseMotion::as_text() const {
  523. String button_mask_string = "";
  524. switch (get_button_mask()) {
  525. case BUTTON_MASK_LEFT:
  526. button_mask_string = "BUTTON_MASK_LEFT";
  527. break;
  528. case BUTTON_MASK_MIDDLE:
  529. button_mask_string = "BUTTON_MASK_MIDDLE";
  530. break;
  531. case BUTTON_MASK_RIGHT:
  532. button_mask_string = "BUTTON_MASK_RIGHT";
  533. break;
  534. case BUTTON_MASK_XBUTTON1:
  535. button_mask_string = "BUTTON_MASK_XBUTTON1";
  536. break;
  537. case BUTTON_MASK_XBUTTON2:
  538. button_mask_string = "BUTTON_MASK_XBUTTON2";
  539. break;
  540. default:
  541. button_mask_string = itos(get_button_mask());
  542. break;
  543. }
  544. return "InputEventMouseMotion : button_mask=" + button_mask_string + ", position=(" + String(get_position()) + "), relative=(" + String(get_relative()) + "), speed=(" + String(get_speed()) + "), pressure=(" + rtos(get_pressure()) + "), tilt=(" + String(get_tilt()) + ")";
  545. }
  546. bool InputEventMouseMotion::accumulate(const Ref<InputEvent> &p_event) {
  547. Ref<InputEventMouseMotion> motion = p_event;
  548. if (motion.is_null()) {
  549. return false;
  550. }
  551. if (is_pressed() != motion->is_pressed()) {
  552. return false;
  553. }
  554. if (get_button_mask() != motion->get_button_mask()) {
  555. return false;
  556. }
  557. if (get_shift() != motion->get_shift()) {
  558. return false;
  559. }
  560. if (get_control() != motion->get_control()) {
  561. return false;
  562. }
  563. if (get_alt() != motion->get_alt()) {
  564. return false;
  565. }
  566. if (get_metakey() != motion->get_metakey()) {
  567. return false;
  568. }
  569. set_position(motion->get_position());
  570. set_global_position(motion->get_global_position());
  571. set_speed(motion->get_speed());
  572. relative += motion->get_relative();
  573. return true;
  574. }
  575. void InputEventMouseMotion::_bind_methods() {
  576. ClassDB::bind_method(D_METHOD("set_tilt", "tilt"), &InputEventMouseMotion::set_tilt);
  577. ClassDB::bind_method(D_METHOD("get_tilt"), &InputEventMouseMotion::get_tilt);
  578. ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventMouseMotion::set_pressure);
  579. ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventMouseMotion::get_pressure);
  580. ClassDB::bind_method(D_METHOD("set_relative", "relative"), &InputEventMouseMotion::set_relative);
  581. ClassDB::bind_method(D_METHOD("get_relative"), &InputEventMouseMotion::get_relative);
  582. ClassDB::bind_method(D_METHOD("set_speed", "speed"), &InputEventMouseMotion::set_speed);
  583. ClassDB::bind_method(D_METHOD("get_speed"), &InputEventMouseMotion::get_speed);
  584. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "tilt"), "set_tilt", "get_tilt");
  585. ADD_PROPERTY(PropertyInfo(Variant::REAL, "pressure"), "set_pressure", "get_pressure");
  586. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "relative"), "set_relative", "get_relative");
  587. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "speed"), "set_speed", "get_speed");
  588. }
  589. InputEventMouseMotion::InputEventMouseMotion() {
  590. pressure = 0;
  591. }
  592. ////////////////////////////////////////
  593. void InputEventJoypadMotion::set_axis(int p_axis) {
  594. axis = p_axis;
  595. }
  596. int InputEventJoypadMotion::get_axis() const {
  597. return axis;
  598. }
  599. void InputEventJoypadMotion::set_axis_value(float p_value) {
  600. axis_value = p_value;
  601. }
  602. float InputEventJoypadMotion::get_axis_value() const {
  603. return axis_value;
  604. }
  605. bool InputEventJoypadMotion::is_pressed() const {
  606. return Math::abs(axis_value) >= 0.5f;
  607. }
  608. bool InputEventJoypadMotion::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
  609. Ref<InputEventJoypadMotion> jm = p_event;
  610. if (jm.is_null()) {
  611. return false;
  612. }
  613. // Matches even if not in the same direction, but returns a "not pressed" event.
  614. bool match = (axis == jm->axis);
  615. if (p_exact_match) {
  616. match &= (axis_value < 0) == (jm->axis_value < 0);
  617. }
  618. if (match) {
  619. float jm_abs_axis_value = Math::abs(jm->get_axis_value());
  620. bool same_direction = (((axis_value < 0) == (jm->axis_value < 0)) || jm->axis_value == 0);
  621. bool pressed = same_direction ? jm_abs_axis_value >= p_deadzone : false;
  622. if (p_pressed != nullptr) {
  623. *p_pressed = pressed;
  624. }
  625. if (p_strength != nullptr) {
  626. if (pressed) {
  627. if (p_deadzone == 1.0f) {
  628. *p_strength = 1.0f;
  629. } else {
  630. *p_strength = CLAMP(Math::inverse_lerp(p_deadzone, 1.0f, jm_abs_axis_value), 0.0f, 1.0f);
  631. }
  632. } else {
  633. *p_strength = 0.0f;
  634. }
  635. }
  636. if (p_raw_strength != nullptr) {
  637. if (same_direction) { // NOT pressed, because we want to ignore the deadzone.
  638. *p_raw_strength = jm_abs_axis_value;
  639. } else {
  640. *p_raw_strength = 0.0f;
  641. }
  642. }
  643. }
  644. return match;
  645. }
  646. bool InputEventJoypadMotion::shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
  647. Ref<InputEventJoypadMotion> jm = p_event;
  648. if (jm.is_null()) {
  649. return false;
  650. }
  651. return axis == jm->axis &&
  652. (!p_exact_match || ((axis_value < 0) == (jm->axis_value < 0)));
  653. }
  654. String InputEventJoypadMotion::as_text() const {
  655. return "InputEventJoypadMotion : axis=" + itos(axis) + ", axis_value=" + String(Variant(axis_value));
  656. }
  657. void InputEventJoypadMotion::_bind_methods() {
  658. ClassDB::bind_method(D_METHOD("set_axis", "axis"), &InputEventJoypadMotion::set_axis);
  659. ClassDB::bind_method(D_METHOD("get_axis"), &InputEventJoypadMotion::get_axis);
  660. ClassDB::bind_method(D_METHOD("set_axis_value", "axis_value"), &InputEventJoypadMotion::set_axis_value);
  661. ClassDB::bind_method(D_METHOD("get_axis_value"), &InputEventJoypadMotion::get_axis_value);
  662. ADD_PROPERTY(PropertyInfo(Variant::INT, "axis"), "set_axis", "get_axis");
  663. ADD_PROPERTY(PropertyInfo(Variant::REAL, "axis_value"), "set_axis_value", "get_axis_value");
  664. }
  665. InputEventJoypadMotion::InputEventJoypadMotion() {
  666. axis = 0;
  667. axis_value = 0;
  668. }
  669. /////////////////////////////////
  670. void InputEventJoypadButton::set_button_index(int p_index) {
  671. button_index = p_index;
  672. }
  673. int InputEventJoypadButton::get_button_index() const {
  674. return button_index;
  675. }
  676. void InputEventJoypadButton::set_pressed(bool p_pressed) {
  677. pressed = p_pressed;
  678. }
  679. bool InputEventJoypadButton::is_pressed() const {
  680. return pressed;
  681. }
  682. void InputEventJoypadButton::set_pressure(float p_pressure) {
  683. pressure = p_pressure;
  684. }
  685. float InputEventJoypadButton::get_pressure() const {
  686. return pressure;
  687. }
  688. bool InputEventJoypadButton::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
  689. Ref<InputEventJoypadButton> jb = p_event;
  690. if (jb.is_null()) {
  691. return false;
  692. }
  693. bool match = button_index == jb->button_index;
  694. if (match) {
  695. bool pressed = jb->is_pressed();
  696. if (p_pressed != nullptr) {
  697. *p_pressed = pressed;
  698. }
  699. float strength = pressed ? 1.0f : 0.0f;
  700. if (p_strength != nullptr) {
  701. *p_strength = strength;
  702. }
  703. if (p_raw_strength != nullptr) {
  704. *p_raw_strength = strength;
  705. }
  706. }
  707. return match;
  708. }
  709. bool InputEventJoypadButton::shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
  710. Ref<InputEventJoypadButton> button = p_event;
  711. if (button.is_null()) {
  712. return false;
  713. }
  714. return button_index == button->button_index;
  715. }
  716. String InputEventJoypadButton::as_text() const {
  717. return "InputEventJoypadButton : button_index=" + itos(button_index) + ", pressed=" + (pressed ? "true" : "false") + ", pressure=" + String(Variant(pressure));
  718. }
  719. void InputEventJoypadButton::_bind_methods() {
  720. ClassDB::bind_method(D_METHOD("set_button_index", "button_index"), &InputEventJoypadButton::set_button_index);
  721. ClassDB::bind_method(D_METHOD("get_button_index"), &InputEventJoypadButton::get_button_index);
  722. ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventJoypadButton::set_pressure);
  723. ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventJoypadButton::get_pressure);
  724. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventJoypadButton::set_pressed);
  725. // ClassDB::bind_method(D_METHOD("is_pressed"), &InputEventJoypadButton::is_pressed);
  726. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_index"), "set_button_index", "get_button_index");
  727. ADD_PROPERTY(PropertyInfo(Variant::REAL, "pressure"), "set_pressure", "get_pressure");
  728. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  729. }
  730. InputEventJoypadButton::InputEventJoypadButton() {
  731. button_index = 0;
  732. pressure = 0;
  733. pressed = false;
  734. }
  735. //////////////////////////////////////////////
  736. void InputEventScreenTouch::set_index(int p_index) {
  737. index = p_index;
  738. }
  739. int InputEventScreenTouch::get_index() const {
  740. return index;
  741. }
  742. void InputEventScreenTouch::set_position(const Vector2 &p_pos) {
  743. pos = p_pos;
  744. }
  745. Vector2 InputEventScreenTouch::get_position() const {
  746. return pos;
  747. }
  748. void InputEventScreenTouch::set_pressed(bool p_pressed) {
  749. pressed = p_pressed;
  750. }
  751. bool InputEventScreenTouch::is_pressed() const {
  752. return pressed;
  753. }
  754. Ref<InputEvent> InputEventScreenTouch::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  755. Ref<InputEventScreenTouch> st;
  756. st.instance();
  757. st->set_device(get_device());
  758. st->set_index(index);
  759. st->set_position(p_xform.xform(pos + p_local_ofs));
  760. st->set_pressed(pressed);
  761. return st;
  762. }
  763. String InputEventScreenTouch::as_text() const {
  764. return "InputEventScreenTouch : index=" + itos(index) + ", pressed=" + (pressed ? "true" : "false") + ", position=(" + String(get_position()) + ")";
  765. }
  766. void InputEventScreenTouch::_bind_methods() {
  767. ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenTouch::set_index);
  768. ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenTouch::get_index);
  769. ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventScreenTouch::set_position);
  770. ClassDB::bind_method(D_METHOD("get_position"), &InputEventScreenTouch::get_position);
  771. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventScreenTouch::set_pressed);
  772. //ClassDB::bind_method(D_METHOD("is_pressed"),&InputEventScreenTouch::is_pressed);
  773. ADD_PROPERTY(PropertyInfo(Variant::INT, "index"), "set_index", "get_index");
  774. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
  775. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  776. }
  777. InputEventScreenTouch::InputEventScreenTouch() {
  778. index = 0;
  779. pressed = false;
  780. }
  781. /////////////////////////////
  782. void InputEventScreenDrag::set_index(int p_index) {
  783. index = p_index;
  784. }
  785. int InputEventScreenDrag::get_index() const {
  786. return index;
  787. }
  788. void InputEventScreenDrag::set_position(const Vector2 &p_pos) {
  789. pos = p_pos;
  790. }
  791. Vector2 InputEventScreenDrag::get_position() const {
  792. return pos;
  793. }
  794. void InputEventScreenDrag::set_relative(const Vector2 &p_relative) {
  795. relative = p_relative;
  796. }
  797. Vector2 InputEventScreenDrag::get_relative() const {
  798. return relative;
  799. }
  800. void InputEventScreenDrag::set_speed(const Vector2 &p_speed) {
  801. speed = p_speed;
  802. }
  803. Vector2 InputEventScreenDrag::get_speed() const {
  804. return speed;
  805. }
  806. Ref<InputEvent> InputEventScreenDrag::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  807. Ref<InputEventScreenDrag> sd;
  808. sd.instance();
  809. sd->set_device(get_device());
  810. sd->set_index(index);
  811. sd->set_position(p_xform.xform(pos + p_local_ofs));
  812. sd->set_relative(p_xform.basis_xform(relative));
  813. sd->set_speed(p_xform.basis_xform(speed));
  814. return sd;
  815. }
  816. String InputEventScreenDrag::as_text() const {
  817. return "InputEventScreenDrag : index=" + itos(index) + ", position=(" + String(get_position()) + "), relative=(" + String(get_relative()) + "), speed=(" + String(get_speed()) + ")";
  818. }
  819. bool InputEventScreenDrag::accumulate(const Ref<InputEvent> &p_event) {
  820. Ref<InputEventScreenDrag> drag = p_event;
  821. if (drag.is_null())
  822. return false;
  823. if (get_index() != drag->get_index()) {
  824. return false;
  825. }
  826. set_position(drag->get_position());
  827. set_speed(drag->get_speed());
  828. relative += drag->get_relative();
  829. return true;
  830. }
  831. void InputEventScreenDrag::_bind_methods() {
  832. ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenDrag::set_index);
  833. ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenDrag::get_index);
  834. ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventScreenDrag::set_position);
  835. ClassDB::bind_method(D_METHOD("get_position"), &InputEventScreenDrag::get_position);
  836. ClassDB::bind_method(D_METHOD("set_relative", "relative"), &InputEventScreenDrag::set_relative);
  837. ClassDB::bind_method(D_METHOD("get_relative"), &InputEventScreenDrag::get_relative);
  838. ClassDB::bind_method(D_METHOD("set_speed", "speed"), &InputEventScreenDrag::set_speed);
  839. ClassDB::bind_method(D_METHOD("get_speed"), &InputEventScreenDrag::get_speed);
  840. ADD_PROPERTY(PropertyInfo(Variant::INT, "index"), "set_index", "get_index");
  841. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
  842. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "relative"), "set_relative", "get_relative");
  843. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "speed"), "set_speed", "get_speed");
  844. }
  845. InputEventScreenDrag::InputEventScreenDrag() {
  846. index = 0;
  847. }
  848. /////////////////////////////
  849. void InputEventAction::set_action(const StringName &p_action) {
  850. action = p_action;
  851. }
  852. StringName InputEventAction::get_action() const {
  853. return action;
  854. }
  855. void InputEventAction::set_pressed(bool p_pressed) {
  856. pressed = p_pressed;
  857. }
  858. bool InputEventAction::is_pressed() const {
  859. return pressed;
  860. }
  861. void InputEventAction::set_strength(float p_strength) {
  862. strength = CLAMP(p_strength, 0.0f, 1.0f);
  863. }
  864. float InputEventAction::get_strength() const {
  865. return strength;
  866. }
  867. bool InputEventAction::shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
  868. if (p_event.is_null()) {
  869. return false;
  870. }
  871. return p_event->is_action(action);
  872. }
  873. bool InputEventAction::is_action(const StringName &p_action) const {
  874. return action == p_action;
  875. }
  876. bool InputEventAction::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
  877. Ref<InputEventAction> act = p_event;
  878. if (act.is_null()) {
  879. return false;
  880. }
  881. bool match = action == act->action;
  882. if (match) {
  883. bool pressed = act->pressed;
  884. if (p_pressed != nullptr) {
  885. *p_pressed = pressed;
  886. }
  887. float strength = pressed ? 1.0f : 0.0f;
  888. if (p_strength != nullptr) {
  889. *p_strength = strength;
  890. }
  891. if (p_raw_strength != nullptr) {
  892. *p_raw_strength = strength;
  893. }
  894. }
  895. return match;
  896. }
  897. String InputEventAction::as_text() const {
  898. return "InputEventAction : action=" + action + ", pressed=(" + (pressed ? "true" : "false");
  899. }
  900. void InputEventAction::_bind_methods() {
  901. ClassDB::bind_method(D_METHOD("set_action", "action"), &InputEventAction::set_action);
  902. ClassDB::bind_method(D_METHOD("get_action"), &InputEventAction::get_action);
  903. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventAction::set_pressed);
  904. //ClassDB::bind_method(D_METHOD("is_pressed"), &InputEventAction::is_pressed);
  905. ClassDB::bind_method(D_METHOD("set_strength", "strength"), &InputEventAction::set_strength);
  906. ClassDB::bind_method(D_METHOD("get_strength"), &InputEventAction::get_strength);
  907. // ClassDB::bind_method(D_METHOD("is_action", "name"), &InputEventAction::is_action);
  908. ADD_PROPERTY(PropertyInfo(Variant::STRING, "action"), "set_action", "get_action");
  909. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  910. ADD_PROPERTY(PropertyInfo(Variant::REAL, "strength", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_strength", "get_strength");
  911. }
  912. InputEventAction::InputEventAction() {
  913. pressed = false;
  914. strength = 1.0f;
  915. }
  916. /////////////////////////////
  917. void InputEventGesture::set_position(const Vector2 &p_pos) {
  918. pos = p_pos;
  919. }
  920. void InputEventGesture::_bind_methods() {
  921. ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventGesture::set_position);
  922. ClassDB::bind_method(D_METHOD("get_position"), &InputEventGesture::get_position);
  923. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
  924. }
  925. Vector2 InputEventGesture::get_position() const {
  926. return pos;
  927. }
  928. /////////////////////////////
  929. void InputEventMagnifyGesture::set_factor(real_t p_factor) {
  930. factor = p_factor;
  931. }
  932. real_t InputEventMagnifyGesture::get_factor() const {
  933. return factor;
  934. }
  935. Ref<InputEvent> InputEventMagnifyGesture::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  936. Ref<InputEventMagnifyGesture> ev;
  937. ev.instance();
  938. ev->set_device(get_device());
  939. ev->set_modifiers_from_event(this);
  940. ev->set_position(p_xform.xform(get_position() + p_local_ofs));
  941. ev->set_factor(get_factor());
  942. return ev;
  943. }
  944. String InputEventMagnifyGesture::as_text() const {
  945. return "InputEventMagnifyGesture : factor=" + rtos(get_factor()) + ", position=(" + String(get_position()) + ")";
  946. }
  947. void InputEventMagnifyGesture::_bind_methods() {
  948. ClassDB::bind_method(D_METHOD("set_factor", "factor"), &InputEventMagnifyGesture::set_factor);
  949. ClassDB::bind_method(D_METHOD("get_factor"), &InputEventMagnifyGesture::get_factor);
  950. ADD_PROPERTY(PropertyInfo(Variant::REAL, "factor"), "set_factor", "get_factor");
  951. }
  952. InputEventMagnifyGesture::InputEventMagnifyGesture() {
  953. factor = 1.0;
  954. }
  955. /////////////////////////////
  956. void InputEventPanGesture::set_delta(const Vector2 &p_delta) {
  957. delta = p_delta;
  958. }
  959. Vector2 InputEventPanGesture::get_delta() const {
  960. return delta;
  961. }
  962. Ref<InputEvent> InputEventPanGesture::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  963. Ref<InputEventPanGesture> ev;
  964. ev.instance();
  965. ev->set_device(get_device());
  966. ev->set_modifiers_from_event(this);
  967. ev->set_position(p_xform.xform(get_position() + p_local_ofs));
  968. ev->set_delta(get_delta());
  969. return ev;
  970. }
  971. String InputEventPanGesture::as_text() const {
  972. return "InputEventPanGesture : delta=(" + String(get_delta()) + "), position=(" + String(get_position()) + ")";
  973. }
  974. void InputEventPanGesture::_bind_methods() {
  975. ClassDB::bind_method(D_METHOD("set_delta", "delta"), &InputEventPanGesture::set_delta);
  976. ClassDB::bind_method(D_METHOD("get_delta"), &InputEventPanGesture::get_delta);
  977. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "delta"), "set_delta", "get_delta");
  978. }
  979. InputEventPanGesture::InputEventPanGesture() {
  980. delta = Vector2(0, 0);
  981. }
  982. /////////////////////////////
  983. void InputEventMIDI::set_channel(const int p_channel) {
  984. channel = p_channel;
  985. }
  986. int InputEventMIDI::get_channel() const {
  987. return channel;
  988. }
  989. void InputEventMIDI::set_message(const int p_message) {
  990. message = p_message;
  991. }
  992. int InputEventMIDI::get_message() const {
  993. return message;
  994. }
  995. void InputEventMIDI::set_pitch(const int p_pitch) {
  996. pitch = p_pitch;
  997. }
  998. int InputEventMIDI::get_pitch() const {
  999. return pitch;
  1000. }
  1001. void InputEventMIDI::set_velocity(const int p_velocity) {
  1002. velocity = p_velocity;
  1003. }
  1004. int InputEventMIDI::get_velocity() const {
  1005. return velocity;
  1006. }
  1007. void InputEventMIDI::set_instrument(const int p_instrument) {
  1008. instrument = p_instrument;
  1009. }
  1010. int InputEventMIDI::get_instrument() const {
  1011. return instrument;
  1012. }
  1013. void InputEventMIDI::set_pressure(const int p_pressure) {
  1014. pressure = p_pressure;
  1015. }
  1016. int InputEventMIDI::get_pressure() const {
  1017. return pressure;
  1018. }
  1019. void InputEventMIDI::set_controller_number(const int p_controller_number) {
  1020. controller_number = p_controller_number;
  1021. }
  1022. int InputEventMIDI::get_controller_number() const {
  1023. return controller_number;
  1024. }
  1025. void InputEventMIDI::set_controller_value(const int p_controller_value) {
  1026. controller_value = p_controller_value;
  1027. }
  1028. int InputEventMIDI::get_controller_value() const {
  1029. return controller_value;
  1030. }
  1031. String InputEventMIDI::as_text() const {
  1032. return "InputEventMIDI : channel=(" + itos(get_channel()) + "), message=(" + itos(get_message()) + ")";
  1033. }
  1034. void InputEventMIDI::_bind_methods() {
  1035. ClassDB::bind_method(D_METHOD("set_channel", "channel"), &InputEventMIDI::set_channel);
  1036. ClassDB::bind_method(D_METHOD("get_channel"), &InputEventMIDI::get_channel);
  1037. ClassDB::bind_method(D_METHOD("set_message", "message"), &InputEventMIDI::set_message);
  1038. ClassDB::bind_method(D_METHOD("get_message"), &InputEventMIDI::get_message);
  1039. ClassDB::bind_method(D_METHOD("set_pitch", "pitch"), &InputEventMIDI::set_pitch);
  1040. ClassDB::bind_method(D_METHOD("get_pitch"), &InputEventMIDI::get_pitch);
  1041. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &InputEventMIDI::set_velocity);
  1042. ClassDB::bind_method(D_METHOD("get_velocity"), &InputEventMIDI::get_velocity);
  1043. ClassDB::bind_method(D_METHOD("set_instrument", "instrument"), &InputEventMIDI::set_instrument);
  1044. ClassDB::bind_method(D_METHOD("get_instrument"), &InputEventMIDI::get_instrument);
  1045. ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventMIDI::set_pressure);
  1046. ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventMIDI::get_pressure);
  1047. ClassDB::bind_method(D_METHOD("set_controller_number", "controller_number"), &InputEventMIDI::set_controller_number);
  1048. ClassDB::bind_method(D_METHOD("get_controller_number"), &InputEventMIDI::get_controller_number);
  1049. ClassDB::bind_method(D_METHOD("set_controller_value", "controller_value"), &InputEventMIDI::set_controller_value);
  1050. ClassDB::bind_method(D_METHOD("get_controller_value"), &InputEventMIDI::get_controller_value);
  1051. ADD_PROPERTY(PropertyInfo(Variant::INT, "channel"), "set_channel", "get_channel");
  1052. ADD_PROPERTY(PropertyInfo(Variant::INT, "message"), "set_message", "get_message");
  1053. ADD_PROPERTY(PropertyInfo(Variant::INT, "pitch"), "set_pitch", "get_pitch");
  1054. ADD_PROPERTY(PropertyInfo(Variant::INT, "velocity"), "set_velocity", "get_velocity");
  1055. ADD_PROPERTY(PropertyInfo(Variant::INT, "instrument"), "set_instrument", "get_instrument");
  1056. ADD_PROPERTY(PropertyInfo(Variant::INT, "pressure"), "set_pressure", "get_pressure");
  1057. ADD_PROPERTY(PropertyInfo(Variant::INT, "controller_number"), "set_controller_number", "get_controller_number");
  1058. ADD_PROPERTY(PropertyInfo(Variant::INT, "controller_value"), "set_controller_value", "get_controller_value");
  1059. }
  1060. InputEventMIDI::InputEventMIDI() {
  1061. channel = 0;
  1062. message = 0;
  1063. pitch = 0;
  1064. velocity = 0;
  1065. instrument = 0;
  1066. pressure = 0;
  1067. controller_number = 0;
  1068. controller_value = 0;
  1069. }