input_event.cpp 26 KB

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