2024-01-07 02:38:21 +00:00
|
|
|
use std::{collections::HashMap, ops::Deref, hash::{Hash, DefaultHasher, Hasher}, fmt::Debug};
|
2023-11-04 15:34:27 +00:00
|
|
|
|
2024-01-07 01:15:54 +00:00
|
|
|
use glam::Vec2;
|
2024-03-03 02:37:21 +00:00
|
|
|
use lyra_ecs::World;
|
2024-02-25 22:06:53 +00:00
|
|
|
use lyra_reflect::Reflect;
|
2023-11-04 15:34:27 +00:00
|
|
|
|
2024-01-07 01:15:54 +00:00
|
|
|
use crate::{plugin::Plugin, game::GameStages, EventQueue};
|
2023-11-04 15:34:27 +00:00
|
|
|
|
2024-03-09 05:34:50 +00:00
|
|
|
use super::{Button, InputButtons, KeyCode, MouseButton, MouseMotion};
|
2023-11-04 15:34:27 +00:00
|
|
|
|
2024-01-07 02:38:21 +00:00
|
|
|
pub trait ActionLabel: Debug {
|
|
|
|
/// Returns a unique hash of the label.
|
|
|
|
fn label_hash(&self) -> u64;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Hash + Debug> ActionLabel for T {
|
|
|
|
fn label_hash(&self) -> u64 {
|
|
|
|
let mut s = DefaultHasher::new();
|
|
|
|
self.hash(&mut s);
|
|
|
|
s.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ActionLabelWrapper(String, u64);
|
|
|
|
|
|
|
|
impl Debug for ActionLabelWrapper {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.write_str(&self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ActionLabel for ActionLabelWrapper {
|
|
|
|
fn label_hash(&self) -> u64 {
|
|
|
|
self.1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: ActionLabel + Hash> From<A> for ActionLabelWrapper {
|
|
|
|
fn from(value: A) -> Self {
|
|
|
|
let lbl = format!("{:?}", value);
|
|
|
|
Self(lbl, value.label_hash())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Some commonly used action labels.
|
|
|
|
///
|
|
|
|
/// The built-in systems uses these labels
|
|
|
|
#[derive(Clone, Copy, Hash, Debug)]
|
|
|
|
pub enum CommonActionLabel {
|
|
|
|
MoveForwardBackward,
|
|
|
|
MoveLeftRight,
|
|
|
|
MoveUpDown,
|
|
|
|
LookLeftRight,
|
|
|
|
LookUpDown,
|
|
|
|
LookRoll,
|
|
|
|
}
|
|
|
|
|
2023-12-28 03:53:58 +00:00
|
|
|
#[allow(dead_code)]
|
2023-11-04 15:34:27 +00:00
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
2023-12-28 03:53:58 +00:00
|
|
|
pub enum GamepadFormat {
|
2023-11-04 15:34:27 +00:00
|
|
|
DualAxis,
|
|
|
|
Joystick,
|
|
|
|
}
|
|
|
|
|
2023-12-28 03:53:58 +00:00
|
|
|
#[allow(dead_code)]
|
2023-11-04 15:34:27 +00:00
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
2023-12-28 03:53:58 +00:00
|
|
|
pub enum GamepadButton {
|
2023-11-04 15:34:27 +00:00
|
|
|
FaceBottom,
|
|
|
|
FaceLeft,
|
|
|
|
FaceRight,
|
|
|
|
FaceTop,
|
|
|
|
VirtualConfirm,
|
|
|
|
VirtualDeny,
|
|
|
|
LThumbstick,
|
|
|
|
RThumbstick,
|
|
|
|
DPadUp,
|
|
|
|
DPadDown,
|
|
|
|
DPadLeft,
|
|
|
|
DPadRight,
|
|
|
|
LShoulder,
|
|
|
|
RShoulder,
|
|
|
|
LTrigger,
|
|
|
|
RTrigger,
|
|
|
|
Special,
|
|
|
|
LSpecial,
|
|
|
|
RSpecial,
|
|
|
|
}
|
|
|
|
|
2023-12-28 03:53:58 +00:00
|
|
|
#[allow(dead_code)]
|
2023-11-04 15:34:27 +00:00
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
2023-12-28 03:53:58 +00:00
|
|
|
pub enum GamepadAxis {
|
2023-11-04 15:34:27 +00:00
|
|
|
LThumbstickX,
|
|
|
|
LThumbstickY,
|
|
|
|
RThumbstickX,
|
|
|
|
RThumbstickY,
|
|
|
|
LTrigger,
|
|
|
|
RTrigger,
|
|
|
|
}
|
|
|
|
|
2023-12-28 03:53:58 +00:00
|
|
|
#[allow(dead_code)]
|
2023-11-04 15:34:27 +00:00
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
2023-12-28 03:53:58 +00:00
|
|
|
pub enum GamepadInput {
|
2023-11-04 15:34:27 +00:00
|
|
|
Button(GamepadButton),
|
|
|
|
Axis(GamepadAxis),
|
|
|
|
}
|
|
|
|
|
2024-01-07 01:15:54 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
|
|
|
pub enum MouseAxis {
|
|
|
|
X,
|
|
|
|
Y,
|
|
|
|
ScrollWheel,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
|
|
|
pub enum MouseInput {
|
|
|
|
Button(MouseButton),
|
|
|
|
Axis(MouseAxis),
|
|
|
|
}
|
|
|
|
|
2023-12-28 03:53:58 +00:00
|
|
|
#[allow(dead_code)]
|
2023-11-04 15:34:27 +00:00
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
|
|
|
pub enum ActionSource {
|
|
|
|
Keyboard(KeyCode),
|
|
|
|
Gamepad(GamepadFormat, GamepadInput),
|
2024-01-07 01:15:54 +00:00
|
|
|
Mouse(MouseInput)
|
2023-11-04 15:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ActionSource {
|
|
|
|
/// Convert this Source into a Binding. Uses a default Binding modifier value of `1.0`.
|
|
|
|
pub fn into_binding(self) -> Binding {
|
|
|
|
Binding::from_source(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert this Source into a Binding, setting the modifier.
|
|
|
|
pub fn into_binding_modifier(self, modifier: f32) -> Binding {
|
|
|
|
Binding::from_source_modifier(self, modifier)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Binding {
|
|
|
|
pub source: ActionSource,
|
|
|
|
pub modifier: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Binding {
|
|
|
|
/// Create a binding from a Source. Uses a default value of `1.0` as the modifier.
|
|
|
|
pub fn from_source(source: ActionSource) -> Self {
|
2024-01-07 01:15:54 +00:00
|
|
|
// TODO: Some types of input will have a default modifier, mainly axis'
|
2023-11-04 15:34:27 +00:00
|
|
|
Self {
|
|
|
|
source,
|
|
|
|
modifier: 1.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a binding from a Source, with a modifier
|
|
|
|
pub fn from_source_modifier(source: ActionSource, modifier: f32) -> Self {
|
|
|
|
Self {
|
|
|
|
source,
|
|
|
|
modifier,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-06 03:50:57 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
pub enum ActionState {
|
|
|
|
/// No input is being given by the user for the button/axis.
|
|
|
|
Idle,
|
|
|
|
/// The button is pressed
|
|
|
|
Pressed(f32),
|
|
|
|
/// The button was just pressed
|
|
|
|
JustPressed(f32),
|
|
|
|
/// The button was just released
|
|
|
|
JustReleased,
|
|
|
|
|
|
|
|
//Released,
|
|
|
|
/// The axis moved
|
|
|
|
Axis(f32),
|
|
|
|
/// Some other state with a modifier
|
|
|
|
Other(f32),
|
|
|
|
}
|
|
|
|
|
2023-11-04 15:34:27 +00:00
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
|
|
|
pub enum ActionKind {
|
|
|
|
Button,
|
|
|
|
Axis
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Action {
|
2023-11-06 03:50:57 +00:00
|
|
|
pub kind: ActionKind,
|
|
|
|
pub state: ActionState,
|
2023-11-04 15:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Action {
|
|
|
|
pub fn new(kind: ActionKind) -> Self {
|
|
|
|
Self {
|
|
|
|
kind,
|
2023-11-06 03:50:57 +00:00
|
|
|
state: ActionState::Idle,
|
2023-11-04 15:34:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 03:53:58 +00:00
|
|
|
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
2024-02-25 22:06:53 +00:00
|
|
|
pub struct LayoutId(pub u32);
|
2023-11-04 15:34:27 +00:00
|
|
|
|
|
|
|
impl From<u32> for LayoutId {
|
|
|
|
fn from(value: u32) -> Self {
|
|
|
|
Self(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 03:53:58 +00:00
|
|
|
#[derive(Clone, Default)]
|
2023-11-04 15:34:27 +00:00
|
|
|
pub struct Layout {
|
|
|
|
mappings: HashMap<ActionMappingId, ActionMapping>,
|
2023-11-06 03:50:57 +00:00
|
|
|
active_mapping: ActionMappingId,
|
2023-11-04 15:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Layout {
|
|
|
|
pub fn new() -> Self {
|
2023-12-28 03:53:58 +00:00
|
|
|
Self::default()
|
2023-11-04 15:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_mapping(&mut self, mapping: ActionMapping) -> &mut Self {
|
|
|
|
self.mappings.insert(mapping.id, mapping);
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 03:53:58 +00:00
|
|
|
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
2024-02-25 22:06:53 +00:00
|
|
|
pub struct ActionMappingId(pub u32);
|
2023-11-04 15:34:27 +00:00
|
|
|
|
|
|
|
impl From<u32> for ActionMappingId {
|
|
|
|
fn from(value: u32) -> Self {
|
|
|
|
Self(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ActionMapping {
|
|
|
|
layout: LayoutId,
|
|
|
|
id: ActionMappingId,
|
2024-01-07 02:38:21 +00:00
|
|
|
action_binds: HashMap<u64, Vec<Binding>>,
|
2023-11-04 15:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ActionMapping {
|
|
|
|
pub fn new(layout: LayoutId, id: ActionMappingId) -> Self {
|
|
|
|
Self {
|
|
|
|
layout,
|
|
|
|
id,
|
|
|
|
action_binds: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 22:06:53 +00:00
|
|
|
pub fn builder(layout: LayoutId, id: ActionMappingId) -> ActionMappingBuilder {
|
|
|
|
ActionMappingBuilder::new(ActionMapping::new(layout, id))
|
|
|
|
}
|
|
|
|
|
2023-11-04 15:34:27 +00:00
|
|
|
/// Creates a binding for the action.
|
|
|
|
///
|
|
|
|
/// If the action is not in this layout, this will panic!
|
|
|
|
///
|
|
|
|
/// Parameters:
|
2024-01-07 02:38:21 +00:00
|
|
|
/// * `action` - The label corresponding to the action in this Layout.
|
2023-11-04 15:34:27 +00:00
|
|
|
/// * `bind` - The Binding to add to the Action.
|
2024-02-25 22:06:53 +00:00
|
|
|
pub fn bind<L>(&mut self, action: L, bindings: &[Binding]) -> &mut Self
|
2024-01-07 02:38:21 +00:00
|
|
|
where
|
|
|
|
L: ActionLabel
|
|
|
|
{
|
2023-11-04 15:34:27 +00:00
|
|
|
let mut bindings = bindings.to_vec();
|
|
|
|
|
2024-01-07 02:38:21 +00:00
|
|
|
let action_binds = self.action_binds.entry(action.label_hash()).or_default();
|
2023-11-04 15:34:27 +00:00
|
|
|
action_binds.append(&mut bindings);
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-02-25 22:06:53 +00:00
|
|
|
pub fn finish(self) -> Self {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ActionMappingBuilder {
|
|
|
|
mapping: ActionMapping,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ActionMappingBuilder {
|
|
|
|
fn new(mapping: ActionMapping) -> Self {
|
|
|
|
Self {
|
|
|
|
mapping,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bind<L>(mut self, action: L, bindings: &[Binding]) -> Self
|
|
|
|
where
|
|
|
|
L: ActionLabel
|
|
|
|
{
|
2023-11-04 15:34:27 +00:00
|
|
|
let mut bindings = bindings.to_vec();
|
2024-02-25 22:06:53 +00:00
|
|
|
|
|
|
|
let action_binds = self.mapping.action_binds.entry(action.label_hash()).or_default();
|
2023-11-04 15:34:27 +00:00
|
|
|
action_binds.append(&mut bindings);
|
|
|
|
|
|
|
|
self
|
2024-02-25 22:06:53 +00:00
|
|
|
}
|
2023-11-04 15:34:27 +00:00
|
|
|
|
2024-02-25 22:06:53 +00:00
|
|
|
pub fn finish(self) -> ActionMapping {
|
|
|
|
self.mapping
|
2023-11-04 15:34:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 22:06:53 +00:00
|
|
|
#[derive(Clone, Default, Reflect)]
|
2023-11-04 15:34:27 +00:00
|
|
|
pub struct ActionHandler {
|
2024-02-25 22:06:53 +00:00
|
|
|
#[reflect(skip)] // TODO: dont just skip all these
|
2024-01-07 02:38:21 +00:00
|
|
|
pub actions: HashMap<u64, Action>,
|
2024-02-25 22:06:53 +00:00
|
|
|
#[reflect(skip)]
|
2023-11-06 03:50:57 +00:00
|
|
|
pub layouts: HashMap<LayoutId, Layout>,
|
2024-02-25 22:06:53 +00:00
|
|
|
#[reflect(skip)]
|
2023-11-06 03:50:57 +00:00
|
|
|
pub current_layout: LayoutId,
|
2024-02-25 22:06:53 +00:00
|
|
|
#[reflect(skip)]
|
2023-11-06 03:50:57 +00:00
|
|
|
pub current_mapping: ActionMappingId,
|
2023-11-04 15:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ActionHandler {
|
|
|
|
pub fn new() -> Self {
|
2023-12-28 03:53:58 +00:00
|
|
|
Self::default()
|
2023-11-04 15:34:27 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 22:06:53 +00:00
|
|
|
pub fn builder() -> ActionHandlerBuilder {
|
|
|
|
ActionHandlerBuilder::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_layout(&mut self, id: LayoutId) {
|
2023-11-04 15:34:27 +00:00
|
|
|
self.layouts.insert(id, Layout::new());
|
2024-02-25 22:06:53 +00:00
|
|
|
}
|
2023-11-04 15:34:27 +00:00
|
|
|
|
2024-02-25 22:06:53 +00:00
|
|
|
pub fn action<L>(&self, label: L) -> Option<&Action>
|
|
|
|
where
|
|
|
|
L: ActionLabel
|
|
|
|
{
|
|
|
|
self.actions.get(&label.label_hash())
|
2023-11-04 15:34:27 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 22:06:53 +00:00
|
|
|
pub fn add_action<L>(&mut self, label: L, action: Action)
|
2024-01-07 02:38:21 +00:00
|
|
|
where
|
|
|
|
L: ActionLabel
|
|
|
|
{
|
|
|
|
self.actions.insert(label.label_hash(), action);
|
2023-11-04 15:34:27 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 22:06:53 +00:00
|
|
|
pub fn add_mapping(&mut self, mapping: ActionMapping) {
|
2023-11-04 15:34:27 +00:00
|
|
|
let layout = self.layouts.get_mut(&mapping.layout).unwrap();
|
|
|
|
layout.add_mapping(mapping);
|
|
|
|
}
|
2023-11-06 03:50:57 +00:00
|
|
|
|
|
|
|
/// Returns true if the action is pressed (or was just pressed).
|
|
|
|
///
|
|
|
|
/// This will panic if the action name does not correspond to an action.
|
2024-01-07 02:38:21 +00:00
|
|
|
pub fn is_action_pressed<L>(&self, action: L) -> bool
|
|
|
|
where
|
|
|
|
L: ActionLabel
|
|
|
|
{
|
|
|
|
let action = self.actions.get(&action.label_hash())
|
|
|
|
.unwrap_or_else(|| panic!("Action {action:?} was not found"));
|
2023-11-06 03:50:57 +00:00
|
|
|
|
2023-12-28 03:53:58 +00:00
|
|
|
matches!(action.state, ActionState::Pressed(_) | ActionState::JustPressed(_))
|
2023-11-06 03:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the action was just pressed.
|
|
|
|
///
|
|
|
|
/// This will panic if the action name does not correspond to an action.
|
2024-01-07 02:38:21 +00:00
|
|
|
pub fn was_action_just_pressed<L>(&self, action: L) -> bool
|
|
|
|
where
|
|
|
|
L: ActionLabel
|
|
|
|
{
|
|
|
|
let action = self.actions.get(&action.label_hash())
|
|
|
|
.unwrap_or_else(|| panic!("Action {action:?} was not found"));
|
2023-11-06 03:50:57 +00:00
|
|
|
|
2023-12-28 03:53:58 +00:00
|
|
|
matches!(action.state, ActionState::JustPressed(_))
|
2023-11-06 03:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the action was just released.
|
|
|
|
///
|
|
|
|
/// This will panic if the action name does not correspond to an action.
|
2024-01-07 02:38:21 +00:00
|
|
|
pub fn was_action_just_released<L>(&self, action: L) -> bool
|
|
|
|
where
|
|
|
|
L: ActionLabel
|
|
|
|
{
|
|
|
|
let action = self.actions.get(&action.label_hash())
|
|
|
|
.unwrap_or_else(|| panic!("Action {action:?} was not found"));
|
2023-11-06 03:50:57 +00:00
|
|
|
|
2023-12-28 03:53:58 +00:00
|
|
|
matches!(action.state, ActionState::JustReleased)
|
2023-11-06 03:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an action's state.
|
|
|
|
///
|
|
|
|
/// This will panic if the action name does not correspond to an action.
|
2024-01-07 02:38:21 +00:00
|
|
|
pub fn get_action_state<L>(&self, action: L) -> ActionState
|
|
|
|
where
|
|
|
|
L: ActionLabel
|
|
|
|
{
|
|
|
|
let action = self.actions.get(&action.label_hash())
|
|
|
|
.unwrap_or_else(|| panic!("Action {action:?} was not found"));
|
2023-11-06 03:50:57 +00:00
|
|
|
|
|
|
|
action.state
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the action's modifier if it is pressed (or was just pressed).
|
|
|
|
/// Returns `None` if the action's state is not `ActionState::Pressed` or `ActionState::JustPressed`.
|
|
|
|
///
|
|
|
|
/// This will panic if the action name does not correspond to an action.
|
2024-01-07 02:38:21 +00:00
|
|
|
pub fn get_pressed_modifier<L>(&self, action: L) -> Option<f32>
|
|
|
|
where
|
|
|
|
L: ActionLabel
|
|
|
|
{
|
|
|
|
let action = self.actions.get(&action.label_hash())
|
|
|
|
.unwrap_or_else(|| panic!("Action {action:?} was not found"));
|
2023-11-06 03:50:57 +00:00
|
|
|
|
|
|
|
match action.state {
|
|
|
|
ActionState::Pressed(v) | ActionState::JustPressed(v) => Some(v),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the action's modifier if it was just pressed.
|
|
|
|
/// Returns `None` if the action's state is not `ActionState::JustPressed`.
|
|
|
|
///
|
|
|
|
/// This will panic if the action name does not correspond to an action.
|
2024-01-07 02:38:21 +00:00
|
|
|
pub fn get_just_pressed_modifier<L>(&self, action: L) -> Option<f32>
|
|
|
|
where
|
|
|
|
L: ActionLabel
|
|
|
|
{
|
|
|
|
let action = self.actions.get(&action.label_hash())
|
|
|
|
.unwrap_or_else(|| panic!("Action {action:?} was not found"));
|
2023-11-06 03:50:57 +00:00
|
|
|
|
|
|
|
match action.state {
|
|
|
|
ActionState::JustPressed(v) => Some(v),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the action's modifier if its an updated axis.
|
|
|
|
/// Returns `None` if the action's state is not `ActionState::Axis`.
|
|
|
|
///
|
|
|
|
/// This will panic if the action name does not correspond to an action.
|
2024-01-07 02:38:21 +00:00
|
|
|
pub fn get_axis_modifier<L>(&self, action: L) -> Option<f32>
|
|
|
|
where
|
|
|
|
L: ActionLabel
|
|
|
|
{
|
|
|
|
let action = self.actions.get(&action.label_hash())
|
|
|
|
.unwrap_or_else(|| panic!("Action {action:?} was not found"));
|
2023-11-06 03:50:57 +00:00
|
|
|
|
|
|
|
match action.state {
|
|
|
|
ActionState::Axis(v) => Some(v),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 22:06:53 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct ActionHandlerBuilder {
|
|
|
|
handler: ActionHandler,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ActionHandlerBuilder {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_layout(mut self, id: LayoutId) -> Self {
|
|
|
|
self.handler.layouts.insert(id, Layout::new());
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_action<L>(mut self, label: L, action: Action) -> Self
|
|
|
|
where
|
|
|
|
L: ActionLabel
|
|
|
|
{
|
|
|
|
self.handler.actions.insert(label.label_hash(), action);
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_mapping(mut self, mapping: ActionMapping) -> Self {
|
|
|
|
let layout = self.handler.layouts.get_mut(&mapping.layout).unwrap();
|
|
|
|
layout.add_mapping(mapping);
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn finish(self) -> ActionHandler {
|
|
|
|
self.handler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-06 03:50:57 +00:00
|
|
|
fn actions_system(world: &mut World) -> anyhow::Result<()> {
|
2023-12-26 19:12:53 +00:00
|
|
|
let keys = world.try_get_resource::<InputButtons<KeyCode>>()
|
2023-11-06 03:50:57 +00:00
|
|
|
.map(|r| r.deref().clone());
|
2024-01-07 01:15:54 +00:00
|
|
|
let mouse_events = world
|
|
|
|
.try_get_resource::<EventQueue>()
|
|
|
|
.and_then(|q| q.read_events::<MouseMotion>());
|
|
|
|
//let mouse = world.try_get_resource()
|
2023-11-06 03:50:57 +00:00
|
|
|
|
2024-01-07 01:15:54 +00:00
|
|
|
// clear the states of all axises each frame
|
|
|
|
{
|
2023-12-26 19:12:53 +00:00
|
|
|
let mut handler = world.try_get_resource_mut::<ActionHandler>()
|
2023-11-06 03:50:57 +00:00
|
|
|
.expect("No Input Action handler was created in the world!");
|
|
|
|
|
|
|
|
let layout = handler.layouts.get(&handler.current_layout).expect("No active layout");
|
|
|
|
let mapping = layout.mappings.get(&layout.active_mapping).expect("No active mapping");
|
|
|
|
|
2024-01-07 02:38:21 +00:00
|
|
|
for (action, _) in mapping.action_binds.clone().iter() {
|
|
|
|
let action = handler.actions.get_mut(action).expect("Action name for binding is invalid!");
|
2024-01-07 01:15:54 +00:00
|
|
|
if action.kind == ActionKind::Axis {
|
|
|
|
action.state = ActionState::Axis(0.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 17:24:32 +00:00
|
|
|
let motion_avg = if let Some(mouse_events) = mouse_events {
|
2024-01-07 01:15:54 +00:00
|
|
|
let count = mouse_events.len();
|
|
|
|
let mut sum = Vec2::ZERO;
|
2024-03-31 17:24:32 +00:00
|
|
|
for mm in mouse_events {
|
2024-01-07 01:15:54 +00:00
|
|
|
sum += mm.delta;
|
|
|
|
}
|
|
|
|
Some(sum / count as f32)
|
|
|
|
} else { None };
|
|
|
|
|
|
|
|
let mut handler = world.try_get_resource_mut::<ActionHandler>()
|
|
|
|
.expect("No Input Action handler was created in the world!");
|
|
|
|
|
|
|
|
let layout = handler.layouts.get(&handler.current_layout).expect("No active layout");
|
|
|
|
let mapping = layout.mappings.get(&layout.active_mapping).expect("No active mapping");
|
|
|
|
|
2024-01-07 02:38:21 +00:00
|
|
|
for (action_lbl, binds) in mapping.action_binds.clone().iter() {
|
|
|
|
let action = handler.actions.get_mut(action_lbl).expect("Action name for binding is invalid!");
|
2024-01-07 01:15:54 +00:00
|
|
|
let mut new_state = None;
|
|
|
|
|
|
|
|
for bind in binds.iter() {
|
|
|
|
match bind.source {
|
|
|
|
ActionSource::Keyboard(key) => if let Some(keys) = &keys {
|
|
|
|
// JustPressed needs to be first, since is_pressed includes buttons that
|
|
|
|
// were just pressed.
|
|
|
|
match action.kind {
|
|
|
|
ActionKind::Button => {
|
|
|
|
if keys.was_just_pressed(key) {
|
|
|
|
new_state = Some(ActionState::JustPressed(bind.modifier));
|
|
|
|
} else if keys.is_pressed(key) {
|
|
|
|
new_state = Some(ActionState::Pressed(bind.modifier));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ActionKind::Axis => {
|
|
|
|
if keys.is_pressed(key) {
|
|
|
|
new_state = Some(ActionState::Axis(bind.modifier));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
ActionSource::Gamepad(_, _) => todo!(),
|
|
|
|
ActionSource::Mouse(m) => match m {
|
|
|
|
MouseInput::Button(_) => todo!(),
|
|
|
|
MouseInput::Axis(a) => if let Some(motion_avg) = motion_avg {
|
|
|
|
match a {
|
|
|
|
MouseAxis::X => {
|
|
|
|
new_state = Some(ActionState::Axis(motion_avg.x));
|
|
|
|
},
|
|
|
|
MouseAxis::Y => {
|
|
|
|
new_state = Some(ActionState::Axis(motion_avg.y));
|
|
|
|
},
|
|
|
|
MouseAxis::ScrollWheel => todo!(),
|
2023-11-06 03:50:57 +00:00
|
|
|
}
|
|
|
|
},
|
2024-01-07 01:15:54 +00:00
|
|
|
},
|
2023-11-06 03:50:57 +00:00
|
|
|
}
|
2024-01-07 01:15:54 +00:00
|
|
|
}
|
2023-11-06 03:50:57 +00:00
|
|
|
|
2024-01-07 01:15:54 +00:00
|
|
|
if let Some(new_state) = new_state {
|
|
|
|
action.state = new_state;
|
|
|
|
} else if action.kind == ActionKind::Button {
|
|
|
|
match action.state {
|
|
|
|
ActionState::Idle => {},
|
|
|
|
ActionState::JustReleased => action.state = ActionState::Idle,
|
|
|
|
_ => action.state = ActionState::JustReleased,
|
2023-11-06 03:50:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Clone, Copy)]
|
|
|
|
pub struct InputActionPlugin;
|
|
|
|
|
|
|
|
impl Plugin for InputActionPlugin {
|
|
|
|
fn setup(&self, game: &mut crate::game::Game) {
|
2024-01-06 21:49:36 +00:00
|
|
|
game.add_system_to_stage(GameStages::PreUpdate, "input_actions", actions_system, &[]);
|
2023-11-06 03:50:57 +00:00
|
|
|
}
|
2023-11-04 15:34:27 +00:00
|
|
|
}
|