| 1 | /**************************************************************************/ |
| 2 | /* openxr_action_set_editor.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 "openxr_action_set_editor.h" |
| 32 | |
| 33 | #include "editor/editor_string_names.h" |
| 34 | #include "editor/gui/editor_spin_slider.h" |
| 35 | #include "editor/themes/editor_scale.h" |
| 36 | #include "openxr_action_editor.h" |
| 37 | #include "scene/gui/box_container.h" |
| 38 | #include "scene/gui/button.h" |
| 39 | #include "scene/gui/line_edit.h" |
| 40 | #include "scene/gui/panel_container.h" |
| 41 | #include "scene/gui/text_edit.h" |
| 42 | |
| 43 | void OpenXRActionSetEditor::_bind_methods() { |
| 44 | ClassDB::bind_method(D_METHOD("_do_set_name", "name"), &OpenXRActionSetEditor::_do_set_name); |
| 45 | ClassDB::bind_method(D_METHOD("_do_set_localized_name", "name"), &OpenXRActionSetEditor::_do_set_localized_name); |
| 46 | ClassDB::bind_method(D_METHOD("_do_set_priority", "value"), &OpenXRActionSetEditor::_do_set_priority); |
| 47 | ClassDB::bind_method(D_METHOD("_do_add_action_editor", "action_editor"), &OpenXRActionSetEditor::_do_add_action_editor); |
| 48 | ClassDB::bind_method(D_METHOD("_do_remove_action_editor", "action_editor"), &OpenXRActionSetEditor::_do_remove_action_editor); |
| 49 | |
| 50 | ADD_SIGNAL(MethodInfo("remove", PropertyInfo(Variant::OBJECT, "action_set_editor"))); |
| 51 | ADD_SIGNAL(MethodInfo("action_removed", PropertyInfo(Variant::OBJECT, "action"))); |
| 52 | } |
| 53 | |
| 54 | void OpenXRActionSetEditor::_set_fold_icon() { |
| 55 | if (is_expanded) { |
| 56 | fold_btn->set_button_icon(get_theme_icon(SNAME("GuiTreeArrowDown"), EditorStringName(EditorIcons))); |
| 57 | } else { |
| 58 | fold_btn->set_button_icon(get_theme_icon(SNAME("GuiTreeArrowRight"), EditorStringName(EditorIcons))); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void OpenXRActionSetEditor::_theme_changed() { |
| 63 | _set_fold_icon(); |
| 64 | add_action->set_button_icon(get_theme_icon(SNAME("Add"), EditorStringName(EditorIcons))); |
| 65 | rem_action_set->set_button_icon(get_theme_icon(SNAME("Remove"), EditorStringName(EditorIcons))); |
| 66 | } |
| 67 | |
| 68 | void OpenXRActionSetEditor::_notification(int p_what) { |
| 69 | switch (p_what) { |
| 70 | case NOTIFICATION_THEME_CHANGED: { |
| 71 | _theme_changed(); |
| 72 | panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("TabContainer"))); |
| 73 | } break; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | OpenXRActionEditor *OpenXRActionSetEditor::_add_action_editor(const Ref<OpenXRAction> &p_action) { |
| 78 | OpenXRActionEditor *action_editor = memnew(OpenXRActionEditor(p_action)); |
| 79 | action_editor->connect("remove", callable_mp(this, &OpenXRActionSetEditor::_on_remove_action)); |
| 80 | actions_vb->add_child(action_editor); |
| 81 | |
| 82 | return action_editor; |
| 83 | } |
| 84 | |
| 85 | void OpenXRActionSetEditor::_on_toggle_expand() { |
| 86 | is_expanded = !is_expanded; |
| 87 | actions_vb->set_visible(is_expanded); |
| 88 | _set_fold_icon(); |
| 89 | } |
| 90 | |
| 91 | void OpenXRActionSetEditor::_on_action_set_name_changed(const String &p_new_text) { |
| 92 | if (action_set->get_name() != p_new_text) { |
| 93 | undo_redo->create_action(TTR("Rename Action Set")); |
| 94 | undo_redo->add_do_method(this, "_do_set_name", p_new_text); |
| 95 | undo_redo->add_undo_method(this, "_do_set_name", action_set->get_name()); |
| 96 | undo_redo->commit_action(false); |
| 97 | |
| 98 | // If our localized name matches our action set name, set this too |
| 99 | if (action_set->get_name() == action_set->get_localized_name()) { |
| 100 | undo_redo->create_action(TTR("Rename Action Sets Localized name")); |
| 101 | undo_redo->add_do_method(this, "_do_set_localized_name", p_new_text); |
| 102 | undo_redo->add_undo_method(this, "_do_set_localized_name", action_set->get_localized_name()); |
| 103 | undo_redo->commit_action(false); |
| 104 | |
| 105 | action_set->set_localized_name(p_new_text); |
| 106 | action_set_localized_name->set_text(p_new_text); |
| 107 | } |
| 108 | action_set->set_name(p_new_text); |
| 109 | action_set->set_edited(true); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void OpenXRActionSetEditor::_do_set_name(const String &p_new_text) { |
| 114 | action_set->set_name(p_new_text); |
| 115 | action_set_name->set_text(p_new_text); |
| 116 | } |
| 117 | |
| 118 | void OpenXRActionSetEditor::_on_action_set_localized_name_changed(const String &p_new_text) { |
| 119 | if (action_set->get_localized_name() != p_new_text) { |
| 120 | undo_redo->create_action(TTR("Rename Action Sets Localized name")); |
| 121 | undo_redo->add_do_method(this, "_do_set_localized_name", p_new_text); |
| 122 | undo_redo->add_undo_method(this, "_do_set_localized_name", action_set->get_localized_name()); |
| 123 | undo_redo->commit_action(false); |
| 124 | |
| 125 | action_set->set_localized_name(p_new_text); |
| 126 | action_set->set_edited(true); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void OpenXRActionSetEditor::_do_set_localized_name(const String &p_new_text) { |
| 131 | action_set->set_localized_name(p_new_text); |
| 132 | action_set_localized_name->set_text(p_new_text); |
| 133 | } |
| 134 | |
| 135 | void OpenXRActionSetEditor::_on_action_set_priority_changed(const double p_new_value) { |
| 136 | int64_t value = (int64_t)p_new_value; |
| 137 | |
| 138 | if (action_set->get_priority() != value) { |
| 139 | undo_redo->create_action(TTR("Change Action Sets priority")); |
| 140 | undo_redo->add_do_method(this, "_do_set_priority", value); |
| 141 | undo_redo->add_undo_method(this, "_do_set_priority", action_set->get_priority()); |
| 142 | undo_redo->commit_action(false); |
| 143 | |
| 144 | action_set->set_priority(value); |
| 145 | action_set->set_edited(true); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void OpenXRActionSetEditor::_do_set_priority(int64_t p_value) { |
| 150 | action_set->set_priority(p_value); |
| 151 | action_set_priority->set_value_no_signal(p_value); |
| 152 | } |
| 153 | |
| 154 | void OpenXRActionSetEditor::_on_add_action() { |
| 155 | Ref<OpenXRAction> new_action; |
| 156 | |
| 157 | new_action.instantiate(); |
| 158 | new_action->set_name("New"); |
| 159 | new_action->set_localized_name("New"); |
| 160 | action_set->add_action(new_action); |
| 161 | action_set->set_edited(true); |
| 162 | |
| 163 | OpenXRActionEditor *action_editor = _add_action_editor(new_action); |
| 164 | |
| 165 | undo_redo->create_action(TTR("Add action")); |
| 166 | undo_redo->add_do_method(this, "_do_add_action_editor", action_editor); |
| 167 | undo_redo->add_undo_method(this, "_do_remove_action_editor", action_editor); |
| 168 | undo_redo->commit_action(false); |
| 169 | |
| 170 | // TODO handle focus |
| 171 | } |
| 172 | |
| 173 | void OpenXRActionSetEditor::_on_remove_action_set() { |
| 174 | emit_signal("remove", this); |
| 175 | } |
| 176 | |
| 177 | void OpenXRActionSetEditor::_on_remove_action(Object *p_action_editor) { |
| 178 | OpenXRActionEditor *action_editor = Object::cast_to<OpenXRActionEditor>(p_action_editor); |
| 179 | ERR_FAIL_NULL(action_editor); |
| 180 | ERR_FAIL_COND(action_editor->get_parent() != actions_vb); |
| 181 | Ref<OpenXRAction> action = action_editor->get_action(); |
| 182 | ERR_FAIL_COND(action.is_null()); |
| 183 | |
| 184 | emit_signal("action_removed", action); |
| 185 | |
| 186 | undo_redo->create_action(TTR("Delete action")); |
| 187 | undo_redo->add_do_method(this, "_do_remove_action_editor", action_editor); |
| 188 | undo_redo->add_undo_method(this, "_do_add_action_editor", action_editor); |
| 189 | undo_redo->commit_action(true); |
| 190 | |
| 191 | action_set->set_edited(true); |
| 192 | } |
| 193 | |
| 194 | void OpenXRActionSetEditor::_do_add_action_editor(OpenXRActionEditor *p_action_editor) { |
| 195 | Ref<OpenXRAction> action = p_action_editor->get_action(); |
| 196 | ERR_FAIL_COND(action.is_null()); |
| 197 | |
| 198 | action_set->add_action(action); |
| 199 | actions_vb->add_child(p_action_editor); |
| 200 | } |
| 201 | |
| 202 | void OpenXRActionSetEditor::_do_remove_action_editor(OpenXRActionEditor *p_action_editor) { |
| 203 | Ref<OpenXRAction> action = p_action_editor->get_action(); |
| 204 | ERR_FAIL_COND(action.is_null()); |
| 205 | |
| 206 | actions_vb->remove_child(p_action_editor); |
| 207 | action_set->remove_action(action); |
| 208 | } |
| 209 | |
| 210 | void OpenXRActionSetEditor::remove_all_actions() { |
| 211 | for (int i = actions_vb->get_child_count(); i > 0; --i) { |
| 212 | _on_remove_action(actions_vb->get_child(i)); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void OpenXRActionSetEditor::set_focus_on_entry() { |
| 217 | ERR_FAIL_NULL(action_set_name); |
| 218 | action_set_name->grab_focus(); |
| 219 | } |
| 220 | |
| 221 | OpenXRActionSetEditor::OpenXRActionSetEditor(const Ref<OpenXRActionMap> &p_action_map, const Ref<OpenXRActionSet> &p_action_set) { |
| 222 | undo_redo = EditorUndoRedoManager::get_singleton(); |
| 223 | action_map = p_action_map; |
| 224 | action_set = p_action_set; |
| 225 | |
| 226 | set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 227 | |
| 228 | panel = memnew(PanelContainer); |
| 229 | panel->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 230 | add_child(panel); |
| 231 | |
| 232 | HBoxContainer *panel_hb = memnew(HBoxContainer); |
| 233 | panel_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 234 | panel->add_child(panel_hb); |
| 235 | |
| 236 | fold_btn = memnew(Button); |
| 237 | fold_btn->set_v_size_flags(Control::SIZE_SHRINK_BEGIN); |
| 238 | fold_btn->connect(SceneStringName(pressed), callable_mp(this, &OpenXRActionSetEditor::_on_toggle_expand)); |
| 239 | fold_btn->set_accessibility_name(TTRC("Fold")); |
| 240 | fold_btn->set_flat(true); |
| 241 | panel_hb->add_child(fold_btn); |
| 242 | |
| 243 | main_vb = memnew(VBoxContainer); |
| 244 | main_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 245 | panel_hb->add_child(main_vb); |
| 246 | |
| 247 | action_set_hb = memnew(HBoxContainer); |
| 248 | action_set_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 249 | main_vb->add_child(action_set_hb); |
| 250 | |
| 251 | action_set_name = memnew(LineEdit); |
| 252 | action_set_name->set_text(action_set->get_name()); |
| 253 | action_set_name->set_tooltip_text(TTR("Internal name of the action. Some XR runtimes don't allow spaces or special characters.")); |
| 254 | action_set_name->set_custom_minimum_size(Size2(150.0 * EDSCALE, 0.0)); |
| 255 | action_set_name->connect(SceneStringName(text_changed), callable_mp(this, &OpenXRActionSetEditor::_on_action_set_name_changed)); |
| 256 | action_set_name->set_accessibility_name(TTRC("Action Set Name")); |
| 257 | action_set_hb->add_child(action_set_name); |
| 258 | |
| 259 | action_set_localized_name = memnew(LineEdit); |
| 260 | action_set_localized_name->set_text(action_set->get_localized_name()); |
| 261 | action_set_localized_name->set_tooltip_text(TTR("Human-readable name of the action set. This can be displayed to end users.")); |
| 262 | action_set_localized_name->set_custom_minimum_size(Size2(150.0 * EDSCALE, 0.0)); |
| 263 | action_set_localized_name->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 264 | action_set_localized_name->connect(SceneStringName(text_changed), callable_mp(this, &OpenXRActionSetEditor::_on_action_set_localized_name_changed)); |
| 265 | action_set_localized_name->set_accessibility_name(TTRC("Action Set Localized Name")); |
| 266 | action_set_hb->add_child(action_set_localized_name); |
| 267 | |
| 268 | action_set_priority = memnew(EditorSpinSlider); |
| 269 | action_set_priority->set_tooltip_text(TTR("Priority of the action set. If multiple action sets bind to the same input, the action set with the highest priority will be updated.")); |
| 270 | action_set_priority->set_editing_integer(true); |
| 271 | action_set_priority->set_min(2147483647.0); |
| 272 | action_set_priority->set_min(-2147483648.0); |
| 273 | action_set_priority->set_value_no_signal(action_set->get_priority()); |
| 274 | action_set_priority->set_custom_minimum_size(Size2(75.0 * EDSCALE, 0.0)); |
| 275 | action_set_priority->connect(SceneStringName(value_changed), callable_mp(this, &OpenXRActionSetEditor::_on_action_set_priority_changed)); |
| 276 | action_set_priority->set_accessibility_name(TTRC("Action Set Priority")); |
| 277 | action_set_hb->add_child(action_set_priority); |
| 278 | |
| 279 | add_action = memnew(Button); |
| 280 | add_action->set_tooltip_text(TTR("Add action.")); |
| 281 | add_action->connect(SceneStringName(pressed), callable_mp(this, &OpenXRActionSetEditor::_on_add_action)); |
| 282 | add_action->set_flat(true); |
| 283 | action_set_hb->add_child(add_action); |
| 284 | |
| 285 | rem_action_set = memnew(Button); |
| 286 | rem_action_set->set_tooltip_text(TTR("Remove action set.")); |
| 287 | rem_action_set->connect(SceneStringName(pressed), callable_mp(this, &OpenXRActionSetEditor::_on_remove_action_set)); |
| 288 | rem_action_set->set_flat(true); |
| 289 | action_set_hb->add_child(rem_action_set); |
| 290 | |
| 291 | actions_vb = memnew(VBoxContainer); |
| 292 | actions_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 293 | main_vb->add_child(actions_vb); |
| 294 | |
| 295 | // Add our existing actions |
| 296 | Array actions = action_set->get_actions(); |
| 297 | for (int i = 0; i < actions.size(); i++) { |
| 298 | Ref<OpenXRAction> action = actions[i]; |
| 299 | _add_action_editor(action); |
| 300 | } |
| 301 | } |