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
|
#include "GameButton.h"
#include "game_utils.h"
/**
* This file, as the rest of the project is under MIT license.
* see http://opensource.org/licenses/MIT
*
* Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
*/
GameButton * game_button_new (int x, int y, int h, int w) {
GameButton * self = malloc (sizeof(GameButton));
fprintf (stdout, "Creating GameButton: address: %ld\n", (long) self);
SDL_Rect * base = (SDL_Rect *) self;
base->x = x;
base->y = y;
base->h = h;
base->w = w;
self->clicked = NULL;
self->is_clicked = false;
self->fill = false;
self->hover = NULL;
self->is_hover = false;
self->is_stop_hover = false;
self->colour = game_color_create_color (0,0,255,255);
return self;
}
bool game_button_envelopes_SDL_Point (GameButton *self, SDL_Point * point) {
if (SDL_EnclosePoints (point, 1, (SDL_Rect *)self, NULL)) {
return true;
} else {
return false;
}
}
bool game_button_check_clicked (GameButton * self, MousePointer * pointer) {
SDL_bool contains = SDL_EnclosePoints ((SDL_Point *)pointer, 1, (SDL_Rect *)self, NULL);
if (contains) {
if (pointer->pressed) {
game_button_do_callback (self, "clicked");
return true;
}
}
self->is_clicked = false;
return false;
}
bool game_button_check_hover (GameButton * self, MousePointer * pointer) {
SDL_bool contains = SDL_EnclosePoints ((SDL_Point *)pointer, 1, (SDL_Rect *)self, NULL);
if (contains) {
if (!self->is_clicked ) {
game_button_do_callback (self, "hover");
}
self->is_hover = true;
self->is_stop_hover = false;
return true;
} else if (self->is_hover) {
game_button_do_callback (self, "stop_hover");
self->is_stop_hover = true;
}
self->is_hover = false;
return false;
}
void game_button_set_callback (GameButton * self,
const char * name,
BtnCallback callback,
void * data) {
if (!strcmp(name, "clicked")) {
self->clicked = callback;
self->clicked_data = data;
} else if (!strcmp(name, "hover")) {
self->hover = callback;
self->hover_data = data;
} else if (!strcmp(name, "stop_hover") && self->hover) {
self->stop_hover = callback;
self->stop_hover_data = data;
} else {
fprintf (stderr,
"GameButton: Can not set callback \"%s\", it does not exist!\n",
name);
return;
}
return;
}
void game_button_do_callback (GameButton * self,
const char * name) {
BtnCallback callback;
if (!strcmp(name, "clicked") && self->clicked) {
if (self->is_clicked) {
return;
}
callback = self->clicked;
callback (self, self->clicked_data);
} else if (!strcmp(name, "hover") && self->hover) {
if (self->is_hover) {
return;
}
callback = self->hover;
callback (self, self->hover_data);
} else if (!strcmp(name, "stop_hover") && self->stop_hover) {
if (self->is_stop_hover) {
return;
}
callback = self->stop_hover;
callback (self, self->stop_hover_data);
} else {
fprintf (stderr,
"GameButton: Can not do callback \"%s\", it does not exist, or is undefined!\n",
name);
return;
}
self->is_clicked = true;
return;
}
void game_button_draw (GameButton * self, SDL_Renderer * renderer) {
SDL_Color oc = game_color_set_draw_color (renderer, self->colour);
if (self->fill) {
SDL_RenderFillRect (renderer , (SDL_Rect *) self);
} else {
SDL_RenderDrawRect (renderer , (SDL_Rect *) self);
}
game_color_set_draw_color (renderer, oc); /* reset the colour */
}
void game_button_set_fill (GameButton * self, bool fill) {
self->fill = fill;
}
void game_button_free (GameButton * self) {
fprintf (stdout, "Freeing GameButton: address: %ld\n", (long) self);
free (self);
}
|