generated from Leafwing-Studios/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace axis-like input configuration with new input processors #494
Merged
alice-i-cecile
merged 64 commits into
Leafwing-Studios:main
from
Shute052:input-processors
Apr 12, 2024
Merged
Changes from all commits
Commits
Show all changes
64 commits
Select commit
Hold shift + click to select a range
6b5b344
Add input processors and input settings
Shute052 88f7a7a
Merge branch 'main' into input-processors
Shute052 fd18dcb
Replace all usage
Shute052 91f6e4e
RELEASES.md
Shute052 08a6acf
Document
Shute052 d7ac626
Document
Shute052 03e54fa
Document
Shute052 714c296
Document
Shute052 09579bb
Add more test cases
Shute052 f58953d
Split out deadzones into another module
Shute052 491c38f
Fix CI and examples
Shute052 789017c
add `with_settings`
Shute052 cfd4afd
Update comment
Shute052 4c44577
Split axis settings into separate modules
Shute052 a3ccf29
Update the documentation
Shute052 f8a3d4d
typo
Shute052 f4468cc
RELEASES.md
Shute052 324e76e
fix ci
Shute052 ed9b72e
fix RELEASES.md
Shute052 6b8d445
fix documentation
Shute052 1eb50b2
fix documentation
Shute052 34de561
Use new implementation
Shute052 9066278
Add comments
Shute052 cf860d7
Merge branch 'main' into input-processors
Shute052 fed7811
Add an example
Shute052 e9810ef
Fix warnings in examples
Shute052 f572244
Remove compilation configuration
Shute052 3513305
Doc typo
Shute052 b181a30
Doc typo
Shute052 d4a4f35
Fix hashing
Shute052 070e815
Simplify
Shute052 a191dd4
Fix macros
Shute052 3623571
Merge branch 'main' into input-processors
Shute052 3d8d7ad
Improve docs
Shute052 84ad6ef
Improve docs
Shute052 ee10149
Improve
Shute052 b130c8a
Rename macros.rs
Shute052 64f23ce
Prefer import from bevy::prelude
Shute052 feefdf1
Improve macro
Shute052 25011c8
Improve docs
Shute052 f7808aa
Improve docs
Shute052 6b08029
Improve docs and fix CI
Shute052 bc63f18
Rename Square* to DualAxis*
Shute052 729948f
Split dual_axis.rs
Shute052 a10d675
Update module docs
Shute052 c700dae
Rename `with_processor` to `replace_processor`, add `with_processor`
Shute052 e79f5d2
Improve docs
Shute052 48a8929
Improve docs
Shute052 5a9dc5a
Rearrange the order of match arms
Shute052 2bacf98
Improve docs
Shute052 953a935
Typo
Shute052 f2d8cd3
Expand macros and refine the results
Shute052 a444164
Rearrange dual-axis input processors
Shute052 4a8afa0
Rearrange processors
Shute052 de7ca2b
Remove macros.rs
Shute052 531cf51
Switch to `serde_flexitos`
Shute052 ff18aa4
Add tests
Shute052 d614f7c
Minor
Shute052 5c1821c
Improve docs
Shute052 5a4d4de
Inline doc alias
Shute052 1f35022
Typo
Shute052 c846c38
Add missing tests
Shute052 8a9c864
Cleanup
Shute052 9fe0a6d
Cleanup
Shute052 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
//! Note that [`ActionState`] can also be serialized and sent directly. | ||
//! This approach will be less bandwidth efficient, but involve less complexity and CPU work. | ||
|
||
use bevy::ecs::event::{Events, ManualEventReader}; | ||
use bevy::ecs::event::ManualEventReader; | ||
use bevy::input::InputPlugin; | ||
use bevy::prelude::*; | ||
use leafwing_input_manager::action_diff::ActionDiffEvent; | ||
|
@@ -23,10 +23,6 @@ enum FpsAction { | |
Shoot, | ||
} | ||
|
||
/// This identifier uniquely identifies entities across the network | ||
#[derive(Component, Clone, PartialEq, Eq, Hash, Debug)] | ||
struct StableId(u64); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This cleanup is good, but not particularly related. Whatever, it can stay. |
||
|
||
/// Processes an [`Events`] stream of [`ActionDiff`] to update an [`ActionState`] | ||
/// | ||
/// In a real scenario, you would have to map the entities between the server and client world. | ||
|
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make
action_data
andaction_data_mut
infallible again, and remove this. Seperate PR though; I don't care whether it's before or after this PR is merged.