more cows

This commit is contained in:
Jay Christy
2024-05-16 09:56:04 -04:00
parent db975b06f0
commit f74f701f50

View File

@@ -2,23 +2,25 @@
use std::borrow::Cow; use std::borrow::Cow;
use bevy::prelude::*; use bevy::{prelude::*, utils::hashbrown::HashMap};
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::{ActiveActionSet, Path}; use openxr::{ActionType, ActiveActionSet, Path};
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)
.add_systems(Startup, create_action)
.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_openxr_events.after(create_action_entities))
.run(); .run();
} }
@@ -103,17 +105,20 @@ fn create_action(
fn read_action( fn read_action(
session: ResMut<OxrSession>, session: ResMut<OxrSession>,
test: ResMut<TestAction>, test: ResMut<TestAction>,
attached: ResMut<AttachedActionSets> attached: ResMut<AttachedActionSets>,
) { ) {
//maybe sync before? //maybe sync before?
let why = &attached.sets.iter().map(|v| ActiveActionSet::from(v)).collect::<Vec<_>>(); let why = &attached
.sets
.iter()
.map(|v| ActiveActionSet::from(v))
.collect::<Vec<_>>();
let sync = session.sync_actions(&why[..]); let sync = session.sync_actions(&why[..]);
match sync { match sync {
Ok(_) =>info!("sync ok"), Ok(_) => info!("sync ok"),
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 {
@@ -128,4 +133,112 @@ fn read_action(
} }
None => info!("no action"), None => info!("no action"),
} }
} }
#[derive(Component)]
struct CreateActionSet {
name: Cow<'static, str>,
pretty_name: Cow<'static, str>,
priority: u32,
}
#[derive(Component)]
struct CreateAction {
action_name: Cow<'static, str>,
localized_name: Cow<'static, str>,
action_type: ActionType,
}
#[derive(Component)]
struct CreateBinding {
profile: Cow<'static, str>,
binding: Cow<'static, str>,
}
fn create_action_entities(mut commands: Commands) {
//create a set
let set = commands
.spawn(CreateActionSet {
name: "test".into(),
pretty_name: "pretty test".into(),
priority: u32::MIN,
})
.id();
//create an action
let action = commands
.spawn(CreateAction {
action_name: "action_name".into(),
localized_name: "localized_name".into(),
action_type: ActionType::BOOLEAN_INPUT,
})
.id();
//create a binding
let binding = commands
.spawn(CreateBinding {
profile: "/interaction_profiles/valve/index_controller".into(),
binding: "/user/hand/right/input/a/click".into(),
})
.id();
//add action to set, this isnt the best
//TODO look into a better system
commands.entity(action).add_child(binding);
commands.entity(set).add_child(action);
}
fn create_openxr_events(
ActionSetsQuery: Query<(&CreateActionSet, &Children)>,
ActionsQuery: Query<(&CreateAction, &Children)>,
BindingsQuery: Query<&CreateBinding>,
instance: ResMut<OxrInstance>,
mut binding_writer: EventWriter<OxrSuggestActionBinding>,
mut attach_writer: EventWriter<OxrAttachActionSet>,
//please remove this
mut test: ResMut<TestAction>,
) {
//lets create some sets!
//we gonna need a collection of these sets for later
// let mut ActionSets = HashMap::new();
for (set, children) in ActionSetsQuery.iter() {
//create action set
let action_set: openxr::ActionSet = instance
.create_action_set(&set.name, &set.pretty_name, set.priority)
.unwrap();
// ActionSets.insert(set.name.clone(), action_set);
//since the actions are made from the sets lets go
for &child in children.iter() {
//first get the action entity and stuff
let (create_action, bindings) = ActionsQuery.get(child).unwrap();
//lets create dat actions
let bool_action: openxr::Action<bool> = action_set
.create_action::<bool>(
&create_action.action_name,
&create_action.localized_name,
&[],
)
.unwrap();
//TODO remove this crap
test.action = Some(bool_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 = BindingsQuery.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: bool_action.as_raw(),
interaction_profile: profile,
bindings: binding,
};
//finally send the suggestion
binding_writer.send(sugestion);
}
}
attach_writer.send(OxrAttachActionSet(action_set));
}
}