diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-06-24 16:55:18 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-06-26 20:00:27 +0200 |
| commit | 7c75118429596dcfd86dbefb32e9ae79585c4da0 (patch) | |
| tree | d06478b8e303d0e2729c57b079f481fbcf5b9adf /src/gui/inc | |
| parent | f17c76c4007563389188c147d4e1c766039fb686 (diff) | |
| download | rts-7c75118429596dcfd86dbefb32e9ae79585c4da0.tar.gz | |
Revamp gui component
`gui` was tighly coupled to game logic, and could not be extended for
other purposes. Therefore, a generic GUI implementation, loosely
inspired by well-known GUI frameworks such as GTK, is now provided, with
the following properties:
- Does not depend on dynamic or static memory allocation, only automatic
(i.e., stack) memory allocation required.
- Portable among existing implementations.
- Simple to extend.
- Tiny memory footprint.
`gui` is now composed by GUI elements that can be chained to form a tree
structure. This is useful e.g.: to calculate X/Y coordinates for a given
GUI element given its parent(s).
This commit also refactors the older implementation, moving
game-specific logic into `player` and making use of the new component.
Diffstat (limited to 'src/gui/inc')
| -rw-r--r-- | src/gui/inc/gui.h | 31 | ||||
| -rw-r--r-- | src/gui/inc/gui/bar.h | 39 | ||||
| -rw-r--r-- | src/gui/inc/gui/button.h | 44 | ||||
| -rw-r--r-- | src/gui/inc/gui/label.h | 30 | ||||
| -rw-r--r-- | src/gui/inc/gui/progress_bar.h | 37 | ||||
| -rw-r--r-- | src/gui/inc/gui/rounded_rect.h | 43 |
6 files changed, 207 insertions, 17 deletions
diff --git a/src/gui/inc/gui.h b/src/gui/inc/gui.h index 254fd47..ec87f45 100644 --- a/src/gui/inc/gui.h +++ b/src/gui/inc/gui.h @@ -1,33 +1,30 @@ #ifndef GUI_H #define GUI_H +#include <camera.h> #include <gfx.h> -#include <human_player.h> +#include <peripheral.h> #ifdef __cplusplus extern "C" { #endif -enum +struct gui_common { - GUI_BAR_LEFT, - GUI_BAR_MID, - GUI_BAR_RIGHT, - GUI_SELECTION_UP_LEFT, - GUI_SELECTION_UP_RIGHT, - GUI_SELECTION_MID_VERT, - GUI_SELECTION_DOWN_LEFT, - GUI_SELECTION_DOWN_RIGHT, - GUI_SELECTION_MID, - - MAX_GUI_SPRITES + int (*update)(struct gui_common *, const union peripheral *, + const struct camera *); + int (*render)(const struct gui_common *); + short x, y; + bool centered; + struct gui_common *parent, *child, *sibling; }; -void gui_update(struct human_player *h); -int gui_render(const struct human_player *h); - -extern struct sprite gui_sprites[MAX_GUI_SPRITES]; +void gui_add_child(struct gui_common *parent, struct gui_common *child); +void gui_add_sibling(struct gui_common *g, struct gui_common *sibling); +int gui_update(struct gui_common *g, const union peripheral *p, + const struct camera *c); +int gui_render(const struct gui_common *g); #ifdef __cplusplus } diff --git a/src/gui/inc/gui/bar.h b/src/gui/inc/gui/bar.h new file mode 100644 index 0000000..a8000cf --- /dev/null +++ b/src/gui/inc/gui/bar.h @@ -0,0 +1,39 @@ +#ifndef GUI_BAR_H +#define GUI_BAR_H + +#include <gui.h> +#include <gfx.h> +#include <util.h> +#include <stddef.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct gui_bar +{ + struct gui_common common; +}; + +UTIL_STATIC_ASSERT(!offsetof(struct gui_bar, common), + "unexpected offset for struct gui_bar"); + +void gui_bar_init(struct gui_bar *b); + +enum +{ + GUI_BAR_LEFT, + GUI_BAR_MID, + GUI_BAR_RIGHT, + + MAX_GUI_BAR_SPRITES +}; + +extern struct sprite gui_bar_sprites[MAX_GUI_BAR_SPRITES]; + +#ifdef __cplusplus +} +#endif + +#endif /* GUI_BAR_H */ diff --git a/src/gui/inc/gui/button.h b/src/gui/inc/gui/button.h new file mode 100644 index 0000000..acfc6c1 --- /dev/null +++ b/src/gui/inc/gui/button.h @@ -0,0 +1,44 @@ +#ifndef GUI_BUTTON_H +#define GUI_BUTTON_H + +#include <gfx.h> +#include <gui.h> +#include <gui/label.h> +#include <util.h> +#include <stddef.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct gui_button +{ + struct gui_common common; + struct gui_label label; + short w; + void *arg; + void (*on_pressed)(void *); +}; + +UTIL_STATIC_ASSERT(!offsetof(struct gui_button, common), + "unexpected offset for struct gui_button"); + +void gui_button_init(struct gui_button *b); + +enum +{ + GUI_BUTTON_LEFT, + GUI_BUTTON_MID, + GUI_BUTTON_RIGHT, + + MAX_GUI_BUTTON_SPRITES +}; + +extern struct sprite gui_button_sprites[MAX_GUI_BUTTON_SPRITES]; + +#ifdef __cplusplus +} +#endif + +#endif /* GUI_BUTTON_H */ diff --git a/src/gui/inc/gui/label.h b/src/gui/inc/gui/label.h new file mode 100644 index 0000000..7a30624 --- /dev/null +++ b/src/gui/inc/gui/label.h @@ -0,0 +1,30 @@ +#ifndef GUI_LABEL_H +#define GUI_LABEL_H + +#include <gui.h> +#include <font.h> +#include <util.h> +#include <stddef.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct gui_label +{ + struct gui_common common; + enum font font; + const char *text; +}; + +UTIL_STATIC_ASSERT(!offsetof(struct gui_label, common), + "unexpected offset for struct gui_label"); + +void gui_label_init(struct gui_label *l); + +#ifdef __cplusplus +} +#endif + +#endif /* GUI_LABEL_H */ diff --git a/src/gui/inc/gui/progress_bar.h b/src/gui/inc/gui/progress_bar.h new file mode 100644 index 0000000..4fa5129 --- /dev/null +++ b/src/gui/inc/gui/progress_bar.h @@ -0,0 +1,37 @@ +#ifndef GUI_PROGRESS_BAR_H +#define GUI_PROGRESS_BAR_H + +#include <gui.h> +#include <util.h> +#include <stdbool.h> +#include <stddef.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + +enum {GUI_PROGRESS_BAR_MAX = 255}; + +struct gui_progress_bar +{ + struct gui_common common; + short w, h; + bool stp; + unsigned char progress; + struct + { + unsigned char r, g, b; + } fg, bg; +}; + +UTIL_STATIC_ASSERT(!offsetof(struct gui_progress_bar, common), + "unexpected offset for struct gui_progress_bar"); + +void gui_progress_bar_init(struct gui_progress_bar *pb); + +#ifdef __cplusplus +} +#endif + +#endif /* GUI_PROGRESS_BAR_H */ diff --git a/src/gui/inc/gui/rounded_rect.h b/src/gui/inc/gui/rounded_rect.h new file mode 100644 index 0000000..2d5a273 --- /dev/null +++ b/src/gui/inc/gui/rounded_rect.h @@ -0,0 +1,43 @@ +#ifndef GUI_ROUNDED_RECT_H +#define GUI_ROUNDED_RECT_H + +#include <gui.h> +#include <gfx.h> +#include <util.h> +#include <stddef.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct gui_rounded_rect +{ + struct gui_common common; + unsigned short w, h; +}; + +UTIL_STATIC_ASSERT(!offsetof(struct gui_rounded_rect, common), + "unexpected offset for struct gui_rounded_rect"); + +void gui_rounded_rect_init(struct gui_rounded_rect *l); + +enum +{ + GUI_ROUNDED_RECT_UP_LEFT, + GUI_ROUNDED_RECT_UP_RIGHT, + GUI_ROUNDED_RECT_MID_VERT, + GUI_ROUNDED_RECT_DOWN_LEFT, + GUI_ROUNDED_RECT_DOWN_RIGHT, + GUI_ROUNDED_RECT_MID, + + MAX_GUI_ROUNDED_RECT_SPRITES +}; + +extern struct sprite gui_rounded_rect_sprites[MAX_GUI_ROUNDED_RECT_SPRITES]; + +#ifdef __cplusplus +} +#endif + +#endif /* GUI_ROUNDED_RECT_H */ |
