Merge pull request #102889 from ryevdokimov/add-editorsettings-shortcuts

Add ability to add new EditorSettings shortcuts
This commit is contained in:
Thaddeus Crews
2025-11-14 14:23:11 -06:00
3 changed files with 133 additions and 19 deletions

View File

@ -70,6 +70,33 @@
[/codeblocks] [/codeblocks]
</description> </description>
</method> </method>
<method name="add_shortcut">
<return type="void" />
<param index="0" name="path" type="String" />
<param index="1" name="shortcut" type="Shortcut" />
<description>
Adds a [param shortcut] whose path is specified by [param path].
The [param path] determines how the shortcut is organized and displayed in the editor's shortcut settings. The path format affects the display as follows:
- [code]"name"[/code] (no slash): Creates a category named [code]name[/code] with the shortcut displayed as [code]name[/code].
- [code]"category/name"[/code] (single slash): Displays as [code]name[/code] in the [code]category[/code] section.
- [code]"category/name/extra"[/code] (multiple slashes): Extra path components are ignored, so this behaves the same as [code]"category/name"[/code].
[b]Note:[/b] Shortcuts are only saved to the editor settings if they differ from their original/default state. This means empty shortcuts that were originally empty will not persist between editor sessions and must be re-added. If a shortcut with the same [param path] already exists, this method will update it with the new [param shortcut] instead of creating a duplicate.
[codeblock]
# Add a custom shortcut for a plugin action.
var my_shortcut = Shortcut.new()
var input_event = InputEventKey.new()
input_event.keycode = KEY_F5
input_event.ctrl_pressed = true
my_shortcut.events.append(input_event)
# This will appear under the "My Plugin" category as "Reload Data".
EditorInterface.get_editor_settings().add_shortcut("my_plugin/reload_data", my_shortcut)
# This will appear under the "Test Action" category as "Test Action".
EditorInterface.get_editor_settings().add_shortcut("test_action", my_shortcut)
[/codeblock]
</description>
</method>
<method name="check_changed_settings_in_group" qualifiers="const"> <method name="check_changed_settings_in_group" qualifiers="const">
<return type="bool" /> <return type="bool" />
<param index="0" name="setting_prefix" type="String" /> <param index="0" name="setting_prefix" type="String" />
@ -118,6 +145,19 @@
Returns the value of the setting specified by [param name]. This is equivalent to using [method Object.get] on the EditorSettings instance. Returns the value of the setting specified by [param name]. This is equivalent to using [method Object.get] on the EditorSettings instance.
</description> </description>
</method> </method>
<method name="get_shortcut" qualifiers="const">
<return type="Shortcut" />
<param index="0" name="path" type="String" />
<description>
Returns the shortcut specified by [param path]. Tries to find a built-in action if no shortcut with the provided path is found in the shortcut list. If found, adds it to the list and returns it, otherwise returns [code]null[/code].
</description>
</method>
<method name="get_shortcut_list">
<return type="PackedStringArray" />
<description>
Returns the list of stored shortcut paths.
</description>
</method>
<method name="has_setting" qualifiers="const"> <method name="has_setting" qualifiers="const">
<return type="bool" /> <return type="bool" />
<param index="0" name="name" type="String" /> <param index="0" name="name" type="String" />
@ -125,6 +165,21 @@
Returns [code]true[/code] if the setting specified by [param name] exists, [code]false[/code] otherwise. Returns [code]true[/code] if the setting specified by [param name] exists, [code]false[/code] otherwise.
</description> </description>
</method> </method>
<method name="has_shortcut" qualifiers="const">
<return type="bool" />
<param index="0" name="path" type="String" />
<description>
Returns [code]true[/code] if the shortcut specified by [param path] exists, [code]false[/code] otherwise.
</description>
</method>
<method name="is_shortcut" qualifiers="const">
<return type="bool" />
<param index="0" name="path" type="String" />
<param index="1" name="event" type="InputEvent" />
<description>
Returns [code]true[/code] if the shortcut specified by [param path] matches the event specified by [param event], [code]false[/code] otherwise.
</description>
</method>
<method name="mark_setting_changed"> <method name="mark_setting_changed">
<return type="void" /> <return type="void" />
<param index="0" name="setting" type="String" /> <param index="0" name="setting" type="String" />
@ -132,6 +187,13 @@
Marks the passed editor setting as being changed, see [method get_changed_settings]. Only settings which exist (see [method has_setting]) will be accepted. Marks the passed editor setting as being changed, see [method get_changed_settings]. Only settings which exist (see [method has_setting]) will be accepted.
</description> </description>
</method> </method>
<method name="remove_shortcut">
<return type="void" />
<param index="0" name="path" type="String" />
<description>
Removes the shortcut specified by [param path].
</description>
</method>
<method name="set_builtin_action_override"> <method name="set_builtin_action_override">
<return type="void" /> <return type="void" />
<param index="0" name="name" type="String" /> <param index="0" name="name" type="String" />

View File

@ -1898,24 +1898,54 @@ String EditorSettings::get_language() const {
// Shortcuts // Shortcuts
void EditorSettings::_add_shortcut_default(const String &p_name, const Ref<Shortcut> &p_shortcut) { void EditorSettings::_add_shortcut_default(const String &p_path, const Ref<Shortcut> &p_shortcut) {
shortcuts[p_name] = p_shortcut; shortcuts[p_path] = p_shortcut;
} }
void EditorSettings::add_shortcut(const String &p_name, const Ref<Shortcut> &p_shortcut) { void EditorSettings::add_shortcut(const String &p_path, const Ref<Shortcut> &p_shortcut) {
shortcuts[p_name] = p_shortcut; Array use_events = p_shortcut->get_events();
shortcuts[p_name]->set_meta("customized", true); if (shortcuts.has(p_path)) {
Ref<Shortcut> existing = shortcuts.get(p_path);
if (!existing->has_meta("original")) {
// Loaded from editor settings, but plugin not loaded yet.
// Keep the events from editor settings but still override the shortcut in the shortcuts map
use_events = existing->get_events();
} else if (!Shortcut::is_event_array_equal(existing->get_events(), existing->get_meta("original"))) {
// Shortcut exists and is customized - don't override with default.
return;
}
} }
bool EditorSettings::is_shortcut(const String &p_name, const Ref<InputEvent> &p_event) const { p_shortcut->set_meta("original", p_shortcut->get_events());
HashMap<String, Ref<Shortcut>>::ConstIterator E = shortcuts.find(p_name); p_shortcut->set_events(use_events);
ERR_FAIL_COND_V_MSG(!E, false, "Unknown Shortcut: " + p_name + "."); if (p_shortcut->get_name().is_empty()) {
String shortcut_name = p_path.get_slicec('/', 1);
if (shortcut_name.is_empty()) {
shortcut_name = p_path;
}
p_shortcut->set_name(shortcut_name);
}
shortcuts[p_path] = p_shortcut;
shortcuts[p_path]->set_meta("customized", true);
}
void EditorSettings::remove_shortcut(const String &p_path) {
shortcuts.erase(p_path);
}
bool EditorSettings::is_shortcut(const String &p_path, const Ref<InputEvent> &p_event) const {
HashMap<String, Ref<Shortcut>>::ConstIterator E = shortcuts.find(p_path);
ERR_FAIL_COND_V_MSG(!E, false, "Unknown Shortcut: " + p_path + ".");
return E->value->matches_event(p_event); return E->value->matches_event(p_event);
} }
Ref<Shortcut> EditorSettings::get_shortcut(const String &p_name) const { bool EditorSettings::has_shortcut(const String &p_path) const {
HashMap<String, Ref<Shortcut>>::ConstIterator SC = shortcuts.find(p_name); return get_shortcut(p_path).is_valid();
}
Ref<Shortcut> EditorSettings::get_shortcut(const String &p_path) const {
HashMap<String, Ref<Shortcut>>::ConstIterator SC = shortcuts.find(p_path);
if (SC) { if (SC) {
return SC->value; return SC->value;
} }
@ -1924,32 +1954,42 @@ Ref<Shortcut> EditorSettings::get_shortcut(const String &p_name) const {
// Use the first item in the action list for the shortcut event, since a shortcut can only have 1 linked event. // Use the first item in the action list for the shortcut event, since a shortcut can only have 1 linked event.
Ref<Shortcut> sc; Ref<Shortcut> sc;
HashMap<String, List<Ref<InputEvent>>>::ConstIterator builtin_override = builtin_action_overrides.find(p_name); HashMap<String, List<Ref<InputEvent>>>::ConstIterator builtin_override = builtin_action_overrides.find(p_path);
if (builtin_override) { if (builtin_override) {
sc.instantiate(); sc.instantiate();
sc->set_events_list(&builtin_override->value); sc->set_events_list(&builtin_override->value);
sc->set_name(InputMap::get_singleton()->get_builtin_display_name(p_name)); sc->set_name(InputMap::get_singleton()->get_builtin_display_name(p_path));
} }
// If there was no override, check the default builtins to see if it has an InputEvent for the provided name. // If there was no override, check the default builtins to see if it has an InputEvent for the provided name.
if (sc.is_null()) { if (sc.is_null()) {
HashMap<String, List<Ref<InputEvent>>>::ConstIterator builtin_default = InputMap::get_singleton()->get_builtins_with_feature_overrides_applied().find(p_name); HashMap<String, List<Ref<InputEvent>>>::ConstIterator builtin_default = InputMap::get_singleton()->get_builtins_with_feature_overrides_applied().find(p_path);
if (builtin_default) { if (builtin_default) {
sc.instantiate(); sc.instantiate();
sc->set_events_list(&builtin_default->value); sc->set_events_list(&builtin_default->value);
sc->set_name(InputMap::get_singleton()->get_builtin_display_name(p_name)); sc->set_name(InputMap::get_singleton()->get_builtin_display_name(p_path));
} }
} }
if (sc.is_valid()) { if (sc.is_valid()) {
// Add the shortcut to the list. // Add the shortcut to the list.
shortcuts[p_name] = sc; shortcuts[p_path] = sc;
return sc; return sc;
} }
return Ref<Shortcut>(); return Ref<Shortcut>();
} }
Vector<String> EditorSettings::_get_shortcut_list() {
List<String> shortcut_list;
get_shortcut_list(&shortcut_list);
Vector<String> ret;
for (const String &shortcut : shortcut_list) {
ret.push_back(shortcut);
}
return ret;
}
void EditorSettings::get_shortcut_list(List<String> *r_shortcuts) { void EditorSettings::get_shortcut_list(List<String> *r_shortcuts) {
for (const KeyValue<String, Ref<Shortcut>> &E : shortcuts) { for (const KeyValue<String, Ref<Shortcut>> &E : shortcuts) {
r_shortcuts->push_back(E.key); r_shortcuts->push_back(E.key);
@ -2202,6 +2242,13 @@ void EditorSettings::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_builtin_action_override", "name", "actions_list"), &EditorSettings::set_builtin_action_override); ClassDB::bind_method(D_METHOD("set_builtin_action_override", "name", "actions_list"), &EditorSettings::set_builtin_action_override);
ClassDB::bind_method(D_METHOD("add_shortcut", "path", "shortcut"), &EditorSettings::add_shortcut);
ClassDB::bind_method(D_METHOD("remove_shortcut", "path"), &EditorSettings::remove_shortcut);
ClassDB::bind_method(D_METHOD("is_shortcut", "path", "event"), &EditorSettings::is_shortcut);
ClassDB::bind_method(D_METHOD("has_shortcut", "path"), &EditorSettings::has_shortcut);
ClassDB::bind_method(D_METHOD("get_shortcut", "path"), &EditorSettings::get_shortcut);
ClassDB::bind_method(D_METHOD("get_shortcut_list"), &EditorSettings::_get_shortcut_list);
ClassDB::bind_method(D_METHOD("check_changed_settings_in_group", "setting_prefix"), &EditorSettings::check_changed_settings_in_group); ClassDB::bind_method(D_METHOD("check_changed_settings_in_group", "setting_prefix"), &EditorSettings::check_changed_settings_in_group);
ClassDB::bind_method(D_METHOD("get_changed_settings"), &EditorSettings::get_changed_settings); ClassDB::bind_method(D_METHOD("get_changed_settings"), &EditorSettings::get_changed_settings);
ClassDB::bind_method(D_METHOD("mark_setting_changed", "setting"), &EditorSettings::mark_setting_changed); ClassDB::bind_method(D_METHOD("mark_setting_changed", "setting"), &EditorSettings::mark_setting_changed);

View File

@ -127,6 +127,9 @@ private:
void _remove_deprecated_settings(); void _remove_deprecated_settings();
#endif #endif
// Bind helpers.
Vector<String> _get_shortcut_list();
protected: protected:
static void _bind_methods(); static void _bind_methods();
@ -191,10 +194,12 @@ public:
static float get_auto_display_scale(); static float get_auto_display_scale();
String get_language() const; String get_language() const;
void _add_shortcut_default(const String &p_name, const Ref<Shortcut> &p_shortcut); void _add_shortcut_default(const String &p_path, const Ref<Shortcut> &p_shortcut);
void add_shortcut(const String &p_name, const Ref<Shortcut> &p_shortcut); void add_shortcut(const String &p_path, const Ref<Shortcut> &p_shortcut);
bool is_shortcut(const String &p_name, const Ref<InputEvent> &p_event) const; void remove_shortcut(const String &p_path);
Ref<Shortcut> get_shortcut(const String &p_name) const; bool is_shortcut(const String &p_path, const Ref<InputEvent> &p_event) const;
bool has_shortcut(const String &p_path) const;
Ref<Shortcut> get_shortcut(const String &p_path) const;
void get_shortcut_list(List<String> *r_shortcuts); void get_shortcut_list(List<String> *r_shortcuts);
void set_builtin_action_override(const String &p_name, const TypedArray<InputEvent> &p_events); void set_builtin_action_override(const String &p_name, const TypedArray<InputEvent> &p_events);