Implement button component

This commit is contained in:
Xavier Del Campo Romero 2022-06-11 23:22:04 +02:00
parent 9b75ff3c2e
commit a84a55aa25
6 changed files with 170 additions and 1 deletions

View File

@ -43,6 +43,7 @@ add_subdirectory("res")
set(components
building
button
camera
container
font

View File

@ -0,0 +1,3 @@
add_library(button "src/button.c")
target_include_directories(button PUBLIC "inc")
target_link_libraries(button PUBLIC gfx PRIVATE font)

36
src/button/inc/button.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef BUTTON_H
#define BUTTON_H
#include <gfx.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
enum
{
BUTTON_LEFT,
BUTTON_MID,
BUTTON_RIGHT,
MAX_BUTTON_SPRITES
};
struct button
{
const char *text;
short x, y, w;
};
bool button_is_pressed(const struct button *b);
int button_render(const struct button *b);
extern struct sprite button_sprites[MAX_BUTTON_SPRITES];
#ifdef __cplusplus
}
#endif
#endif /* BUTTON_H */

100
src/button/src/button.c Normal file
View File

@ -0,0 +1,100 @@
#include <button.h>
#include <font.h>
#include <gfx.h>
struct sprite button_sprites[MAX_BUTTON_SPRITES];
bool button_is_pressed(const struct button *const b)
{
return false;
}
static int render_left(const struct button *const b, short *const x)
{
sprite_get_or_ret(s, -1);
if (sprite_clone(&button_sprites[BUTTON_LEFT], s))
return -1;
s->x = b->x;
s->y = b->y;
sprite_sort(s);
*x = s->x + s->w;
return 0;
}
static int render_mid(const struct button *const b, short *const x)
{
const short mid_w = button_sprites[BUTTON_MID].w;
const short lw = button_sprites[BUTTON_LEFT].w;
const short rw = button_sprites[BUTTON_RIGHT].w;
const short w = b->w - lw - rw;
if (w > 0)
{
const short rem_mid = w > 0 ? w % mid_w : 0;
const short whole_mid = w / mid_w;
const short n_mid = rem_mid ? whole_mid + 1 : whole_mid;
for (struct
{
size_t i;
short x;
} a = {.i = 0, .x = lw};
a.i < n_mid;
a.i++, a.x += mid_w)
{
sprite_get_or_ret(m, -1);
if (sprite_clone(&button_sprites[BUTTON_MID], m))
return -1;
m->x = *x;
m->y = b->y;
if (rem_mid && a.i + 1 == n_mid)
m->w = rem_mid;
else
m->w = mid_w;
sprite_sort(m);
*x += m->w;
}
}
return 0;
}
static int render_right(const struct button *const b, const short *const x)
{
sprite_get_or_ret(s, -1);
if (sprite_clone(&button_sprites[BUTTON_RIGHT], s))
return -1;
s->x = *x;
s->y = b->y;
sprite_sort(s);
return 0;
}
static int render_text(const struct button *const b)
{
const short x = b->x + button_sprites[BUTTON_LEFT].w,
y = b->y + (button_sprites[BUTTON_MID].h >> 2);
return font_printf(FONT, x, y, "%s", b->text) < 0;
}
int button_render(const struct button *const b)
{
short x;
if (render_left(b, &x)
|| render_mid(b, &x)
|| render_right(b, &x)
|| render_text(b))
return -1;
return 0;
}

View File

@ -2,6 +2,7 @@ add_library(game "src/game.c" "src/res.c")
target_include_directories(game PUBLIC "inc" PRIVATE "privinc")
target_link_libraries(game PRIVATE
building
button
container
font
gfx

View File

@ -1,5 +1,6 @@
#include <game_private.h>
#include <building.h>
#include <button.h>
#include <container.h>
#include <font.h>
#include <gfx.h>
@ -216,7 +217,34 @@ static const struct container c[] =
{
.sound = &unit_sounds[UNIT_SOUND_SELECTED]
}
}
},
{
.path = "btn_left",
.type = CONTAINER_TYPE_SPRITE,
.data =
{
.sprite = &button_sprites[BUTTON_LEFT]
}
},
{
.path = "btn_mid",
.type = CONTAINER_TYPE_SPRITE,
.data =
{
.sprite = &button_sprites[BUTTON_MID]
}
},
{
.path = "btn_right",
.type = CONTAINER_TYPE_SPRITE,
.data =
{
.sprite = &button_sprites[BUTTON_RIGHT]
}
},
};
static bool init;