Skip to content

Commit 761f8df

Browse files
committed
Merge pull request #109016 from Rindbee/use-undo_redo-to-track-property-changes
Use `EditorUndoRedoManager` to track the property changes of the configured `InputEvent` in the plugin
2 parents 87bb5ab + 9540320 commit 761f8df

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

core/input/input_event.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ int64_t InputEventFromWindow::get_window_id() const {
153153
///////////////////////////////////
154154

155155
void InputEventWithModifiers::set_command_or_control_autoremap(bool p_enabled) {
156+
if (command_or_control_autoremap == p_enabled) {
157+
return;
158+
}
156159
command_or_control_autoremap = p_enabled;
157160
if (command_or_control_autoremap) {
158161
if (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) {
@@ -166,6 +169,7 @@ void InputEventWithModifiers::set_command_or_control_autoremap(bool p_enabled) {
166169
ctrl_pressed = false;
167170
meta_pressed = false;
168171
}
172+
notify_property_list_changed();
169173
emit_changed();
170174
}
171175

@@ -309,9 +313,11 @@ void InputEventWithModifiers::_validate_property(PropertyInfo &p_property) const
309313
// Cannot be used with Meta/Command or Control!
310314
if (p_property.name == "meta_pressed") {
311315
p_property.usage ^= PROPERTY_USAGE_STORAGE;
316+
p_property.usage ^= PROPERTY_USAGE_EDITOR;
312317
}
313318
if (p_property.name == "ctrl_pressed") {
314319
p_property.usage ^= PROPERTY_USAGE_STORAGE;
320+
p_property.usage ^= PROPERTY_USAGE_EDITOR;
315321
}
316322
} else {
317323
if (p_property.name == "command_or_control_autoremap") {

editor/inspector/input_event_editor_plugin.cpp

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include "input_event_editor_plugin.h"
3232

33+
#include "editor/editor_undo_redo_manager.h"
3334
#include "editor/settings/event_listener_line_edit.h"
3435
#include "editor/settings/input_event_configuration_dialog.h"
3536

@@ -51,8 +52,57 @@ void InputEventConfigContainer::_event_changed() {
5152

5253
void InputEventConfigContainer::_config_dialog_confirmed() {
5354
Ref<InputEvent> ie = config_dialog->get_event();
54-
input_event->copy_from(ie);
55-
_event_changed();
55+
56+
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
57+
undo_redo->create_action(TTR("Event Configured"));
58+
59+
// When command_or_control_autoremap is toggled to false, it should be set first;
60+
// and when it is toggled to true, it should be set last.
61+
bool will_toggle = false;
62+
bool pending = false;
63+
Ref<InputEventWithModifiers> iewm = input_event;
64+
if (iewm.is_valid()) {
65+
Variant new_value = ie->get("command_or_control_autoremap");
66+
will_toggle = new_value != input_event->get("command_or_control_autoremap");
67+
if (will_toggle) {
68+
pending = new_value;
69+
if (pending) {
70+
undo_redo->add_undo_property(input_event.ptr(), "command_or_control_autoremap", !pending);
71+
} else {
72+
undo_redo->add_do_property(input_event.ptr(), "command_or_control_autoremap", pending);
73+
}
74+
}
75+
}
76+
77+
List<PropertyInfo> pi;
78+
ie->get_property_list(&pi);
79+
for (const PropertyInfo &E : pi) {
80+
if (E.name == "resource_path") {
81+
continue; // Do not change path.
82+
}
83+
if (E.name == "command_or_control_autoremap") {
84+
continue; // Handle it separately.
85+
}
86+
Variant old_value = input_event->get(E.name);
87+
Variant new_value = ie->get(E.name);
88+
if (old_value == new_value) {
89+
continue;
90+
}
91+
undo_redo->add_do_property(input_event.ptr(), E.name, new_value);
92+
undo_redo->add_undo_property(input_event.ptr(), E.name, old_value);
93+
}
94+
95+
if (will_toggle) {
96+
if (pending) {
97+
undo_redo->add_do_property(input_event.ptr(), "command_or_control_autoremap", pending);
98+
} else {
99+
undo_redo->add_undo_property(input_event.ptr(), "command_or_control_autoremap", !pending);
100+
}
101+
}
102+
103+
undo_redo->add_do_property(input_event_text, "text", ie->as_text());
104+
undo_redo->add_undo_property(input_event_text, "text", input_event->as_text());
105+
undo_redo->commit_action();
56106
}
57107

58108
void InputEventConfigContainer::set_event(const Ref<InputEvent> &p_event) {

0 commit comments

Comments
 (0)