Path: blob/master/modules/openxr/action_map/openxr_interaction_profile.cpp
10278 views
/**************************************************************************/1/* openxr_interaction_profile.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.h"3132void OpenXRIPBinding::_bind_methods() {33ClassDB::bind_method(D_METHOD("set_action", "action"), &OpenXRIPBinding::set_action);34ClassDB::bind_method(D_METHOD("get_action"), &OpenXRIPBinding::get_action);35ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "action", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRAction"), "set_action", "get_action");3637ClassDB::bind_method(D_METHOD("set_binding_path", "binding_path"), &OpenXRIPBinding::set_binding_path);38ClassDB::bind_method(D_METHOD("get_binding_path"), &OpenXRIPBinding::get_binding_path);39ADD_PROPERTY(PropertyInfo(Variant::STRING, "binding_path"), "set_binding_path", "get_binding_path");4041ClassDB::bind_method(D_METHOD("get_binding_modifier_count"), &OpenXRIPBinding::get_binding_modifier_count);42ClassDB::bind_method(D_METHOD("get_binding_modifier", "index"), &OpenXRIPBinding::get_binding_modifier);43ClassDB::bind_method(D_METHOD("set_binding_modifiers", "binding_modifiers"), &OpenXRIPBinding::set_binding_modifiers);44ClassDB::bind_method(D_METHOD("get_binding_modifiers"), &OpenXRIPBinding::get_binding_modifiers);45ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "binding_modifiers", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRActionBindingModifier", PROPERTY_USAGE_NO_EDITOR), "set_binding_modifiers", "get_binding_modifiers");4647// Deprecated48#ifndef DISABLE_DEPRECATED49ClassDB::bind_method(D_METHOD("set_paths", "paths"), &OpenXRIPBinding::set_paths);50ClassDB::bind_method(D_METHOD("get_paths"), &OpenXRIPBinding::get_paths);51ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "paths", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_paths", "get_paths");5253ClassDB::bind_method(D_METHOD("get_path_count"), &OpenXRIPBinding::get_path_count);54ClassDB::bind_method(D_METHOD("has_path", "path"), &OpenXRIPBinding::has_path);55ClassDB::bind_method(D_METHOD("add_path", "path"), &OpenXRIPBinding::add_path);56ClassDB::bind_method(D_METHOD("remove_path", "path"), &OpenXRIPBinding::remove_path);57#endif // DISABLE_DEPRECATED58}5960Ref<OpenXRIPBinding> OpenXRIPBinding::new_binding(const Ref<OpenXRAction> p_action, const String &p_binding_path) {61// This is a helper function to help build our default action sets6263Ref<OpenXRIPBinding> binding;64binding.instantiate();65binding->set_action(p_action);66binding->set_binding_path(p_binding_path);6768return binding;69}7071void OpenXRIPBinding::set_action(const Ref<OpenXRAction> &p_action) {72action = p_action;73emit_changed();74}7576Ref<OpenXRAction> OpenXRIPBinding::get_action() const {77return action;78}7980void OpenXRIPBinding::set_binding_path(const String &path) {81binding_path = path;82emit_changed();83}8485String OpenXRIPBinding::get_binding_path() const {86return binding_path;87}8889int OpenXRIPBinding::get_binding_modifier_count() const {90return binding_modifiers.size();91}9293Ref<OpenXRActionBindingModifier> OpenXRIPBinding::get_binding_modifier(int p_index) const {94ERR_FAIL_INDEX_V(p_index, binding_modifiers.size(), nullptr);9596return binding_modifiers[p_index];97}9899void OpenXRIPBinding::clear_binding_modifiers() {100// Binding modifiers held within our interaction profile set should be released and destroyed but just in case they are still used some where else.101if (binding_modifiers.is_empty()) {102return;103}104105for (int i = 0; i < binding_modifiers.size(); i++) {106Ref<OpenXRActionBindingModifier> binding_modifier = binding_modifiers[i];107binding_modifier->ip_binding = nullptr;108}109binding_modifiers.clear();110emit_changed();111}112113void OpenXRIPBinding::set_binding_modifiers(const Array &p_binding_modifiers) {114clear_binding_modifiers();115116// Any binding modifier not retained in p_binding_modifiers should be freed automatically, those held within our Array will have be relinked to our interaction profile.117for (int i = 0; i < p_binding_modifiers.size(); i++) {118// Add them anew so we verify our binding modifier pointer.119add_binding_modifier(p_binding_modifiers[i]);120}121}122123Array OpenXRIPBinding::get_binding_modifiers() const {124Array ret;125for (const Ref<OpenXRActionBindingModifier> &binding_modifier : binding_modifiers) {126ret.push_back(binding_modifier);127}128return ret;129}130131void OpenXRIPBinding::add_binding_modifier(const Ref<OpenXRActionBindingModifier> &p_binding_modifier) {132ERR_FAIL_COND(p_binding_modifier.is_null());133134if (!binding_modifiers.has(p_binding_modifier)) {135if (p_binding_modifier->ip_binding && p_binding_modifier->ip_binding != this) {136// Binding modifier should only relate to our binding.137p_binding_modifier->ip_binding->remove_binding_modifier(p_binding_modifier);138}139140p_binding_modifier->ip_binding = this;141binding_modifiers.push_back(p_binding_modifier);142emit_changed();143}144}145146void OpenXRIPBinding::remove_binding_modifier(const Ref<OpenXRActionBindingModifier> &p_binding_modifier) {147int idx = binding_modifiers.find(p_binding_modifier);148if (idx != -1) {149binding_modifiers.remove_at(idx);150151ERR_FAIL_COND_MSG(p_binding_modifier->ip_binding != this, "Removing binding modifier that belongs to this binding but had incorrect binding pointer."); // This should never happen!152p_binding_modifier->ip_binding = nullptr;153154emit_changed();155}156}157158#ifndef DISABLE_DEPRECATED159160void OpenXRIPBinding::set_paths(const PackedStringArray p_paths) { // Deprecated, but needed for loading old action maps.161// Fallback logic, this should ONLY be called when loading older action maps.162// We'll parse this momentarily and extract individual bindings.163binding_path = "";164for (const String &path : p_paths) {165if (!binding_path.is_empty()) {166binding_path += ",";167}168binding_path += path;169}170}171172PackedStringArray OpenXRIPBinding::get_paths() const { // Deprecated, but needed for converting old action maps.173// Fallback logic, return an array.174// If we just loaded an old action map from disc, this will be a comma separated list of actions.175// Once parsed there should be only one path in our array.176PackedStringArray paths = binding_path.split(",", false);177178return paths;179}180181int OpenXRIPBinding::get_path_count() const { // Deprecated.182// Fallback logic, we only have one entry.183return binding_path.is_empty() ? 0 : 1;184}185186bool OpenXRIPBinding::has_path(const String p_path) const { // Deprecated.187// Fallback logic, return true if this is our path.188return binding_path == p_path;189}190191void OpenXRIPBinding::add_path(const String p_path) { // Deprecated.192// Fallback logic, only assign first time this is called.193if (binding_path != p_path) {194ERR_FAIL_COND_MSG(!binding_path.is_empty(), "Method add_path has been deprecated. A binding path was already set, create separate binding resources for each path and use set_binding_path instead.");195196binding_path = p_path;197emit_changed();198}199}200201void OpenXRIPBinding::remove_path(const String p_path) { // Deprecated.202ERR_FAIL_COND_MSG(binding_path != p_path, "Method remove_path has been deprecated. Attempt at removing a different binding path, remove the correct binding record from the interaction profile instead.");203204// Fallback logic, clear if this is our path.205binding_path = p_path;206emit_changed();207}208209#endif // DISABLE_DEPRECATED210211OpenXRIPBinding::~OpenXRIPBinding() {212action.unref();213}214215void OpenXRInteractionProfile::_bind_methods() {216ClassDB::bind_method(D_METHOD("set_interaction_profile_path", "interaction_profile_path"), &OpenXRInteractionProfile::set_interaction_profile_path);217ClassDB::bind_method(D_METHOD("get_interaction_profile_path"), &OpenXRInteractionProfile::get_interaction_profile_path);218ADD_PROPERTY(PropertyInfo(Variant::STRING, "interaction_profile_path"), "set_interaction_profile_path", "get_interaction_profile_path");219220ClassDB::bind_method(D_METHOD("get_binding_count"), &OpenXRInteractionProfile::get_binding_count);221ClassDB::bind_method(D_METHOD("get_binding", "index"), &OpenXRInteractionProfile::get_binding);222ClassDB::bind_method(D_METHOD("set_bindings", "bindings"), &OpenXRInteractionProfile::set_bindings);223ClassDB::bind_method(D_METHOD("get_bindings"), &OpenXRInteractionProfile::get_bindings);224ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "bindings", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRIPBinding", PROPERTY_USAGE_NO_EDITOR), "set_bindings", "get_bindings");225226ClassDB::bind_method(D_METHOD("get_binding_modifier_count"), &OpenXRInteractionProfile::get_binding_modifier_count);227ClassDB::bind_method(D_METHOD("get_binding_modifier", "index"), &OpenXRInteractionProfile::get_binding_modifier);228ClassDB::bind_method(D_METHOD("set_binding_modifiers", "binding_modifiers"), &OpenXRInteractionProfile::set_binding_modifiers);229ClassDB::bind_method(D_METHOD("get_binding_modifiers"), &OpenXRInteractionProfile::get_binding_modifiers);230ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "binding_modifiers", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRIPBindingModifier", PROPERTY_USAGE_NO_EDITOR), "set_binding_modifiers", "get_binding_modifiers");231}232233Ref<OpenXRInteractionProfile> OpenXRInteractionProfile::new_profile(const char *p_input_profile_path) {234Ref<OpenXRInteractionProfile> profile;235profile.instantiate();236profile->set_interaction_profile_path(String(p_input_profile_path));237238return profile;239}240241void OpenXRInteractionProfile::set_interaction_profile_path(const String p_input_profile_path) {242OpenXRInteractionProfileMetadata *pmd = OpenXRInteractionProfileMetadata::get_singleton();243if (pmd) {244interaction_profile_path = pmd->check_profile_name(p_input_profile_path);245} else {246// OpenXR module not enabled, ignore checks.247interaction_profile_path = p_input_profile_path;248}249emit_changed();250}251252String OpenXRInteractionProfile::get_interaction_profile_path() const {253return interaction_profile_path;254}255256int OpenXRInteractionProfile::get_binding_count() const {257return bindings.size();258}259260Ref<OpenXRIPBinding> OpenXRInteractionProfile::get_binding(int p_index) const {261ERR_FAIL_INDEX_V(p_index, bindings.size(), Ref<OpenXRIPBinding>());262263return bindings[p_index];264}265266void OpenXRInteractionProfile::set_bindings(const Array &p_bindings) {267bindings.clear();268269for (Ref<OpenXRIPBinding> binding : p_bindings) {270String binding_path = binding->get_binding_path();271if (binding_path.find_char(',') >= 0) {272// Convert old binding approach to new...273add_new_binding(binding->get_action(), binding_path);274} else {275add_binding(binding);276}277}278279emit_changed();280}281282Array OpenXRInteractionProfile::get_bindings() const {283return bindings;284}285286Ref<OpenXRIPBinding> OpenXRInteractionProfile::find_binding(const Ref<OpenXRAction> &p_action, const String &p_binding_path) const {287for (Ref<OpenXRIPBinding> binding : bindings) {288if (binding->get_action() == p_action && binding->get_binding_path() == p_binding_path) {289return binding;290}291}292293return Ref<OpenXRIPBinding>();294}295296Vector<Ref<OpenXRIPBinding>> OpenXRInteractionProfile::get_bindings_for_action(const Ref<OpenXRAction> &p_action) const {297Vector<Ref<OpenXRIPBinding>> ret_bindings;298299for (Ref<OpenXRIPBinding> binding : bindings) {300if (binding->get_action() == p_action) {301ret_bindings.push_back(binding);302}303}304305return ret_bindings;306}307308void OpenXRInteractionProfile::add_binding(const Ref<OpenXRIPBinding> &p_binding) {309ERR_FAIL_COND(p_binding.is_null());310311if (!bindings.has(p_binding)) {312ERR_FAIL_COND_MSG(find_binding(p_binding->get_action(), p_binding->get_binding_path()).is_valid(), "There is already a binding for this action and binding path in this interaction profile.");313314bindings.push_back(p_binding);315emit_changed();316}317}318319void OpenXRInteractionProfile::remove_binding(const Ref<OpenXRIPBinding> &p_binding) {320int idx = bindings.find(p_binding);321if (idx != -1) {322bindings.remove_at(idx);323emit_changed();324}325}326327void OpenXRInteractionProfile::add_new_binding(const Ref<OpenXRAction> &p_action, const String &p_paths) {328// This is a helper function to help build our default action sets329330PackedStringArray paths = p_paths.split(",", false);331332for (const String &path : paths) {333Ref<OpenXRIPBinding> binding = OpenXRIPBinding::new_binding(p_action, path);334add_binding(binding);335}336}337338void OpenXRInteractionProfile::remove_binding_for_action(const Ref<OpenXRAction> &p_action) {339for (int i = bindings.size() - 1; i >= 0; i--) {340Ref<OpenXRIPBinding> binding = bindings[i];341if (binding->get_action() == p_action) {342remove_binding(binding);343}344}345}346347bool OpenXRInteractionProfile::has_binding_for_action(const Ref<OpenXRAction> &p_action) {348for (int i = bindings.size() - 1; i >= 0; i--) {349Ref<OpenXRIPBinding> binding = bindings[i];350if (binding->get_action() == p_action) {351return true;352}353}354355return false;356}357358int OpenXRInteractionProfile::get_binding_modifier_count() const {359return binding_modifiers.size();360}361362Ref<OpenXRIPBindingModifier> OpenXRInteractionProfile::get_binding_modifier(int p_index) const {363ERR_FAIL_INDEX_V(p_index, binding_modifiers.size(), nullptr);364365return binding_modifiers[p_index];366}367368void OpenXRInteractionProfile::clear_binding_modifiers() {369// Binding modifiers held within our interaction profile set should be released and destroyed but just in case they are still used some where else.370if (binding_modifiers.is_empty()) {371return;372}373374for (int i = 0; i < binding_modifiers.size(); i++) {375Ref<OpenXRIPBindingModifier> binding_modifier = binding_modifiers[i];376binding_modifier->interaction_profile = nullptr;377}378binding_modifiers.clear();379emit_changed();380}381382void OpenXRInteractionProfile::set_binding_modifiers(const Array &p_binding_modifiers) {383clear_binding_modifiers();384385// Any binding modifier not retained in p_binding_modifiers should be freed automatically, those held within our Array will have be relinked to our interaction profile.386for (int i = 0; i < p_binding_modifiers.size(); i++) {387// Add them anew so we verify our binding modifier pointer.388add_binding_modifier(p_binding_modifiers[i]);389}390}391392Array OpenXRInteractionProfile::get_binding_modifiers() const {393Array ret;394for (const Ref<OpenXRIPBindingModifier> &binding_modifier : binding_modifiers) {395ret.push_back(binding_modifier);396}397return ret;398}399400void OpenXRInteractionProfile::add_binding_modifier(const Ref<OpenXRIPBindingModifier> &p_binding_modifier) {401ERR_FAIL_COND(p_binding_modifier.is_null());402403if (!binding_modifiers.has(p_binding_modifier)) {404if (p_binding_modifier->interaction_profile && p_binding_modifier->interaction_profile != this) {405// Binding modifier should only relate to our interaction profile.406p_binding_modifier->interaction_profile->remove_binding_modifier(p_binding_modifier);407}408409p_binding_modifier->interaction_profile = this;410binding_modifiers.push_back(p_binding_modifier);411emit_changed();412}413}414415void OpenXRInteractionProfile::remove_binding_modifier(const Ref<OpenXRIPBindingModifier> &p_binding_modifier) {416int idx = binding_modifiers.find(p_binding_modifier);417if (idx != -1) {418binding_modifiers.remove_at(idx);419420ERR_FAIL_COND_MSG(p_binding_modifier->interaction_profile != this, "Removing binding modifier that belongs to this interaction profile but had incorrect interaction profile pointer."); // This should never happen!421p_binding_modifier->interaction_profile = nullptr;422423emit_changed();424}425}426427OpenXRInteractionProfile::~OpenXRInteractionProfile() {428bindings.clear();429clear_binding_modifiers();430}431432433