Skip to content

Commit

Permalink
Minor bump to support bevy 0.14. (#9)
Browse files Browse the repository at this point in the history
* chore: Update for bevy 0.14.

* style: Run cargo fmt.
  • Loading branch information
shanecelis authored Jul 16, 2024
1 parent 5a43f8e commit 5719b11
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 134 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "bevy-input-sequence"
description = "Recognizes input sequences and send events"
version = "0.4.0"
version = "0.5.0"
edition = "2021"
authors = ["elm", "Shane Celis <[email protected]>"]
keywords = [
Expand Down Expand Up @@ -35,11 +35,11 @@ path = "examples/multiple_input.rs"


[dependencies]
bevy = { version = "0.13", default-features = false, features = [] }
bevy = { version = "0.14", default-features = false, features = [] }
trie-rs = { version = "0.4" }
keyseq = { version = "0.2.3", features = [ "bevy" ] }
keyseq = { version = "0.3.0", features = [ "bevy" ] }

[dev-dependencies]
bevy = "0.13"
bevy = "0.14"
trybuild = "1.0"
version-sync = "0.9"
37 changes: 24 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# bevy-input-sequence

This crate recognizes input sequences from keyboard and gamepad.
Detect input sequences from the keyboard or a gamepad.

# Use Cases

Expand All @@ -19,14 +19,14 @@ cargo install bevy-input-sequence
Here are some code snippets. These also run as doctests so they do a few things
differently than a regular runnable example:

- Use `MinimalPlugins` instead of `DefaultPlugins`,
- and call `app.update()` instead of `app.run()`.
- Instead of `DefaultPlugins` they use `MinimalPlugins`.
- Instead of `app.run()` they call `app.update()`.

The next section describes the runnable examples that come with the crate.

## Run a System on a Key Sequence

Runs a system whenever the user presses the key sequence `H I` or "hi" within a
Run a system whenever the user presses the key sequence `H I` or "hi" within a
time limit.

```rust
Expand Down Expand Up @@ -56,8 +56,8 @@ fn say_hello() {

## Send an Event on Key Sequence

Originally `bevy-input-sequence` always send an event. You can still do that
with the `action::send_event()`.
Originally `bevy-input-sequence` always sent an event. You can still do that
with `action::send_event()`.

```rust
use bevy::prelude::*;
Expand Down Expand Up @@ -94,7 +94,7 @@ fn check_events(mut events: EventReader<MyEvent>) {
## Send an Event on Gamepad Button Sequence

Gamepads have something that keyboards don't: identity problems. Which player
hit the button sequence may be important to know. So the systems it accepts will
hit the button sequence may be important to know. So the systems it accepts
take an input of `Gamepad`.

```rust
Expand Down Expand Up @@ -132,9 +132,9 @@ fn check_events(mut events: EventReader<MyEvent>) {
}
```

## KeySequence creation patterns
## KeySequence Creation Patterns

`KeySequence::new` now returns an implementing of `Command` instead of itself.
`KeySequence::new` now returns `KeySequenceBuilder`, which implements `Command`.
Therefore, you need to call `Commands::add` instead of `Commands::spawn`.

```rust
Expand All @@ -146,7 +146,7 @@ struct MyEvent;

fn create_key_sequence(mut commands: Commands) {
commands.add(KeySequence::new(
action::send_event(bevy::app::AppExit),
action::send_event(bevy::app::AppExit::default()),
keyseq! { ctrl-E L M }
));
}
Expand All @@ -163,15 +163,25 @@ fn create_key_sequence_and_add_it_to_an_entity(mut commands: Commands) {
keyseq! { ctrl-E L M }
));
}
```

### Advanced Creation

The `KeySequenceBuilder` requires a `&mut World` to build it. You can build it
yourself like so:

```rust
use bevy::prelude::*;
use bevy_input_sequence::prelude::*;

fn create_key_sequence_within_command(mut commands: Commands) {
commands.add(|world: &mut World| {
let builder = KeySequence::new(
action::send_event(MyEvent),
move || { info!("got it"); },
keyseq! { ctrl-E L M }
);
let key_sequence = builder.build(world);
// And then put it somewhere?
let key_sequence: KeySequence = builder.build(world);
// And then put it somewhere? It ought to go as a component.
});
}
```
Expand Down Expand Up @@ -244,6 +254,7 @@ cargo run --example run_if

| bevy-input-sequence | bevy |
|---------------------|------|
| 0.5 | 0.14 |
| 0.3 ~ 0.4.0 | 0.13 |
| 0.2 | 0.12 |
| 0.1 | 0.11 |
Expand Down
3 changes: 2 additions & 1 deletion examples/keycode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum Direction {
}

#[derive(Event, Clone, Debug)]
#[allow(dead_code)]
struct MyEvent(Direction);

fn main() {
Expand Down Expand Up @@ -49,6 +50,6 @@ fn setup(mut commands: Commands) {

fn event_listener(mut er: EventReader<MyEvent>) {
for e in er.read() {
println!("{e:?} emitted.");
println!("{:?} emitted.", e);
}
}
1 change: 1 addition & 0 deletions examples/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn main() {
}

fn setup(mut commands: Commands) {
info!("Type H I or \"hi\".");
commands.add(KeySequence::new(say_hi, keyseq! { H I }));
}

Expand Down
1 change: 1 addition & 0 deletions examples/multiple_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bevy::prelude::*;
use bevy_input_sequence::prelude::*;

#[derive(Event, Clone, Debug)]
#[allow(dead_code)]
struct MyEvent(u8, Option<Gamepad>);

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Cache the trie for reuse.
use crate::input_sequence::InputSequence;
use bevy::{ecs::system::Resource, log::info, reflect::TypePath};
use bevy::{ecs::system::Resource, reflect::TypePath};
use std::{collections::HashMap, hash::Hash};
use trie_rs::{
inc_search::{IncSearch, Position},
Expand Down
2 changes: 1 addition & 1 deletion src/input_sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ where
}
}

impl<Act, S> bevy::ecs::system::Command for InputSequenceBuilder<Act, S>
impl<Act, S> bevy::ecs::world::Command for InputSequenceBuilder<Act, S>
where
Act: Send + Sync + 'static,
S: System<Out = ()> + Send + Sync + 'static,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![doc(html_root_url = "https://docs.rs/bevy-input-sequence/0.4.0")]
#![doc(html_root_url = "https://docs.rs/bevy-input-sequence/0.5.0")]
#![doc = include_str!("../README.md")]
#![forbid(missing_docs)]

Expand Down
6 changes: 3 additions & 3 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bevy::{
app::{App, Plugin, Update},
core::FrameCount,
ecs::{
intern::Interned,
query::Added,
removal_detection::RemovedComponents,
schedule::{IntoSystemConfigs, ScheduleLabel, SystemSet},
Expand All @@ -14,7 +15,6 @@ use bevy::{
},
log::warn,
time::Time,
utils::intern::Interned,
};
use std::collections::{HashMap, VecDeque};

Expand Down Expand Up @@ -49,7 +49,7 @@ impl Plugin for InputSequencePlugin {
fn build(&self, app: &mut App) {
if self
.match_key
.unwrap_or(app.world.get_resource::<ButtonInput<KeyCode>>().is_some())
.unwrap_or(app.world().get_resource::<ButtonInput<KeyCode>>().is_some())
{
// Add key sequence.
app.init_resource::<InputSequenceCache<KeyChord, ()>>();
Expand Down Expand Up @@ -83,7 +83,7 @@ impl Plugin for InputSequencePlugin {
}

if self.match_button.unwrap_or(
app.world
app.world()
.get_resource::<ButtonInput<GamepadButton>>()
.is_some(),
) {
Expand Down
Loading

0 comments on commit 5719b11

Please sign in to comment.