input_event.cpp 27 KB

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