| 1 | /**************************************************************************/ |
| 2 | /* input_event_editor_plugin.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "input_event_editor_plugin.h" |
| 32 | |
| 33 | #include "editor/editor_undo_redo_manager.h" |
| 34 | #include "editor/settings/event_listener_line_edit.h" |
| 35 | #include "editor/settings/input_event_configuration_dialog.h" |
| 36 | |
| 37 | void InputEventConfigContainer::_configure_pressed() { |
| 38 | config_dialog->popup_and_configure(input_event); |
| 39 | } |
| 40 | |
| 41 | void InputEventConfigContainer::_event_changed() { |
| 42 | input_event_text->set_text(input_event->as_text()); |
| 43 | } |
| 44 | |
| 45 | void InputEventConfigContainer::_config_dialog_confirmed() { |
| 46 | Ref<InputEvent> ie = config_dialog->get_event(); |
| 47 | |
| 48 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 49 | undo_redo->create_action(TTR("Event Configured")); |
| 50 | |
| 51 | // When command_or_control_autoremap is toggled to false, it should be set first; |
| 52 | // and when it is toggled to true, it should be set last. |
| 53 | bool will_toggle = false; |
| 54 | bool pending = false; |
| 55 | Ref<InputEventWithModifiers> iewm = input_event; |
| 56 | if (iewm.is_valid()) { |
| 57 | Variant new_value = ie->get("command_or_control_autoremap"); |
| 58 | will_toggle = new_value != input_event->get("command_or_control_autoremap"); |
| 59 | if (will_toggle) { |
| 60 | pending = new_value; |
| 61 | if (pending) { |
| 62 | undo_redo->add_undo_property(input_event.ptr(), "command_or_control_autoremap", !pending); |
| 63 | } else { |
| 64 | undo_redo->add_do_property(input_event.ptr(), "command_or_control_autoremap", pending); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | List<PropertyInfo> pi; |
| 70 | ie->get_property_list(&pi); |
| 71 | for (const PropertyInfo &E : pi) { |
| 72 | if (E.name == "resource_path") { |
| 73 | continue; // Do not change path. |
| 74 | } |
| 75 | if (E.name == "command_or_control_autoremap") { |
| 76 | continue; // Handle it separately. |
| 77 | } |
| 78 | Variant old_value = input_event->get(E.name); |
| 79 | Variant new_value = ie->get(E.name); |
| 80 | if (old_value == new_value) { |
| 81 | continue; |
| 82 | } |
| 83 | undo_redo->add_do_property(input_event.ptr(), E.name, new_value); |
| 84 | undo_redo->add_undo_property(input_event.ptr(), E.name, old_value); |
| 85 | } |
| 86 | |
| 87 | if (will_toggle) { |
| 88 | if (pending) { |
| 89 | undo_redo->add_do_property(input_event.ptr(), "command_or_control_autoremap", pending); |
| 90 | } else { |
| 91 | undo_redo->add_undo_property(input_event.ptr(), "command_or_control_autoremap", !pending); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | undo_redo->add_do_property(input_event_text, "text", ie->as_text()); |
| 96 | undo_redo->add_undo_property(input_event_text, "text", input_event->as_text()); |
| 97 | undo_redo->commit_action(); |
| 98 | } |
| 99 | |
| 100 | void InputEventConfigContainer::set_event(const Ref<InputEvent> &p_event) { |
| 101 | Ref<InputEventKey> k = p_event; |
| 102 | Ref<InputEventMouseButton> m = p_event; |
| 103 | Ref<InputEventJoypadButton> jb = p_event; |
| 104 | Ref<InputEventJoypadMotion> jm = p_event; |
| 105 | |
| 106 | if (k.is_valid()) { |
| 107 | config_dialog->set_allowed_input_types(INPUT_KEY); |
| 108 | } else if (m.is_valid()) { |
| 109 | config_dialog->set_allowed_input_types(INPUT_MOUSE_BUTTON); |
| 110 | } else if (jb.is_valid()) { |
| 111 | config_dialog->set_allowed_input_types(INPUT_JOY_BUTTON); |
| 112 | } else if (jm.is_valid()) { |
| 113 | config_dialog->set_allowed_input_types(INPUT_JOY_MOTION); |
| 114 | } |
| 115 | |
| 116 | input_event = p_event; |
| 117 | _event_changed(); |
| 118 | input_event->connect_changed(callable_mp(this, &InputEventConfigContainer::_event_changed)); |
| 119 | } |
| 120 | |
| 121 | InputEventConfigContainer::InputEventConfigContainer() { |
| 122 | input_event_text = memnew(Label); |
| 123 | input_event_text->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 124 | input_event_text->set_h_size_flags(SIZE_EXPAND_FILL); |
| 125 | input_event_text->set_autowrap_mode(TextServer::AutowrapMode::AUTOWRAP_WORD_SMART); |
| 126 | input_event_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 127 | add_child(input_event_text); |
| 128 | |
| 129 | EditorInspectorActionButton *open_config_button = memnew(EditorInspectorActionButton(TTRC("Configure"), SNAME("Edit"))); |
| 130 | open_config_button->connect(SceneStringName(pressed), callable_mp(this, &InputEventConfigContainer::_configure_pressed)); |
| 131 | add_child(open_config_button); |
| 132 | |
| 133 | add_child(memnew(Control)); |
| 134 | |
| 135 | config_dialog = memnew(InputEventConfigurationDialog); |
| 136 | config_dialog->connect(SceneStringName(confirmed), callable_mp(this, &InputEventConfigContainer::_config_dialog_confirmed)); |
| 137 | add_child(config_dialog); |
| 138 | } |
| 139 | |
| 140 | /////////////////////// |
| 141 | |
| 142 | bool EditorInspectorPluginInputEvent::can_handle(Object *p_object) { |
| 143 | Ref<InputEventKey> k = Ref<InputEventKey>(p_object); |
| 144 | Ref<InputEventMouseButton> m = Ref<InputEventMouseButton>(p_object); |
| 145 | Ref<InputEventJoypadButton> jb = Ref<InputEventJoypadButton>(p_object); |
| 146 | Ref<InputEventJoypadMotion> jm = Ref<InputEventJoypadMotion>(p_object); |
| 147 | |
| 148 | return k.is_valid() || m.is_valid() || jb.is_valid() || jm.is_valid(); |
| 149 | } |
| 150 | |
| 151 | void EditorInspectorPluginInputEvent::parse_begin(Object *p_object) { |
| 152 | Ref<InputEvent> ie = Ref<InputEvent>(p_object); |
| 153 | |
| 154 | InputEventConfigContainer *picker_controls = memnew(InputEventConfigContainer); |
| 155 | picker_controls->set_event(ie); |
| 156 | add_custom_control(picker_controls); |
| 157 | } |
| 158 | |
| 159 | /////////////////////// |
| 160 | |
| 161 | InputEventEditorPlugin::InputEventEditorPlugin() { |
| 162 | Ref<EditorInspectorPluginInputEvent> plugin; |
| 163 | plugin.instantiate(); |
| 164 | add_inspector_plugin(plugin); |
| 165 | } |