Skip to content

Commit

Permalink
Merge pull request #22 from bugadani/input
Browse files Browse the repository at this point in the history
Add simulator input adapter
  • Loading branch information
bugadani authored Aug 22, 2023
2 parents 23ac1aa + e9d4ca7 commit af7627d
Show file tree
Hide file tree
Showing 17 changed files with 208 additions and 259 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ jobs:
- name: Run tests
run: |
cargo test --lib --verbose
cargo test --lib --verbose --no-default-features
- name: Check examples
run: cargo check --examples --verbose
run: |
cargo check --examples --verbose
cargo check --examples --verbose --features=simulator
docs:
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Unreleased (??)
- Reworked input handling. `InteractionController` has been replaced by `InputAdapter`
- Added more options for `InteractionType`
- Added `selection_indicator::invert::Invert`
- Renamed `MenuStyle::with_interaction_controller` to `with_input_adapter`
- Added the `simulator` feature and the `interaction::simulator::Simulator` input adapter

0.3.1 (2023-08-06)
==================
Expand Down
37 changes: 37 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,45 @@ embedded-text = "0.6.0"

embedded-menu-macros = { version = "0.2.0", path = "embedded-menu-macros" }

embedded-graphics-simulator = { version = "0.5.0", optional = true }

[features]
simulator = ["embedded-graphics-simulator"]

[dev-dependencies]
embedded-graphics-simulator = "0.5.0"

[workspace]
members = [".", "embedded-menu-macros"]

[[example]]
name = "font"
required-features = ["simulator"]

[[example]]
name = "macros"
required-features = ["simulator"]

[[example]]
name = "no_title"
required-features = ["simulator"]

[[example]]
name = "owned"
required-features = ["simulator"]

[[example]]
name = "save_state"
required-features = ["simulator"]

[[example]]
name = "scrolling"
required-features = ["simulator"]

[[example]]
name = "scrolling_slice"
required-features = ["simulator"]

[[example]]
name = "small"
required-features = ["simulator"]
51 changes: 0 additions & 51 deletions examples/details_dev.rs

This file was deleted.

24 changes: 8 additions & 16 deletions examples/font.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Run using `cargo run --example simple --target x86_64-pc-windows-msvc`
//! Run using `cargo run --example simple --target x86_64-pc-windows-msvc` --features=simulator
//!
//! Navigate using up/down arrows, interact using the Enter key

Expand All @@ -9,11 +9,10 @@ use embedded_graphics::{
Drawable,
};
use embedded_graphics_simulator::{
sdl2::Keycode, BinaryColorTheme, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent,
Window,
BinaryColorTheme, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
use embedded_menu::{
interaction::InteractionType,
interaction::simulator::Simulator,
items::{select::SelectValue, NavigationItem, Select},
Menu, MenuStyle,
};
Expand Down Expand Up @@ -49,6 +48,7 @@ fn main() -> Result<(), core::convert::Infallible> {
MenuStyle::new(BinaryColor::On)
.with_font(&FONT_6X10)
.with_title_font(&FONT_8X13_BOLD)
.with_input_adapter(Simulator::default())
.with_details_delay(100),
)
.add_item(
Expand All @@ -73,20 +73,12 @@ fn main() -> Result<(), core::convert::Infallible> {
window.update(&display);

for event in window.events() {
menu.interact(event);

match event {
SimulatorEvent::KeyDown {
keycode,
repeat: false,
..
} => match keycode {
Keycode::Return => menu.interact(InteractionType::Select),
Keycode::Up => menu.interact(InteractionType::Previous),
Keycode::Down => menu.interact(InteractionType::Next),
_ => None,
},
SimulatorEvent::Quit => break 'running,
_ => None,
};
_ => continue,
}
}
}

Expand Down
38 changes: 12 additions & 26 deletions examples/macros.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//! Run using `cargo run --example simple --target x86_64-pc-windows-msvc`
//! Run using `cargo run --example simple --target x86_64-pc-windows-msvc` --features=simulator
//!
//! Navigate using up/down arrows, interact using the Enter key

use embedded_graphics::{pixelcolor::BinaryColor, prelude::Size, Drawable};
use embedded_graphics_simulator::{
sdl2::Keycode, BinaryColorTheme, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent,
Window,
BinaryColorTheme, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
use embedded_menu::{
interaction::InteractionType, selection_indicator::style::animated_triangle::AnimatedTriangle,
Menu, MenuStyle, SelectValue,
interaction::simulator::Simulator,
selection_indicator::style::animated_triangle::AnimatedTriangle, Menu, MenuStyle, SelectValue,
};

#[derive(Copy, Clone, Debug, PartialEq, SelectValue)]
Expand Down Expand Up @@ -50,7 +49,8 @@ fn main() -> Result<(), core::convert::Infallible> {
MenuStyle::new(BinaryColor::On)
.with_details_delay(250)
.with_animated_selection_indicator(10)
.with_selection_indicator(AnimatedTriangle::new(200)),
.with_selection_indicator(AnimatedTriangle::new(200))
.with_input_adapter(Simulator { page_size: 5 }),
);

let output_settings = OutputSettingsBuilder::new()
Expand All @@ -65,27 +65,13 @@ fn main() -> Result<(), core::convert::Infallible> {
window.update(&display);

for event in window.events() {
let interaction = match event {
SimulatorEvent::KeyDown { keycode, .. } => match keycode {
Keycode::Return => Some(InteractionType::Select),
Keycode::Up => Some(InteractionType::Previous),
Keycode::Down => Some(InteractionType::Next),
_ => None,
},
SimulatorEvent::Quit => break 'running,
_ => None,
};

let Some(interaction) = interaction else {
continue;
};
let output = menu.interact(interaction);
let Some(output) = output else {
continue;
};
if let Some(NavEvents::Quit) = menu.interact(event) {
break 'running;
}

match output {
NavEvents::Quit => break 'running,
match event {
SimulatorEvent::Quit => break 'running,
_ => continue,
}
}
}
Expand Down
46 changes: 18 additions & 28 deletions examples/no_title.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//! Run using `cargo run --example simple --target x86_64-pc-windows-msvc`
//! Run using `cargo run --example simple --target x86_64-pc-windows-msvc` --features=simulator
//!
//! Navigate using up/down arrows, interact using the Enter key

use embedded_graphics::{pixelcolor::BinaryColor, prelude::Size, Drawable};
use embedded_graphics_simulator::{
sdl2::Keycode, BinaryColorTheme, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent,
Window,
BinaryColorTheme, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
use embedded_menu::{
interaction::InteractionType,
interaction::simulator::Simulator,
items::{select::SelectValue, NavigationItem, Select},
Menu, MenuStyle,
};
Expand Down Expand Up @@ -39,18 +38,17 @@ impl SelectValue for TestEnum {
}

fn main() -> Result<(), core::convert::Infallible> {
let mut menu = Menu::with_style("", MenuStyle::default().with_details_delay(100))
.add_item(
NavigationItem::new("Foo", ())
.with_marker(">")
.with_detail_text(
"Lorem ipsum dolor sit amet, in per offendit assueverit adversarium, no sed clita adipisci nominati.",
),
)
.add_item(Select::new("Check this 1", false).with_detail_text("Description"))
.add_item(Select::new("Check this 2", false).with_detail_text("Description"))
.add_item(Select::new("Check this 3", TestEnum::A).with_detail_text("Description"))
.build();
let mut menu = Menu::with_style(
"",
MenuStyle::default()
.with_input_adapter(Simulator::default())
.with_details_delay(100),
)
.add_item(NavigationItem::new("Foo", ()).with_marker(">"))
.add_item(Select::new("Check this 1", false).with_detail_text("Description"))
.add_item(Select::new("Check this 2", false).with_detail_text("Description"))
.add_item(Select::new("Check this 3", TestEnum::A).with_detail_text("Description"))
.build();

let output_settings = OutputSettingsBuilder::new()
.theme(BinaryColorTheme::OledBlue)
Expand All @@ -64,20 +62,12 @@ fn main() -> Result<(), core::convert::Infallible> {
window.update(&display);

for event in window.events() {
menu.interact(event);

match event {
SimulatorEvent::KeyDown {
keycode,
repeat: false,
..
} => match keycode {
Keycode::Return => menu.interact(InteractionType::Select),
Keycode::Up => menu.interact(InteractionType::Previous),
Keycode::Down => menu.interact(InteractionType::Next),
_ => None,
},
SimulatorEvent::Quit => break 'running,
_ => None,
};
_ => continue,
}
}
}

Expand Down
42 changes: 15 additions & 27 deletions examples/owned.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
//! Run using `cargo run --example simple --target x86_64-pc-windows-msvc`
//! Run using `cargo run --example simple --target x86_64-pc-windows-msvc` --features=simulator
//!
//! Navigate using up/down arrows, interact using the Enter key

use embedded_graphics::{pixelcolor::BinaryColor, prelude::Size, Drawable};
use embedded_graphics_simulator::{
sdl2::Keycode, BinaryColorTheme, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent,
Window,
BinaryColorTheme, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
use embedded_menu::{
interaction::InteractionType,
interaction::simulator::Simulator,
items::{select::SelectValue, NavigationItem, Select},
Menu,
Menu, MenuStyle,
};

#[derive(Copy, Clone, PartialEq)]
Expand Down Expand Up @@ -45,16 +44,13 @@ fn main() -> Result<(), core::convert::Infallible> {
.map(|i| Select::new(i, false))
.collect();

let mut menu = Menu::new(format!("Items: {}", items.len()))
.add_item(
NavigationItem::new("Foo", ())
.with_marker(">")
.with_detail_text(
"Lorem ipsum dolor sit amet, in per offendit assueverit adversarium, no sed clita adipisci nominati.",
),
)
.add_items(items)
.build();
let mut menu = Menu::with_style(
format!("Items: {}", items.len()),
MenuStyle::new(BinaryColor::On).with_input_adapter(Simulator::default()),
)
.add_item(NavigationItem::new("Foo", ()).with_marker(">"))
.add_items(items)
.build();

let output_settings = OutputSettingsBuilder::new()
.theme(BinaryColorTheme::OledBlue)
Expand All @@ -68,20 +64,12 @@ fn main() -> Result<(), core::convert::Infallible> {
window.update(&display);

for event in window.events() {
menu.interact(event);

match event {
SimulatorEvent::KeyDown {
keycode,
repeat: false,
..
} => match keycode {
Keycode::Return => menu.interact(InteractionType::Select),
Keycode::Up => menu.interact(InteractionType::Previous),
Keycode::Down => menu.interact(InteractionType::Next),
_ => None,
},
SimulatorEvent::Quit => break 'running,
_ => None,
};
_ => continue,
}
}
}

Expand Down
Loading

0 comments on commit af7627d

Please sign in to comment.