Path: blob/master/modules/openxr/action_map/openxr_action_set.cpp
10278 views
/**************************************************************************/1/* openxr_action_set.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.h"3132void OpenXRActionSet::_bind_methods() {33ClassDB::bind_method(D_METHOD("set_localized_name", "localized_name"), &OpenXRActionSet::set_localized_name);34ClassDB::bind_method(D_METHOD("get_localized_name"), &OpenXRActionSet::get_localized_name);35ADD_PROPERTY(PropertyInfo(Variant::STRING, "localized_name"), "set_localized_name", "get_localized_name");3637ClassDB::bind_method(D_METHOD("set_priority", "priority"), &OpenXRActionSet::set_priority);38ClassDB::bind_method(D_METHOD("get_priority"), &OpenXRActionSet::get_priority);39ADD_PROPERTY(PropertyInfo(Variant::INT, "priority"), "set_priority", "get_priority");4041ClassDB::bind_method(D_METHOD("get_action_count"), &OpenXRActionSet::get_action_count);42ClassDB::bind_method(D_METHOD("set_actions", "actions"), &OpenXRActionSet::set_actions);43ClassDB::bind_method(D_METHOD("get_actions"), &OpenXRActionSet::get_actions);44ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "actions", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRAction", PROPERTY_USAGE_NO_EDITOR), "set_actions", "get_actions");4546ClassDB::bind_method(D_METHOD("add_action", "action"), &OpenXRActionSet::add_action);47ClassDB::bind_method(D_METHOD("remove_action", "action"), &OpenXRActionSet::remove_action);48}4950Ref<OpenXRActionSet> OpenXRActionSet::new_action_set(const char *p_name, const char *p_localized_name, const int p_priority) {51// This is a helper function to help build our default action sets5253Ref<OpenXRActionSet> action_set;54action_set.instantiate();55action_set->set_name(String(p_name));56action_set->set_localized_name(p_localized_name);57action_set->set_priority(p_priority);5859return action_set;60}6162void OpenXRActionSet::set_localized_name(const String p_localized_name) {63localized_name = p_localized_name;64emit_changed();65}6667String OpenXRActionSet::get_localized_name() const {68return localized_name;69}7071void OpenXRActionSet::set_priority(const int p_priority) {72priority = p_priority;73emit_changed();74}7576int OpenXRActionSet::get_priority() const {77return priority;78}7980int OpenXRActionSet::get_action_count() const {81return actions.size();82}8384void OpenXRActionSet::clear_actions() {85// Actions held within our action set should be released and destroyed but just in case they are still used some where else86if (actions.is_empty()) {87return;88}8990for (int i = 0; i < actions.size(); i++) {91Ref<OpenXRAction> action = actions[i];92action->action_set = nullptr;93}94actions.clear();95emit_changed();96}9798void OpenXRActionSet::set_actions(Array p_actions) {99// Any actions not retained in p_actions should be freed automatically, those held within our Array will have be relinked to our action set.100clear_actions();101102for (int i = 0; i < p_actions.size(); i++) {103// add them anew so we verify our action_set pointer104add_action(p_actions[i]);105}106}107108Array OpenXRActionSet::get_actions() const {109return actions;110}111112Ref<OpenXRAction> OpenXRActionSet::get_action(const String p_name) const {113for (int i = 0; i < actions.size(); i++) {114Ref<OpenXRAction> action = actions[i];115if (action->get_name() == p_name) {116return action;117}118}119120return Ref<OpenXRAction>();121}122123void OpenXRActionSet::add_action(Ref<OpenXRAction> p_action) {124ERR_FAIL_COND(p_action.is_null());125126if (!actions.has(p_action)) {127if (p_action->action_set && p_action->action_set != this) {128// action should only relate to our action set129p_action->action_set->remove_action(p_action);130}131132p_action->action_set = this;133actions.push_back(p_action);134emit_changed();135}136}137138void OpenXRActionSet::remove_action(Ref<OpenXRAction> p_action) {139int idx = actions.find(p_action);140if (idx != -1) {141actions.remove_at(idx);142143ERR_FAIL_COND_MSG(p_action->action_set != this, "Removing action that belongs to this action set but had incorrect action set pointer."); // This should never happen!144p_action->action_set = nullptr;145146emit_changed();147}148}149150Ref<OpenXRAction> OpenXRActionSet::add_new_action(const char *p_name, const char *p_localized_name, const OpenXRAction::ActionType p_action_type, const char *p_toplevel_paths) {151// This is a helper function to help build our default action sets152153Ref<OpenXRAction> new_action = OpenXRAction::new_action(p_name, p_localized_name, p_action_type, p_toplevel_paths);154add_action(new_action);155return new_action;156}157158OpenXRActionSet::~OpenXRActionSet() {159clear_actions();160}161162163