Path: blob/master/modules/openxr/editor/openxr_binding_modifier_editor.cpp
10278 views
/**************************************************************************/1/* openxr_binding_modifier_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_binding_modifier_editor.h"3132#include "editor/editor_string_names.h"33#include "scene/gui/option_button.h"3435///////////////////////////////////////////////////////////////////////////////////////////////////36// EditorPropertyActionSet3738void EditorPropertyActionSet::_set_read_only(bool p_read_only) {39options->set_disabled(p_read_only);40}4142void EditorPropertyActionSet::_option_selected(int p_which) {43Ref<OpenXRActionSet> val = options->get_item_metadata(p_which);44emit_changed(get_edited_property(), val);45}4647void EditorPropertyActionSet::update_property() {48Variant current = get_edited_property_value();49if (current.get_type() == Variant::NIL) {50options->select(-1);51return;52}5354Ref<OpenXRActionSet> which = current;55for (int i = 0; i < options->get_item_count(); i++) {56if (which == (Ref<OpenXRActionSet>)options->get_item_metadata(i)) {57options->select(i);58return;59}60}6162// Couldn't find it? deselect..63options->select(-1);64}6566void EditorPropertyActionSet::setup(const Ref<OpenXRActionMap> &p_action_map) {67options->clear();6869Array action_sets = p_action_map->get_action_sets();70for (Ref<OpenXRActionSet> action_set : action_sets) {71options->add_item(action_set->get_localized_name());72options->set_item_metadata(-1, action_set);73}74}7576void EditorPropertyActionSet::set_option_button_clip(bool p_enable) {77options->set_clip_text(p_enable);78}7980EditorPropertyActionSet::EditorPropertyActionSet() {81options = memnew(OptionButton);82options->set_clip_text(true);83options->set_flat(true);84options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);85add_child(options);86add_focusable(options);87options->connect(SceneStringName(item_selected), callable_mp(this, &EditorPropertyActionSet::_option_selected));88}8990///////////////////////////////////////////////////////////////////////////////////////////////////91// EditorPropertyBindingPath9293void EditorPropertyBindingPath::_set_read_only(bool p_read_only) {94options->set_disabled(p_read_only);95}9697void EditorPropertyBindingPath::_option_selected(int p_which) {98String val = options->get_item_metadata(p_which);99emit_changed(get_edited_property(), val);100}101102void EditorPropertyBindingPath::update_property() {103Variant current = get_edited_property_value();104if (current.get_type() == Variant::NIL) {105options->select(-1);106return;107}108109String which = current;110for (int i = 0; i < options->get_item_count(); i++) {111if (which == (String)options->get_item_metadata(i)) {112options->select(i);113return;114}115}116117// Couldn't find it? deselect..118options->select(-1);119}120121void EditorPropertyBindingPath::setup(const String &p_interaction_profile_path, Vector<OpenXRAction::ActionType> p_include_action_types) {122options->clear();123124const OpenXRInteractionProfileMetadata::InteractionProfile *profile_def = OpenXRInteractionProfileMetadata::get_singleton()->get_profile(p_interaction_profile_path);125126// Determine toplevel paths127Vector<String> top_level_paths;128for (const OpenXRInteractionProfileMetadata::IOPath &io_path : profile_def->io_paths) {129if (!top_level_paths.has(io_path.top_level_path)) {130top_level_paths.push_back(io_path.top_level_path);131}132}133134for (const String &top_level_path : top_level_paths) {135String top_level_name = OpenXRInteractionProfileMetadata::get_singleton()->get_top_level_name(top_level_path);136137for (const OpenXRInteractionProfileMetadata::IOPath &io_path : profile_def->io_paths) {138if (io_path.top_level_path == top_level_path && p_include_action_types.has(io_path.action_type)) {139options->add_item(top_level_name + "/" + io_path.display_name);140options->set_item_metadata(-1, io_path.openxr_path);141}142}143}144}145146void EditorPropertyBindingPath::set_option_button_clip(bool p_enable) {147options->set_clip_text(p_enable);148}149150EditorPropertyBindingPath::EditorPropertyBindingPath() {151options = memnew(OptionButton);152options->set_clip_text(true);153options->set_flat(true);154options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);155add_child(options);156add_focusable(options);157options->connect(SceneStringName(item_selected), callable_mp(this, &EditorPropertyBindingPath::_option_selected));158}159160///////////////////////////////////////////////////////////////////////////////////////////////////161// OpenXRBindingModifierEditor162163bool EditorInspectorPluginBindingModifier::can_handle(Object *p_object) {164Ref<OpenXRBindingModifier> binding_modifier(Object::cast_to<OpenXRBindingModifier>(p_object));165return binding_modifier.is_valid();166}167168bool EditorInspectorPluginBindingModifier::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {169Ref<OpenXRActionBindingModifier> action_binding_modifier(Object::cast_to<OpenXRActionBindingModifier>(p_object));170if (action_binding_modifier.is_valid()) {171if (p_type == Variant::OBJECT && p_hint == PROPERTY_HINT_RESOURCE_TYPE && p_hint_text == OpenXRActionSet::get_class_static()) {172OpenXRIPBinding *ip_binding = action_binding_modifier->get_ip_binding();173ERR_FAIL_NULL_V(ip_binding, false);174175OpenXRActionMap *action_map = ip_binding->get_action_map();176ERR_FAIL_NULL_V(action_map, false);177178EditorPropertyActionSet *action_set_property = memnew(EditorPropertyActionSet);179action_set_property->setup(action_map);180add_property_editor(p_path, action_set_property);181return true;182}183184return false;185}186187Ref<OpenXRIPBindingModifier> ip_binding_modifier(Object::cast_to<OpenXRIPBindingModifier>(p_object));188if (ip_binding_modifier.is_valid()) {189if (p_type == Variant::OBJECT && p_hint == PROPERTY_HINT_RESOURCE_TYPE && p_hint_text == OpenXRActionSet::get_class_static()) {190OpenXRInteractionProfile *interaction_profile = ip_binding_modifier->get_interaction_profile();191ERR_FAIL_NULL_V(interaction_profile, false);192193OpenXRActionMap *action_map = interaction_profile->get_action_map();194ERR_FAIL_NULL_V(action_map, false);195196EditorPropertyActionSet *action_set_property = memnew(EditorPropertyActionSet);197action_set_property->setup(action_map);198add_property_editor(p_path, action_set_property);199return true;200}201202if (p_type == Variant::STRING && p_hint == PROPERTY_HINT_TYPE_STRING && p_hint_text == "binding_path") {203EditorPropertyBindingPath *binding_path_property = memnew(EditorPropertyBindingPath);204205OpenXRInteractionProfile *interaction_profile = ip_binding_modifier->get_interaction_profile();206ERR_FAIL_NULL_V(interaction_profile, false);207208Vector<OpenXRAction::ActionType> action_types;209action_types.push_back(OpenXRAction::OPENXR_ACTION_VECTOR2);210binding_path_property->setup(interaction_profile->get_interaction_profile_path(), action_types);211212add_property_editor(p_path, binding_path_property);213return true;214}215216return false;217}218219return false;220}221222///////////////////////////////////////////////////////////////////////////////////////////////////223// OpenXRBindingModifierEditor224225void OpenXRBindingModifierEditor::_bind_methods() {226ClassDB::bind_method(D_METHOD("get_binding_modifier"), &OpenXRBindingModifierEditor::get_binding_modifier);227ClassDB::bind_method(D_METHOD("setup", "action_map", "binding_modifier"), &OpenXRBindingModifierEditor::setup);228229ADD_SIGNAL(MethodInfo("binding_modifier_removed", PropertyInfo(Variant::OBJECT, "binding_modifier_editor")));230}231232void OpenXRBindingModifierEditor::_notification(int p_what) {233switch (p_what) {234case NOTIFICATION_THEME_CHANGED: {235rem_binding_modifier_btn->set_button_icon(get_theme_icon(SNAME("Remove"), EditorStringName(EditorIcons)));236} break;237}238}239240void OpenXRBindingModifierEditor::_on_remove_binding_modifier() {241// Tell parent to remove us242emit_signal("binding_modifier_removed", this);243}244245OpenXRBindingModifierEditor::OpenXRBindingModifierEditor() {246undo_redo = EditorUndoRedoManager::get_singleton();247248set_h_size_flags(Control::SIZE_EXPAND_FILL);249250main_vb = memnew(VBoxContainer);251main_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);252add_child(main_vb);253254header_hb = memnew(HBoxContainer);255header_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);256main_vb->add_child(header_hb);257258binding_modifier_title = memnew(Label);259binding_modifier_title->set_h_size_flags(Control::SIZE_EXPAND_FILL);260header_hb->add_child(binding_modifier_title);261262rem_binding_modifier_btn = memnew(Button);263rem_binding_modifier_btn->set_tooltip_text(TTR("Remove this binding modifier."));264rem_binding_modifier_btn->connect(SceneStringName(pressed), callable_mp(this, &OpenXRBindingModifierEditor::_on_remove_binding_modifier));265rem_binding_modifier_btn->set_flat(true);266header_hb->add_child(rem_binding_modifier_btn);267268editor_inspector = memnew(EditorInspector);269editor_inspector->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);270editor_inspector->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);271editor_inspector->set_h_size_flags(Control::SIZE_EXPAND_FILL);272main_vb->add_child(editor_inspector);273}274275void OpenXRBindingModifierEditor::setup(Ref<OpenXRActionMap> p_action_map, Ref<OpenXRBindingModifier> p_binding_modifier) {276ERR_FAIL_NULL(binding_modifier_title);277ERR_FAIL_NULL(editor_inspector);278279action_map = p_action_map;280binding_modifier = p_binding_modifier;281282if (p_binding_modifier.is_valid()) {283binding_modifier_title->set_text(p_binding_modifier->get_description());284285editor_inspector->set_object_class(p_binding_modifier->get_class());286editor_inspector->edit(p_binding_modifier.ptr());287}288}289290291