finnish everything

This commit is contained in:
Schmarni
2023-11-14 02:32:57 +01:00
parent 74e4e0c3b7
commit e5363acac9
10 changed files with 97 additions and 74 deletions

View File

@@ -4,6 +4,8 @@ use xr::{Action, Binding, Haptic, Posef};
use crate::resources::{XrInstance, XrSession};
use super::oculus_touch::ActionSets;
pub struct OpenXrActionsPlugin;
impl Plugin for OpenXrActionsPlugin {
fn build(&self, app: &mut App) {
@@ -22,103 +24,123 @@ pub fn setup_oxr_actions(world: &mut World) {
let right_path = instance.string_to_path("/user/hand/right").unwrap();
let hands = [left_path, right_path];
let mut action_sets = ActionSets { sets: default() };
let mut action_bindings: HashMap<&'static str, Vec<xr::Path>> = HashMap::new();
let mut oxr_action_sets = Vec::new();
let mut action_sets = XrActionSets { sets: default() };
// let mut action_bindings: HashMap<&'static str, Vec<xr::Path>> = HashMap::new();
let mut a_iter = actions.sets.into_iter();
let mut action_bindings: HashMap<
(&'static str, &'static str),
HashMap<&'static str, Vec<xr::Path>>,
> = HashMap::new();
while let Some((set_name, set)) = a_iter.next() {
let mut actions: HashMap<&'static str, TypedAction> = default();
let oxr_action_set = instance
.create_action_set(set_name, set.pretty_name, set.priority)
.unwrap();
.expect("Unable to create action set");
for (action_name, action) in set.actions.into_iter() {
let typed_action = match action.action_type {
ActionType::F32 => TypedAction::F32(match action.handednes {
ActionHandednes::Single => oxr_action_set
.create_action(action_name, action.pretty_name, &[])
.unwrap(),
.expect(&format!("Unable to create action: {}", action_name)),
ActionHandednes::Double => oxr_action_set
.create_action(action_name, action.pretty_name, &hands)
.unwrap(),
.expect(&format!("Unable to create action: {}", action_name)),
}),
ActionType::Bool => TypedAction::Bool(match action.handednes {
ActionHandednes::Single => oxr_action_set
.create_action(action_name, action.pretty_name, &[])
.expect(action.pretty_name),
.expect(&format!("Unable to create action: {}", action_name)),
ActionHandednes::Double => oxr_action_set
.create_action(action_name, action.pretty_name, &hands)
.unwrap(),
.expect(&format!("Unable to create action: {}", action_name)),
}),
ActionType::PoseF => TypedAction::PoseF(match action.handednes {
ActionHandednes::Single => oxr_action_set
.create_action(action_name, action.pretty_name, &[])
.unwrap(),
.expect(&format!("Unable to create action: {}", action_name)),
ActionHandednes::Double => oxr_action_set
.create_action(action_name, action.pretty_name, &hands)
.unwrap(),
.expect(&format!("Unable to create action: {}", action_name)),
}),
ActionType::Haptic => TypedAction::Haptic(match action.handednes {
ActionHandednes::Single => oxr_action_set
.create_action(action_name, action.pretty_name, &[])
.unwrap(),
.expect(&format!("Unable to create action: {}", action_name)),
ActionHandednes::Double => oxr_action_set
.create_action(action_name, action.pretty_name, &hands)
.unwrap(),
.expect(&format!("Unable to create action: {}", action_name)),
}),
};
actions.insert(action_name, typed_action);
for (device_path, bindings) in action.bindings.into_iter() {
for b in bindings {
info!("binding {} to {}", action_name, b);
action_bindings
.entry((set_name, action_name))
.or_default()
.entry(device_path)
.or_default()
.push(instance.string_to_path(b).unwrap());
}
}
}
oxr_action_sets.push(oxr_action_set);
action_sets.sets.insert(
set_name,
ActionSet {
oxr_action_set,
// oxr_action_set,
actions,
enabled: true,
},
);
}
for (dev, bindings) in action_sets
let mut b_indings: HashMap<&'static str, Vec<Binding>> = HashMap::new();
for (dev, mut bindings) in action_sets
.sets
.iter()
.flat_map(|(_, set)| set.actions.iter().map(|(_, a)| a))
.zip(action_bindings.into_iter())
.map(|(action, (dev, bindings))| {
.flat_map(|(set_name, set)| {
set.actions
.iter()
.map(move |(action_name, a)| (set_name, action_name, a))
})
.zip([&action_bindings].into_iter().cycle())
.flat_map(move |((set_name, action_name, action), bindings)| {
bindings
.get(&(set_name.clone(), action_name.clone()))
.unwrap()
.iter()
.map(move |(dev, bindings)| (action.clone(), dev.clone(), bindings))
})
.map(|(action, dev, bindings)| {
info!("Hi");
(
dev,
bindings
.into_iter()
.map(move |binding| match &action {
TypedAction::F32(a) => Binding::new(a, binding),
TypedAction::Bool(a) => Binding::new(a, binding),
TypedAction::PoseF(a) => Binding::new(a, binding),
TypedAction::Haptic(a) => Binding::new(a, binding),
TypedAction::F32(a) => Binding::new(a, *binding),
TypedAction::Bool(a) => Binding::new(a, *binding),
TypedAction::PoseF(a) => Binding::new(a, *binding),
TypedAction::Haptic(a) => Binding::new(a, *binding),
})
.collect::<Vec<_>>(),
)
})
{
b_indings.entry(dev).or_default().append(&mut bindings);
}
for (dev, bindings) in b_indings.into_iter() {
info!(dev);
instance
.suggest_interaction_profile_bindings(instance.string_to_path(dev).unwrap(), &bindings)
.unwrap();
.expect("Unable to suggest interaction bindings!");
}
session
.attach_action_sets(
&action_sets
.sets
.iter()
.map(|(_, set)| &set.oxr_action_set)
.collect::<Vec<_>>(),
)
.unwrap();
.attach_action_sets(&oxr_action_sets.iter().collect::<Vec<_>>())
.expect("Unable to attach action sets!");
world.insert_resource(ActionSets(oxr_action_sets));
world.insert_resource(action_sets);
}
@@ -224,17 +246,17 @@ impl SetupActionSets {
}
pub struct ActionSet {
oxr_action_set: xr::ActionSet,
// oxr_action_set: xr::ActionSet,
enabled: bool,
actions: HashMap<&'static str, TypedAction>,
}
#[derive(Resource)]
pub struct ActionSets {
pub struct XrActionSets {
sets: HashMap<&'static str, ActionSet>,
}
impl ActionSets {
impl XrActionSets {
pub fn get_action_f32(
&self,
action_set: &'static str,