Rebuild UI as a scriptable element tree, archive the old system
The old UI system (X-macro static element list, hand-authored C screens/widgets/focus stack) is archived under archive/ rather than deleted, since it's a useful reference during the rewrite. New native UI element pool (src/dusk/ui): a flat UI_ELEMENTS[128] pool of tagged-union elements (Label, Rectangle, Scripted), a real persistent parent/child tree (children[8] per element, cycle- and capacity-checked uiElementSetParent), always-fresh worldX/worldY (cheap enough to recompute on every read, no dirty-flag cache needed), and cascading dispose. Rendering stays manual/immediate: a scripted element with no render() override auto-renders its children by default, but overriding render() takes full control (an override must call renderChildren() itself to opt back in) -- this is deliberately preserved end to end via a render()-shadow trampoline so overriding render() always keeps working the same way regardless of how a node is reached. New scripting layer (src/dusk/script/module/ui): UIElement/Label/ Rectangle JS classes (Label/Rectangle share UIElement's prototype via manual chaining, not JS `extends`), exposing x/y/worldX/worldY/parent/ add()/remove()/render()/renderChildren()/dispose(). UI.add()/ UI.remove() manage top-level render roots, mutually exclusive with being someone's child. Also: Scene gains a lateUpdate() hook (called once per frame after every other update, for things like camera-follow that need to react to where everything else ended up); several duskrpg call sites (cutscene items, entityinteractable, entityplayer) that depended on the now-archived RPG textbox are stubbed to console output instead of a dialogue box, pending the new UI reaching that far. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -19,5 +19,4 @@ add_subdirectory(scene)
|
||||
# add_subdirectory(item)
|
||||
add_subdirectory(script)
|
||||
add_subdirectory(time)
|
||||
add_subdirectory(ui)
|
||||
add_subdirectory(util)
|
||||
@@ -21,3 +21,5 @@ target_compile_definitions(test_scriptinput PRIVATE
|
||||
)
|
||||
|
||||
dusktest(test_moduleui.c)
|
||||
|
||||
dusktest(test_moduleuielement.c)
|
||||
|
||||
+37
-35
@@ -8,11 +8,16 @@
|
||||
#include "dusktest.h"
|
||||
#include "console/console.h"
|
||||
#include "script/scriptmanager.h"
|
||||
#include "ui/ui.h"
|
||||
#include "util/memory.h"
|
||||
|
||||
static int ui_setup(void **state) {
|
||||
consoleInit();
|
||||
|
||||
memoryZero(UI_ELEMENTS, sizeof(UI_ELEMENTS));
|
||||
memoryZero(&UI, sizeof(UI));
|
||||
for(uint8_t i = 0; i < UI_ROOT_MAX; i++) UI.root[i] = UI_ELEMENT_ID_INVALID;
|
||||
|
||||
errorret_t ret = scriptManagerInit();
|
||||
if(errorIsNotOk(ret)) { errorCatch(ret); return -1; }
|
||||
|
||||
@@ -27,56 +32,53 @@ static int ui_teardown(void **state) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_ui_add_logs_prefixed_args(void **state) {
|
||||
static void test_ui_add_requires_an_argument(void **state) {
|
||||
errorret_t ret = scriptManagerExec("UI.add();", NULL);
|
||||
assert_true(errorIsNotOk(ret));
|
||||
errorCatch(ret);
|
||||
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_ui_add_requires_an_object(void **state) {
|
||||
errorret_t ret = scriptManagerExec("UI.add('button');", NULL);
|
||||
assert_true(errorIsNotOk(ret));
|
||||
errorCatch(ret);
|
||||
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_ui_add_requires_a_uielement(void **state) {
|
||||
errorret_t ret = scriptManagerExec("UI.add({});", NULL);
|
||||
assert_true(errorIsNotOk(ret));
|
||||
errorCatch(ret);
|
||||
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_ui_remove_of_untracked_element_is_a_noop(void **state) {
|
||||
jerry_value_t result;
|
||||
errorret_t ret = scriptManagerExec(
|
||||
"UI.add('button', 1, true);", &result
|
||||
"UI.remove(new UIElement());", &result
|
||||
);
|
||||
assert_true(errorIsOk(ret));
|
||||
jerry_value_free(result);
|
||||
|
||||
assert_string_equal(
|
||||
CONSOLE.line[CONSOLE_HISTORY_MAX - 1], "UI.add button 1 true"
|
||||
);
|
||||
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_ui_remove_logs_prefixed_args(void **state) {
|
||||
jerry_value_t result;
|
||||
errorret_t ret = scriptManagerExec("UI.remove('button');", &result);
|
||||
assert_true(errorIsOk(ret));
|
||||
jerry_value_free(result);
|
||||
|
||||
assert_string_equal(
|
||||
CONSOLE.line[CONSOLE_HISTORY_MAX - 1], "UI.remove button"
|
||||
);
|
||||
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_ui_add_no_args_logs_just_the_name(void **state) {
|
||||
jerry_value_t result;
|
||||
errorret_t ret = scriptManagerExec("UI.add();", &result);
|
||||
assert_true(errorIsOk(ret));
|
||||
jerry_value_free(result);
|
||||
|
||||
assert_string_equal(CONSOLE.line[CONSOLE_HISTORY_MAX - 1], "UI.add");
|
||||
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
assertInit();
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_ui_add_logs_prefixed_args, ui_setup, ui_teardown
|
||||
test_ui_add_requires_an_argument, ui_setup, ui_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_ui_remove_logs_prefixed_args, ui_setup, ui_teardown
|
||||
test_ui_add_requires_an_object, ui_setup, ui_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_ui_add_no_args_logs_just_the_name, ui_setup, ui_teardown
|
||||
test_ui_add_requires_a_uielement, ui_setup, ui_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_ui_remove_of_untracked_element_is_a_noop, ui_setup, ui_teardown
|
||||
),
|
||||
};
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
|
||||
@@ -0,0 +1,369 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "dusktest.h"
|
||||
#include "console/console.h"
|
||||
#include "script/scriptmanager.h"
|
||||
#include "ui/ui.h"
|
||||
#include "display/text/font.h"
|
||||
#include "util/memory.h"
|
||||
|
||||
// Label's text rebuild (uiLabelSetText -> textBuildSpriteCache) only
|
||||
// ever reads font->tileset (pure geometry math), never font->texture --
|
||||
// so a fake tileset with no real GPU texture behind it is enough to
|
||||
// exercise it here, without needing the real FONT_DEFAULT (which this
|
||||
// headless test binary can't initialize -- fontInitDefault() uploads a
|
||||
// real GL texture, and there's no GL context in a plain cmocka binary).
|
||||
static tileset_t TEST_FAKE_TILESET = {
|
||||
.tileWidth = 6, .tileHeight = 10, .tileCount = 100,
|
||||
.columns = 10, .rows = 10, .uv = { 0.1f, 0.1f }
|
||||
};
|
||||
static font_t TEST_FAKE_FONT = { .texture = NULL, .tileset = &TEST_FAKE_TILESET };
|
||||
|
||||
static int uielement_setup(void **state) {
|
||||
consoleInit();
|
||||
|
||||
memoryZero(UI_ELEMENTS, sizeof(UI_ELEMENTS));
|
||||
memoryZero(&UI, sizeof(UI));
|
||||
for(uint8_t i = 0; i < UI_ROOT_MAX; i++) UI.root[i] = UI_ELEMENT_ID_INVALID;
|
||||
|
||||
errorret_t ret = scriptManagerInit();
|
||||
if(errorIsNotOk(ret)) { errorCatch(ret); return -1; }
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int uielement_teardown(void **state) {
|
||||
errorret_t ret = scriptManagerDispose();
|
||||
if(errorIsNotOk(ret)) errorCatch(ret);
|
||||
|
||||
consoleDispose();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void exec(const char_t *script) {
|
||||
jerry_value_t result;
|
||||
errorret_t ret = scriptManagerExec(script, &result);
|
||||
if(errorIsNotOk(ret)) {
|
||||
errorCatch(errorPrint(ret));
|
||||
fail_msg("Script execution failed (see printed error above)");
|
||||
}
|
||||
assert_true(errorIsOk(ret));
|
||||
jerry_value_free(result);
|
||||
}
|
||||
|
||||
static void execExpectError(const char_t *script) {
|
||||
errorret_t ret = scriptManagerExec(script, NULL);
|
||||
assert_true(errorIsNotOk(ret));
|
||||
errorCatch(ret);
|
||||
}
|
||||
|
||||
static void test_uielement_x_y_readback(void **state) {
|
||||
exec(
|
||||
"var el = new UIElement();"
|
||||
"el.x = 10;"
|
||||
"el.y = 20;"
|
||||
"if(el.x !== 10 || el.y !== 20) throw new Error('x/y mismatch');"
|
||||
);
|
||||
}
|
||||
|
||||
static void test_uielement_construct_calls_init(void **state) {
|
||||
exec(
|
||||
"class Foo extends UIElement {"
|
||||
" init() { this.x = 42; }"
|
||||
"}"
|
||||
"var foo = new Foo();"
|
||||
"if(foo.x !== 42) throw new Error('init() did not run');"
|
||||
);
|
||||
}
|
||||
|
||||
static void test_uielement_render_no_override_is_noop_when_childless(
|
||||
void **state
|
||||
) {
|
||||
// No override, no children -- render() should just do nothing, safely.
|
||||
exec("new UIElement().render();");
|
||||
}
|
||||
|
||||
static void test_uielement_render_dispatches_to_js_override(void **state) {
|
||||
exec(
|
||||
"class Foo extends UIElement {"
|
||||
" render() { this.rendered = true; }"
|
||||
"}"
|
||||
"var foo = new Foo();"
|
||||
"foo.render();"
|
||||
"if(foo.rendered !== true) throw new Error('render() override not called');"
|
||||
);
|
||||
}
|
||||
|
||||
static void test_uielement_add_sets_parent_and_world_position(void **state) {
|
||||
// No render() anywhere -- worldX/worldY/parent are set by add() itself
|
||||
// and always current, not just "as of last render()".
|
||||
exec(
|
||||
"class Child extends UIElement {"
|
||||
" init() { this.x = 32; this.y = 32; }"
|
||||
"}"
|
||||
"class Parent extends UIElement {"
|
||||
" init() {"
|
||||
" this.child = new Child();"
|
||||
" this.add(this.child);"
|
||||
" this.x = 64;"
|
||||
" this.y = 64;"
|
||||
" }"
|
||||
"}"
|
||||
"var parent = new Parent();"
|
||||
"if(parent.child.worldX !== 96) throw new Error('worldX wrong: ' + parent.child.worldX);"
|
||||
"if(parent.child.worldY !== 96) throw new Error('worldY wrong: ' + parent.child.worldY);"
|
||||
"if(parent.child.parent !== parent) throw new Error('parent identity wrong');"
|
||||
"if(parent.child.parent.x !== 64) throw new Error('parent.x wrong');"
|
||||
"if(parent.childCount !== 1) throw new Error('childCount wrong: ' + parent.childCount);"
|
||||
);
|
||||
}
|
||||
|
||||
static void test_uielement_no_override_auto_renders_children(void **state) {
|
||||
exec(
|
||||
"class Child extends UIElement {"
|
||||
" render() { this.rendered = true; }"
|
||||
"}"
|
||||
"var parent = new UIElement();"
|
||||
"var child = new Child();"
|
||||
"parent.add(child);"
|
||||
"parent.render();"
|
||||
"if(child.rendered !== true) throw new Error('child was not auto-rendered');"
|
||||
);
|
||||
}
|
||||
|
||||
static void test_uielement_override_does_not_auto_render_children(
|
||||
void **state
|
||||
) {
|
||||
exec(
|
||||
"class Child extends UIElement {"
|
||||
" render() { this.rendered = true; }"
|
||||
"}"
|
||||
"class Parent extends UIElement {"
|
||||
" render() { this.overrideCalled = true; }"
|
||||
"}"
|
||||
"var parent = new Parent();"
|
||||
"var child = new Child();"
|
||||
"parent.add(child);"
|
||||
"parent.render();"
|
||||
"if(parent.overrideCalled !== true) throw new Error('override not called');"
|
||||
"if(child.rendered === true) {"
|
||||
" throw new Error('child should not auto-render under an override');"
|
||||
"}"
|
||||
);
|
||||
}
|
||||
|
||||
static void test_uielement_render_children_explicit_opt_in(void **state) {
|
||||
exec(
|
||||
"class Child extends UIElement {"
|
||||
" render() { this.rendered = true; }"
|
||||
"}"
|
||||
"class Parent extends UIElement {"
|
||||
" render() { this.renderChildren(); }"
|
||||
"}"
|
||||
"var parent = new Parent();"
|
||||
"var child = new Child();"
|
||||
"parent.add(child);"
|
||||
"parent.render();"
|
||||
"if(child.rendered !== true) throw new Error('renderChildren() did not render child');"
|
||||
);
|
||||
}
|
||||
|
||||
static void test_uielement_add_self_throws(void **state) {
|
||||
execExpectError("var el = new UIElement(); el.add(el);");
|
||||
}
|
||||
|
||||
static void test_uielement_add_cycle_throws(void **state) {
|
||||
execExpectError(
|
||||
"var a = new UIElement();"
|
||||
"var b = new UIElement();"
|
||||
"a.add(b);"
|
||||
"b.add(a);"
|
||||
);
|
||||
}
|
||||
|
||||
static void test_uielement_add_reparents(void **state) {
|
||||
exec(
|
||||
"var parentA = new UIElement();"
|
||||
"var parentB = new UIElement();"
|
||||
"var child = new UIElement();"
|
||||
"parentA.add(child);"
|
||||
"if(parentA.childCount !== 1) throw new Error('parentA should have 1 child');"
|
||||
"parentB.add(child);"
|
||||
"if(parentA.childCount !== 0) {"
|
||||
" throw new Error('parentA should have 0 children after reparent');"
|
||||
"}"
|
||||
"if(parentB.childCount !== 1) throw new Error('parentB should have 1 child');"
|
||||
"if(child.parent !== parentB) throw new Error('child.parent should be parentB');"
|
||||
);
|
||||
}
|
||||
|
||||
static void test_uielement_add_capacity_exceeded_throws(void **state) {
|
||||
exec(
|
||||
"globalThis.parent = new UIElement();"
|
||||
"for(var i = 0; i < 8; i++) parent.add(new UIElement());"
|
||||
"if(parent.childCount !== 8) throw new Error('expected 8 children');"
|
||||
);
|
||||
execExpectError("parent.add(new UIElement());");
|
||||
}
|
||||
|
||||
static void test_uielement_dispose_cascades(void **state) {
|
||||
// Freshly-reset pool (see uielement_setup) -- parent is id 0, child is
|
||||
// id 1, in construction order.
|
||||
exec(
|
||||
"globalThis.parent = new UIElement();"
|
||||
"globalThis.child = new UIElement();"
|
||||
"parent.add(child);"
|
||||
"UI.add(parent);"
|
||||
);
|
||||
|
||||
assert_true(UI.root[0] == 0);
|
||||
assert_true(UI_ELEMENTS[1].type != UI_ELEMENT_TYPE_NULL);
|
||||
|
||||
exec("parent.dispose();");
|
||||
|
||||
assert_true(UI.root[0] == UI_ELEMENT_ID_INVALID);
|
||||
assert_true(UI_ELEMENTS[0].type == UI_ELEMENT_TYPE_NULL);
|
||||
assert_true(UI_ELEMENTS[1].type == UI_ELEMENT_TYPE_NULL);
|
||||
}
|
||||
|
||||
static void test_uielement_dispose_is_idempotent(void **state) {
|
||||
exec(
|
||||
"globalThis.el = new UIElement();"
|
||||
"el.dispose();"
|
||||
"el.dispose();"
|
||||
);
|
||||
}
|
||||
|
||||
static void test_label_text_default_and_set(void **state) {
|
||||
// Freshly-reset pool (see uielement_setup) -- the first element
|
||||
// constructed in this test is always id 0.
|
||||
exec("globalThis.label = new Label();");
|
||||
UI_ELEMENTS[0].label.font = &TEST_FAKE_FONT;
|
||||
|
||||
exec(
|
||||
"if(label.text !== '') throw new Error('default text not empty');"
|
||||
"label.text = 'Hello World!';"
|
||||
"if(label.text !== 'Hello World!') throw new Error('text readback wrong');"
|
||||
);
|
||||
}
|
||||
|
||||
static void test_rectangle_size_and_color(void **state) {
|
||||
exec(
|
||||
"var rect = new Rectangle();"
|
||||
"rect.width = 100;"
|
||||
"rect.height = 50;"
|
||||
"rect.color = { r: 255, g: 0, b: 0, a: 255 };"
|
||||
"if(rect.width !== 100 || rect.height !== 50) {"
|
||||
" throw new Error('size readback wrong');"
|
||||
"}"
|
||||
"if(rect.color.r !== 255 || rect.color.g !== 0 || rect.color.b !== 0) {"
|
||||
" throw new Error('color readback wrong');"
|
||||
"}"
|
||||
);
|
||||
}
|
||||
|
||||
static void test_ui_add_and_remove(void **state) {
|
||||
assert_true(UI.root[0] == UI_ELEMENT_ID_INVALID);
|
||||
|
||||
exec(
|
||||
"var el = new UIElement();"
|
||||
"el.x = 7;"
|
||||
"UI.add(el);"
|
||||
);
|
||||
|
||||
assert_true(UI.root[0] != UI_ELEMENT_ID_INVALID);
|
||||
assert_true(UI_ELEMENTS[UI.root[0]].type == UI_ELEMENT_TYPE_SCRIPTED);
|
||||
assert_float_equal(UI_ELEMENTS[UI.root[0]].x, 7.0f, 0.001f);
|
||||
|
||||
exec("UI.remove(el);");
|
||||
|
||||
assert_true(UI.root[0] == UI_ELEMENT_ID_INVALID);
|
||||
}
|
||||
|
||||
static void test_ui_add_detaches_from_parent(void **state) {
|
||||
exec(
|
||||
"globalThis.parent = new UIElement();"
|
||||
"globalThis.child = new UIElement();"
|
||||
"parent.add(child);"
|
||||
"if(parent.childCount !== 1) throw new Error('expected 1 child');"
|
||||
"UI.add(child);"
|
||||
"if(parent.childCount !== 0) {"
|
||||
" throw new Error('expected 0 children after UI.add');"
|
||||
"}"
|
||||
"if(child.parent !== undefined) throw new Error('child.parent should be undefined');"
|
||||
);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
assertInit();
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_uielement_x_y_readback, uielement_setup, uielement_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_uielement_construct_calls_init, uielement_setup, uielement_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_uielement_render_no_override_is_noop_when_childless,
|
||||
uielement_setup, uielement_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_uielement_render_dispatches_to_js_override,
|
||||
uielement_setup, uielement_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_uielement_add_sets_parent_and_world_position,
|
||||
uielement_setup, uielement_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_uielement_no_override_auto_renders_children,
|
||||
uielement_setup, uielement_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_uielement_override_does_not_auto_render_children,
|
||||
uielement_setup, uielement_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_uielement_render_children_explicit_opt_in,
|
||||
uielement_setup, uielement_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_uielement_add_self_throws, uielement_setup, uielement_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_uielement_add_cycle_throws, uielement_setup, uielement_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_uielement_add_reparents, uielement_setup, uielement_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_uielement_add_capacity_exceeded_throws,
|
||||
uielement_setup, uielement_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_uielement_dispose_cascades, uielement_setup, uielement_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_uielement_dispose_is_idempotent,
|
||||
uielement_setup, uielement_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_label_text_default_and_set, uielement_setup, uielement_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_rectangle_size_and_color, uielement_setup, uielement_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_ui_add_and_remove, uielement_setup, uielement_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_ui_add_detaches_from_parent, uielement_setup, uielement_teardown
|
||||
),
|
||||
};
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
# Copyright (c) 2026 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
include(dusktest)
|
||||
|
||||
# Tests
|
||||
dusktest(test_uiframe.c)
|
||||
dusktest(test_uislider.c)
|
||||
dusktest(test_uitab.c)
|
||||
@@ -1,68 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "dusktest.h"
|
||||
#include "ui/frame/uiframe.h"
|
||||
|
||||
static void test_uiFrameBuildSprites_layout(void **state) {
|
||||
UI_FRAME.tileset = (tileset_t){
|
||||
.tileWidth = 1,
|
||||
.tileHeight = 1,
|
||||
.tileCount = 9,
|
||||
.columns = 3,
|
||||
.rows = 3,
|
||||
.uv = { 0.25f, 0.25f }
|
||||
};
|
||||
|
||||
const float_t x = 100.0f;
|
||||
const float_t y = 50.0f;
|
||||
const float_t width = 40.0f;
|
||||
const float_t height = 30.0f;
|
||||
const float_t tileW = (float_t)UI_FRAME_BORDER_WIDTH;
|
||||
const float_t tileH = (float_t)UI_FRAME_BORDER_HEIGHT;
|
||||
|
||||
spritebatchsprite_t sprites[9];
|
||||
uiFrameBuildSprites(sprites, x, y, width, height);
|
||||
|
||||
// Top-left corner sits exactly at the rect's origin.
|
||||
assert_float_equal(sprites[0].min[0], x, 0.0001f);
|
||||
assert_float_equal(sprites[0].min[1], y, 0.0001f);
|
||||
assert_float_equal(sprites[0].max[0], x + tileW, 0.0001f);
|
||||
assert_float_equal(sprites[0].max[1], y + tileH, 0.0001f);
|
||||
assert_float_equal(sprites[0].uvMin[0], 0.0f, 0.0001f);
|
||||
assert_float_equal(sprites[0].uvMin[1], 0.0f, 0.0001f);
|
||||
assert_float_equal(sprites[0].uvMax[0], 0.25f, 0.0001f);
|
||||
assert_float_equal(sprites[0].uvMax[1], 0.25f, 0.0001f);
|
||||
|
||||
// Top-middle edge stretches to fill the width minus both corners.
|
||||
assert_float_equal(sprites[1].min[0], x + tileW, 0.0001f);
|
||||
assert_float_equal(sprites[1].max[0], x + width - tileW, 0.0001f);
|
||||
assert_float_equal(sprites[1].max[1], y + tileH, 0.0001f);
|
||||
|
||||
// Center tile fills the remaining interior on both axes.
|
||||
assert_float_equal(sprites[4].min[0], x + tileW, 0.0001f);
|
||||
assert_float_equal(sprites[4].min[1], y + tileH, 0.0001f);
|
||||
assert_float_equal(sprites[4].max[0], x + width - tileW, 0.0001f);
|
||||
assert_float_equal(sprites[4].max[1], y + height - tileH, 0.0001f);
|
||||
|
||||
// Bottom-right corner sits exactly at the rect's far corner, sampling
|
||||
// tileset column 2, row 2.
|
||||
assert_float_equal(sprites[8].min[0], x + width - tileW, 0.0001f);
|
||||
assert_float_equal(sprites[8].min[1], y + height - tileH, 0.0001f);
|
||||
assert_float_equal(sprites[8].max[0], x + width, 0.0001f);
|
||||
assert_float_equal(sprites[8].max[1], y + height, 0.0001f);
|
||||
assert_float_equal(sprites[8].uvMin[0], 0.5f, 0.0001f);
|
||||
assert_float_equal(sprites[8].uvMin[1], 0.5f, 0.0001f);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_uiFrameBuildSprites_layout),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "dusktest.h"
|
||||
#include "ui/widget/uislider.h"
|
||||
#include "util/memory.h"
|
||||
|
||||
// uiSliderInitFloat/InitInt require a real font (FONT_DEFAULT) to build
|
||||
// the label's glyph cache, which needs a live GL context this test binary
|
||||
// doesn't have -- see the label size caching contract for why this is
|
||||
// safe to bypass here (uiSliderRebuildGeometry only reads slider->label's
|
||||
// already-measured width/height, it never touches the font itself).
|
||||
static void sliderSetupFloat(
|
||||
uislider_t *slider,
|
||||
const float_t value,
|
||||
const float_t min,
|
||||
const float_t max,
|
||||
const float_t step
|
||||
) {
|
||||
memoryZero(slider, sizeof(uislider_t));
|
||||
slider->label.width = 20;
|
||||
slider->label.height = 10;
|
||||
slider->type = UI_SLIDER_TYPE_FLOAT;
|
||||
slider->min.f = min;
|
||||
slider->max.f = max;
|
||||
slider->step.f = step;
|
||||
slider->value.f = value;
|
||||
}
|
||||
|
||||
static void sliderSetupInt(
|
||||
uislider_t *slider,
|
||||
const int32_t value,
|
||||
const int32_t min,
|
||||
const int32_t max,
|
||||
const int32_t step
|
||||
) {
|
||||
memoryZero(slider, sizeof(uislider_t));
|
||||
slider->label.width = 20;
|
||||
slider->label.height = 10;
|
||||
slider->type = UI_SLIDER_TYPE_INT;
|
||||
slider->min.i = min;
|
||||
slider->max.i = max;
|
||||
slider->step.i = step;
|
||||
slider->value.i = value;
|
||||
}
|
||||
|
||||
static void test_uiSliderRebuildGeometry_trackPosition(void **state) {
|
||||
uislider_t slider;
|
||||
sliderSetupFloat(&slider, 0.0f, 0.0f, 1.0f, 0.1f);
|
||||
uiSliderRebuildGeometry(&slider);
|
||||
|
||||
const float_t expectedX = (float_t)slider.label.width + UI_SLIDER_GAP;
|
||||
const float_t expectedY =
|
||||
((float_t)slider.label.height - UI_SLIDER_TRACK_HEIGHT) * 0.5f;
|
||||
|
||||
assert_float_equal(slider.cachedTrack.min[0], expectedX, 0.0001f);
|
||||
assert_float_equal(slider.cachedTrack.min[1], expectedY, 0.0001f);
|
||||
assert_float_equal(
|
||||
slider.cachedTrack.max[0], expectedX + UI_SLIDER_TRACK_WIDTH, 0.0001f
|
||||
);
|
||||
}
|
||||
|
||||
static void test_uiSliderRebuildGeometry_fillVisibility(void **state) {
|
||||
uislider_t slider;
|
||||
|
||||
// At the minimum value, the ratio is 0 -- no fill should be visible.
|
||||
sliderSetupFloat(&slider, 0.0f, 0.0f, 10.0f, 1.0f);
|
||||
uiSliderRebuildGeometry(&slider);
|
||||
assert_false(slider.cachedFillVisible);
|
||||
|
||||
// Halfway to max -- fill should cover half the track.
|
||||
sliderSetupFloat(&slider, 5.0f, 0.0f, 10.0f, 1.0f);
|
||||
uiSliderRebuildGeometry(&slider);
|
||||
assert_true(slider.cachedFillVisible);
|
||||
assert_float_equal(
|
||||
slider.cachedFill.max[0] - slider.cachedFill.min[0],
|
||||
UI_SLIDER_TRACK_WIDTH * 0.5f,
|
||||
0.0001f
|
||||
);
|
||||
}
|
||||
|
||||
static void test_uiSliderRebuildGeometry_stepMarkers(void **state) {
|
||||
uislider_t slider;
|
||||
|
||||
// 5 steps between 0 and 10 -- under the marker cap, so markers render.
|
||||
sliderSetupInt(&slider, 0, 0, 10, 2);
|
||||
uiSliderRebuildGeometry(&slider);
|
||||
assert_int_equal(slider.cachedMarkerCount, 6);
|
||||
assert_float_equal(
|
||||
slider.cachedMarkers[0].min[0], slider.cachedTrack.min[0] -
|
||||
UI_SLIDER_STEP_MARKER_WIDTH * 0.5f,
|
||||
0.0001f
|
||||
);
|
||||
|
||||
// 100 steps -- over the marker cap, so it falls back to a smooth fill
|
||||
// with no markers.
|
||||
sliderSetupInt(&slider, 0, 0, 100, 1);
|
||||
uiSliderRebuildGeometry(&slider);
|
||||
assert_int_equal(slider.cachedMarkerCount, 0);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_uiSliderRebuildGeometry_trackPosition),
|
||||
cmocka_unit_test(test_uiSliderRebuildGeometry_fillVisibility),
|
||||
cmocka_unit_test(test_uiSliderRebuildGeometry_stepMarkers),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "dusktest.h"
|
||||
#include "ui/widget/uitab.h"
|
||||
#include "util/memory.h"
|
||||
|
||||
// uiTabInit requires a real font (FONT_DEFAULT) to build the label's
|
||||
// glyph cache, which needs a live GL context this test binary doesn't
|
||||
// have. uiTabRebuildBackground only reads the label's already-measured
|
||||
// width/height, so it's safe to set those directly and exercise it here.
|
||||
static void test_uiTabRebuildBackground_matchesLabelSize(void **state) {
|
||||
uitab_t tab;
|
||||
memoryZero(&tab, sizeof(uitab_t));
|
||||
tab.label.width = 42;
|
||||
tab.label.height = 12;
|
||||
|
||||
uiTabRebuildBackground(&tab);
|
||||
|
||||
assert_float_equal(tab.cachedBackground.min[0], 0.0f, 0.0001f);
|
||||
assert_float_equal(tab.cachedBackground.min[1], 0.0f, 0.0001f);
|
||||
assert_float_equal(tab.cachedBackground.max[0], 42.0f, 0.0001f);
|
||||
assert_float_equal(tab.cachedBackground.max[1], 12.0f, 0.0001f);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_uiTabRebuildBackground_matchesLabelSize),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user