Text
This commit is contained in:
@@ -7,5 +7,4 @@
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
ui.c
|
||||
uielement.c
|
||||
)
|
||||
30
src/ui/ui.c
30
src/ui/ui.c
@@ -24,11 +24,6 @@ errorret_t uiInit(void) {
|
||||
}
|
||||
|
||||
void uiUpdate(void) {
|
||||
uint_fast8_t i = 0;
|
||||
while(i < UI.stackSize) {
|
||||
errorCatch(errorPrint(uiElementUpdate(&UI.stack[i])));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void uiRender(void) {
|
||||
@@ -38,34 +33,9 @@ void uiRender(void) {
|
||||
cameraPushMatrix(&UI.camera);
|
||||
spriteBatchClear();
|
||||
|
||||
uint_fast8_t i = 0;
|
||||
while(i < UI.stackSize) {
|
||||
errorCatch(errorPrint(uiElementRender(&UI.stack[i])));
|
||||
i++;
|
||||
}
|
||||
|
||||
spriteBatchFlush();
|
||||
cameraPopMatrix();
|
||||
}
|
||||
|
||||
errorret_t uiPushScript(const char_t *path) {
|
||||
assertTrue(UI.stackSize < UI_STACK_SIZE, "UI stack overflow");
|
||||
uielement_t *element = &UI.stack[UI.stackSize];
|
||||
errorChain(uiElementInitScript(element, path));
|
||||
UI.stackSize++;
|
||||
// Ret Id or something?
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void uiPop(void) {
|
||||
assertTrue(UI.stackSize > 0, "UI stack underflow");
|
||||
UI.stackSize--;
|
||||
uielement_t *element = &UI.stack[UI.stackSize];
|
||||
uiElementDispose(element);
|
||||
}
|
||||
|
||||
void uiDispose(void) {
|
||||
while(UI.stackSize > 0) {
|
||||
uiPop();
|
||||
}
|
||||
}
|
||||
19
src/ui/ui.h
19
src/ui/ui.h
@@ -7,15 +7,10 @@
|
||||
|
||||
#pragma once
|
||||
#include "display/camera/camera.h"
|
||||
#include "ui/uielement.h"
|
||||
|
||||
#define UI_STACK_SIZE 64
|
||||
#include "error/error.h"
|
||||
|
||||
typedef struct {
|
||||
camera_t camera;
|
||||
|
||||
uielement_t stack[UI_STACK_SIZE];
|
||||
uint_fast8_t stackSize;
|
||||
} ui_t;
|
||||
|
||||
extern ui_t UI;
|
||||
@@ -35,18 +30,6 @@ void uiUpdate(void);
|
||||
*/
|
||||
void uiRender(void);
|
||||
|
||||
/**
|
||||
* Pushes a new UI element onto the UI stack from a script file.
|
||||
*
|
||||
* @param path Path to the UI script file.
|
||||
*/
|
||||
errorret_t uiPushScript(const char_t *path);
|
||||
|
||||
/**
|
||||
* Pops the top UI element from the UI stack.
|
||||
*/
|
||||
void uiPop(void);
|
||||
|
||||
/**
|
||||
* Disposes of the UI system.
|
||||
*/
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "uielement.h"
|
||||
#include "display/screen.h"
|
||||
#include "debug/debug.h"
|
||||
|
||||
errorret_t uiElementInitScript(
|
||||
uielement_t *element,
|
||||
const char_t *path
|
||||
) {
|
||||
errorChain(scriptContextInit(&element->ctx));
|
||||
errorChain(scriptContextExecFile(&element->ctx, path));
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t uiElementUpdate(uielement_t *element) {
|
||||
lua_getglobal(element->ctx.luaState, "uiElementUpdate");
|
||||
if(lua_isfunction(element->ctx.luaState, -1)) {
|
||||
if(lua_pcall(element->ctx.luaState, 0, 0, 0) != LUA_OK) {
|
||||
const char_t *strErr = lua_tostring(element->ctx.luaState, -1);
|
||||
lua_pop(element->ctx.luaState, 1);
|
||||
errorThrow("Failed to call UI element uiElementUpdate: %s", strErr);
|
||||
}
|
||||
} else {
|
||||
lua_pop(element->ctx.luaState, 1);
|
||||
}
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t uiElementRender(uielement_t *element) {
|
||||
lua_getglobal(element->ctx.luaState, "uiElementRender");
|
||||
if(lua_isfunction(element->ctx.luaState, -1)) {
|
||||
|
||||
// Push parameters: x, y, w, h
|
||||
lua_pushnumber(element->ctx.luaState, 0);
|
||||
lua_pushnumber(element->ctx.luaState, 0);
|
||||
lua_pushnumber(element->ctx.luaState, SCREEN.width);
|
||||
lua_pushnumber(element->ctx.luaState, SCREEN.height);
|
||||
|
||||
if(lua_pcall(element->ctx.luaState, 4, 0, 0) != LUA_OK) {
|
||||
const char_t *strErr = lua_tostring(element->ctx.luaState, -1);
|
||||
lua_pop(element->ctx.luaState, 1);
|
||||
errorThrow("Failed to call UI element uiElementRender: %s", strErr);
|
||||
}
|
||||
} else {
|
||||
lua_pop(element->ctx.luaState, 1);
|
||||
}
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void uiElementDispose(uielement_t *element) {
|
||||
lua_getglobal(element->ctx.luaState, "uiElementDispose");
|
||||
if(lua_isfunction(element->ctx.luaState, -1)) {
|
||||
if(lua_pcall(element->ctx.luaState, 0, 0, 0) != LUA_OK) {
|
||||
const char_t *strErr = lua_tostring(element->ctx.luaState, -1);
|
||||
lua_pop(element->ctx.luaState, 1);
|
||||
debugPrint("Failed to call UI element uiElementDispose: %s\n", strErr);
|
||||
}
|
||||
} else {
|
||||
lua_pop(element->ctx.luaState, 1);
|
||||
}
|
||||
scriptContextDispose(&element->ctx);
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/scriptcontext.h"
|
||||
|
||||
// TODO: Support both scripted and native UI elements.
|
||||
typedef struct {
|
||||
scriptcontext_t ctx;
|
||||
} uielement_t;
|
||||
|
||||
/**
|
||||
* Initializes a scripted UI Element.
|
||||
*
|
||||
* @param element The UI element to initialize.
|
||||
* @param path Path to the UI script file.
|
||||
*/
|
||||
errorret_t uiElementInitScript(
|
||||
uielement_t *element,
|
||||
const char_t *path
|
||||
);
|
||||
|
||||
/**
|
||||
* Updates/ticks a UI element's logic.
|
||||
*
|
||||
* @param element The UI element to tick.
|
||||
*/
|
||||
errorret_t uiElementUpdate(uielement_t *element);
|
||||
|
||||
/**
|
||||
* Renders a UI element.
|
||||
*
|
||||
* @param element The UI element to render.
|
||||
*/
|
||||
errorret_t uiElementRender(uielement_t *element);
|
||||
|
||||
/**
|
||||
* Disposes of a UI element.
|
||||
*
|
||||
* @param element The UI element to dispose of.
|
||||
*/
|
||||
void uiElementDispose(uielement_t *element);
|
||||
Reference in New Issue
Block a user