Refactored and simplified lua stuff a lot.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# Copyright (c) 2026 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
@@ -7,11 +7,5 @@
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
ui.c
|
||||
uitext.c
|
||||
uidebug.c
|
||||
uiframe.c
|
||||
uitextbox.c
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(element)
|
||||
uielement.c
|
||||
)
|
||||
@@ -1,9 +0,0 @@
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
)
|
||||
@@ -1,16 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
typedef enum {
|
||||
UI_ELEMENT_TYPE_NULL,
|
||||
|
||||
UI_ELEMENT_TYPE_TEXT,
|
||||
|
||||
UI_ELEMENT_TYPE_COUNT,
|
||||
} uielementtype_t;
|
||||
71
src/ui/ui.c
71
src/ui/ui.c
@@ -1,67 +1,68 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "ui.h"
|
||||
#include "assert/assert.h"
|
||||
#include "ui/uidebug.h"
|
||||
#include "util/memory.h"
|
||||
#include "display/tileset/tileset_minogram.h"
|
||||
#include "display/screen.h"
|
||||
// #include "ui/uitextbox.h"
|
||||
#include "assert/assert.h"
|
||||
#include "display/spritebatch.h"
|
||||
|
||||
ui_t UI;
|
||||
|
||||
errorret_t uiInit(void) {
|
||||
memoryZero(&UI, sizeof(ui_t));
|
||||
|
||||
cameraInitOrthographic(&UI.camera);
|
||||
|
||||
// Initialize UI components here
|
||||
uiSetFont(&TILESET_MINOGRAM);
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void uiUpdate(void) {
|
||||
// Update UI state here
|
||||
UI.camera.orthographic.left = 0;
|
||||
UI.camera.orthographic.right = SCREEN.width;
|
||||
UI.camera.orthographic.top = 0;
|
||||
UI.camera.orthographic.bottom = SCREEN.height;
|
||||
|
||||
// uiTextboxUpdate();
|
||||
uint_fast8_t i = 0;
|
||||
while(i < UI.stackSize) {
|
||||
errorCatch(errorPrint(uiElementUpdate(&UI.stack[i])));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void uiRender(void) {
|
||||
cameraPushMatrix(&UI.camera);
|
||||
|
||||
// Render UI elements here
|
||||
if(UI.fontTexture.width > 0) {
|
||||
uiDebugRender(UI.fontTileset, &UI.fontTexture);
|
||||
spriteBatchClear();
|
||||
|
||||
uint_fast8_t i = 0;
|
||||
while(i < UI.stackSize) {
|
||||
errorCatch(errorPrint(uiElementRender(&UI.stack[i])));
|
||||
i++;
|
||||
}
|
||||
|
||||
// spriteBatchPush(
|
||||
// NULL,
|
||||
// 0, 0, 32, 32, COLOR_RED, 0, 0, 1, 1
|
||||
// );
|
||||
|
||||
spriteBatchFlush();
|
||||
cameraPopMatrix();
|
||||
}
|
||||
|
||||
errorret_t uiSetFont(const tileset_t *fontTileset) {
|
||||
if(UI.fontTexture.width > 0) {
|
||||
textureDispose(&UI.fontTexture);
|
||||
UI.fontTexture.width = -1;
|
||||
}
|
||||
|
||||
assertNotNull(fontTileset, "Font tileset cannot be NULL.");
|
||||
|
||||
UI.fontTileset = fontTileset;
|
||||
errorChain(assetLoad(UI.fontTileset->image, &UI.fontTexture));
|
||||
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) {
|
||||
if(UI.fontTexture.width > 0) {
|
||||
textureDispose(&UI.fontTexture);
|
||||
UI.fontTexture.width = -1;
|
||||
while(UI.stackSize > 0) {
|
||||
uiPop();
|
||||
}
|
||||
}
|
||||
36
src/ui/ui.h
36
src/ui/ui.h
@@ -1,51 +1,53 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "asset/asset.h"
|
||||
#include "display/texture.h"
|
||||
#include "display/tileset/tileset.h"
|
||||
#include "display/camera/camera.h"
|
||||
#include "ui/uielement.h"
|
||||
|
||||
#define UI_STACK_SIZE 64
|
||||
|
||||
typedef struct {
|
||||
camera_t camera;
|
||||
texture_t fontTexture;
|
||||
const tileset_t *fontTileset;
|
||||
|
||||
uielement_t stack[UI_STACK_SIZE];
|
||||
uint_fast8_t stackSize;
|
||||
} ui_t;
|
||||
|
||||
extern ui_t UI;
|
||||
|
||||
/**
|
||||
* Initializes the UI system, loading necessary resources.
|
||||
*
|
||||
* @return An errorret_t indicating success or failure.
|
||||
* Initializes the UI system.
|
||||
*/
|
||||
errorret_t uiInit(void);
|
||||
|
||||
/**
|
||||
* Updates the UI state, handling user interactions and animations.
|
||||
* Updates the UI system.
|
||||
*/
|
||||
void uiUpdate(void);
|
||||
|
||||
/**
|
||||
* Renders the UI elements to the screen.
|
||||
* Renders the UI system.
|
||||
*/
|
||||
void uiRender(void);
|
||||
|
||||
/**
|
||||
* Sets the font tileset for UI text rendering.
|
||||
* Pushes a new UI element onto the UI stack from a script file.
|
||||
*
|
||||
* @param fontTileset Pointer to the tileset to use for UI fonts.
|
||||
*
|
||||
* @return An errorret_t indicating success or failure.
|
||||
* @param path Path to the UI script file.
|
||||
*/
|
||||
errorret_t uiSetFont(const tileset_t *fontTileset);
|
||||
errorret_t uiPushScript(const char_t *path);
|
||||
|
||||
/**
|
||||
* Cleans up and frees all UI resources.
|
||||
* Pops the top UI element from the UI stack.
|
||||
*/
|
||||
void uiPop(void);
|
||||
|
||||
/**
|
||||
* Disposes of the UI system.
|
||||
*/
|
||||
void uiDispose(void);
|
||||
@@ -1,98 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "uidebug.h"
|
||||
#include "time/time.h"
|
||||
#include "util/string.h"
|
||||
#include "ui/uitext.h"
|
||||
#include "display/screen.h"
|
||||
#include "display/spritebatch.h"
|
||||
// #include "rpg/entity/entity.h"
|
||||
|
||||
bool_t UI_DEBUG_DRAW = true;
|
||||
|
||||
void uiDebugRender(const tileset_t *tileset, texture_t *texture) {
|
||||
if(!UI_DEBUG_DRAW) return;
|
||||
|
||||
char_t buffer[128];
|
||||
color_t color;
|
||||
int32_t w, h, hOffset = 0;
|
||||
|
||||
// FPS Meter
|
||||
#if TIME_FIXED == 0
|
||||
float_t fpsDynamic = TIME.dynamicDelta > 0.0f ? (1.0f / TIME.dynamicDelta) : 0.0f;
|
||||
float_t fpsFixed = TIME.delta > 0.0f ? (1.0f / TIME.delta) : 0.0f;
|
||||
snprintf(
|
||||
buffer,
|
||||
sizeof(buffer),
|
||||
"%.2f/%.2f/%d/%d",
|
||||
TIME.dynamicDelta * 1000.0f,
|
||||
TIME.delta * 1000.0f,
|
||||
TIME.dynamicUpdate,
|
||||
(int32_t)fpsDynamic
|
||||
);
|
||||
|
||||
color = (
|
||||
fpsDynamic >= 50.0f ? COLOR_GREEN :
|
||||
fpsDynamic >= 30.0f ? COLOR_YELLOW :
|
||||
COLOR_RED
|
||||
);
|
||||
#else
|
||||
float_t fps = TIME.delta > 0.0f ? (1.0f / TIME.delta) : 0.0f;
|
||||
snprintf(
|
||||
buffer,
|
||||
sizeof(buffer),
|
||||
"%.2f/%d/FXD",
|
||||
TIME.delta * 1000.0f,
|
||||
(int32_t)fps
|
||||
);
|
||||
|
||||
color = (
|
||||
fps >= 50.0f ? COLOR_GREEN :
|
||||
fps >= 30.0f ? COLOR_YELLOW :
|
||||
COLOR_RED
|
||||
);
|
||||
#endif
|
||||
|
||||
uiTextMeasure(buffer, tileset, &w, &h);
|
||||
uiTextDraw(
|
||||
SCREEN.width - w, hOffset,
|
||||
buffer, color, tileset, texture
|
||||
);
|
||||
hOffset += h;
|
||||
|
||||
|
||||
// Player position
|
||||
// entity_t *player = NULL;
|
||||
// for(uint8_t i = 0; i < ENTITY_COUNT; i++) {
|
||||
// if(ENTITIES[i].type != ENTITY_TYPE_PLAYER) continue;
|
||||
// player = &ENTITIES[i];
|
||||
// break;
|
||||
// }
|
||||
// if(player == NULL) {
|
||||
// snprintf(buffer, sizeof(buffer), "Player: N/A");
|
||||
// } else {
|
||||
// snprintf(
|
||||
// buffer,
|
||||
// sizeof(buffer),
|
||||
// "%d,%d,%d/%d/%d",
|
||||
// player->position.x,
|
||||
// player->position.y,
|
||||
// player->position.z,
|
||||
// (int32_t)player->direction,
|
||||
// (int32_t)player->animation
|
||||
// );
|
||||
// }
|
||||
// uiTextMeasure(buffer, tileset, &w, &h);
|
||||
// uiTextDraw(
|
||||
// SCREEN.width - w, hOffset,
|
||||
// buffer, COLOR_GREEN, tileset, texture
|
||||
// );
|
||||
// hOffset += h;
|
||||
|
||||
spriteBatchFlush();
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "display/tileset/tileset.h"
|
||||
#include "display/texture.h"
|
||||
|
||||
/**
|
||||
* Renders the debug information UI element.
|
||||
*
|
||||
* @param tileset The tileset to use for rendering text.
|
||||
* @param texture The texture associated with the tileset.
|
||||
*/
|
||||
void uiDebugRender(const tileset_t *tileset, texture_t *texture);
|
||||
57
src/ui/uielement.c
Normal file
57
src/ui/uielement.c
Normal file
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* 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"
|
||||
|
||||
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) {
|
||||
if(!scriptContextHasFunc(&element->ctx, "update")) errorOk();
|
||||
|
||||
errorChain(scriptContextCallFunc(
|
||||
&element->ctx,
|
||||
"update",
|
||||
NULL,
|
||||
0,
|
||||
NULL
|
||||
));
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t uiElementRender(uielement_t *element) {
|
||||
if(!scriptContextHasFunc(&element->ctx, "render")) errorOk();
|
||||
|
||||
// scriptvalue_t args[4] = {
|
||||
// { .type = SCRIPT_VALUE_TYPE_FLOAT, .value.floatValue = 0.0f },
|
||||
// { .type = SCRIPT_VALUE_TYPE_FLOAT, .value.floatValue = 0.0f },
|
||||
// { .type = SCRIPT_VALUE_TYPE_FLOAT, .value.floatValue = SCREEN.width },
|
||||
// { .type = SCRIPT_VALUE_TYPE_FLOAT, .value.floatValue = SCREEN.height },
|
||||
// };
|
||||
|
||||
// errorChain(scriptContextCallFunc(
|
||||
// &element->ctx,
|
||||
// "render",
|
||||
// args,
|
||||
// 4,
|
||||
// NULL
|
||||
// ));
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void uiElementDispose(uielement_t *element) {
|
||||
scriptContextDispose(&element->ctx);
|
||||
}
|
||||
46
src/ui/uielement.h
Normal file
46
src/ui/uielement.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* 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);
|
||||
390
src/ui/uiframe.c
390
src/ui/uiframe.c
@@ -1,390 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "uiframe.h"
|
||||
#include "display/spritebatch.h"
|
||||
#include "assert/assert.h"
|
||||
#include <math.h>
|
||||
|
||||
void uiFrameDraw(
|
||||
const float_t x,
|
||||
const float_t y,
|
||||
const float_t width,
|
||||
const float_t height,
|
||||
const tileset_t *tileset,
|
||||
const uint16_t column,
|
||||
const uint16_t row,
|
||||
const bool_t drawInner,
|
||||
texture_t *texture
|
||||
) {
|
||||
assertNotNull(tileset, "Tileset cannot be NULL");
|
||||
assertNotNull(texture, "Texture cannot be NULL");
|
||||
|
||||
if(height <= 0 || width <= 0) return;
|
||||
|
||||
// Top-Left
|
||||
vec4 uv;
|
||||
|
||||
tilesetPositionGetUV(
|
||||
tileset,
|
||||
column,
|
||||
row,
|
||||
uv
|
||||
);
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x, y,
|
||||
x + tileset->tileWidth,
|
||||
y + tileset->tileHeight,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
|
||||
// Top-Center
|
||||
tilesetPositionGetUV(
|
||||
tileset,
|
||||
column + 1,
|
||||
row,
|
||||
uv
|
||||
);
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x + tileset->tileWidth, y,
|
||||
x + width - tileset->tileWidth, y + tileset->tileHeight,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
|
||||
// Top-Right
|
||||
tilesetPositionGetUV(
|
||||
tileset,
|
||||
column + 2,
|
||||
row,
|
||||
uv
|
||||
);
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x + width - tileset->tileWidth, y,
|
||||
x + width, y + tileset->tileHeight,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
|
||||
// Middle-Left
|
||||
tilesetPositionGetUV(
|
||||
tileset,
|
||||
column,
|
||||
row + 1,
|
||||
uv
|
||||
);
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x, y + tileset->tileHeight,
|
||||
x + tileset->tileWidth, y + height - tileset->tileHeight,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
|
||||
// Middle-Center
|
||||
if(drawInner) {
|
||||
tilesetPositionGetUV(
|
||||
tileset,
|
||||
column + 1,
|
||||
row + 1,
|
||||
uv
|
||||
);
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x + tileset->tileWidth, y + tileset->tileHeight,
|
||||
x + width - tileset->tileWidth, y + height - tileset->tileHeight,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
|
||||
// Middle-Right
|
||||
tilesetPositionGetUV(
|
||||
tileset,
|
||||
column + 2,
|
||||
row + 1,
|
||||
uv
|
||||
);
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x + width - tileset->tileWidth, y + tileset->tileHeight,
|
||||
x + width, y + height - tileset->tileHeight,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
|
||||
// Bottom-Left
|
||||
tilesetPositionGetUV(
|
||||
tileset,
|
||||
column,
|
||||
row + 2,
|
||||
uv
|
||||
);
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x, y + height - tileset->tileHeight,
|
||||
x + tileset->tileWidth, y + height,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
|
||||
// Bottom-Center
|
||||
tilesetPositionGetUV(
|
||||
tileset,
|
||||
column + 1,
|
||||
row + 2,
|
||||
uv
|
||||
);
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x + tileset->tileWidth, y + height - tileset->tileHeight,
|
||||
x + width - tileset->tileWidth, y + height,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
|
||||
// Bottom-Right
|
||||
tilesetPositionGetUV(
|
||||
tileset,
|
||||
column + 2,
|
||||
row + 2,
|
||||
uv
|
||||
);
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x + width - tileset->tileWidth, y + height - tileset->tileHeight,
|
||||
x + width, y + height,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
|
||||
void uiFrameDrawTiled(
|
||||
const float_t x,
|
||||
const float_t y,
|
||||
const float_t width,
|
||||
const float_t height,
|
||||
const tileset_t *tileset,
|
||||
const uint16_t column,
|
||||
const uint16_t row,
|
||||
const bool_t drawInner,
|
||||
texture_t *texture
|
||||
) {
|
||||
assertNotNull(tileset, "Tileset cannot be NULL");
|
||||
assertNotNull(texture, "Texture cannot be NULL");
|
||||
|
||||
if(height <= 0 || width <= 0) return;
|
||||
|
||||
uint32_t segmentsX, segmentsY;
|
||||
float_t remainderX, remainderY;
|
||||
segmentsX = (width - (tileset->tileWidth * 2)) / tileset->tileWidth;
|
||||
segmentsY = (height - (tileset->tileHeight * 2)) / tileset->tileHeight;
|
||||
remainderX = fmodf(width - (tileset->tileWidth * 2), tileset->tileWidth);
|
||||
remainderY = fmodf(height - (tileset->tileHeight * 2), tileset->tileHeight);
|
||||
|
||||
// Corners
|
||||
vec4 uv;
|
||||
|
||||
// Top-Left
|
||||
tilesetPositionGetUV(
|
||||
tileset,
|
||||
column,
|
||||
row,
|
||||
uv
|
||||
);
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x, y,
|
||||
x + tileset->tileWidth,
|
||||
y + tileset->tileHeight,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
|
||||
// Top-Right
|
||||
tilesetPositionGetUV(
|
||||
tileset,
|
||||
column + 2,
|
||||
row,
|
||||
uv
|
||||
);
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x + width - tileset->tileWidth, y,
|
||||
x + width, y + tileset->tileHeight,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
|
||||
// Bottom-Left
|
||||
tilesetPositionGetUV(
|
||||
tileset,
|
||||
column,
|
||||
row + 2,
|
||||
uv
|
||||
);
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x, y + height - tileset->tileHeight,
|
||||
x + tileset->tileWidth, y + height,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
|
||||
// Bottom-Right
|
||||
tilesetPositionGetUV(
|
||||
tileset,
|
||||
column + 2,
|
||||
row + 2,
|
||||
uv
|
||||
);
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x + width - tileset->tileWidth, y + height - tileset->tileHeight,
|
||||
x + width, y + height,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
|
||||
// Top and Bottom Edges (Tiled)
|
||||
tilesetPositionGetUV(tileset, column + 1, row, uv); // Top edge
|
||||
float_t edgeX = x + tileset->tileWidth;
|
||||
for(uint32_t i = 0; i < segmentsX; ++i, edgeX += tileset->tileWidth) {
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
edgeX, y,
|
||||
edgeX + tileset->tileWidth, y + tileset->tileHeight,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
if(remainderX) {
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
edgeX, y,
|
||||
edgeX + remainderX, y + tileset->tileHeight,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
|
||||
tilesetPositionGetUV(tileset, column + 1, row + 2, uv); // Bottom edge
|
||||
edgeX = x + tileset->tileWidth;
|
||||
for(uint32_t i = 0; i < segmentsX; ++i, edgeX += tileset->tileWidth) {
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
edgeX, y + height - tileset->tileHeight,
|
||||
edgeX + tileset->tileWidth, y + height,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
if(remainderX) {
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
edgeX, y + height - tileset->tileHeight,
|
||||
edgeX + remainderX, y + height,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
|
||||
// Left and Right Edges (Tiled)
|
||||
tilesetPositionGetUV(tileset, column, row + 1, uv); // Left edge
|
||||
float_t edgeY = y + tileset->tileHeight;
|
||||
for(uint32_t i = 0; i < segmentsY; ++i, edgeY += tileset->tileHeight) {
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x, edgeY,
|
||||
x + tileset->tileWidth, edgeY + tileset->tileHeight,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
if(remainderY) {
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x, edgeY,
|
||||
x + tileset->tileWidth, edgeY + remainderY,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
|
||||
tilesetPositionGetUV(tileset, column + 2, row + 1, uv); // Right edge
|
||||
edgeY = y + tileset->tileHeight;
|
||||
for(uint32_t i = 0; i < segmentsY; ++i, edgeY += tileset->tileHeight) {
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x + width - tileset->tileWidth, edgeY,
|
||||
x + width, edgeY + tileset->tileHeight,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
if(remainderY) {
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x + width - tileset->tileWidth, edgeY,
|
||||
x + width, edgeY + remainderY,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
|
||||
// Center (Tiled)
|
||||
if(!drawInner) return;
|
||||
|
||||
tilesetPositionGetUV(tileset, column + 1, row + 1, uv); // Center tile
|
||||
float_t centerY = y + tileset->tileHeight;
|
||||
for(uint32_t j = 0; j < segmentsY; ++j, centerY += tileset->tileHeight) {
|
||||
float_t centerX = x + tileset->tileWidth;
|
||||
for(uint32_t i = 0; i < segmentsX; ++i, centerX += tileset->tileWidth) {
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
centerX, centerY,
|
||||
centerX + tileset->tileWidth, centerY + tileset->tileHeight,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
if(remainderX) {
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
centerX, centerY,
|
||||
centerX + remainderX, centerY + tileset->tileHeight,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
}
|
||||
if(remainderY) {
|
||||
float_t centerX = x + tileset->tileWidth;
|
||||
for(uint32_t i = 0; i < segmentsX; ++i, centerX += tileset->tileWidth) {
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
centerX, centerY,
|
||||
centerX + tileset->tileWidth, centerY + remainderY,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
if(remainderX) {
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
centerX, centerY,
|
||||
centerX + remainderX, centerY + remainderY,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "display/tileset/tileset.h"
|
||||
#include "display/texture.h"
|
||||
|
||||
/**
|
||||
* Draw a UI Frame (using the 9-slice technique).
|
||||
*
|
||||
* @param x The x position to draw the frame at.
|
||||
* @param y The y position to draw the frame at.
|
||||
* @param width The width of the frame.
|
||||
* @param height The height of the frame.
|
||||
* @param tileset The tileset to use for rendering the frame.
|
||||
* @param column The column in the tileset to use for the frame.
|
||||
* @param row The row in the tileset to use for the frame.
|
||||
* @param drawInner Whether to draw the inner area.
|
||||
* @param texture The texture associated with the tileset.
|
||||
*/
|
||||
void uiFrameDraw(
|
||||
const float_t x,
|
||||
const float_t y,
|
||||
const float_t width,
|
||||
const float_t height,
|
||||
const tileset_t *tileset,
|
||||
const uint16_t column,
|
||||
const uint16_t row,
|
||||
const bool_t drawInner,
|
||||
texture_t *texture
|
||||
);
|
||||
|
||||
/**
|
||||
* Draw a tiled UI Frame (using the 9-slice technique). This will tile the
|
||||
* center components to allow them to repeat without distortion.
|
||||
*
|
||||
* @param x The x position to draw the frame at.
|
||||
* @param y The y position to draw the frame at.
|
||||
* @param width The width of the frame.
|
||||
* @param height The height of the frame.
|
||||
* @param tileset The tileset to use for rendering the frame.
|
||||
* @param column The column in the tileset to use for the frame.
|
||||
* @param row The row in the tileset to use for the frame.
|
||||
* @param drawInner Whether to draw the inner tiled area.
|
||||
* @param texture The texture associated with the tileset.
|
||||
*/
|
||||
void uiFrameDrawTiled(
|
||||
const float_t x,
|
||||
const float_t y,
|
||||
const float_t width,
|
||||
const float_t height,
|
||||
const tileset_t *tileset,
|
||||
const uint16_t column,
|
||||
const uint16_t row,
|
||||
const bool_t drawInner,
|
||||
texture_t *texture
|
||||
);
|
||||
111
src/ui/uitext.c
111
src/ui/uitext.c
@@ -1,111 +0,0 @@
|
||||
// /**
|
||||
// * Copyright (c) 2025 Dominic Masters
|
||||
// *
|
||||
// * This software is released under the MIT License.
|
||||
// * https://opensource.org/licenses/MIT
|
||||
// */
|
||||
|
||||
#include "uitext.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/memory.h"
|
||||
#include "display/spritebatch.h"
|
||||
|
||||
void uiTextDrawChar(
|
||||
const float_t x,
|
||||
const float_t y,
|
||||
const char_t c,
|
||||
const color_t color,
|
||||
const tileset_t *tileset,
|
||||
texture_t *texture
|
||||
) {
|
||||
int32_t tileIndex = (int32_t)(c) - UI_TEXT_CHAR_START;
|
||||
if(tileIndex < 0 || tileIndex >= tileset->tileCount) {
|
||||
tileIndex = ((int32_t)'@') - UI_TEXT_CHAR_START;
|
||||
}
|
||||
|
||||
assertTrue(
|
||||
tileIndex >= 0 && tileIndex <= tileset->tileCount,
|
||||
"Character is out of bounds for font tiles"
|
||||
);
|
||||
|
||||
vec4 uv;
|
||||
tilesetTileGetUV(tileset, tileIndex, uv);
|
||||
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x, y,
|
||||
x + tileset->tileWidth,
|
||||
y + tileset->tileHeight,
|
||||
color,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
|
||||
void uiTextDraw(
|
||||
const float_t x,
|
||||
const float_t y,
|
||||
const char_t *text,
|
||||
const color_t color,
|
||||
const tileset_t *tileset,
|
||||
texture_t *texture
|
||||
) {
|
||||
assertNotNull(text, "Text cannot be NULL");
|
||||
|
||||
float_t posX = x;
|
||||
float_t posY = y;
|
||||
|
||||
char_t c;
|
||||
int32_t i = 0;
|
||||
while((c = text[i++]) != '\0') {
|
||||
if(c == '\n') {
|
||||
posX = x;
|
||||
posY += tileset->tileHeight;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(c == ' ') {
|
||||
posX += tileset->tileWidth;
|
||||
continue;
|
||||
}
|
||||
|
||||
uiTextDrawChar(posX, posY, c, color, tileset, texture);
|
||||
posX += tileset->tileWidth;
|
||||
}
|
||||
}
|
||||
|
||||
void uiTextMeasure(
|
||||
const char_t *text,
|
||||
const tileset_t *tileset,
|
||||
int32_t *outWidth,
|
||||
int32_t *outHeight
|
||||
) {
|
||||
assertNotNull(text, "Text cannot be NULL");
|
||||
assertNotNull(outWidth, "Output width pointer cannot be NULL");
|
||||
assertNotNull(outHeight, "Output height pointer cannot be NULL");
|
||||
|
||||
int32_t width = 0;
|
||||
int32_t height = tileset->tileHeight;
|
||||
int32_t lineWidth = 0;
|
||||
|
||||
char_t c;
|
||||
int32_t i = 0;
|
||||
while((c = text[i++]) != '\0') {
|
||||
if(c == '\n') {
|
||||
if(lineWidth > width) {
|
||||
width = lineWidth;
|
||||
}
|
||||
lineWidth = 0;
|
||||
height += tileset->tileHeight;
|
||||
continue;
|
||||
}
|
||||
|
||||
lineWidth += tileset->tileWidth;
|
||||
}
|
||||
|
||||
if(lineWidth > width) {
|
||||
width = lineWidth;
|
||||
}
|
||||
|
||||
*outWidth = width;
|
||||
*outHeight = height;
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "asset/asset.h"
|
||||
#include "display/texture.h"
|
||||
#include "display/tileset/tileset_minogram.h"
|
||||
|
||||
#define UI_TEXT_CHAR_START '!'
|
||||
|
||||
/**
|
||||
* Draws a single character at the specified position.
|
||||
*
|
||||
* @param x The x-coordinate to draw the character at.
|
||||
* @param y The y-coordinate to draw the character at.
|
||||
* @param c The character to draw.
|
||||
* @param color The color to draw the character in.
|
||||
* @param tileset Font tileset to use for rendering.
|
||||
* @param texture Texture containing the font tileset image.
|
||||
*/
|
||||
void uiTextDrawChar(
|
||||
const float_t x,
|
||||
const float_t y,
|
||||
const char_t c,
|
||||
const color_t color,
|
||||
const tileset_t *tileset,
|
||||
texture_t *texture
|
||||
);
|
||||
|
||||
/**
|
||||
* Draws a string of text at the specified position.
|
||||
*
|
||||
* @param x The x-coordinate to draw the text at.
|
||||
* @param y The y-coordinate to draw the text at.
|
||||
* @param text The null-terminated string of text to draw.
|
||||
* @param color The color to draw the text in.
|
||||
* @param tileset Font tileset to use for rendering.
|
||||
* @param texture Texture containing the font tileset image.
|
||||
*/
|
||||
void uiTextDraw(
|
||||
const float_t x,
|
||||
const float_t y,
|
||||
const char_t *text,
|
||||
const color_t color,
|
||||
const tileset_t *tileset,
|
||||
texture_t *texture
|
||||
);
|
||||
|
||||
/**
|
||||
* Measures the width and height of the given text string when rendered.
|
||||
*
|
||||
* @param text The null-terminated string of text to measure.
|
||||
* @param tileset Font tileset to use for measurement.
|
||||
* @param outWidth Pointer to store the measured width in pixels.
|
||||
* @param outHeight Pointer to store the measured height in pixels.
|
||||
*/
|
||||
void uiTextMeasure(
|
||||
const char_t *text,
|
||||
const tileset_t *tileset,
|
||||
int32_t *outWidth,
|
||||
int32_t *outHeight
|
||||
);
|
||||
@@ -1,45 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "uitextbox.h"
|
||||
#include "ui/ui.h"
|
||||
#include "ui/uitext.h"
|
||||
// #include "rpg/rpgtextbox.h"
|
||||
#include "display/screen.h"
|
||||
#include "display/spritebatch.h"
|
||||
#include "input/input.h"
|
||||
|
||||
// void uiTextboxUpdate() {
|
||||
// if(!rpgTextboxIsVisible()) return;
|
||||
|
||||
// if(inputPressed(INPUT_ACTION_ACCEPT)) {
|
||||
// rpgTextboxHide();
|
||||
// }
|
||||
// }
|
||||
|
||||
// void uiTextboxRender() {
|
||||
// if(!rpgTextboxIsVisible()) return;
|
||||
|
||||
// const char_t *text = RPG_TEXTBOX.text;
|
||||
// int32_t textWidth, textHeight;
|
||||
|
||||
// uiTextMeasure(text, UI.fontTileset, &textWidth, &textHeight);
|
||||
|
||||
// float_t y = 0;
|
||||
// if(RPG_TEXTBOX.position == RPG_TEXTBOX_POS_BOTTOM) {
|
||||
// y = SCREEN.height - (float_t)textHeight;
|
||||
// }
|
||||
|
||||
// spriteBatchPush(
|
||||
// NULL,
|
||||
// 0.0f, y,
|
||||
// (float_t)SCREEN.width, (float_t)(y + textHeight),
|
||||
// COLOR_BLACK,
|
||||
// 0.0f, 0.0f, 1.0f, 1.0f
|
||||
// );
|
||||
// uiTextDraw(0, y, text, COLOR_RED, UI.fontTileset, &UI.fontTexture);
|
||||
// }
|
||||
@@ -1,19 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
|
||||
/**
|
||||
* Updates the RPG textbox state.
|
||||
*/
|
||||
void uiTextboxUpdate();
|
||||
|
||||
/**
|
||||
* Draws the RPG textbox if it is visible.
|
||||
*/
|
||||
void uiTextboxRender();
|
||||
Reference in New Issue
Block a user