Add top bar game UI

This commit is contained in:
2023-12-28 09:47:55 +01:00
parent 0d09ffd67d
commit 118f7f9d93
6 changed files with 99 additions and 6 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 366 KiB

After

Width:  |  Height:  |  Size: 368 KiB

View File

@@ -331,6 +331,17 @@ typedef enum AnimType {
ANIM_NONE,
} AnimType;
static EntityType getEntityTile(EntityType type) {
switch (type) {
case ENTITY_WORKER: return 27;
case ENTITY_WOOD: return 7424;
case ENTITY_STONE: return 7425;
case ENTITY_GOLD: return 7426;
case ENTITY_POP: return 7427;
default: return -1;
}
}
static bool entityHasAnimation(EntityType entity, AnimType type) {
switch (entity) {
case ENTITY_WORKER:

View File

@@ -280,6 +280,12 @@ void update(float dt, void *userData) {
}
}
static void drawOverScreen(Color c) {
i32 width = GetScreenWidth();
i32 height = GetScreenHeight();
DrawRectangle(0, 0, width, height, c);
}
static void renderGame(Game *game, float dt) {
ClearBackground(RAYWHITE);
BeginMode2D(game->camera);
@@ -300,13 +306,48 @@ static void renderGame(Game *game, float dt) {
EndMode2D();
}
static void drawOverScreen(Color c) {
// UI
i32 width = GetScreenWidth();
i32 height = GetScreenHeight();
bzUIBegin(UI, width, height);
bzUISetParentLayout(UI, (BzUILayout) {
.type = BZ_UI_LAYOUT_FLEX_BOX,
.flags = BZ_UI_FLEX_DIR_COLUMN |
BZ_UI_FLEX_JUSTIFY_SPACE_BETWEEN |
BZ_UI_FLEX_ALIGN_CENTER
});
// top bar
f32 topBarHeight = 0.05f;
BzUINode *topBar = uiPushDivParentPercentage(1.0f, topBarHeight);
bzUISetParentLayout(UI, (BzUILayout) {
.type = BZ_UI_LAYOUT_FLEX_BOX,
.flags = BZ_UI_FLEX_DIR_ROW |
BZ_UI_FLEX_ALIGN_CENTER |
BZ_UI_FLEX_JUSTIFY_START
});
Color topBarBG = {0, 0, 0, 50};
bzUISetBackgroundStyle(UI, topBar, (BzUIBackgroundStyle) {
.normal = topBarBG,
.hover = topBarBG,
.active = topBarBG,
});
BzTileset *tileset = &game->tileset;
Rectangle woodRec = bzTilesetGetTileRegion(tileset, getEntityTile(ENTITY_WOOD));
Rectangle stoneRec = bzTilesetGetTileRegion(tileset, getEntityTile(ENTITY_STONE));
Rectangle goldRec = bzTilesetGetTileRegion(tileset, getEntityTile(ENTITY_GOLD));
Rectangle popRec = bzTilesetGetTileRegion(tileset, getEntityTile(ENTITY_POP));
uiGameResCount(100, -1, woodRec, tileset->tiles);
uiGameResCount(100, -1, stoneRec, tileset->tiles);
uiGameResCount(100, -1, goldRec, tileset->tiles);
uiGameResCount(25, 100, popRec, tileset->tiles);
bzUIPopParent(UI);
bzUIEnd(UI);
DrawRectangle(0, 0, width, height, c);
}
static void renderMainMenu(Game *game, float dt) {
i32 width = GetScreenWidth();
i32 height = GetScreenHeight();

View File

@@ -16,8 +16,8 @@ Font getFont() {
return game->font;
}
void uiPushDivParentPercentage(f32 xPercent, f32 yPercent) {
bzUIPushDiv(UI, (BzUISize) {
BzUINode *uiPushDivParentPercentage(f32 xPercent, f32 yPercent) {
return bzUIPushDiv(UI, (BzUISize) {
.kind = BZ_UI_SIZE_REL_PARENT,
.value = xPercent,
}, (BzUISize) {
@@ -201,3 +201,40 @@ void uiSettingsCheckbox(const char *txt, bool *check) {
void uiSettingsSlider(const char *txt, f32 *value) {
uiBaseSlider(txt, getFont(), 1.0f, value, 0, 10);
}
void uiGameResCount(i32 amount, i32 capacity, Rectangle icon, Texture2D texture) {
bzUIPushDiv(UI, (BzUISize) { BZ_UI_SIZE_CHILD_SUM },
(BzUISize) { BZ_UI_SIZE_CHILD_MAX });
bzUISetParentLayout(UI, (BzUILayout) {
BZ_UI_LAYOUT_FLEX_BOX,
BZ_UI_FLEX_DIR_ROW | BZ_UI_FLEX_ALIGN_CENTER
});
char buf[64];
BzUIKey id = bzUIGetUniqueKey(UI);
if (capacity == -1)
snprintf(buf, sizeof(buf), "%d##%d", amount, id);
else
snprintf(buf, sizeof(buf), "%d/%d##%d", amount, capacity, id);
const f32 scl = 0.4f;
uiBaseLabel(buf, getFont(), scl);
BzUINode *iconNode = bzUINodeMake(UI, id, &(BzUINodeDesc) {
.flags = BZ_UI_DRAW_SPRITE | BZ_UI_ALIGN_CENTER,
.semanticSize[BZ_UI_AXIS_X] = {
BZ_UI_SIZE_PIXELS, 80 * scl * uiGetScale(),
},
.semanticSize[BZ_UI_AXIS_Y] = {
BZ_UI_SIZE_PIXELS, 80 * scl * uiGetScale(),
},
.margin = {0, 0, 20 * uiGetScale(), 0}
});
bzUISetSpriteStyle(UI, iconNode, (BzUISpriteStyle) {
.rec = icon,
.texture = texture,
.tintNormal = WHITE,
.tintHover = WHITE,
.tintActive = WHITE,
});
bzUIPopParent(UI);
}

View File

@@ -8,7 +8,7 @@ extern BzUI *UI; // defined in main.c
f32 uiGetScale();
void uiPushDivParentPercentage(f32 xPercent, f32 yPercent);
BzUINode *uiPushDivParentPercentage(f32 xPercent, f32 yPercent);
// Template stuff
void uiBaseLabel(const char *txt, Font font, f32 scl);
@@ -25,4 +25,6 @@ bool uiSettingsButton(const char *txt);
void uiSettingsCheckbox(const char *txt, bool *check);
void uiSettingsSlider(const char *txt, f32 *value);
void uiGameResCount(i32 amount, i32 capacity, Rectangle icon, Texture2D texture);
#endif //PIXELDEFENSE_UI_WIDGETS_H

View File

@@ -72,6 +72,8 @@ anim_writer = AnimationWriter(writer, "entity", "anim", entity_tiles)
anim_writer.output_enum()
anim_writer.output_anim_enum()
anim_writer.output_enum_to_tile("getEntityTile")
anim_writer.output_has_anim("entityHasAnimation")
anim_writer.output_anim_sequence("entityGetAnimationSequence")
anim_writer.output_anim_frame("entityGetAnimationFrame")