7 unstable releases
Uses new Rust 2024
| new 0.4.0 | Jan 27, 2026 |
|---|---|
| 0.3.1 | Oct 9, 2019 |
| 0.2.0 | Oct 7, 2019 |
| 0.1.2 | Jan 8, 2019 |
| 0.1.1 | Dec 26, 2018 |
#4 in #event-system
Used in 4 crates
(via sylasteven)
11KB
181 lines
Layer System
A lightweight, flexible system for managing stateful layers in games or UI applications. Perfect for handling menus, game states, and event propagation with minimal boilerplate.
Features
- Layer Stack: Add and remove layers dynamically (contains the game, useful for adding pause menu, HUD, and similar).
- Event Flow:
update(): Top-to-bottom, might stop early (useful for pause menus, clicks, and also general game update).passive_update(): Bottom-to-top, always executed for every layer (especially useful for rendering)
- Generic: Works with any
StateandEventtypes.
Quick Example
use layer_system::{Change, Layer, LayerManager};
// 1. Define Events
enum Event {
Fixed,
Click(u16, u16),
Quit,
}
// 2. Implement Layers
struct World;
impl Layer<(), Event> for World {
fn update(&mut self, _: &mut (), event: &Event) -> Change<(), Event> {
match event {
Event::Fixed => {
/* physics */
Change::none()
}
_ => Change::pass(),
}
}
}
// 3. Run
let mut manager = LayerManager::new(vec![Box::new(World)]);
manager.update(&mut (), Event::Fixed);
Key Concepts
Change Actions
| Method | Behavior |
|---|---|
add() |
Push a new layer (open a menu) |
remove() |
Remove the current layer (close a menu) |
close() |
Clear all layers (end the game) |
pass() |
Defer event to lower layers (the next layer will also act) |
When to Use
- Games: Main menu → Game world → Pause screen transitions.
- UI: Modal dialogs, tooltips, or stacked panels.
- Simulations: Layered behavior.