generated from Leafwing-Studios/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace axis-like input configuration with new input processors (#494)
* Add input processors and input settings * Replace all usage * RELEASES.md * Document * Document * Document * Document * Add more test cases * Split out deadzones into another module * Fix CI and examples * add `with_settings` * Update comment * Split axis settings into separate modules * Update the documentation * typo * RELEASES.md * fix ci * fix RELEASES.md * fix documentation * fix documentation * Use new implementation * Add comments * Add an example * Fix warnings in examples * Remove compilation configuration * Doc typo * Doc typo * Fix hashing * Simplify * Fix macros * Improve docs * Improve docs * Improve * Rename macros.rs * Prefer import from bevy::prelude * Improve macro * Improve docs * Improve docs * Improve docs and fix CI * Rename Square* to DualAxis* * Split dual_axis.rs * Update module docs * Rename `with_processor` to `replace_processor`, add `with_processor` * Improve docs * Improve docs * Rearrange the order of match arms * Improve docs * Typo * Expand macros and refine the results * Rearrange dual-axis input processors * Rearrange processors * Remove macros.rs * Switch to `serde_flexitos` * Add tests * Minor * Improve docs * Inline doc alias * Typo * Add missing tests * Cleanup * Cleanup
- Loading branch information
Showing
39 changed files
with
4,585 additions
and
654 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use bevy::prelude::*; | ||
use leafwing_input_manager::prelude::*; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins) | ||
.add_plugins(InputManagerPlugin::<Action>::default()) | ||
.add_systems(Startup, spawn_player) | ||
.add_systems(Update, check_data) | ||
.run(); | ||
} | ||
|
||
#[derive(Actionlike, PartialEq, Eq, Clone, Copy, Hash, Debug, Reflect)] | ||
enum Action { | ||
Move, | ||
LookAround, | ||
} | ||
|
||
#[derive(Component)] | ||
struct Player; | ||
|
||
fn spawn_player(mut commands: Commands) { | ||
let mut input_map = InputMap::default(); | ||
input_map | ||
.insert( | ||
Action::Move, | ||
VirtualDPad::wasd() | ||
// You can add a processor to handle axis-like user inputs by using the `with_processor`. | ||
// | ||
// This processor is a circular deadzone that normalizes input values | ||
// by clamping their magnitude to a maximum of 1.0, | ||
// excluding those with a magnitude less than 0.1, | ||
// and scaling other values linearly in between. | ||
.with_processor(CircleDeadZone::new(0.1)) | ||
// Followed by appending Y-axis inversion for the next processing step. | ||
.with_processor(DualAxisInverted::ONLY_Y), | ||
) | ||
.insert( | ||
Action::Move, | ||
DualAxis::left_stick() | ||
// You can replace the currently used processor with another processor. | ||
.replace_processor(CircleDeadZone::default()) | ||
// Or remove the processor directly, leaving no processor applied. | ||
.no_processor(), | ||
) | ||
.insert( | ||
Action::LookAround, | ||
// You can also add a pipeline to handle axis-like user inputs. | ||
DualAxis::mouse_motion().with_processor( | ||
DualAxisProcessingPipeline::default() | ||
// The first processor is a circular deadzone. | ||
.with(CircleDeadZone::new(0.1)) | ||
// The next processor doubles inputs normalized by the deadzone. | ||
.with(DualAxisSensitivity::all(2.0)), | ||
), | ||
); | ||
commands | ||
.spawn(InputManagerBundle::with_map(input_map)) | ||
.insert(Player); | ||
} | ||
|
||
fn check_data(query: Query<&ActionState<Action>, With<Player>>) { | ||
let action_state = query.single(); | ||
for action in action_state.get_pressed() { | ||
println!( | ||
"Pressed {action:?}! Its data: {:?}", | ||
action_state.axis_pair(&action) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::{Error, ItemImpl, Type, TypePath}; | ||
|
||
use crate::utils; | ||
|
||
/// This approach and implementation is inspired by the `typetag` crate, | ||
/// Copyright (c) 2019 David Tolnay | ||
/// available under either of `Apache License, Version 2.0` or `MIT` license | ||
/// at <https://github.com/dtolnay/typetag> | ||
pub(crate) fn expand_serde_typetag(input: &ItemImpl) -> TokenStream { | ||
let Some(trait_) = &input.trait_ else { | ||
let impl_token = input.impl_token; | ||
let ty = &input.self_ty; | ||
let span = quote!(#impl_token, #ty); | ||
let msg = "expected impl Trait for Type"; | ||
return Error::new_spanned(span, msg).to_compile_error(); | ||
}; | ||
|
||
let trait_path = &trait_.1; | ||
|
||
let self_ty = input.self_ty.clone(); | ||
|
||
let ident = match type_name(&self_ty) { | ||
Some(name) => quote!(#name), | ||
None => { | ||
let impl_token = input.impl_token; | ||
let ty = &input.self_ty; | ||
let span = quote!(#impl_token, #ty); | ||
let msg = "expected explicit name for Type"; | ||
return Error::new_spanned(span, msg).to_compile_error(); | ||
} | ||
}; | ||
|
||
let crate_path = utils::crate_path(); | ||
|
||
quote! { | ||
#input | ||
|
||
impl<'de> #crate_path::typetag::RegisterTypeTag<'de, dyn #trait_path> for #self_ty { | ||
fn register_typetag( | ||
registry: &mut #crate_path::typetag::MapRegistry<dyn #trait_path>, | ||
) { | ||
#crate_path::typetag::Registry::register( | ||
registry, | ||
#ident, | ||
|de| Ok(::std::boxed::Box::new( | ||
::bevy::reflect::erased_serde::deserialize::<#self_ty>(de)?, | ||
)), | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn type_name(mut ty: &Type) -> Option<String> { | ||
loop { | ||
match ty { | ||
Type::Group(group) => { | ||
ty = &group.elem; | ||
} | ||
Type::Path(TypePath { qself, path }) if qself.is_none() => { | ||
return Some(path.segments.last().unwrap().ident.to_string()) | ||
} | ||
_ => return None, | ||
} | ||
} | ||
} |
Oops, something went wrong.