#renderer #agnostic #ui #interface #gamedev

raui-html-renderer

RAUI renderer for simple HTML static webpages

11 breaking releases

new 0.16.0 Jan 13, 2021
0.14.0 Jan 5, 2021
0.12.1 Dec 31, 2020

#208 in Graphics APIs

Download history 26/week @ 2020-11-30 34/week @ 2020-12-07 46/week @ 2020-12-14 23/week @ 2020-12-21 42/week @ 2020-12-28 63/week @ 2021-01-04 38/week @ 2021-01-11

79 downloads per month
Used in raui

MIT license

205KB
6K SLoC

RAUI Crates.ioDocs.rs

Renderer Agnostic User Interface

Table of contents

  1. About
  2. Architecture
    1. Application
    2. Widget
    3. Component Function
    4. Hooks
    5. Layouting
    6. Interactivity
  3. Media
  4. Installation
  5. Milestones

About

RAUI (spelled as "ra" (Egiptian god) + "oui" (french for "yes")) is heavely inspired by React declarative UI composition and UE4 Slate widget components system.

The main idea behind RAUI architecture is to treat UI as another data that you transform into target renderable data format used by your rendering engine of choice.

Architecture

Application

It is the central point of user interrest. It performs whole UI processing logic. There you apply widget tree that wil be processed, send messages from host application to widgets and receive signals sent from widgets to host application.

let mut application = Application::new();
let tree = widget! {
    (app {
        title = (title_bar: {"Hello".to_owned()})
        content = (vertical_box [
            (#{"hi"} button: {"Say hi!".to_owned()})
            (#{"exit"} button: {"Close".to_owned()})
        ])
    })
};
let mut renderer = HtmlRenderer::default();
application.apply(tree);
let mapping = CoordsMapping::new(Rect {
    left: 0.0,
    right: 1024.0,
    top: 0.0,
    bottom: 576.0,
});
application.layout(&mapping, &mut DefaultLayoutEngine);
if let Ok(output) = application.render(&mapping, &mut renderer) {
    println!("OUTPUT: {}", output);
}

Widget

Widgets are divided into three categories:

  • Widget Node - used as source UI trees (variant that can be either a component, unit or none)
    widget! {
        (app {
            title = (title_bar: {"Hello".to_owned()})
            content = (vertical_box [
                (#{"hi"} button: {"Say hi!".to_owned()})
                (#{"exit"} button: {"Close".to_owned()})
            ])
        })
    }
    
  • Widget Component - you can think of them as Virtual DOM nodes, they store:
    • pointer to component function (that process their data)
    • unique key (that is a part of widget ID and will be used to tell the system if it should carry its state to next processing run)
    • boxed cloneable properties data (if component is a function, then properties are function arguments)
    • listed slots (simply: widget children)
    • named slots (similar to listed slots: widget children, but these ones have names assigned to them, so you can access them by name instead of by index)
  • Widget Unit - an atomic element that renderers use to convert into target renderable data format for rendering engine of choice.
    widget!{{{
        TextBoxNode {
            text: "Hello World".to_owned(),
            ..Default::default()
        }
    }}}
    

Component Function

Component functions are static functions that transforms input data (properties, state or neither of them) into output widget tree (usually used to simply wrap another components tree under one simple component, where at some point the simplest components returns final widget units). They work together as a chain of transforms - root component applies some properties into children components using data from its own properties or state.

#[derive(Debug, Default, Copy, Clone)]
struct AppProps {
    pub index: usize,
}
implement_props_data!(AppProps);

widget_component! {
    app(props, named_slots) {
        unpack_named_slots!(named_slots => { title, content });
        let index = props.read::<AppProps>().map(|p| p.index).unwrap_or(0);

        widget! {
            (#{index} vertical_box [
                {title}
                {content}
            ])
        }
    }
}

widget_component! {
    vertical_box(key, listed_slots) {
        let items = listed_slots
            .into_iter()
            .map(|slot| FlexBoxItemNode {
                slot: slot.try_into().expect("Cannot convert slot to WidgetUnit!"),
                ..Default::default()
            })
            .collect::<Vec<_>>();

        widget! {{{
            FlexBoxNode {
                items,
                ..Default::default()
            }
        }}}
    }
}

This may bring up a question: "If i use only functions and no objects to tell how to visualize UI, how do i keep some data between each render run?". For that you use states. State is a data that is stored between each processing call as long as given widget is alive (that means: as long as widget id stays the same between two processing calls, to make sure your widget stays the same, you use keys - if no key is assigned, system will generate one for your widget but that will make it possible to die at any time if for example number of widget children changes in your common parent, your widget will change its id when key wasn't assigned). Some additional notes: While you use properties to send information down the tree and states to store widget data between processing cals, you can communicate with another widgets and host application using messages and signals! More than that, you can use hooks to listen for widget life cycle and perform actions there.

#[derive(Debug, Default, Copy, Clone)]
struct ButtonState {
    pub pressed: bool,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum ButtonAction {
    Pressed,
    Released,
}

widget_hook! {
    use_button(life_cycle) {
        life_cycle.mount(move |context| {
            drop(context.state.write(ButtonState { pressed: false }));
        });

        life_cycle.change(move |context| {
            for msg in context.messenger.messages {
                if let Some(msg) = msg.downcast_ref::<ButtonAction>() {
                    let pressed = match msg {
                        ButtonAction::Pressed => true,
                        ButtonAction::Released => false,
                    };
                    drop(context.state.write(ButtonState { pressed }));
                    drop(context.signals.write(*msg));
                }
            }
        });
    }
}

widget_component! {
    button(key, props) [use_button] {
        let label = props.read_cloned_or_default::<String>();

        widget!{
            (#{key} text: {label})
        }
    }
}

Hooks

Hooks are used to put common widget logic into separate functions that can be chained in widgets and another hooks (you can build a reusable dependency chain of logic with that). Usually it is used to listen for life cycle events such as mount, change and unmount, additionally you can chain hooks to be processed sequentially in order they are chained in widgets and other hooks.

widget_hook! {
    use_empty {}
}

widget_hook! {
    use_button(life_cycle) [use_empty] {
        life_cycle.mount(move |context| {
            drop(context.state.write(ButtonState { pressed: false }));
        });

        life_cycle.change(move |context| {
            for msg in context.messenger.messages {
                if let Some(msg) = msg.downcast_ref::<ButtonAction>() {
                    let pressed = match msg {
                        ButtonAction::Pressed => true,
                        ButtonAction::Released => false,
                    };
                    drop(context.state.write(ButtonState { pressed }));
                    drop(context.signals.write(*msg));
                }
            }
        });
    }
}

widget_component! {
    button(key, props) [use_button] {
        widget!{
            (#{key} text: {props})
        }
    }
}

What happens under the hood:

  • Application calls button on a node
    • button calls use_button hook
      • use_button calls use_empty hook
    • use_button logic is executed
  • button logic is executed

Layouting

TODO

RAUI exposes API (Application::layout()) to allow use of virtual-to-real coords mapping and custom layout engines to perform widget tree positioning data, which is later used by custom UI renderers to specify boxes where given widgets should be placed. Every call to perform layouting will store a layout data inside Application, you can always access that data at any time. There is a DefaultLayoutEngine that does this in a generic way. If you find some part of its pipeline working different than what you've expected, feel free to create your custom layout engine!

let mut application = Application::new();
application.apply(tree);
application.process();
let mapping = CoordsMapping::new(Rect {
    left: 0.0,
    right: 1024.0,
    top: 0.0,
    bottom: 576.0,
});
if application.layout(&mapping, &mut DefaultLayoutEngine).is_ok() {
    println!("LAYOUT:\n{:#?}", application.layout_data());
}

Interactivity

TODO

RAUI allows you to ease and automate interactions with UI by use of Interactions Engine - this is just a struct that implements perform_interactions method with reference to Application, and all you should do there is to send user input related messages to widgets. There is DefaultInteractionsEngine that covers button and input field actions sent from mouse (or any single pointer), keyboard and gamepad. RAUI integrations that use these devices should make use of this struct composed in them and call its interact metgod with information about what input change was made. There is an example of that feature covered in GGEZ integration crate (GgezInteractionsEngine struct).

NOTE: Interactions engines should use layout for pointer events so make sure that you rebuild layout before you perform interactions!

let mut application = Application::new();
let mut interactions = DefaultInteractionsEngine::new();
interactions.interact(Interaction::PointerMove(200.0, 100.0));
interactions.interact(Interaction::PointerDown(PointerButton::Trigger, 200.0, 100.0));
application.apply(tree);
application.process();
let mapping = CoordsMapping::new(Rect {
    left: 0.0,
    right: 1024.0,
    top: 0.0,
    bottom: 576.0,
});
application.layout(&mapping, &mut DefaultLayoutEngine);
application.interact(&mapping, &mut interactions);

Media

  • RAUI + GGEZ In-Game An example of an In-Game integration of RAUI with custom Material theme, using GGEZ as a renderer.

    RAUI + GGEZ In-Game

  • RAUI + GGEZ todo app An example of TODO app with GGEZ renderer and dark theme Material component library.

    RAUI + GGEZ todo app

Installation

There is a main raui crate that contains all of the project sub-crates to allow easy access to all features needed at any time, each enabled using Cargo feature flags (by default only raui-core subcrate is enabled).

[dependencies]
raui = { version = "*", features = ["all"] }
  • raui-core - Core module that contains all RAUI essential logic.
    [dependencies]
    raui-core = "*"
    
  • raui-material - Material Library module that contains themeable Material components for RAUI (material feature).
    [dependencies]
    raui-material = "*"
    
  • raui-binary-renderer - Renders RAUI widget tree into binary format (binary feature).
    [dependencies]
    raui-binary-renderer = "*"
    
  • raui-html-renderer - Renders RAUI widget tree into simple HTML format (html feature).
    [dependencies]
    raui-html-renderer = "*"
    
  • raui-json-renderer - Renders RAUI widget tree into JSON format (json feature).
    [dependencies]
    raui-json-renderer = "*"
    
  • raui-ron-renderer - Renders RAUI widget tree into RON format (ron feature).
    [dependencies]
    raui-ron-renderer = "*"
    
  • raui-yaml-renderer - Renders RAUI widget tree into YAML format (yaml feature).
    [dependencies]
    raui-yaml-renderer = "*"
    
  • raui-ggez-renderer - Renders RAUI widget tree with GGEZ renderer.
    [dependencies]
    raui-ggez-renderer = "*"
    

Milestones

RAUI is still in early development phase, so prepare for these changes until v1.0:

  • Integrate RAUI into one public open source Rust game.
  • Write documentation.
  • Write MD book about how to use RAUI properly and make UI efficient.
  • Props feature starts to look more like a micro ECS - make use of that and make custom allocator for them that would optimize frequent props creation/cloning.
  • Implement VDOM diffing algorithm for tree rebuilding optimizations.
  • Find a solution (or make it a feature) for moving from trait objects data into strongly typed data for properties and states.
  • Make WASM/JS API bindings.
  • Make C API bindings.

Things that now are done:

  • Add suport for layouting.
  • Add suport for interactions (user input).
  • Create renderer for at least one popular Rust graphics engine.
  • Create basic user components.
  • Create basic Hello World example application.
  • Decouple shared props from props (don't merge them, put shared props in context).
  • Create TODO app as an example.
  • Create In-Game app as an example.
  • Create renderer for Oxygengine game engine.

Dependencies

~0.8–1.4MB
~32K SLoC