Allow shortcuts to have any number of bindings. Updated UI as required.

This commit is contained in:
Eric M
2021-06-21 11:34:50 +10:00
parent 4baddc1126
commit ad30b0a8dd
9 changed files with 483 additions and 278 deletions
+70 -14
View File
@@ -31,14 +31,26 @@
#include "shortcut.h"
#include "core/os/keyboard.h"
void Shortcut::set_event(const Ref<InputEvent> &p_event) {
ERR_FAIL_COND_MSG(Object::cast_to<InputEventShortcut>(*p_event), "Cannot set a shortcut event to an instance of InputEventShortcut.");
event = p_event;
void Shortcut::set_events(const Array &p_events) {
for (int i = 0; i < p_events.size(); i++) {
Ref<InputEventShortcut> ies = p_events[i];
ERR_FAIL_COND_MSG(ies.is_valid(), "Cannot set a shortcut event to an instance of InputEventShortcut.");
}
events = p_events;
emit_changed();
}
Ref<InputEvent> Shortcut::get_event() const {
return event;
void Shortcut::set_events_list(const List<Ref<InputEvent>> *p_events) {
events.clear();
for (const Ref<InputEvent> &ie : *p_events) {
events.push_back(ie);
}
}
Array Shortcut::get_events() const {
return events;
}
bool Shortcut::matches_event(const Ref<InputEvent> &p_event) const {
@@ -48,29 +60,73 @@ bool Shortcut::matches_event(const Ref<InputEvent> &p_event) const {
return true;
}
}
return event.is_valid() && event->is_match(p_event, true);
for (int i = 0; i < events.size(); i++) {
Ref<InputEvent> ie = events[i];
bool valid = ie.is_valid() && ie->is_match(p_event);
// Stop on first valid event - don't need to check further.
if (valid) {
return true;
}
}
return false;
}
String Shortcut::get_as_text() const {
if (event.is_valid()) {
return event->as_text();
} else {
return "None";
for (int i = 0; i < events.size(); i++) {
Ref<InputEvent> ie = events[i];
// Return first shortcut which is valid
if (ie.is_valid()) {
return ie->as_text();
}
}
return "None";
}
bool Shortcut::has_valid_event() const {
return event.is_valid();
// Tests if there is ANY input event which is valid.
for (int i = 0; i < events.size(); i++) {
Ref<InputEvent> ie = events[i];
if (ie.is_valid()) {
return true;
}
}
return false;
}
void Shortcut::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_event", "event"), &Shortcut::set_event);
ClassDB::bind_method(D_METHOD("get_event"), &Shortcut::get_event);
ClassDB::bind_method(D_METHOD("set_events", "events"), &Shortcut::set_events);
ClassDB::bind_method(D_METHOD("get_events"), &Shortcut::get_events);
ClassDB::bind_method(D_METHOD("has_valid_event"), &Shortcut::has_valid_event);
ClassDB::bind_method(D_METHOD("matches_event", "event"), &Shortcut::matches_event);
ClassDB::bind_method(D_METHOD("get_as_text"), &Shortcut::get_as_text);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"), "set_event", "get_event");
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "events", PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")), "set_events", "get_events");
}
bool Shortcut::is_event_array_equal(const Array &p_event_array1, const Array &p_event_array2) {
if (p_event_array1.size() != p_event_array2.size()) {
return false;
}
bool is_same = true;
for (int i = 0; i < p_event_array1.size(); i++) {
Ref<InputEvent> ie_1 = p_event_array1[i];
Ref<InputEvent> ie_2 = p_event_array2[i];
is_same = ie_1->is_match(ie_2);
// Break on the first that doesn't match - don't need to check further.
if (!is_same) {
break;
}
}
return is_same;
}
+8 -4
View File
@@ -37,18 +37,22 @@
class Shortcut : public Resource {
GDCLASS(Shortcut, Resource);
Ref<InputEvent> event;
Array events;
protected:
static void _bind_methods();
public:
void set_event(const Ref<InputEvent> &p_shortcut);
Ref<InputEvent> get_event() const;
void set_events(const Array &p_events);
Array get_events() const;
void set_events_list(const List<Ref<InputEvent>> *p_events);
bool matches_event(const Ref<InputEvent> &p_event) const;
bool has_valid_event() const;
String get_as_text() const;
};
static bool is_event_array_equal(const Array &p_event_array1, const Array &p_event_array2);
};
#endif // SHORTCUT_H
+3 -3
View File
@@ -742,10 +742,10 @@
</method>
<method name="set_drag_forwarding">
<return type="void" />
<argument index="0" name="target" type="Control" />
<argument index="0" name="target" type="Node" />
<description>
Forwards the handling of this control's drag and drop to [code]target[/code] control.
Forwarding can be implemented in the target control similar to the methods [method _get_drag_data], [method _can_drop_data], and [method _drop_data] but with two differences:
Forwards the handling of this control's drag and drop to [code]target[/code] node.
Forwarding can be implemented in the target node similar to the methods [method _get_drag_data], [method _can_drop_data], and [method _drop_data] but with two differences:
1. The function name must be suffixed with [b]_fw[/b]
2. The function must take an extra argument that is the control doing the forwarding
[codeblocks]
+7 -7
View File
@@ -4,8 +4,8 @@
A shortcut for binding input.
</brief_description>
<description>
A shortcut for binding input.
Shortcuts are commonly used for interacting with a [Control] element from an [InputEvent] (also known as hotkeys).
One shortcut can contain multiple [InputEvent]'s, allowing the possibility of triggering one action with multiple different inputs.
</description>
<tutorials>
</tutorials>
@@ -13,27 +13,27 @@
<method name="get_as_text" qualifiers="const">
<return type="String" />
<description>
Returns the shortcut's [InputEvent] as a [String].
Returns the shortcut's first valid [InputEvent] as a [String].
</description>
</method>
<method name="has_valid_event" qualifiers="const">
<return type="bool" />
<description>
Returns whether the shortcut has a valid [member event] assigned to it.
Returns whether [member events] contains an [InputEvent] which is valid.
</description>
</method>
<method name="matches_event" qualifiers="const">
<return type="bool" />
<argument index="0" name="event" type="InputEvent" />
<description>
Returns whether the shortcut's [member event] matches [code]event[/code].
Returns whether any [InputEvent] in [member events] equals [code]event[/code].
</description>
</method>
</methods>
<members>
<member name="event" type="InputEvent" setter="set_event" getter="get_event">
The shortcut's [InputEvent].
Generally the [InputEvent] is a keyboard key, though it can be any [InputEvent], including an [InputEventAction].
<member name="events" type="Array" setter="set_events" getter="get_events" default="[]">
The shortcut's [InputEvent] array.
Generally the [InputEvent] used is an [InputEventKey], though it can be any [InputEvent], including an [InputEventAction].
</member>
</members>
</class>
+111 -48
View File
@@ -73,14 +73,15 @@ bool EditorSettings::_set_only(const StringName &p_name, const Variant &p_value)
if (p_name == "shortcuts") {
Array arr = p_value;
ERR_FAIL_COND_V(arr.size() && arr.size() & 1, true);
for (int i = 0; i < arr.size(); i += 2) {
String name = arr[i];
Ref<InputEvent> shortcut = arr[i + 1];
for (int i = 0; i < arr.size(); i++) {
Dictionary dict = arr[i];
String name = dict["name"];
Array shortcut_events = dict["shortcuts"];
Ref<Shortcut> sc;
sc.instantiate();
sc->set_event(shortcut);
sc->set_events(shortcut_events);
add_shortcut(name, sc);
}
@@ -138,11 +139,11 @@ bool EditorSettings::_get(const StringName &p_name, Variant &r_ret) const {
_THREAD_SAFE_METHOD_
if (p_name == "shortcuts") {
Array arr;
for (const KeyValue<String, Ref<Shortcut>> &E : shortcuts) {
Ref<Shortcut> sc = E.value;
Array save_array;
for (const KeyValue<String, Ref<Shortcut>> &shortcut_definition : shortcuts) {
Ref<Shortcut> sc = shortcut_definition.value;
if (builtin_action_overrides.has(E.key)) {
if (builtin_action_overrides.has(shortcut_definition.key)) {
// This shortcut was auto-generated from built in actions: don't save.
continue;
}
@@ -151,34 +152,57 @@ bool EditorSettings::_get(const StringName &p_name, Variant &r_ret) const {
if (!sc->has_meta("original")) {
continue; //this came from settings but is not any longer used
}
Ref<InputEvent> original = sc->get_meta("original");
if (sc->matches_event(original) || (original.is_null() && sc->get_event().is_null())) {
continue; //not changed from default, don't save
}
}
arr.push_back(E.key);
arr.push_back(sc->get_event());
Array original_events = sc->get_meta("original");
Array shortcut_events = sc->get_events();
bool is_same = Shortcut::is_event_array_equal(original_events, shortcut_events);
if (is_same) {
continue; // Not changed from default; don't save.
}
Dictionary dict;
dict["name"] = shortcut_definition.key;
dict["shortcuts"] = shortcut_events;
save_array.push_back(dict);
}
r_ret = arr;
r_ret = save_array;
return true;
} else if (p_name == "builtin_action_overrides") {
Array actions_arr;
for (const KeyValue<String, List<Ref<InputEvent>>> &E : builtin_action_overrides) {
List<Ref<InputEvent>> events = E.value;
for (const KeyValue<String, List<Ref<InputEvent>>> &action_override : builtin_action_overrides) {
List<Ref<InputEvent>> events = action_override.value;
// TODO: skip actions which are the same as the builtin.
Dictionary action_dict;
action_dict["name"] = E.key;
action_dict["name"] = action_override.key;
// Convert the list to an array, and only keep key events as this is for the editor.
Array events_arr;
for (List<Ref<InputEvent>>::Element *I = events.front(); I; I = I->next()) {
events_arr.push_back(I->get());
for (const Ref<InputEvent> &ie : events) {
Ref<InputEventKey> iek = ie;
if (iek.is_valid()) {
events_arr.append(iek);
}
}
Array defaults_arr;
List<Ref<InputEvent>> defaults = InputMap::get_singleton()->get_builtins()[action_override.key];
for (const Ref<InputEvent> &default_input_event : defaults) {
if (default_input_event.is_valid()) {
defaults_arr.append(default_input_event);
}
}
bool same = Shortcut::is_event_array_equal(events_arr, defaults_arr);
// Don't save if same as default.
if (same) {
continue;
}
action_dict["events"] = events_arr;
actions_arr.push_back(action_dict);
}
@@ -1405,7 +1429,7 @@ Ref<Shortcut> EditorSettings::get_shortcut(const String &p_name) const {
const Map<String, List<Ref<InputEvent>>>::Element *builtin_override = builtin_action_overrides.find(p_name);
if (builtin_override) {
sc.instantiate();
sc->set_event(builtin_override->get().front()->get());
sc->set_events_list(&builtin_override->get());
sc->set_name(InputMap::get_singleton()->get_builtin_display_name(p_name));
}
@@ -1414,7 +1438,7 @@ Ref<Shortcut> EditorSettings::get_shortcut(const String &p_name) const {
const OrderedHashMap<String, List<Ref<InputEvent>>>::ConstElement builtin_default = InputMap::get_singleton()->get_builtins_with_feature_overrides_applied().find(p_name);
if (builtin_default) {
sc.instantiate();
sc->set_event(builtin_default.get().front()->get());
sc->set_events_list(&builtin_default.get());
sc->set_name(InputMap::get_singleton()->get_builtin_display_name(p_name));
}
}
@@ -1450,52 +1474,91 @@ void ED_SHORTCUT_OVERRIDE(const String &p_path, const String &p_feature, Key p_k
Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(p_path);
ERR_FAIL_COND_MSG(!sc.is_valid(), "Used ED_SHORTCUT_OVERRIDE with invalid shortcut: " + p_path + ".");
// Only add the override if the OS supports the provided feature.
if (OS::get_singleton()->has_feature(p_feature)) {
Ref<InputEventKey> ie;
if (p_keycode) {
ie = InputEventKey::create_reference(p_keycode);
}
PackedInt32Array arr;
arr.push_back(p_keycode);
// Directly override the existing shortcut.
sc->set_event(ie);
sc->set_meta("original", ie);
ED_SHORTCUT_OVERRIDE_ARRAY(p_path, p_feature, arr);
}
void ED_SHORTCUT_OVERRIDE_ARRAY(const String &p_path, const String &p_feature, const PackedInt32Array &p_keycodes) {
Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(p_path);
ERR_FAIL_COND_MSG(!sc.is_valid(), "Used ED_SHORTCUT_OVERRIDE_ARRAY with invalid shortcut: " + p_path + ".");
// Only add the override if the OS supports the provided feature.
if (!OS::get_singleton()->has_feature(p_feature)) {
return;
}
Array events;
for (int i = 0; i < p_keycodes.size(); i++) {
Key keycode = (Key)p_keycodes[i];
#ifdef OSX_ENABLED
// Use Cmd+Backspace as a general replacement for Delete shortcuts on macOS
if (keycode == KEY_DELETE) {
keycode = KEY_MASK_CMD | KEY_BACKSPACE;
}
#endif
Ref<InputEventKey> ie;
if (keycode) {
ie = InputEventKey::create_reference(keycode);
events.push_back(ie);
}
}
// Directly override the existing shortcut.
sc->set_events(events);
sc->set_meta("original", events);
}
Ref<Shortcut> ED_SHORTCUT(const String &p_path, const String &p_name, Key p_keycode) {
PackedInt32Array arr;
arr.push_back(p_keycode);
return ED_SHORTCUT_ARRAY(p_path, p_name, arr);
}
Ref<Shortcut> ED_SHORTCUT_ARRAY(const String &p_path, const String &p_name, const PackedInt32Array &p_keycodes) {
Array events;
for (int i = 0; i < p_keycodes.size(); i++) {
Key keycode = (Key)p_keycodes[i];
#ifdef OSX_ENABLED
// Use Cmd+Backspace as a general replacement for Delete shortcuts on macOS
if (p_keycode == KEY_DELETE) {
p_keycode = KEY_MASK_CMD | KEY_BACKSPACE;
}
// Use Cmd+Backspace as a general replacement for Delete shortcuts on macOS
if (keycode == KEY_DELETE) {
keycode = KEY_MASK_CMD | KEY_BACKSPACE;
}
#endif
Ref<InputEventKey> ie;
if (p_keycode) {
ie = InputEventKey::create_reference(p_keycode);
Ref<InputEventKey> ie;
if (keycode) {
ie = InputEventKey::create_reference(keycode);
events.push_back(ie);
}
}
if (!EditorSettings::get_singleton()) {
Ref<Shortcut> sc;
sc.instantiate();
sc->set_name(p_name);
sc->set_event(ie);
sc->set_meta("original", ie);
sc->set_events(events);
sc->set_meta("original", events);
return sc;
}
Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(p_path);
if (sc.is_valid()) {
sc->set_name(p_name); //keep name (the ones that come from disk have no name)
sc->set_meta("original", ie); //to compare against changes
sc->set_meta("original", events); //to compare against changes
return sc;
}
sc.instantiate();
sc->set_name(p_name);
sc->set_event(ie);
sc->set_meta("original", ie); //to compare against changes
sc->set_events(events);
sc->set_meta("original", events); //to compare against changes
EditorSettings::get_singleton()->add_shortcut(p_path, sc);
return sc;
@@ -1550,7 +1613,7 @@ void EditorSettings::set_builtin_action_override(const String &p_name, const Arr
// Update the shortcut (if it is used somewhere in the editor) to be the first event of the new list.
if (shortcuts.has(p_name)) {
shortcuts[p_name]->set_event(event_list.front()->get());
shortcuts[p_name]->set_events_list(&event_list);
}
}
+2
View File
@@ -201,7 +201,9 @@ Variant _EDITOR_GET(const String &p_setting);
#define ED_IS_SHORTCUT(p_name, p_ev) (EditorSettings::get_singleton()->is_shortcut(p_name, p_ev))
Ref<Shortcut> ED_SHORTCUT(const String &p_path, const String &p_name, Key p_keycode = KEY_NONE);
Ref<Shortcut> ED_SHORTCUT_ARRAY(const String &p_path, const String &p_name, const PackedInt32Array &p_keycodes);
void ED_SHORTCUT_OVERRIDE(const String &p_path, const String &p_feature, Key p_keycode = KEY_NONE);
void ED_SHORTCUT_OVERRIDE_ARRAY(const String &p_path, const String &p_feature, const PackedInt32Array &p_keycodes);
Ref<Shortcut> ED_GET_SHORTCUT(const String &p_path);
#endif // EDITOR_SETTINGS_H
+8 -2
View File
@@ -2483,8 +2483,14 @@ static bool is_shortcut_pressed(const String &p_path) {
if (shortcut.is_null()) {
return false;
}
InputEventKey *k = Object::cast_to<InputEventKey>(shortcut->get_event().ptr());
if (k == nullptr) {
const Array shortcuts = shortcut->get_events();
Ref<InputEventKey> k;
if (shortcuts.size() > 0) {
k = shortcuts.front();
}
if (k.is_null()) {
return false;
}
const Input &input = *Input::get_singleton();
File diff suppressed because it is too large Load Diff
+18 -12
View File
@@ -53,29 +53,28 @@ class EditorSettingsDialog : public AcceptDialog {
LineEdit *shortcut_search_box;
SectionedInspector *inspector;
// Shortcuts
enum ShortcutButton {
SHORTCUT_ADD,
SHORTCUT_EDIT,
SHORTCUT_ERASE,
SHORTCUT_REVERT
};
int button_idx;
int current_action_event_index = -1;
bool editing_action = false;
String current_action;
Array current_action_events;
PopupMenu *action_popup;
Tree *shortcuts;
String shortcut_filter;
InputEventConfigurationDialog *shortcut_editor;
bool is_editing_action = false;
String current_edited_identifier;
Array current_events;
int current_event_index = -1;
Timer *timer;
UndoRedo *undo_redo;
// Shortcuts
String shortcut_filter;
Tree *shortcuts;
InputEventConfigurationDialog *shortcut_editor;
String shortcut_being_edited;
virtual void cancel_pressed() override;
virtual void ok_pressed() override;
@@ -89,7 +88,14 @@ class EditorSettingsDialog : public AcceptDialog {
void _event_config_confirmed();
void _create_shortcut_treeitem(TreeItem *p_parent, const String &p_shortcut_identifier, const String &p_display, Array &p_events, bool p_allow_revert, bool p_is_common, bool p_is_collapsed);
Array _event_list_to_array_helper(List<Ref<InputEvent>> &p_events);
void _update_builtin_action(const String &p_name, const Array &p_events);
void _update_shortcut_events(const String &p_path, const Array &p_events);
Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
void _tabs_tab_changed(int p_tab);
void _focus_current_search_box();