Implement checkbox GUI element

This commit is contained in:
Xavier Del Campo Romero 2022-09-23 04:12:07 +02:00
parent 467f09e952
commit 9d3e754f36
8 changed files with 127 additions and 0 deletions

View File

@ -208,6 +208,14 @@ sprite(NAME line_edit_right
CY 491
TRANSPARENT FALSE)
sprite(NAME checkbox
X 376
Y 132
BPP 4
CX 368
CY 490
TRANSPARENT TRUE)
sound(NAME acknowledge_01)
sound(NAME acknowledge_02)
sound(NAME selected_01)
@ -218,6 +226,7 @@ container(NAME rts
btn_left
btn_mid
btn_right
checkbox
worker_n
worker_ne
worker_e

View File

@ -57,6 +57,8 @@ line_edit_mid.bmp:
line_edit_mid_24.bmp:
line_edit_right.bmp:
line_edit_right_24.bmp:
checkbox_24.bmp:
checkbox.bmp:
Derived works from ui_sheet.png
font.bmp:

BIN
res/checkbox.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 382 B

BIN
res/checkbox_24.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -7,6 +7,7 @@
#include <gui/button.h>
#include <gui/line_edit.h>
#include <gui/rounded_rect.h>
#include <gui/checkbox.h>
#include <resource.h>
#include <terrain.h>
#include <unit.h>
@ -273,6 +274,15 @@ static const struct container c[] =
{
.sprite = &gui_line_edit_sprites[GUI_LINE_EDIT_RIGHT]
}
},
{
.path = "checkbox",
.type = CONTAINER_TYPE_SPRITE,
.data =
{
.sprite = &gui_checkbox_sprite
}
}
};

View File

@ -2,6 +2,7 @@ add_library(gui
"src/bar.c"
"src/button.c"
"src/button_type1.c"
"src/checkbox.c"
"src/container.c"
"src/gui.c"
"src/label.c"

View File

@ -0,0 +1,34 @@
#ifndef GUI_CHECKBOX_H
#define GUI_CHECKBOX_H
#include <gui.h>
#include <gfx.h>
#include <util.h>
#include <stdbool.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct gui_checkbox
{
struct gui_common common;
bool active;
void *arg;
void (*on_pressed)(bool active, void *arg);
};
void gui_checkbox_init(struct gui_checkbox *c);
extern struct sprite gui_checkbox_sprite;
UTIL_STATIC_ASSERT(!offsetof(struct gui_checkbox, common),
"unexpected offset for struct gui_checkbox");
#ifdef __cplusplus
}
#endif
#endif /* GUI_CHECKBOX_H */

71
src/gui/src/checkbox.c Normal file
View File

@ -0,0 +1,71 @@
#include <gui.h>
#include <gui/checkbox.h>
#include <gui_private.h>
#include <gfx.h>
#include <stdbool.h>
struct sprite gui_checkbox_sprite;
static int render(const struct gui_common *const g)
{
const struct gui_checkbox *const c = (const struct gui_checkbox *)g;
sprite_get_or_ret(s, -1);
if (sprite_clone(&gui_checkbox_sprite, s))
return -1;
gui_coords(g, &s->x, &s->y);
s->w /= 2;
if (c->active)
s->u += s->w;
sprite_sort(s);
return 0;
}
static void get_dim(const struct gui_common *const g,
short *const w, short *const h)
{
*w = gui_checkbox_sprite.w / 2;
*h = gui_checkbox_sprite.h;
}
static int update(struct gui_common *const g,
const union peripheral *const p, const struct camera *const cam,
struct input *const in)
{
struct gui_checkbox *const c = (struct gui_checkbox *)g;
short w, h;
get_dim(g, &w, &h);
if (gui_pressed(g, in, p, cam, w, h))
{
c->active ^= true;
if (c->on_pressed)
c->on_pressed(c->active, c->arg);
}
return 0;
}
void gui_checkbox_init(struct gui_checkbox *const c)
{
static const struct gui_common_cb cb =
{
.get_dim = get_dim,
.update = update,
.render = render
};
*c = (const struct gui_checkbox)
{
.common =
{
.cb = &cb
}
};
}