Path: blob/master/modules/openxr/editor/openxr_interaction_profile_editor.cpp
21222 views
/**************************************************************************/1/* openxr_interaction_profile_editor.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_interaction_profile_editor.h"31#include "../openxr_api.h"32#include "editor/editor_string_names.h"3334///////////////////////////////////////////////////////////////////////////35// Interaction profile editor base3637void OpenXRInteractionProfileEditorBase::_bind_methods() {38ClassDB::bind_method(D_METHOD("setup", "action_map", "interaction_profile"), &OpenXRInteractionProfileEditorBase::setup);3940ClassDB::bind_method(D_METHOD("_add_binding", "action", "path"), &OpenXRInteractionProfileEditorBase::_add_binding);41ClassDB::bind_method(D_METHOD("_remove_binding", "action", "path"), &OpenXRInteractionProfileEditorBase::_remove_binding);42}4344void OpenXRInteractionProfileEditorBase::_notification(int p_what) {45switch (p_what) {46case NOTIFICATION_ENTER_TREE: {47_update_interaction_profile();48} break;4950case NOTIFICATION_THEME_CHANGED: {51_theme_changed();52} break;53}54}5556void OpenXRInteractionProfileEditorBase::_do_update_interaction_profile() {57if (!is_dirty) {58is_dirty = true;59callable_mp(this, &OpenXRInteractionProfileEditorBase::_update_interaction_profile).call_deferred();60}61}6263void OpenXRInteractionProfileEditorBase::_add_binding(const String &p_action, const String &p_path) {64ERR_FAIL_COND(action_map.is_null());65ERR_FAIL_COND(interaction_profile.is_null());6667Ref<OpenXRAction> action = action_map->get_action(p_action);68ERR_FAIL_COND(action.is_null());6970Ref<OpenXRIPBinding> binding = interaction_profile->find_binding(action, p_path);71if (binding.is_null()) {72// create a new binding73binding.instantiate();74binding->set_action(action);75binding->set_binding_path(p_path);7677// add it to our interaction profile78interaction_profile->add_binding(binding);79interaction_profile->set_edited(true);8081binding->set_edited(true);82}8384// Update our toplevel paths85action->set_toplevel_paths(action_map->get_top_level_paths(action));8687_do_update_interaction_profile();88}8990void OpenXRInteractionProfileEditorBase::_remove_binding(const String &p_action, const String &p_path) {91ERR_FAIL_COND(action_map.is_null());92ERR_FAIL_COND(interaction_profile.is_null());9394Ref<OpenXRAction> action = action_map->get_action(p_action);95ERR_FAIL_COND(action.is_null());9697Ref<OpenXRIPBinding> binding = interaction_profile->find_binding(action, p_path);98if (binding.is_valid()) {99interaction_profile->remove_binding(binding);100interaction_profile->set_edited(true);101102// Update our toplevel paths103action->set_toplevel_paths(action_map->get_top_level_paths(action));104105_do_update_interaction_profile();106}107}108109void OpenXRInteractionProfileEditorBase::_update_interaction_profile() {110if (!is_dirty) {111// no need to update112return;113}114115// Nothing to do here for now..116117// and we've updated it...118is_dirty = false;119}120121void OpenXRInteractionProfileEditorBase::_theme_changed() {122if (binding_modifiers_btn) {123binding_modifiers_btn->set_button_icon(get_theme_icon(SNAME("Modifiers"), EditorStringName(EditorIcons)));124}125}126127void OpenXRInteractionProfileEditorBase::remove_all_for_action_set(const Ref<OpenXRActionSet> &p_action_set) {128// Note, don't need to remove bindings themselves as remove_all_for_action will be called for each before this is called.129130// TODO update binding modifiers131}132133void OpenXRInteractionProfileEditorBase::remove_all_for_action(const Ref<OpenXRAction> &p_action) {134Vector<Ref<OpenXRIPBinding>> bindings = interaction_profile->get_bindings_for_action(p_action);135if (bindings.size() > 0) {136String action_name = p_action->get_name_with_set();137138// For our undo/redo we process all paths139undo_redo->create_action(TTR("Remove action from interaction profile"));140for (const Ref<OpenXRIPBinding> &binding : bindings) {141undo_redo->add_do_method(this, "_remove_binding", action_name, binding->get_binding_path());142undo_redo->add_undo_method(this, "_add_binding", action_name, binding->get_binding_path());143}144undo_redo->commit_action(false);145146// But remove them all in one go so we're more efficient in updating our UI.147for (const Ref<OpenXRIPBinding> &binding : bindings) {148interaction_profile->remove_binding(binding);149}150interaction_profile->set_edited(true);151152// Update our toplevel paths153p_action->set_toplevel_paths(action_map->get_top_level_paths(p_action));154155_do_update_interaction_profile();156}157}158void OpenXRInteractionProfileEditorBase::_on_open_binding_modifiers() {159binding_modifiers_dialog->popup_centered(Size2i(500, 400));160}161162OpenXRInteractionProfileEditorBase::OpenXRInteractionProfileEditorBase() {163undo_redo = EditorUndoRedoManager::get_singleton();164165set_h_size_flags(SIZE_EXPAND_FILL);166set_v_size_flags(SIZE_EXPAND_FILL);167168interaction_profile_sc = memnew(ScrollContainer);169interaction_profile_sc->set_h_size_flags(SIZE_EXPAND_FILL);170interaction_profile_sc->set_v_size_flags(SIZE_EXPAND_FILL);171add_child(interaction_profile_sc);172173binding_modifiers_dialog = memnew(OpenXRBindingModifiersDialog);174add_child(binding_modifiers_dialog);175176toolbar_vb = memnew(VBoxContainer);177toolbar_vb->set_v_size_flags(SIZE_EXPAND_FILL);178add_child(toolbar_vb);179180binding_modifiers_btn = memnew(Button);181binding_modifiers_btn->set_tooltip_text(TTR("Edit binding modifiers"));182binding_modifiers_btn->connect("pressed", callable_mp(this, &OpenXRInteractionProfileEditorBase::_on_open_binding_modifiers));183// TODO show visual difference if there are binding modifiers for this interaction profile184toolbar_vb->add_child(binding_modifiers_btn);185}186187void OpenXRInteractionProfileEditorBase::setup(const Ref<OpenXRActionMap> &p_action_map, const Ref<OpenXRInteractionProfile> &p_interaction_profile) {188ERR_FAIL_NULL(binding_modifiers_dialog);189binding_modifiers_dialog->setup(p_action_map, p_interaction_profile);190191action_map = p_action_map;192interaction_profile = p_interaction_profile;193String profile_path = interaction_profile->get_interaction_profile_path();194String profile_name = profile_path;195196profile_def = OpenXRInteractionProfileMetadata::get_singleton()->get_profile(profile_path);197if (profile_def != nullptr) {198profile_name = profile_def->display_name;199200if (!profile_def->openxr_extension_names.is_empty()) {201profile_name += "*";202203tooltip = vformat(TTR("Note: This interaction profile requires extension %s support."), profile_def->openxr_extension_names);204}205}206207set_name(profile_name);208209// Make sure it is updated when it enters the tree...210is_dirty = true;211}212213///////////////////////////////////////////////////////////////////////////214// Default interaction profile editor215216void OpenXRInteractionProfileEditor::select_action_for(const String &p_io_path) {217selecting_for_io_path = p_io_path;218select_action_dialog->open();219}220221void OpenXRInteractionProfileEditor::_on_action_selected(const String &p_action) {222undo_redo->create_action(TTR("Add binding"));223undo_redo->add_do_method(this, "_add_binding", p_action, selecting_for_io_path);224undo_redo->add_undo_method(this, "_remove_binding", p_action, selecting_for_io_path);225undo_redo->commit_action(true);226227selecting_for_io_path = "";228}229230void OpenXRInteractionProfileEditor::_on_remove_pressed(const String &p_action, const String &p_for_io_path) {231undo_redo->create_action(TTR("Remove binding"));232undo_redo->add_do_method(this, "_remove_binding", p_action, p_for_io_path);233undo_redo->add_undo_method(this, "_add_binding", p_action, p_for_io_path);234undo_redo->commit_action(true);235}236237void OpenXRInteractionProfileEditor::_add_io_path(VBoxContainer *p_container, const OpenXRInteractionProfileMetadata::IOPath *p_io_path) {238HBoxContainer *path_hb = memnew(HBoxContainer);239path_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);240p_container->add_child(path_hb);241242Label *path_label = memnew(Label);243path_label->set_focus_mode(FOCUS_ACCESSIBILITY);244if (p_io_path->openxr_extension_names.is_empty()) {245path_label->set_text(p_io_path->display_name);246} else {247path_label->set_text(p_io_path->display_name + "*");248249String extension_names = p_io_path->openxr_extension_names.replace(",", " or ").replace(XR_OPENXR_1_1_NAME, "OpenXR 1.1");250path_hb->set_tooltip_text(vformat(TTR("Note: This binding path requires extension %s support."), extension_names));251}252path_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);253path_hb->add_child(path_label);254255Label *type_label = memnew(Label);256type_label->set_focus_mode(FOCUS_ACCESSIBILITY);257switch (p_io_path->action_type) {258case OpenXRAction::OPENXR_ACTION_BOOL: {259type_label->set_text(TTR("Boolean"));260} break;261case OpenXRAction::OPENXR_ACTION_FLOAT: {262type_label->set_text(TTR("Float"));263} break;264case OpenXRAction::OPENXR_ACTION_VECTOR2: {265type_label->set_text(TTR("Vector2"));266} break;267case OpenXRAction::OPENXR_ACTION_POSE: {268type_label->set_text(TTR("Pose"));269} break;270case OpenXRAction::OPENXR_ACTION_HAPTIC: {271type_label->set_text(TTR("Haptic"));272} break;273default: {274type_label->set_text(TTR("Unknown"));275} break;276}277type_label->set_custom_minimum_size(Size2(50.0, 0.0));278path_hb->add_child(type_label);279280Button *path_add = memnew(Button);281path_add->set_button_icon(get_theme_icon(SNAME("Add"), EditorStringName(EditorIcons)));282path_add->set_flat(true);283path_add->connect(SceneStringName(pressed), callable_mp(this, &OpenXRInteractionProfileEditor::select_action_for).bind(String(p_io_path->openxr_path)));284path_hb->add_child(path_add);285286if (interaction_profile.is_valid()) {287String io_path = String(p_io_path->openxr_path);288Array bindings = interaction_profile->get_bindings();289for (Ref<OpenXRIPBinding> binding : bindings) {290if (binding->get_binding_path() == io_path) {291Ref<OpenXRAction> action = binding->get_action();292293HBoxContainer *action_hb = memnew(HBoxContainer);294action_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);295p_container->add_child(action_hb);296297Control *indent_node = memnew(Control);298indent_node->set_custom_minimum_size(Size2(10.0, 0.0));299action_hb->add_child(indent_node);300301Label *action_label = memnew(Label);302action_label->set_focus_mode(FOCUS_ACCESSIBILITY);303action_label->set_text(action->get_name_with_set() + ": " + action->get_localized_name());304action_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);305action_hb->add_child(action_label);306307OpenXRBindingModifiersDialog *action_binding_modifiers_dialog = memnew(OpenXRBindingModifiersDialog);308action_binding_modifiers_dialog->setup(action_map, interaction_profile, binding);309action_hb->add_child(action_binding_modifiers_dialog);310311Button *action_binding_modifiers_btn = memnew(Button);312action_binding_modifiers_btn->set_flat(true);313action_binding_modifiers_btn->set_button_icon(get_theme_icon(SNAME("Modifiers"), EditorStringName(EditorIcons)));314action_binding_modifiers_btn->connect(SceneStringName(pressed), callable_mp((Window *)action_binding_modifiers_dialog, &Window::popup_centered).bind(Size2i(500, 400)));315action_binding_modifiers_btn->set_accessibility_name(TTRC("Modifiers"));316// TODO change style of button if there are binding modifiers317action_hb->add_child(action_binding_modifiers_btn);318319Button *action_rem = memnew(Button);320action_rem->set_flat(true);321action_rem->set_button_icon(get_theme_icon(SNAME("Remove"), EditorStringName(EditorIcons)));322action_rem->connect(SceneStringName(pressed), callable_mp((OpenXRInteractionProfileEditor *)this, &OpenXRInteractionProfileEditor::_on_remove_pressed).bind(action->get_name_with_set(), String(p_io_path->openxr_path)));323action_rem->set_accessibility_name(TTRC("Remove"));324action_hb->add_child(action_rem);325}326}327}328}329330void OpenXRInteractionProfileEditor::_update_interaction_profile() {331ERR_FAIL_NULL(profile_def);332333if (!is_dirty) {334// no need to update335return;336}337338PackedStringArray requested_extensions = OpenXRAPI::get_all_requested_extensions(0);339340// out with the old...341while (interaction_profile_hb->get_child_count() > 0) {342memdelete(interaction_profile_hb->get_child(0));343}344345// in with the new...346347// Determine toplevel paths348Vector<String> top_level_paths;349for (int i = 0; i < profile_def->io_paths.size(); i++) {350const OpenXRInteractionProfileMetadata::IOPath *io_path = &profile_def->io_paths[i];351352if (!top_level_paths.has(io_path->top_level_path)) {353top_level_paths.push_back(io_path->top_level_path);354}355}356357for (int i = 0; i < top_level_paths.size(); i++) {358PanelContainer *panel = memnew(PanelContainer);359panel->set_v_size_flags(Control::SIZE_EXPAND_FILL);360interaction_profile_hb->add_child(panel);361panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("TabContainer")));362363VBoxContainer *container = memnew(VBoxContainer);364panel->add_child(container);365366Label *label = memnew(Label);367label->set_focus_mode(Control::FOCUS_ACCESSIBILITY);368label->set_text(OpenXRInteractionProfileMetadata::get_singleton()->get_top_level_name(top_level_paths[i]));369container->add_child(label);370371for (int j = 0; j < profile_def->io_paths.size(); j++) {372const OpenXRInteractionProfileMetadata::IOPath *io_path = &profile_def->io_paths[j];373374const Vector<String> extensions = io_path->openxr_extension_names.split(",", false);375bool extension_is_requested = extensions.is_empty(); // If none, then yes we can use this.376for (const String &extension : extensions) {377extension_is_requested |= requested_extensions.has(extension);378}379380if (io_path->top_level_path == top_level_paths[i] && extension_is_requested) {381_add_io_path(container, io_path);382}383}384}385386OpenXRInteractionProfileEditorBase::_update_interaction_profile();387}388389void OpenXRInteractionProfileEditor::_theme_changed() {390OpenXRInteractionProfileEditorBase::_theme_changed();391392interaction_profile_sc->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));393394for (int i = 0; i < interaction_profile_hb->get_child_count(); i++) {395Control *panel = Object::cast_to<Control>(interaction_profile_hb->get_child(i));396if (panel) {397panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("TabContainer")));398}399}400}401402OpenXRInteractionProfileEditor::OpenXRInteractionProfileEditor() {403interaction_profile_hb = memnew(HBoxContainer);404interaction_profile_sc->add_child(interaction_profile_hb);405}406407void OpenXRInteractionProfileEditor::setup(const Ref<OpenXRActionMap> &p_action_map, const Ref<OpenXRInteractionProfile> &p_interaction_profile) {408OpenXRInteractionProfileEditorBase::setup(p_action_map, p_interaction_profile);409410select_action_dialog = memnew(OpenXRSelectActionDialog(p_action_map));411select_action_dialog->connect("action_selected", callable_mp(this, &OpenXRInteractionProfileEditor::_on_action_selected));412add_child(select_action_dialog);413}414415416