mirror of
https://github.com/godotengine/godot.git
synced 2025-11-01 06:01:14 +00:00
OpenXR: Add support for binding modifiers
This commit is contained in:
parent
c2e4ae782a
commit
0a61ebdcea
56 changed files with 3127 additions and 763 deletions
|
|
@ -39,6 +39,12 @@ void OpenXRIPBinding::_bind_methods() {
|
|||
ClassDB::bind_method(D_METHOD("get_binding_path"), &OpenXRIPBinding::get_binding_path);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::STRING, "binding_path"), "set_binding_path", "get_binding_path");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_binding_modifier_count"), &OpenXRIPBinding::get_binding_modifier_count);
|
||||
ClassDB::bind_method(D_METHOD("get_binding_modifier", "index"), &OpenXRIPBinding::get_binding_modifier);
|
||||
ClassDB::bind_method(D_METHOD("set_binding_modifiers", "binding_modifiers"), &OpenXRIPBinding::set_binding_modifiers);
|
||||
ClassDB::bind_method(D_METHOD("get_binding_modifiers"), &OpenXRIPBinding::get_binding_modifiers);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "binding_modifiers", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRActionBindingModifier", PROPERTY_USAGE_NO_EDITOR), "set_binding_modifiers", "get_binding_modifiers");
|
||||
|
||||
// Deprecated
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
ClassDB::bind_method(D_METHOD("set_paths", "paths"), &OpenXRIPBinding::set_paths);
|
||||
|
|
@ -63,7 +69,7 @@ Ref<OpenXRIPBinding> OpenXRIPBinding::new_binding(const Ref<OpenXRAction> p_acti
|
|||
return binding;
|
||||
}
|
||||
|
||||
void OpenXRIPBinding::set_action(const Ref<OpenXRAction> p_action) {
|
||||
void OpenXRIPBinding::set_action(const Ref<OpenXRAction> &p_action) {
|
||||
action = p_action;
|
||||
emit_changed();
|
||||
}
|
||||
|
|
@ -81,6 +87,75 @@ String OpenXRIPBinding::get_binding_path() const {
|
|||
return binding_path;
|
||||
}
|
||||
|
||||
int OpenXRIPBinding::get_binding_modifier_count() const {
|
||||
return binding_modifiers.size();
|
||||
}
|
||||
|
||||
Ref<OpenXRActionBindingModifier> OpenXRIPBinding::get_binding_modifier(int p_index) const {
|
||||
ERR_FAIL_INDEX_V(p_index, binding_modifiers.size(), nullptr);
|
||||
|
||||
return binding_modifiers[p_index];
|
||||
}
|
||||
|
||||
void OpenXRIPBinding::clear_binding_modifiers() {
|
||||
// Binding modifiers held within our interaction profile set should be released and destroyed but just in case they are still used some where else.
|
||||
if (binding_modifiers.is_empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < binding_modifiers.size(); i++) {
|
||||
Ref<OpenXRActionBindingModifier> binding_modifier = binding_modifiers[i];
|
||||
binding_modifier->ip_binding = nullptr;
|
||||
}
|
||||
binding_modifiers.clear();
|
||||
emit_changed();
|
||||
}
|
||||
|
||||
void OpenXRIPBinding::set_binding_modifiers(const Array &p_binding_modifiers) {
|
||||
clear_binding_modifiers();
|
||||
|
||||
// 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.
|
||||
for (int i = 0; i < p_binding_modifiers.size(); i++) {
|
||||
// Add them anew so we verify our binding modifier pointer.
|
||||
add_binding_modifier(p_binding_modifiers[i]);
|
||||
}
|
||||
}
|
||||
|
||||
Array OpenXRIPBinding::get_binding_modifiers() const {
|
||||
Array ret;
|
||||
for (const Ref<OpenXRActionBindingModifier> &binding_modifier : binding_modifiers) {
|
||||
ret.push_back(binding_modifier);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void OpenXRIPBinding::add_binding_modifier(const Ref<OpenXRActionBindingModifier> &p_binding_modifier) {
|
||||
ERR_FAIL_COND(p_binding_modifier.is_null());
|
||||
|
||||
if (!binding_modifiers.has(p_binding_modifier)) {
|
||||
if (p_binding_modifier->ip_binding && p_binding_modifier->ip_binding != this) {
|
||||
// Binding modifier should only relate to our binding.
|
||||
p_binding_modifier->ip_binding->remove_binding_modifier(p_binding_modifier);
|
||||
}
|
||||
|
||||
p_binding_modifier->ip_binding = this;
|
||||
binding_modifiers.push_back(p_binding_modifier);
|
||||
emit_changed();
|
||||
}
|
||||
}
|
||||
|
||||
void OpenXRIPBinding::remove_binding_modifier(const Ref<OpenXRActionBindingModifier> &p_binding_modifier) {
|
||||
int idx = binding_modifiers.find(p_binding_modifier);
|
||||
if (idx != -1) {
|
||||
binding_modifiers.remove_at(idx);
|
||||
|
||||
ERR_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!
|
||||
p_binding_modifier->ip_binding = nullptr;
|
||||
|
||||
emit_changed();
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
|
||||
void OpenXRIPBinding::set_paths(const PackedStringArray p_paths) { // Deprecated, but needed for loading old action maps.
|
||||
|
|
@ -148,6 +223,12 @@ void OpenXRInteractionProfile::_bind_methods() {
|
|||
ClassDB::bind_method(D_METHOD("set_bindings", "bindings"), &OpenXRInteractionProfile::set_bindings);
|
||||
ClassDB::bind_method(D_METHOD("get_bindings"), &OpenXRInteractionProfile::get_bindings);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "bindings", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRIPBinding", PROPERTY_USAGE_NO_EDITOR), "set_bindings", "get_bindings");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_binding_modifier_count"), &OpenXRInteractionProfile::get_binding_modifier_count);
|
||||
ClassDB::bind_method(D_METHOD("get_binding_modifier", "index"), &OpenXRInteractionProfile::get_binding_modifier);
|
||||
ClassDB::bind_method(D_METHOD("set_binding_modifiers", "binding_modifiers"), &OpenXRInteractionProfile::set_binding_modifiers);
|
||||
ClassDB::bind_method(D_METHOD("get_binding_modifiers"), &OpenXRInteractionProfile::get_binding_modifiers);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "binding_modifiers", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRIPBindingModifier", PROPERTY_USAGE_NO_EDITOR), "set_binding_modifiers", "get_binding_modifiers");
|
||||
}
|
||||
|
||||
Ref<OpenXRInteractionProfile> OpenXRInteractionProfile::new_profile(const char *p_input_profile_path) {
|
||||
|
|
@ -183,7 +264,7 @@ Ref<OpenXRIPBinding> OpenXRInteractionProfile::get_binding(int p_index) const {
|
|||
return bindings[p_index];
|
||||
}
|
||||
|
||||
void OpenXRInteractionProfile::set_bindings(Array p_bindings) {
|
||||
void OpenXRInteractionProfile::set_bindings(const Array &p_bindings) {
|
||||
bindings.clear();
|
||||
|
||||
for (Ref<OpenXRIPBinding> binding : p_bindings) {
|
||||
|
|
@ -203,7 +284,7 @@ Array OpenXRInteractionProfile::get_bindings() const {
|
|||
return bindings;
|
||||
}
|
||||
|
||||
Ref<OpenXRIPBinding> OpenXRInteractionProfile::find_binding(const Ref<OpenXRAction> p_action, const String &p_binding_path) const {
|
||||
Ref<OpenXRIPBinding> OpenXRInteractionProfile::find_binding(const Ref<OpenXRAction> &p_action, const String &p_binding_path) const {
|
||||
for (Ref<OpenXRIPBinding> binding : bindings) {
|
||||
if (binding->get_action() == p_action && binding->get_binding_path() == p_binding_path) {
|
||||
return binding;
|
||||
|
|
@ -213,7 +294,7 @@ Ref<OpenXRIPBinding> OpenXRInteractionProfile::find_binding(const Ref<OpenXRActi
|
|||
return Ref<OpenXRIPBinding>();
|
||||
}
|
||||
|
||||
Vector<Ref<OpenXRIPBinding>> OpenXRInteractionProfile::get_bindings_for_action(const Ref<OpenXRAction> p_action) const {
|
||||
Vector<Ref<OpenXRIPBinding>> OpenXRInteractionProfile::get_bindings_for_action(const Ref<OpenXRAction> &p_action) const {
|
||||
Vector<Ref<OpenXRIPBinding>> ret_bindings;
|
||||
|
||||
for (Ref<OpenXRIPBinding> binding : bindings) {
|
||||
|
|
@ -225,7 +306,7 @@ Vector<Ref<OpenXRIPBinding>> OpenXRInteractionProfile::get_bindings_for_action(c
|
|||
return ret_bindings;
|
||||
}
|
||||
|
||||
void OpenXRInteractionProfile::add_binding(Ref<OpenXRIPBinding> p_binding) {
|
||||
void OpenXRInteractionProfile::add_binding(const Ref<OpenXRIPBinding> &p_binding) {
|
||||
ERR_FAIL_COND(p_binding.is_null());
|
||||
|
||||
if (!bindings.has(p_binding)) {
|
||||
|
|
@ -236,7 +317,7 @@ void OpenXRInteractionProfile::add_binding(Ref<OpenXRIPBinding> p_binding) {
|
|||
}
|
||||
}
|
||||
|
||||
void OpenXRInteractionProfile::remove_binding(Ref<OpenXRIPBinding> p_binding) {
|
||||
void OpenXRInteractionProfile::remove_binding(const Ref<OpenXRIPBinding> &p_binding) {
|
||||
int idx = bindings.find(p_binding);
|
||||
if (idx != -1) {
|
||||
bindings.remove_at(idx);
|
||||
|
|
@ -244,7 +325,7 @@ void OpenXRInteractionProfile::remove_binding(Ref<OpenXRIPBinding> p_binding) {
|
|||
}
|
||||
}
|
||||
|
||||
void OpenXRInteractionProfile::add_new_binding(const Ref<OpenXRAction> p_action, const String &p_paths) {
|
||||
void OpenXRInteractionProfile::add_new_binding(const Ref<OpenXRAction> &p_action, const String &p_paths) {
|
||||
// This is a helper function to help build our default action sets
|
||||
|
||||
PackedStringArray paths = p_paths.split(",", false);
|
||||
|
|
@ -255,7 +336,7 @@ void OpenXRInteractionProfile::add_new_binding(const Ref<OpenXRAction> p_action,
|
|||
}
|
||||
}
|
||||
|
||||
void OpenXRInteractionProfile::remove_binding_for_action(const Ref<OpenXRAction> p_action) {
|
||||
void OpenXRInteractionProfile::remove_binding_for_action(const Ref<OpenXRAction> &p_action) {
|
||||
for (int i = bindings.size() - 1; i >= 0; i--) {
|
||||
Ref<OpenXRIPBinding> binding = bindings[i];
|
||||
if (binding->get_action() == p_action) {
|
||||
|
|
@ -264,7 +345,7 @@ void OpenXRInteractionProfile::remove_binding_for_action(const Ref<OpenXRAction>
|
|||
}
|
||||
}
|
||||
|
||||
bool OpenXRInteractionProfile::has_binding_for_action(const Ref<OpenXRAction> p_action) {
|
||||
bool OpenXRInteractionProfile::has_binding_for_action(const Ref<OpenXRAction> &p_action) {
|
||||
for (int i = bindings.size() - 1; i >= 0; i--) {
|
||||
Ref<OpenXRIPBinding> binding = bindings[i];
|
||||
if (binding->get_action() == p_action) {
|
||||
|
|
@ -275,6 +356,76 @@ bool OpenXRInteractionProfile::has_binding_for_action(const Ref<OpenXRAction> p_
|
|||
return false;
|
||||
}
|
||||
|
||||
int OpenXRInteractionProfile::get_binding_modifier_count() const {
|
||||
return binding_modifiers.size();
|
||||
}
|
||||
|
||||
Ref<OpenXRIPBindingModifier> OpenXRInteractionProfile::get_binding_modifier(int p_index) const {
|
||||
ERR_FAIL_INDEX_V(p_index, binding_modifiers.size(), nullptr);
|
||||
|
||||
return binding_modifiers[p_index];
|
||||
}
|
||||
|
||||
void OpenXRInteractionProfile::clear_binding_modifiers() {
|
||||
// Binding modifiers held within our interaction profile set should be released and destroyed but just in case they are still used some where else.
|
||||
if (binding_modifiers.is_empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < binding_modifiers.size(); i++) {
|
||||
Ref<OpenXRIPBindingModifier> binding_modifier = binding_modifiers[i];
|
||||
binding_modifier->interaction_profile = nullptr;
|
||||
}
|
||||
binding_modifiers.clear();
|
||||
emit_changed();
|
||||
}
|
||||
|
||||
void OpenXRInteractionProfile::set_binding_modifiers(const Array &p_binding_modifiers) {
|
||||
clear_binding_modifiers();
|
||||
|
||||
// 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.
|
||||
for (int i = 0; i < p_binding_modifiers.size(); i++) {
|
||||
// Add them anew so we verify our binding modifier pointer.
|
||||
add_binding_modifier(p_binding_modifiers[i]);
|
||||
}
|
||||
}
|
||||
|
||||
Array OpenXRInteractionProfile::get_binding_modifiers() const {
|
||||
Array ret;
|
||||
for (const Ref<OpenXRIPBindingModifier> &binding_modifier : binding_modifiers) {
|
||||
ret.push_back(binding_modifier);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void OpenXRInteractionProfile::add_binding_modifier(const Ref<OpenXRIPBindingModifier> &p_binding_modifier) {
|
||||
ERR_FAIL_COND(p_binding_modifier.is_null());
|
||||
|
||||
if (!binding_modifiers.has(p_binding_modifier)) {
|
||||
if (p_binding_modifier->interaction_profile && p_binding_modifier->interaction_profile != this) {
|
||||
// Binding modifier should only relate to our interaction profile.
|
||||
p_binding_modifier->interaction_profile->remove_binding_modifier(p_binding_modifier);
|
||||
}
|
||||
|
||||
p_binding_modifier->interaction_profile = this;
|
||||
binding_modifiers.push_back(p_binding_modifier);
|
||||
emit_changed();
|
||||
}
|
||||
}
|
||||
|
||||
void OpenXRInteractionProfile::remove_binding_modifier(const Ref<OpenXRIPBindingModifier> &p_binding_modifier) {
|
||||
int idx = binding_modifiers.find(p_binding_modifier);
|
||||
if (idx != -1) {
|
||||
binding_modifiers.remove_at(idx);
|
||||
|
||||
ERR_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!
|
||||
p_binding_modifier->interaction_profile = nullptr;
|
||||
|
||||
emit_changed();
|
||||
}
|
||||
}
|
||||
|
||||
OpenXRInteractionProfile::~OpenXRInteractionProfile() {
|
||||
bindings.clear();
|
||||
clear_binding_modifiers();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue