Give BASIC menus, dialogs and HUD labels over libakgl's UI helpers
Some checks failed
akbasic CI Build / cmake_build (push) Failing after 3m23s
akbasic CI Build / sanitizers (push) Failing after 4m36s
akbasic CI Build / coverage (push) Failing after 3m41s
akbasic CI Build / akgl_build (push) Failing after 4m45s
akbasic CI Build / mutation_test (push) Failing after 3m31s

Group K, and the first verbs to reach the akgl_ui subsystem 0.9.0 brought
in: MENU and GETMENU and RMENU, DIALOG, HUD and UISTYLE. A program that
wanted a title screen had to draw one out of CHAR and GETKEY, which is
what both breakout tutorials make a reader do.

The interesting part is the impedance mismatch. libakgl's UI is immediate
mode -- widgets are re-declared inside a frame bracket every frame and
clay borrows their text until the bracket closes -- and a BASIC program
says MENU 1, "START" on line 100 and expects it up on line 900, several
hundred frames later. So src/ui_akgl.c is retained on this side and
immediate on that one: the record's entry points are setters that copy
into akbasic_AkglUi, and akbasic_ui_akgl_render() replays the whole set
once a frame from the host's pump. No BASIC string, which lives in the
per-line value pool, is ever what clay is handed.

The shapes are borrowed rather than invented. MENU retires the way SOLID
does -- no entries retires one, no arguments retire them all. GETMENU
holds the step loop the way GETKEY does, so parking is not blocking: the
step still returns, the host keeps its frame rate, and the sprite, audio
and collision services keep running underneath because they run before
the blocking checks. RMENU(n,1) reads and clears the way BUMP() does.
Withdrawing the device or retiring the menu releases a holding GETMENU
with 0 rather than wedging the script, which is akbasic_input_service()'s
rule for a withdrawn keyboard.

One thing a program has to know, and docs/19-user-interface.md says it
twice: a menu that is up owns the cursor keys and Return. It has to, and
retiring it gives them back -- forget the MENU n before an INPUT and the
INPUT never sees the Return that ends it.

akbasic_runtime_set_ui() is its own function rather than a fifth argument
to akbasic_runtime_set_devices(), whose signature has twenty-eight call
sites in tests and documentation that are about something else.

deps/libakgl is not touched. akgl_UiAnchor has the four corners and dead
centre, so HUD offers exactly those five; TODO.md records what a
top-centre and bottom-centre would cost upstream, along with the three
other things this deliberately leaves out. No new error code either --
DEVICE, BOUNDS, SYNTAX and TYPE cover the group, and 520 stays free.

tools/screenshot.c had to learn that "needs a font" and "draws the text
grid" are two questions. They were one, and a UI figure came out black:
the text layer owns every pixel of the rows it covers and painted over
the widgets. The new ui=1 fence attribute asks for the first without the
second; MAINTENANCE.md documents it.

112/112 in both configurations, 112/112 under ASan and UBSan, coverage
94.1% against the 90% gate with src/runtime_ui.c at 99% of lines and
100% of functions, doxygen clean, and the four new figures byte-identical
on a re-render. TODO.md section 8's gate table was stale on several
counts besides these and is refreshed with measured numbers.

Co-Authored-By: Tachikoma (Claude Code Opus 5 1M) <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EwxGB6TdoVvZ11KQQME9cL
This commit is contained in:
2026-08-02 18:37:10 -04:00
parent c44a5903ec
commit 9151438fad
33 changed files with 2424 additions and 31 deletions

View File

@@ -42,6 +42,7 @@
#include <akbasic/sprite.h>
#include <akbasic/runtime.h>
#include <akbasic/sink.h>
#include <akbasic/ui.h>
#include "testutil.h"
@@ -58,6 +59,8 @@ static akbasic_GraphicsBackend GRAPHICS;
static akbasic_AkglGraphics GRAPHICSSTATE;
static akbasic_InputBackend INPUT;
static akbasic_SpriteBackend SPRITES;
static akbasic_UiBackend UI;
static akbasic_AkglUi UISTATE;
static akbasic_AkglSprites SPRITESSTATE;
static TTF_Font *font = NULL;
static char OUTPUT[8192];
@@ -445,6 +448,107 @@ static akerr_ErrorContext AKERR_NOIGNORE *test_input_backend(void)
SUCCEED_RETURN(errctx);
}
/**
* @brief The UI adaptor: retained widgets, a rendered frame, and a real choice.
*
* The seam this asserts is the one the whole file is about, and here it is more
* than a coordinate conversion. libakgl's UI is immediate mode and this
* interpreter is not, so what has to work is that a MENU declared once survives
* a frame bracket it never saw, and that a keystroke libakgl consumed comes back
* out through the record as an activation a BASIC program can read.
*
* The keystrokes are **keycodes, not scancodes** -- akgl_ui_menu_handle_event
* matches on `event.key.key`, and a scancode-filled event is silently ignored.
*/
static akerr_ErrorContext AKERR_NOIGNORE *test_ui_backend(void)
{
PREPARE_ERROR(errctx);
const char *items[3] = { "START", "OPTIONS", "QUIT" };
akbasic_Color fill = { 0x00, 0x00, 0x00, 0xff };
akbasic_Color edge = { 0xff, 0xff, 0xff, 0xff };
akbasic_Color ink = { 0xff, 0xff, 0xff, 0xff };
SDL_Event event;
int selected = 99;
bool activated = true;
bool consumed = false;
PASS(errctx, akbasic_ui_init_akgl(&UI, &UISTATE, akgl_renderer,
AKBASIC_TEST_FONT, 12, TARGET_SIZE, TARGET_SIZE));
/* Nothing declared yet: a frame with no widgets is still a legal frame. */
PASS(errctx, akbasic_ui_akgl_render(&UI));
PASS(errctx, UI.style(&UI, &fill, &edge, &ink, 4.0, 0.0));
PASS(errctx, UI.label(&UI, 0, AKBASIC_UI_ANCHOR_TOP_LEFT, "LIVES 3"));
PASS(errctx, UI.dialog(&UI, "A PANEL"));
PASS(errctx, UI.menu(&UI, 0, items, 3));
/* Freshly defined: first entry highlighted, nothing chosen. */
PASS(errctx, UI.menu_state(&UI, 0, &selected, &activated, false));
TEST_REQUIRE_INT(selected, 0);
TEST_REQUIRE(!activated, "a freshly defined menu should not be activated");
/*
* A whole frame, from what the verbs left behind. This is the assertion that
* the retained set is re-declarable: clay borrows the text until frame_end,
* and every pointer it is handed here belongs to UISTATE rather than to a
* BASIC value that stopped existing several statements ago.
*/
PASS(errctx, akbasic_ui_akgl_render(&UI));
/* Down, then Return -- the menu owns both while it has entries. */
memset(&event, 0, sizeof(event));
event.type = SDL_EVENT_KEY_DOWN;
event.key.key = SDLK_DOWN;
PASS(errctx, akbasic_ui_akgl_handle_event(&UI, &event, &consumed));
TEST_REQUIRE(consumed, "a menu that is up should consume Down");
memset(&event, 0, sizeof(event));
event.type = SDL_EVENT_KEY_DOWN;
event.key.key = SDLK_RETURN;
PASS(errctx, akbasic_ui_akgl_handle_event(&UI, &event, &consumed));
TEST_REQUIRE(consumed, "a menu that is up should consume Return");
PASS(errctx, UI.menu_state(&UI, 0, &selected, &activated, true));
TEST_REQUIRE_INT(selected, 1);
TEST_REQUIRE(activated, "Return should have activated the second entry");
/* Reading with clear consumed the latch; the highlight is untouched. */
PASS(errctx, UI.menu_state(&UI, 0, &selected, &activated, false));
TEST_REQUIRE_INT(selected, 1);
TEST_REQUIRE(!activated, "reading the latch should have cleared it");
/*
* Retiring the menu hands the cursor keys back. Without this a program that
* put a menu up could never take a typed line again, which is what makes the
* REPL usable at all.
*/
PASS(errctx, UI.menu(&UI, 0, NULL, 0));
memset(&event, 0, sizeof(event));
event.type = SDL_EVENT_KEY_DOWN;
event.key.key = SDLK_DOWN;
PASS(errctx, akbasic_ui_akgl_handle_event(&UI, &event, &consumed));
TEST_REQUIRE(!consumed, "a retired menu must not still be eating Down");
/* Escape is nobody's, even with a menu up. */
PASS(errctx, UI.menu(&UI, 0, items, 3));
memset(&event, 0, sizeof(event));
event.type = SDL_EVENT_KEY_DOWN;
event.key.key = SDLK_ESCAPE;
PASS(errctx, akbasic_ui_akgl_handle_event(&UI, &event, &consumed));
TEST_REQUIRE(!consumed, "Escape should reach the program, not the menu");
PASS(errctx, UI.clear(&UI));
PASS(errctx, UI.menu_state(&UI, 0, &selected, &activated, false));
TEST_REQUIRE_INT(selected, 0);
PASS(errctx, akbasic_ui_akgl_render(&UI));
/* Shutdown is idempotent, because teardown paths are already unwinding. */
akbasic_ui_akgl_shutdown(&UI);
akbasic_ui_akgl_shutdown(&UI);
SUCCEED_RETURN(errctx);
}
/**
* @brief A sprite loaded from an image file lands on the target where MOVSPR put it.
*
@@ -1225,6 +1329,7 @@ int main(void)
CATCH(errctx, test_sink_writes_past_a_short_row());
CATCH(errctx, test_sink_renders());
CATCH(errctx, test_input_backend());
CATCH(errctx, test_ui_backend());
CATCH(errctx, test_sprite_from_file());
CATCH(errctx, test_sprite_from_pattern());
CATCH(errctx, test_sprite_from_shape());