Trait UserInterface

Source
pub trait UserInterface: Game {
    type Message;
    type Renderer: Renderer;

    // Required methods
    fn react(&mut self, message: Self::Message, window: &mut Window);
    fn layout(
        &mut self,
        window: &Window,
    ) -> Element<'_, Self::Message, Self::Renderer>;

    // Provided methods
    fn configuration() -> <Self::Renderer as Renderer>::Configuration { ... }
    fn run(window_settings: WindowSettings) -> Result<()>
       where Self: 'static + Sized { ... }
}
Expand description

The user interface of your game.

Implementors of this trait must also implement Game and should hold all the state of the user interface.

Be sure to read the introduction of the ui module first! It will help you understand the purpose of this trait.

Required Associated Types§

Source

type Message

The type of messages handled by the user interface.

Messages are produced by user interactions. The runtime feeds these messages to the react method, which updates the state of the game depending on the user interaction.

The Message type should normally be an enumeration of different user interactions. For example:

enum Message {
    ButtonPressed,
    CheckboxToggled(bool),
    SliderChanged(f32),
    // ...
}
Source

type Renderer: Renderer

The renderer used to draw the user interface.

If you just want to use the built-in widgets in Coffee, you should use the built-in Renderer type here.

If you want to write your own renderer, you will need to implement the core::Renderer trait.

Required Methods§

Source

fn react(&mut self, message: Self::Message, window: &mut Window)

Reacts to a Message, updating game state as needed.

This method is analogous to Game::interact, but it processes a Message instead of Game::Input.

The logic of your user interface should live here.

Source

fn layout( &mut self, window: &Window, ) -> Element<'_, Self::Message, Self::Renderer>

Produces the layout of the user interface.

It returns an Element containing the different widgets that comprise the user interface.

This method is called on every frame. The produced layout is rendered and used by the runtime to allow user interaction.

Provided Methods§

Source

fn configuration() -> <Self::Renderer as Renderer>::Configuration

Builds the renderer configuration for the user interface.

By default, it returns Default::default().

Source

fn run(window_settings: WindowSettings) -> Result<()>
where Self: 'static + Sized,

Runs the Game with a user interface.

Call this method instead of Game::run once you have implemented the UserInterface.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§