Create structs for the input actions
ci/woodpecker/push/build Pipeline was successful
Details
ci/woodpecker/push/build Pipeline was successful
Details
This commit is contained in:
parent
76f81d6b02
commit
75c0377d9c
|
@ -1,4 +1,4 @@
|
||||||
use lyra_engine::{math::{self, Vec3}, ecs::{World, components::{transform::TransformComponent, camera::CameraComponent, model::ModelComponent, DeltaTime}, EventQueue, SimpleSystem, Component, Criteria, CriteriaSchedule, BatchedSystem}, math::Transform, input::{KeyCode, InputButtons, MouseMotion}, game::Game, plugin::Plugin, render::window::{CursorGrabMode, WindowOptions}, change_tracker::Ct};
|
use lyra_engine::{math::{self, Vec3}, ecs::{World, components::{transform::TransformComponent, camera::CameraComponent, model::ModelComponent, DeltaTime}, EventQueue, SimpleSystem, Component, Criteria, CriteriaSchedule, BatchedSystem}, math::Transform, input::{KeyCode, InputButtons, MouseMotion, ActionHandler, Layout, Action, ActionKind, LayoutId, ActionMapping, Binding, ActionSource, ActionMappingId}, game::Game, plugin::Plugin, render::window::{CursorGrabMode, WindowOptions}, change_tracker::Ct};
|
||||||
use lyra_engine::assets::{ResourceManager, Model};
|
use lyra_engine::assets::{ResourceManager, Model};
|
||||||
|
|
||||||
mod free_fly_camera;
|
mod free_fly_camera;
|
||||||
|
@ -58,6 +58,22 @@ struct TpsAccumulator(f32);
|
||||||
|
|
||||||
#[async_std::main]
|
#[async_std::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
let _action_handler = ActionHandler::new()
|
||||||
|
.add_layout(LayoutId::from(0))
|
||||||
|
.add_action("forward_backward", Action::new(ActionKind::Button))
|
||||||
|
.add_action("left_right", Action::new(ActionKind::Button))
|
||||||
|
.add_mapping(ActionMapping::new(LayoutId::from(0), ActionMappingId::from(0))
|
||||||
|
.bind("forward_backward", &[
|
||||||
|
ActionSource::Keyboard(KeyCode::W).into_binding_modifier(1.0),
|
||||||
|
ActionSource::Keyboard(KeyCode::S).into_binding_modifier(-1.0)
|
||||||
|
])
|
||||||
|
.bind("left_right", &[
|
||||||
|
ActionSource::Keyboard(KeyCode::A).into_binding_modifier(1.0),
|
||||||
|
ActionSource::Keyboard(KeyCode::D).into_binding_modifier(-1.0)
|
||||||
|
])
|
||||||
|
.finish()
|
||||||
|
);
|
||||||
|
|
||||||
let setup_sys = |world: &mut World| -> anyhow::Result<()> {
|
let setup_sys = |world: &mut World| -> anyhow::Result<()> {
|
||||||
{
|
{
|
||||||
let mut window_options = world.get_resource_mut::<Ct<WindowOptions>>().unwrap();
|
let mut window_options = world.get_resource_mut::<Ct<WindowOptions>>().unwrap();
|
||||||
|
|
|
@ -0,0 +1,253 @@
|
||||||
|
use std::{collections::HashMap, cell::RefCell, borrow::BorrowMut};
|
||||||
|
|
||||||
|
use edict::action;
|
||||||
|
|
||||||
|
use crate::castable_any::CastableAny;
|
||||||
|
|
||||||
|
use super::{Button, KeyCode};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||||
|
enum GamepadFormat {
|
||||||
|
DualAxis,
|
||||||
|
Joystick,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||||
|
enum GamepadButton {
|
||||||
|
FaceBottom,
|
||||||
|
FaceLeft,
|
||||||
|
FaceRight,
|
||||||
|
FaceTop,
|
||||||
|
VirtualConfirm,
|
||||||
|
VirtualDeny,
|
||||||
|
LThumbstick,
|
||||||
|
RThumbstick,
|
||||||
|
DPadUp,
|
||||||
|
DPadDown,
|
||||||
|
DPadLeft,
|
||||||
|
DPadRight,
|
||||||
|
LShoulder,
|
||||||
|
RShoulder,
|
||||||
|
LTrigger,
|
||||||
|
RTrigger,
|
||||||
|
Special,
|
||||||
|
LSpecial,
|
||||||
|
RSpecial,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||||
|
enum GamepadAxis {
|
||||||
|
LThumbstickX,
|
||||||
|
LThumbstickY,
|
||||||
|
RThumbstickX,
|
||||||
|
RThumbstickY,
|
||||||
|
LTrigger,
|
||||||
|
RTrigger,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||||
|
enum GamepadInput {
|
||||||
|
Button(GamepadButton),
|
||||||
|
Axis(GamepadAxis),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||||
|
pub enum ActionSource {
|
||||||
|
Keyboard(KeyCode),
|
||||||
|
Gamepad(GamepadFormat, GamepadInput),
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||||
|
pub enum ActionKind {
|
||||||
|
Button,
|
||||||
|
Axis
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Action {
|
||||||
|
kind: ActionKind,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Action {
|
||||||
|
pub fn new(kind: ActionKind) -> Self {
|
||||||
|
Self {
|
||||||
|
kind,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||||
|
pub struct LayoutId(u32);
|
||||||
|
|
||||||
|
impl From<u32> for LayoutId {
|
||||||
|
fn from(value: u32) -> Self {
|
||||||
|
Self(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for LayoutId {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Layout {
|
||||||
|
mappings: HashMap<ActionMappingId, ActionMapping>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Layout {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
mappings: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_mapping(&mut self, mapping: ActionMapping) -> &mut Self {
|
||||||
|
self.mappings.insert(mapping.id, mapping);
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||||
|
pub struct ActionMappingId(u32);
|
||||||
|
|
||||||
|
impl From<u32> for ActionMappingId {
|
||||||
|
fn from(value: u32) -> Self {
|
||||||
|
Self(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ActionMappingId {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct ActionMapping {
|
||||||
|
layout: LayoutId,
|
||||||
|
id: ActionMappingId,
|
||||||
|
action_binds: HashMap<String, Vec<Binding>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ActionMapping {
|
||||||
|
pub fn new(layout: LayoutId, id: ActionMappingId) -> Self {
|
||||||
|
Self {
|
||||||
|
layout,
|
||||||
|
id,
|
||||||
|
action_binds: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a binding for the action.
|
||||||
|
///
|
||||||
|
/// If the action is not in this layout, this will panic!
|
||||||
|
///
|
||||||
|
/// Parameters:
|
||||||
|
/// * `action_label` - The label corresponding to the action in this Layout.
|
||||||
|
/// * `bind` - The Binding to add to the Action.
|
||||||
|
pub fn bind(mut self, action_label: &str, bindings: &[Binding]) -> Self {
|
||||||
|
let mut bindings = bindings.to_vec();
|
||||||
|
|
||||||
|
let action_binds = self.action_binds.entry(action_label.to_string())
|
||||||
|
.or_insert_with(|| Vec::new());
|
||||||
|
action_binds.append(&mut bindings);
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates multiple binding for the action.
|
||||||
|
///
|
||||||
|
/// If the action is not in this layout, this will panic!
|
||||||
|
///
|
||||||
|
/// Parameters:
|
||||||
|
/// * `action_label` - The label corresponding to the action in this Layout.
|
||||||
|
/// * `bindings` - The list of Bindings to add to the Action.
|
||||||
|
/* pub fn add_bindings(&mut self, action_label: String, bindings: &[Binding]) -> &mut Self {
|
||||||
|
let mut bindings = bindings.to_vec();
|
||||||
|
let action_binds = self.action_binds.entry(action_label)
|
||||||
|
.or_insert_with(|| Vec::new());
|
||||||
|
action_binds.append(&mut bindings);
|
||||||
|
|
||||||
|
self
|
||||||
|
} */
|
||||||
|
|
||||||
|
pub fn finish(self) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ActionHandler {
|
||||||
|
actions: HashMap<String, Action>,
|
||||||
|
layouts: HashMap<LayoutId, Layout>,
|
||||||
|
current_layout: LayoutId,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ActionHandler {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
actions: HashMap::new(),
|
||||||
|
layouts: HashMap::new(),
|
||||||
|
current_layout: LayoutId::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_layout(&mut self, id: LayoutId) -> &mut Self {
|
||||||
|
self.layouts.insert(id, Layout::new());
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_action(&mut self, label: &str, action: Action) -> &mut Self {
|
||||||
|
/* let layout = self.layouts.get_mut(&layout_id).unwrap();
|
||||||
|
layout.actions.insert(label.to_string(), action); */
|
||||||
|
self.actions.insert(label.to_string(), action);
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_mapping(&mut self, mapping: ActionMapping) -> &mut Self {
|
||||||
|
let layout = self.layouts.get_mut(&mapping.layout).unwrap();
|
||||||
|
layout.add_mapping(mapping);
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,14 +2,20 @@ use std::{hash::{Hash, Hasher}, collections::{HashMap, hash_map::DefaultHasher}}
|
||||||
|
|
||||||
use winit::event::ElementState;
|
use winit::event::ElementState;
|
||||||
|
|
||||||
|
//pub trait Button : Clone + Hash + Eq + PartialEq + 'static {}
|
||||||
|
/* pub trait Button {}
|
||||||
|
impl<T: Clone + Hash + Eq + PartialEq + 'static> Button for T {} */
|
||||||
|
|
||||||
|
pub trait Button = Clone + Hash + Eq + PartialEq + 'static;
|
||||||
|
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
|
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
|
||||||
pub enum ButtonEvent<T: Clone + Hash + Eq + PartialEq + 'static> {
|
pub enum ButtonEvent<T: Button> {
|
||||||
Pressed(T),
|
Pressed(T),
|
||||||
Released(T),
|
Released(T),
|
||||||
JustPressed(T),
|
JustPressed(T),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Clone + Hash + Eq + PartialEq + 'static> ButtonEvent<T> {
|
impl<T: Button> ButtonEvent<T> {
|
||||||
fn take_val(self) -> T {
|
fn take_val(self) -> T {
|
||||||
match self {
|
match self {
|
||||||
ButtonEvent::Pressed(t) => t,
|
ButtonEvent::Pressed(t) => t,
|
||||||
|
@ -24,19 +30,19 @@ impl<T: Clone + Hash + Eq + PartialEq + 'static> ButtonEvent<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct InputButtons<T: Clone + Hash + Eq + PartialEq + 'static> {
|
pub struct InputButtons<T: Button> {
|
||||||
// the u64 as the key is the hashed value of T. this makes it easier to
|
// the u64 as the key is the hashed value of T. this makes it easier to
|
||||||
// search for a button and see its state
|
// search for a button and see its state
|
||||||
button_events: HashMap<u64, ButtonEvent<T>>,
|
button_events: HashMap<u64, ButtonEvent<T>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Clone + Hash + Eq + PartialEq + 'static> Default for InputButtons<T> {
|
impl<T: Button> Default for InputButtons<T> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
Self::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Clone + Hash + Eq + PartialEq + 'static> InputButtons<T> {
|
impl<T: Button> InputButtons<T> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
button_events: HashMap::new(),
|
button_events: HashMap::new(),
|
||||||
|
|
|
@ -9,3 +9,6 @@ pub use events::*;
|
||||||
|
|
||||||
pub mod buttons;
|
pub mod buttons;
|
||||||
pub use buttons::*;
|
pub use buttons::*;
|
||||||
|
|
||||||
|
pub mod action;
|
||||||
|
pub use action::*;
|
|
@ -1,5 +1,6 @@
|
||||||
#![feature(hash_extract_if)]
|
#![feature(hash_extract_if)]
|
||||||
#![feature(lint_reasons)]
|
#![feature(lint_reasons)]
|
||||||
|
#![feature(trait_alias)]
|
||||||
|
|
||||||
pub mod game;
|
pub mod game;
|
||||||
pub mod render;
|
pub mod render;
|
||||||
|
|
Loading…
Reference in New Issue