Path: blob/master/modules/openxr/editor/openxr_binding_modifiers_dialog.cpp
10278 views
/**************************************************************************/1/* openxr_binding_modifiers_dialog.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "openxr_binding_modifiers_dialog.h"31#include "../action_map/openxr_interaction_profile_metadata.h"32#include "openxr_action_map_editor.h"3334#include "editor/themes/editor_scale.h"3536void OpenXRBindingModifiersDialog::_bind_methods() {37ClassDB::bind_method(D_METHOD("_do_add_binding_modifier_editor", "binding_modifier_editor"), &OpenXRBindingModifiersDialog::_do_add_binding_modifier_editor);38ClassDB::bind_method(D_METHOD("_do_remove_binding_modifier_editor", "binding_modifier_editor"), &OpenXRBindingModifiersDialog::_do_remove_binding_modifier_editor);39}4041void OpenXRBindingModifiersDialog::_notification(int p_what) {42switch (p_what) {43case NOTIFICATION_READY: {44_create_binding_modifiers();45} break;4647case NOTIFICATION_THEME_CHANGED: {48if (binding_modifier_sc) {49binding_modifier_sc->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));50}51} break;52}53}5455OpenXRBindingModifierEditor *OpenXRBindingModifiersDialog::_add_binding_modifier_editor(Ref<OpenXRBindingModifier> p_binding_modifier) {56ERR_FAIL_COND_V(p_binding_modifier.is_null(), nullptr);5758String class_name = p_binding_modifier->get_class();59ERR_FAIL_COND_V(class_name.is_empty(), nullptr);60String editor_class = OpenXRActionMapEditor::get_binding_modifier_editor_class(class_name);61ERR_FAIL_COND_V(editor_class.is_empty(), nullptr);6263OpenXRBindingModifierEditor *new_editor = nullptr;6465Object *obj = ClassDB::instantiate(editor_class);66if (obj) {67new_editor = Object::cast_to<OpenXRBindingModifierEditor>(obj);68if (!new_editor) {69// Not of correct type?? Free it.70memfree(obj);71}72}73ERR_FAIL_NULL_V(new_editor, nullptr);7475new_editor->setup(action_map, p_binding_modifier);76new_editor->connect("binding_modifier_removed", callable_mp(this, &OpenXRBindingModifiersDialog::_on_remove_binding_modifier));7778binding_modifiers_vb->add_child(new_editor);79new_editor->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));8081return new_editor;82}8384void OpenXRBindingModifiersDialog::_create_binding_modifiers() {85Array new_binding_modifiers;8687if (ip_binding.is_valid()) {88new_binding_modifiers = ip_binding->get_binding_modifiers();89} else if (interaction_profile.is_valid()) {90new_binding_modifiers = interaction_profile->get_binding_modifiers();91} else {92ERR_FAIL_MSG("No binding nor interaction profile specified.");93}9495for (int i = 0; i < new_binding_modifiers.size(); i++) {96Ref<OpenXRBindingModifier> binding_modifier = new_binding_modifiers[i];97_add_binding_modifier_editor(binding_modifier);98}99}100101void OpenXRBindingModifiersDialog::_on_add_binding_modifier() {102create_dialog->popup_create(false);103}104105void OpenXRBindingModifiersDialog::_on_remove_binding_modifier(Object *p_binding_modifier_editor) {106if (ip_binding.is_valid()) {107ip_binding->set_edited(true);108} else if (interaction_profile.is_valid()) {109interaction_profile->set_edited(true);110} else {111ERR_FAIL_MSG("No binding nor interaction profile specified.");112}113114OpenXRBindingModifierEditor *binding_modifier_editor = Object::cast_to<OpenXRBindingModifierEditor>(p_binding_modifier_editor);115ERR_FAIL_NULL(binding_modifier_editor);116ERR_FAIL_COND(binding_modifier_editor->get_parent() != binding_modifiers_vb);117118undo_redo->create_action(TTR("Remove binding modifier"));119undo_redo->add_do_method(this, "_do_remove_binding_modifier_editor", binding_modifier_editor);120undo_redo->add_undo_method(this, "_do_add_binding_modifier_editor", binding_modifier_editor);121undo_redo->commit_action(true);122}123124void OpenXRBindingModifiersDialog::_on_dialog_created() {125// Instance new binding modifier object126Variant obj = create_dialog->instantiate_selected();127ERR_FAIL_COND(obj.get_type() != Variant::OBJECT);128129Ref<OpenXRBindingModifier> new_binding_modifier = obj;130ERR_FAIL_COND(new_binding_modifier.is_null());131132if (ip_binding.is_valid()) {133// Add it to our binding.134ip_binding->add_binding_modifier(new_binding_modifier);135ip_binding->set_edited(true);136} else if (interaction_profile.is_valid()) {137// Add it to our interaction profile.138interaction_profile->add_binding_modifier(new_binding_modifier);139interaction_profile->set_edited(true);140} else {141ERR_FAIL_MSG("No binding nor interaction profile specified.");142}143144// Create our editor for this.145OpenXRBindingModifierEditor *binding_modifier_editor = _add_binding_modifier_editor(new_binding_modifier);146ERR_FAIL_NULL(binding_modifier_editor);147148// Add undo/redo.149undo_redo->create_action(TTR("Add binding modifier"));150undo_redo->add_do_method(this, "_do_add_binding_modifier_editor", binding_modifier_editor);151undo_redo->add_undo_method(this, "_do_remove_binding_modifier_editor", binding_modifier_editor);152undo_redo->commit_action(false);153}154155void OpenXRBindingModifiersDialog::_do_add_binding_modifier_editor(OpenXRBindingModifierEditor *p_binding_modifier_editor) {156Ref<OpenXRBindingModifier> binding_modifier = p_binding_modifier_editor->get_binding_modifier();157ERR_FAIL_COND(binding_modifier.is_null());158159if (ip_binding.is_valid()) {160// Add it to our binding161ip_binding->add_binding_modifier(binding_modifier);162} else if (interaction_profile.is_valid()) {163// Add it to our interaction profile164interaction_profile->add_binding_modifier(binding_modifier);165} else {166ERR_FAIL_MSG("No binding nor interaction profile specified.");167}168169binding_modifiers_vb->add_child(p_binding_modifier_editor);170}171172void OpenXRBindingModifiersDialog::_do_remove_binding_modifier_editor(OpenXRBindingModifierEditor *p_binding_modifier_editor) {173Ref<OpenXRBindingModifier> binding_modifier = p_binding_modifier_editor->get_binding_modifier();174ERR_FAIL_COND(binding_modifier.is_null());175176if (ip_binding.is_valid()) {177// Remove it from our binding.178ip_binding->remove_binding_modifier(binding_modifier);179} else if (interaction_profile.is_valid()) {180// Removed it to from interaction profile.181interaction_profile->remove_binding_modifier(binding_modifier);182} else {183ERR_FAIL_MSG("No binding nor interaction profile specified.");184}185186binding_modifiers_vb->remove_child(p_binding_modifier_editor);187}188189OpenXRBindingModifiersDialog::OpenXRBindingModifiersDialog() {190undo_redo = EditorUndoRedoManager::get_singleton();191192set_transient(true);193194binding_modifier_sc = memnew(ScrollContainer);195binding_modifier_sc->set_custom_minimum_size(Size2(350.0 * EDSCALE, 0.0));196binding_modifier_sc->set_h_size_flags(Control::SIZE_EXPAND_FILL);197binding_modifier_sc->set_v_size_flags(Control::SIZE_EXPAND_FILL);198binding_modifier_sc->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);199add_child(binding_modifier_sc);200201binding_modifiers_vb = memnew(VBoxContainer);202binding_modifiers_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);203binding_modifier_sc->add_child(binding_modifiers_vb);204205binding_warning_label = memnew(Label);206binding_warning_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY);207binding_warning_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);208binding_warning_label->set_text(TTR("Note: modifiers will only be applied if supported on the host system."));209binding_modifiers_vb->add_child(binding_warning_label);210211add_binding_modifier_btn = memnew(Button);212add_binding_modifier_btn->set_text(TTR("Add binding modifier"));213add_binding_modifier_btn->connect("pressed", callable_mp(this, &OpenXRBindingModifiersDialog::_on_add_binding_modifier));214binding_modifiers_vb->add_child(add_binding_modifier_btn);215216// TODO may need to create our own dialog for this that can filter on binding modifiers recorded on interaction profiles or on individual bindings.217218create_dialog = memnew(CreateDialog);219create_dialog->set_transient(true);220create_dialog->connect("create", callable_mp(this, &OpenXRBindingModifiersDialog::_on_dialog_created));221add_child(create_dialog);222}223224void OpenXRBindingModifiersDialog::setup(Ref<OpenXRActionMap> p_action_map, Ref<OpenXRInteractionProfile> p_interaction_profile, Ref<OpenXRIPBinding> p_ip_binding) {225OpenXRInteractionProfileMetadata *meta_data = OpenXRInteractionProfileMetadata::get_singleton();226action_map = p_action_map;227interaction_profile = p_interaction_profile;228ip_binding = p_ip_binding;229230String profile_path = interaction_profile->get_interaction_profile_path();231232if (ip_binding.is_valid()) {233String action_name = "unset";234String path_name = "unset";235236Ref<OpenXRAction> action = p_ip_binding->get_action();237if (action.is_valid()) {238action_name = action->get_name_with_set();239}240241const OpenXRInteractionProfileMetadata::IOPath *io_path = meta_data->get_io_path(profile_path, p_ip_binding->get_binding_path());242if (io_path != nullptr) {243path_name = io_path->display_name;244}245246create_dialog->set_base_type("OpenXRActionBindingModifier");247set_title(TTR("Binding modifiers for:") + " " + action_name + ": " + path_name);248} else if (interaction_profile.is_valid()) {249String profile_name = profile_path;250251const OpenXRInteractionProfileMetadata::InteractionProfile *profile_def = meta_data->get_profile(profile_path);252if (profile_def != nullptr) {253profile_name = profile_def->display_name;254}255256create_dialog->set_base_type("OpenXRIPBindingModifier");257set_title(TTR("Binding modifiers for:") + " " + profile_name);258}259}260261262