Path: blob/master/modules/openxr/editor/openxr_action_map_editor.cpp
21085 views
/**************************************************************************/1/* openxr_action_map_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_action_map_editor.h"3132#include "core/config/project_settings.h"33#include "core/io/dir_access.h"34#include "core/io/resource_loader.h"35#include "core/io/resource_saver.h"36#include "editor/editor_node.h"37#include "editor/gui/editor_file_dialog.h"38#include "editor/settings/editor_command_palette.h"39#include "editor/settings/editor_settings.h"40#include "editor/themes/editor_scale.h"4142HashMap<String, String> OpenXRActionMapEditor::interaction_profile_editors;43HashMap<String, String> OpenXRActionMapEditor::binding_modifier_editors;4445void OpenXRActionMapEditor::_bind_methods() {46ClassDB::bind_method("_add_action_set_editor", &OpenXRActionMapEditor::_add_action_set_editor);47ClassDB::bind_method("_add_interaction_profile_editor", &OpenXRActionMapEditor::_add_interaction_profile_editor);4849ClassDB::bind_method(D_METHOD("_add_action_set", "name"), &OpenXRActionMapEditor::_add_action_set);50ClassDB::bind_method(D_METHOD("_remove_action_set", "name"), &OpenXRActionMapEditor::_remove_action_set);5152ClassDB::bind_method(D_METHOD("_do_add_action_set_editor", "action_set_editor"), &OpenXRActionMapEditor::_do_add_action_set_editor);53ClassDB::bind_method(D_METHOD("_do_remove_action_set_editor", "action_set_editor"), &OpenXRActionMapEditor::_do_remove_action_set_editor);54ClassDB::bind_method(D_METHOD("_do_add_interaction_profile_editor", "interaction_profile_editor"), &OpenXRActionMapEditor::_do_add_interaction_profile_editor);55ClassDB::bind_method(D_METHOD("_do_remove_interaction_profile_editor", "interaction_profile_editor"), &OpenXRActionMapEditor::_do_remove_interaction_profile_editor);5657ClassDB::bind_static_method("OpenXRActionMapEditor", D_METHOD("register_interaction_profile_editor", "interaction_profile_path", "editor_class"), &OpenXRActionMapEditor::register_interaction_profile_editor);58ClassDB::bind_static_method("OpenXRActionMapEditor", D_METHOD("register_binding_modifier_editor", "binding_modifier_class", "editor_class"), &OpenXRActionMapEditor::register_binding_modifier_editor);59}6061void OpenXRActionMapEditor::_notification(int p_what) {62switch (p_what) {63case NOTIFICATION_THEME_CHANGED: {64const String theme_style = EDITOR_GET("interface/theme/style");65tabs->set_theme_type_variation(theme_style == "Classic" ? "TabContainerOdd" : "TabContainerInner");6667for (int i = 0; i < tabs->get_child_count(); i++) {68Control *tab = Object::cast_to<Control>(tabs->get_child(i));69if (tab) {70tab->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));71}72}73} break;7475case NOTIFICATION_READY: {76_create_action_sets();77_create_interaction_profiles();78} break;79}80}8182OpenXRActionSetEditor *OpenXRActionMapEditor::_add_action_set_editor(const Ref<OpenXRActionSet> &p_action_set) {83ERR_FAIL_COND_V(p_action_set.is_null(), nullptr);8485OpenXRActionSetEditor *action_set_editor = memnew(OpenXRActionSetEditor(action_map, p_action_set));86action_set_editor->connect("remove", callable_mp(this, &OpenXRActionMapEditor::_on_remove_action_set));87action_set_editor->connect("action_removed", callable_mp(this, &OpenXRActionMapEditor::_on_action_removed));8889actionsets_vb->add_child(action_set_editor);9091return action_set_editor;92}9394void OpenXRActionMapEditor::_create_action_sets() {95if (action_map.is_valid()) {96Array action_sets = action_map->get_action_sets();97for (int i = 0; i < action_sets.size(); i++) {98Ref<OpenXRActionSet> action_set = action_sets[i];99_add_action_set_editor(action_set);100}101}102}103104OpenXRInteractionProfileEditorBase *OpenXRActionMapEditor::_add_interaction_profile_editor(const Ref<OpenXRInteractionProfile> &p_interaction_profile) {105ERR_FAIL_COND_V(p_interaction_profile.is_null(), nullptr);106107String profile_path = p_interaction_profile->get_interaction_profile_path();108109// need to instance the correct editor for our profile110OpenXRInteractionProfileEditorBase *new_profile_editor = nullptr;111if (interaction_profile_editors.has(profile_path)) {112Object *new_editor = ClassDB::instantiate(interaction_profile_editors[profile_path]);113if (new_editor) {114new_profile_editor = Object::cast_to<OpenXRInteractionProfileEditorBase>(new_editor);115if (!new_profile_editor) {116WARN_PRINT("Interaction profile editor type mismatch for " + profile_path);117memfree(new_editor);118}119}120}121if (!new_profile_editor) {122// instance generic editor123new_profile_editor = memnew(OpenXRInteractionProfileEditor);124}125126// now add it in..127ERR_FAIL_NULL_V(new_profile_editor, nullptr);128new_profile_editor->setup(action_map, p_interaction_profile);129tabs->add_child(new_profile_editor);130new_profile_editor->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));131tabs->set_tab_button_icon(tabs->get_tab_count() - 1, get_theme_icon(SNAME("close"), SNAME("TabBar")));132133if (!new_profile_editor->tooltip.is_empty()) {134tabs->set_tab_tooltip(tabs->get_tab_count() - 1, new_profile_editor->tooltip);135}136137return new_profile_editor;138}139140void OpenXRActionMapEditor::_create_interaction_profiles() {141if (action_map.is_valid()) {142Array new_interaction_profiles = action_map->get_interaction_profiles();143for (int i = 0; i < new_interaction_profiles.size(); i++) {144Ref<OpenXRInteractionProfile> interaction_profile = new_interaction_profiles[i];145_add_interaction_profile_editor(interaction_profile);146}147}148}149150OpenXRActionSetEditor *OpenXRActionMapEditor::_add_action_set(const String &p_name) {151ERR_FAIL_COND_V(action_map.is_null(), nullptr);152Ref<OpenXRActionSet> new_action_set;153154// add our new action set155new_action_set.instantiate();156new_action_set->set_name(p_name);157new_action_set->set_localized_name(p_name);158action_map->add_action_set(new_action_set);159action_map->set_edited(true);160161// update our editor right away162OpenXRActionSetEditor *action_set_editor = _add_action_set_editor(new_action_set);163164undo_redo->create_action(TTR("Add action set"));165undo_redo->add_do_method(this, "_do_add_action_set_editor", action_set_editor);166undo_redo->add_undo_method(this, "_do_remove_action_set_editor", action_set_editor);167undo_redo->commit_action(false);168169return action_set_editor;170}171172void OpenXRActionMapEditor::_remove_action_set(const String &p_name) {173ERR_FAIL_COND(action_map.is_null());174Ref<OpenXRActionSet> action_set = action_map->find_action_set(p_name);175ERR_FAIL_COND(action_set.is_null());176177for (int i = 0; i < actionsets_vb->get_child_count(); i++) {178OpenXRActionSetEditor *action_set_editor = Object::cast_to<OpenXRActionSetEditor>(actionsets_vb->get_child(i));179if (action_set_editor && action_set_editor->get_action_set() == action_set) {180_on_remove_action_set(action_set_editor);181}182}183}184185void OpenXRActionMapEditor::_on_add_action_set() {186ERR_FAIL_COND(action_map.is_null());187String new_name = "New";188int count = 0;189190while (action_map->find_action_set(new_name).is_valid()) {191new_name = "New_" + itos(count++);192}193194OpenXRActionSetEditor *new_action_set_editor = _add_action_set(new_name);195196// Make sure our action set is the current tab197tabs->set_current_tab(0);198199callable_mp(this, &OpenXRActionMapEditor::_set_focus_on_action_set).call_deferred(new_action_set_editor);200}201202void OpenXRActionMapEditor::_set_focus_on_action_set(OpenXRActionSetEditor *p_action_set_editor) {203// Scroll down to our new entry204actionsets_scroll->ensure_control_visible(p_action_set_editor);205206// Set focus on this entry207p_action_set_editor->set_focus_on_entry();208}209210void OpenXRActionMapEditor::_on_remove_action_set(Object *p_action_set_editor) {211ERR_FAIL_COND(action_map.is_null());212213OpenXRActionSetEditor *action_set_editor = Object::cast_to<OpenXRActionSetEditor>(p_action_set_editor);214ERR_FAIL_NULL(action_set_editor);215ERR_FAIL_COND(action_set_editor->get_parent() != actionsets_vb);216Ref<OpenXRActionSet> action_set = action_set_editor->get_action_set();217ERR_FAIL_COND(action_set.is_null());218219// Remove all actions first.220action_set_editor->remove_all_actions();221222// Make sure we update our interaction profiles.223for (int i = 0; i < tabs->get_tab_count(); i++) {224// First tab won't be an interaction profile editor, but being thorough..225OpenXRInteractionProfileEditorBase *interaction_profile_editor = Object::cast_to<OpenXRInteractionProfileEditorBase>(tabs->get_tab_control(i));226if (interaction_profile_editor) {227interaction_profile_editor->remove_all_for_action_set(action_set);228}229}230231// And now we can remove our action set.232undo_redo->create_action(TTR("Remove action set"));233undo_redo->add_do_method(this, "_do_remove_action_set_editor", action_set_editor);234undo_redo->add_undo_method(this, "_do_add_action_set_editor", action_set_editor);235undo_redo->commit_action(true);236237action_map->set_edited(true);238}239240void OpenXRActionMapEditor::_on_action_removed(const Ref<OpenXRAction> &p_action) {241for (int i = 0; i < tabs->get_tab_count(); i++) {242// First tab won't be an interaction profile editor, but being thorough..243OpenXRInteractionProfileEditorBase *interaction_profile_editor = Object::cast_to<OpenXRInteractionProfileEditorBase>(tabs->get_tab_control(i));244if (interaction_profile_editor) {245interaction_profile_editor->remove_all_for_action(p_action);246}247}248}249250void OpenXRActionMapEditor::_on_add_interaction_profile() {251ERR_FAIL_COND(action_map.is_null());252253PackedStringArray already_selected;254255for (int i = 0; i < action_map->get_interaction_profile_count(); i++) {256already_selected.push_back(action_map->get_interaction_profile(i)->get_interaction_profile_path());257}258259select_interaction_profile_dialog->open(already_selected);260}261262void OpenXRActionMapEditor::_on_interaction_profile_selected(const String &p_path) {263ERR_FAIL_COND(action_map.is_null());264265Ref<OpenXRInteractionProfile> new_profile;266new_profile.instantiate();267new_profile->set_interaction_profile_path(p_path);268action_map->add_interaction_profile(new_profile);269action_map->set_edited(true);270271OpenXRInteractionProfileEditorBase *interaction_profile_editor = _add_interaction_profile_editor(new_profile);272273undo_redo->create_action(TTR("Add interaction profile"));274undo_redo->add_do_method(this, "_do_add_interaction_profile_editor", interaction_profile_editor);275undo_redo->add_undo_method(this, "_do_remove_interaction_profile_editor", interaction_profile_editor);276undo_redo->commit_action(false);277278tabs->set_current_tab(tabs->get_tab_count() - 1);279}280281void OpenXRActionMapEditor::_load_action_map(const String &p_path, bool p_create_new_if_missing) {282Error err = OK;283Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);284if (da->file_exists(p_path)) {285action_map = ResourceLoader::load(p_path, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err);286if (err != OK) {287EditorNode::get_singleton()->show_warning(vformat(TTR("Error loading %s: %s."), edited_path, TTR(error_names[err])));288289edited_path = "";290header_label->set_text("");291return;292}293} else if (p_create_new_if_missing) {294action_map.instantiate();295action_map->create_default_action_sets();296297// Save it immediately298err = ResourceSaver::save(action_map, p_path);299if (err != OK) {300// Show warning but continue.301EditorNode::get_singleton()->show_warning(vformat(TTR("Error saving file %s: %s"), p_path, TTR(error_names[err])));302}303}304305edited_path = p_path;306header_label->set_text(TTR("OpenXR Action map:") + " " + edited_path.get_file());307}308309void OpenXRActionMapEditor::_on_save_action_map() {310Error err = ResourceSaver::save(action_map, edited_path);311if (err != OK) {312EditorNode::get_singleton()->show_warning(vformat(TTR("Error saving file %s: %s"), edited_path, TTR(error_names[err])));313return;314}315316// TODO should clear undo/redo history317318// out with the old319_clear_action_map();320321_create_action_sets();322_create_interaction_profiles();323}324325void OpenXRActionMapEditor::_on_reset_to_default_layout() {326// TODO should clear undo/redo history327328// out with the old329_clear_action_map();330331// create a new one332action_map.unref();333action_map.instantiate();334action_map->create_default_action_sets();335action_map->set_edited(true);336337_create_action_sets();338_create_interaction_profiles();339}340341void OpenXRActionMapEditor::_on_tabs_tab_changed(int p_tab) {342}343344void OpenXRActionMapEditor::_on_tab_button_pressed(int p_tab) {345OpenXRInteractionProfileEditorBase *interaction_profile_editor = Object::cast_to<OpenXRInteractionProfileEditorBase>(tabs->get_tab_control(p_tab));346ERR_FAIL_NULL(interaction_profile_editor);347348undo_redo->create_action(TTR("Remove interaction profile"));349undo_redo->add_do_method(this, "_do_remove_interaction_profile_editor", interaction_profile_editor);350undo_redo->add_undo_method(this, "_do_add_interaction_profile_editor", interaction_profile_editor);351undo_redo->commit_action(true);352353action_map->set_edited(true);354}355356void OpenXRActionMapEditor::_do_add_action_set_editor(OpenXRActionSetEditor *p_action_set_editor) {357Ref<OpenXRActionSet> action_set = p_action_set_editor->get_action_set();358ERR_FAIL_COND(action_set.is_null());359360action_map->add_action_set(action_set);361actionsets_vb->add_child(p_action_set_editor);362}363364void OpenXRActionMapEditor::_do_remove_action_set_editor(OpenXRActionSetEditor *p_action_set_editor) {365Ref<OpenXRActionSet> action_set = p_action_set_editor->get_action_set();366ERR_FAIL_COND(action_set.is_null());367368actionsets_vb->remove_child(p_action_set_editor);369action_map->remove_action_set(action_set);370}371372void OpenXRActionMapEditor::_do_add_interaction_profile_editor(OpenXRInteractionProfileEditorBase *p_interaction_profile_editor) {373Ref<OpenXRInteractionProfile> interaction_profile = p_interaction_profile_editor->get_interaction_profile();374ERR_FAIL_COND(interaction_profile.is_null());375376action_map->add_interaction_profile(interaction_profile);377tabs->add_child(p_interaction_profile_editor);378tabs->set_tab_button_icon(tabs->get_tab_count() - 1, get_theme_icon(SNAME("close"), SNAME("TabBar")));379380tabs->set_current_tab(tabs->get_tab_count() - 1);381}382383void OpenXRActionMapEditor::_do_remove_interaction_profile_editor(OpenXRInteractionProfileEditorBase *p_interaction_profile_editor) {384Ref<OpenXRInteractionProfile> interaction_profile = p_interaction_profile_editor->get_interaction_profile();385ERR_FAIL_COND(interaction_profile.is_null());386387tabs->remove_child(p_interaction_profile_editor);388action_map->remove_interaction_profile(interaction_profile);389}390391void OpenXRActionMapEditor::open_action_map(const String &p_path) {392make_visible();393394// out with the old...395_clear_action_map();396397// now load in our new action map398_load_action_map(p_path);399400_create_action_sets();401_create_interaction_profiles();402}403404void OpenXRActionMapEditor::_clear_action_map() {405while (actionsets_vb->get_child_count() > 0) {406Node *child = actionsets_vb->get_child(0);407actionsets_vb->remove_child(child);408child->queue_free();409}410411for (int i = tabs->get_tab_count() - 1; i >= 0; --i) {412// First tab won't be an interaction profile editor, but being thorough..413OpenXRInteractionProfileEditorBase *interaction_profile_editor = Object::cast_to<OpenXRInteractionProfileEditorBase>(tabs->get_tab_control(i));414if (interaction_profile_editor) {415tabs->remove_child(interaction_profile_editor);416interaction_profile_editor->queue_free();417}418}419}420421void OpenXRActionMapEditor::register_interaction_profile_editor(const String &p_for_path, const String &p_editor_class) {422interaction_profile_editors[p_for_path] = p_editor_class;423}424425void OpenXRActionMapEditor::register_binding_modifier_editor(const String &p_binding_modifier_class, const String &p_editor_class) {426binding_modifier_editors[p_binding_modifier_class] = p_editor_class;427}428429String OpenXRActionMapEditor::get_binding_modifier_editor_class(const String &p_binding_modifier_class) {430if (binding_modifier_editors.has(p_binding_modifier_class)) {431return binding_modifier_editors[p_binding_modifier_class];432}433434return OpenXRBindingModifierEditor::get_class_static();435}436437OpenXRActionMapEditor::OpenXRActionMapEditor() {438set_name(TTRC("OpenXR Action Map"));439set_icon_name("OpenXRActionMap");440set_dock_shortcut(ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_openxr_action_map_bottom_panel", TTRC("Toggle OpenXR Action Map Dock")));441set_default_slot(EditorDock::DOCK_SLOT_BOTTOM);442set_available_layouts(EditorDock::DOCK_LAYOUT_HORIZONTAL | EditorDock::DOCK_LAYOUT_FLOATING);443set_custom_minimum_size(Size2(0.0, 300.0 * EDSCALE));444445undo_redo = EditorUndoRedoManager::get_singleton();446447VBoxContainer *main_vb = memnew(VBoxContainer);448add_child(main_vb);449450top_hb = memnew(HBoxContainer);451main_vb->add_child(top_hb);452453header_label = memnew(Label);454header_label->set_text(String(TTR("Action Map")));455header_label->set_clip_text(true);456header_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);457top_hb->add_child(header_label);458459add_action_set = memnew(Button);460add_action_set->set_text(TTR("Add Action Set"));461add_action_set->set_tooltip_text(TTR("Add an action set."));462add_action_set->connect(SceneStringName(pressed), callable_mp(this, &OpenXRActionMapEditor::_on_add_action_set));463top_hb->add_child(add_action_set);464465add_interaction_profile = memnew(Button);466add_interaction_profile->set_text(TTR("Add profile"));467add_interaction_profile->set_tooltip_text(TTR("Add an interaction profile."));468add_interaction_profile->connect(SceneStringName(pressed), callable_mp(this, &OpenXRActionMapEditor::_on_add_interaction_profile));469top_hb->add_child(add_interaction_profile);470471VSeparator *vseparator = memnew(VSeparator);472top_hb->add_child(vseparator);473474save_as = memnew(Button);475save_as->set_text(TTR("Save"));476save_as->set_tooltip_text(TTR("Save this OpenXR action map."));477save_as->connect(SceneStringName(pressed), callable_mp(this, &OpenXRActionMapEditor::_on_save_action_map));478top_hb->add_child(save_as);479480_default = memnew(Button);481_default->set_text(TTR("Reset to Default"));482_default->set_tooltip_text(TTR("Reset to default OpenXR action map."));483_default->connect(SceneStringName(pressed), callable_mp(this, &OpenXRActionMapEditor::_on_reset_to_default_layout));484top_hb->add_child(_default);485486tabs = memnew(TabContainer);487tabs->set_h_size_flags(SIZE_EXPAND_FILL);488tabs->set_v_size_flags(SIZE_EXPAND_FILL);489tabs->connect("tab_changed", callable_mp(this, &OpenXRActionMapEditor::_on_tabs_tab_changed));490tabs->connect("tab_button_pressed", callable_mp(this, &OpenXRActionMapEditor::_on_tab_button_pressed));491main_vb->add_child(tabs);492493actionsets_scroll = memnew(ScrollContainer);494actionsets_scroll->set_h_size_flags(SIZE_EXPAND_FILL);495actionsets_scroll->set_v_size_flags(SIZE_EXPAND_FILL);496actionsets_scroll->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);497tabs->add_child(actionsets_scroll);498actionsets_scroll->set_name(TTR("Action Sets"));499500actionsets_vb = memnew(VBoxContainer);501actionsets_vb->set_h_size_flags(SIZE_EXPAND_FILL);502actionsets_scroll->add_child(actionsets_vb);503504select_interaction_profile_dialog = memnew(OpenXRSelectInteractionProfileDialog);505select_interaction_profile_dialog->connect("interaction_profile_selected", callable_mp(this, &OpenXRActionMapEditor::_on_interaction_profile_selected));506add_child(select_interaction_profile_dialog);507508// Our Action map editor is only shown if openxr is enabled in project settings509// So load our action map and if it doesn't exist, create it right away.510_load_action_map(ResourceUID::ensure_path(GLOBAL_GET("xr/openxr/default_action_map")), true);511}512513514