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
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:
@@ -176,6 +176,14 @@ akerr_ErrorContext *akbasic_frontend_akgl_init(akbasic_AkglFrontend *obj, const
|
||||
*/
|
||||
PASS(errctx, akbasic_sprite_init_akgl(&obj->sprites, &obj->spritesstate,
|
||||
obj->renderer, &obj->graphicsstate));
|
||||
/*
|
||||
* And the UI, which loads the same font file again -- under a registry name
|
||||
* of its own, at the same size. That is not a duplicate by accident: the
|
||||
* sink holds a TTF_Font it opened itself and never registers it, and clay
|
||||
* addresses a font by registry name, so the two cannot share one entry.
|
||||
*/
|
||||
PASS(errctx, akbasic_ui_init_akgl(&obj->ui, &obj->uistate, obj->renderer,
|
||||
fontpath, fontsize, w, h));
|
||||
|
||||
/*
|
||||
* Audio last, in its own subsystem, and allowed to fail. The reference asks
|
||||
@@ -216,6 +224,7 @@ akerr_ErrorContext *akbasic_frontend_akgl_attach(akbasic_AkglFrontend *obj, akba
|
||||
PASS(errctx, akbasic_runtime_set_devices(rt, &obj->graphics,
|
||||
(obj->audioready ? &obj->audio : NULL),
|
||||
&obj->input, &obj->sprites));
|
||||
PASS(errctx, akbasic_runtime_set_ui(rt, &obj->ui));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
@@ -229,6 +238,7 @@ static akerr_ErrorContext AKERR_NOIGNORE *pump_events(akbasic_AkglFrontend *obj)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
SDL_Event event;
|
||||
bool consumed = false;
|
||||
|
||||
while ( SDL_PollEvent(&event) ) {
|
||||
if ( event.type == SDL_EVENT_QUIT ||
|
||||
@@ -236,6 +246,16 @@ static akerr_ErrorContext AKERR_NOIGNORE *pump_events(akbasic_AkglFrontend *obj)
|
||||
obj->running = false;
|
||||
continue;
|
||||
}
|
||||
/*
|
||||
* The UI gets first refusal. The mouse half never takes a keystroke, but
|
||||
* a menu with entries takes Up, Down and Return -- which is why this has
|
||||
* to come *before* the ring is filled, and why retiring a menu is how a
|
||||
* program gives those keys back to the line editor.
|
||||
*/
|
||||
PASS(errctx, akbasic_ui_akgl_handle_event(&obj->ui, &event, &consumed));
|
||||
if ( consumed ) {
|
||||
continue;
|
||||
}
|
||||
/*
|
||||
* Everything else goes to libakgl, which pushes key-downs into the ring
|
||||
* the input backend reads before it consults its own control maps. The
|
||||
@@ -302,6 +322,11 @@ akerr_ErrorContext *akbasic_frontend_akgl_pump(void *self, bool *running)
|
||||
* spr_configure().
|
||||
*/
|
||||
PASS(errctx, akbasic_sprite_akgl_render(&obj->sprites));
|
||||
/*
|
||||
* The UI last of all, over the sprites as well as over the text. A menu the
|
||||
* player is being asked to act on is not something a sprite may cover.
|
||||
*/
|
||||
PASS(errctx, akbasic_ui_akgl_render(&obj->ui));
|
||||
FAIL_ZERO_RETURN(errctx, SDL_RenderPresent(obj->renderer->sdl_renderer),
|
||||
AKGL_ERR_SDL, "Couldn't present the frame: %s", SDL_GetError());
|
||||
if ( resume ) {
|
||||
@@ -357,6 +382,7 @@ void akbasic_frontend_akgl_shutdown(akbasic_AkglFrontend *obj)
|
||||
return;
|
||||
}
|
||||
/* Before the renderer goes: these are textures it created. */
|
||||
akbasic_ui_akgl_shutdown(&obj->ui);
|
||||
akbasic_sprite_akgl_shutdown(&obj->sprites);
|
||||
akbasic_graphics_akgl_shutdown(&obj->graphics);
|
||||
if ( obj->font != NULL ) {
|
||||
|
||||
@@ -232,6 +232,7 @@ akerr_ErrorContext *akbasic_runtime_init(akbasic_Runtime *obj, akbasic_TextSink
|
||||
PASS(errctx, akbasic_data_state_init(&obj->data_state));
|
||||
PASS(errctx, akbasic_disk_state_init(&obj->disk_state));
|
||||
PASS(errctx, akbasic_audio_state_init(&obj->audio_state));
|
||||
PASS(errctx, akbasic_ui_state_init(&obj->ui_state));
|
||||
PASS(errctx, akbasic_valuepool_init(&obj->valuepool));
|
||||
PASS(errctx, akbasic_value_zero(&obj->staticTrueValue));
|
||||
PASS(errctx, akbasic_value_zero(&obj->staticFalseValue));
|
||||
@@ -265,6 +266,16 @@ akerr_ErrorContext *akbasic_runtime_set_devices(akbasic_Runtime *obj, akbasic_Gr
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akbasic_runtime_set_ui(akbasic_Runtime *obj, akbasic_UiBackend *ui)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL), AKERR_NULLPOINTER,
|
||||
"NULL runtime in set_ui");
|
||||
obj->ui = ui;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akbasic_runtime_set_source_path(akbasic_Runtime *obj, const char *path)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
@@ -1756,6 +1767,16 @@ akerr_ErrorContext *akbasic_runtime_step(akbasic_Runtime *obj)
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* A GETMENU nobody has answered yet holds it the same way, and beside the
|
||||
* keyboard rather than after the clock because it is the same kind of wait:
|
||||
* the program is parked on the player, not on time.
|
||||
*/
|
||||
PASS(errctx, akbasic_ui_service(obj, &blocked));
|
||||
if ( blocked ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* SLEEP and WAIT hold the same way GETKEY does, and the clock is refreshed
|
||||
* before they are asked -- a SLEEP that read a stale clock would wake a step
|
||||
|
||||
@@ -136,6 +136,17 @@ akerr_ErrorContext *akbasic_cmd_new(akbasic_Runtime *obj, akbasic_ASTLeaf *expr,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* And every widget comes down. Unlike a sprite there *is* an entry point
|
||||
* that undefines these, so this one is complete rather than half: a menu the
|
||||
* deleted program put up would otherwise sit on screen eating the cursor
|
||||
* keys, with nothing left running that knows how to retire it.
|
||||
*/
|
||||
PASS(errctx, akbasic_ui_state_init(&obj->ui_state));
|
||||
if ( obj->ui != NULL && obj->ui->clear != NULL ) {
|
||||
PASS(errctx, obj->ui->clear(obj->ui));
|
||||
}
|
||||
|
||||
/*
|
||||
* The line counter goes home too. Without this a NEW typed at the REPL
|
||||
* leaves the next entered line filed under wherever the last program
|
||||
|
||||
535
src/runtime_ui.c
Normal file
535
src/runtime_ui.c
Normal file
@@ -0,0 +1,535 @@
|
||||
/**
|
||||
* @file runtime_ui.c
|
||||
* @brief The group K verbs: MENU, GETMENU, RMENU, DIALOG, HUD and UISTYLE.
|
||||
*
|
||||
* Nothing here includes an SDL or a libakgl header. Every call goes through the
|
||||
* akbasic_UiBackend record the host attached, which is what lets the whole group
|
||||
* be tested in a build with no SDL on the machine.
|
||||
*
|
||||
* None of these is in the Go reference and none is in Commodore BASIC 7.0 --
|
||||
* a C128 had no widget toolkit to wrap. They exist because libakgl 0.9.0 grew
|
||||
* one, and their shapes come from the verbs already here rather than from
|
||||
* anywhere else: MENU retires the way SOLID does, GETMENU holds the step loop
|
||||
* the way GETKEY does, and RMENU reads-and-clears the way BUMP does.
|
||||
*
|
||||
* The backend is a set of *setters*, not a set of draw calls. libakgl's UI is
|
||||
* immediate mode -- widgets are re-declared inside a frame bracket every frame
|
||||
* and clay borrows the text until the bracket closes -- and a BASIC program is
|
||||
* not. See include/akbasic/ui.h for the whole of that reasoning.
|
||||
*/
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <akerror.h>
|
||||
|
||||
#include <akbasic/args.h>
|
||||
#include <akbasic/error.h>
|
||||
#include <akbasic/runtime.h>
|
||||
#include <akbasic/ui.h>
|
||||
|
||||
#include "verbs.h"
|
||||
|
||||
/* Most verbs answer "did something happen"; this is that answer. */
|
||||
#define SUCCEED_TRUE(__obj, __dest) \
|
||||
do { \
|
||||
*(__dest) = &(__obj)->staticTrueValue; \
|
||||
} while ( 0 )
|
||||
|
||||
/**
|
||||
* @brief Refuse politely when the host lent us no UI device.
|
||||
*
|
||||
* Every verb in this file opens with it, and it has to name the verb: "no UI
|
||||
* device" on its own tells a program author nothing about which line to look at.
|
||||
*/
|
||||
static akerr_ErrorContext *require_ui(akbasic_Runtime *obj, const char *verb)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL && verb != NULL), AKERR_NULLPOINTER,
|
||||
"NULL argument in require_ui");
|
||||
FAIL_ZERO_RETURN(errctx, (obj->ui != NULL), AKBASIC_ERR_DEVICE,
|
||||
"%s needs a UI device and this runtime has none", verb);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Turn a BASIC menu number into a zero-based slot, or refuse.
|
||||
*
|
||||
* Menus are numbered from one, the way sprites and colour sources are. Nothing
|
||||
* in this dialect is numbered from zero and a UI group is a poor place to start.
|
||||
*/
|
||||
static akerr_ErrorContext *menu_index(int64_t n, const char *verb, int *dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (n >= 1 && n <= AKBASIC_UI_MAX_MENUS), AKBASIC_ERR_BOUNDS,
|
||||
"%s: menu %" PRId64 " is outside 1..%d", verb, n, AKBASIC_UI_MAX_MENUS);
|
||||
*dest = (int)(n - 1);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/** @brief The same for a HUD slot. */
|
||||
static akerr_ErrorContext *label_index(int64_t n, const char *verb, int *dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (n >= 1 && n <= AKBASIC_UI_MAX_LABELS), AKBASIC_ERR_BOUNDS,
|
||||
"%s: slot %" PRId64 " is outside 1..%d", verb, n, AKBASIC_UI_MAX_LABELS);
|
||||
*dest = (int)(n - 1);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The nth argument of a command or function, or NULL past the end.
|
||||
*
|
||||
* A plain walk rather than a wrapper around akbasic_args_numbers(), because
|
||||
* every verb in this file mixes numbers and strings in one list and that helper
|
||||
* refuses a string by design -- correctly, for the groups it was written for.
|
||||
*/
|
||||
static akbasic_ASTLeaf *nth_argument(akbasic_ASTLeaf *expr, int n)
|
||||
{
|
||||
akbasic_ASTLeaf *arg = NULL;
|
||||
int i = 0;
|
||||
|
||||
if ( expr == NULL ) {
|
||||
return NULL;
|
||||
}
|
||||
arg = akbasic_leaf_first_argument(expr);
|
||||
for ( i = 0; i < n && arg != NULL; i++ ) {
|
||||
arg = arg->next;
|
||||
}
|
||||
return arg;
|
||||
}
|
||||
|
||||
/** @brief How many arguments the verb was given. */
|
||||
static int argument_count(akbasic_ASTLeaf *expr)
|
||||
{
|
||||
akbasic_ASTLeaf *arg = NULL;
|
||||
int count = 0;
|
||||
|
||||
if ( expr == NULL ) {
|
||||
return 0;
|
||||
}
|
||||
for ( arg = akbasic_leaf_first_argument(expr); arg != NULL; arg = arg->next ) {
|
||||
count += 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/** @brief Evaluate argument @p n and insist it is a number. */
|
||||
static akerr_ErrorContext *nth_number(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, const char *verb, int n, int64_t *dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_ASTLeaf *arg = nth_argument(expr, n);
|
||||
akbasic_Value *value = NULL;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (arg != NULL), AKBASIC_ERR_SYNTAX,
|
||||
"%s is missing argument %d", verb, n + 1);
|
||||
PASS(errctx, akbasic_runtime_evaluate(obj, arg, &value));
|
||||
FAIL_NONZERO_RETURN(errctx, (value->valuetype == AKBASIC_TYPE_STRING), AKBASIC_ERR_TYPE,
|
||||
"%s expected a number in argument %d", verb, n + 1);
|
||||
*dest = (value->valuetype == AKBASIC_TYPE_FLOAT)
|
||||
? (int64_t)value->floatval : value->intval;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Evaluate argument @p n, insist it is a string, and copy it out.
|
||||
*
|
||||
* Copied rather than pointed at. The value it came from lives in the per-line
|
||||
* pool, and a caller collecting sixteen of these has no promise that sixteen
|
||||
* separate slots were used -- so a table of pointers into the pool could quietly
|
||||
* be a table of sixteen pointers to the same string.
|
||||
*/
|
||||
static akerr_ErrorContext *nth_string(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, const char *verb, int n, char *dest, size_t len)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_ASTLeaf *arg = nth_argument(expr, n);
|
||||
akbasic_Value *value = NULL;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (arg != NULL), AKBASIC_ERR_SYNTAX,
|
||||
"%s is missing argument %d", verb, n + 1);
|
||||
PASS(errctx, akbasic_runtime_evaluate(obj, arg, &value));
|
||||
FAIL_ZERO_RETURN(errctx, (value->valuetype == AKBASIC_TYPE_STRING), AKBASIC_ERR_TYPE,
|
||||
"%s expected a string in argument %d", verb, n + 1);
|
||||
strncpy(dest, value->stringval, len - 1);
|
||||
dest[len - 1] = '\0';
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/** @brief Hand back an integer from a function. */
|
||||
static akerr_ErrorContext *integer_result(akbasic_Runtime *obj, int64_t value, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_Value *out = NULL;
|
||||
|
||||
PASS(errctx, akbasic_environment_new_value(obj->environment, &out));
|
||||
PASS(errctx, akbasic_value_zero(out));
|
||||
out->valuetype = AKBASIC_TYPE_INTEGER;
|
||||
out->intval = value;
|
||||
*dest = out;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- MENU --- */
|
||||
|
||||
akerr_ErrorContext *akbasic_cmd_menu(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
/*
|
||||
* The entries are copied here rather than pointed at, so that what reaches
|
||||
* the backend is sixteen distinct strings whatever the value pool did. Four
|
||||
* kilobytes of stack, once, in a verb that is not on any hot path.
|
||||
*/
|
||||
char items[AKBASIC_UI_MAX_MENU_ITEMS][AKBASIC_MAX_STRING_LENGTH];
|
||||
const char *pointers[AKBASIC_UI_MAX_MENU_ITEMS];
|
||||
int count = 0;
|
||||
int entries = 0;
|
||||
int slot = 0;
|
||||
int i = 0;
|
||||
int64_t n = 0;
|
||||
|
||||
(void)lval; (void)rval;
|
||||
PASS(errctx, require_ui(obj, "MENU"));
|
||||
count = argument_count(expr);
|
||||
|
||||
/* No arguments retires them all, the way a bare SOLID does. */
|
||||
if ( count == 0 ) {
|
||||
for ( i = 0; i < AKBASIC_UI_MAX_MENUS; i++ ) {
|
||||
PASS(errctx, obj->ui->menu(obj->ui, i, NULL, 0));
|
||||
obj->ui_state.menudefined[i] = false;
|
||||
}
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
PASS(errctx, nth_number(obj, expr, "MENU", 0, &n));
|
||||
PASS(errctx, menu_index(n, "MENU", &slot));
|
||||
|
||||
/* A number and nothing else retires that one. */
|
||||
if ( count == 1 ) {
|
||||
PASS(errctx, obj->ui->menu(obj->ui, slot, NULL, 0));
|
||||
obj->ui_state.menudefined[slot] = false;
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
entries = count - 1;
|
||||
FAIL_ZERO_RETURN(errctx, (entries <= AKBASIC_UI_MAX_MENU_ITEMS), AKBASIC_ERR_SYNTAX,
|
||||
"MENU takes at most %d entries and was given %d",
|
||||
AKBASIC_UI_MAX_MENU_ITEMS, entries);
|
||||
for ( i = 0; i < entries; i++ ) {
|
||||
PASS(errctx, nth_string(obj, expr, "MENU", i + 1, items[i], sizeof(items[i])));
|
||||
pointers[i] = items[i];
|
||||
}
|
||||
|
||||
PASS(errctx, obj->ui->menu(obj->ui, slot, pointers, entries));
|
||||
obj->ui_state.menudefined[slot] = true;
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------- GETMENU --- */
|
||||
|
||||
/**
|
||||
* @brief Identify the numeric variable a GETMENU assigns into.
|
||||
*
|
||||
* Numeric only. The answer is an entry number, and a program that wanted the
|
||||
* entry's *text* already has it -- it wrote the MENU.
|
||||
*/
|
||||
static akerr_ErrorContext *target_variable(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Variable **dest, char *name, size_t len)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_ASTLeaf *arg = nth_argument(expr, 1);
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (arg != NULL), AKBASIC_ERR_SYNTAX,
|
||||
"GETMENU expected a menu number and a variable");
|
||||
/*
|
||||
* Either numeric spelling -- `C%` or `C!` -- and not a bare `C`, which in
|
||||
* this dialect is a *label* rather than a variable and cannot be assigned to
|
||||
* at all. GETKEY refuses one for the same reason. A string is refused
|
||||
* because the answer is an entry *number*: a program that wanted the entry's
|
||||
* text already has it, since it wrote the MENU.
|
||||
*/
|
||||
FAIL_ZERO_RETURN(errctx,
|
||||
(arg->leaftype == AKBASIC_LEAF_IDENTIFIER_INT ||
|
||||
arg->leaftype == AKBASIC_LEAF_IDENTIFIER_FLOAT),
|
||||
AKBASIC_ERR_TYPE, "GETMENU expected a numeric variable such as C%%");
|
||||
PASS(errctx, akbasic_environment_get(obj->environment, arg->identifier, dest));
|
||||
FAIL_ZERO_RETURN(errctx, (*dest != NULL), AKBASIC_ERR_UNDEFINED,
|
||||
"GETMENU could not reach the variable %s", arg->identifier);
|
||||
strncpy(name, arg->identifier, len - 1);
|
||||
name[len - 1] = '\0';
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/** @brief Write an entry number -- or 0 for "released without a choice" -- into a variable. */
|
||||
static akerr_ErrorContext *store_entry(akbasic_Variable *variable, int64_t entry)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int64_t zerosubscript[1] = { 0 };
|
||||
|
||||
PASS(errctx, akbasic_variable_set_integer(variable, entry, zerosubscript, 1));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akbasic_cmd_getmenu(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_Variable *variable = NULL;
|
||||
char name[64];
|
||||
int slot = 0;
|
||||
int selected = 0;
|
||||
bool activated = false;
|
||||
int64_t n = 0;
|
||||
|
||||
(void)lval; (void)rval;
|
||||
PASS(errctx, require_ui(obj, "GETMENU"));
|
||||
PASS(errctx, nth_number(obj, expr, "GETMENU", 0, &n));
|
||||
PASS(errctx, menu_index(n, "GETMENU", &slot));
|
||||
PASS(errctx, target_variable(obj, expr, &variable, name, sizeof(name)));
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, obj->ui_state.menudefined[slot], AKBASIC_ERR_STATE,
|
||||
"GETMENU: menu %" PRId64 " has no entries; define it with MENU first", n);
|
||||
|
||||
PASS(errctx, obj->ui->menu_state(obj->ui, slot, &selected, &activated, true));
|
||||
if ( activated ) {
|
||||
PASS(errctx, store_entry(variable, (int64_t)selected + 1));
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* Nobody has chosen yet. A C128 would sit here; the library may not, so the
|
||||
* step loop is told to stop advancing the program counter instead. The
|
||||
* program stays on this line, the host keeps its frame rate, and the sprite,
|
||||
* audio and collision services -- which run before this one -- keep running
|
||||
* underneath. See akbasic_ui_service().
|
||||
*/
|
||||
obj->ui_state.waiting = true;
|
||||
obj->ui_state.waitmenu = slot;
|
||||
strncpy(obj->ui_state.variable, name, sizeof(obj->ui_state.variable) - 1);
|
||||
obj->ui_state.variable[sizeof(obj->ui_state.variable) - 1] = '\0';
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------- RMENU --- */
|
||||
|
||||
akerr_ErrorContext *akbasic_fn_rmenu(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int slot = 0;
|
||||
int selected = 0;
|
||||
bool activated = false;
|
||||
int64_t n = 0;
|
||||
int64_t field = 0;
|
||||
|
||||
(void)lval; (void)rval;
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER,
|
||||
"NULL argument in RMENU");
|
||||
PASS(errctx, require_ui(obj, "RMENU"));
|
||||
PASS(errctx, nth_number(obj, expr, "RMENU", 0, &n));
|
||||
PASS(errctx, nth_number(obj, expr, "RMENU", 1, &field));
|
||||
PASS(errctx, menu_index(n, "RMENU", &slot));
|
||||
FAIL_ZERO_RETURN(errctx, (field == 0 || field == 1), AKBASIC_ERR_BOUNDS,
|
||||
"RMENU: field %" PRId64 " is outside 0..1", field);
|
||||
|
||||
/*
|
||||
* Field 1 clears the latch and field 0 does not, which is the whole
|
||||
* difference between them: reading which entry is highlighted must not
|
||||
* consume the fact that one was chosen, and reading that one was chosen must
|
||||
* consume it or a polling loop sees the same choice forever. Same contract
|
||||
* BUMP() carries, and for the same reason.
|
||||
*/
|
||||
PASS(errctx, obj->ui->menu_state(obj->ui, slot, &selected, &activated, (field == 1)));
|
||||
if ( field == 1 ) {
|
||||
/*
|
||||
* An integer -1 or 0, not a BOOLEAN. A BOOLEAN value prints as "true" or
|
||||
* "false" -- the Go reference's %t, kept deliberately and recorded in
|
||||
* src/value.c -- and `PRINT RMENU(1,1)` saying "false" where every other
|
||||
* predicate in this dialect says 0 would be a surprise with no upside.
|
||||
* Both work in `IF RMENU(1,1) THEN`.
|
||||
*/
|
||||
PASS(errctx, integer_result(obj, activated ? AKBASIC_TRUE : 0, dest));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
PASS(errctx, integer_result(obj, (int64_t)selected + 1, dest));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------- DIALOG --- */
|
||||
|
||||
akerr_ErrorContext *akbasic_cmd_dialog(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
char text[AKBASIC_MAX_STRING_LENGTH];
|
||||
int count = 0;
|
||||
|
||||
(void)lval; (void)rval;
|
||||
PASS(errctx, require_ui(obj, "DIALOG"));
|
||||
count = argument_count(expr);
|
||||
|
||||
/*
|
||||
* No argument takes the panel down. libakgl has no dismiss contract at all
|
||||
* -- a declarative frame is the visibility flag -- so this verb is both the
|
||||
* only way up and the only way down.
|
||||
*/
|
||||
if ( count == 0 ) {
|
||||
PASS(errctx, obj->ui->dialog(obj->ui, NULL));
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
FAIL_ZERO_RETURN(errctx, (count == 1), AKBASIC_ERR_SYNTAX,
|
||||
"DIALOG takes one string, or nothing at all");
|
||||
PASS(errctx, nth_string(obj, expr, "DIALOG", 0, text, sizeof(text)));
|
||||
PASS(errctx, obj->ui->dialog(obj->ui, text));
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------- HUD --- */
|
||||
|
||||
akerr_ErrorContext *akbasic_cmd_hud(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
char text[AKBASIC_MAX_STRING_LENGTH];
|
||||
int count = 0;
|
||||
int slot = 0;
|
||||
int i = 0;
|
||||
int64_t n = 0;
|
||||
int64_t anchor = 0;
|
||||
|
||||
(void)lval; (void)rval;
|
||||
PASS(errctx, require_ui(obj, "HUD"));
|
||||
count = argument_count(expr);
|
||||
|
||||
if ( count == 0 ) {
|
||||
for ( i = 0; i < AKBASIC_UI_MAX_LABELS; i++ ) {
|
||||
PASS(errctx, obj->ui->label(obj->ui, i, AKBASIC_UI_ANCHOR_TOP_LEFT, NULL));
|
||||
}
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
PASS(errctx, nth_number(obj, expr, "HUD", 0, &n));
|
||||
PASS(errctx, label_index(n, "HUD", &slot));
|
||||
|
||||
if ( count == 1 ) {
|
||||
PASS(errctx, obj->ui->label(obj->ui, slot, AKBASIC_UI_ANCHOR_TOP_LEFT, NULL));
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
FAIL_ZERO_RETURN(errctx, (count == 3), AKBASIC_ERR_SYNTAX,
|
||||
"HUD expected a slot, an anchor and a string");
|
||||
|
||||
PASS(errctx, nth_number(obj, expr, "HUD", 1, &anchor));
|
||||
FAIL_ZERO_RETURN(errctx, (anchor >= 0 && anchor < AKBASIC_UI_ANCHOR_LIMIT),
|
||||
AKBASIC_ERR_BOUNDS, "HUD: anchor %" PRId64 " is outside 0..%d",
|
||||
anchor, (int)AKBASIC_UI_ANCHOR_LIMIT - 1);
|
||||
PASS(errctx, nth_string(obj, expr, "HUD", 2, text, sizeof(text)));
|
||||
PASS(errctx, obj->ui->label(obj->ui, slot, (int)anchor, text));
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------- UISTYLE --- */
|
||||
|
||||
akerr_ErrorContext *akbasic_cmd_uistyle(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
double args[5];
|
||||
akbasic_Color fill;
|
||||
akbasic_Color edge;
|
||||
akbasic_Color ink;
|
||||
int count = 0;
|
||||
double padding = 0.0;
|
||||
double radius = 0.0;
|
||||
|
||||
(void)lval; (void)rval;
|
||||
PASS(errctx, require_ui(obj, "UISTYLE"));
|
||||
PASS(errctx, akbasic_args_numbers(obj, expr, "UISTYLE", args, 5, &count));
|
||||
|
||||
/*
|
||||
* No arguments hands the widgets back to libakgl's own default, which is a
|
||||
* dark textbox with parchment ink. Three NULLs say "the library's", not
|
||||
* "black": a style is not a thing this interpreter can spell in full, and
|
||||
* inventing values here would make UISTYLE with no arguments mean something
|
||||
* different from never having called it.
|
||||
*/
|
||||
if ( count == 0 ) {
|
||||
PASS(errctx, obj->ui->style(obj->ui, NULL, NULL, NULL, 0.0, 0.0));
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
FAIL_ZERO_RETURN(errctx, (count >= 3), AKBASIC_ERR_SYNTAX,
|
||||
"UISTYLE expected a fill, an edge and an ink colour");
|
||||
|
||||
PASS(errctx, akbasic_graphics_palette((int)args[0], &fill));
|
||||
PASS(errctx, akbasic_graphics_palette((int)args[1], &edge));
|
||||
PASS(errctx, akbasic_graphics_palette((int)args[2], &ink));
|
||||
padding = (count >= 4) ? args[3] : 8.0;
|
||||
radius = (count >= 5) ? args[4] : 0.0;
|
||||
FAIL_NONZERO_RETURN(errctx, (padding < 0.0 || radius < 0.0), AKBASIC_ERR_VALUE,
|
||||
"UISTYLE: padding and radius cannot be negative");
|
||||
|
||||
PASS(errctx, obj->ui->style(obj->ui, &fill, &edge, &ink, padding, radius));
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------- the step loop --- */
|
||||
|
||||
akerr_ErrorContext *akbasic_ui_state_init(akbasic_UiState *obj)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL), AKERR_NULLPOINTER,
|
||||
"NULL argument in ui_state_init");
|
||||
memset(obj, 0, sizeof(*obj));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akbasic_ui_service(akbasic_Runtime *obj, bool *blocked)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_Variable *variable = NULL;
|
||||
int selected = 0;
|
||||
bool activated = false;
|
||||
bool released = false;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL && blocked != NULL), AKERR_NULLPOINTER,
|
||||
"NULL argument in ui_service");
|
||||
*blocked = false;
|
||||
if ( !obj->ui_state.waiting ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* Two ways out that are not a choice, and neither may wedge the script. A
|
||||
* host is allowed to change its mind about what it lends out, and a program
|
||||
* is allowed to retire the menu it is waiting on -- from an interrupt
|
||||
* handler, which is the only place it could -- so both release the hold and
|
||||
* assign 0. Same rule akbasic_input_service() keeps for a withdrawn keyboard.
|
||||
*/
|
||||
if ( obj->ui == NULL ) {
|
||||
released = true;
|
||||
} else if ( !obj->ui_state.menudefined[obj->ui_state.waitmenu] ) {
|
||||
released = true;
|
||||
} else {
|
||||
PASS(errctx, obj->ui->menu_state(obj->ui, obj->ui_state.waitmenu,
|
||||
&selected, &activated, true));
|
||||
if ( !activated ) {
|
||||
*blocked = true;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
}
|
||||
|
||||
obj->ui_state.waiting = false;
|
||||
PASS(errctx, akbasic_environment_get(obj->environment, obj->ui_state.variable, &variable));
|
||||
FAIL_ZERO_RETURN(errctx, (variable != NULL), AKBASIC_ERR_UNDEFINED,
|
||||
"GETMENU could not reach the variable %s", obj->ui_state.variable);
|
||||
PASS(errctx, store_entry(variable, released ? 0 : (int64_t)selected + 1));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
414
src/ui_akgl.c
Normal file
414
src/ui_akgl.c
Normal file
@@ -0,0 +1,414 @@
|
||||
/**
|
||||
* @file ui_akgl.c
|
||||
* @brief Wires the UI backend record to libakgl's clay-backed widget helpers.
|
||||
*
|
||||
* The three helpers -- akgl_ui_dialog, akgl_ui_label and akgl_ui_menu -- and the
|
||||
* frame bracket around them. Nothing here touches clay directly: a BASIC program
|
||||
* gets the widgets, not the layout engine, and `CLAY()` blocks are the route a
|
||||
* game written in C takes instead.
|
||||
*
|
||||
* **This file is where immediate mode meets a language that is not.** libakgl
|
||||
* wants the widget set declared inside a bracket every frame and borrows the
|
||||
* text until the bracket closes; a BASIC program says `MENU 1, "START"` on line
|
||||
* 100 and expects it up on line 900. So the record's entry points are setters
|
||||
* that copy into akbasic_AkglUi, and akbasic_ui_akgl_render() replays the whole
|
||||
* set once a frame. The verbs never see a frame and the frame never sees a BASIC
|
||||
* string.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <akerror.h>
|
||||
|
||||
#include <akgl/error.h>
|
||||
#include <akgl/registry.h>
|
||||
#include <akgl/text.h>
|
||||
#include <akgl/ui.h>
|
||||
|
||||
#include <akbasic/akgl.h>
|
||||
#include <akbasic/error.h>
|
||||
|
||||
/**
|
||||
* @brief Registry name the UI font is loaded under.
|
||||
*
|
||||
* Its own name rather than sharing the sink's, because the sink does not use the
|
||||
* font registry at all -- it holds a `TTF_Font *` it opened itself -- and because
|
||||
* a size is baked into a registered handle, so the UI's font is a different
|
||||
* entry even when it is the same file.
|
||||
*/
|
||||
#define UI_FONT_NAME "akbasic.ui"
|
||||
|
||||
/**
|
||||
* @brief Fail the build if the two menu ceilings ever disagree.
|
||||
*
|
||||
* include/akbasic/ui.h restates AKGL_UI_MENU_MAX_ITEMS rather than including an
|
||||
* akgl header, which is correct and is also exactly the kind of restatement that
|
||||
* rots. A mismatch would overflow akgl_UiMenu::items, so it is a negative array
|
||||
* size here rather than a memory error at frame one.
|
||||
*/
|
||||
typedef char ui_menu_ceilings_agree[
|
||||
(AKBASIC_UI_MAX_MENU_ITEMS == AKGL_UI_MENU_MAX_ITEMS) ? 1 : -1];
|
||||
|
||||
/** @brief Recover the backend's own state, or say that it has none. */
|
||||
static akerr_ErrorContext *state_of(akbasic_UiBackend *self, akbasic_AkglUi **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (self != NULL && dest != NULL), AKERR_NULLPOINTER,
|
||||
"NULL argument in akgl UI backend");
|
||||
*dest = (akbasic_AkglUi *)self->self;
|
||||
FAIL_ZERO_RETURN(errctx, (*dest != NULL), AKERR_NULLPOINTER,
|
||||
"akgl UI backend has no state");
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/** @brief The style every widget draws with, or NULL for libakgl's own default. */
|
||||
static akgl_UiStyle *look_of(akbasic_AkglUi *state)
|
||||
{
|
||||
return state->styled ? &state->style : NULL;
|
||||
}
|
||||
|
||||
/** @brief The colour conversion, which is the whole impedance mismatch. */
|
||||
static SDL_Color to_sdl(akbasic_Color color)
|
||||
{
|
||||
SDL_Color out;
|
||||
|
||||
out.r = color.r;
|
||||
out.g = color.g;
|
||||
out.b = color.b;
|
||||
out.a = color.a;
|
||||
return out;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------- the record's setters --- */
|
||||
|
||||
static akerr_ErrorContext *ui_dialog(akbasic_UiBackend *self, const char *text)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_AkglUi *state = NULL;
|
||||
|
||||
PASS(errctx, state_of(self, &state));
|
||||
if ( text == NULL || text[0] == '\0' ) {
|
||||
state->dialogopen = false;
|
||||
state->dialogtext[0] = '\0';
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
strncpy(state->dialogtext, text, sizeof(state->dialogtext) - 1);
|
||||
state->dialogtext[sizeof(state->dialogtext) - 1] = '\0';
|
||||
state->dialogopen = true;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
static akerr_ErrorContext *ui_label(akbasic_UiBackend *self, int slot, int anchor, const char *text)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_AkglUi *state = NULL;
|
||||
|
||||
PASS(errctx, state_of(self, &state));
|
||||
FAIL_ZERO_RETURN(errctx, (slot >= 0 && slot < AKBASIC_UI_MAX_LABELS),
|
||||
AKERR_OUTOFBOUNDS, "HUD slot %d is outside the backend's range", slot);
|
||||
if ( text == NULL ) {
|
||||
state->labelset[slot] = false;
|
||||
state->labeltext[slot][0] = '\0';
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
strncpy(state->labeltext[slot], text, sizeof(state->labeltext[slot]) - 1);
|
||||
state->labeltext[slot][sizeof(state->labeltext[slot]) - 1] = '\0';
|
||||
state->labelanchor[slot] = anchor;
|
||||
state->labelset[slot] = true;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
static akerr_ErrorContext *ui_menu(akbasic_UiBackend *self, int slot, const char *const *items, int count)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_AkglUi *state = NULL;
|
||||
int i = 0;
|
||||
|
||||
PASS(errctx, state_of(self, &state));
|
||||
FAIL_ZERO_RETURN(errctx, (slot >= 0 && slot < AKBASIC_UI_MAX_MENUS),
|
||||
AKERR_OUTOFBOUNDS, "MENU slot %d is outside the backend's range", slot);
|
||||
FAIL_ZERO_RETURN(errctx, (count >= 0 && count <= AKBASIC_UI_MAX_MENU_ITEMS),
|
||||
AKERR_OUTOFBOUNDS, "MENU entry count %d is outside 0..%d",
|
||||
count, AKBASIC_UI_MAX_MENU_ITEMS);
|
||||
FAIL_NONZERO_RETURN(errctx, (count > 0 && items == NULL), AKERR_NULLPOINTER,
|
||||
"MENU was given %d entries and no list", count);
|
||||
|
||||
/*
|
||||
* Redefining resets the selection and the latch. The entries have just
|
||||
* changed meaning, so an index into the old list is not worth carrying over
|
||||
* and an unread activation of an entry that no longer exists is worse.
|
||||
*/
|
||||
for ( i = 0; i < count; i++ ) {
|
||||
FAIL_ZERO_RETURN(errctx, (items[i] != NULL), AKERR_NULLPOINTER,
|
||||
"MENU entry %d is NULL", i + 1);
|
||||
strncpy(state->menuitems[slot][i], items[i], sizeof(state->menuitems[slot][i]) - 1);
|
||||
state->menuitems[slot][i][sizeof(state->menuitems[slot][i]) - 1] = '\0';
|
||||
state->menus[slot].items[i] = state->menuitems[slot][i];
|
||||
}
|
||||
for ( i = count; i < AKBASIC_UI_MAX_MENU_ITEMS; i++ ) {
|
||||
state->menus[slot].items[i] = NULL;
|
||||
}
|
||||
state->menus[slot].count = count;
|
||||
state->menus[slot].selected = 0;
|
||||
state->menus[slot].activated = false;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
static akerr_ErrorContext *ui_menu_state(akbasic_UiBackend *self, int slot, int *selected, bool *activated, bool clear)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_AkglUi *state = NULL;
|
||||
|
||||
PASS(errctx, state_of(self, &state));
|
||||
FAIL_ZERO_RETURN(errctx, (selected != NULL && activated != NULL), AKERR_NULLPOINTER,
|
||||
"NULL destination in menu_state");
|
||||
FAIL_ZERO_RETURN(errctx, (slot >= 0 && slot < AKBASIC_UI_MAX_MENUS),
|
||||
AKERR_OUTOFBOUNDS, "MENU slot %d is outside the backend's range", slot);
|
||||
|
||||
*selected = (int)state->menus[slot].selected;
|
||||
*activated = state->menus[slot].activated;
|
||||
if ( clear ) {
|
||||
state->menus[slot].activated = false;
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
static akerr_ErrorContext *ui_style(akbasic_UiBackend *self, akbasic_Color *fill, akbasic_Color *edge, akbasic_Color *ink, double padding, double radius)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_AkglUi *state = NULL;
|
||||
|
||||
PASS(errctx, state_of(self, &state));
|
||||
/*
|
||||
* No colours means the library's default, and the way to say that is to stop
|
||||
* having a style rather than to copy libakgl's values into ours. Copying them
|
||||
* would make UISTYLE-with-no-arguments a snapshot of whatever the default was
|
||||
* on the day this was written.
|
||||
*/
|
||||
if ( fill == NULL || edge == NULL || ink == NULL ) {
|
||||
state->styled = false;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
state->style.fill = to_sdl(*fill);
|
||||
state->style.edge = to_sdl(*edge);
|
||||
state->style.ink = to_sdl(*ink);
|
||||
state->style.padding = (float32_t)padding;
|
||||
state->style.corner_radius = (float32_t)radius;
|
||||
state->style.fontid = state->fontid;
|
||||
state->styled = true;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
static akerr_ErrorContext *ui_clear(akbasic_UiBackend *self)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_AkglUi *state = NULL;
|
||||
int i = 0;
|
||||
|
||||
PASS(errctx, state_of(self, &state));
|
||||
state->dialogopen = false;
|
||||
state->dialogtext[0] = '\0';
|
||||
state->styled = false;
|
||||
for ( i = 0; i < AKBASIC_UI_MAX_LABELS; i++ ) {
|
||||
state->labelset[i] = false;
|
||||
state->labeltext[i][0] = '\0';
|
||||
}
|
||||
for ( i = 0; i < AKBASIC_UI_MAX_MENUS; i++ ) {
|
||||
state->menus[i].count = 0;
|
||||
state->menus[i].selected = 0;
|
||||
state->menus[i].activated = false;
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------- the frame --- */
|
||||
|
||||
/**
|
||||
* @brief Declare every retained widget into the open frame.
|
||||
*
|
||||
* Its own function because it is three loops, and CATCH inside one escapes only
|
||||
* the loop -- PASS is the only thing that may appear in here. The caller CATCHes
|
||||
* this single call, which is what lets it close the bracket on the way out.
|
||||
*
|
||||
* Order is deliberate: labels, then the dialog, then the menus. A menu is the
|
||||
* thing the player is being asked to act on, so nothing else should be able to
|
||||
* overlap it.
|
||||
*/
|
||||
static akerr_ErrorContext AKERR_NOIGNORE *declare_widgets(akbasic_AkglUi *state)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akgl_UiStyle *look = look_of(state);
|
||||
int i = 0;
|
||||
|
||||
for ( i = 0; i < AKBASIC_UI_MAX_LABELS; i++ ) {
|
||||
if ( state->labelset[i] ) {
|
||||
PASS(errctx, akgl_ui_label(state->labelid[i], state->labeltext[i],
|
||||
(akgl_UiAnchor)state->labelanchor[i], look));
|
||||
}
|
||||
}
|
||||
if ( state->dialogopen ) {
|
||||
PASS(errctx, akgl_ui_dialog("dialog", state->dialogtext, look));
|
||||
}
|
||||
for ( i = 0; i < AKBASIC_UI_MAX_MENUS; i++ ) {
|
||||
if ( state->menus[i].count > 0 ) {
|
||||
state->menus[i].style = look;
|
||||
PASS(errctx, akgl_ui_menu(&state->menus[i]));
|
||||
}
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akbasic_ui_akgl_render(akbasic_UiBackend *obj)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_AkglUi *state = NULL;
|
||||
bool opened = false;
|
||||
|
||||
PASS(errctx, state_of(obj, &state));
|
||||
if ( !state->ready ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
ATTEMPT {
|
||||
CATCH(errctx, akgl_ui_frame_begin());
|
||||
opened = true;
|
||||
CATCH(errctx, declare_widgets(state));
|
||||
/*
|
||||
* Cleared before frame_end rather than after, because frame_end closes
|
||||
* the bracket whether it succeeds or fails -- so a CLEANUP that closed it
|
||||
* again would be closing somebody else's next frame.
|
||||
*/
|
||||
opened = false;
|
||||
CATCH(errctx, akgl_ui_frame_end(state->renderer));
|
||||
} CLEANUP {
|
||||
if ( opened ) {
|
||||
IGNORE(akgl_ui_frame_end(state->renderer));
|
||||
}
|
||||
} PROCESS(errctx) {
|
||||
} FINISH(errctx, true);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akbasic_ui_akgl_handle_event(akbasic_UiBackend *obj, SDL_Event *event, bool *consumed)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_AkglUi *state = NULL;
|
||||
int i = 0;
|
||||
|
||||
PASS(errctx, state_of(obj, &state));
|
||||
FAIL_ZERO_RETURN(errctx, (event != NULL && consumed != NULL), AKERR_NULLPOINTER,
|
||||
"NULL argument in ui_akgl_handle_event");
|
||||
*consumed = false;
|
||||
if ( !state->ready ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* The subsystem first: it is mouse-only and never consumes a keystroke, so
|
||||
* this is safe whatever else is going on.
|
||||
*/
|
||||
PASS(errctx, akgl_ui_handle_event(state, event, consumed));
|
||||
if ( *consumed ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* Then each menu that has entries -- and these *do* take Up, Down and
|
||||
* Return. That is why the loop is over menus with a count rather than over
|
||||
* all four: a program with no menu up leaves the cursor keys and Return to
|
||||
* the line editor, which is what makes the REPL usable at all.
|
||||
*/
|
||||
for ( i = 0; i < AKBASIC_UI_MAX_MENUS; i++ ) {
|
||||
if ( state->menus[i].count == 0 ) {
|
||||
continue;
|
||||
}
|
||||
PASS(errctx, akgl_ui_menu_handle_event(&state->menus[i], event, consumed));
|
||||
if ( *consumed ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------- lifecycle --- */
|
||||
|
||||
akerr_ErrorContext *akbasic_ui_init_akgl(akbasic_UiBackend *obj, akbasic_AkglUi *state, akgl_RenderBackend *renderer, const char *fontpath, int fontsize, int width, int height)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int count = 0;
|
||||
int i = 0;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL && state != NULL && fontpath != NULL),
|
||||
AKERR_NULLPOINTER, "NULL argument in ui_init_akgl");
|
||||
FAIL_ZERO_RETURN(errctx, (renderer != NULL), AKERR_NULLPOINTER,
|
||||
"NULL renderer in ui_init_akgl: the host creates it, not this");
|
||||
FAIL_ZERO_RETURN(errctx, (fontsize > 0 && width > 0 && height > 0), AKBASIC_ERR_VALUE,
|
||||
"A %dx%d UI at %d points is not a UI", width, height, fontsize);
|
||||
PASS(errctx, akgl_error_init());
|
||||
|
||||
memset(state, 0, sizeof(*state));
|
||||
state->renderer = renderer;
|
||||
strncpy(state->fontname, UI_FONT_NAME, sizeof(state->fontname) - 1);
|
||||
|
||||
/*
|
||||
* The element ids, once. clay identifies an element by its string and keeps
|
||||
* hover and scroll state against it between frames, so these have to be the
|
||||
* same every frame -- which means they cannot be built on the stack of the
|
||||
* function that declares them.
|
||||
*/
|
||||
for ( i = 0; i < AKBASIC_UI_MAX_LABELS; i++ ) {
|
||||
snprintf(state->labelid[i], sizeof(state->labelid[i]), "hud%d", i + 1);
|
||||
}
|
||||
for ( i = 0; i < AKBASIC_UI_MAX_MENUS; i++ ) {
|
||||
snprintf(state->menuid[i], sizeof(state->menuid[i]), "menu%d", i + 1);
|
||||
state->menus[i].id = state->menuid[i];
|
||||
}
|
||||
(void)count;
|
||||
|
||||
/*
|
||||
* **akgl_registry_init() does not initialize the font registry** -- its own
|
||||
* header says so -- and nothing else in this repository has needed it,
|
||||
* because the text sink opens its font with a bare TTF_OpenFont and holds
|
||||
* the handle. akgl_ui_font_register() resolves a *registry name* per use, so
|
||||
* the font has to go in there. It is idempotent.
|
||||
*/
|
||||
PASS(errctx, akgl_registry_init_font());
|
||||
PASS(errctx, akgl_text_loadfont(state->fontname, (char *)fontpath, fontsize));
|
||||
PASS(errctx, akgl_ui_init(width, height));
|
||||
PASS(errctx, akgl_ui_font_register(state->fontname, &state->fontid));
|
||||
state->ready = true;
|
||||
|
||||
obj->self = state;
|
||||
obj->dialog = ui_dialog;
|
||||
obj->label = ui_label;
|
||||
obj->menu = ui_menu;
|
||||
obj->menu_state = ui_menu_state;
|
||||
obj->style = ui_style;
|
||||
obj->clear = ui_clear;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
void akbasic_ui_akgl_shutdown(akbasic_UiBackend *obj)
|
||||
{
|
||||
akbasic_AkglUi *state = NULL;
|
||||
|
||||
if ( obj == NULL || obj->self == NULL ) {
|
||||
return;
|
||||
}
|
||||
state = (akbasic_AkglUi *)obj->self;
|
||||
if ( !state->ready ) {
|
||||
return;
|
||||
}
|
||||
/*
|
||||
* The subsystem first, then the font: anything still holding a clay layout
|
||||
* would be talking to a disowned context after the shutdown, and the font is
|
||||
* what its text commands resolve through. Both are ignorable -- this is
|
||||
* called from teardown paths that are already unwinding, and a failure to
|
||||
* close something down has nowhere useful to go.
|
||||
*/
|
||||
IGNORE(akgl_ui_shutdown());
|
||||
IGNORE(akgl_text_unloadfont(state->fontname));
|
||||
state->ready = false;
|
||||
}
|
||||
@@ -63,6 +63,7 @@ static const akbasic_Verb VERBS[] = {
|
||||
{ "DCLOSE", AKBASIC_TOK_COMMAND, -1, akbasic_parse_optional_arglist, akbasic_cmd_dclose },
|
||||
{ "DEF", AKBASIC_TOK_COMMAND, -1, akbasic_parse_def, akbasic_cmd_def },
|
||||
{ "DELETE", AKBASIC_TOK_COMMAND_IMMEDIATE, -1, NULL, akbasic_cmd_delete },
|
||||
{ "DIALOG", AKBASIC_TOK_COMMAND, -1, akbasic_parse_optional_arglist, akbasic_cmd_dialog },
|
||||
{ "DIM", AKBASIC_TOK_COMMAND, -1, akbasic_parse_dim, akbasic_cmd_dim },
|
||||
{ "DIRECTORY", AKBASIC_TOK_COMMAND_IMMEDIATE, -1, akbasic_parse_optional_arglist, akbasic_cmd_directory },
|
||||
{ "DLOAD", AKBASIC_TOK_COMMAND_IMMEDIATE, -1, NULL, akbasic_cmd_dload },
|
||||
@@ -81,6 +82,7 @@ static const akbasic_Verb VERBS[] = {
|
||||
{ "FOR", AKBASIC_TOK_COMMAND, -1, akbasic_parse_for, akbasic_cmd_for },
|
||||
{ "GET", AKBASIC_TOK_COMMAND, -1, NULL, akbasic_cmd_get },
|
||||
{ "GETKEY", AKBASIC_TOK_COMMAND, -1, NULL, akbasic_cmd_getkey },
|
||||
{ "GETMENU", AKBASIC_TOK_COMMAND, -1, akbasic_parse_arglist, akbasic_cmd_getmenu },
|
||||
{ "GOSUB", AKBASIC_TOK_COMMAND, -1, NULL, akbasic_cmd_gosub },
|
||||
{ "GOTO", AKBASIC_TOK_COMMAND, -1, NULL, akbasic_cmd_goto },
|
||||
{ "GRAPHIC", AKBASIC_TOK_COMMAND, -1, akbasic_parse_graphic, akbasic_cmd_graphic },
|
||||
@@ -88,6 +90,7 @@ static const akbasic_Verb VERBS[] = {
|
||||
{ "HEADER", AKBASIC_TOK_COMMAND, -1, akbasic_parse_optional_arglist, akbasic_cmd_header },
|
||||
{ "HELP", AKBASIC_TOK_COMMAND_IMMEDIATE, -1, NULL, akbasic_cmd_help },
|
||||
{ "HEX", AKBASIC_TOK_FUNCTION, 1, NULL, akbasic_fn_hex },
|
||||
{ "HUD", AKBASIC_TOK_COMMAND, -1, akbasic_parse_optional_arglist, akbasic_cmd_hud },
|
||||
{ "IF", AKBASIC_TOK_COMMAND, -1, akbasic_parse_if, akbasic_cmd_if },
|
||||
{ "INPUT", AKBASIC_TOK_COMMAND, -1, akbasic_parse_input, akbasic_cmd_input },
|
||||
/*
|
||||
@@ -109,6 +112,7 @@ static const akbasic_Verb VERBS[] = {
|
||||
{ "LOCATE", AKBASIC_TOK_COMMAND, -1, akbasic_parse_arglist, akbasic_cmd_locate },
|
||||
{ "LOG", AKBASIC_TOK_FUNCTION, 1, NULL, akbasic_fn_log },
|
||||
{ "LOOP", AKBASIC_TOK_COMMAND, -1, akbasic_parse_loop, akbasic_cmd_loop },
|
||||
{ "MENU", AKBASIC_TOK_COMMAND, -1, akbasic_parse_optional_arglist, akbasic_cmd_menu },
|
||||
{ "MID", AKBASIC_TOK_FUNCTION, 3, NULL, akbasic_fn_mid },
|
||||
{ "MOD", AKBASIC_TOK_FUNCTION, 2, NULL, akbasic_fn_mod },
|
||||
{ "MOVSPR", AKBASIC_TOK_COMMAND, -1, akbasic_parse_movspr, akbasic_cmd_movspr },
|
||||
@@ -140,6 +144,7 @@ static const akbasic_Verb VERBS[] = {
|
||||
{ "RETURN", AKBASIC_TOK_COMMAND, -1, NULL, akbasic_cmd_return },
|
||||
{ "RGR", AKBASIC_TOK_FUNCTION, 1, NULL, akbasic_fn_rgr },
|
||||
{ "RIGHT", AKBASIC_TOK_FUNCTION, 2, NULL, akbasic_fn_right },
|
||||
{ "RMENU", AKBASIC_TOK_FUNCTION, 2, NULL, akbasic_fn_rmenu },
|
||||
{ "RSPCOLOR", AKBASIC_TOK_FUNCTION, 1, NULL, akbasic_fn_rspcolor },
|
||||
{ "RSPHIT", AKBASIC_TOK_FUNCTION, 2, NULL, akbasic_fn_rsphit },
|
||||
{ "RSPPOS", AKBASIC_TOK_FUNCTION, 2, NULL, akbasic_fn_rsppos },
|
||||
@@ -177,6 +182,7 @@ static const akbasic_Verb VERBS[] = {
|
||||
{ "TROFF", AKBASIC_TOK_COMMAND, -1, NULL, akbasic_cmd_troff },
|
||||
{ "TRON", AKBASIC_TOK_COMMAND, -1, NULL, akbasic_cmd_tron },
|
||||
{ "TYPE", AKBASIC_TOK_COMMAND, -1, akbasic_parse_type, akbasic_cmd_type },
|
||||
{ "UISTYLE", AKBASIC_TOK_COMMAND, -1, akbasic_parse_optional_arglist, akbasic_cmd_uistyle },
|
||||
{ "UNTIL", AKBASIC_TOK_COMMAND, -1, NULL, NULL },
|
||||
{ "USING", AKBASIC_TOK_COMMAND, -1, NULL, NULL },
|
||||
{ "VAL", AKBASIC_TOK_FUNCTION, 1, NULL, akbasic_fn_val },
|
||||
|
||||
@@ -198,4 +198,12 @@ akerr_ErrorContext AKERR_NOIGNORE *akbasic_cmd_get(struct akbasic_Runtime *obj,
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_cmd_getkey(struct akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_cmd_scnclr(struct akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest);
|
||||
|
||||
/* Group K user interface verbs and readbacks -- src/runtime_ui.c */
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_cmd_dialog(struct akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_cmd_getmenu(struct akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_cmd_hud(struct akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_cmd_menu(struct akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_cmd_uistyle(struct akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_fn_rmenu(struct akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest);
|
||||
|
||||
#endif // _AKBASIC_SRC_VERBS_H_
|
||||
|
||||
Reference in New Issue
Block a user