Path: blob/master/modules/openxr/editor/openxr_action_set_editor.cpp
10278 views
/**************************************************************************/1/* openxr_action_set_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_set_editor.h"3132#include "editor/editor_string_names.h"33#include "editor/gui/editor_spin_slider.h"34#include "editor/themes/editor_scale.h"35#include "openxr_action_editor.h"36#include "scene/gui/box_container.h"37#include "scene/gui/button.h"38#include "scene/gui/line_edit.h"39#include "scene/gui/panel_container.h"40#include "scene/gui/text_edit.h"4142void OpenXRActionSetEditor::_bind_methods() {43ClassDB::bind_method(D_METHOD("_do_set_name", "name"), &OpenXRActionSetEditor::_do_set_name);44ClassDB::bind_method(D_METHOD("_do_set_localized_name", "name"), &OpenXRActionSetEditor::_do_set_localized_name);45ClassDB::bind_method(D_METHOD("_do_set_priority", "value"), &OpenXRActionSetEditor::_do_set_priority);46ClassDB::bind_method(D_METHOD("_do_add_action_editor", "action_editor"), &OpenXRActionSetEditor::_do_add_action_editor);47ClassDB::bind_method(D_METHOD("_do_remove_action_editor", "action_editor"), &OpenXRActionSetEditor::_do_remove_action_editor);4849ADD_SIGNAL(MethodInfo("remove", PropertyInfo(Variant::OBJECT, "action_set_editor")));50ADD_SIGNAL(MethodInfo("action_removed", PropertyInfo(Variant::OBJECT, "action")));51}5253void OpenXRActionSetEditor::_set_fold_icon() {54if (is_expanded) {55fold_btn->set_button_icon(get_theme_icon(SNAME("GuiTreeArrowDown"), EditorStringName(EditorIcons)));56} else {57fold_btn->set_button_icon(get_theme_icon(SNAME("GuiTreeArrowRight"), EditorStringName(EditorIcons)));58}59}6061void OpenXRActionSetEditor::_theme_changed() {62_set_fold_icon();63add_action->set_button_icon(get_theme_icon(SNAME("Add"), EditorStringName(EditorIcons)));64rem_action_set->set_button_icon(get_theme_icon(SNAME("Remove"), EditorStringName(EditorIcons)));65}6667void OpenXRActionSetEditor::_notification(int p_what) {68switch (p_what) {69case NOTIFICATION_THEME_CHANGED: {70_theme_changed();71panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("TabContainer")));72} break;73}74}7576OpenXRActionEditor *OpenXRActionSetEditor::_add_action_editor(Ref<OpenXRAction> p_action) {77OpenXRActionEditor *action_editor = memnew(OpenXRActionEditor(p_action));78action_editor->connect("remove", callable_mp(this, &OpenXRActionSetEditor::_on_remove_action));79actions_vb->add_child(action_editor);8081return action_editor;82}8384void OpenXRActionSetEditor::_on_toggle_expand() {85is_expanded = !is_expanded;86actions_vb->set_visible(is_expanded);87_set_fold_icon();88}8990void OpenXRActionSetEditor::_on_action_set_name_changed(const String p_new_text) {91if (action_set->get_name() != p_new_text) {92undo_redo->create_action(TTR("Rename Action Set"));93undo_redo->add_do_method(this, "_do_set_name", p_new_text);94undo_redo->add_undo_method(this, "_do_set_name", action_set->get_name());95undo_redo->commit_action(false);9697// If our localized name matches our action set name, set this too98if (action_set->get_name() == action_set->get_localized_name()) {99undo_redo->create_action(TTR("Rename Action Sets Localized name"));100undo_redo->add_do_method(this, "_do_set_localized_name", p_new_text);101undo_redo->add_undo_method(this, "_do_set_localized_name", action_set->get_localized_name());102undo_redo->commit_action(false);103104action_set->set_localized_name(p_new_text);105action_set_localized_name->set_text(p_new_text);106}107action_set->set_name(p_new_text);108action_set->set_edited(true);109}110}111112void OpenXRActionSetEditor::_do_set_name(const String p_new_text) {113action_set->set_name(p_new_text);114action_set_name->set_text(p_new_text);115}116117void OpenXRActionSetEditor::_on_action_set_localized_name_changed(const String p_new_text) {118if (action_set->get_localized_name() != p_new_text) {119undo_redo->create_action(TTR("Rename Action Sets Localized name"));120undo_redo->add_do_method(this, "_do_set_localized_name", p_new_text);121undo_redo->add_undo_method(this, "_do_set_localized_name", action_set->get_localized_name());122undo_redo->commit_action(false);123124action_set->set_localized_name(p_new_text);125action_set->set_edited(true);126}127}128129void OpenXRActionSetEditor::_do_set_localized_name(const String p_new_text) {130action_set->set_localized_name(p_new_text);131action_set_localized_name->set_text(p_new_text);132}133134void OpenXRActionSetEditor::_on_action_set_priority_changed(const double p_new_value) {135int64_t value = (int64_t)p_new_value;136137if (action_set->get_priority() != value) {138undo_redo->create_action(TTR("Change Action Sets priority"));139undo_redo->add_do_method(this, "_do_set_priority", value);140undo_redo->add_undo_method(this, "_do_set_priority", action_set->get_priority());141undo_redo->commit_action(false);142143action_set->set_priority(value);144action_set->set_edited(true);145}146}147148void OpenXRActionSetEditor::_do_set_priority(int64_t p_value) {149action_set->set_priority(p_value);150action_set_priority->set_value_no_signal(p_value);151}152153void OpenXRActionSetEditor::_on_add_action() {154Ref<OpenXRAction> new_action;155156new_action.instantiate();157new_action->set_name("New");158new_action->set_localized_name("New");159action_set->add_action(new_action);160action_set->set_edited(true);161162OpenXRActionEditor *action_editor = _add_action_editor(new_action);163164undo_redo->create_action(TTR("Add action"));165undo_redo->add_do_method(this, "_do_add_action_editor", action_editor);166undo_redo->add_undo_method(this, "_do_remove_action_editor", action_editor);167undo_redo->commit_action(false);168169// TODO handle focus170}171172void OpenXRActionSetEditor::_on_remove_action_set() {173emit_signal("remove", this);174}175176void OpenXRActionSetEditor::_on_remove_action(Object *p_action_editor) {177OpenXRActionEditor *action_editor = Object::cast_to<OpenXRActionEditor>(p_action_editor);178ERR_FAIL_NULL(action_editor);179ERR_FAIL_COND(action_editor->get_parent() != actions_vb);180Ref<OpenXRAction> action = action_editor->get_action();181ERR_FAIL_COND(action.is_null());182183emit_signal("action_removed", action);184185undo_redo->create_action(TTR("Delete action"));186undo_redo->add_do_method(this, "_do_remove_action_editor", action_editor);187undo_redo->add_undo_method(this, "_do_add_action_editor", action_editor);188undo_redo->commit_action(true);189190action_set->set_edited(true);191}192193void OpenXRActionSetEditor::_do_add_action_editor(OpenXRActionEditor *p_action_editor) {194Ref<OpenXRAction> action = p_action_editor->get_action();195ERR_FAIL_COND(action.is_null());196197action_set->add_action(action);198actions_vb->add_child(p_action_editor);199}200201void OpenXRActionSetEditor::_do_remove_action_editor(OpenXRActionEditor *p_action_editor) {202Ref<OpenXRAction> action = p_action_editor->get_action();203ERR_FAIL_COND(action.is_null());204205actions_vb->remove_child(p_action_editor);206action_set->remove_action(action);207}208209void OpenXRActionSetEditor::remove_all_actions() {210for (int i = actions_vb->get_child_count(); i > 0; --i) {211_on_remove_action(actions_vb->get_child(i));212}213}214215void OpenXRActionSetEditor::set_focus_on_entry() {216ERR_FAIL_NULL(action_set_name);217action_set_name->grab_focus();218}219220OpenXRActionSetEditor::OpenXRActionSetEditor(Ref<OpenXRActionMap> p_action_map, Ref<OpenXRActionSet> p_action_set) {221undo_redo = EditorUndoRedoManager::get_singleton();222action_map = p_action_map;223action_set = p_action_set;224225set_h_size_flags(Control::SIZE_EXPAND_FILL);226227panel = memnew(PanelContainer);228panel->set_h_size_flags(Control::SIZE_EXPAND_FILL);229add_child(panel);230231HBoxContainer *panel_hb = memnew(HBoxContainer);232panel_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);233panel->add_child(panel_hb);234235fold_btn = memnew(Button);236fold_btn->set_v_size_flags(Control::SIZE_SHRINK_BEGIN);237fold_btn->connect(SceneStringName(pressed), callable_mp(this, &OpenXRActionSetEditor::_on_toggle_expand));238fold_btn->set_accessibility_name(TTRC("Fold"));239fold_btn->set_flat(true);240panel_hb->add_child(fold_btn);241242main_vb = memnew(VBoxContainer);243main_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);244panel_hb->add_child(main_vb);245246action_set_hb = memnew(HBoxContainer);247action_set_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);248main_vb->add_child(action_set_hb);249250action_set_name = memnew(LineEdit);251action_set_name->set_text(action_set->get_name());252action_set_name->set_tooltip_text(TTR("Internal name of the action. Some XR runtimes don't allow spaces or special characters."));253action_set_name->set_custom_minimum_size(Size2(150.0 * EDSCALE, 0.0));254action_set_name->connect(SceneStringName(text_changed), callable_mp(this, &OpenXRActionSetEditor::_on_action_set_name_changed));255action_set_name->set_accessibility_name(TTRC("Action Set Name"));256action_set_hb->add_child(action_set_name);257258action_set_localized_name = memnew(LineEdit);259action_set_localized_name->set_text(action_set->get_localized_name());260action_set_localized_name->set_tooltip_text(TTR("Human-readable name of the action set. This can be displayed to end users."));261action_set_localized_name->set_custom_minimum_size(Size2(150.0 * EDSCALE, 0.0));262action_set_localized_name->set_h_size_flags(Control::SIZE_EXPAND_FILL);263action_set_localized_name->connect(SceneStringName(text_changed), callable_mp(this, &OpenXRActionSetEditor::_on_action_set_localized_name_changed));264action_set_localized_name->set_accessibility_name(TTRC("Action Set Localized Name"));265action_set_hb->add_child(action_set_localized_name);266267action_set_priority = memnew(EditorSpinSlider);268action_set_priority->set_tooltip_text(TTR("Priority of the action set. If multiple action sets bind to the same input, the action set with the highest priority will be updated."));269action_set_priority->set_editing_integer(true);270action_set_priority->set_min(2147483647.0);271action_set_priority->set_min(-2147483648.0);272action_set_priority->set_value_no_signal(action_set->get_priority());273action_set_priority->set_custom_minimum_size(Size2(75.0 * EDSCALE, 0.0));274action_set_priority->connect(SceneStringName(value_changed), callable_mp(this, &OpenXRActionSetEditor::_on_action_set_priority_changed));275action_set_priority->set_accessibility_name(TTRC("Action Set Priority"));276action_set_hb->add_child(action_set_priority);277278add_action = memnew(Button);279add_action->set_tooltip_text(TTR("Add action."));280add_action->connect(SceneStringName(pressed), callable_mp(this, &OpenXRActionSetEditor::_on_add_action));281add_action->set_flat(true);282action_set_hb->add_child(add_action);283284rem_action_set = memnew(Button);285rem_action_set->set_tooltip_text(TTR("Remove action set."));286rem_action_set->connect(SceneStringName(pressed), callable_mp(this, &OpenXRActionSetEditor::_on_remove_action_set));287rem_action_set->set_flat(true);288action_set_hb->add_child(rem_action_set);289290actions_vb = memnew(VBoxContainer);291actions_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);292main_vb->add_child(actions_vb);293294// Add our existing actions295Array actions = action_set->get_actions();296for (int i = 0; i < actions.size(); i++) {297Ref<OpenXRAction> action = actions[i];298_add_action_editor(action);299}300}301302303