GoldFish Engine
Quite simple and lightweight game engine
Loading...
Searching...
No Matches
gf_gui.c
1#define GF_EXPOSE_GUI
2#define GF_EXPOSE_DRAW
3#define GF_EXPOSE_INPUT
4
5#include <gf_pre.h>
6
7/* External library */
8
9/* Interface */
10#include <gf_gui.h>
11
12/* Engine */
13#include <gf_graphic.h>
14#include <gf_draw.h>
15#include <gf_log.h>
16
17/* Standard */
18#include <stdlib.h>
19#include <string.h>
20
21const double gf_gui_border_width = 2;
22
23gf_graphic_color_t gf_gui_base_color;
24gf_graphic_color_t gf_gui_font_color;
25
26gf_gui_t* gf_gui_create(gf_engine_t* engine, gf_draw_t* draw) {
27 gf_gui_t* gui = malloc(sizeof(*gui));
29 memset(gui, 0, sizeof(*gui));
30 gui->engine = engine;
31 gui->draw = draw;
32
33 gui->pressed = -1;
34
35 GF_SET_COLOR(gf_gui_base_color, 48, 96, 48, 255);
36 GF_SET_COLOR(gf_gui_font_color, 256 - 32, 256 - 32, 256 - 32, 255);
37
38 for(i = 0; i < GF_GUI_MAX_COMPONENTS; i++) gui->area[i].type = -1;
39
40 return gui;
41}
42
43/* note... left top should be the lightest in the border */
44
45void gf_gui_draw_box(gf_gui_t* gui, int mul, double x, double y, double w, double h) {
46 const int color_diff = 32; /* color diff */
48
49 int cd = mul * color_diff;
50
51 col = gf_gui_base_color;
52 col.r += cd;
53 col.g += cd;
54 col.b += cd;
55 gf_graphic_fill_rect(gui->draw, x, y, w, h, col);
56
57 col = gf_gui_base_color;
58 col.r -= cd;
59 col.g -= cd;
60 col.b -= cd;
61 gf_graphic_fill_polygon(gui->draw, col, GF_GRAPHIC_2D, 5, x + w, y + h, x + w, y, x + w - gf_gui_border_width, y + gf_gui_border_width, x + gf_gui_border_width, y + h - gf_gui_border_width, x, y + h);
62
63 col = gf_gui_base_color;
64 gf_graphic_fill_rect(gui->draw, x + gf_gui_border_width, y + gf_gui_border_width, w - gf_gui_border_width * 2, h - gf_gui_border_width * 2, col);
65}
66
67gf_gui_component_t* gf_gui_first_unused(gf_gui_t* gui, gf_gui_id_t* id) {
69 for(i = 0; i < GF_GUI_MAX_COMPONENTS; i++) {
70 if(gui->area[i].type == GF_GUI_UNUSED) {
71 *id = i;
72 return &gui->area[i];
73 }
74 }
75 return NULL;
76}
77
78gf_gui_id_t gf_gui_create_button(gf_gui_t* gui, double x, double y, double w, double h, const char* text) {
79 gf_gui_id_t id;
80 gf_gui_component_t* c = gf_gui_first_unused(gui, &id);
81
82 c->type = GF_GUI_BUTTON;
83 c->x = x;
84 c->y = y;
85 c->width = w;
86 c->height = h;
87
88 c->pressed = 0;
89 c->callback = NULL;
90
91 c->u.button.text = malloc(strlen(text) + 1);
92 strcpy(c->u.button.text, text);
93 return id;
94}
95
96void gf_gui_render(gf_gui_t* gui) {
98 gf_input_t* input = gui->draw->input;
99 for(i = GF_GUI_MAX_COMPONENTS - 1; i >= 0; i--) {
100 gf_gui_component_t* c = &gui->area[i];
101 double cx = c->x;
102 double cy = c->y;
103 double cw = c->width;
104 double ch = c->height;
105 switch(c->type) {
106 case GF_GUI_BUTTON: {
107 if(input->mouse_x != -1 && input->mouse_y != -1 && gui->pressed == -1 && (input->mouse_flag & GF_INPUT_MOUSE_LEFT_MASK) && (cx <= input->mouse_x && input->mouse_x <= cx + cw) && (cy <= input->mouse_y && input->mouse_y <= cy + ch)) {
108 gui->pressed = i;
109 } else if(gui->pressed == -1) {
110 c->pressed = 0;
111 }
112 break;
113 }
114 }
115 }
116 for(i = 0; i < GF_GUI_MAX_COMPONENTS; i++) {
117 gf_gui_component_t* c = &gui->area[i];
118 double cx = c->x;
119 double cy = c->y;
120 double cw = c->width;
121 double ch = c->height;
122 switch(c->type) {
123 case GF_GUI_BUTTON: {
124 double x = cx + cw / 2 - gf_graphic_text_width(gui->draw, GF_GUI_FONT_SIZE, c->u.button.text) / 2;
125 double y = cy + ch / 2 - GF_GUI_FONT_SIZE / 2;
126 if(gui->pressed == i) {
127 x += gf_gui_border_width / 1;
128 y += gf_gui_border_width / 1;
129 }
130 gf_gui_draw_box(gui, (gui->pressed == i) ? GF_GUI_INVERT : GF_GUI_NORMAL, cx, cy, cw, ch);
131 gf_graphic_text(gui->draw, x, y, GF_GUI_FONT_SIZE, c->u.button.text, gf_gui_font_color);
132 break;
133 }
134 }
135 }
136 if((gui->pressed != -1) && !(input->mouse_flag & GF_INPUT_MOUSE_LEFT_MASK)) {
137 if(gui->area[gui->pressed].callback != NULL) {
138 gui->area[gui->pressed].callback(gui->engine, gui->draw, gui->pressed, GF_GUI_PRESS_EVENT);
139 }
140 gui->area[gui->pressed].pressed = 1;
141 gui->pressed = -1;
142 }
143}
144
145void gf_gui_set_callback(gf_gui_t* gui, gf_gui_id_t id, gf_gui_callback_t callback) { gui->area[id].callback = callback; }
Drawing interface.
Graphic interface.
#define GF_SET_COLOR(color, red, green, blue, alpha)
Macro to set color safely and shorter.
Definition gf_graphic.h:39
#define GF_GRAPHIC_2D
Dimension parameter for 2D.
Definition gf_graphic.h:26
#define GF_GUI_INVERT
Draw border inverted.
Definition gf_gui.h:32
#define GF_GUI_FONT_SIZE
Default GUI font size.
Definition gf_gui.h:38
#define GF_GUI_NORMAL
Draw border normally.
Definition gf_gui.h:26
Logger.
Required headers before anything.
int gf_gui_id_t
Component ID.
Definition gui.h:17
#define GF_GUI_MAX_COMPONENTS
Max components GUI can handle.
Definition gui.h:48
#define GF_INPUT_MOUSE_LEFT_MASK
Mask for left mouse button.
Definition input.h:28
Drawing interface.
Definition draw.h:108
gf_input_t * input
Input interface.
Definition draw.h:108
Engine instance.
Definition core.h:46
double g
Green.
Definition graphic.h:44
double b
Blue.
Definition graphic.h:44
double r
Red.
Definition graphic.h:44
char * text
Button text.
Definition gui.h:67
Component.
Definition gui.h:120
int pressed
1 if pressed, otherwise 0
Definition gui.h:120
double y
X coord of component.
Definition gui.h:120
double width
Width of component.
Definition gui.h:120
double height
Height of component.
Definition gui.h:120
gf_gui_callback_t callback
Event callback.
Definition gui.h:120
gf_gui_union_t u
Component union.
Definition gui.h:120
int type
Component type.
Definition gui.h:120
double x
X coord of component.
Definition gui.h:120
GUI.
Definition gui.h:144
gf_gui_id_t pressed
1 if something is being pressed, otherwise 0
Definition gui.h:144
gf_engine_t * engine
Engine instance.
Definition gui.h:144
gf_draw_t * draw
Drawing interface.
Definition gui.h:144
gf_gui_component_t area[64]
Created components.
Definition gui.h:144
Input interface.
Definition input.h:64
int mouse_flag
Mouse flag.
Definition input.h:64
int mouse_y
Y coord of mouse.
Definition input.h:64
int mouse_x
X coord of mouse.
Definition input.h:64
gf_gui_button_t button
Button component.
Definition gui.h:78