reading inputs with marker components

This commit is contained in:
Jay Christy
2024-05-16 13:08:37 -04:00
parent 20e3a1cf7a
commit 4e279e3197

View File

@@ -2,22 +2,21 @@
use std::borrow::Cow; use std::borrow::Cow;
use bevy::{prelude::*, scene::ron::value::Float}; use bevy::prelude::*;
use bevy_openxr::{ use bevy_openxr::{
action_binding::OxrSuggestActionBinding, action_binding::OxrSuggestActionBinding,
action_set_attaching::{AttachedActionSets, OxrAttachActionSet}, action_set_attaching::{AttachedActionSets, OxrAttachActionSet},
add_xr_plugins, add_xr_plugins,
resources::{OxrInstance, OxrSession}, resources::{OxrInstance, OxrSession},
}; };
use openxr::{ActionType, ActiveActionSet, Path, Vector2f}; use openxr::{ActiveActionSet, Path, Vector2f};
fn main() { fn main() {
App::new() App::new()
.add_plugins(add_xr_plugins(DefaultPlugins)) .add_plugins(add_xr_plugins(DefaultPlugins))
.add_plugins(bevy_xr_utils::hand_gizmos::HandGizmosPlugin) .add_plugins(bevy_xr_utils::hand_gizmos::HandGizmosPlugin)
.add_systems(Startup, setup_scene) .add_systems(Startup, setup_scene)
.init_resource::<TestAction>() .add_systems(Update, read_action_with_marker_component)
.add_systems(Update, read_action)
.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))
.run(); .run();
@@ -58,44 +57,6 @@ fn setup_scene(
}); });
} }
#[derive(Resource, Default)]
struct TestAction {
action: Option<openxr::Action<f32>>,
}
fn read_action(
session: ResMut<OxrSession>,
test: ResMut<TestAction>,
attached: ResMut<AttachedActionSets>,
) {
//sync before
let why = &attached
.sets
.iter()
.map(|v| ActiveActionSet::from(v))
.collect::<Vec<_>>();
let sync = session.sync_actions(&why[..]);
match sync {
Ok(_) => info!("sync ok"),
Err(_) => error!("sync error"),
}
//now check the action
let action = &test.action;
match action {
Some(act) => {
let thing = act.state(&session, Path::NULL);
match thing {
Ok(a) => {
info!("action state: {:?}", a);
}
Err(_) => info!("error getting state"),
}
}
None => info!("no action"),
}
}
#[derive(Component)] #[derive(Component)]
struct CreateActionSet { struct CreateActionSet {
name: Cow<'static, str>, name: Cow<'static, str>,
@@ -116,6 +77,14 @@ struct CreateBinding {
binding: Cow<'static, str>, binding: Cow<'static, str>,
} }
#[derive(Component)]
struct ActionReference {
action: openxr::Action<f32>,
}
#[derive(Component)]
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
@@ -127,11 +96,14 @@ fn create_action_entities(mut commands: Commands) {
.id(); .id();
//create an action //create an action
let action = commands let action = commands
.spawn(CreateAction { .spawn((
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::Float,
}) },
CustomActionMarker, //lets try a marker component
))
.id(); .id();
//create a binding //create a binding
@@ -155,8 +127,8 @@ fn create_openxr_events(
instance: ResMut<OxrInstance>, instance: ResMut<OxrInstance>,
mut binding_writer: EventWriter<OxrSuggestActionBinding>, mut binding_writer: EventWriter<OxrSuggestActionBinding>,
mut attach_writer: EventWriter<OxrAttachActionSet>, mut attach_writer: EventWriter<OxrAttachActionSet>,
//please remove this //not my favorite way of doing this
mut test: ResMut<TestAction>, mut commands: Commands,
) { ) {
//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
@@ -211,8 +183,10 @@ fn create_openxr_events(
.unwrap(); .unwrap();
//please put this in a function so I dont go crazy //please put this in a function so I dont go crazy
//TODO remove this crap //insert a reference for later
test.action = Some(action.clone()); commands.entity(child).insert(ActionReference {
action: action.clone(),
});
//since we need actions for bindings lets go!! //since we need actions for bindings lets go!!
for &bind in bindings.iter() { for &bind in bindings.iter() {
//interaction profile //interaction profile
@@ -265,3 +239,38 @@ fn create_openxr_events(
attach_writer.send(OxrAttachActionSet(action_set)); attach_writer.send(OxrAttachActionSet(action_set));
} }
} }
fn read_action_with_marker_component(
action_query: Query<&ActionReference, With<CustomActionMarker>>,
session: ResMut<OxrSession>,
attached: ResMut<AttachedActionSets>,
) {
//first we need to sync our actions
let why = &attached
.sets
.iter()
.map(|v| ActiveActionSet::from(v))
.collect::<Vec<_>>();
let sync = session.sync_actions(&why[..]);
match sync {
Ok(_) => info!("sync ok"),
Err(_) => error!("sync error"),
}
//now for the actual checking
let action = action_query.get_single();
match action {
Ok(reference) => {
let state = reference.action.state(&session, Path::NULL);
match state {
Ok(a) => {
info!("action state: {:?}", a);
}
Err(_) => info!("error getting state"),
}
}
Err(_) => {
info!("no action")
}
}
}