Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hardcoded shortcuts from /scene and instead use the input action system to allow them to be customised. #43663

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 25 additions & 162 deletions core/config/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "core/core_bind.h"
#include "core/core_string_names.h"
#include "core/input/input_map.h"
#include "core/io/file_access_network.h"
#include "core/io/file_access_pack.h"
#include "core/io/marshalls.h"
Expand Down Expand Up @@ -1057,17 +1058,35 @@ void ProjectSettings::_bind_methods() {
ClassDB::bind_method(D_METHOD("save_custom", "file"), &ProjectSettings::_save_custom_bnd);
}

void ProjectSettings::_add_builtin_input_map() {
if (InputMap::get_singleton()) {
OrderedHashMap<String, List<Ref<InputEvent>>> builtins = InputMap::get_singleton()->get_builtins();

for (OrderedHashMap<String, List<Ref<InputEvent>>>::Element E = builtins.front(); E; E = E.next()) {
Array events;

// Convert list of input events into array
for (List<Ref<InputEvent>>::Element *I = E.get().front(); I; I = I->next()) {
events.push_back(I->get());
}

Dictionary action;
action["deadzone"] = Variant(0.5f);
action["events"] = events;

String action_name = "input/" + E.key();
GLOBAL_DEF(action_name, action);
input_presets.push_back(action_name);
}
}
}

ProjectSettings::ProjectSettings() {
// Initialization of engine variables should be done in the setup() method,
// so that the values can be overridden from project.godot or project.binary.

singleton = this;

Array events;
Dictionary action;
Ref<InputEventKey> key;
Ref<InputEventJoypadButton> joyb;

GLOBAL_DEF_BASIC("application/config/name", "");
GLOBAL_DEF_BASIC("application/config/description", "");
custom_prop_info["application/config/description"] = PropertyInfo(Variant::STRING, "application/config/description", PROPERTY_HINT_MULTILINE_TEXT);
Expand All @@ -1094,163 +1113,7 @@ ProjectSettings::ProjectSettings() {
GLOBAL_DEF("editor/script/templates_search_path", "res://script_templates");
custom_prop_info["editor/script/templates_search_path"] = PropertyInfo(Variant::STRING, "editor/script/templates_search_path", PROPERTY_HINT_DIR);

action = Dictionary();
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
key->set_keycode(KEY_ENTER);
events.push_back(key);
key.instance();
key->set_keycode(KEY_KP_ENTER);
events.push_back(key);
key.instance();
key->set_keycode(KEY_SPACE);
events.push_back(key);
joyb.instance();
joyb->set_button_index(JOY_BUTTON_A);
events.push_back(joyb);
action["events"] = events;
GLOBAL_DEF("input/ui_accept", action);
input_presets.push_back("input/ui_accept");

action = Dictionary();
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
key->set_keycode(KEY_SPACE);
events.push_back(key);
joyb.instance();
joyb->set_button_index(JOY_BUTTON_Y);
events.push_back(joyb);
action["events"] = events;
GLOBAL_DEF("input/ui_select", action);
input_presets.push_back("input/ui_select");

action = Dictionary();
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
key->set_keycode(KEY_ESCAPE);
events.push_back(key);
joyb.instance();
joyb->set_button_index(JOY_BUTTON_B);
events.push_back(joyb);
action["events"] = events;
GLOBAL_DEF("input/ui_cancel", action);
input_presets.push_back("input/ui_cancel");

action = Dictionary();
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
key->set_keycode(KEY_TAB);
events.push_back(key);
action["events"] = events;
GLOBAL_DEF("input/ui_focus_next", action);
input_presets.push_back("input/ui_focus_next");

action = Dictionary();
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
key->set_keycode(KEY_TAB);
key->set_shift(true);
events.push_back(key);
action["events"] = events;
GLOBAL_DEF("input/ui_focus_prev", action);
input_presets.push_back("input/ui_focus_prev");

action = Dictionary();
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
key->set_keycode(KEY_LEFT);
events.push_back(key);
joyb.instance();
joyb->set_button_index(JOY_BUTTON_DPAD_LEFT);
events.push_back(joyb);
action["events"] = events;
GLOBAL_DEF("input/ui_left", action);
input_presets.push_back("input/ui_left");

action = Dictionary();
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
key->set_keycode(KEY_RIGHT);
events.push_back(key);
joyb.instance();
joyb->set_button_index(JOY_BUTTON_DPAD_RIGHT);
events.push_back(joyb);
action["events"] = events;
GLOBAL_DEF("input/ui_right", action);
input_presets.push_back("input/ui_right");

action = Dictionary();
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
key->set_keycode(KEY_UP);
events.push_back(key);
joyb.instance();
joyb->set_button_index(JOY_BUTTON_DPAD_UP);
events.push_back(joyb);
action["events"] = events;
GLOBAL_DEF("input/ui_up", action);
input_presets.push_back("input/ui_up");

action = Dictionary();
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
key->set_keycode(KEY_DOWN);
events.push_back(key);
joyb.instance();
joyb->set_button_index(JOY_BUTTON_DPAD_DOWN);
events.push_back(joyb);
action["events"] = events;
GLOBAL_DEF("input/ui_down", action);
input_presets.push_back("input/ui_down");

action = Dictionary();
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
key->set_keycode(KEY_PAGEUP);
events.push_back(key);
action["events"] = events;
GLOBAL_DEF("input/ui_page_up", action);
input_presets.push_back("input/ui_page_up");

action = Dictionary();
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
key->set_keycode(KEY_PAGEDOWN);
events.push_back(key);
action["events"] = events;
GLOBAL_DEF("input/ui_page_down", action);
input_presets.push_back("input/ui_page_down");

action = Dictionary();
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
key->set_keycode(KEY_HOME);
events.push_back(key);
action["events"] = events;
GLOBAL_DEF("input/ui_home", action);
input_presets.push_back("input/ui_home");

action = Dictionary();
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
key->set_keycode(KEY_END);
events.push_back(key);
action["events"] = events;
GLOBAL_DEF("input/ui_end", action);
input_presets.push_back("input/ui_end");
_add_builtin_input_map();

custom_prop_info["display/window/handheld/orientation"] = PropertyInfo(Variant::STRING, "display/window/handheld/orientation", PROPERTY_HINT_ENUM, "landscape,portrait,reverse_landscape,reverse_portrait,sensor_landscape,sensor_portrait,sensor");
custom_prop_info["rendering/driver/threads/thread_model"] = PropertyInfo(Variant::INT, "rendering/driver/threads/thread_model", PROPERTY_HINT_ENUM, "Single-Unsafe,Single-Safe,Multi-Threaded");
Expand Down
3 changes: 3 additions & 0 deletions core/config/project_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ class ProjectSettings : public Object {

Error _setup(const String &p_path, const String &p_main_pack, bool p_upwards = false);

void _add_builtin_input_map();

protected:
static void _bind_methods();

public:
Expand Down
16 changes: 8 additions & 8 deletions core/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,21 +604,21 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em
}
}

for (const Map<StringName, InputMap::Action>::Element *E = InputMap::get_singleton()->get_action_map().front(); E; E = E->next()) {
if (InputMap::get_singleton()->event_is_action(p_event, E->key())) {
for (OrderedHashMap<StringName, InputMap::Action>::ConstElement E = InputMap::get_singleton()->get_action_map().front(); E; E = E.next()) {
if (InputMap::get_singleton()->event_is_action(p_event, E.key())) {
// If not echo and action pressed state has changed
if (!p_event->is_echo() && is_action_pressed(E->key(), false) != p_event->is_action_pressed(E->key())) {
if (!p_event->is_echo() && is_action_pressed(E.key(), false) != p_event->is_action_pressed(E.key())) {
Action action;
action.physics_frame = Engine::get_singleton()->get_physics_frames();
action.process_frame = Engine::get_singleton()->get_process_frames();
action.pressed = p_event->is_action_pressed(E->key());
action.pressed = p_event->is_action_pressed(E.key());
action.strength = 0.0f;
action.raw_strength = 0.0f;
action.exact = InputMap::get_singleton()->event_is_action(p_event, E->key(), true);
action_state[E->key()] = action;
action.exact = InputMap::get_singleton()->event_is_action(p_event, E.key(), true);
action_state[E.key()] = action;
}
action_state[E->key()].strength = p_event->get_action_strength(E->key());
action_state[E->key()].raw_strength = p_event->get_action_raw_strength(E->key());
action_state[E.key()].strength = p_event->get_action_strength(E.key());
action_state[E.key()].raw_strength = p_event->get_action_raw_strength(E.key());
}
}

Expand Down
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
Loading