Add sprite drawing for UI
This commit is contained in:
@@ -31,6 +31,7 @@ typedef struct BzUINode {
|
|||||||
i32 textStyleIdx;
|
i32 textStyleIdx;
|
||||||
i32 textShadowStyleIdx;
|
i32 textShadowStyleIdx;
|
||||||
i32 borderStyleIdx;
|
i32 borderStyleIdx;
|
||||||
|
i32 spriteStyleIdx;
|
||||||
|
|
||||||
BzUISize semanticSize[BZ_UI_AXIS_COUNT];
|
BzUISize semanticSize[BZ_UI_AXIS_COUNT];
|
||||||
f32 padding[BZ_UI_AXIS_COUNT * 2];
|
f32 padding[BZ_UI_AXIS_COUNT * 2];
|
||||||
@@ -58,6 +59,7 @@ typedef struct BzUI {
|
|||||||
BzUITextStyle *textStyles;
|
BzUITextStyle *textStyles;
|
||||||
BzUITextShadowStyle *textShadowStyles;
|
BzUITextShadowStyle *textShadowStyles;
|
||||||
BzUIBorderStyle *borderStyles;
|
BzUIBorderStyle *borderStyles;
|
||||||
|
BzUISpriteStyle *spriteStyles;
|
||||||
|
|
||||||
// TODO: Use arena (when implemented) instead of stack allocator.
|
// TODO: Use arena (when implemented) instead of stack allocator.
|
||||||
BzStackAlloc strArena;
|
BzStackAlloc strArena;
|
||||||
@@ -89,6 +91,7 @@ static void bzUINodeClearStyles(BzUINode *node) {
|
|||||||
node->textStyleIdx = -1;
|
node->textStyleIdx = -1;
|
||||||
node->textShadowStyleIdx = -1;
|
node->textShadowStyleIdx = -1;
|
||||||
node->borderStyleIdx = -1;
|
node->borderStyleIdx = -1;
|
||||||
|
node->spriteStyleIdx = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
BzUI *bzUICreate() {
|
BzUI *bzUICreate() {
|
||||||
@@ -111,6 +114,7 @@ BzUI *bzUICreate() {
|
|||||||
ui->textStyles = bzArrayCreate(BzUITextStyle, 10);
|
ui->textStyles = bzArrayCreate(BzUITextStyle, 10);
|
||||||
ui->textShadowStyles = bzArrayCreate(BzUITextShadowStyle, 10);
|
ui->textShadowStyles = bzArrayCreate(BzUITextShadowStyle, 10);
|
||||||
ui->borderStyles = bzArrayCreate(BzUIBorderStyle, 10);
|
ui->borderStyles = bzArrayCreate(BzUIBorderStyle, 10);
|
||||||
|
ui->spriteStyles = bzArrayCreate(BzUISpriteStyle, 10);
|
||||||
|
|
||||||
// 10kb should be enough
|
// 10kb should be enough
|
||||||
ui->strArena = bzStackAllocCreate(10240);
|
ui->strArena = bzStackAllocCreate(10240);
|
||||||
@@ -129,6 +133,7 @@ void bzUIDestroy(BzUI *ui) {
|
|||||||
bzArrayDestroy(ui->textStyles);
|
bzArrayDestroy(ui->textStyles);
|
||||||
bzArrayDestroy(ui->textShadowStyles);
|
bzArrayDestroy(ui->textShadowStyles);
|
||||||
bzArrayDestroy(ui->borderStyles);
|
bzArrayDestroy(ui->borderStyles);
|
||||||
|
bzArrayDestroy(ui->spriteStyles);
|
||||||
|
|
||||||
bzStackAllocDestroy(&ui->strArena);
|
bzStackAllocDestroy(&ui->strArena);
|
||||||
|
|
||||||
@@ -156,6 +161,7 @@ void bzUIBegin(BzUI *ui, i32 width, i32 height) {
|
|||||||
bzArrayClear(ui->textStyles);
|
bzArrayClear(ui->textStyles);
|
||||||
bzArrayClear(ui->textShadowStyles);
|
bzArrayClear(ui->textShadowStyles);
|
||||||
bzArrayClear(ui->borderStyles);
|
bzArrayClear(ui->borderStyles);
|
||||||
|
bzArrayClear(ui->spriteStyles);
|
||||||
|
|
||||||
bzStackAllocReset(&ui->strArena);
|
bzStackAllocReset(&ui->strArena);
|
||||||
|
|
||||||
@@ -209,13 +215,13 @@ static void calculateAxisSizePostorder(const BzUI *ui, const BzUIAxis axis, BzUI
|
|||||||
case BZ_UI_SIZE_CHILD_SUM:
|
case BZ_UI_SIZE_CHILD_SUM:
|
||||||
BZ_ASSERT(node->first);
|
BZ_ASSERT(node->first);
|
||||||
for (BzUINode *child = node->first; child; child = child->next) {
|
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;
|
break;
|
||||||
case BZ_UI_SIZE_CHILD_MAX:
|
case BZ_UI_SIZE_CHILD_MAX:
|
||||||
BZ_ASSERT(node->first);
|
BZ_ASSERT(node->first);
|
||||||
for (BzUINode *child = node->first; child; child = child->next) {
|
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);
|
compSize = BZ_MAX(compSize, childSize);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -424,6 +430,16 @@ static void renderNode(BzUI *ui, BzUINode *node) {
|
|||||||
DrawRectangleRoundedLines(rect, style.roundness, 0, style.thickness, color);
|
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) {
|
if (node->flags & BZ_UI_DRAW_TEXT) {
|
||||||
BzUITextStyle style = bzUIGetTextStyle(ui, node);
|
BzUITextStyle style = bzUIGetTextStyle(ui, node);
|
||||||
BZ_ASSERT(style.text);
|
BZ_ASSERT(style.text);
|
||||||
@@ -444,7 +460,7 @@ static void renderNode(BzUI *ui, BzUINode *node) {
|
|||||||
drawRect.x, drawRect.y
|
drawRect.x, drawRect.y
|
||||||
}, style.fontSize, style.fontSpacing, color);
|
}, 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);
|
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);
|
BZ_ASSERT(node->borderStyleIdx != -1);
|
||||||
return ui->borderStyles[node->borderStyleIdx];
|
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) \
|
#define BZ_UI_SET_STYLE(ui, node, mIdx, styles, style) \
|
||||||
do { \
|
do { \
|
||||||
@@ -608,6 +629,11 @@ void bzUISetBorderStyle(BzUI *ui, BzUINode *node, BzUIBorderStyle style) {
|
|||||||
BZ_ASSERT(node->flags & BZ_UI_DRAW_BORDER);
|
BZ_ASSERT(node->flags & BZ_UI_DRAW_BORDER);
|
||||||
BZ_UI_SET_STYLE(ui, node, borderStyleIdx, borderStyles, style);
|
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
|
#undef BZ_UI_SET_STYLE
|
||||||
|
|
||||||
BzUILayout bzUIGetLayout(const BzUI *ui, BzUINode *node) {
|
BzUILayout bzUIGetLayout(const BzUI *ui, BzUINode *node) {
|
||||||
|
|||||||
@@ -143,6 +143,15 @@ typedef struct BzUITextShadowStyle {
|
|||||||
Color active;
|
Color active;
|
||||||
} BzUITextShadowStyle;
|
} BzUITextShadowStyle;
|
||||||
|
|
||||||
|
typedef struct BzUISpriteStyle {
|
||||||
|
Texture2D texture;
|
||||||
|
Rectangle rec;
|
||||||
|
|
||||||
|
Color tintNormal;
|
||||||
|
Color tintHover;
|
||||||
|
Color tintActive;
|
||||||
|
} BzUISpriteStyle;
|
||||||
|
|
||||||
typedef struct BzUINode BzUINode;
|
typedef struct BzUINode BzUINode;
|
||||||
|
|
||||||
typedef struct BzUINodeDesc {
|
typedef struct BzUINodeDesc {
|
||||||
@@ -189,12 +198,14 @@ BzUIBoxShadowStyle bzUIGetBoxShadowStyle(const BzUI *ui, BzUINode *node);
|
|||||||
BzUITextStyle bzUIGetTextStyle(const BzUI *ui, BzUINode *node);
|
BzUITextStyle bzUIGetTextStyle(const BzUI *ui, BzUINode *node);
|
||||||
BzUITextShadowStyle bzUIGetTextShadowStyle(const BzUI *ui, BzUINode *node);
|
BzUITextShadowStyle bzUIGetTextShadowStyle(const BzUI *ui, BzUINode *node);
|
||||||
BzUIBorderStyle bzUIGetBorderStyle(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 bzUISetBackgroundStyle(BzUI *ui, BzUINode *node, BzUIBackgroundStyle style);
|
||||||
void bzUISetBoxShadowStyle(BzUI *ui, BzUINode *node, BzUIBoxShadowStyle style);
|
void bzUISetBoxShadowStyle(BzUI *ui, BzUINode *node, BzUIBoxShadowStyle style);
|
||||||
void bzUISetTextStyle(BzUI *ui, BzUINode *node, BzUITextStyle style);
|
void bzUISetTextStyle(BzUI *ui, BzUINode *node, BzUITextStyle style);
|
||||||
void bzUISetTextShadowStyle(BzUI *ui, BzUINode *node, BzUITextShadowStyle style);
|
void bzUISetTextShadowStyle(BzUI *ui, BzUINode *node, BzUITextShadowStyle style);
|
||||||
void bzUISetBorderStyle(BzUI *ui, BzUINode *node, BzUIBorderStyle style);
|
void bzUISetBorderStyle(BzUI *ui, BzUINode *node, BzUIBorderStyle style);
|
||||||
|
void bzUISetSpriteStyle(BzUI *ui, BzUINode *node, BzUISpriteStyle style);
|
||||||
|
|
||||||
BzUILayout bzUIGetLayout(const BzUI *ui, BzUINode *node);
|
BzUILayout bzUIGetLayout(const BzUI *ui, BzUINode *node);
|
||||||
void bzUISetLayout(BzUI *ui, BzUINode *node, BzUILayout layout);
|
void bzUISetLayout(BzUI *ui, BzUINode *node, BzUILayout layout);
|
||||||
|
|||||||
Reference in New Issue
Block a user