layer-system 0.4.0

A system for handling different kinds of events
Documentation
enum Event {
    Fixed,           // generated at fixed times
    Click(u16, u16), // generated by click
    Quit,            // genreated by try to close window
}

use self::Event::*;

struct State; // a global state

type LayerManager = layer_system::LayerManager<State, Event>; // the layer manager

use layer_system::{Change, Layer}; // other imports from layer System

struct World; // layer for the game world

impl Layer<State, Event> for World {
    fn passive_update(&mut self, _state: &mut State, event: &Event) {
        match event {
            Fixed => { /* render here */ }
            _ => (),
        }
    }

    fn update(&mut self, _state: &mut State, event: &Event) -> Change<State, Event> {
        match event {
            Fixed => { /* world physics update */ }
            _ => (),
        }
        Change::none()
    }
}

enum ClickEvent {
    Action,
    Pause,
    Exit,
    Nothing,
}

const UI_ROW_HEIGHT: u16 = 0x10;
const BUTTON_WIDTH: u16 = 0x40;

fn click(x: u16, y: u16) -> ClickEvent {
    use ClickEvent::*;

    if UI_ROW_HEIGHT > 0x10 {
        return Nothing;
    }

    if x < BUTTON_WIDTH {
        Action
    } else if x < BUTTON_WIDTH * 2 {
        Pause
    } else if x < BUTTON_WIDTH * 3 {
        Exit
    } else {
        Nothing
    }
}

struct Interface;

impl Layer<State, Event> for Interface {
    fn passive_update(&mut self, _state: &mut State, event: &Event) {
        match event {
            Fixed => { /* render here */ }
            _ => (),
        }
    }

    fn update(&mut self, _state: &mut State, event: &Event) -> Change<State, Event> {
        match event {
            Click(x, y) => match click(*x, *y) {
                // execute some action when clicking "Action"
                ClickEvent::Action => {
                    /* execute action */
                    Change::none()
                }
                // pause when clicking "Pause"
                ClickEvent::Pause => Change::add_single(Box::new(Pause)),
                // remove all layers when clicking "Exit"
                ClickEvent::Exit => Change::close(),
                // nothing was clicked, so in case the world supports a click event, just defer it
                ClickEvent::Nothing => Change::pass(),
            },
            // when trying to quit the game, pause it first
            Quit => Change::add_single(Box::new(Pause)),
            // Defer all other events, so World will handle everything else
            Fixed => Change::pass(),
        }
    }
}

struct Pause;

impl Layer<State, Event> for Pause {
    fn passive_update(&mut self, _state: &mut State, event: &Event) {
        match event {
            Fixed => { /* render here */ }
            _ => (),
        }
    }

    fn update(&mut self, _state: &mut State, event: &Event) -> Change<State, Event> {
        match event {
            // do not defer `Fixed`, so world physics won't be updated anymore
            Fixed => Change::none(),
            // end pause by clicking somewhere
            Click(_, _) => Change::remove(),
            // when trying to quit the game again, quit it
            Quit => Change::close(),
        }
    }
}

fn main() {
    let mut manager = LayerManager::new(vec![Box::new(World), Box::new(Interface)]);

    let mut state = State;

    while manager.is_active() {
        manager.update(&mut state, Fixed);
        /* handle other events in events loop */
        // if click event:
        manager.update(&mut state, Click(0, 0));
        // if quit event:
        manager.update(&mut state, Quit);
        /* wait for a fixed time */
    }
}