got vectors working

This commit is contained in:
Jay Christy
2024-05-22 23:18:23 -04:00
parent 61259aa8d4
commit 99b11eb675

View File

@@ -5,7 +5,7 @@ use std::borrow::Cow;
use bevy::prelude::*; use bevy::prelude::*;
use bevy_openxr::{ use bevy_openxr::{
action_binding::OxrSuggestActionBinding, action_binding::OxrSuggestActionBinding,
action_set_attaching::{AttachedActionSets, OxrAttachActionSet}, action_set_attaching::OxrAttachActionSet,
add_xr_plugins, add_xr_plugins,
resources::{OxrInstance, OxrSession}, resources::{OxrInstance, OxrSession},
}; };
@@ -18,16 +18,24 @@ fn main() {
.add_systems(Startup, setup_scene) .add_systems(Startup, setup_scene)
.add_systems(Startup, create_action_entities) .add_systems(Startup, create_action_entities)
.add_systems(Startup, create_openxr_events.after(create_action_entities)) .add_systems(Startup, create_openxr_events.after(create_action_entities))
.add_systems(Update, sync_actions) .add_systems(Update, sync_active_action_sets)
.add_systems( .add_systems(
Update, Update,
sync_and_update_action_states_f32.after(sync_actions), sync_and_update_action_states_f32.after(sync_active_action_sets),
) )
.add_systems( .add_systems(
Update, Update,
sync_and_update_action_states_bool.after(sync_actions), sync_and_update_action_states_bool.after(sync_active_action_sets),
) )
.add_systems(Update, read_action_with_marker_component.after(sync_and_update_action_states_f32)) .add_systems(
Update,
sync_and_update_action_states_vector.after(sync_active_action_sets),
)
.add_systems(
Update,
read_action_with_marker_component.after(sync_and_update_action_states_f32),
)
.run(); .run();
} }
@@ -73,6 +81,15 @@ struct CreateActionSet {
priority: u32, priority: u32,
} }
#[derive(Component, Clone)]
struct OXRActionSet(openxr::ActionSet);
#[derive(Component)]
struct AttachedActionSet;
#[derive(Component)]
struct ActiveSet;
#[derive(Component)] #[derive(Component)]
struct CreateAction { struct CreateAction {
action_name: Cow<'static, str>, action_name: Cow<'static, str>,
@@ -107,11 +124,14 @@ struct CustomActionMarker;
fn create_action_entities(mut commands: Commands) { fn create_action_entities(mut commands: Commands) {
//create a set //create a set
let set = commands let set = commands
.spawn(CreateActionSet { .spawn((
name: "test".into(), CreateActionSet {
pretty_name: "pretty test".into(), name: "test".into(),
priority: u32::MIN, pretty_name: "pretty test".into(),
}) priority: u32::MIN,
},
ActiveSet, //marker to indicate we want this synced
))
.id(); .id();
//create an action //create an action
let action = commands let action = commands
@@ -119,7 +139,7 @@ fn create_action_entities(mut commands: Commands) {
CreateAction { CreateAction {
action_name: "action_name".into(), action_name: "action_name".into(),
localized_name: "localized_name".into(), localized_name: "localized_name".into(),
action_type: bevy_xr::actions::ActionType::Float, action_type: bevy_xr::actions::ActionType::Vector,
}, },
CustomActionMarker, //lets try a marker component CustomActionMarker, //lets try a marker component
)) ))
@@ -129,7 +149,7 @@ fn create_action_entities(mut commands: Commands) {
let binding = commands let binding = commands
.spawn(CreateBinding { .spawn(CreateBinding {
profile: "/interaction_profiles/valve/index_controller".into(), profile: "/interaction_profiles/valve/index_controller".into(),
binding: "/user/hand/right/input/thumbstick/y".into(), binding: "/user/hand/right/input/thumbstick".into(),
}) })
.id(); .id();
@@ -138,34 +158,34 @@ fn create_action_entities(mut commands: Commands) {
commands.entity(action).add_child(binding); commands.entity(action).add_child(binding);
commands.entity(set).add_child(action); commands.entity(set).add_child(action);
//create an action // //create an action
let action = commands // let action = commands
.spawn(( // .spawn((
CreateAction { // CreateAction {
action_name: "action_name_bool".into(), // action_name: "action_name_bool".into(),
localized_name: "localized_name_bool".into(), // localized_name: "localized_name_bool".into(),
action_type: bevy_xr::actions::ActionType::Bool, // action_type: bevy_xr::actions::ActionType::Bool,
}, // },
CustomActionMarker, //lets try a marker component // CustomActionMarker, //lets try a marker component
)) // ))
.id(); // .id();
//create a binding // //create a binding
let binding = commands // let binding = commands
.spawn(CreateBinding { // .spawn(CreateBinding {
profile: "/interaction_profiles/valve/index_controller".into(), // profile: "/interaction_profiles/valve/index_controller".into(),
binding: "/user/hand/right/input/a/click".into(), // binding: "/user/hand/right/input/a/click".into(),
}) // })
.id(); // .id();
//add action to set, this isnt the best // //add action to set, this isnt the best
//TODO look into a better system // //TODO look into a better system
commands.entity(action).add_child(binding); // commands.entity(action).add_child(binding);
commands.entity(set).add_child(action); // commands.entity(set).add_child(action);
} }
fn create_openxr_events( fn create_openxr_events(
action_sets_query: Query<(&CreateActionSet, &Children)>, action_sets_query: Query<(&CreateActionSet, &Children, Entity)>,
actions_query: Query<(&CreateAction, &Children)>, actions_query: Query<(&CreateAction, &Children)>,
bindings_query: Query<&CreateBinding>, bindings_query: Query<&CreateBinding>,
instance: ResMut<OxrInstance>, instance: ResMut<OxrInstance>,
@@ -177,11 +197,14 @@ fn create_openxr_events(
//lets create some sets! //lets create some sets!
//we gonna need a collection of these sets for later //we gonna need a collection of these sets for later
// let mut ActionSets = HashMap::new(); // let mut ActionSets = HashMap::new();
for (set, children) in action_sets_query.iter() { for (set, children, id) in action_sets_query.iter() {
//create action set //create action set
let action_set: openxr::ActionSet = instance let action_set: openxr::ActionSet = instance
.create_action_set(&set.name, &set.pretty_name, set.priority) .create_action_set(&set.name, &set.pretty_name, set.priority)
.unwrap(); .unwrap();
//now that we have the action set we need to put it back onto the entity for later
let oxr_action_set = OXRActionSet(action_set.clone());
commands.entity(id).insert(oxr_action_set);
//since the actions are made from the sets lets go //since the actions are made from the sets lets go
for &child in children.iter() { for &child in children.iter() {
@@ -312,14 +335,15 @@ fn create_openxr_events(
} }
} }
fn sync_actions(session: Res<OxrSession>, attached: Res<AttachedActionSets>) { fn sync_active_action_sets(
//first we need to sync our actions session: Res<OxrSession>,
let why = &attached active_action_set_query: Query<&OXRActionSet, With<ActiveSet>>,
.sets ) {
let active_sets = active_action_set_query
.iter() .iter()
.map(|v| ActiveActionSet::from(v)) .map(|v| ActiveActionSet::from(&v.0))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let sync = session.sync_actions(&why[..]); let sync = session.sync_actions(&active_sets[..]);
match sync { match sync {
Ok(_) => info!("sync ok"), Ok(_) => info!("sync ok"),
Err(_) => error!("sync error"), Err(_) => error!("sync error"),
@@ -378,6 +402,32 @@ fn sync_and_update_action_states_bool(
} }
} }
fn sync_and_update_action_states_vector(
session: Res<OxrSession>,
mut vector_query: Query<(&ActionVector2fReference, &mut MyActionState)>,
) {
//now we do the action state for f32
for (reference, mut silly_state) in vector_query.iter_mut() {
let state = reference.action.state(&session, Path::NULL);
match state {
Ok(s) => {
info!("we found a state");
let new_state = MyActionState::Vector(ActionStateVector {
current_state: [s.current_state.x, s.current_state.y],
changed_since_last_sync: s.changed_since_last_sync,
last_change_time: s.last_change_time.as_nanos(),
is_active: s.is_active,
});
*silly_state = new_state;
}
Err(_) => {
info!("error getting action state");
}
}
}
}
fn read_action_with_marker_component( fn read_action_with_marker_component(
mut action_query: Query<&MyActionState, With<CustomActionMarker>>, mut action_query: Query<&MyActionState, With<CustomActionMarker>>,
) { ) {