12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484 |
- /*************************************************************************/
- /* input_event.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
- /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- #include "input_event.h"
- #include "core/input/input_map.h"
- #include "core/os/keyboard.h"
- const int InputEvent::DEVICE_ID_TOUCH_MOUSE = -1;
- const int InputEvent::DEVICE_ID_INTERNAL = -2;
- void InputEvent::set_device(int p_device) {
- device = p_device;
- }
- int InputEvent::get_device() const {
- return device;
- }
- bool InputEvent::is_action(const StringName &p_action, bool p_exact_match) const {
- return InputMap::get_singleton()->event_is_action(Ref<InputEvent>((InputEvent *)this), p_action, p_exact_match);
- }
- bool InputEvent::is_action_pressed(const StringName &p_action, bool p_allow_echo, bool p_exact_match) const {
- bool pressed;
- bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>((InputEvent *)this), p_action, p_exact_match, &pressed, nullptr, nullptr);
- return valid && pressed && (p_allow_echo || !is_echo());
- }
- bool InputEvent::is_action_released(const StringName &p_action, bool p_exact_match) const {
- bool pressed;
- bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>((InputEvent *)this), p_action, p_exact_match, &pressed, nullptr, nullptr);
- return valid && !pressed;
- }
- float InputEvent::get_action_strength(const StringName &p_action, bool p_exact_match) const {
- float strength;
- bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>((InputEvent *)this), p_action, p_exact_match, nullptr, &strength, nullptr);
- return valid ? strength : 0.0f;
- }
- float InputEvent::get_action_raw_strength(const StringName &p_action, bool p_exact_match) const {
- float raw_strength;
- bool valid = InputMap::get_singleton()->event_get_action_status(Ref<InputEvent>((InputEvent *)this), p_action, p_exact_match, nullptr, nullptr, &raw_strength);
- return valid ? raw_strength : 0.0f;
- }
- bool InputEvent::is_pressed() const {
- return false;
- }
- bool InputEvent::is_echo() const {
- return false;
- }
- Ref<InputEvent> InputEvent::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
- return Ref<InputEvent>((InputEvent *)this);
- }
- bool InputEvent::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
- return false;
- }
- bool InputEvent::shortcut_match(const Ref<InputEvent> &p_event) const {
- return false;
- }
- bool InputEvent::is_action_type() const {
- return false;
- }
- void InputEvent::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_device", "device"), &InputEvent::set_device);
- ClassDB::bind_method(D_METHOD("get_device"), &InputEvent::get_device);
- ClassDB::bind_method(D_METHOD("is_action", "action", "exact_match"), &InputEvent::is_action, DEFVAL(false));
- ClassDB::bind_method(D_METHOD("is_action_pressed", "action", "allow_echo", "exact_match"), &InputEvent::is_action_pressed, DEFVAL(false), DEFVAL(false));
- ClassDB::bind_method(D_METHOD("is_action_released", "action", "exact_match"), &InputEvent::is_action_released, DEFVAL(false));
- ClassDB::bind_method(D_METHOD("get_action_strength", "action", "exact_match"), &InputEvent::get_action_strength, DEFVAL(false));
- ClassDB::bind_method(D_METHOD("is_pressed"), &InputEvent::is_pressed);
- ClassDB::bind_method(D_METHOD("is_echo"), &InputEvent::is_echo);
- ClassDB::bind_method(D_METHOD("as_text"), &InputEvent::as_text);
- ClassDB::bind_method(D_METHOD("shortcut_match", "event"), &InputEvent::shortcut_match);
- ClassDB::bind_method(D_METHOD("is_action_type"), &InputEvent::is_action_type);
- ClassDB::bind_method(D_METHOD("accumulate", "with_event"), &InputEvent::accumulate);
- ClassDB::bind_method(D_METHOD("xformed_by", "xform", "local_ofs"), &InputEvent::xformed_by, DEFVAL(Vector2()));
- ADD_PROPERTY(PropertyInfo(Variant::INT, "device"), "set_device", "get_device");
- }
- ///////////////////////////////////
- void InputEventFromWindow::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_window_id", "id"), &InputEventFromWindow::set_window_id);
- ClassDB::bind_method(D_METHOD("get_window_id"), &InputEventFromWindow::get_window_id);
- ADD_PROPERTY(PropertyInfo(Variant::INT, "window_id"), "set_window_id", "get_window_id");
- }
- void InputEventFromWindow::set_window_id(int64_t p_id) {
- window_id = p_id;
- }
- int64_t InputEventFromWindow::get_window_id() const {
- return window_id;
- }
- ///////////////////////////////////
- void InputEventWithModifiers::set_store_command(bool p_enabled) {
- store_command = p_enabled;
- }
- bool InputEventWithModifiers::is_storing_command() const {
- return store_command;
- }
- void InputEventWithModifiers::set_shift(bool p_enabled) {
- shift = p_enabled;
- }
- bool InputEventWithModifiers::get_shift() const {
- return shift;
- }
- void InputEventWithModifiers::set_alt(bool p_enabled) {
- alt = p_enabled;
- }
- bool InputEventWithModifiers::get_alt() const {
- return alt;
- }
- void InputEventWithModifiers::set_control(bool p_enabled) {
- control = p_enabled;
- }
- bool InputEventWithModifiers::get_control() const {
- return control;
- }
- void InputEventWithModifiers::set_metakey(bool p_enabled) {
- meta = p_enabled;
- }
- bool InputEventWithModifiers::get_metakey() const {
- return meta;
- }
- void InputEventWithModifiers::set_command(bool p_enabled) {
- command = p_enabled;
- }
- bool InputEventWithModifiers::get_command() const {
- return command;
- }
- void InputEventWithModifiers::set_modifiers_from_event(const InputEventWithModifiers *event) {
- set_alt(event->get_alt());
- set_shift(event->get_shift());
- set_control(event->get_control());
- set_metakey(event->get_metakey());
- }
- String InputEventWithModifiers::as_text() const {
- Vector<String> mod_names;
- if (get_control()) {
- mod_names.push_back(find_keycode_name(KEY_CONTROL));
- }
- if (get_shift()) {
- mod_names.push_back(find_keycode_name(KEY_SHIFT));
- }
- if (get_alt()) {
- mod_names.push_back(find_keycode_name(KEY_ALT));
- }
- if (get_metakey()) {
- mod_names.push_back(find_keycode_name(KEY_META));
- }
- if (!mod_names.is_empty()) {
- return String("+").join(mod_names);
- } else {
- return "";
- }
- }
- String InputEventWithModifiers::to_string() {
- return as_text();
- }
- void InputEventWithModifiers::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_store_command", "enable"), &InputEventWithModifiers::set_store_command);
- ClassDB::bind_method(D_METHOD("is_storing_command"), &InputEventWithModifiers::is_storing_command);
- ClassDB::bind_method(D_METHOD("set_alt", "enable"), &InputEventWithModifiers::set_alt);
- ClassDB::bind_method(D_METHOD("get_alt"), &InputEventWithModifiers::get_alt);
- ClassDB::bind_method(D_METHOD("set_shift", "enable"), &InputEventWithModifiers::set_shift);
- ClassDB::bind_method(D_METHOD("get_shift"), &InputEventWithModifiers::get_shift);
- ClassDB::bind_method(D_METHOD("set_control", "enable"), &InputEventWithModifiers::set_control);
- ClassDB::bind_method(D_METHOD("get_control"), &InputEventWithModifiers::get_control);
- ClassDB::bind_method(D_METHOD("set_metakey", "enable"), &InputEventWithModifiers::set_metakey);
- ClassDB::bind_method(D_METHOD("get_metakey"), &InputEventWithModifiers::get_metakey);
- ClassDB::bind_method(D_METHOD("set_command", "enable"), &InputEventWithModifiers::set_command);
- ClassDB::bind_method(D_METHOD("get_command"), &InputEventWithModifiers::get_command);
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "store_command"), "set_store_command", "is_storing_command");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "alt"), "set_alt", "get_alt");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shift"), "set_shift", "get_shift");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "control"), "set_control", "get_control");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meta"), "set_metakey", "get_metakey");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "command"), "set_command", "get_command");
- }
- void InputEventWithModifiers::_validate_property(PropertyInfo &property) const {
- if (store_command) {
- // If we only want to Store "Command".
- #ifdef APPLE_STYLE_KEYS
- // Don't store "Meta" on Mac.
- if (property.name == "meta") {
- property.usage ^= PROPERTY_USAGE_STORAGE;
- }
- #else
- // Don't store "Control".
- if (property.name == "control") {
- property.usage ^= PROPERTY_USAGE_STORAGE;
- }
- #endif
- } else {
- // We don't want to store command, only control or meta (on mac).
- if (property.name == "command") {
- property.usage ^= PROPERTY_USAGE_STORAGE;
- }
- }
- }
- ///////////////////////////////////
- void InputEventKey::set_pressed(bool p_pressed) {
- pressed = p_pressed;
- }
- bool InputEventKey::is_pressed() const {
- return pressed;
- }
- void InputEventKey::set_keycode(uint32_t p_keycode) {
- keycode = p_keycode;
- }
- uint32_t InputEventKey::get_keycode() const {
- return keycode;
- }
- void InputEventKey::set_physical_keycode(uint32_t p_keycode) {
- physical_keycode = p_keycode;
- }
- uint32_t InputEventKey::get_physical_keycode() const {
- return physical_keycode;
- }
- void InputEventKey::set_unicode(uint32_t p_unicode) {
- unicode = p_unicode;
- }
- uint32_t InputEventKey::get_unicode() const {
- return unicode;
- }
- void InputEventKey::set_echo(bool p_enable) {
- echo = p_enable;
- }
- bool InputEventKey::is_echo() const {
- return echo;
- }
- uint32_t InputEventKey::get_keycode_with_modifiers() const {
- uint32_t sc = keycode;
- if (get_control()) {
- sc |= KEY_MASK_CTRL;
- }
- if (get_alt()) {
- sc |= KEY_MASK_ALT;
- }
- if (get_shift()) {
- sc |= KEY_MASK_SHIFT;
- }
- if (get_metakey()) {
- sc |= KEY_MASK_META;
- }
- return sc;
- }
- uint32_t InputEventKey::get_physical_keycode_with_modifiers() const {
- uint32_t sc = physical_keycode;
- if (get_control()) {
- sc |= KEY_MASK_CTRL;
- }
- if (get_alt()) {
- sc |= KEY_MASK_ALT;
- }
- if (get_shift()) {
- sc |= KEY_MASK_SHIFT;
- }
- if (get_metakey()) {
- sc |= KEY_MASK_META;
- }
- return sc;
- }
- String InputEventKey::as_text() const {
- String kc;
- if (keycode == 0) {
- kc = keycode_get_string(physical_keycode) + " (" + RTR("Physical") + ")";
- } else {
- kc = keycode_get_string(keycode);
- }
- if (kc == String()) {
- return kc;
- }
- String mods_text = InputEventWithModifiers::as_text();
- return mods_text == "" ? kc : mods_text + "+" + kc;
- }
- String InputEventKey::to_string() {
- String p = is_pressed() ? "true" : "false";
- String e = is_echo() ? "true" : "false";
- String kc = "";
- String physical = "false";
- if (keycode == 0) {
- kc = itos(physical_keycode) + " " + keycode_get_string(physical_keycode);
- physical = "true";
- } else {
- kc = itos(keycode) + " " + keycode_get_string(keycode);
- }
- String mods = InputEventWithModifiers::as_text();
- mods = mods == "" ? TTR("None") : mods;
- return vformat("InputEventKey: keycode=%s mods=%s physical=%s pressed=%s echo=%s", kc, mods, physical, p, e);
- }
- Ref<InputEventKey> InputEventKey::create_reference(uint32_t p_keycode) {
- Ref<InputEventKey> ie;
- ie.instance();
- ie->set_keycode(p_keycode & KEY_CODE_MASK);
- ie->set_unicode(p_keycode & KEY_CODE_MASK);
- if (p_keycode & KEY_MASK_SHIFT) {
- ie->set_shift(true);
- }
- if (p_keycode & KEY_MASK_ALT) {
- ie->set_alt(true);
- }
- if (p_keycode & KEY_MASK_CTRL) {
- ie->set_control(true);
- }
- if (p_keycode & KEY_MASK_CMD) {
- ie->set_command(true);
- }
- if (p_keycode & KEY_MASK_META) {
- ie->set_metakey(true);
- }
- return ie;
- }
- bool InputEventKey::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
- Ref<InputEventKey> key = p_event;
- if (key.is_null()) {
- return false;
- }
- bool match = false;
- if (get_keycode() == 0) {
- uint32_t code = get_physical_keycode_with_modifiers();
- uint32_t event_code = key->get_physical_keycode_with_modifiers();
- match = get_physical_keycode() == key->get_physical_keycode() && (!key->is_pressed() || (code & event_code) == code);
- } else {
- uint32_t code = get_keycode_with_modifiers();
- uint32_t event_code = key->get_keycode_with_modifiers();
- match = get_keycode() == key->get_keycode() && (!key->is_pressed() || (code & event_code) == code);
- }
- if (match) {
- if (p_pressed != nullptr) {
- *p_pressed = key->is_pressed();
- }
- float strength = (p_pressed != nullptr && *p_pressed) ? 1.0f : 0.0f;
- if (p_strength != nullptr) {
- *p_strength = strength;
- }
- if (p_raw_strength != nullptr) {
- *p_raw_strength = strength;
- }
- }
- return match;
- }
- bool InputEventKey::shortcut_match(const Ref<InputEvent> &p_event) const {
- Ref<InputEventKey> key = p_event;
- if (key.is_null()) {
- return false;
- }
- uint32_t code = get_keycode_with_modifiers();
- uint32_t event_code = key->get_keycode_with_modifiers();
- return code == event_code;
- }
- void InputEventKey::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventKey::set_pressed);
- ClassDB::bind_method(D_METHOD("set_keycode", "keycode"), &InputEventKey::set_keycode);
- ClassDB::bind_method(D_METHOD("get_keycode"), &InputEventKey::get_keycode);
- ClassDB::bind_method(D_METHOD("set_physical_keycode", "physical_keycode"), &InputEventKey::set_physical_keycode);
- ClassDB::bind_method(D_METHOD("get_physical_keycode"), &InputEventKey::get_physical_keycode);
- ClassDB::bind_method(D_METHOD("set_unicode", "unicode"), &InputEventKey::set_unicode);
- ClassDB::bind_method(D_METHOD("get_unicode"), &InputEventKey::get_unicode);
- ClassDB::bind_method(D_METHOD("set_echo", "echo"), &InputEventKey::set_echo);
- ClassDB::bind_method(D_METHOD("get_keycode_with_modifiers"), &InputEventKey::get_keycode_with_modifiers);
- ClassDB::bind_method(D_METHOD("get_physical_keycode_with_modifiers"), &InputEventKey::get_physical_keycode_with_modifiers);
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "keycode"), "set_keycode", "get_keycode");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "physical_keycode"), "set_physical_keycode", "get_physical_keycode");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "unicode"), "set_unicode", "get_unicode");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "echo"), "set_echo", "is_echo");
- }
- ///////////////////////////////////
- void InputEventMouse::set_button_mask(int p_mask) {
- button_mask = p_mask;
- }
- int InputEventMouse::get_button_mask() const {
- return button_mask;
- }
- void InputEventMouse::set_position(const Vector2 &p_pos) {
- pos = p_pos;
- }
- Vector2 InputEventMouse::get_position() const {
- return pos;
- }
- void InputEventMouse::set_global_position(const Vector2 &p_global_pos) {
- global_pos = p_global_pos;
- }
- Vector2 InputEventMouse::get_global_position() const {
- return global_pos;
- }
- void InputEventMouse::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_button_mask", "button_mask"), &InputEventMouse::set_button_mask);
- ClassDB::bind_method(D_METHOD("get_button_mask"), &InputEventMouse::get_button_mask);
- ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventMouse::set_position);
- ClassDB::bind_method(D_METHOD("get_position"), &InputEventMouse::get_position);
- ClassDB::bind_method(D_METHOD("set_global_position", "global_position"), &InputEventMouse::set_global_position);
- ClassDB::bind_method(D_METHOD("get_global_position"), &InputEventMouse::get_global_position);
- ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask"), "set_button_mask", "get_button_mask");
- ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
- ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_position"), "set_global_position", "get_global_position");
- }
- ///////////////////////////////////
- void InputEventMouseButton::set_factor(float p_factor) {
- factor = p_factor;
- }
- float InputEventMouseButton::get_factor() const {
- return factor;
- }
- void InputEventMouseButton::set_button_index(int p_index) {
- button_index = p_index;
- }
- int InputEventMouseButton::get_button_index() const {
- return button_index;
- }
- void InputEventMouseButton::set_pressed(bool p_pressed) {
- pressed = p_pressed;
- }
- bool InputEventMouseButton::is_pressed() const {
- return pressed;
- }
- void InputEventMouseButton::set_doubleclick(bool p_doubleclick) {
- doubleclick = p_doubleclick;
- }
- bool InputEventMouseButton::is_doubleclick() const {
- return doubleclick;
- }
- Ref<InputEvent> InputEventMouseButton::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
- Vector2 g = get_global_position();
- Vector2 l = p_xform.xform(get_position() + p_local_ofs);
- Ref<InputEventMouseButton> mb;
- mb.instance();
- mb->set_device(get_device());
- mb->set_window_id(get_window_id());
- mb->set_modifiers_from_event(this);
- mb->set_position(l);
- mb->set_global_position(g);
- mb->set_button_mask(get_button_mask());
- mb->set_pressed(pressed);
- mb->set_doubleclick(doubleclick);
- mb->set_factor(factor);
- mb->set_button_index(button_index);
- return mb;
- }
- bool InputEventMouseButton::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
- Ref<InputEventMouseButton> mb = p_event;
- if (mb.is_null()) {
- return false;
- }
- bool match = mb->button_index == button_index;
- if (match) {
- if (p_pressed != nullptr) {
- *p_pressed = mb->is_pressed();
- }
- float strength = (p_pressed != nullptr && *p_pressed) ? 1.0f : 0.0f;
- if (p_strength != nullptr) {
- *p_strength = strength;
- }
- if (p_raw_strength != nullptr) {
- *p_raw_strength = strength;
- }
- }
- return match;
- }
- static const char *_mouse_button_descriptions[9] = {
- TTRC("Left Mouse Button"),
- TTRC("Right Mouse Button"),
- TTRC("Middle Mouse Button"),
- TTRC("Mouse Wheel Up"),
- TTRC("Mouse Wheel Down"),
- TTRC("Mouse Wheel Left"),
- TTRC("Mouse Wheel Right"),
- TTRC("Mouse Thumb Button 1"),
- TTRC("Mouse Thumb Button 2")
- };
- String InputEventMouseButton::as_text() const {
- // Modifiers
- String mods_text = InputEventWithModifiers::as_text();
- String full_string = mods_text == "" ? "" : mods_text + "+";
- // Button
- int idx = get_button_index();
- switch (idx) {
- case BUTTON_LEFT:
- case BUTTON_RIGHT:
- case BUTTON_MIDDLE:
- case BUTTON_WHEEL_UP:
- case BUTTON_WHEEL_DOWN:
- case BUTTON_WHEEL_LEFT:
- case BUTTON_WHEEL_RIGHT:
- case BUTTON_XBUTTON1:
- case BUTTON_XBUTTON2:
- full_string += RTR(_mouse_button_descriptions[idx - 1]); // button index starts from 1, array index starts from 0, so subtract 1
- break;
- default:
- full_string += RTR("Button") + " #" + itos(idx);
- break;
- }
- // Double Click
- if (doubleclick) {
- full_string += " (" + RTR("Double Click") + ")";
- }
- return full_string;
- }
- String InputEventMouseButton::to_string() {
- String p = is_pressed() ? "true" : "false";
- String d = doubleclick ? "true" : "false";
- int idx = get_button_index();
- String button_string = itos(idx);
- switch (idx) {
- case BUTTON_LEFT:
- case BUTTON_RIGHT:
- case BUTTON_MIDDLE:
- case BUTTON_WHEEL_UP:
- case BUTTON_WHEEL_DOWN:
- case BUTTON_WHEEL_LEFT:
- case BUTTON_WHEEL_RIGHT:
- case BUTTON_XBUTTON1:
- case BUTTON_XBUTTON2:
- button_string += " (" + RTR(_mouse_button_descriptions[idx - 1]) + ")"; // button index starts from 1, array index starts from 0, so subtract 1
- break;
- default:
- break;
- }
- String mods = InputEventWithModifiers::as_text();
- mods = mods == "" ? TTR("None") : mods;
- // Work around the fact vformat can only take 5 substitutions but 6 need to be passed.
- String index_and_mods = vformat("button_index=%s mods=%s", button_index, mods);
- return vformat("InputEventMouseButton: %s pressed=%s position=(%s) button_mask=%s doubleclick=%s", index_and_mods, p, String(get_position()), itos(get_button_mask()), d);
- }
- void InputEventMouseButton::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_factor", "factor"), &InputEventMouseButton::set_factor);
- ClassDB::bind_method(D_METHOD("get_factor"), &InputEventMouseButton::get_factor);
- ClassDB::bind_method(D_METHOD("set_button_index", "button_index"), &InputEventMouseButton::set_button_index);
- ClassDB::bind_method(D_METHOD("get_button_index"), &InputEventMouseButton::get_button_index);
- ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventMouseButton::set_pressed);
- // ClassDB::bind_method(D_METHOD("is_pressed"), &InputEventMouseButton::is_pressed);
- ClassDB::bind_method(D_METHOD("set_doubleclick", "doubleclick"), &InputEventMouseButton::set_doubleclick);
- ClassDB::bind_method(D_METHOD("is_doubleclick"), &InputEventMouseButton::is_doubleclick);
- ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "factor"), "set_factor", "get_factor");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "button_index"), "set_button_index", "get_button_index");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "doubleclick"), "set_doubleclick", "is_doubleclick");
- }
- ///////////////////////////////////
- void InputEventMouseMotion::set_tilt(const Vector2 &p_tilt) {
- tilt = p_tilt;
- }
- Vector2 InputEventMouseMotion::get_tilt() const {
- return tilt;
- }
- void InputEventMouseMotion::set_pressure(float p_pressure) {
- pressure = p_pressure;
- }
- float InputEventMouseMotion::get_pressure() const {
- return pressure;
- }
- void InputEventMouseMotion::set_relative(const Vector2 &p_relative) {
- relative = p_relative;
- }
- Vector2 InputEventMouseMotion::get_relative() const {
- return relative;
- }
- void InputEventMouseMotion::set_speed(const Vector2 &p_speed) {
- speed = p_speed;
- }
- Vector2 InputEventMouseMotion::get_speed() const {
- return speed;
- }
- Ref<InputEvent> InputEventMouseMotion::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
- Vector2 g = get_global_position();
- Vector2 l = p_xform.xform(get_position() + p_local_ofs);
- Vector2 r = p_xform.basis_xform(get_relative());
- Vector2 s = p_xform.basis_xform(get_speed());
- Ref<InputEventMouseMotion> mm;
- mm.instance();
- mm->set_device(get_device());
- mm->set_window_id(get_window_id());
- mm->set_modifiers_from_event(this);
- mm->set_position(l);
- mm->set_pressure(get_pressure());
- mm->set_tilt(get_tilt());
- mm->set_global_position(g);
- mm->set_button_mask(get_button_mask());
- mm->set_relative(r);
- mm->set_speed(s);
- return mm;
- }
- String InputEventMouseMotion::as_text() const {
- return vformat(RTR("Mouse motion at position (%s) with speed (%s)"), String(get_position()), String(get_speed()));
- }
- String InputEventMouseMotion::to_string() {
- int button_mask = get_button_mask();
- String button_mask_string = itos(button_mask);
- switch (get_button_mask()) {
- case BUTTON_MASK_LEFT:
- button_mask_string += " (" + RTR(_mouse_button_descriptions[BUTTON_LEFT - 1]) + ")";
- break;
- case BUTTON_MASK_MIDDLE:
- button_mask_string += " (" + RTR(_mouse_button_descriptions[BUTTON_MIDDLE - 1]) + ")";
- break;
- case BUTTON_MASK_RIGHT:
- button_mask_string += " (" + RTR(_mouse_button_descriptions[BUTTON_RIGHT - 1]) + ")";
- break;
- case BUTTON_MASK_XBUTTON1:
- button_mask_string += " (" + RTR(_mouse_button_descriptions[BUTTON_XBUTTON1 - 1]) + ")";
- break;
- case BUTTON_MASK_XBUTTON2:
- button_mask_string += " (" + RTR(_mouse_button_descriptions[BUTTON_XBUTTON2 - 1]) + ")";
- break;
- default:
- break;
- }
- 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()) + ")";
- }
- bool InputEventMouseMotion::accumulate(const Ref<InputEvent> &p_event) {
- Ref<InputEventMouseMotion> motion = p_event;
- if (motion.is_null()) {
- return false;
- }
- if (get_window_id() != motion->get_window_id()) {
- return false;
- }
- if (is_pressed() != motion->is_pressed()) {
- return false;
- }
- if (get_button_mask() != motion->get_button_mask()) {
- return false;
- }
- if (get_shift() != motion->get_shift()) {
- return false;
- }
- if (get_control() != motion->get_control()) {
- return false;
- }
- if (get_alt() != motion->get_alt()) {
- return false;
- }
- if (get_metakey() != motion->get_metakey()) {
- return false;
- }
- set_position(motion->get_position());
- set_global_position(motion->get_global_position());
- set_speed(motion->get_speed());
- relative += motion->get_relative();
- return true;
- }
- void InputEventMouseMotion::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_tilt", "tilt"), &InputEventMouseMotion::set_tilt);
- ClassDB::bind_method(D_METHOD("get_tilt"), &InputEventMouseMotion::get_tilt);
- ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventMouseMotion::set_pressure);
- ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventMouseMotion::get_pressure);
- ClassDB::bind_method(D_METHOD("set_relative", "relative"), &InputEventMouseMotion::set_relative);
- ClassDB::bind_method(D_METHOD("get_relative"), &InputEventMouseMotion::get_relative);
- ClassDB::bind_method(D_METHOD("set_speed", "speed"), &InputEventMouseMotion::set_speed);
- ClassDB::bind_method(D_METHOD("get_speed"), &InputEventMouseMotion::get_speed);
- ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "tilt"), "set_tilt", "get_tilt");
- ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pressure"), "set_pressure", "get_pressure");
- ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "relative"), "set_relative", "get_relative");
- ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "speed"), "set_speed", "get_speed");
- }
- ///////////////////////////////////
- void InputEventJoypadMotion::set_axis(int p_axis) {
- axis = p_axis;
- }
- int InputEventJoypadMotion::get_axis() const {
- return axis;
- }
- void InputEventJoypadMotion::set_axis_value(float p_value) {
- axis_value = p_value;
- }
- float InputEventJoypadMotion::get_axis_value() const {
- return axis_value;
- }
- bool InputEventJoypadMotion::is_pressed() const {
- return Math::abs(axis_value) >= 0.5f;
- }
- bool InputEventJoypadMotion::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
- Ref<InputEventJoypadMotion> jm = p_event;
- if (jm.is_null()) {
- return false;
- }
- bool match = (axis == jm->axis); // Matches even if not in the same direction, but returns a "not pressed" event.
- if (match) {
- float jm_abs_axis_value = Math::abs(jm->get_axis_value());
- bool same_direction = (((axis_value < 0) == (jm->axis_value < 0)) || jm->axis_value == 0);
- bool pressed = same_direction && jm_abs_axis_value >= p_deadzone;
- if (p_pressed != nullptr) {
- *p_pressed = pressed;
- }
- if (p_strength != nullptr) {
- if (pressed) {
- if (p_deadzone == 1.0f) {
- *p_strength = 1.0f;
- } else {
- *p_strength = CLAMP(Math::inverse_lerp(p_deadzone, 1.0f, jm_abs_axis_value), 0.0f, 1.0f);
- }
- } else {
- *p_strength = 0.0f;
- }
- }
- if (p_raw_strength != nullptr) {
- if (same_direction) { // NOT pressed, because we want to ignore the deadzone.
- *p_raw_strength = jm_abs_axis_value;
- } else {
- *p_raw_strength = 0.0f;
- }
- }
- }
- return match;
- }
- static const char *_joy_axis_descriptions[JOY_AXIS_MAX] = {
- TTRC("Left Stick X-Axis, Joystick 0 X-Axis"),
- TTRC("Left Stick Y-Axis, Joystick 0 Y-Axis"),
- TTRC("Right Stick X-Axis, Joystick 1 X-Axis"),
- TTRC("Right Stick Y-Axis, Joystick 1 Y-Axis"),
- TTRC("Joystick 2 X-Axis, Left Trigger, Sony L2, Xbox LT"),
- TTRC("Joystick 2 Y-Axis, Right Trigger, Sony R2, Xbox RT"),
- TTRC("Joystick 3 X-Axis"),
- TTRC("Joystick 3 Y-Axis"),
- TTRC("Joystick 4 X-Axis"),
- TTRC("Joystick 4 Y-Axis"),
- };
- String InputEventJoypadMotion::as_text() const {
- String desc = axis < JOY_AXIS_MAX ? RTR(_joy_axis_descriptions[axis]) : TTR("Unknown Joypad Axis");
- return vformat(TTR("Joypad Motion on Axis %s (%s) with Value %s"), itos(axis), desc, String(Variant(axis_value)));
- }
- String InputEventJoypadMotion::to_string() {
- return "InputEventJoypadMotion : axis=" + itos(axis) + ", axis_value=" + String(Variant(axis_value));
- }
- void InputEventJoypadMotion::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_axis", "axis"), &InputEventJoypadMotion::set_axis);
- ClassDB::bind_method(D_METHOD("get_axis"), &InputEventJoypadMotion::get_axis);
- ClassDB::bind_method(D_METHOD("set_axis_value", "axis_value"), &InputEventJoypadMotion::set_axis_value);
- ClassDB::bind_method(D_METHOD("get_axis_value"), &InputEventJoypadMotion::get_axis_value);
- ADD_PROPERTY(PropertyInfo(Variant::INT, "axis"), "set_axis", "get_axis");
- ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "axis_value"), "set_axis_value", "get_axis_value");
- }
- ///////////////////////////////////
- void InputEventJoypadButton::set_button_index(int p_index) {
- button_index = p_index;
- }
- int InputEventJoypadButton::get_button_index() const {
- return button_index;
- }
- void InputEventJoypadButton::set_pressed(bool p_pressed) {
- pressed = p_pressed;
- }
- bool InputEventJoypadButton::is_pressed() const {
- return pressed;
- }
- void InputEventJoypadButton::set_pressure(float p_pressure) {
- pressure = p_pressure;
- }
- float InputEventJoypadButton::get_pressure() const {
- return pressure;
- }
- bool InputEventJoypadButton::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
- Ref<InputEventJoypadButton> jb = p_event;
- if (jb.is_null()) {
- return false;
- }
- bool match = button_index == jb->button_index;
- if (match) {
- if (p_pressed != nullptr) {
- *p_pressed = jb->is_pressed();
- }
- float strength = (p_pressed != nullptr && *p_pressed) ? 1.0f : 0.0f;
- if (p_strength != nullptr) {
- *p_strength = strength;
- }
- if (p_raw_strength != nullptr) {
- *p_raw_strength = strength;
- }
- }
- return match;
- }
- bool InputEventJoypadButton::shortcut_match(const Ref<InputEvent> &p_event) const {
- Ref<InputEventJoypadButton> button = p_event;
- if (button.is_null()) {
- return false;
- }
- return button_index == button->button_index;
- }
- static const char *_joy_button_descriptions[JOY_BUTTON_SDL_MAX] = {
- TTRC("Bottom Action, Sony Cross, Xbox A, Nintendo B"),
- TTRC("Right Action, Sony Circle, Xbox B, Nintendo A"),
- TTRC("Left Action, Sony Square, Xbox X, Nintendo Y"),
- TTRC("Top Action, Sony Triangle, Xbox Y, Nintendo X"),
- TTRC("Back, Sony Select, Xbox Back, Nintendo -"),
- TTRC("Guide, Sony PS, Xbox Home"),
- TTRC("Start, Nintendo +"),
- TTRC("Left Stick, Sony L3, Xbox L/LS"),
- TTRC("Right Stick, Sony R3, Xbox R/RS"),
- TTRC("Left Shoulder, Sony L1, Xbox LB"),
- TTRC("Right Shoulder, Sony R1, Xbox RB"),
- TTRC("D-pad Up"),
- TTRC("D-pad Down"),
- TTRC("D-pad Left"),
- TTRC("D-pad Right"),
- TTRC("Xbox Share, PS5 Microphone, Nintendo Capture"),
- TTRC("Xbox Paddle 1"),
- TTRC("Xbox Paddle 2"),
- TTRC("Xbox Paddle 3"),
- TTRC("Xbox Paddle 4"),
- TTRC("PS4/5 Touchpad"),
- };
- String InputEventJoypadButton::as_text() const {
- String text = "Joypad Button " + itos(button_index);
- if (button_index < JOY_BUTTON_SDL_MAX) {
- text += vformat(" (%s)", _joy_button_descriptions[button_index]);
- }
- if (pressure != 0) {
- text += ", Pressure:" + String(Variant(pressure));
- }
- return text;
- }
- String InputEventJoypadButton::to_string() {
- return "InputEventJoypadButton : button_index=" + itos(button_index) + ", pressed=" + (pressed ? "true" : "false") + ", pressure=" + String(Variant(pressure));
- }
- Ref<InputEventJoypadButton> InputEventJoypadButton::create_reference(int p_btn_index) {
- Ref<InputEventJoypadButton> ie;
- ie.instance();
- ie->set_button_index(p_btn_index);
- return ie;
- }
- void InputEventJoypadButton::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_button_index", "button_index"), &InputEventJoypadButton::set_button_index);
- ClassDB::bind_method(D_METHOD("get_button_index"), &InputEventJoypadButton::get_button_index);
- ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventJoypadButton::set_pressure);
- ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventJoypadButton::get_pressure);
- ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventJoypadButton::set_pressed);
- // ClassDB::bind_method(D_METHOD("is_pressed"), &InputEventJoypadButton::is_pressed);
- ADD_PROPERTY(PropertyInfo(Variant::INT, "button_index"), "set_button_index", "get_button_index");
- ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pressure"), "set_pressure", "get_pressure");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
- }
- ///////////////////////////////////
- void InputEventScreenTouch::set_index(int p_index) {
- index = p_index;
- }
- int InputEventScreenTouch::get_index() const {
- return index;
- }
- void InputEventScreenTouch::set_position(const Vector2 &p_pos) {
- pos = p_pos;
- }
- Vector2 InputEventScreenTouch::get_position() const {
- return pos;
- }
- void InputEventScreenTouch::set_pressed(bool p_pressed) {
- pressed = p_pressed;
- }
- bool InputEventScreenTouch::is_pressed() const {
- return pressed;
- }
- Ref<InputEvent> InputEventScreenTouch::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
- Ref<InputEventScreenTouch> st;
- st.instance();
- st->set_device(get_device());
- st->set_window_id(get_window_id());
- st->set_index(index);
- st->set_position(p_xform.xform(pos + p_local_ofs));
- st->set_pressed(pressed);
- return st;
- }
- String InputEventScreenTouch::as_text() const {
- String status = pressed ? RTR("touched") : RTR("released");
- return vformat(RTR("Screen %s at (%s) with %s touch points"), status, String(get_position()), itos(index));
- }
- String InputEventScreenTouch::to_string() {
- return "InputEventScreenTouch : index=" + itos(index) + ", pressed=" + (pressed ? "true" : "false") + ", position=(" + String(get_position()) + ")";
- }
- void InputEventScreenTouch::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenTouch::set_index);
- ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenTouch::get_index);
- ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventScreenTouch::set_position);
- ClassDB::bind_method(D_METHOD("get_position"), &InputEventScreenTouch::get_position);
- ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventScreenTouch::set_pressed);
- //ClassDB::bind_method(D_METHOD("is_pressed"),&InputEventScreenTouch::is_pressed);
- ADD_PROPERTY(PropertyInfo(Variant::INT, "index"), "set_index", "get_index");
- ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
- }
- ///////////////////////////////////
- void InputEventScreenDrag::set_index(int p_index) {
- index = p_index;
- }
- int InputEventScreenDrag::get_index() const {
- return index;
- }
- void InputEventScreenDrag::set_position(const Vector2 &p_pos) {
- pos = p_pos;
- }
- Vector2 InputEventScreenDrag::get_position() const {
- return pos;
- }
- void InputEventScreenDrag::set_relative(const Vector2 &p_relative) {
- relative = p_relative;
- }
- Vector2 InputEventScreenDrag::get_relative() const {
- return relative;
- }
- void InputEventScreenDrag::set_speed(const Vector2 &p_speed) {
- speed = p_speed;
- }
- Vector2 InputEventScreenDrag::get_speed() const {
- return speed;
- }
- Ref<InputEvent> InputEventScreenDrag::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
- Ref<InputEventScreenDrag> sd;
- sd.instance();
- sd->set_device(get_device());
- sd->set_window_id(get_window_id());
- sd->set_index(index);
- sd->set_position(p_xform.xform(pos + p_local_ofs));
- sd->set_relative(p_xform.basis_xform(relative));
- sd->set_speed(p_xform.basis_xform(speed));
- return sd;
- }
- String InputEventScreenDrag::as_text() const {
- return vformat(RTR("Screen dragged with %s touch points at position (%s) with speed of (%s)"), itos(index), String(get_position()), String(get_speed()));
- }
- String InputEventScreenDrag::to_string() {
- return "InputEventScreenDrag : index=" + itos(index) + ", position=(" + String(get_position()) + "), relative=(" + String(get_relative()) + "), speed=(" + String(get_speed()) + ")";
- }
- void InputEventScreenDrag::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenDrag::set_index);
- ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenDrag::get_index);
- ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventScreenDrag::set_position);
- ClassDB::bind_method(D_METHOD("get_position"), &InputEventScreenDrag::get_position);
- ClassDB::bind_method(D_METHOD("set_relative", "relative"), &InputEventScreenDrag::set_relative);
- ClassDB::bind_method(D_METHOD("get_relative"), &InputEventScreenDrag::get_relative);
- ClassDB::bind_method(D_METHOD("set_speed", "speed"), &InputEventScreenDrag::set_speed);
- ClassDB::bind_method(D_METHOD("get_speed"), &InputEventScreenDrag::get_speed);
- ADD_PROPERTY(PropertyInfo(Variant::INT, "index"), "set_index", "get_index");
- ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
- ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "relative"), "set_relative", "get_relative");
- ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "speed"), "set_speed", "get_speed");
- }
- ///////////////////////////////////
- void InputEventAction::set_action(const StringName &p_action) {
- action = p_action;
- }
- StringName InputEventAction::get_action() const {
- return action;
- }
- void InputEventAction::set_pressed(bool p_pressed) {
- pressed = p_pressed;
- }
- bool InputEventAction::is_pressed() const {
- return pressed;
- }
- void InputEventAction::set_strength(float p_strength) {
- strength = CLAMP(p_strength, 0.0f, 1.0f);
- }
- float InputEventAction::get_strength() const {
- return strength;
- }
- bool InputEventAction::shortcut_match(const Ref<InputEvent> &p_event) const {
- if (p_event.is_null()) {
- return false;
- }
- return p_event->is_action(action);
- }
- bool InputEventAction::is_action(const StringName &p_action) const {
- return action == p_action;
- }
- bool InputEventAction::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
- Ref<InputEventAction> act = p_event;
- if (act.is_null()) {
- return false;
- }
- bool match = action == act->action;
- if (match) {
- if (p_pressed != nullptr) {
- *p_pressed = act->pressed;
- }
- float strength = (p_pressed != nullptr && *p_pressed) ? 1.0f : 0.0f;
- if (p_strength != nullptr) {
- *p_strength = strength;
- }
- if (p_raw_strength != nullptr) {
- *p_raw_strength = strength;
- }
- }
- return match;
- }
- String InputEventAction::as_text() const {
- return vformat(RTR("Input Action %s was %s"), action, pressed ? "pressed" : "released");
- }
- String InputEventAction::to_string() {
- return "InputEventAction : action=" + action + ", pressed=(" + (pressed ? "true" : "false");
- }
- void InputEventAction::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_action", "action"), &InputEventAction::set_action);
- ClassDB::bind_method(D_METHOD("get_action"), &InputEventAction::get_action);
- ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventAction::set_pressed);
- //ClassDB::bind_method(D_METHOD("is_pressed"), &InputEventAction::is_pressed);
- ClassDB::bind_method(D_METHOD("set_strength", "strength"), &InputEventAction::set_strength);
- ClassDB::bind_method(D_METHOD("get_strength"), &InputEventAction::get_strength);
- // ClassDB::bind_method(D_METHOD("is_action", "name"), &InputEventAction::is_action);
- ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "action"), "set_action", "get_action");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
- ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "strength", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_strength", "get_strength");
- }
- ///////////////////////////////////
- void InputEventGesture::set_position(const Vector2 &p_pos) {
- pos = p_pos;
- }
- void InputEventGesture::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventGesture::set_position);
- ClassDB::bind_method(D_METHOD("get_position"), &InputEventGesture::get_position);
- ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
- }
- Vector2 InputEventGesture::get_position() const {
- return pos;
- }
- ///////////////////////////////////
- void InputEventMagnifyGesture::set_factor(real_t p_factor) {
- factor = p_factor;
- }
- real_t InputEventMagnifyGesture::get_factor() const {
- return factor;
- }
- Ref<InputEvent> InputEventMagnifyGesture::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
- Ref<InputEventMagnifyGesture> ev;
- ev.instance();
- ev->set_device(get_device());
- ev->set_window_id(get_window_id());
- ev->set_modifiers_from_event(this);
- ev->set_position(p_xform.xform(get_position() + p_local_ofs));
- ev->set_factor(get_factor());
- return ev;
- }
- String InputEventMagnifyGesture::as_text() const {
- return vformat(RTR("Magnify Gesture at (%s) with factor %s"), String(get_position()), rtos(get_factor()));
- }
- String InputEventMagnifyGesture::to_string() {
- return "InputEventMagnifyGesture : factor=" + rtos(get_factor()) + ", position=(" + String(get_position()) + ")";
- }
- void InputEventMagnifyGesture::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_factor", "factor"), &InputEventMagnifyGesture::set_factor);
- ClassDB::bind_method(D_METHOD("get_factor"), &InputEventMagnifyGesture::get_factor);
- ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "factor"), "set_factor", "get_factor");
- }
- ///////////////////////////////////
- void InputEventPanGesture::set_delta(const Vector2 &p_delta) {
- delta = p_delta;
- }
- Vector2 InputEventPanGesture::get_delta() const {
- return delta;
- }
- Ref<InputEvent> InputEventPanGesture::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
- Ref<InputEventPanGesture> ev;
- ev.instance();
- ev->set_device(get_device());
- ev->set_window_id(get_window_id());
- ev->set_modifiers_from_event(this);
- ev->set_position(p_xform.xform(get_position() + p_local_ofs));
- ev->set_delta(get_delta());
- return ev;
- }
- String InputEventPanGesture::as_text() const {
- return vformat(RTR("Pan Gesture at (%s) with delta (%s)"), String(get_position()), String(get_delta()));
- }
- String InputEventPanGesture::to_string() {
- return "InputEventPanGesture : delta=(" + String(get_delta()) + "), position=(" + String(get_position()) + ")";
- }
- void InputEventPanGesture::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_delta", "delta"), &InputEventPanGesture::set_delta);
- ClassDB::bind_method(D_METHOD("get_delta"), &InputEventPanGesture::get_delta);
- ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "delta"), "set_delta", "get_delta");
- }
- ///////////////////////////////////
- void InputEventMIDI::set_channel(const int p_channel) {
- channel = p_channel;
- }
- int InputEventMIDI::get_channel() const {
- return channel;
- }
- void InputEventMIDI::set_message(const int p_message) {
- message = p_message;
- }
- int InputEventMIDI::get_message() const {
- return message;
- }
- void InputEventMIDI::set_pitch(const int p_pitch) {
- pitch = p_pitch;
- }
- int InputEventMIDI::get_pitch() const {
- return pitch;
- }
- void InputEventMIDI::set_velocity(const int p_velocity) {
- velocity = p_velocity;
- }
- int InputEventMIDI::get_velocity() const {
- return velocity;
- }
- void InputEventMIDI::set_instrument(const int p_instrument) {
- instrument = p_instrument;
- }
- int InputEventMIDI::get_instrument() const {
- return instrument;
- }
- void InputEventMIDI::set_pressure(const int p_pressure) {
- pressure = p_pressure;
- }
- int InputEventMIDI::get_pressure() const {
- return pressure;
- }
- void InputEventMIDI::set_controller_number(const int p_controller_number) {
- controller_number = p_controller_number;
- }
- int InputEventMIDI::get_controller_number() const {
- return controller_number;
- }
- void InputEventMIDI::set_controller_value(const int p_controller_value) {
- controller_value = p_controller_value;
- }
- int InputEventMIDI::get_controller_value() const {
- return controller_value;
- }
- String InputEventMIDI::as_text() const {
- return vformat(RTR("MIDI Input on Channel=%s Message=%s"), itos(channel), itos(message));
- }
- String InputEventMIDI::to_string() {
- return vformat("InputEvenMIDI: channel=%s message=%s pitch=%s velocity=%s pressure=%s", itos(channel), itos(message), itos(pitch), itos(velocity), itos(pressure));
- }
- void InputEventMIDI::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_channel", "channel"), &InputEventMIDI::set_channel);
- ClassDB::bind_method(D_METHOD("get_channel"), &InputEventMIDI::get_channel);
- ClassDB::bind_method(D_METHOD("set_message", "message"), &InputEventMIDI::set_message);
- ClassDB::bind_method(D_METHOD("get_message"), &InputEventMIDI::get_message);
- ClassDB::bind_method(D_METHOD("set_pitch", "pitch"), &InputEventMIDI::set_pitch);
- ClassDB::bind_method(D_METHOD("get_pitch"), &InputEventMIDI::get_pitch);
- ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &InputEventMIDI::set_velocity);
- ClassDB::bind_method(D_METHOD("get_velocity"), &InputEventMIDI::get_velocity);
- ClassDB::bind_method(D_METHOD("set_instrument", "instrument"), &InputEventMIDI::set_instrument);
- ClassDB::bind_method(D_METHOD("get_instrument"), &InputEventMIDI::get_instrument);
- ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventMIDI::set_pressure);
- ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventMIDI::get_pressure);
- ClassDB::bind_method(D_METHOD("set_controller_number", "controller_number"), &InputEventMIDI::set_controller_number);
- ClassDB::bind_method(D_METHOD("get_controller_number"), &InputEventMIDI::get_controller_number);
- ClassDB::bind_method(D_METHOD("set_controller_value", "controller_value"), &InputEventMIDI::set_controller_value);
- ClassDB::bind_method(D_METHOD("get_controller_value"), &InputEventMIDI::get_controller_value);
- ADD_PROPERTY(PropertyInfo(Variant::INT, "channel"), "set_channel", "get_channel");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "message"), "set_message", "get_message");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "pitch"), "set_pitch", "get_pitch");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "velocity"), "set_velocity", "get_velocity");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "instrument"), "set_instrument", "get_instrument");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "pressure"), "set_pressure", "get_pressure");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "controller_number"), "set_controller_number", "get_controller_number");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "controller_value"), "set_controller_value", "get_controller_value");
- }
|