1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
#include "game.h"
struct Widget *
add_button(unsigned char layer, const char *label, int x, int y,
void (*callback)(struct Widget *))
{
SDL_Surface *surface;
SDL_Color c = {255, 255, 255, 255};
struct Resources *res = &game->resources;
struct Layer *l = &game->layers[layer];
struct Widget *w;
w = &l->widgets[l->num_widgets ++];
w->callback = callback;
w->is_blended = true;
surface = TTF_RenderText_Solid(res->font, label, c);
TTF_SizeText(res->font, label, &w->rect.w, &w->rect.h);
w->texture = SDL_CreateTextureFromSurface(game->renderer, surface);
res->textures[res->num_textures ++] = w->texture;
SDL_FreeSurface(surface);
if(x == POSITION_CENTERED)
w->rect.x = (game->width / 2) - (w->rect.w / 2);
else
w->rect.x = x;
if(y == POSITION_CENTERED)
w->rect.y = (game->height / 2) - (w->rect.h / 2);
else
w->rect.y = y;
return w;
}
void set_color(struct Widget *w, struct Color color)
{
w->color = color;
w->on_hover_color = color;
w->default_color = color;
}
struct Widget *add_image(unsigned char layer, SDL_Texture *image,
int x, int y, int w, int h)
{
struct Layer *l = &game->layers[layer];
struct Widget *widget;
widget = &l->widgets[l->num_widgets ++];
widget->callback = NULL;
widget->texture = image;
widget->is_blended = false;
widget->rect.w = w;
widget->rect.h = h;
widget->color = (struct Color){255, 255, 255};
widget->default_color = widget->color;
if(x == POSITION_CENTERED)
widget->rect.x = (game->width / 2) - (widget->rect.w / 2);
else
widget->rect.x = x;
if(y == POSITION_CENTERED)
widget->rect.y = (game->height / 2) - (widget->rect.h / 2);
else
widget->rect.y = y;
return widget;
}
void process_widget_events(unsigned char type, int x, int y)
{
int i;
struct Layer *layer = &game->layers[game->current_layer];
struct Widget *widget;
for(i = 0; i < layer->num_widgets; i ++)
{
widget = &layer->widgets[i];
if(x > widget->rect.x && x < widget->rect.x + widget->rect.w
&& y > widget->rect.y && y < widget->rect.y + widget->rect.h)
{
widget->is_hover = true;
widget->color = widget->on_hover_color;
if(widget->callback != NULL && type == MOUSE_BUTTON)
widget->callback(widget);
}
else
{
widget->is_hover = false;
widget->color = widget->default_color;
}
}
}
struct Widget *add_label(unsigned char layer,
const char *text,
struct Color color,
int x, int y)
{
SDL_Surface *surface;
SDL_Color c = (SDL_Color){color.r, color.g, color.b, 255};
struct Resources *res = &game->resources;
struct Layer *l = &game->layers[layer];
struct Widget *w;
w = &l->widgets[l->num_widgets ++];
w->callback = NULL;
w->is_blended = true;
w->initial_x = x;
w->initial_y = y;
w->default_color = color;
w->color = color;
w->on_hover_color = color;
surface = TTF_RenderText_Solid(res->font, text, c);
TTF_SizeText(res->font, text, &w->rect.w, &w->rect.h);
w->texture = SDL_CreateTextureFromSurface(game->renderer, surface);
res->textures[res->num_textures ++] = w->texture;
SDL_FreeSurface(surface);
if(x == POSITION_CENTERED)
w->rect.x = (game->width / 2) - (w->rect.w / 2);
else
w->rect.x = x;
if(y == POSITION_CENTERED)
w->rect.y = (game->height / 2) - (w->rect.h / 2);
else
w->rect.y = y;
return w;
}
void set_text(struct Widget *w, const char *text)
{
int i;
SDL_Surface *surface;
SDL_Texture *texture;
SDL_Color c = (SDL_Color){w->default_color.r,
w->default_color.g,
w->default_color.b,
255};
surface = TTF_RenderText_Solid(game->resources.font, text, c);
TTF_SizeText(game->resources.font, text, &w->rect.w, &w->rect.h);
texture = SDL_CreateTextureFromSurface(game->renderer, surface);
SDL_FreeSurface(surface);
for(i = 0; i < game->resources.num_textures; i ++)
{
if(game->resources.textures[i] == w->texture)
{
SDL_DestroyTexture(w->texture);
game->resources.textures[i] = texture;
}
}
w->texture = texture;
if(w->initial_x == POSITION_CENTERED)
w->rect.x = (game->width / 2) - (w->rect.w / 2);
if(w->initial_y == POSITION_CENTERED)
w->rect.y = (game->height / 2) - (w->rect.h / 2);
}
|