input_event.cpp 43 KB

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