| 1 | /**************************************************************************/ |
| 2 | /* openxr_binding_modifiers_dialog.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_binding_modifiers_dialog.h" |
| 32 | #include "../action_map/openxr_interaction_profile_metadata.h" |
| 33 | #include "openxr_action_map_editor.h" |
| 34 | |
| 35 | #include "editor/themes/editor_scale.h" |
| 36 | |
| 37 | void OpenXRBindingModifiersDialog::_bind_methods() { |
| 38 | ClassDB::bind_method(D_METHOD("_do_add_binding_modifier_editor", "binding_modifier_editor"), &OpenXRBindingModifiersDialog::_do_add_binding_modifier_editor); |
| 39 | ClassDB::bind_method(D_METHOD("_do_remove_binding_modifier_editor", "binding_modifier_editor"), &OpenXRBindingModifiersDialog::_do_remove_binding_modifier_editor); |
| 40 | } |
| 41 | |
| 42 | void OpenXRBindingModifiersDialog::_notification(int p_what) { |
| 43 | switch (p_what) { |
| 44 | case NOTIFICATION_READY: { |
| 45 | _create_binding_modifiers(); |
| 46 | } break; |
| 47 | |
| 48 | case NOTIFICATION_THEME_CHANGED: { |
| 49 | if (binding_modifier_sc) { |
| 50 | binding_modifier_sc->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree"))); |
| 51 | } |
| 52 | } break; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | OpenXRBindingModifierEditor *OpenXRBindingModifiersDialog::_add_binding_modifier_editor(const Ref<OpenXRBindingModifier> &p_binding_modifier) { |
| 57 | ERR_FAIL_COND_V(p_binding_modifier.is_null(), nullptr); |
| 58 | |
| 59 | String class_name = p_binding_modifier->get_class(); |
| 60 | ERR_FAIL_COND_V(class_name.is_empty(), nullptr); |
| 61 | String editor_class = OpenXRActionMapEditor::get_binding_modifier_editor_class(class_name); |
| 62 | ERR_FAIL_COND_V(editor_class.is_empty(), nullptr); |
| 63 | |
| 64 | OpenXRBindingModifierEditor *new_editor = nullptr; |
| 65 | |
| 66 | Object *obj = ClassDB::instantiate(editor_class); |
| 67 | if (obj) { |
| 68 | new_editor = Object::cast_to<OpenXRBindingModifierEditor>(obj); |
| 69 | if (!new_editor) { |
| 70 | // Not of correct type?? Free it. |
| 71 | memfree(obj); |
| 72 | } |
| 73 | } |
| 74 | ERR_FAIL_NULL_V(new_editor, nullptr); |
| 75 | |
| 76 | new_editor->setup(action_map, p_binding_modifier); |
| 77 | new_editor->connect("binding_modifier_removed", callable_mp(this, &OpenXRBindingModifiersDialog::_on_remove_binding_modifier)); |
| 78 | |
| 79 | binding_modifiers_vb->add_child(new_editor); |
| 80 | new_editor->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree"))); |
| 81 | |
| 82 | return new_editor; |
| 83 | } |
| 84 | |
| 85 | void OpenXRBindingModifiersDialog::_create_binding_modifiers() { |
| 86 | Array new_binding_modifiers; |
| 87 | |
| 88 | if (ip_binding.is_valid()) { |
| 89 | new_binding_modifiers = ip_binding->get_binding_modifiers(); |
| 90 | } else if (interaction_profile.is_valid()) { |
| 91 | new_binding_modifiers = interaction_profile->get_binding_modifiers(); |
| 92 | } else { |
| 93 | ERR_FAIL_MSG("No binding nor interaction profile specified."); |
| 94 | } |
| 95 | |
| 96 | for (int i = 0; i < new_binding_modifiers.size(); i++) { |
| 97 | Ref<OpenXRBindingModifier> binding_modifier = new_binding_modifiers[i]; |
| 98 | _add_binding_modifier_editor(binding_modifier); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void OpenXRBindingModifiersDialog::_on_add_binding_modifier() { |
| 103 | create_dialog->popup_create(false); |
| 104 | } |
| 105 | |
| 106 | void OpenXRBindingModifiersDialog::_on_remove_binding_modifier(Object *p_binding_modifier_editor) { |
| 107 | if (ip_binding.is_valid()) { |
| 108 | ip_binding->set_edited(true); |
| 109 | } else if (interaction_profile.is_valid()) { |
| 110 | interaction_profile->set_edited(true); |
| 111 | } else { |
| 112 | ERR_FAIL_MSG("No binding nor interaction profile specified."); |
| 113 | } |
| 114 | |
| 115 | OpenXRBindingModifierEditor *binding_modifier_editor = Object::cast_to<OpenXRBindingModifierEditor>(p_binding_modifier_editor); |
| 116 | ERR_FAIL_NULL(binding_modifier_editor); |
| 117 | ERR_FAIL_COND(binding_modifier_editor->get_parent() != binding_modifiers_vb); |
| 118 | |
| 119 | undo_redo->create_action(TTR("Remove binding modifier")); |
| 120 | undo_redo->add_do_method(this, "_do_remove_binding_modifier_editor", binding_modifier_editor); |
| 121 | undo_redo->add_undo_method(this, "_do_add_binding_modifier_editor", binding_modifier_editor); |
| 122 | undo_redo->commit_action(true); |
| 123 | } |
| 124 | |
| 125 | void OpenXRBindingModifiersDialog::_on_dialog_created() { |
| 126 | // Instance new binding modifier object |
| 127 | Variant obj = create_dialog->instantiate_selected(); |
| 128 | ERR_FAIL_COND(obj.get_type() != Variant::OBJECT); |
| 129 | |
| 130 | Ref<OpenXRBindingModifier> new_binding_modifier = obj; |
| 131 | ERR_FAIL_COND(new_binding_modifier.is_null()); |
| 132 | |
| 133 | if (ip_binding.is_valid()) { |
| 134 | // Add it to our binding. |
| 135 | ip_binding->add_binding_modifier(new_binding_modifier); |
| 136 | ip_binding->set_edited(true); |
| 137 | } else if (interaction_profile.is_valid()) { |
| 138 | // Add it to our interaction profile. |
| 139 | interaction_profile->add_binding_modifier(new_binding_modifier); |
| 140 | interaction_profile->set_edited(true); |
| 141 | } else { |
| 142 | ERR_FAIL_MSG("No binding nor interaction profile specified."); |
| 143 | } |
| 144 | |
| 145 | // Create our editor for this. |
| 146 | OpenXRBindingModifierEditor *binding_modifier_editor = _add_binding_modifier_editor(new_binding_modifier); |
| 147 | ERR_FAIL_NULL(binding_modifier_editor); |
| 148 | |
| 149 | // Add undo/redo. |
| 150 | undo_redo->create_action(TTR("Add binding modifier")); |
| 151 | undo_redo->add_do_method(this, "_do_add_binding_modifier_editor", binding_modifier_editor); |
| 152 | undo_redo->add_undo_method(this, "_do_remove_binding_modifier_editor", binding_modifier_editor); |
| 153 | undo_redo->commit_action(false); |
| 154 | } |
| 155 | |
| 156 | void OpenXRBindingModifiersDialog::_do_add_binding_modifier_editor(OpenXRBindingModifierEditor *p_binding_modifier_editor) { |
| 157 | Ref<OpenXRBindingModifier> binding_modifier = p_binding_modifier_editor->get_binding_modifier(); |
| 158 | ERR_FAIL_COND(binding_modifier.is_null()); |
| 159 | |
| 160 | if (ip_binding.is_valid()) { |
| 161 | // Add it to our binding |
| 162 | ip_binding->add_binding_modifier(binding_modifier); |
| 163 | } else if (interaction_profile.is_valid()) { |
| 164 | // Add it to our interaction profile |
| 165 | interaction_profile->add_binding_modifier(binding_modifier); |
| 166 | } else { |
| 167 | ERR_FAIL_MSG("No binding nor interaction profile specified."); |
| 168 | } |
| 169 | |
| 170 | binding_modifiers_vb->add_child(p_binding_modifier_editor); |
| 171 | } |
| 172 | |
| 173 | void OpenXRBindingModifiersDialog::_do_remove_binding_modifier_editor(OpenXRBindingModifierEditor *p_binding_modifier_editor) { |
| 174 | Ref<OpenXRBindingModifier> binding_modifier = p_binding_modifier_editor->get_binding_modifier(); |
| 175 | ERR_FAIL_COND(binding_modifier.is_null()); |
| 176 | |
| 177 | if (ip_binding.is_valid()) { |
| 178 | // Remove it from our binding. |
| 179 | ip_binding->remove_binding_modifier(binding_modifier); |
| 180 | } else if (interaction_profile.is_valid()) { |
| 181 | // Removed it to from interaction profile. |
| 182 | interaction_profile->remove_binding_modifier(binding_modifier); |
| 183 | } else { |
| 184 | ERR_FAIL_MSG("No binding nor interaction profile specified."); |
| 185 | } |
| 186 | |
| 187 | binding_modifiers_vb->remove_child(p_binding_modifier_editor); |
| 188 | } |
| 189 | |
| 190 | OpenXRBindingModifiersDialog::OpenXRBindingModifiersDialog() { |
| 191 | undo_redo = EditorUndoRedoManager::get_singleton(); |
| 192 | |
| 193 | set_transient(true); |
| 194 | |
| 195 | binding_modifier_sc = memnew(ScrollContainer); |
| 196 | binding_modifier_sc->set_custom_minimum_size(Size2(350.0 * EDSCALE, 0.0)); |
| 197 | binding_modifier_sc->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 198 | binding_modifier_sc->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 199 | binding_modifier_sc->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED); |
| 200 | add_child(binding_modifier_sc); |
| 201 | |
| 202 | binding_modifiers_vb = memnew(VBoxContainer); |
| 203 | binding_modifiers_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 204 | binding_modifier_sc->add_child(binding_modifiers_vb); |
| 205 | |
| 206 | binding_warning_label = memnew(Label); |
| 207 | binding_warning_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY); |
| 208 | binding_warning_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD); |
| 209 | binding_warning_label->set_text(TTR("Note: modifiers will only be applied if supported on the host system.")); |
| 210 | binding_modifiers_vb->add_child(binding_warning_label); |
| 211 | |
| 212 | add_binding_modifier_btn = memnew(Button); |
| 213 | add_binding_modifier_btn->set_text(TTR("Add binding modifier")); |
| 214 | add_binding_modifier_btn->connect("pressed", callable_mp(this, &OpenXRBindingModifiersDialog::_on_add_binding_modifier)); |
| 215 | binding_modifiers_vb->add_child(add_binding_modifier_btn); |
| 216 | |
| 217 | // TODO may need to create our own dialog for this that can filter on binding modifiers recorded on interaction profiles or on individual bindings. |
| 218 | |
| 219 | create_dialog = memnew(CreateDialog); |
| 220 | create_dialog->set_transient(true); |
| 221 | create_dialog->connect("create", callable_mp(this, &OpenXRBindingModifiersDialog::_on_dialog_created)); |
| 222 | add_child(create_dialog); |
| 223 | } |
| 224 | |
| 225 | void OpenXRBindingModifiersDialog::setup(const Ref<OpenXRActionMap> &p_action_map, const Ref<OpenXRInteractionProfile> &p_interaction_profile, const Ref<OpenXRIPBinding> &p_ip_binding) { |
| 226 | OpenXRInteractionProfileMetadata *meta_data = OpenXRInteractionProfileMetadata::get_singleton(); |
| 227 | action_map = p_action_map; |
| 228 | interaction_profile = p_interaction_profile; |
| 229 | ip_binding = p_ip_binding; |
| 230 | |
| 231 | String profile_path = interaction_profile->get_interaction_profile_path(); |
| 232 | |
| 233 | if (ip_binding.is_valid()) { |
| 234 | String action_name = "unset"; |
| 235 | String path_name = "unset"; |
| 236 | |
| 237 | Ref<OpenXRAction> action = p_ip_binding->get_action(); |
| 238 | if (action.is_valid()) { |
| 239 | action_name = action->get_name_with_set(); |
| 240 | } |
| 241 | |
| 242 | const OpenXRInteractionProfileMetadata::IOPath *io_path = meta_data->get_io_path(profile_path, p_ip_binding->get_binding_path()); |
| 243 | if (io_path != nullptr) { |
| 244 | path_name = io_path->display_name; |
| 245 | } |
| 246 | |
| 247 | create_dialog->set_base_type("OpenXRActionBindingModifier"); |
| 248 | set_title(TTR("Binding modifiers for:") + " " + action_name + ": " + path_name); |
| 249 | } else if (interaction_profile.is_valid()) { |
| 250 | String profile_name = profile_path; |
| 251 | |
| 252 | const OpenXRInteractionProfileMetadata::InteractionProfile *profile_def = meta_data->get_profile(profile_path); |
| 253 | if (profile_def != nullptr) { |
| 254 | profile_name = profile_def->display_name; |
| 255 | } |
| 256 | |
| 257 | create_dialog->set_base_type("OpenXRIPBindingModifier"); |
| 258 | set_title(TTR("Binding modifiers for:") + " " + profile_name); |
| 259 | } |
| 260 | } |