| 1 | /**************************************************************************/ |
| 2 | /* openxr_binding_modifier_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_binding_modifier_editor.h" |
| 32 | |
| 33 | #include "editor/editor_string_names.h" |
| 34 | #include "scene/gui/option_button.h" |
| 35 | |
| 36 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 37 | // EditorPropertyActionSet |
| 38 | |
| 39 | void EditorPropertyActionSet::_set_read_only(bool p_read_only) { |
| 40 | options->set_disabled(p_read_only); |
| 41 | } |
| 42 | |
| 43 | void EditorPropertyActionSet::_option_selected(int p_which) { |
| 44 | Ref<OpenXRActionSet> val = options->get_item_metadata(p_which); |
| 45 | emit_changed(get_edited_property(), val); |
| 46 | } |
| 47 | |
| 48 | void EditorPropertyActionSet::update_property() { |
| 49 | Variant current = get_edited_property_value(); |
| 50 | if (current.get_type() == Variant::NIL) { |
| 51 | options->select(-1); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | Ref<OpenXRActionSet> which = current; |
| 56 | for (int i = 0; i < options->get_item_count(); i++) { |
| 57 | if (which == (Ref<OpenXRActionSet>)options->get_item_metadata(i)) { |
| 58 | options->select(i); |
| 59 | return; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Couldn't find it? deselect.. |
| 64 | options->select(-1); |
| 65 | } |
| 66 | |
| 67 | void EditorPropertyActionSet::setup(const Ref<OpenXRActionMap> &p_action_map) { |
| 68 | options->clear(); |
| 69 | |
| 70 | Array action_sets = p_action_map->get_action_sets(); |
| 71 | for (Ref<OpenXRActionSet> action_set : action_sets) { |
| 72 | options->add_item(action_set->get_localized_name()); |
| 73 | options->set_item_metadata(-1, action_set); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void EditorPropertyActionSet::set_option_button_clip(bool p_enable) { |
| 78 | options->set_clip_text(p_enable); |
| 79 | } |
| 80 | |
| 81 | EditorPropertyActionSet::EditorPropertyActionSet() { |
| 82 | options = memnew(OptionButton); |
| 83 | options->set_clip_text(true); |
| 84 | options->set_flat(true); |
| 85 | options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 86 | add_child(options); |
| 87 | add_focusable(options); |
| 88 | options->connect(SceneStringName(item_selected), callable_mp(this, &EditorPropertyActionSet::_option_selected)); |
| 89 | } |
| 90 | |
| 91 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 92 | // EditorPropertyBindingPath |
| 93 | |
| 94 | void EditorPropertyBindingPath::_set_read_only(bool p_read_only) { |
| 95 | options->set_disabled(p_read_only); |
| 96 | } |
| 97 | |
| 98 | void EditorPropertyBindingPath::_option_selected(int p_which) { |
| 99 | String val = options->get_item_metadata(p_which); |
| 100 | emit_changed(get_edited_property(), val); |
| 101 | } |
| 102 | |
| 103 | void EditorPropertyBindingPath::update_property() { |
| 104 | Variant current = get_edited_property_value(); |
| 105 | if (current.get_type() == Variant::NIL) { |
| 106 | options->select(-1); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | String which = current; |
| 111 | for (int i = 0; i < options->get_item_count(); i++) { |
| 112 | if (which == (String)options->get_item_metadata(i)) { |
| 113 | options->select(i); |
| 114 | return; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Couldn't find it? deselect.. |
| 119 | options->select(-1); |
| 120 | } |
| 121 | |
| 122 | void EditorPropertyBindingPath::setup(const String &p_interaction_profile_path, Vector<OpenXRAction::ActionType> p_include_action_types) { |
| 123 | options->clear(); |
| 124 | |
| 125 | const OpenXRInteractionProfileMetadata::InteractionProfile *profile_def = OpenXRInteractionProfileMetadata::get_singleton()->get_profile(p_interaction_profile_path); |
| 126 | |
| 127 | // Determine toplevel paths |
| 128 | Vector<String> top_level_paths; |
| 129 | for (const OpenXRInteractionProfileMetadata::IOPath &io_path : profile_def->io_paths) { |
| 130 | if (!top_level_paths.has(io_path.top_level_path)) { |
| 131 | top_level_paths.push_back(io_path.top_level_path); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | for (const String &top_level_path : top_level_paths) { |
| 136 | String top_level_name = OpenXRInteractionProfileMetadata::get_singleton()->get_top_level_name(top_level_path); |
| 137 | |
| 138 | for (const OpenXRInteractionProfileMetadata::IOPath &io_path : profile_def->io_paths) { |
| 139 | if (io_path.top_level_path == top_level_path && p_include_action_types.has(io_path.action_type)) { |
| 140 | options->add_item(top_level_name + "/" + io_path.display_name); |
| 141 | options->set_item_metadata(-1, io_path.openxr_path); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void EditorPropertyBindingPath::set_option_button_clip(bool p_enable) { |
| 148 | options->set_clip_text(p_enable); |
| 149 | } |
| 150 | |
| 151 | EditorPropertyBindingPath::EditorPropertyBindingPath() { |
| 152 | options = memnew(OptionButton); |
| 153 | options->set_clip_text(true); |
| 154 | options->set_flat(true); |
| 155 | options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 156 | add_child(options); |
| 157 | add_focusable(options); |
| 158 | options->connect(SceneStringName(item_selected), callable_mp(this, &EditorPropertyBindingPath::_option_selected)); |
| 159 | } |
| 160 | |
| 161 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 162 | // OpenXRBindingModifierEditor |
| 163 | |
| 164 | bool EditorInspectorPluginBindingModifier::can_handle(Object *p_object) { |
| 165 | Ref<OpenXRBindingModifier> binding_modifier(Object::cast_to<OpenXRBindingModifier>(p_object)); |
| 166 | return binding_modifier.is_valid(); |
| 167 | } |
| 168 | |
| 169 | bool EditorInspectorPluginBindingModifier::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) { |
| 170 | Ref<OpenXRActionBindingModifier> action_binding_modifier(Object::cast_to<OpenXRActionBindingModifier>(p_object)); |
| 171 | if (action_binding_modifier.is_valid()) { |
| 172 | if (p_type == Variant::OBJECT && p_hint == PROPERTY_HINT_RESOURCE_TYPE && p_hint_text == OpenXRActionSet::get_class_static()) { |
| 173 | OpenXRIPBinding *ip_binding = action_binding_modifier->get_ip_binding(); |
| 174 | ERR_FAIL_NULL_V(ip_binding, false); |
| 175 | |
| 176 | OpenXRActionMap *action_map = ip_binding->get_action_map(); |
| 177 | ERR_FAIL_NULL_V(action_map, false); |
| 178 | |
| 179 | EditorPropertyActionSet *action_set_property = memnew(EditorPropertyActionSet); |
| 180 | action_set_property->setup(action_map); |
| 181 | add_property_editor(p_path, action_set_property); |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | Ref<OpenXRIPBindingModifier> ip_binding_modifier(Object::cast_to<OpenXRIPBindingModifier>(p_object)); |
| 189 | if (ip_binding_modifier.is_valid()) { |
| 190 | if (p_type == Variant::OBJECT && p_hint == PROPERTY_HINT_RESOURCE_TYPE && p_hint_text == OpenXRActionSet::get_class_static()) { |
| 191 | OpenXRInteractionProfile *interaction_profile = ip_binding_modifier->get_interaction_profile(); |
| 192 | ERR_FAIL_NULL_V(interaction_profile, false); |
| 193 | |
| 194 | OpenXRActionMap *action_map = interaction_profile->get_action_map(); |
| 195 | ERR_FAIL_NULL_V(action_map, false); |
| 196 | |
| 197 | EditorPropertyActionSet *action_set_property = memnew(EditorPropertyActionSet); |
| 198 | action_set_property->setup(action_map); |
| 199 | add_property_editor(p_path, action_set_property); |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | if (p_type == Variant::STRING && p_hint == PROPERTY_HINT_TYPE_STRING && p_hint_text == "binding_path") { |
| 204 | EditorPropertyBindingPath *binding_path_property = memnew(EditorPropertyBindingPath); |
| 205 | |
| 206 | OpenXRInteractionProfile *interaction_profile = ip_binding_modifier->get_interaction_profile(); |
| 207 | ERR_FAIL_NULL_V(interaction_profile, false); |
| 208 | |
| 209 | Vector<OpenXRAction::ActionType> action_types; |
| 210 | action_types.push_back(OpenXRAction::OPENXR_ACTION_VECTOR2); |
| 211 | binding_path_property->setup(interaction_profile->get_interaction_profile_path(), action_types); |
| 212 | |
| 213 | add_property_editor(p_path, binding_path_property); |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 224 | // OpenXRBindingModifierEditor |
| 225 | |
| 226 | void OpenXRBindingModifierEditor::_bind_methods() { |
| 227 | ClassDB::bind_method(D_METHOD("get_binding_modifier"), &OpenXRBindingModifierEditor::get_binding_modifier); |
| 228 | ClassDB::bind_method(D_METHOD("setup", "action_map", "binding_modifier"), &OpenXRBindingModifierEditor::setup); |
| 229 | |
| 230 | ADD_SIGNAL(MethodInfo("binding_modifier_removed", PropertyInfo(Variant::OBJECT, "binding_modifier_editor"))); |
| 231 | } |
| 232 | |
| 233 | void OpenXRBindingModifierEditor::_notification(int p_what) { |
| 234 | switch (p_what) { |
| 235 | case NOTIFICATION_THEME_CHANGED: { |
| 236 | rem_binding_modifier_btn->set_button_icon(get_theme_icon(SNAME("Remove"), EditorStringName(EditorIcons))); |
| 237 | } break; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | void OpenXRBindingModifierEditor::_on_remove_binding_modifier() { |
| 242 | // Tell parent to remove us |
| 243 | emit_signal("binding_modifier_removed", this); |
| 244 | } |
| 245 | |
| 246 | OpenXRBindingModifierEditor::OpenXRBindingModifierEditor() { |
| 247 | undo_redo = EditorUndoRedoManager::get_singleton(); |
| 248 | |
| 249 | set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 250 | |
| 251 | main_vb = memnew(VBoxContainer); |
| 252 | main_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 253 | add_child(main_vb); |
| 254 | |
| 255 | header_hb = memnew(HBoxContainer); |
| 256 | header_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 257 | main_vb->add_child(header_hb); |
| 258 | |
| 259 | binding_modifier_title = memnew(Label); |
| 260 | binding_modifier_title->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 261 | header_hb->add_child(binding_modifier_title); |
| 262 | |
| 263 | rem_binding_modifier_btn = memnew(Button); |
| 264 | rem_binding_modifier_btn->set_tooltip_text(TTR("Remove this binding modifier.")); |
| 265 | rem_binding_modifier_btn->connect(SceneStringName(pressed), callable_mp(this, &OpenXRBindingModifierEditor::_on_remove_binding_modifier)); |
| 266 | rem_binding_modifier_btn->set_flat(true); |
| 267 | header_hb->add_child(rem_binding_modifier_btn); |
| 268 | |
| 269 | editor_inspector = memnew(EditorInspector); |
| 270 | editor_inspector->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED); |
| 271 | editor_inspector->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED); |
| 272 | editor_inspector->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 273 | main_vb->add_child(editor_inspector); |
| 274 | } |
| 275 | |
| 276 | void OpenXRBindingModifierEditor::setup(const Ref<OpenXRActionMap> &p_action_map, const Ref<OpenXRBindingModifier> &p_binding_modifier) { |
| 277 | ERR_FAIL_NULL(binding_modifier_title); |
| 278 | ERR_FAIL_NULL(editor_inspector); |
| 279 | |
| 280 | action_map = p_action_map; |
| 281 | binding_modifier = p_binding_modifier; |
| 282 | |
| 283 | if (p_binding_modifier.is_valid()) { |
| 284 | binding_modifier_title->set_text(p_binding_modifier->get_description()); |
| 285 | |
| 286 | editor_inspector->set_object_class(p_binding_modifier->get_class()); |
| 287 | editor_inspector->edit(p_binding_modifier.ptr()); |
| 288 | } |
| 289 | } |