Console::UI

console 0.2.0 · app/console/ui.rb · 463 lines · view source

Console::UI -- immediate-mode widgets.

Widgets are described as data and drawn through Console::Draw. There is no retained widget tree: you ask for a widget, it draws itself and reports its state. That keeps the API small and keeps a prototype's UI readable top to bottom.

b = ui.button x: 40, y: 300, w: 200, h: 44, text: 'START'
goto :play if b[:clicked]
ui.panel x: 20, y: 20, w: 260, h: 120, title: 'STATS'
ui.bar x: 40, y: 60, w: 200, value: hp, max: max_hp
ui.label x: 40, y: 100, text: "HP: #{hp}"

Every widget takes ONE options hash and returns a Hash describing what happened (at least :rect, plus :hover/:clicked/:value/:index as relevant). One hash per widget is not just style: mruby binds a trailing key: value list to the FIRST optional parameter of a method, so multi-parameter widget signatures silently swallow their geometry. One hash sidesteps that entirely and matches the draw.* helpers.

Widgets are keyboard/gamepad navigable by default: hold a pointer over a button (or press direction keys on a menu) and :accept activates it.

def initialize(console)
line 28
def draw
line 32
def input
line 36
def store
line 40
def box(opts, w, h)

Resolve a widget's box from its options, honouring either y: (bottom-left origin, DragonRuby's convention) or top: (distance from the top of the screen, which is how UI is usually specified). Widgets use this so that top: works everywhere consistently.

text

def label(options = {})

A text label. Returns the measured rect.

def chip(options = {})

A label with a solid plate behind it, for HUD counters over a busy background.

containers

def panel(options = {})

A panel with an optional title bar. Returns the inner content rect.

indicators

def bar(options = {})

A progress bar. Returns the rect, the fill width and a clamped fraction.

def pips(options = {})

Segmented meter: one cell per unit, filled up to value.

def slider(options = {})

A slider bound to console UI state. Click to focus, then use left/right (or the dpad) to adjust.

interactive

def button(options = {})

A button. :clicked is true on the frame it is activated, by pointer or by :accept while focused.

def checkbox(options = {})

A checkbox bound to console UI state. Returns the new value.

HUD

def hud(_options = {})

The debug/status overlay. Toggle with :debug_toggle (F1).

def card(options = {})

A centred modal card for title / pause / game-over screens.