Path: blob/master/modules/openxr/editor/openxr_action_map_editor.cpp
10278 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 "editor/editor_node.h"34#include "editor/gui/editor_bottom_panel.h"35#include "editor/gui/editor_file_dialog.h"36#include "editor/themes/editor_scale.h"3738HashMap<String, String> OpenXRActionMapEditor::interaction_profile_editors;39HashMap<String, String> OpenXRActionMapEditor::binding_modifier_editors;4041void OpenXRActionMapEditor::_bind_methods() {42ClassDB::bind_method("_add_action_set_editor", &OpenXRActionMapEditor::_add_action_set_editor);43ClassDB::bind_method("_add_interaction_profile_editor", &OpenXRActionMapEditor::_add_interaction_profile_editor);4445ClassDB::bind_method(D_METHOD("_add_action_set", "name"), &OpenXRActionMapEditor::_add_action_set);46ClassDB::bind_method(D_METHOD("_remove_action_set", "name"), &OpenXRActionMapEditor::_remove_action_set);4748ClassDB::bind_method(D_METHOD("_do_add_action_set_editor", "action_set_editor"), &OpenXRActionMapEditor::_do_add_action_set_editor);49ClassDB::bind_method(D_METHOD("_do_remove_action_set_editor", "action_set_editor"), &OpenXRActionMapEditor::_do_remove_action_set_editor);50ClassDB::bind_method(D_METHOD("_do_add_interaction_profile_editor", "interaction_profile_editor"), &OpenXRActionMapEditor::_do_add_interaction_profile_editor);51ClassDB::bind_method(D_METHOD("_do_remove_interaction_profile_editor", "interaction_profile_editor"), &OpenXRActionMapEditor::_do_remove_interaction_profile_editor);5253ClassDB::bind_static_method("OpenXRActionMapEditor", D_METHOD("register_interaction_profile_editor", "interaction_profile_path", "editor_class"), &OpenXRActionMapEditor::register_interaction_profile_editor);54ClassDB::bind_static_method("OpenXRActionMapEditor", D_METHOD("register_binding_modifier_editor", "binding_modifier_class", "editor_class"), &OpenXRActionMapEditor::register_binding_modifier_editor);55}5657void OpenXRActionMapEditor::_notification(int p_what) {58switch (p_what) {59case NOTIFICATION_THEME_CHANGED: {60for (int i = 0; i < tabs->get_child_count(); i++) {61Control *tab = Object::cast_to<Control>(tabs->get_child(i));62if (tab) {63tab->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));64}65}66} break;6768case NOTIFICATION_READY: {69_create_action_sets();70_create_interaction_profiles();71} break;72}73}7475OpenXRActionSetEditor *OpenXRActionMapEditor::_add_action_set_editor(Ref<OpenXRActionSet> p_action_set) {76ERR_FAIL_COND_V(p_action_set.is_null(), nullptr);7778OpenXRActionSetEditor *action_set_editor = memnew(OpenXRActionSetEditor(action_map, p_action_set));79action_set_editor->connect("remove", callable_mp(this, &OpenXRActionMapEditor::_on_remove_action_set));80action_set_editor->connect("action_removed", callable_mp(this, &OpenXRActionMapEditor::_on_action_removed));8182actionsets_vb->add_child(action_set_editor);8384return action_set_editor;85}8687void OpenXRActionMapEditor::_create_action_sets() {88if (action_map.is_valid()) {89Array action_sets = action_map->get_action_sets();90for (int i = 0; i < action_sets.size(); i++) {91Ref<OpenXRActionSet> action_set = action_sets[i];92_add_action_set_editor(action_set);93}94}95}9697OpenXRInteractionProfileEditorBase *OpenXRActionMapEditor::_add_interaction_profile_editor(Ref<OpenXRInteractionProfile> p_interaction_profile) {98ERR_FAIL_COND_V(p_interaction_profile.is_null(), nullptr);99100String profile_path = p_interaction_profile->get_interaction_profile_path();101102// need to instance the correct editor for our profile103OpenXRInteractionProfileEditorBase *new_profile_editor = nullptr;104if (interaction_profile_editors.has(profile_path)) {105Object *new_editor = ClassDB::instantiate(interaction_profile_editors[profile_path]);106if (new_editor) {107new_profile_editor = Object::cast_to<OpenXRInteractionProfileEditorBase>(new_editor);108if (!new_profile_editor) {109WARN_PRINT("Interaction profile editor type mismatch for " + profile_path);110memfree(new_editor);111}112}113}114if (!new_profile_editor) {115// instance generic editor116new_profile_editor = memnew(OpenXRInteractionProfileEditor);117}118119// now add it in..120ERR_FAIL_NULL_V(new_profile_editor, nullptr);121new_profile_editor->setup(action_map, p_interaction_profile);122tabs->add_child(new_profile_editor);123new_profile_editor->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));124tabs->set_tab_button_icon(tabs->get_tab_count() - 1, get_theme_icon(SNAME("close"), SNAME("TabBar")));125126if (!new_profile_editor->tooltip.is_empty()) {127tabs->set_tab_tooltip(tabs->get_tab_count() - 1, new_profile_editor->tooltip);128}129130return new_profile_editor;131}132133void OpenXRActionMapEditor::_create_interaction_profiles() {134if (action_map.is_valid()) {135Array new_interaction_profiles = action_map->get_interaction_profiles();136for (int i = 0; i < new_interaction_profiles.size(); i++) {137Ref<OpenXRInteractionProfile> interaction_profile = new_interaction_profiles[i];138_add_interaction_profile_editor(interaction_profile);139}140}141}142143OpenXRActionSetEditor *OpenXRActionMapEditor::_add_action_set(String p_name) {144ERR_FAIL_COND_V(action_map.is_null(), nullptr);145Ref<OpenXRActionSet> new_action_set;146147// add our new action set148new_action_set.instantiate();149new_action_set->set_name(p_name);150new_action_set->set_localized_name(p_name);151action_map->add_action_set(new_action_set);152action_map->set_edited(true);153154// update our editor right away155OpenXRActionSetEditor *action_set_editor = _add_action_set_editor(new_action_set);156157undo_redo->create_action(TTR("Add action set"));158undo_redo->add_do_method(this, "_do_add_action_set_editor", action_set_editor);159undo_redo->add_undo_method(this, "_do_remove_action_set_editor", action_set_editor);160undo_redo->commit_action(false);161162return action_set_editor;163}164165void OpenXRActionMapEditor::_remove_action_set(String p_name) {166ERR_FAIL_COND(action_map.is_null());167Ref<OpenXRActionSet> action_set = action_map->find_action_set(p_name);168ERR_FAIL_COND(action_set.is_null());169170for (int i = 0; i < actionsets_vb->get_child_count(); i++) {171OpenXRActionSetEditor *action_set_editor = Object::cast_to<OpenXRActionSetEditor>(actionsets_vb->get_child(i));172if (action_set_editor && action_set_editor->get_action_set() == action_set) {173_on_remove_action_set(action_set_editor);174}175}176}177178void OpenXRActionMapEditor::_on_add_action_set() {179ERR_FAIL_COND(action_map.is_null());180String new_name = "New";181int count = 0;182183while (action_map->find_action_set(new_name).is_valid()) {184new_name = "New_" + itos(count++);185}186187OpenXRActionSetEditor *new_action_set_editor = _add_action_set(new_name);188189// Make sure our action set is the current tab190tabs->set_current_tab(0);191192callable_mp(this, &OpenXRActionMapEditor::_set_focus_on_action_set).call_deferred(new_action_set_editor);193}194195void OpenXRActionMapEditor::_set_focus_on_action_set(OpenXRActionSetEditor *p_action_set_editor) {196// Scroll down to our new entry197actionsets_scroll->ensure_control_visible(p_action_set_editor);198199// Set focus on this entry200p_action_set_editor->set_focus_on_entry();201}202203void OpenXRActionMapEditor::_on_remove_action_set(Object *p_action_set_editor) {204ERR_FAIL_COND(action_map.is_null());205206OpenXRActionSetEditor *action_set_editor = Object::cast_to<OpenXRActionSetEditor>(p_action_set_editor);207ERR_FAIL_NULL(action_set_editor);208ERR_FAIL_COND(action_set_editor->get_parent() != actionsets_vb);209Ref<OpenXRActionSet> action_set = action_set_editor->get_action_set();210ERR_FAIL_COND(action_set.is_null());211212// Remove all actions first.213action_set_editor->remove_all_actions();214215// Make sure we update our interaction profiles.216for (int i = 0; i < tabs->get_tab_count(); i++) {217// First tab won't be an interaction profile editor, but being thorough..218OpenXRInteractionProfileEditorBase *interaction_profile_editor = Object::cast_to<OpenXRInteractionProfileEditorBase>(tabs->get_tab_control(i));219if (interaction_profile_editor) {220interaction_profile_editor->remove_all_for_action_set(action_set);221}222}223224// And now we can remove our action set.225undo_redo->create_action(TTR("Remove action set"));226undo_redo->add_do_method(this, "_do_remove_action_set_editor", action_set_editor);227undo_redo->add_undo_method(this, "_do_add_action_set_editor", action_set_editor);228undo_redo->commit_action(true);229230action_map->set_edited(true);231}232233void OpenXRActionMapEditor::_on_action_removed(Ref<OpenXRAction> p_action) {234for (int i = 0; i < tabs->get_tab_count(); i++) {235// First tab won't be an interaction profile editor, but being thorough..236OpenXRInteractionProfileEditorBase *interaction_profile_editor = Object::cast_to<OpenXRInteractionProfileEditorBase>(tabs->get_tab_control(i));237if (interaction_profile_editor) {238interaction_profile_editor->remove_all_for_action(p_action);239}240}241}242243void OpenXRActionMapEditor::_on_add_interaction_profile() {244ERR_FAIL_COND(action_map.is_null());245246PackedStringArray already_selected;247248for (int i = 0; i < action_map->get_interaction_profile_count(); i++) {249already_selected.push_back(action_map->get_interaction_profile(i)->get_interaction_profile_path());250}251252select_interaction_profile_dialog->open(already_selected);253}254255void OpenXRActionMapEditor::_on_interaction_profile_selected(const String p_path) {256ERR_FAIL_COND(action_map.is_null());257258Ref<OpenXRInteractionProfile> new_profile;259new_profile.instantiate();260new_profile->set_interaction_profile_path(p_path);261action_map->add_interaction_profile(new_profile);262action_map->set_edited(true);263264OpenXRInteractionProfileEditorBase *interaction_profile_editor = _add_interaction_profile_editor(new_profile);265266undo_redo->create_action(TTR("Add interaction profile"));267undo_redo->add_do_method(this, "_do_add_interaction_profile_editor", interaction_profile_editor);268undo_redo->add_undo_method(this, "_do_remove_interaction_profile_editor", interaction_profile_editor);269undo_redo->commit_action(false);270271tabs->set_current_tab(tabs->get_tab_count() - 1);272}273274void OpenXRActionMapEditor::_load_action_map(const String p_path, bool p_create_new_if_missing) {275Error err = OK;276Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);277if (da->file_exists(p_path)) {278action_map = ResourceLoader::load(p_path, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err);279if (err != OK) {280EditorNode::get_singleton()->show_warning(vformat(TTR("Error loading %s: %s."), edited_path, error_names[err]));281282edited_path = "";283header_label->set_text("");284return;285}286} else if (p_create_new_if_missing) {287action_map.instantiate();288action_map->create_default_action_sets();289290// Save it immediately291err = ResourceSaver::save(action_map, p_path);292if (err != OK) {293// Show warning but continue.294EditorNode::get_singleton()->show_warning(vformat(TTR("Error saving file %s: %s"), p_path, error_names[err]));295}296}297298edited_path = p_path;299header_label->set_text(TTR("OpenXR Action map:") + " " + edited_path.get_file());300}301302void OpenXRActionMapEditor::_on_save_action_map() {303Error err = ResourceSaver::save(action_map, edited_path);304if (err != OK) {305EditorNode::get_singleton()->show_warning(vformat(TTR("Error saving file %s: %s"), edited_path, error_names[err]));306return;307}308309// TODO should clear undo/redo history310311// out with the old312_clear_action_map();313314_create_action_sets();315_create_interaction_profiles();316}317318void OpenXRActionMapEditor::_on_reset_to_default_layout() {319// TODO should clear undo/redo history320321// out with the old322_clear_action_map();323324// create a new one325action_map.unref();326action_map.instantiate();327action_map->create_default_action_sets();328action_map->set_edited(true);329330_create_action_sets();331_create_interaction_profiles();332}333334void OpenXRActionMapEditor::_on_tabs_tab_changed(int p_tab) {335}336337void OpenXRActionMapEditor::_on_tab_button_pressed(int p_tab) {338OpenXRInteractionProfileEditorBase *interaction_profile_editor = Object::cast_to<OpenXRInteractionProfileEditorBase>(tabs->get_tab_control(p_tab));339ERR_FAIL_NULL(interaction_profile_editor);340341undo_redo->create_action(TTR("Remove interaction profile"));342undo_redo->add_do_method(this, "_do_remove_interaction_profile_editor", interaction_profile_editor);343undo_redo->add_undo_method(this, "_do_add_interaction_profile_editor", interaction_profile_editor);344undo_redo->commit_action(true);345346action_map->set_edited(true);347}348349void OpenXRActionMapEditor::_do_add_action_set_editor(OpenXRActionSetEditor *p_action_set_editor) {350Ref<OpenXRActionSet> action_set = p_action_set_editor->get_action_set();351ERR_FAIL_COND(action_set.is_null());352353action_map->add_action_set(action_set);354actionsets_vb->add_child(p_action_set_editor);355}356357void OpenXRActionMapEditor::_do_remove_action_set_editor(OpenXRActionSetEditor *p_action_set_editor) {358Ref<OpenXRActionSet> action_set = p_action_set_editor->get_action_set();359ERR_FAIL_COND(action_set.is_null());360361actionsets_vb->remove_child(p_action_set_editor);362action_map->remove_action_set(action_set);363}364365void OpenXRActionMapEditor::_do_add_interaction_profile_editor(OpenXRInteractionProfileEditorBase *p_interaction_profile_editor) {366Ref<OpenXRInteractionProfile> interaction_profile = p_interaction_profile_editor->get_interaction_profile();367ERR_FAIL_COND(interaction_profile.is_null());368369action_map->add_interaction_profile(interaction_profile);370tabs->add_child(p_interaction_profile_editor);371tabs->set_tab_button_icon(tabs->get_tab_count() - 1, get_theme_icon(SNAME("close"), SNAME("TabBar")));372373tabs->set_current_tab(tabs->get_tab_count() - 1);374}375376void OpenXRActionMapEditor::_do_remove_interaction_profile_editor(OpenXRInteractionProfileEditorBase *p_interaction_profile_editor) {377Ref<OpenXRInteractionProfile> interaction_profile = p_interaction_profile_editor->get_interaction_profile();378ERR_FAIL_COND(interaction_profile.is_null());379380tabs->remove_child(p_interaction_profile_editor);381action_map->remove_interaction_profile(interaction_profile);382}383384void OpenXRActionMapEditor::open_action_map(String p_path) {385EditorNode::get_bottom_panel()->make_item_visible(this);386387// out with the old...388_clear_action_map();389390// now load in our new action map391_load_action_map(p_path);392393_create_action_sets();394_create_interaction_profiles();395}396397void OpenXRActionMapEditor::_clear_action_map() {398while (actionsets_vb->get_child_count() > 0) {399Node *child = actionsets_vb->get_child(0);400actionsets_vb->remove_child(child);401child->queue_free();402}403404for (int i = tabs->get_tab_count() - 1; i >= 0; --i) {405// First tab won't be an interaction profile editor, but being thorough..406OpenXRInteractionProfileEditorBase *interaction_profile_editor = Object::cast_to<OpenXRInteractionProfileEditorBase>(tabs->get_tab_control(i));407if (interaction_profile_editor) {408tabs->remove_child(interaction_profile_editor);409interaction_profile_editor->queue_free();410}411}412}413414void OpenXRActionMapEditor::register_interaction_profile_editor(const String &p_for_path, const String &p_editor_class) {415interaction_profile_editors[p_for_path] = p_editor_class;416}417418void OpenXRActionMapEditor::register_binding_modifier_editor(const String &p_binding_modifier_class, const String &p_editor_class) {419binding_modifier_editors[p_binding_modifier_class] = p_editor_class;420}421422String OpenXRActionMapEditor::get_binding_modifier_editor_class(const String &p_binding_modifier_class) {423if (binding_modifier_editors.has(p_binding_modifier_class)) {424return binding_modifier_editors[p_binding_modifier_class];425}426427return OpenXRBindingModifierEditor::get_class_static();428}429430OpenXRActionMapEditor::OpenXRActionMapEditor() {431undo_redo = EditorUndoRedoManager::get_singleton();432set_custom_minimum_size(Size2(0.0, 300.0 * EDSCALE));433434top_hb = memnew(HBoxContainer);435add_child(top_hb);436437header_label = memnew(Label);438header_label->set_text(String(TTR("Action Map")));439header_label->set_clip_text(true);440header_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);441top_hb->add_child(header_label);442443add_action_set = memnew(Button);444add_action_set->set_text(TTR("Add Action Set"));445add_action_set->set_tooltip_text(TTR("Add an action set."));446add_action_set->connect(SceneStringName(pressed), callable_mp(this, &OpenXRActionMapEditor::_on_add_action_set));447top_hb->add_child(add_action_set);448449add_interaction_profile = memnew(Button);450add_interaction_profile->set_text(TTR("Add profile"));451add_interaction_profile->set_tooltip_text(TTR("Add an interaction profile."));452add_interaction_profile->connect(SceneStringName(pressed), callable_mp(this, &OpenXRActionMapEditor::_on_add_interaction_profile));453top_hb->add_child(add_interaction_profile);454455VSeparator *vseparator = memnew(VSeparator);456top_hb->add_child(vseparator);457458save_as = memnew(Button);459save_as->set_text(TTR("Save"));460save_as->set_tooltip_text(TTR("Save this OpenXR action map."));461save_as->connect(SceneStringName(pressed), callable_mp(this, &OpenXRActionMapEditor::_on_save_action_map));462top_hb->add_child(save_as);463464_default = memnew(Button);465_default->set_text(TTR("Reset to Default"));466_default->set_tooltip_text(TTR("Reset to default OpenXR action map."));467_default->connect(SceneStringName(pressed), callable_mp(this, &OpenXRActionMapEditor::_on_reset_to_default_layout));468top_hb->add_child(_default);469470tabs = memnew(TabContainer);471tabs->set_h_size_flags(SIZE_EXPAND_FILL);472tabs->set_v_size_flags(SIZE_EXPAND_FILL);473tabs->set_theme_type_variation("TabContainerOdd");474tabs->connect("tab_changed", callable_mp(this, &OpenXRActionMapEditor::_on_tabs_tab_changed));475tabs->connect("tab_button_pressed", callable_mp(this, &OpenXRActionMapEditor::_on_tab_button_pressed));476add_child(tabs);477478actionsets_scroll = memnew(ScrollContainer);479actionsets_scroll->set_h_size_flags(SIZE_EXPAND_FILL);480actionsets_scroll->set_v_size_flags(SIZE_EXPAND_FILL);481actionsets_scroll->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);482tabs->add_child(actionsets_scroll);483actionsets_scroll->set_name(TTR("Action Sets"));484485actionsets_vb = memnew(VBoxContainer);486actionsets_vb->set_h_size_flags(SIZE_EXPAND_FILL);487actionsets_scroll->add_child(actionsets_vb);488489select_interaction_profile_dialog = memnew(OpenXRSelectInteractionProfileDialog);490select_interaction_profile_dialog->connect("interaction_profile_selected", callable_mp(this, &OpenXRActionMapEditor::_on_interaction_profile_selected));491add_child(select_interaction_profile_dialog);492493// Our Action map editor is only shown if openxr is enabled in project settings494// So load our action map and if it doesn't exist, create it right away.495_load_action_map(ResourceUID::ensure_path(GLOBAL_GET("xr/openxr/default_action_map")), true);496}497498499