input_event.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  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/input_map.h"
  32. #include "core/input/shortcut.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/os/os.h"
  35. const int InputEvent::DEVICE_ID_EMULATION = -1;
  36. const int InputEvent::DEVICE_ID_INTERNAL = -2;
  37. void InputEvent::set_device(int p_device) {
  38. device = p_device;
  39. emit_changed();
  40. }
  41. int InputEvent::get_device() const {
  42. return device;
  43. }
  44. bool InputEvent::is_action(const StringName &p_action, bool p_exact_match) const {
  45. return InputMap::get_singleton()->event_is_action(Ref<InputEvent>(const_cast<InputEvent *>(this)), p_action, p_exact_match);
  46. }
  47. bool InputEvent::is_action_pressed(const StringName &p_action, bool p_allow_echo, bool p_exact_match) const {
  48. bool pressed_state;
  49. bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>(const_cast<InputEvent *>(this)), p_action, p_exact_match, &pressed_state, nullptr, nullptr);
  50. return valid && pressed_state && (p_allow_echo || !is_echo());
  51. }
  52. bool InputEvent::is_action_released(const StringName &p_action, bool p_exact_match) const {
  53. bool pressed_state;
  54. bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>(const_cast<InputEvent *>(this)), p_action, p_exact_match, &pressed_state, nullptr, nullptr);
  55. return valid && !pressed_state;
  56. }
  57. float InputEvent::get_action_strength(const StringName &p_action, bool p_exact_match) const {
  58. float strength;
  59. bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>(const_cast<InputEvent *>(this)), p_action, p_exact_match, nullptr, &strength, nullptr);
  60. return valid ? strength : 0.0f;
  61. }
  62. float InputEvent::get_action_raw_strength(const StringName &p_action, bool p_exact_match) const {
  63. float raw_strength;
  64. bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>(const_cast<InputEvent *>(this)), p_action, p_exact_match, nullptr, nullptr, &raw_strength);
  65. return valid ? raw_strength : 0.0f;
  66. }
  67. bool InputEvent::is_canceled() const {
  68. return canceled;
  69. }
  70. bool InputEvent::is_pressed() const {
  71. return pressed && !canceled;
  72. }
  73. bool InputEvent::is_released() const {
  74. return !pressed && !canceled;
  75. }
  76. bool InputEvent::is_echo() const {
  77. return false;
  78. }
  79. Ref<InputEvent> InputEvent::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  80. return Ref<InputEvent>(const_cast<InputEvent *>(this));
  81. }
  82. bool InputEvent::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const {
  83. return false;
  84. }
  85. bool InputEvent::is_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("is_match", "event", "exact_match"), &InputEvent::is_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. BIND_CONSTANT(DEVICE_ID_EMULATION);
  109. }
  110. ///////////////////////////////////
  111. void InputEventFromWindow::_bind_methods() {
  112. ClassDB::bind_method(D_METHOD("set_window_id", "id"), &InputEventFromWindow::set_window_id);
  113. ClassDB::bind_method(D_METHOD("get_window_id"), &InputEventFromWindow::get_window_id);
  114. ADD_PROPERTY(PropertyInfo(Variant::INT, "window_id"), "set_window_id", "get_window_id");
  115. }
  116. void InputEventFromWindow::set_window_id(int64_t p_id) {
  117. window_id = p_id;
  118. emit_changed();
  119. }
  120. int64_t InputEventFromWindow::get_window_id() const {
  121. return window_id;
  122. }
  123. ///////////////////////////////////
  124. void InputEventWithModifiers::set_command_or_control_autoremap(bool p_enabled) {
  125. command_or_control_autoremap = p_enabled;
  126. if (command_or_control_autoremap) {
  127. if (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) {
  128. ctrl_pressed = false;
  129. meta_pressed = true;
  130. } else {
  131. ctrl_pressed = true;
  132. meta_pressed = false;
  133. }
  134. } else {
  135. ctrl_pressed = false;
  136. meta_pressed = false;
  137. }
  138. emit_changed();
  139. }
  140. bool InputEventWithModifiers::is_command_or_control_autoremap() const {
  141. return command_or_control_autoremap;
  142. }
  143. bool InputEventWithModifiers::is_command_or_control_pressed() const {
  144. if (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) {
  145. return meta_pressed;
  146. } else {
  147. return ctrl_pressed;
  148. }
  149. }
  150. void InputEventWithModifiers::set_shift_pressed(bool p_enabled) {
  151. shift_pressed = p_enabled;
  152. emit_changed();
  153. }
  154. bool InputEventWithModifiers::is_shift_pressed() const {
  155. return shift_pressed;
  156. }
  157. void InputEventWithModifiers::set_alt_pressed(bool p_enabled) {
  158. alt_pressed = p_enabled;
  159. emit_changed();
  160. }
  161. bool InputEventWithModifiers::is_alt_pressed() const {
  162. return alt_pressed;
  163. }
  164. void InputEventWithModifiers::set_ctrl_pressed(bool p_enabled) {
  165. ERR_FAIL_COND_MSG(command_or_control_autoremap, "Command or Control autoremapping is enabled, cannot set Control directly!");
  166. ctrl_pressed = p_enabled;
  167. emit_changed();
  168. }
  169. bool InputEventWithModifiers::is_ctrl_pressed() const {
  170. return ctrl_pressed;
  171. }
  172. void InputEventWithModifiers::set_meta_pressed(bool p_enabled) {
  173. ERR_FAIL_COND_MSG(command_or_control_autoremap, "Command or Control autoremapping is enabled, cannot set Meta directly!");
  174. meta_pressed = p_enabled;
  175. emit_changed();
  176. }
  177. bool InputEventWithModifiers::is_meta_pressed() const {
  178. return meta_pressed;
  179. }
  180. void InputEventWithModifiers::set_modifiers_from_event(const InputEventWithModifiers *event) {
  181. set_alt_pressed(event->is_alt_pressed());
  182. set_shift_pressed(event->is_shift_pressed());
  183. set_ctrl_pressed(event->is_ctrl_pressed());
  184. set_meta_pressed(event->is_meta_pressed());
  185. }
  186. BitField<KeyModifierMask> InputEventWithModifiers::get_modifiers_mask() const {
  187. BitField<KeyModifierMask> mask;
  188. if (is_ctrl_pressed()) {
  189. mask.set_flag(KeyModifierMask::CTRL);
  190. }
  191. if (is_shift_pressed()) {
  192. mask.set_flag(KeyModifierMask::SHIFT);
  193. }
  194. if (is_alt_pressed()) {
  195. mask.set_flag(KeyModifierMask::ALT);
  196. }
  197. if (is_meta_pressed()) {
  198. mask.set_flag(KeyModifierMask::META);
  199. }
  200. if (is_command_or_control_autoremap()) {
  201. if (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) {
  202. mask.set_flag(KeyModifierMask::META);
  203. } else {
  204. mask.set_flag(KeyModifierMask::CTRL);
  205. }
  206. }
  207. return mask;
  208. }
  209. String InputEventWithModifiers::as_text() const {
  210. Vector<String> mod_names;
  211. if (is_ctrl_pressed()) {
  212. mod_names.push_back(find_keycode_name(Key::CTRL));
  213. }
  214. if (is_shift_pressed()) {
  215. mod_names.push_back(find_keycode_name(Key::SHIFT));
  216. }
  217. if (is_alt_pressed()) {
  218. mod_names.push_back(find_keycode_name(Key::ALT));
  219. }
  220. if (is_meta_pressed()) {
  221. mod_names.push_back(find_keycode_name(Key::META));
  222. }
  223. if (!mod_names.is_empty()) {
  224. return String("+").join(mod_names);
  225. } else {
  226. return "";
  227. }
  228. }
  229. String InputEventWithModifiers::to_string() {
  230. return as_text();
  231. }
  232. void InputEventWithModifiers::_bind_methods() {
  233. ClassDB::bind_method(D_METHOD("set_command_or_control_autoremap", "enable"), &InputEventWithModifiers::set_command_or_control_autoremap);
  234. ClassDB::bind_method(D_METHOD("is_command_or_control_autoremap"), &InputEventWithModifiers::is_command_or_control_autoremap);
  235. ClassDB::bind_method(D_METHOD("is_command_or_control_pressed"), &InputEventWithModifiers::is_command_or_control_pressed);
  236. ClassDB::bind_method(D_METHOD("set_alt_pressed", "pressed"), &InputEventWithModifiers::set_alt_pressed);
  237. ClassDB::bind_method(D_METHOD("is_alt_pressed"), &InputEventWithModifiers::is_alt_pressed);
  238. ClassDB::bind_method(D_METHOD("set_shift_pressed", "pressed"), &InputEventWithModifiers::set_shift_pressed);
  239. ClassDB::bind_method(D_METHOD("is_shift_pressed"), &InputEventWithModifiers::is_shift_pressed);
  240. ClassDB::bind_method(D_METHOD("set_ctrl_pressed", "pressed"), &InputEventWithModifiers::set_ctrl_pressed);
  241. ClassDB::bind_method(D_METHOD("is_ctrl_pressed"), &InputEventWithModifiers::is_ctrl_pressed);
  242. ClassDB::bind_method(D_METHOD("set_meta_pressed", "pressed"), &InputEventWithModifiers::set_meta_pressed);
  243. ClassDB::bind_method(D_METHOD("is_meta_pressed"), &InputEventWithModifiers::is_meta_pressed);
  244. ClassDB::bind_method(D_METHOD("get_modifiers_mask"), &InputEventWithModifiers::get_modifiers_mask);
  245. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "command_or_control_autoremap"), "set_command_or_control_autoremap", "is_command_or_control_autoremap");
  246. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "alt_pressed"), "set_alt_pressed", "is_alt_pressed");
  247. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shift_pressed"), "set_shift_pressed", "is_shift_pressed");
  248. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ctrl_pressed"), "set_ctrl_pressed", "is_ctrl_pressed");
  249. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meta_pressed"), "set_meta_pressed", "is_meta_pressed");
  250. }
  251. void InputEventWithModifiers::_validate_property(PropertyInfo &p_property) const {
  252. if (command_or_control_autoremap) {
  253. // Cannot be used with Meta/Command or Control!
  254. if (p_property.name == "meta_pressed") {
  255. p_property.usage ^= PROPERTY_USAGE_STORAGE;
  256. }
  257. if (p_property.name == "ctrl_pressed") {
  258. p_property.usage ^= PROPERTY_USAGE_STORAGE;
  259. }
  260. } else {
  261. if (p_property.name == "command_or_control_autoremap") {
  262. p_property.usage ^= PROPERTY_USAGE_STORAGE;
  263. }
  264. }
  265. }
  266. ///////////////////////////////////
  267. void InputEventKey::set_pressed(bool p_pressed) {
  268. pressed = p_pressed;
  269. emit_changed();
  270. }
  271. void InputEventKey::set_keycode(Key p_keycode) {
  272. keycode = p_keycode;
  273. emit_changed();
  274. }
  275. Key InputEventKey::get_keycode() const {
  276. return keycode;
  277. }
  278. void InputEventKey::set_key_label(Key p_key_label) {
  279. key_label = p_key_label;
  280. emit_changed();
  281. }
  282. Key InputEventKey::get_key_label() const {
  283. return key_label;
  284. }
  285. void InputEventKey::set_physical_keycode(Key p_keycode) {
  286. physical_keycode = p_keycode;
  287. emit_changed();
  288. }
  289. Key InputEventKey::get_physical_keycode() const {
  290. return physical_keycode;
  291. }
  292. void InputEventKey::set_unicode(char32_t p_unicode) {
  293. unicode = p_unicode;
  294. emit_changed();
  295. }
  296. char32_t InputEventKey::get_unicode() const {
  297. return unicode;
  298. }
  299. void InputEventKey::set_location(KeyLocation p_key_location) {
  300. location = p_key_location;
  301. emit_changed();
  302. }
  303. KeyLocation InputEventKey::get_location() const {
  304. return location;
  305. }
  306. void InputEventKey::set_echo(bool p_enable) {
  307. echo = p_enable;
  308. emit_changed();
  309. }
  310. bool InputEventKey::is_echo() const {
  311. return echo;
  312. }
  313. Key InputEventKey::get_keycode_with_modifiers() const {
  314. return keycode | (int64_t)get_modifiers_mask();
  315. }
  316. Key InputEventKey::get_physical_keycode_with_modifiers() const {
  317. return physical_keycode | (int64_t)get_modifiers_mask();
  318. }
  319. Key InputEventKey::get_key_label_with_modifiers() const {
  320. return key_label | get_modifiers_mask();
  321. }
  322. String InputEventKey::as_text_physical_keycode() const {
  323. String kc;
  324. if (physical_keycode != Key::NONE) {
  325. kc = keycode_get_string(physical_keycode);
  326. } else {
  327. kc = "(" + RTR("Unset") + ")";
  328. }
  329. if (kc.is_empty()) {
  330. return kc;
  331. }
  332. String mods_text = InputEventWithModifiers::as_text();
  333. return mods_text.is_empty() ? kc : mods_text + "+" + kc;
  334. }
  335. String InputEventKey::as_text_keycode() const {
  336. String kc;
  337. if (keycode != Key::NONE) {
  338. kc = keycode_get_string(keycode);
  339. } else {
  340. kc = "(" + RTR("Unset") + ")";
  341. }
  342. if (kc.is_empty()) {
  343. return kc;
  344. }
  345. String mods_text = InputEventWithModifiers::as_text();
  346. return mods_text.is_empty() ? kc : mods_text + "+" + kc;
  347. }
  348. String InputEventKey::as_text_key_label() const {
  349. String kc;
  350. if (key_label != Key::NONE) {
  351. kc = keycode_get_string(key_label);
  352. } else {
  353. kc = "(" + RTR("Unset") + ")";
  354. }
  355. if (kc.is_empty()) {
  356. return kc;
  357. }
  358. String mods_text = InputEventWithModifiers::as_text();
  359. return mods_text.is_empty() ? kc : mods_text + "+" + kc;
  360. }
  361. String InputEventKey::as_text_location() const {
  362. String loc;
  363. switch (location) {
  364. case KeyLocation::LEFT:
  365. loc = "left";
  366. break;
  367. case KeyLocation::RIGHT:
  368. loc = "right";
  369. break;
  370. default:
  371. break;
  372. }
  373. return loc;
  374. }
  375. String InputEventKey::as_text() const {
  376. String kc;
  377. if (keycode == Key::NONE && physical_keycode == Key::NONE && key_label != Key::NONE) {
  378. kc = keycode_get_string(key_label) + " (Unicode)";
  379. } else if (keycode != Key::NONE) {
  380. kc = keycode_get_string(keycode);
  381. } else if (physical_keycode != Key::NONE) {
  382. kc = keycode_get_string(physical_keycode) + " (" + RTR("Physical") + ")";
  383. } else {
  384. kc = "(" + RTR("Unset") + ")";
  385. }
  386. if (kc.is_empty()) {
  387. return kc;
  388. }
  389. String mods_text = InputEventWithModifiers::as_text();
  390. return mods_text.is_empty() ? kc : mods_text + "+" + kc;
  391. }
  392. String InputEventKey::to_string() {
  393. String p = is_pressed() ? "true" : "false";
  394. String e = is_echo() ? "true" : "false";
  395. String kc = "";
  396. String physical = "false";
  397. String loc = as_text_location();
  398. if (loc.is_empty()) {
  399. loc = "unspecified";
  400. }
  401. if (keycode == Key::NONE && physical_keycode == Key::NONE && unicode != 0) {
  402. kc = "U+" + String::num_uint64(unicode, 16) + " (" + String::chr(unicode) + ")";
  403. } else if (keycode != Key::NONE) {
  404. kc = itos((int64_t)keycode) + " (" + keycode_get_string(keycode) + ")";
  405. } else if (physical_keycode != Key::NONE) {
  406. kc = itos((int64_t)physical_keycode) + " (" + keycode_get_string(physical_keycode) + ")";
  407. physical = "true";
  408. } else {
  409. kc = "(" + RTR("Unset") + ")";
  410. }
  411. String mods = InputEventWithModifiers::as_text();
  412. mods = mods.is_empty() ? "none" : mods;
  413. return vformat("InputEventKey: keycode=%s, mods=%s, physical=%s, location=%s, pressed=%s, echo=%s", kc, mods, physical, loc, p, e);
  414. }
  415. Ref<InputEventKey> InputEventKey::create_reference(Key p_keycode, bool p_physical) {
  416. Ref<InputEventKey> ie;
  417. ie.instantiate();
  418. if (p_physical) {
  419. ie->set_physical_keycode(p_keycode & KeyModifierMask::CODE_MASK);
  420. } else {
  421. ie->set_keycode(p_keycode & KeyModifierMask::CODE_MASK);
  422. }
  423. char32_t ch = char32_t(p_keycode & KeyModifierMask::CODE_MASK);
  424. if (ch < 0xd800 || (ch > 0xdfff && ch <= 0x10ffff)) {
  425. ie->set_unicode(ch);
  426. }
  427. if ((p_keycode & KeyModifierMask::SHIFT) != Key::NONE) {
  428. ie->set_shift_pressed(true);
  429. }
  430. if ((p_keycode & KeyModifierMask::ALT) != Key::NONE) {
  431. ie->set_alt_pressed(true);
  432. }
  433. if ((p_keycode & KeyModifierMask::CMD_OR_CTRL) != Key::NONE) {
  434. ie->set_command_or_control_autoremap(true);
  435. if ((p_keycode & KeyModifierMask::CTRL) != Key::NONE || (p_keycode & KeyModifierMask::META) != Key::NONE) {
  436. WARN_PRINT("Invalid Key Modifiers: Command or Control autoremapping is enabled, Meta and Control values are ignored!");
  437. }
  438. } else {
  439. if ((p_keycode & KeyModifierMask::CTRL) != Key::NONE) {
  440. ie->set_ctrl_pressed(true);
  441. }
  442. if ((p_keycode & KeyModifierMask::META) != Key::NONE) {
  443. ie->set_meta_pressed(true);
  444. }
  445. }
  446. return ie;
  447. }
  448. bool InputEventKey::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const {
  449. Ref<InputEventKey> key = p_event;
  450. if (key.is_null()) {
  451. return false;
  452. }
  453. bool match;
  454. if (keycode == Key::NONE && physical_keycode == Key::NONE && key_label != Key::NONE) {
  455. match = key_label == key->key_label;
  456. } else if (keycode != Key::NONE) {
  457. match = keycode == key->keycode;
  458. } else if (physical_keycode != Key::NONE) {
  459. match = physical_keycode == key->physical_keycode;
  460. if (location != KeyLocation::UNSPECIFIED) {
  461. match &= location == key->location;
  462. }
  463. } else {
  464. match = false;
  465. }
  466. Key action_mask = (Key)(int64_t)get_modifiers_mask();
  467. Key key_mask = (Key)(int64_t)key->get_modifiers_mask();
  468. if (key->is_pressed()) {
  469. match &= (action_mask & key_mask) == action_mask;
  470. }
  471. if (p_exact_match) {
  472. match &= action_mask == key_mask;
  473. }
  474. if (match) {
  475. bool key_pressed = key->is_pressed();
  476. if (r_pressed != nullptr) {
  477. *r_pressed = key_pressed;
  478. }
  479. float strength = key_pressed ? 1.0f : 0.0f;
  480. if (r_strength != nullptr) {
  481. *r_strength = strength;
  482. }
  483. if (r_raw_strength != nullptr) {
  484. *r_raw_strength = strength;
  485. }
  486. }
  487. return match;
  488. }
  489. bool InputEventKey::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
  490. Ref<InputEventKey> key = p_event;
  491. if (key.is_null()) {
  492. return false;
  493. }
  494. if (keycode == Key::NONE && physical_keycode == Key::NONE && key_label != Key::NONE) {
  495. return (key_label == key->key_label) &&
  496. (!p_exact_match || get_modifiers_mask() == key->get_modifiers_mask());
  497. } else if (keycode != Key::NONE) {
  498. return (keycode == key->keycode) &&
  499. (!p_exact_match || get_modifiers_mask() == key->get_modifiers_mask());
  500. } else if (physical_keycode != Key::NONE) {
  501. if (location != KeyLocation::UNSPECIFIED && location != key->location) {
  502. return false;
  503. }
  504. return (physical_keycode == key->physical_keycode) &&
  505. (!p_exact_match || get_modifiers_mask() == key->get_modifiers_mask());
  506. } else {
  507. return false;
  508. }
  509. }
  510. void InputEventKey::_bind_methods() {
  511. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventKey::set_pressed);
  512. ClassDB::bind_method(D_METHOD("set_keycode", "keycode"), &InputEventKey::set_keycode);
  513. ClassDB::bind_method(D_METHOD("get_keycode"), &InputEventKey::get_keycode);
  514. ClassDB::bind_method(D_METHOD("set_physical_keycode", "physical_keycode"), &InputEventKey::set_physical_keycode);
  515. ClassDB::bind_method(D_METHOD("get_physical_keycode"), &InputEventKey::get_physical_keycode);
  516. ClassDB::bind_method(D_METHOD("set_key_label", "key_label"), &InputEventKey::set_key_label);
  517. ClassDB::bind_method(D_METHOD("get_key_label"), &InputEventKey::get_key_label);
  518. ClassDB::bind_method(D_METHOD("set_unicode", "unicode"), &InputEventKey::set_unicode);
  519. ClassDB::bind_method(D_METHOD("get_unicode"), &InputEventKey::get_unicode);
  520. ClassDB::bind_method(D_METHOD("set_location", "location"), &InputEventKey::set_location);
  521. ClassDB::bind_method(D_METHOD("get_location"), &InputEventKey::get_location);
  522. ClassDB::bind_method(D_METHOD("set_echo", "echo"), &InputEventKey::set_echo);
  523. ClassDB::bind_method(D_METHOD("get_keycode_with_modifiers"), &InputEventKey::get_keycode_with_modifiers);
  524. ClassDB::bind_method(D_METHOD("get_physical_keycode_with_modifiers"), &InputEventKey::get_physical_keycode_with_modifiers);
  525. ClassDB::bind_method(D_METHOD("get_key_label_with_modifiers"), &InputEventKey::get_key_label_with_modifiers);
  526. ClassDB::bind_method(D_METHOD("as_text_keycode"), &InputEventKey::as_text_keycode);
  527. ClassDB::bind_method(D_METHOD("as_text_physical_keycode"), &InputEventKey::as_text_physical_keycode);
  528. ClassDB::bind_method(D_METHOD("as_text_key_label"), &InputEventKey::as_text_key_label);
  529. ClassDB::bind_method(D_METHOD("as_text_location"), &InputEventKey::as_text_location);
  530. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  531. ADD_PROPERTY(PropertyInfo(Variant::INT, "keycode"), "set_keycode", "get_keycode");
  532. ADD_PROPERTY(PropertyInfo(Variant::INT, "physical_keycode"), "set_physical_keycode", "get_physical_keycode");
  533. ADD_PROPERTY(PropertyInfo(Variant::INT, "key_label"), "set_key_label", "get_key_label");
  534. ADD_PROPERTY(PropertyInfo(Variant::INT, "unicode"), "set_unicode", "get_unicode");
  535. ADD_PROPERTY(PropertyInfo(Variant::INT, "location", PROPERTY_HINT_ENUM, "Unspecified,Left,Right"), "set_location", "get_location");
  536. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "echo"), "set_echo", "is_echo");
  537. }
  538. ///////////////////////////////////
  539. void InputEventMouse::set_button_mask(BitField<MouseButtonMask> p_mask) {
  540. button_mask = p_mask;
  541. emit_changed();
  542. }
  543. BitField<MouseButtonMask> InputEventMouse::get_button_mask() const {
  544. return button_mask;
  545. }
  546. void InputEventMouse::set_position(const Vector2 &p_pos) {
  547. pos = p_pos;
  548. }
  549. Vector2 InputEventMouse::get_position() const {
  550. return pos;
  551. }
  552. void InputEventMouse::set_global_position(const Vector2 &p_global_pos) {
  553. global_pos = p_global_pos;
  554. }
  555. Vector2 InputEventMouse::get_global_position() const {
  556. return global_pos;
  557. }
  558. void InputEventMouse::_bind_methods() {
  559. ClassDB::bind_method(D_METHOD("set_button_mask", "button_mask"), &InputEventMouse::set_button_mask);
  560. ClassDB::bind_method(D_METHOD("get_button_mask"), &InputEventMouse::get_button_mask);
  561. ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventMouse::set_position);
  562. ClassDB::bind_method(D_METHOD("get_position"), &InputEventMouse::get_position);
  563. ClassDB::bind_method(D_METHOD("set_global_position", "global_position"), &InputEventMouse::set_global_position);
  564. ClassDB::bind_method(D_METHOD("get_global_position"), &InputEventMouse::get_global_position);
  565. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask"), "set_button_mask", "get_button_mask");
  566. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position", PROPERTY_HINT_NONE, "suffix:px"), "set_position", "get_position");
  567. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_position", PROPERTY_HINT_NONE, "suffix:px"), "set_global_position", "get_global_position");
  568. }
  569. ///////////////////////////////////
  570. void InputEventMouseButton::set_factor(float p_factor) {
  571. factor = p_factor;
  572. }
  573. float InputEventMouseButton::get_factor() const {
  574. return factor;
  575. }
  576. void InputEventMouseButton::set_button_index(MouseButton p_index) {
  577. button_index = p_index;
  578. emit_changed();
  579. }
  580. MouseButton InputEventMouseButton::get_button_index() const {
  581. return button_index;
  582. }
  583. void InputEventMouseButton::set_pressed(bool p_pressed) {
  584. pressed = p_pressed;
  585. }
  586. void InputEventMouseButton::set_canceled(bool p_canceled) {
  587. canceled = p_canceled;
  588. }
  589. void InputEventMouseButton::set_double_click(bool p_double_click) {
  590. double_click = p_double_click;
  591. }
  592. bool InputEventMouseButton::is_double_click() const {
  593. return double_click;
  594. }
  595. Ref<InputEvent> InputEventMouseButton::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  596. Vector2 g = get_global_position();
  597. Vector2 l = p_xform.xform(get_position() + p_local_ofs);
  598. Ref<InputEventMouseButton> mb;
  599. mb.instantiate();
  600. mb->set_device(get_device());
  601. mb->set_window_id(get_window_id());
  602. mb->set_modifiers_from_event(this);
  603. mb->set_position(l);
  604. mb->set_global_position(g);
  605. mb->set_button_mask(get_button_mask());
  606. mb->set_pressed(pressed);
  607. mb->set_canceled(canceled);
  608. mb->set_double_click(double_click);
  609. mb->set_factor(factor);
  610. mb->set_button_index(button_index);
  611. return mb;
  612. }
  613. bool InputEventMouseButton::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const {
  614. Ref<InputEventMouseButton> mb = p_event;
  615. if (mb.is_null()) {
  616. return false;
  617. }
  618. bool match = button_index == mb->button_index;
  619. Key action_modifiers_mask = (Key)(int64_t)get_modifiers_mask();
  620. Key button_modifiers_mask = (Key)(int64_t)mb->get_modifiers_mask();
  621. if (mb->is_pressed()) {
  622. match &= (action_modifiers_mask & button_modifiers_mask) == action_modifiers_mask;
  623. }
  624. if (p_exact_match) {
  625. match &= action_modifiers_mask == button_modifiers_mask;
  626. }
  627. if (match) {
  628. bool mb_pressed = mb->is_pressed();
  629. if (r_pressed != nullptr) {
  630. *r_pressed = mb_pressed;
  631. }
  632. float strength = mb_pressed ? 1.0f : 0.0f;
  633. if (r_strength != nullptr) {
  634. *r_strength = strength;
  635. }
  636. if (r_raw_strength != nullptr) {
  637. *r_raw_strength = strength;
  638. }
  639. }
  640. return match;
  641. }
  642. bool InputEventMouseButton::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
  643. Ref<InputEventMouseButton> mb = p_event;
  644. if (mb.is_null()) {
  645. return false;
  646. }
  647. return button_index == mb->button_index &&
  648. (!p_exact_match || get_modifiers_mask() == mb->get_modifiers_mask());
  649. }
  650. static const char *_mouse_button_descriptions[9] = {
  651. TTRC("Left Mouse Button"),
  652. TTRC("Right Mouse Button"),
  653. TTRC("Middle Mouse Button"),
  654. TTRC("Mouse Wheel Up"),
  655. TTRC("Mouse Wheel Down"),
  656. TTRC("Mouse Wheel Left"),
  657. TTRC("Mouse Wheel Right"),
  658. TTRC("Mouse Thumb Button 1"),
  659. TTRC("Mouse Thumb Button 2")
  660. };
  661. String InputEventMouseButton::as_text() const {
  662. // Modifiers
  663. String mods_text = InputEventWithModifiers::as_text();
  664. String full_string = mods_text.is_empty() ? "" : mods_text + "+";
  665. // Button
  666. MouseButton idx = get_button_index();
  667. switch (idx) {
  668. case MouseButton::LEFT:
  669. case MouseButton::RIGHT:
  670. case MouseButton::MIDDLE:
  671. case MouseButton::WHEEL_UP:
  672. case MouseButton::WHEEL_DOWN:
  673. case MouseButton::WHEEL_LEFT:
  674. case MouseButton::WHEEL_RIGHT:
  675. case MouseButton::MB_XBUTTON1:
  676. case MouseButton::MB_XBUTTON2:
  677. full_string += RTR(_mouse_button_descriptions[(size_t)idx - 1]); // button index starts from 1, array index starts from 0, so subtract 1
  678. break;
  679. default:
  680. full_string += RTR("Button") + " #" + itos((int64_t)idx);
  681. break;
  682. }
  683. // Double Click
  684. if (double_click) {
  685. full_string += " (" + RTR("Double Click") + ")";
  686. }
  687. return full_string;
  688. }
  689. String InputEventMouseButton::to_string() {
  690. String p = is_pressed() ? "true" : "false";
  691. String canceled_state = is_canceled() ? "true" : "false";
  692. String d = double_click ? "true" : "false";
  693. MouseButton idx = get_button_index();
  694. String button_string = itos((int64_t)idx);
  695. switch (idx) {
  696. case MouseButton::LEFT:
  697. case MouseButton::RIGHT:
  698. case MouseButton::MIDDLE:
  699. case MouseButton::WHEEL_UP:
  700. case MouseButton::WHEEL_DOWN:
  701. case MouseButton::WHEEL_LEFT:
  702. case MouseButton::WHEEL_RIGHT:
  703. case MouseButton::MB_XBUTTON1:
  704. case MouseButton::MB_XBUTTON2:
  705. button_string += vformat(" (%s)", TTRGET(_mouse_button_descriptions[(size_t)idx - 1])); // button index starts from 1, array index starts from 0, so subtract 1
  706. break;
  707. default:
  708. break;
  709. }
  710. String mods = InputEventWithModifiers::as_text();
  711. mods = mods.is_empty() ? "none" : mods;
  712. // Work around the fact vformat can only take 5 substitutions but 6 need to be passed.
  713. String index_and_mods = vformat("button_index=%s, mods=%s", button_index, mods);
  714. return vformat("InputEventMouseButton: %s, pressed=%s, canceled=%s, position=(%s), button_mask=%d, double_click=%s", index_and_mods, p, canceled_state, String(get_position()), get_button_mask(), d);
  715. }
  716. void InputEventMouseButton::_bind_methods() {
  717. ClassDB::bind_method(D_METHOD("set_factor", "factor"), &InputEventMouseButton::set_factor);
  718. ClassDB::bind_method(D_METHOD("get_factor"), &InputEventMouseButton::get_factor);
  719. ClassDB::bind_method(D_METHOD("set_button_index", "button_index"), &InputEventMouseButton::set_button_index);
  720. ClassDB::bind_method(D_METHOD("get_button_index"), &InputEventMouseButton::get_button_index);
  721. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventMouseButton::set_pressed);
  722. ClassDB::bind_method(D_METHOD("set_canceled", "canceled"), &InputEventMouseButton::set_canceled);
  723. ClassDB::bind_method(D_METHOD("set_double_click", "double_click"), &InputEventMouseButton::set_double_click);
  724. ClassDB::bind_method(D_METHOD("is_double_click"), &InputEventMouseButton::is_double_click);
  725. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "factor"), "set_factor", "get_factor");
  726. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_index"), "set_button_index", "get_button_index");
  727. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "canceled"), "set_canceled", "is_canceled");
  728. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  729. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "double_click"), "set_double_click", "is_double_click");
  730. }
  731. ///////////////////////////////////
  732. void InputEventMouseMotion::set_tilt(const Vector2 &p_tilt) {
  733. tilt = p_tilt;
  734. }
  735. Vector2 InputEventMouseMotion::get_tilt() const {
  736. return tilt;
  737. }
  738. void InputEventMouseMotion::set_pressure(float p_pressure) {
  739. pressure = p_pressure;
  740. }
  741. float InputEventMouseMotion::get_pressure() const {
  742. return pressure;
  743. }
  744. void InputEventMouseMotion::set_pen_inverted(bool p_inverted) {
  745. pen_inverted = p_inverted;
  746. }
  747. bool InputEventMouseMotion::get_pen_inverted() const {
  748. return pen_inverted;
  749. }
  750. void InputEventMouseMotion::set_relative(const Vector2 &p_relative) {
  751. relative = p_relative;
  752. }
  753. Vector2 InputEventMouseMotion::get_relative() const {
  754. return relative;
  755. }
  756. void InputEventMouseMotion::set_relative_screen_position(const Vector2 &p_relative) {
  757. screen_relative = p_relative;
  758. }
  759. Vector2 InputEventMouseMotion::get_relative_screen_position() const {
  760. return screen_relative;
  761. }
  762. void InputEventMouseMotion::set_velocity(const Vector2 &p_velocity) {
  763. velocity = p_velocity;
  764. }
  765. Vector2 InputEventMouseMotion::get_velocity() const {
  766. return velocity;
  767. }
  768. void InputEventMouseMotion::set_screen_velocity(const Vector2 &p_velocity) {
  769. screen_velocity = p_velocity;
  770. }
  771. Vector2 InputEventMouseMotion::get_screen_velocity() const {
  772. return screen_velocity;
  773. }
  774. Ref<InputEvent> InputEventMouseMotion::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  775. Ref<InputEventMouseMotion> mm;
  776. mm.instantiate();
  777. mm->set_device(get_device());
  778. mm->set_window_id(get_window_id());
  779. mm->set_modifiers_from_event(this);
  780. mm->set_position(p_xform.xform(get_position() + p_local_ofs));
  781. mm->set_pressure(get_pressure());
  782. mm->set_pen_inverted(get_pen_inverted());
  783. mm->set_tilt(get_tilt());
  784. mm->set_global_position(get_global_position());
  785. mm->set_button_mask(get_button_mask());
  786. mm->set_relative(p_xform.basis_xform(get_relative()));
  787. mm->set_relative_screen_position(get_relative_screen_position());
  788. mm->set_velocity(p_xform.basis_xform(get_velocity()));
  789. mm->set_screen_velocity(get_screen_velocity());
  790. return mm;
  791. }
  792. String InputEventMouseMotion::as_text() const {
  793. return vformat(RTR("Mouse motion at position (%s) with velocity (%s)"), String(get_position()), String(get_velocity()));
  794. }
  795. String InputEventMouseMotion::to_string() {
  796. BitField<MouseButtonMask> mouse_button_mask = get_button_mask();
  797. String button_mask_string = itos((int64_t)mouse_button_mask);
  798. if (mouse_button_mask.has_flag(MouseButtonMask::LEFT)) {
  799. button_mask_string += vformat(" (%s)", TTRGET(_mouse_button_descriptions[(size_t)MouseButton::LEFT - 1]));
  800. }
  801. if (mouse_button_mask.has_flag(MouseButtonMask::MIDDLE)) {
  802. button_mask_string += vformat(" (%s)", TTRGET(_mouse_button_descriptions[(size_t)MouseButton::MIDDLE - 1]));
  803. }
  804. if (mouse_button_mask.has_flag(MouseButtonMask::RIGHT)) {
  805. button_mask_string += vformat(" (%s)", TTRGET(_mouse_button_descriptions[(size_t)MouseButton::RIGHT - 1]));
  806. }
  807. if (mouse_button_mask.has_flag(MouseButtonMask::MB_XBUTTON1)) {
  808. button_mask_string += vformat(" (%s)", TTRGET(_mouse_button_descriptions[(size_t)MouseButton::MB_XBUTTON1 - 1]));
  809. }
  810. if (mouse_button_mask.has_flag(MouseButtonMask::MB_XBUTTON2)) {
  811. button_mask_string += vformat(" (%s)", TTRGET(_mouse_button_descriptions[(size_t)MouseButton::MB_XBUTTON2 - 1]));
  812. }
  813. // Work around the fact vformat can only take 5 substitutions but 7 need to be passed.
  814. String mask_and_position_and_relative = vformat("button_mask=%s, position=(%s), relative=(%s)", button_mask_string, String(get_position()), String(get_relative()));
  815. return vformat("InputEventMouseMotion: %s, velocity=(%s), pressure=%.2f, tilt=(%s), pen_inverted=(%s)", mask_and_position_and_relative, String(get_velocity()), get_pressure(), String(get_tilt()), get_pen_inverted());
  816. }
  817. bool InputEventMouseMotion::accumulate(const Ref<InputEvent> &p_event) {
  818. Ref<InputEventMouseMotion> motion = p_event;
  819. if (motion.is_null()) {
  820. return false;
  821. }
  822. if (get_window_id() != motion->get_window_id()) {
  823. return false;
  824. }
  825. if (is_canceled() != motion->is_canceled()) {
  826. return false;
  827. }
  828. if (is_pressed() != motion->is_pressed()) {
  829. return false;
  830. }
  831. if (get_button_mask() != motion->get_button_mask()) {
  832. return false;
  833. }
  834. if (is_shift_pressed() != motion->is_shift_pressed()) {
  835. return false;
  836. }
  837. if (is_ctrl_pressed() != motion->is_ctrl_pressed()) {
  838. return false;
  839. }
  840. if (is_alt_pressed() != motion->is_alt_pressed()) {
  841. return false;
  842. }
  843. if (is_meta_pressed() != motion->is_meta_pressed()) {
  844. return false;
  845. }
  846. set_position(motion->get_position());
  847. set_global_position(motion->get_global_position());
  848. set_velocity(motion->get_velocity());
  849. set_screen_velocity(motion->get_screen_velocity());
  850. relative += motion->get_relative();
  851. screen_relative += motion->get_relative_screen_position();
  852. return true;
  853. }
  854. void InputEventMouseMotion::_bind_methods() {
  855. ClassDB::bind_method(D_METHOD("set_tilt", "tilt"), &InputEventMouseMotion::set_tilt);
  856. ClassDB::bind_method(D_METHOD("get_tilt"), &InputEventMouseMotion::get_tilt);
  857. ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventMouseMotion::set_pressure);
  858. ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventMouseMotion::get_pressure);
  859. ClassDB::bind_method(D_METHOD("set_pen_inverted", "pen_inverted"), &InputEventMouseMotion::set_pen_inverted);
  860. ClassDB::bind_method(D_METHOD("get_pen_inverted"), &InputEventMouseMotion::get_pen_inverted);
  861. ClassDB::bind_method(D_METHOD("set_relative", "relative"), &InputEventMouseMotion::set_relative);
  862. ClassDB::bind_method(D_METHOD("get_relative"), &InputEventMouseMotion::get_relative);
  863. ClassDB::bind_method(D_METHOD("set_screen_relative", "relative"), &InputEventMouseMotion::set_relative_screen_position);
  864. ClassDB::bind_method(D_METHOD("get_screen_relative"), &InputEventMouseMotion::get_relative_screen_position);
  865. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &InputEventMouseMotion::set_velocity);
  866. ClassDB::bind_method(D_METHOD("get_velocity"), &InputEventMouseMotion::get_velocity);
  867. ClassDB::bind_method(D_METHOD("set_screen_velocity", "velocity"), &InputEventMouseMotion::set_screen_velocity);
  868. ClassDB::bind_method(D_METHOD("get_screen_velocity"), &InputEventMouseMotion::get_screen_velocity);
  869. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "tilt"), "set_tilt", "get_tilt");
  870. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pressure"), "set_pressure", "get_pressure");
  871. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pen_inverted"), "set_pen_inverted", "get_pen_inverted");
  872. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "relative", PROPERTY_HINT_NONE, "suffix:px"), "set_relative", "get_relative");
  873. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "screen_relative", PROPERTY_HINT_NONE, "suffix:px"), "set_screen_relative", "get_screen_relative");
  874. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "velocity", PROPERTY_HINT_NONE, "suffix:px/s"), "set_velocity", "get_velocity");
  875. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "screen_velocity", PROPERTY_HINT_NONE, "suffix:px/s"), "set_screen_velocity", "get_screen_velocity");
  876. }
  877. ///////////////////////////////////
  878. void InputEventJoypadMotion::set_axis(JoyAxis p_axis) {
  879. ERR_FAIL_COND(p_axis < JoyAxis::LEFT_X || p_axis > JoyAxis::MAX);
  880. axis = p_axis;
  881. emit_changed();
  882. }
  883. JoyAxis InputEventJoypadMotion::get_axis() const {
  884. return axis;
  885. }
  886. void InputEventJoypadMotion::set_axis_value(float p_value) {
  887. axis_value = p_value;
  888. pressed = Math::abs(axis_value) >= 0.5f;
  889. emit_changed();
  890. }
  891. float InputEventJoypadMotion::get_axis_value() const {
  892. return axis_value;
  893. }
  894. bool InputEventJoypadMotion::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const {
  895. Ref<InputEventJoypadMotion> jm = p_event;
  896. if (jm.is_null()) {
  897. return false;
  898. }
  899. // Matches even if not in the same direction, but returns a "not pressed" event.
  900. bool match = axis == jm->axis;
  901. if (p_exact_match) {
  902. match &= (axis_value < 0) == (jm->axis_value < 0);
  903. }
  904. if (match) {
  905. float jm_abs_axis_value = Math::abs(jm->get_axis_value());
  906. bool same_direction = (((axis_value < 0) == (jm->axis_value < 0)) || jm->axis_value == 0);
  907. bool pressed_state = same_direction && jm_abs_axis_value >= p_deadzone;
  908. if (r_pressed != nullptr) {
  909. *r_pressed = pressed_state;
  910. }
  911. if (r_strength != nullptr) {
  912. if (pressed_state) {
  913. if (p_deadzone == 1.0f) {
  914. *r_strength = 1.0f;
  915. } else {
  916. *r_strength = CLAMP(Math::inverse_lerp(p_deadzone, 1.0f, jm_abs_axis_value), 0.0f, 1.0f);
  917. }
  918. } else {
  919. *r_strength = 0.0f;
  920. }
  921. }
  922. if (r_raw_strength != nullptr) {
  923. if (same_direction) { // NOT pressed, because we want to ignore the deadzone.
  924. *r_raw_strength = jm_abs_axis_value;
  925. } else {
  926. *r_raw_strength = 0.0f;
  927. }
  928. }
  929. }
  930. return match;
  931. }
  932. bool InputEventJoypadMotion::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
  933. Ref<InputEventJoypadMotion> jm = p_event;
  934. if (jm.is_null()) {
  935. return false;
  936. }
  937. return axis == jm->axis &&
  938. (!p_exact_match || ((axis_value < 0) == (jm->axis_value < 0)));
  939. }
  940. static const char *_joy_axis_descriptions[(size_t)JoyAxis::MAX] = {
  941. TTRC("Left Stick X-Axis, Joystick 0 X-Axis"),
  942. TTRC("Left Stick Y-Axis, Joystick 0 Y-Axis"),
  943. TTRC("Right Stick X-Axis, Joystick 1 X-Axis"),
  944. TTRC("Right Stick Y-Axis, Joystick 1 Y-Axis"),
  945. TTRC("Joystick 2 X-Axis, Left Trigger, Sony L2, Xbox LT"),
  946. TTRC("Joystick 2 Y-Axis, Right Trigger, Sony R2, Xbox RT"),
  947. TTRC("Joystick 3 X-Axis"),
  948. TTRC("Joystick 3 Y-Axis"),
  949. TTRC("Joystick 4 X-Axis"),
  950. TTRC("Joystick 4 Y-Axis"),
  951. };
  952. String InputEventJoypadMotion::as_text() const {
  953. String desc = axis < JoyAxis::MAX ? TTRGET(_joy_axis_descriptions[(size_t)axis]) : RTR("Unknown Joypad Axis");
  954. return vformat(RTR("Joypad Motion on Axis %d (%s) with Value %.2f"), axis, desc, axis_value);
  955. }
  956. String InputEventJoypadMotion::to_string() {
  957. return vformat("InputEventJoypadMotion: axis=%d, axis_value=%.2f", axis, axis_value);
  958. }
  959. Ref<InputEventJoypadMotion> InputEventJoypadMotion::create_reference(JoyAxis p_axis, float p_value) {
  960. Ref<InputEventJoypadMotion> ie;
  961. ie.instantiate();
  962. ie->set_axis(p_axis);
  963. ie->set_axis_value(p_value);
  964. return ie;
  965. }
  966. void InputEventJoypadMotion::_bind_methods() {
  967. ClassDB::bind_method(D_METHOD("set_axis", "axis"), &InputEventJoypadMotion::set_axis);
  968. ClassDB::bind_method(D_METHOD("get_axis"), &InputEventJoypadMotion::get_axis);
  969. ClassDB::bind_method(D_METHOD("set_axis_value", "axis_value"), &InputEventJoypadMotion::set_axis_value);
  970. ClassDB::bind_method(D_METHOD("get_axis_value"), &InputEventJoypadMotion::get_axis_value);
  971. ADD_PROPERTY(PropertyInfo(Variant::INT, "axis"), "set_axis", "get_axis");
  972. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "axis_value"), "set_axis_value", "get_axis_value");
  973. }
  974. ///////////////////////////////////
  975. void InputEventJoypadButton::set_button_index(JoyButton p_index) {
  976. button_index = p_index;
  977. emit_changed();
  978. }
  979. JoyButton InputEventJoypadButton::get_button_index() const {
  980. return button_index;
  981. }
  982. void InputEventJoypadButton::set_pressed(bool p_pressed) {
  983. pressed = p_pressed;
  984. }
  985. void InputEventJoypadButton::set_pressure(float p_pressure) {
  986. pressure = p_pressure;
  987. }
  988. float InputEventJoypadButton::get_pressure() const {
  989. return pressure;
  990. }
  991. bool InputEventJoypadButton::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const {
  992. Ref<InputEventJoypadButton> jb = p_event;
  993. if (jb.is_null()) {
  994. return false;
  995. }
  996. bool match = button_index == jb->button_index;
  997. if (match) {
  998. bool jb_pressed = jb->is_pressed();
  999. if (r_pressed != nullptr) {
  1000. *r_pressed = jb_pressed;
  1001. }
  1002. float strength = jb_pressed ? 1.0f : 0.0f;
  1003. if (r_strength != nullptr) {
  1004. *r_strength = strength;
  1005. }
  1006. if (r_raw_strength != nullptr) {
  1007. *r_raw_strength = strength;
  1008. }
  1009. }
  1010. return match;
  1011. }
  1012. bool InputEventJoypadButton::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
  1013. Ref<InputEventJoypadButton> button = p_event;
  1014. if (button.is_null()) {
  1015. return false;
  1016. }
  1017. return button_index == button->button_index;
  1018. }
  1019. static const char *_joy_button_descriptions[(size_t)JoyButton::SDL_MAX] = {
  1020. TTRC("Bottom Action, Sony Cross, Xbox A, Nintendo B"),
  1021. TTRC("Right Action, Sony Circle, Xbox B, Nintendo A"),
  1022. TTRC("Left Action, Sony Square, Xbox X, Nintendo Y"),
  1023. TTRC("Top Action, Sony Triangle, Xbox Y, Nintendo X"),
  1024. TTRC("Back, Sony Select, Xbox Back, Nintendo -"),
  1025. TTRC("Guide, Sony PS, Xbox Home"),
  1026. TTRC("Start, Xbox Menu, Nintendo +"),
  1027. TTRC("Left Stick, Sony L3, Xbox L/LS"),
  1028. TTRC("Right Stick, Sony R3, Xbox R/RS"),
  1029. TTRC("Left Shoulder, Sony L1, Xbox LB"),
  1030. TTRC("Right Shoulder, Sony R1, Xbox RB"),
  1031. TTRC("D-pad Up"),
  1032. TTRC("D-pad Down"),
  1033. TTRC("D-pad Left"),
  1034. TTRC("D-pad Right"),
  1035. TTRC("Xbox Share, PS5 Microphone, Nintendo Capture"),
  1036. TTRC("Xbox Paddle 1"),
  1037. TTRC("Xbox Paddle 2"),
  1038. TTRC("Xbox Paddle 3"),
  1039. TTRC("Xbox Paddle 4"),
  1040. TTRC("PS4/5 Touchpad"),
  1041. };
  1042. String InputEventJoypadButton::as_text() const {
  1043. String text = vformat(RTR("Joypad Button %d"), (int64_t)button_index);
  1044. if (button_index > JoyButton::INVALID && button_index < JoyButton::SDL_MAX) {
  1045. text += vformat(" (%s)", TTRGET(_joy_button_descriptions[(size_t)button_index]));
  1046. }
  1047. if (pressure != 0) {
  1048. text += ", " + RTR("Pressure:") + " " + String(Variant(pressure));
  1049. }
  1050. return text;
  1051. }
  1052. String InputEventJoypadButton::to_string() {
  1053. String p = is_pressed() ? "true" : "false";
  1054. return vformat("InputEventJoypadButton: button_index=%d, pressed=%s, pressure=%.2f", button_index, p, pressure);
  1055. }
  1056. Ref<InputEventJoypadButton> InputEventJoypadButton::create_reference(JoyButton p_btn_index) {
  1057. Ref<InputEventJoypadButton> ie;
  1058. ie.instantiate();
  1059. ie->set_button_index(p_btn_index);
  1060. return ie;
  1061. }
  1062. void InputEventJoypadButton::_bind_methods() {
  1063. ClassDB::bind_method(D_METHOD("set_button_index", "button_index"), &InputEventJoypadButton::set_button_index);
  1064. ClassDB::bind_method(D_METHOD("get_button_index"), &InputEventJoypadButton::get_button_index);
  1065. ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventJoypadButton::set_pressure);
  1066. ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventJoypadButton::get_pressure);
  1067. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventJoypadButton::set_pressed);
  1068. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_index"), "set_button_index", "get_button_index");
  1069. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pressure"), "set_pressure", "get_pressure");
  1070. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  1071. }
  1072. ///////////////////////////////////
  1073. void InputEventScreenTouch::set_index(int p_index) {
  1074. index = p_index;
  1075. }
  1076. int InputEventScreenTouch::get_index() const {
  1077. return index;
  1078. }
  1079. void InputEventScreenTouch::set_position(const Vector2 &p_pos) {
  1080. pos = p_pos;
  1081. }
  1082. Vector2 InputEventScreenTouch::get_position() const {
  1083. return pos;
  1084. }
  1085. void InputEventScreenTouch::set_pressed(bool p_pressed) {
  1086. pressed = p_pressed;
  1087. }
  1088. void InputEventScreenTouch::set_canceled(bool p_canceled) {
  1089. canceled = p_canceled;
  1090. }
  1091. void InputEventScreenTouch::set_double_tap(bool p_double_tap) {
  1092. double_tap = p_double_tap;
  1093. }
  1094. bool InputEventScreenTouch::is_double_tap() const {
  1095. return double_tap;
  1096. }
  1097. Ref<InputEvent> InputEventScreenTouch::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  1098. Ref<InputEventScreenTouch> st;
  1099. st.instantiate();
  1100. st->set_device(get_device());
  1101. st->set_window_id(get_window_id());
  1102. st->set_index(index);
  1103. st->set_position(p_xform.xform(pos + p_local_ofs));
  1104. st->set_pressed(pressed);
  1105. st->set_canceled(canceled);
  1106. st->set_double_tap(double_tap);
  1107. return st;
  1108. }
  1109. String InputEventScreenTouch::as_text() const {
  1110. String status = canceled ? RTR("canceled") : (pressed ? RTR("touched") : RTR("released"));
  1111. return vformat(RTR("Screen %s at (%s) with %s touch points"), status, String(get_position()), itos(index));
  1112. }
  1113. String InputEventScreenTouch::to_string() {
  1114. String p = pressed ? "true" : "false";
  1115. String canceled_state = canceled ? "true" : "false";
  1116. String double_tap_string = double_tap ? "true" : "false";
  1117. return vformat("InputEventScreenTouch: index=%d, pressed=%s, canceled=%s, position=(%s), double_tap=%s", index, p, canceled_state, String(get_position()), double_tap_string);
  1118. }
  1119. void InputEventScreenTouch::_bind_methods() {
  1120. ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenTouch::set_index);
  1121. ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenTouch::get_index);
  1122. ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventScreenTouch::set_position);
  1123. ClassDB::bind_method(D_METHOD("get_position"), &InputEventScreenTouch::get_position);
  1124. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventScreenTouch::set_pressed);
  1125. ClassDB::bind_method(D_METHOD("set_canceled", "canceled"), &InputEventScreenTouch::set_canceled);
  1126. ClassDB::bind_method(D_METHOD("set_double_tap", "double_tap"), &InputEventScreenTouch::set_double_tap);
  1127. ClassDB::bind_method(D_METHOD("is_double_tap"), &InputEventScreenTouch::is_double_tap);
  1128. ADD_PROPERTY(PropertyInfo(Variant::INT, "index"), "set_index", "get_index");
  1129. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position", PROPERTY_HINT_NONE, "suffix:px"), "set_position", "get_position");
  1130. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "canceled"), "set_canceled", "is_canceled");
  1131. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  1132. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "double_tap"), "set_double_tap", "is_double_tap");
  1133. }
  1134. ///////////////////////////////////
  1135. void InputEventScreenDrag::set_index(int p_index) {
  1136. index = p_index;
  1137. }
  1138. int InputEventScreenDrag::get_index() const {
  1139. return index;
  1140. }
  1141. void InputEventScreenDrag::set_tilt(const Vector2 &p_tilt) {
  1142. tilt = p_tilt;
  1143. }
  1144. Vector2 InputEventScreenDrag::get_tilt() const {
  1145. return tilt;
  1146. }
  1147. void InputEventScreenDrag::set_pressure(float p_pressure) {
  1148. pressure = p_pressure;
  1149. }
  1150. float InputEventScreenDrag::get_pressure() const {
  1151. return pressure;
  1152. }
  1153. void InputEventScreenDrag::set_pen_inverted(bool p_inverted) {
  1154. pen_inverted = p_inverted;
  1155. }
  1156. bool InputEventScreenDrag::get_pen_inverted() const {
  1157. return pen_inverted;
  1158. }
  1159. void InputEventScreenDrag::set_position(const Vector2 &p_pos) {
  1160. pos = p_pos;
  1161. }
  1162. Vector2 InputEventScreenDrag::get_position() const {
  1163. return pos;
  1164. }
  1165. void InputEventScreenDrag::set_relative(const Vector2 &p_relative) {
  1166. relative = p_relative;
  1167. }
  1168. Vector2 InputEventScreenDrag::get_relative() const {
  1169. return relative;
  1170. }
  1171. void InputEventScreenDrag::set_relative_screen_position(const Vector2 &p_relative) {
  1172. screen_relative = p_relative;
  1173. }
  1174. Vector2 InputEventScreenDrag::get_relative_screen_position() const {
  1175. return screen_relative;
  1176. }
  1177. void InputEventScreenDrag::set_velocity(const Vector2 &p_velocity) {
  1178. velocity = p_velocity;
  1179. }
  1180. Vector2 InputEventScreenDrag::get_velocity() const {
  1181. return velocity;
  1182. }
  1183. void InputEventScreenDrag::set_screen_velocity(const Vector2 &p_velocity) {
  1184. screen_velocity = p_velocity;
  1185. }
  1186. Vector2 InputEventScreenDrag::get_screen_velocity() const {
  1187. return screen_velocity;
  1188. }
  1189. Ref<InputEvent> InputEventScreenDrag::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  1190. Ref<InputEventScreenDrag> sd;
  1191. sd.instantiate();
  1192. sd->set_device(get_device());
  1193. sd->set_window_id(get_window_id());
  1194. sd->set_index(index);
  1195. sd->set_pressure(get_pressure());
  1196. sd->set_pen_inverted(get_pen_inverted());
  1197. sd->set_tilt(get_tilt());
  1198. sd->set_position(p_xform.xform(pos + p_local_ofs));
  1199. sd->set_relative(p_xform.basis_xform(relative));
  1200. sd->set_relative_screen_position(get_relative_screen_position());
  1201. sd->set_velocity(p_xform.basis_xform(velocity));
  1202. sd->set_screen_velocity(get_screen_velocity());
  1203. return sd;
  1204. }
  1205. String InputEventScreenDrag::as_text() const {
  1206. return vformat(RTR("Screen dragged with %s touch points at position (%s) with velocity of (%s)"), itos(index), String(get_position()), String(get_velocity()));
  1207. }
  1208. String InputEventScreenDrag::to_string() {
  1209. return vformat("InputEventScreenDrag: index=%d, position=(%s), relative=(%s), velocity=(%s), pressure=%.2f, tilt=(%s), pen_inverted=(%s)", index, String(get_position()), String(get_relative()), String(get_velocity()), get_pressure(), String(get_tilt()), get_pen_inverted());
  1210. }
  1211. bool InputEventScreenDrag::accumulate(const Ref<InputEvent> &p_event) {
  1212. Ref<InputEventScreenDrag> drag = p_event;
  1213. if (drag.is_null()) {
  1214. return false;
  1215. }
  1216. if (get_index() != drag->get_index()) {
  1217. return false;
  1218. }
  1219. set_position(drag->get_position());
  1220. set_velocity(drag->get_velocity());
  1221. set_screen_velocity(drag->get_screen_velocity());
  1222. relative += drag->get_relative();
  1223. screen_relative += drag->get_relative_screen_position();
  1224. return true;
  1225. }
  1226. void InputEventScreenDrag::_bind_methods() {
  1227. ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenDrag::set_index);
  1228. ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenDrag::get_index);
  1229. ClassDB::bind_method(D_METHOD("set_tilt", "tilt"), &InputEventScreenDrag::set_tilt);
  1230. ClassDB::bind_method(D_METHOD("get_tilt"), &InputEventScreenDrag::get_tilt);
  1231. ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventScreenDrag::set_pressure);
  1232. ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventScreenDrag::get_pressure);
  1233. ClassDB::bind_method(D_METHOD("set_pen_inverted", "pen_inverted"), &InputEventScreenDrag::set_pen_inverted);
  1234. ClassDB::bind_method(D_METHOD("get_pen_inverted"), &InputEventScreenDrag::get_pen_inverted);
  1235. ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventScreenDrag::set_position);
  1236. ClassDB::bind_method(D_METHOD("get_position"), &InputEventScreenDrag::get_position);
  1237. ClassDB::bind_method(D_METHOD("set_relative", "relative"), &InputEventScreenDrag::set_relative);
  1238. ClassDB::bind_method(D_METHOD("get_relative"), &InputEventScreenDrag::get_relative);
  1239. ClassDB::bind_method(D_METHOD("set_screen_relative", "relative"), &InputEventScreenDrag::set_relative_screen_position);
  1240. ClassDB::bind_method(D_METHOD("get_screen_relative"), &InputEventScreenDrag::get_relative_screen_position);
  1241. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &InputEventScreenDrag::set_velocity);
  1242. ClassDB::bind_method(D_METHOD("get_velocity"), &InputEventScreenDrag::get_velocity);
  1243. ClassDB::bind_method(D_METHOD("set_screen_velocity", "velocity"), &InputEventScreenDrag::set_screen_velocity);
  1244. ClassDB::bind_method(D_METHOD("get_screen_velocity"), &InputEventScreenDrag::get_screen_velocity);
  1245. ADD_PROPERTY(PropertyInfo(Variant::INT, "index"), "set_index", "get_index");
  1246. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "tilt"), "set_tilt", "get_tilt");
  1247. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pressure"), "set_pressure", "get_pressure");
  1248. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pen_inverted"), "set_pen_inverted", "get_pen_inverted");
  1249. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position", PROPERTY_HINT_NONE, "suffix:px"), "set_position", "get_position");
  1250. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "relative", PROPERTY_HINT_NONE, "suffix:px"), "set_relative", "get_relative");
  1251. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "screen_relative", PROPERTY_HINT_NONE, "suffix:px"), "set_screen_relative", "get_screen_relative");
  1252. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "velocity", PROPERTY_HINT_NONE, "suffix:px/s"), "set_velocity", "get_velocity");
  1253. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "screen_velocity", PROPERTY_HINT_NONE, "suffix:px/s"), "set_screen_velocity", "get_screen_velocity");
  1254. }
  1255. ///////////////////////////////////
  1256. void InputEventAction::set_action(const StringName &p_action) {
  1257. action = p_action;
  1258. }
  1259. StringName InputEventAction::get_action() const {
  1260. return action;
  1261. }
  1262. void InputEventAction::set_pressed(bool p_pressed) {
  1263. pressed = p_pressed;
  1264. }
  1265. void InputEventAction::set_strength(float p_strength) {
  1266. strength = CLAMP(p_strength, 0.0f, 1.0f);
  1267. }
  1268. float InputEventAction::get_strength() const {
  1269. return strength;
  1270. }
  1271. bool InputEventAction::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
  1272. if (p_event.is_null()) {
  1273. return false;
  1274. }
  1275. return p_event->is_action(action, p_exact_match);
  1276. }
  1277. bool InputEventAction::is_action(const StringName &p_action) const {
  1278. return action == p_action;
  1279. }
  1280. bool InputEventAction::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const {
  1281. Ref<InputEventAction> act = p_event;
  1282. if (act.is_null()) {
  1283. return false;
  1284. }
  1285. bool match = action == act->action;
  1286. if (match) {
  1287. bool act_pressed = act->is_pressed();
  1288. if (r_pressed != nullptr) {
  1289. *r_pressed = act_pressed;
  1290. }
  1291. float act_strength = act_pressed ? 1.0f : 0.0f;
  1292. if (r_strength != nullptr) {
  1293. *r_strength = act_strength;
  1294. }
  1295. if (r_raw_strength != nullptr) {
  1296. *r_raw_strength = act_strength;
  1297. }
  1298. }
  1299. return match;
  1300. }
  1301. String InputEventAction::as_text() const {
  1302. const List<Ref<InputEvent>> *events = InputMap::get_singleton()->action_get_events(action);
  1303. if (!events) {
  1304. return String();
  1305. }
  1306. for (const Ref<InputEvent> &E : *events) {
  1307. if (E.is_valid()) {
  1308. return E->as_text();
  1309. }
  1310. }
  1311. return String();
  1312. }
  1313. String InputEventAction::to_string() {
  1314. String p = is_pressed() ? "true" : "false";
  1315. return vformat("InputEventAction: action=\"%s\", pressed=%s", action, p);
  1316. }
  1317. void InputEventAction::_bind_methods() {
  1318. ClassDB::bind_method(D_METHOD("set_action", "action"), &InputEventAction::set_action);
  1319. ClassDB::bind_method(D_METHOD("get_action"), &InputEventAction::get_action);
  1320. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventAction::set_pressed);
  1321. ClassDB::bind_method(D_METHOD("set_strength", "strength"), &InputEventAction::set_strength);
  1322. ClassDB::bind_method(D_METHOD("get_strength"), &InputEventAction::get_strength);
  1323. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "action"), "set_action", "get_action");
  1324. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  1325. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "strength", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_strength", "get_strength");
  1326. }
  1327. ///////////////////////////////////
  1328. void InputEventGesture::set_position(const Vector2 &p_pos) {
  1329. pos = p_pos;
  1330. }
  1331. void InputEventGesture::_bind_methods() {
  1332. ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventGesture::set_position);
  1333. ClassDB::bind_method(D_METHOD("get_position"), &InputEventGesture::get_position);
  1334. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position", PROPERTY_HINT_NONE, "suffix:px"), "set_position", "get_position");
  1335. }
  1336. Vector2 InputEventGesture::get_position() const {
  1337. return pos;
  1338. }
  1339. ///////////////////////////////////
  1340. void InputEventMagnifyGesture::set_factor(real_t p_factor) {
  1341. factor = p_factor;
  1342. }
  1343. real_t InputEventMagnifyGesture::get_factor() const {
  1344. return factor;
  1345. }
  1346. Ref<InputEvent> InputEventMagnifyGesture::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  1347. Ref<InputEventMagnifyGesture> ev;
  1348. ev.instantiate();
  1349. ev->set_device(get_device());
  1350. ev->set_window_id(get_window_id());
  1351. ev->set_modifiers_from_event(this);
  1352. ev->set_position(p_xform.xform(get_position() + p_local_ofs));
  1353. ev->set_factor(get_factor());
  1354. return ev;
  1355. }
  1356. String InputEventMagnifyGesture::as_text() const {
  1357. return vformat(RTR("Magnify Gesture at (%s) with factor %s"), String(get_position()), rtos(get_factor()));
  1358. }
  1359. String InputEventMagnifyGesture::to_string() {
  1360. return vformat("InputEventMagnifyGesture: factor=%.2f, position=(%s)", factor, String(get_position()));
  1361. }
  1362. void InputEventMagnifyGesture::_bind_methods() {
  1363. ClassDB::bind_method(D_METHOD("set_factor", "factor"), &InputEventMagnifyGesture::set_factor);
  1364. ClassDB::bind_method(D_METHOD("get_factor"), &InputEventMagnifyGesture::get_factor);
  1365. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "factor"), "set_factor", "get_factor");
  1366. }
  1367. ///////////////////////////////////
  1368. void InputEventPanGesture::set_delta(const Vector2 &p_delta) {
  1369. delta = p_delta;
  1370. }
  1371. Vector2 InputEventPanGesture::get_delta() const {
  1372. return delta;
  1373. }
  1374. Ref<InputEvent> InputEventPanGesture::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
  1375. Ref<InputEventPanGesture> ev;
  1376. ev.instantiate();
  1377. ev->set_device(get_device());
  1378. ev->set_window_id(get_window_id());
  1379. ev->set_modifiers_from_event(this);
  1380. ev->set_position(p_xform.xform(get_position() + p_local_ofs));
  1381. ev->set_delta(get_delta());
  1382. return ev;
  1383. }
  1384. String InputEventPanGesture::as_text() const {
  1385. return vformat(RTR("Pan Gesture at (%s) with delta (%s)"), String(get_position()), String(get_delta()));
  1386. }
  1387. String InputEventPanGesture::to_string() {
  1388. return vformat("InputEventPanGesture: delta=(%s), position=(%s)", String(get_delta()), String(get_position()));
  1389. }
  1390. void InputEventPanGesture::_bind_methods() {
  1391. ClassDB::bind_method(D_METHOD("set_delta", "delta"), &InputEventPanGesture::set_delta);
  1392. ClassDB::bind_method(D_METHOD("get_delta"), &InputEventPanGesture::get_delta);
  1393. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "delta"), "set_delta", "get_delta");
  1394. }
  1395. ///////////////////////////////////
  1396. void InputEventMIDI::set_channel(const int p_channel) {
  1397. channel = p_channel;
  1398. }
  1399. int InputEventMIDI::get_channel() const {
  1400. return channel;
  1401. }
  1402. void InputEventMIDI::set_message(const MIDIMessage p_message) {
  1403. message = p_message;
  1404. }
  1405. MIDIMessage InputEventMIDI::get_message() const {
  1406. return message;
  1407. }
  1408. void InputEventMIDI::set_pitch(const int p_pitch) {
  1409. pitch = p_pitch;
  1410. }
  1411. int InputEventMIDI::get_pitch() const {
  1412. return pitch;
  1413. }
  1414. void InputEventMIDI::set_velocity(const int p_velocity) {
  1415. velocity = p_velocity;
  1416. }
  1417. int InputEventMIDI::get_velocity() const {
  1418. return velocity;
  1419. }
  1420. void InputEventMIDI::set_instrument(const int p_instrument) {
  1421. instrument = p_instrument;
  1422. }
  1423. int InputEventMIDI::get_instrument() const {
  1424. return instrument;
  1425. }
  1426. void InputEventMIDI::set_pressure(const int p_pressure) {
  1427. pressure = p_pressure;
  1428. }
  1429. int InputEventMIDI::get_pressure() const {
  1430. return pressure;
  1431. }
  1432. void InputEventMIDI::set_controller_number(const int p_controller_number) {
  1433. controller_number = p_controller_number;
  1434. }
  1435. int InputEventMIDI::get_controller_number() const {
  1436. return controller_number;
  1437. }
  1438. void InputEventMIDI::set_controller_value(const int p_controller_value) {
  1439. controller_value = p_controller_value;
  1440. }
  1441. int InputEventMIDI::get_controller_value() const {
  1442. return controller_value;
  1443. }
  1444. String InputEventMIDI::as_text() const {
  1445. return vformat(RTR("MIDI Input on Channel=%s Message=%s"), itos(channel), itos((int64_t)message));
  1446. }
  1447. String InputEventMIDI::to_string() {
  1448. String ret;
  1449. switch (message) {
  1450. case MIDIMessage::NOTE_ON:
  1451. ret = vformat("Note On: channel=%d, pitch=%d, velocity=%d", channel, pitch, velocity);
  1452. break;
  1453. case MIDIMessage::NOTE_OFF:
  1454. ret = vformat("Note Off: channel=%d, pitch=%d, velocity=%d", channel, pitch, velocity);
  1455. break;
  1456. case MIDIMessage::PITCH_BEND:
  1457. ret = vformat("Pitch Bend: channel=%d, pitch=%d", channel, pitch);
  1458. break;
  1459. case MIDIMessage::CHANNEL_PRESSURE:
  1460. ret = vformat("Channel Pressure: channel=%d, pressure=%d", channel, pressure);
  1461. break;
  1462. case MIDIMessage::CONTROL_CHANGE:
  1463. ret = vformat("Control Change: channel=%d, controller_number=%d, controller_value=%d", channel, controller_number, controller_value);
  1464. break;
  1465. default:
  1466. ret = vformat("channel=%d, message=%d, pitch=%d, velocity=%d, pressure=%d, controller_number=%d, controller_value=%d, instrument=%d", channel, message, pitch, velocity, pressure, controller_number, controller_value, instrument);
  1467. }
  1468. return "InputEventMIDI: " + ret;
  1469. }
  1470. void InputEventMIDI::_bind_methods() {
  1471. ClassDB::bind_method(D_METHOD("set_channel", "channel"), &InputEventMIDI::set_channel);
  1472. ClassDB::bind_method(D_METHOD("get_channel"), &InputEventMIDI::get_channel);
  1473. ClassDB::bind_method(D_METHOD("set_message", "message"), &InputEventMIDI::set_message);
  1474. ClassDB::bind_method(D_METHOD("get_message"), &InputEventMIDI::get_message);
  1475. ClassDB::bind_method(D_METHOD("set_pitch", "pitch"), &InputEventMIDI::set_pitch);
  1476. ClassDB::bind_method(D_METHOD("get_pitch"), &InputEventMIDI::get_pitch);
  1477. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &InputEventMIDI::set_velocity);
  1478. ClassDB::bind_method(D_METHOD("get_velocity"), &InputEventMIDI::get_velocity);
  1479. ClassDB::bind_method(D_METHOD("set_instrument", "instrument"), &InputEventMIDI::set_instrument);
  1480. ClassDB::bind_method(D_METHOD("get_instrument"), &InputEventMIDI::get_instrument);
  1481. ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventMIDI::set_pressure);
  1482. ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventMIDI::get_pressure);
  1483. ClassDB::bind_method(D_METHOD("set_controller_number", "controller_number"), &InputEventMIDI::set_controller_number);
  1484. ClassDB::bind_method(D_METHOD("get_controller_number"), &InputEventMIDI::get_controller_number);
  1485. ClassDB::bind_method(D_METHOD("set_controller_value", "controller_value"), &InputEventMIDI::set_controller_value);
  1486. ClassDB::bind_method(D_METHOD("get_controller_value"), &InputEventMIDI::get_controller_value);
  1487. ADD_PROPERTY(PropertyInfo(Variant::INT, "channel"), "set_channel", "get_channel");
  1488. ADD_PROPERTY(PropertyInfo(Variant::INT, "message"), "set_message", "get_message");
  1489. ADD_PROPERTY(PropertyInfo(Variant::INT, "pitch"), "set_pitch", "get_pitch");
  1490. ADD_PROPERTY(PropertyInfo(Variant::INT, "velocity"), "set_velocity", "get_velocity");
  1491. ADD_PROPERTY(PropertyInfo(Variant::INT, "instrument"), "set_instrument", "get_instrument");
  1492. ADD_PROPERTY(PropertyInfo(Variant::INT, "pressure"), "set_pressure", "get_pressure");
  1493. ADD_PROPERTY(PropertyInfo(Variant::INT, "controller_number"), "set_controller_number", "get_controller_number");
  1494. ADD_PROPERTY(PropertyInfo(Variant::INT, "controller_value"), "set_controller_value", "get_controller_value");
  1495. }
  1496. ///////////////////////////////////
  1497. void InputEventShortcut::set_shortcut(Ref<Shortcut> p_shortcut) {
  1498. shortcut = p_shortcut;
  1499. emit_changed();
  1500. }
  1501. Ref<Shortcut> InputEventShortcut::get_shortcut() {
  1502. return shortcut;
  1503. }
  1504. void InputEventShortcut::_bind_methods() {
  1505. ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &InputEventShortcut::set_shortcut);
  1506. ClassDB::bind_method(D_METHOD("get_shortcut"), &InputEventShortcut::get_shortcut);
  1507. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "Shortcut"), "set_shortcut", "get_shortcut");
  1508. }
  1509. String InputEventShortcut::as_text() const {
  1510. ERR_FAIL_COND_V(shortcut.is_null(), "None");
  1511. return vformat(RTR("Input Event with Shortcut=%s"), shortcut->get_as_text());
  1512. }
  1513. String InputEventShortcut::to_string() {
  1514. ERR_FAIL_COND_V(shortcut.is_null(), "None");
  1515. return vformat("InputEventShortcut: shortcut=%s", shortcut->get_as_text());
  1516. }
  1517. InputEventShortcut::InputEventShortcut() {
  1518. pressed = true;
  1519. }