1
#include "GameButton.h"
2
#include "game_utils.h"
4
GameButton * game_button_new (int x, int y, int h, int w) {
5
GameButton * self = malloc (sizeof(GameButton));
6
fprintf (stdout, "Creating GameButton: address: %ld\n", (long) self);
7
SDL_Rect * base = (SDL_Rect *) self;
13
self->is_clicked = false;
16
self->is_hover = false;
17
self->is_stop_hover = false;
18
self->colour = game_color_create_color (0,0,255,255);
23
bool game_button_envelopes_SDL_Point (GameButton *self, SDL_Point * point) {
24
if (SDL_EnclosePoints (point, 1, (SDL_Rect *)self, NULL)) {
31
bool game_button_check_clicked (GameButton * self, MousePointer * pointer) {
32
SDL_bool contains = SDL_EnclosePoints ((SDL_Point *)pointer, 1, (SDL_Rect *)self, NULL);
34
if (pointer->pressed) {
35
game_button_do_callback (self, "clicked");
40
self->is_clicked = false;
44
bool game_button_check_hover (GameButton * self, MousePointer * pointer) {
45
SDL_bool contains = SDL_EnclosePoints ((SDL_Point *)pointer, 1, (SDL_Rect *)self, NULL);
47
if (!self->is_clicked ) {
48
game_button_do_callback (self, "hover");
50
self->is_hover = true;
51
self->is_stop_hover = false;
53
} else if (self->is_hover) {
54
game_button_do_callback (self, "stop_hover");
55
self->is_stop_hover = true;
57
self->is_hover = false;
61
void game_button_set_callback (GameButton * self,
65
if (!strcmp(name, "clicked")) {
66
self->clicked = callback;
67
self->clicked_data = data;
68
} else if (!strcmp(name, "hover")) {
69
self->hover = callback;
70
self->hover_data = data;
71
} else if (!strcmp(name, "stop_hover") && self->hover) {
72
self->stop_hover = callback;
73
self->stop_hover_data = data;
76
"GameButton: Can not set callback \"%s\", it does not exist!\n",
84
void game_button_do_callback (GameButton * self,
87
if (!strcmp(name, "clicked") && self->clicked) {
88
if (self->is_clicked) {
91
callback = self->clicked;
92
callback (self, self->clicked_data);
93
} else if (!strcmp(name, "hover") && self->hover) {
97
callback = self->hover;
98
callback (self, self->hover_data);
99
} else if (!strcmp(name, "stop_hover") && self->stop_hover) {
100
if (self->is_stop_hover) {
103
callback = self->stop_hover;
104
callback (self, self->stop_hover_data);
107
"GameButton: Can not do callback \"%s\", it does not exist, or is undefined!\n",
111
self->is_clicked = true;
115
void game_button_draw (GameButton * self, SDL_Renderer * renderer) {
116
SDL_Color oc = game_color_set_draw_color (renderer, self->colour);
118
SDL_RenderFillRect (renderer , (SDL_Rect *) self);
120
SDL_RenderDrawRect (renderer , (SDL_Rect *) self);
122
game_color_set_draw_color (renderer, oc); /* reset the colour */
125
void game_button_set_fill (GameButton * self, bool fill) {
129
void game_button_free (GameButton * self) {
130
fprintf (stdout, "Freeing GameButton: address: %ld\n", (long) self);