From 3019e0ee3be611ea22b022890eab95c3f69d11a5 Mon Sep 17 00:00:00 2001 From: Klemen Plestenjak Date: Thu, 28 Dec 2023 09:36:06 +0100 Subject: [PATCH] Add sprite drawing for UI --- engine/breeze/ui/ui.c | 32 +++++++++++++++++++++++++++++--- engine/breeze/ui/ui.h | 11 +++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/engine/breeze/ui/ui.c b/engine/breeze/ui/ui.c index d5ca5bc..77347df 100644 --- a/engine/breeze/ui/ui.c +++ b/engine/breeze/ui/ui.c @@ -31,6 +31,7 @@ typedef struct BzUINode { i32 textStyleIdx; i32 textShadowStyleIdx; i32 borderStyleIdx; + i32 spriteStyleIdx; BzUISize semanticSize[BZ_UI_AXIS_COUNT]; f32 padding[BZ_UI_AXIS_COUNT * 2]; @@ -58,6 +59,7 @@ typedef struct BzUI { BzUITextStyle *textStyles; BzUITextShadowStyle *textShadowStyles; BzUIBorderStyle *borderStyles; + BzUISpriteStyle *spriteStyles; // TODO: Use arena (when implemented) instead of stack allocator. BzStackAlloc strArena; @@ -89,6 +91,7 @@ static void bzUINodeClearStyles(BzUINode *node) { node->textStyleIdx = -1; node->textShadowStyleIdx = -1; node->borderStyleIdx = -1; + node->spriteStyleIdx = -1; } BzUI *bzUICreate() { @@ -111,6 +114,7 @@ BzUI *bzUICreate() { ui->textStyles = bzArrayCreate(BzUITextStyle, 10); ui->textShadowStyles = bzArrayCreate(BzUITextShadowStyle, 10); ui->borderStyles = bzArrayCreate(BzUIBorderStyle, 10); + ui->spriteStyles = bzArrayCreate(BzUISpriteStyle, 10); // 10kb should be enough ui->strArena = bzStackAllocCreate(10240); @@ -129,6 +133,7 @@ void bzUIDestroy(BzUI *ui) { bzArrayDestroy(ui->textStyles); bzArrayDestroy(ui->textShadowStyles); bzArrayDestroy(ui->borderStyles); + bzArrayDestroy(ui->spriteStyles); bzStackAllocDestroy(&ui->strArena); @@ -156,6 +161,7 @@ void bzUIBegin(BzUI *ui, i32 width, i32 height) { bzArrayClear(ui->textStyles); bzArrayClear(ui->textShadowStyles); bzArrayClear(ui->borderStyles); + bzArrayClear(ui->spriteStyles); bzStackAllocReset(&ui->strArena); @@ -209,13 +215,13 @@ static void calculateAxisSizePostorder(const BzUI *ui, const BzUIAxis axis, BzUI case BZ_UI_SIZE_CHILD_SUM: BZ_ASSERT(node->first); for (BzUINode *child = node->first; child; child = child->next) { - compSize += child->computedSize[axis] + child->margin[axis] + child->margin[axis * 2]; + compSize += child->computedSize[axis] + child->margin[axis] + child->margin[axis + 2]; } break; case BZ_UI_SIZE_CHILD_MAX: BZ_ASSERT(node->first); for (BzUINode *child = node->first; child; child = child->next) { - f32 childSize = child->computedSize[axis] + child->margin[axis] + child->margin[axis * 2]; + f32 childSize = child->computedSize[axis] + child->margin[axis] + child->margin[axis + 2]; compSize = BZ_MAX(compSize, childSize); } break; @@ -424,6 +430,16 @@ static void renderNode(BzUI *ui, BzUINode *node) { DrawRectangleRoundedLines(rect, style.roundness, 0, style.thickness, color); } + if (node->flags & BZ_UI_DRAW_SPRITE) { + BzUISpriteStyle style = bzUIGetSpriteStyle(ui, node); + Color tint = style.tintNormal; + if (inter->hovering) tint = style.tintActive; + if (inter->down) tint = style.tintActive; + Rectangle src = style.rec; + Rectangle dst = rect; + DrawTexturePro(style.texture, src, dst, Vector2Zero(), 0.0f, tint); + } + if (node->flags & BZ_UI_DRAW_TEXT) { BzUITextStyle style = bzUIGetTextStyle(ui, node); BZ_ASSERT(style.text); @@ -444,7 +460,7 @@ static void renderNode(BzUI *ui, BzUINode *node) { drawRect.x, drawRect.y }, style.fontSize, style.fontSpacing, color); } - if (node->flags & BZ_UI_DRAW_DEBUG) { + if (node->flags & BZ_UI_DRAW_DEBUG || true) { DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, RED); } @@ -553,6 +569,11 @@ BzUIBorderStyle bzUIGetBorderStyle(const BzUI *ui, BzUINode *node) { BZ_ASSERT(node->borderStyleIdx != -1); return ui->borderStyles[node->borderStyleIdx]; } +BzUISpriteStyle bzUIGetSpriteStyle(const BzUI *ui, BzUINode *node) { + BZ_ASSERT(ui && node); + BZ_ASSERT(node->spriteStyleIdx != -1); + return ui->spriteStyles[node->spriteStyleIdx]; +} #define BZ_UI_SET_STYLE(ui, node, mIdx, styles, style) \ do { \ @@ -608,6 +629,11 @@ void bzUISetBorderStyle(BzUI *ui, BzUINode *node, BzUIBorderStyle style) { BZ_ASSERT(node->flags & BZ_UI_DRAW_BORDER); BZ_UI_SET_STYLE(ui, node, borderStyleIdx, borderStyles, style); } +void bzUISetSpriteStyle(BzUI *ui, BzUINode *node, BzUISpriteStyle style) { + BZ_ASSERT(ui && node); + BZ_ASSERT(node->flags & BZ_UI_DRAW_SPRITE); + BZ_UI_SET_STYLE(ui, node, spriteStyleIdx, spriteStyles, style); +} #undef BZ_UI_SET_STYLE BzUILayout bzUIGetLayout(const BzUI *ui, BzUINode *node) { diff --git a/engine/breeze/ui/ui.h b/engine/breeze/ui/ui.h index 4208e16..9cfbf1e 100644 --- a/engine/breeze/ui/ui.h +++ b/engine/breeze/ui/ui.h @@ -143,6 +143,15 @@ typedef struct BzUITextShadowStyle { Color active; } BzUITextShadowStyle; +typedef struct BzUISpriteStyle { + Texture2D texture; + Rectangle rec; + + Color tintNormal; + Color tintHover; + Color tintActive; +} BzUISpriteStyle; + typedef struct BzUINode BzUINode; typedef struct BzUINodeDesc { @@ -189,12 +198,14 @@ BzUIBoxShadowStyle bzUIGetBoxShadowStyle(const BzUI *ui, BzUINode *node); BzUITextStyle bzUIGetTextStyle(const BzUI *ui, BzUINode *node); BzUITextShadowStyle bzUIGetTextShadowStyle(const BzUI *ui, BzUINode *node); BzUIBorderStyle bzUIGetBorderStyle(const BzUI *ui, BzUINode *node); +BzUISpriteStyle bzUIGetSpriteStyle(const BzUI *ui, BzUINode *node); void bzUISetBackgroundStyle(BzUI *ui, BzUINode *node, BzUIBackgroundStyle style); void bzUISetBoxShadowStyle(BzUI *ui, BzUINode *node, BzUIBoxShadowStyle style); void bzUISetTextStyle(BzUI *ui, BzUINode *node, BzUITextStyle style); void bzUISetTextShadowStyle(BzUI *ui, BzUINode *node, BzUITextShadowStyle style); void bzUISetBorderStyle(BzUI *ui, BzUINode *node, BzUIBorderStyle style); +void bzUISetSpriteStyle(BzUI *ui, BzUINode *node, BzUISpriteStyle style); BzUILayout bzUIGetLayout(const BzUI *ui, BzUINode *node); void bzUISetLayout(BzUI *ui, BzUINode *node, BzUILayout layout);