confirmed working for bools and f32

This commit is contained in:
Jay Christy
2024-05-16 12:45:18 -04:00
parent f74f701f50
commit 20e3a1cf7a

View File

@@ -2,14 +2,14 @@
use std::borrow::Cow; use std::borrow::Cow;
use bevy::{prelude::*, utils::hashbrown::HashMap}; use bevy::{prelude::*, scene::ron::value::Float};
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}; use openxr::{ActionType, ActiveActionSet, Path, Vector2f};
fn main() { fn main() {
App::new() App::new()
@@ -18,7 +18,6 @@ fn main() {
.add_systems(Startup, setup_scene) .add_systems(Startup, setup_scene)
.init_resource::<TestAction>() .init_resource::<TestAction>()
.add_systems(Update, read_action) .add_systems(Update, read_action)
// .add_systems(Startup, create_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();
@@ -61,45 +60,7 @@ fn setup_scene(
#[derive(Resource, Default)] #[derive(Resource, Default)]
struct TestAction { struct TestAction {
action: Option<openxr::Action<bool>>, action: Option<openxr::Action<f32>>,
}
fn create_action(
mut writer: EventWriter<OxrSuggestActionBinding>,
instance: ResMut<OxrInstance>,
mut test: ResMut<TestAction>,
mut set_writer: EventWriter<OxrAttachActionSet>,
) {
let name = "test";
let pretty_name = "pretty test";
let priority = u32::MIN;
//create action set
let set: openxr::ActionSet = instance
.create_action_set(name, pretty_name, priority)
.unwrap();
let action_name = "action_name";
let localized_name = "localized_name";
//create new action from action set
let bool_action: openxr::Action<bool> = set
.create_action::<bool>(action_name, localized_name, &[])
.unwrap();
//interaction profile
let profile = Cow::from("/interaction_profiles/valve/index_controller");
//bindings
let binding = vec![Cow::from("/user/hand/right/input/a/click")];
let sugestion = OxrSuggestActionBinding {
action: bool_action.as_raw(),
interaction_profile: profile,
bindings: binding,
};
//finally send the suggestion
writer.send(sugestion);
set_writer.send(OxrAttachActionSet(set.clone()));
test.action = Some(bool_action);
} }
fn read_action( fn read_action(
@@ -107,7 +68,7 @@ fn read_action(
test: ResMut<TestAction>, test: ResMut<TestAction>,
attached: ResMut<AttachedActionSets>, attached: ResMut<AttachedActionSets>,
) { ) {
//maybe sync before? //sync before
let why = &attached let why = &attached
.sets .sets
.iter() .iter()
@@ -119,7 +80,7 @@ fn read_action(
Err(_) => error!("sync error"), Err(_) => error!("sync error"),
} }
//now check the action? //now check the action
let action = &test.action; let action = &test.action;
match action { match action {
Some(act) => { Some(act) => {
@@ -146,7 +107,7 @@ struct CreateActionSet {
struct CreateAction { struct CreateAction {
action_name: Cow<'static, str>, action_name: Cow<'static, str>,
localized_name: Cow<'static, str>, localized_name: Cow<'static, str>,
action_type: ActionType, action_type: bevy_xr::actions::ActionType,
} }
#[derive(Component)] #[derive(Component)]
@@ -169,7 +130,7 @@ fn create_action_entities(mut commands: 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: ActionType::BOOLEAN_INPUT, action_type: bevy_xr::actions::ActionType::Float,
}) })
.id(); .id();
@@ -177,7 +138,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/a/click".into(), binding: "/user/hand/right/input/thumbstick/y".into(),
}) })
.id(); .id();
@@ -188,9 +149,9 @@ fn create_action_entities(mut commands: Commands) {
} }
fn create_openxr_events( fn create_openxr_events(
ActionSetsQuery: Query<(&CreateActionSet, &Children)>, action_sets_query: Query<(&CreateActionSet, &Children)>,
ActionsQuery: Query<(&CreateAction, &Children)>, actions_query: Query<(&CreateAction, &Children)>,
BindingsQuery: Query<&CreateBinding>, bindings_query: Query<&CreateBinding>,
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>,
@@ -200,43 +161,105 @@ 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 ActionSetsQuery.iter() { for (set, children) 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();
// ActionSets.insert(set.name.clone(), 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() {
//first get the action entity and stuff //first get the action entity and stuff
let (create_action, bindings) = ActionsQuery.get(child).unwrap(); let (create_action, bindings) = actions_query.get(child).unwrap();
//lets create dat actions //lets create dat actions
let bool_action: openxr::Action<bool> = action_set match create_action.action_type {
.create_action::<bool>( bevy_xr::actions::ActionType::Bool => {
&create_action.action_name, let action: openxr::Action<bool> = action_set
&create_action.localized_name, .create_action::<bool>(
&[], &create_action.action_name,
) &create_action.localized_name,
.unwrap(); &[],
//TODO remove this crap )
test.action = Some(bool_action.clone()); .unwrap();
//since we need actions for bindings lets go!! //please put this in a function so I dont go crazy
for &bind in bindings.iter() { //TODO remove this crap
//interaction profile // test.action = Some(action.clone());
//get the binding entity and stuff //since we need actions for bindings lets go!!
let create_binding = BindingsQuery.get(bind).unwrap(); for &bind in bindings.iter() {
let profile = Cow::from(create_binding.profile.clone()); //interaction profile
//bindings //get the binding entity and stuff
let binding = vec![Cow::from(create_binding.binding.clone())]; let create_binding = bindings_query.get(bind).unwrap();
let sugestion = OxrSuggestActionBinding { let profile = Cow::from(create_binding.profile.clone());
action: bool_action.as_raw(), //bindings
interaction_profile: profile, let binding = vec![Cow::from(create_binding.binding.clone())];
bindings: binding, let sugestion = OxrSuggestActionBinding {
}; action: action.as_raw(),
//finally send the suggestion interaction_profile: profile,
binding_writer.send(sugestion); bindings: binding,
} };
//finally send the suggestion
binding_writer.send(sugestion);
}
}
bevy_xr::actions::ActionType::Float => {
let action: openxr::Action<f32> = action_set
.create_action::<f32>(
&create_action.action_name,
&create_action.localized_name,
&[],
)
.unwrap();
//please put this in a function so I dont go crazy
//TODO remove this crap
test.action = Some(action.clone());
//since we need actions for bindings lets go!!
for &bind in bindings.iter() {
//interaction profile
//get the binding entity and stuff
let create_binding = bindings_query.get(bind).unwrap();
let profile = Cow::from(create_binding.profile.clone());
//bindings
let binding = vec![Cow::from(create_binding.binding.clone())];
let sugestion = OxrSuggestActionBinding {
action: action.as_raw(),
interaction_profile: profile,
bindings: binding,
};
//finally send the suggestion
binding_writer.send(sugestion);
}
}
bevy_xr::actions::ActionType::Vector => {
let action: openxr::Action<Vector2f> = action_set
.create_action::<Vector2f>(
&create_action.action_name,
&create_action.localized_name,
&[],
)
.unwrap();
//please put this in a function so I dont go crazy
//TODO remove this crap
// test.action = Some(action.clone());
//since we need actions for bindings lets go!!
for &bind in bindings.iter() {
//interaction profile
//get the binding entity and stuff
let create_binding = bindings_query.get(bind).unwrap();
let profile = Cow::from(create_binding.profile.clone());
//bindings
let binding = vec![Cow::from(create_binding.binding.clone())];
let sugestion = OxrSuggestActionBinding {
action: action.as_raw(),
interaction_profile: profile,
bindings: binding,
};
//finally send the suggestion
binding_writer.send(sugestion);
}
}
};
} }
attach_writer.send(OxrAttachActionSet(action_set)); attach_writer.send(OxrAttachActionSet(action_set));