input_event.cpp 35 KB

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