Path: blob/master/modules/openxr/action_map/openxr_interaction_profile.cpp
20969 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::get_class_static()), "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::get_class_static(), 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 &p_path) {81OpenXRInteractionProfileMetadata *pmd = OpenXRInteractionProfileMetadata::get_singleton();82if (pmd) {83binding_path = pmd->check_path_name(p_path);84} else {85// OpenXR not enabled, ignore checks.86binding_path = p_path;87}8889emit_changed();90}9192String OpenXRIPBinding::get_binding_path() const {93return binding_path;94}9596int OpenXRIPBinding::get_binding_modifier_count() const {97return binding_modifiers.size();98}99100Ref<OpenXRActionBindingModifier> OpenXRIPBinding::get_binding_modifier(int p_index) const {101ERR_FAIL_INDEX_V(p_index, binding_modifiers.size(), nullptr);102103return binding_modifiers[p_index];104}105106void OpenXRIPBinding::clear_binding_modifiers() {107// Binding modifiers held within our interaction profile set should be released and destroyed but just in case they are still used some where else.108if (binding_modifiers.is_empty()) {109return;110}111112for (int i = 0; i < binding_modifiers.size(); i++) {113Ref<OpenXRActionBindingModifier> binding_modifier = binding_modifiers[i];114binding_modifier->ip_binding = nullptr;115}116binding_modifiers.clear();117emit_changed();118}119120void OpenXRIPBinding::set_binding_modifiers(const Array &p_binding_modifiers) {121clear_binding_modifiers();122123// 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.124for (int i = 0; i < p_binding_modifiers.size(); i++) {125// Add them anew so we verify our binding modifier pointer.126add_binding_modifier(p_binding_modifiers[i]);127}128}129130Array OpenXRIPBinding::get_binding_modifiers() const {131Array ret;132for (const Ref<OpenXRActionBindingModifier> &binding_modifier : binding_modifiers) {133ret.push_back(binding_modifier);134}135return ret;136}137138void OpenXRIPBinding::add_binding_modifier(const Ref<OpenXRActionBindingModifier> &p_binding_modifier) {139ERR_FAIL_COND(p_binding_modifier.is_null());140141if (!binding_modifiers.has(p_binding_modifier)) {142if (p_binding_modifier->ip_binding && p_binding_modifier->ip_binding != this) {143// Binding modifier should only relate to our binding.144p_binding_modifier->ip_binding->remove_binding_modifier(p_binding_modifier);145}146147p_binding_modifier->ip_binding = this;148binding_modifiers.push_back(p_binding_modifier);149emit_changed();150}151}152153void OpenXRIPBinding::remove_binding_modifier(const Ref<OpenXRActionBindingModifier> &p_binding_modifier) {154int idx = binding_modifiers.find(p_binding_modifier);155if (idx != -1) {156binding_modifiers.remove_at(idx);157158ERR_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!159p_binding_modifier->ip_binding = nullptr;160161emit_changed();162}163}164165#ifndef DISABLE_DEPRECATED166167void OpenXRIPBinding::set_paths(const PackedStringArray &p_paths) { // Deprecated, but needed for loading old action maps.168// Fallback logic, this should ONLY be called when loading older action maps.169// We'll parse this momentarily and extract individual bindings.170binding_path = "";171for (const String &path : p_paths) {172if (!binding_path.is_empty()) {173binding_path += ",";174}175binding_path += path;176}177}178179PackedStringArray OpenXRIPBinding::get_paths() const { // Deprecated, but needed for converting old action maps.180// Fallback logic, return an array.181// If we just loaded an old action map from disc, this will be a comma separated list of actions.182// Once parsed there should be only one path in our array.183PackedStringArray paths = binding_path.split(",", false);184185return paths;186}187188int OpenXRIPBinding::get_path_count() const { // Deprecated.189// Fallback logic, we only have one entry.190return binding_path.is_empty() ? 0 : 1;191}192193bool OpenXRIPBinding::has_path(const String &p_path) const { // Deprecated.194// Fallback logic, return true if this is our path.195return binding_path == p_path;196}197198void OpenXRIPBinding::add_path(const String &p_path) { // Deprecated.199// Fallback logic, only assign first time this is called.200if (binding_path != p_path) {201ERR_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.");202203binding_path = p_path;204emit_changed();205}206}207208void OpenXRIPBinding::remove_path(const String &p_path) { // Deprecated.209ERR_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.");210211// Fallback logic, clear if this is our path.212binding_path = p_path;213emit_changed();214}215216#endif // DISABLE_DEPRECATED217218OpenXRIPBinding::~OpenXRIPBinding() {219action.unref();220}221222void OpenXRInteractionProfile::_bind_methods() {223ClassDB::bind_method(D_METHOD("set_interaction_profile_path", "interaction_profile_path"), &OpenXRInteractionProfile::set_interaction_profile_path);224ClassDB::bind_method(D_METHOD("get_interaction_profile_path"), &OpenXRInteractionProfile::get_interaction_profile_path);225ADD_PROPERTY(PropertyInfo(Variant::STRING, "interaction_profile_path"), "set_interaction_profile_path", "get_interaction_profile_path");226227ClassDB::bind_method(D_METHOD("get_binding_count"), &OpenXRInteractionProfile::get_binding_count);228ClassDB::bind_method(D_METHOD("get_binding", "index"), &OpenXRInteractionProfile::get_binding);229ClassDB::bind_method(D_METHOD("set_bindings", "bindings"), &OpenXRInteractionProfile::set_bindings);230ClassDB::bind_method(D_METHOD("get_bindings"), &OpenXRInteractionProfile::get_bindings);231ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "bindings", PROPERTY_HINT_RESOURCE_TYPE, OpenXRIPBinding::get_class_static(), PROPERTY_USAGE_NO_EDITOR), "set_bindings", "get_bindings");232233ClassDB::bind_method(D_METHOD("get_binding_modifier_count"), &OpenXRInteractionProfile::get_binding_modifier_count);234ClassDB::bind_method(D_METHOD("get_binding_modifier", "index"), &OpenXRInteractionProfile::get_binding_modifier);235ClassDB::bind_method(D_METHOD("set_binding_modifiers", "binding_modifiers"), &OpenXRInteractionProfile::set_binding_modifiers);236ClassDB::bind_method(D_METHOD("get_binding_modifiers"), &OpenXRInteractionProfile::get_binding_modifiers);237ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "binding_modifiers", PROPERTY_HINT_RESOURCE_TYPE, OpenXRIPBindingModifier::get_class_static(), PROPERTY_USAGE_NO_EDITOR), "set_binding_modifiers", "get_binding_modifiers");238}239240Ref<OpenXRInteractionProfile> OpenXRInteractionProfile::new_profile(const char *p_input_profile_path) {241Ref<OpenXRInteractionProfile> profile;242profile.instantiate();243profile->set_interaction_profile_path(String(p_input_profile_path));244245return profile;246}247248void OpenXRInteractionProfile::set_interaction_profile_path(const String &p_input_profile_path) {249OpenXRInteractionProfileMetadata *pmd = OpenXRInteractionProfileMetadata::get_singleton();250if (pmd) {251interaction_profile_path = pmd->check_profile_name(p_input_profile_path);252} else {253// OpenXR not enabled, ignore checks.254interaction_profile_path = p_input_profile_path;255}256emit_changed();257}258259String OpenXRInteractionProfile::get_interaction_profile_path() const {260return interaction_profile_path;261}262263int OpenXRInteractionProfile::get_binding_count() const {264return bindings.size();265}266267Ref<OpenXRIPBinding> OpenXRInteractionProfile::get_binding(int p_index) const {268ERR_FAIL_INDEX_V(p_index, bindings.size(), Ref<OpenXRIPBinding>());269270return bindings[p_index];271}272273void OpenXRInteractionProfile::set_bindings(const Array &p_bindings) {274bindings.clear();275276for (Ref<OpenXRIPBinding> binding : p_bindings) {277String binding_path = binding->get_binding_path();278if (binding_path.find_char(',') >= 0) {279// Convert old binding approach to new...280add_new_binding(binding->get_action(), binding_path);281} else {282add_binding(binding);283}284}285286emit_changed();287}288289Array OpenXRInteractionProfile::get_bindings() const {290return bindings;291}292293Ref<OpenXRIPBinding> OpenXRInteractionProfile::find_binding(const Ref<OpenXRAction> &p_action, const String &p_binding_path) const {294for (Ref<OpenXRIPBinding> binding : bindings) {295if (binding->get_action() == p_action && binding->get_binding_path() == p_binding_path) {296return binding;297}298}299300return Ref<OpenXRIPBinding>();301}302303Vector<Ref<OpenXRIPBinding>> OpenXRInteractionProfile::get_bindings_for_action(const Ref<OpenXRAction> &p_action) const {304Vector<Ref<OpenXRIPBinding>> ret_bindings;305306for (Ref<OpenXRIPBinding> binding : bindings) {307if (binding->get_action() == p_action) {308ret_bindings.push_back(binding);309}310}311312return ret_bindings;313}314315void OpenXRInteractionProfile::add_binding(const Ref<OpenXRIPBinding> &p_binding) {316ERR_FAIL_COND(p_binding.is_null());317318if (!bindings.has(p_binding)) {319ERR_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.");320321bindings.push_back(p_binding);322emit_changed();323}324}325326void OpenXRInteractionProfile::remove_binding(const Ref<OpenXRIPBinding> &p_binding) {327int idx = bindings.find(p_binding);328if (idx != -1) {329bindings.remove_at(idx);330emit_changed();331}332}333334void OpenXRInteractionProfile::add_new_binding(const Ref<OpenXRAction> &p_action, const String &p_paths) {335// This is a helper function to help build our default action sets336337PackedStringArray paths = p_paths.split(",", false);338339for (const String &path : paths) {340Ref<OpenXRIPBinding> binding = OpenXRIPBinding::new_binding(p_action, path);341add_binding(binding);342}343}344345void OpenXRInteractionProfile::remove_binding_for_action(const Ref<OpenXRAction> &p_action) {346for (int i = bindings.size() - 1; i >= 0; i--) {347Ref<OpenXRIPBinding> binding = bindings[i];348if (binding->get_action() == p_action) {349remove_binding(binding);350}351}352}353354bool OpenXRInteractionProfile::has_binding_for_action(const Ref<OpenXRAction> &p_action) {355for (int i = bindings.size() - 1; i >= 0; i--) {356Ref<OpenXRIPBinding> binding = bindings[i];357if (binding->get_action() == p_action) {358return true;359}360}361362return false;363}364365int OpenXRInteractionProfile::get_binding_modifier_count() const {366return binding_modifiers.size();367}368369Ref<OpenXRIPBindingModifier> OpenXRInteractionProfile::get_binding_modifier(int p_index) const {370ERR_FAIL_INDEX_V(p_index, binding_modifiers.size(), nullptr);371372return binding_modifiers[p_index];373}374375void OpenXRInteractionProfile::clear_binding_modifiers() {376// Binding modifiers held within our interaction profile set should be released and destroyed but just in case they are still used some where else.377if (binding_modifiers.is_empty()) {378return;379}380381for (int i = 0; i < binding_modifiers.size(); i++) {382Ref<OpenXRIPBindingModifier> binding_modifier = binding_modifiers[i];383binding_modifier->interaction_profile = nullptr;384}385binding_modifiers.clear();386emit_changed();387}388389void OpenXRInteractionProfile::set_binding_modifiers(const Array &p_binding_modifiers) {390clear_binding_modifiers();391392// 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.393for (int i = 0; i < p_binding_modifiers.size(); i++) {394// Add them anew so we verify our binding modifier pointer.395add_binding_modifier(p_binding_modifiers[i]);396}397}398399Array OpenXRInteractionProfile::get_binding_modifiers() const {400Array ret;401for (const Ref<OpenXRIPBindingModifier> &binding_modifier : binding_modifiers) {402ret.push_back(binding_modifier);403}404return ret;405}406407void OpenXRInteractionProfile::add_binding_modifier(const Ref<OpenXRIPBindingModifier> &p_binding_modifier) {408ERR_FAIL_COND(p_binding_modifier.is_null());409410if (!binding_modifiers.has(p_binding_modifier)) {411if (p_binding_modifier->interaction_profile && p_binding_modifier->interaction_profile != this) {412// Binding modifier should only relate to our interaction profile.413p_binding_modifier->interaction_profile->remove_binding_modifier(p_binding_modifier);414}415416p_binding_modifier->interaction_profile = this;417binding_modifiers.push_back(p_binding_modifier);418emit_changed();419}420}421422void OpenXRInteractionProfile::remove_binding_modifier(const Ref<OpenXRIPBindingModifier> &p_binding_modifier) {423int idx = binding_modifiers.find(p_binding_modifier);424if (idx != -1) {425binding_modifiers.remove_at(idx);426427ERR_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!428p_binding_modifier->interaction_profile = nullptr;429430emit_changed();431}432}433434OpenXRInteractionProfile::~OpenXRInteractionProfile() {435bindings.clear();436clear_binding_modifiers();437}438439440