Skip to content

Commit

Permalink
Added convenience create_reference methods for Key and JoyButton inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
EricEzaM committed Feb 18, 2021
1 parent 5c2fe97 commit ca1abc7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
33 changes: 33 additions & 0 deletions core/input/input_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions core/input/input_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ class InputEventKey : public InputEventWithModifiers {
virtual String as_text() const override;
virtual String to_string() override;

static Ref<InputEventKey> create_reference(uint32_t p_keycode_with_modifier_masks);

InputEventKey() {}
};

Expand Down Expand Up @@ -406,6 +408,8 @@ class InputEventJoypadButton : public InputEvent {
virtual String as_text() const override;
virtual String to_string() override;

static Ref<InputEventJoypadButton> create_reference(int p_btn_index);

InputEventJoypadButton() {}
};

Expand Down

0 comments on commit ca1abc7

Please sign in to comment.