Skip to main content

Module engine

Module engine 

Source
Expand description

§Engine

This is the run loop processor that enables Pushrod to open a new window, layout the Widgets in the display, and handle the Event loop.

The event loop processes input in order of receipt from the Backend, which polls the window system for InputEvents. The events are sent to the WidgetCache, which routes them to the Widgets. Any Events generated by the Widgets are queued, and then delivered to the callbacks registered on the Engine.

The Engine owns an application state object of any type S. Callbacks receive mutable access to that state, and to the Widgets through a Ui handle, in the style of Visual Basic event procedures:

The example uses the standard widgets from the pushrod-widgets crate; the core itself contains no widgets other than the window’s RootWidget.

ⓘ
use pushrod::engine::Engine;
use pushrod::geometry::{point, size};
use pushrod_widgets::button_widget::ButtonWidget;
use pushrod_widgets::text_widget::{TextJustify, TextWidget};
use pushrod::color::{Color, FontStyle};

#[derive(Default)]
struct AppState {
    clicks: u32,
}

let mut engine: Engine<AppState> = Engine::new(size(400, 200), 30);

let font = String::from("assets/OpenSans-Regular.ttf");
let label = engine.add_widget(TextWidget::new(point(20, 20), size(200, 20), font.clone(),
    FontStyle::NORMAL, 14, Color::BLACK, TextJustify::Left, String::from("Not clicked")));
let button = engine.add_widget(ButtonWidget::new(point(20, 60), size(120, 20), font,
    FontStyle::NORMAL, 14, Color::BLACK, TextJustify::Center, String::from("Click me"), 2));

engine.on_click(button, move |state, ui| {
    state.clicks += 1;
    ui.get_mut::<TextWidget>(label)
        .unwrap()
        .set_text(format!("Clicked {} times", state.clicks));
});

pushrod_sdl2::run(engine, pushrod_sdl2::WindowSettings::new("app", size(400, 200)), AppState::default());

Re-exports§

pub use crate::timer::TimerId;

Structs§

CallbackId
A handle identifying one registered callback, returned by every on_* registration method.
Engine
The run loop: owns the window’s widget tree, routes input to Widgets, calls the application’s callbacks, and draws.
EventLoop
The state of an application-driven event loop.
Sender
A handle other threads use to post custom events to the UI thread.
Ui
The handle passed to callbacks.

Constants§

MAX_CUSTOM_EVENTS_PER_FRAME
The most custom events delivered in one frame; the rest are discarded, which stops callbacks that post a custom event in response to a custom event forever.

Type Aliases§

EngineEventCallback
A callback that receives the engine events Pushrod emits after its internal actions.
EventCallback
A callback that receives every event the Widgets generate.