Browse Source

Added convenience create_reference methods for Key and JoyButton inputs

Eric M 4 years ago
parent
commit
ca1abc7352
2 changed files with 37 additions and 0 deletions
  1. 33 0
      core/input/input_event.cpp
  2. 4 0
      core/input/input_event.h

+ 33 - 0
core/input/input_event.cpp

@@ -384,6 +384,31 @@ String InputEventKey::to_string() {
 	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()) {
@@ -1011,6 +1036,14 @@ 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);

+ 4 - 0
core/input/input_event.h

@@ -260,6 +260,8 @@ public:
 	virtual String as_text() const override;
 	virtual String to_string() override;
 
+	static Ref<InputEventKey> create_reference(uint32_t p_keycode_with_modifier_masks);
+
 	InputEventKey() {}
 };
 
@@ -406,6 +408,8 @@ public:
 	virtual String as_text() const override;
 	virtual String to_string() override;
 
+	static Ref<InputEventJoypadButton> create_reference(int p_btn_index);
+
 	InputEventJoypadButton() {}
 };