Module: gameui.context

Source: ./gameui/context.reef


Overview

gameui/context.reef - Immediate mode UI context Part of reef-stdlib Game UI module

The UIContext is the core of the immediate mode UI system. Unlike retained mode UI where widgets are created once and persist, immediate mode rebuilds the UI every frame. This is simpler for game UIs that change frequently.

Usage pattern: let ctx = ui_context(surface, input) ui_begin_frame(ctx) if ui_button(ctx, "Play") { start_game() } if ui_checkbox(ctx, "Sound", &sound_enabled) { ... } ui_end_frame(ctx)


Types

StateEntry

Fields:

Name Type
id int
float_value float
bool_value bool
has_float bool
has_bool bool

UIContext

Fields:

Name Type
surface surface.Surface
input input.InputState
style style.Style
next_id int
id_stack [int]
id_stack_size int
hot_id int
active_id int
cursor_x float
cursor_y float
line_height float
indent_level float
same_line bool
clip_stack [core.Rect]
clip_stack_size int
state [StateEntry]
state_count int

Functions

fn ui_context(surf: surface.Surface, inp: input.InputState): UIContext

fn ui_context_with_style(surf: surface.Surface, inp: input.InputState, st: style.Style): UIContext

fn ui_gen_id(ctx: UIContext): int

fn ui_push_id(ctx: UIContext, id: int): UIContext

fn ui_pop_id(ctx: UIContext): UIContext

fn ui_get_id(ctx: UIContext, name: string): int

fn string_hash(s: string): int

fn string_length(s: string): int

fn char_at(s: string, idx: int): int

fn ui_set_hot(ctx: UIContext, id: int): UIContext

fn ui_set_active(ctx: UIContext, id: int): UIContext

fn ui_clear_active(ctx: UIContext): UIContext

fn ui_is_hot(ctx: UIContext, id: int): bool

fn ui_is_active(ctx: UIContext, id: int): bool

fn ui_cursor(ctx: UIContext): core.Point

fn ui_set_cursor(ctx: UIContext, x: float, y: float): UIContext

fn ui_advance_cursor(ctx: UIContext, dx: float, dy: float): UIContext

fn ui_same_line(ctx: UIContext): UIContext

fn ui_new_line(ctx: UIContext): UIContext

fn ui_indent(ctx: UIContext, amount: float): UIContext

fn ui_unindent(ctx: UIContext, amount: float): UIContext

fn ui_space(ctx: UIContext, amount: float): UIContext

fn ui_push_clip(ctx: UIContext, rect: core.Rect): UIContext

fn ui_pop_clip(ctx: UIContext): UIContext

fn ui_get_clip(ctx: UIContext): core.Rect

fn find_state(ctx: UIContext, id: int): option.Option[int]

Index of the entry for id, or None if this context has never stored one. C16: this returned -1. Both callers happened to test it, which is why the sentinel never produced a wrong answer -- but see ensure_state below for what the SECOND -1 in this pair did cost.

fn ensure_state(ctx: UIContext, id: int): option.Option[int]

Index of the entry for id, creating one if the table has room, or None if the table is FULL.

ROADMAP 8.3 called the old return -1 here a silent drop, and the reason is one step further out than the sentinel: both callers DID test it, and then returned the unchanged UIContext anyway. Since a UIContext is a reference, that return value carried no information at all -- so a caller who filled the table got back the same context it passed in, with the value silently not stored and no way to tell. ui_set_float / ui_set_bool return bool now.

fn ui_get_float(ctx: UIContext, id: int): option.Option[float]

The Option form is the SOURCE OF TRUTH, and the _or twin delegates to it. It was the other way round: only the defaulted form existed, so a caller could not distinguish "stored 0.0" from "never stored", which is what 035_ERROR_HANDLING.md:427 requires the Option form to answer.

fn ui_get_float_or(ctx: UIContext, id: int, default: float): float

fn ui_set_float(ctx: UIContext, id: int, value: float): bool

True if the value was stored. False means this context's state table is full (MAX_STATE_VALUES entries) and the value was NOT kept -- which used to be indistinguishable from success, because this returned the same UIContext either way and a UIContext is a reference.

fn ui_get_bool(ctx: UIContext, id: int): option.Option[bool]

fn ui_get_bool_or(ctx: UIContext, id: int, default: bool): bool

fn ui_set_bool(ctx: UIContext, id: int, value: bool): bool

True if the value was stored. See ui_set_float.

fn reef_int_to_float(i: int): float

fn int_to_float_ctx(i: int): float


Procedures

proc ui_begin_frame(ctx: UIContext)

proc ui_end_frame(ctx: UIContext)

proc update_line_height(ctx: UIContext, height: float)

proc advance_after_widget(ctx: UIContext, width: float, height: float)

Called after drawing a widget to advance cursor


Generated by reefc doc