bzr branch
http://gegoxaren.bato24.eu/bzr/%2Bjunk/c_sdl_joypad
21
by Gustav Hatvigsson
* added Modeline to (almost) all files. |
1 |
/* c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil
|
2 |
* vi: set shiftwidth=2 tabstop=2 expandtab:
|
|
3 |
* :indentSize=2:tabSize=2:noTabs=true:
|
|
4 |
*/
|
|
5 |
||
1
by Gustav Hartvigsson
Initial code. |
6 |
#include "GameButton.h" |
7 |
#include "game_utils.h" |
|
8 |
||
7
by Gustav Hartvigsson
* Added licensing information to the files. |
9 |
/**
|
10 |
* This file, as the rest of the project is under MIT license.
|
|
11 |
* see http://opensource.org/licenses/MIT
|
|
12 |
*
|
|
8
by Gustav Hartvigsson
* added and changed little in the files Lincening information. |
13 |
* Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
|
7
by Gustav Hartvigsson
* Added licensing information to the files. |
14 |
*/
|
15 |
||
1
by Gustav Hartvigsson
Initial code. |
16 |
GameButton * game_button_new (int x, int y, int h, int w) { |
17 |
GameButton * self = malloc (sizeof(GameButton)); |
|
18 |
fprintf (stdout, "Creating GameButton: address: %ld\n", (long) self); |
|
19 |
SDL_Rect * base = (SDL_Rect *) self; |
|
20 |
base->x = x; |
|
21 |
base->y = y; |
|
22 |
base->h = h; |
|
23 |
base->w = w; |
|
24 |
self->clicked = NULL; |
|
25 |
self->is_clicked = false; |
|
26 |
self->fill = false; |
|
27 |
self->hover = NULL; |
|
28 |
self->is_hover = false; |
|
29 |
self->is_stop_hover = false; |
|
30 |
self->colour = game_color_create_color (0,0,255,255); |
|
31 |
return self; |
|
32 |
}
|
|
33 |
||
34 |
||
35 |
bool game_button_envelopes_SDL_Point (GameButton *self, SDL_Point * point) { |
|
36 |
if (SDL_EnclosePoints (point, 1, (SDL_Rect *)self, NULL)) { |
|
37 |
return true; |
|
38 |
} else { |
|
39 |
return false; |
|
40 |
} |
|
41 |
}
|
|
42 |
||
43 |
bool game_button_check_clicked (GameButton * self, MousePointer * pointer) { |
|
44 |
SDL_bool contains = SDL_EnclosePoints ((SDL_Point *)pointer, 1, (SDL_Rect *)self, NULL); |
|
45 |
if (contains) { |
|
46 |
if (pointer->pressed) { |
|
47 |
game_button_do_callback (self, "clicked"); |
|
48 |
return true; |
|
49 |
} |
|
50 |
|
|
51 |
} |
|
52 |
self->is_clicked = false; |
|
53 |
return false; |
|
54 |
}
|
|
55 |
||
56 |
bool game_button_check_hover (GameButton * self, MousePointer * pointer) { |
|
57 |
SDL_bool contains = SDL_EnclosePoints ((SDL_Point *)pointer, 1, (SDL_Rect *)self, NULL); |
|
58 |
if (contains) { |
|
59 |
if (!self->is_clicked ) { |
|
60 |
game_button_do_callback (self, "hover"); |
|
61 |
} |
|
62 |
self->is_hover = true; |
|
63 |
self->is_stop_hover = false; |
|
64 |
return true; |
|
65 |
} else if (self->is_hover) { |
|
66 |
game_button_do_callback (self, "stop_hover"); |
|
67 |
self->is_stop_hover = true; |
|
68 |
} |
|
69 |
self->is_hover = false; |
|
70 |
return false; |
|
71 |
}
|
|
72 |
||
73 |
void game_button_set_callback (GameButton * self, |
|
74 |
const char * name, |
|
75 |
BtnCallback callback, |
|
76 |
void * data) { |
|
77 |
if (!strcmp(name, "clicked")) { |
|
78 |
self->clicked = callback; |
|
79 |
self->clicked_data = data; |
|
80 |
} else if (!strcmp(name, "hover")) { |
|
81 |
self->hover = callback; |
|
82 |
self->hover_data = data; |
|
83 |
} else if (!strcmp(name, "stop_hover") && self->hover) { |
|
84 |
self->stop_hover = callback; |
|
85 |
self->stop_hover_data = data; |
|
86 |
} else { |
|
87 |
fprintf (stderr, |
|
88 |
"GameButton: Can not set callback \"%s\", it does not exist!\n", |
|
89 |
name); |
|
90 |
return; |
|
91 |
} |
|
92 |
return; |
|
93 |
}
|
|
94 |
||
95 |
||
96 |
void game_button_do_callback (GameButton * self, |
|
97 |
const char * name) { |
|
98 |
BtnCallback callback; |
|
99 |
if (!strcmp(name, "clicked") && self->clicked) { |
|
100 |
if (self->is_clicked) { |
|
101 |
return; |
|
102 |
} |
|
103 |
callback = self->clicked; |
|
104 |
callback (self, self->clicked_data); |
|
105 |
} else if (!strcmp(name, "hover") && self->hover) { |
|
106 |
if (self->is_hover) { |
|
107 |
return; |
|
108 |
} |
|
109 |
callback = self->hover; |
|
110 |
callback (self, self->hover_data); |
|
111 |
} else if (!strcmp(name, "stop_hover") && self->stop_hover) { |
|
112 |
if (self->is_stop_hover) { |
|
113 |
return; |
|
114 |
} |
|
115 |
callback = self->stop_hover; |
|
116 |
callback (self, self->stop_hover_data); |
|
117 |
} else { |
|
118 |
fprintf (stderr, |
|
119 |
"GameButton: Can not do callback \"%s\", it does not exist, or is undefined!\n", |
|
120 |
name); |
|
121 |
return; |
|
122 |
} |
|
123 |
self->is_clicked = true; |
|
124 |
return; |
|
125 |
}
|
|
126 |
||
127 |
void game_button_draw (GameButton * self, SDL_Renderer * renderer) { |
|
128 |
SDL_Color oc = game_color_set_draw_color (renderer, self->colour); |
|
129 |
if (self->fill) { |
|
130 |
SDL_RenderFillRect (renderer , (SDL_Rect *) self); |
|
131 |
} else { |
|
132 |
SDL_RenderDrawRect (renderer , (SDL_Rect *) self); |
|
133 |
} |
|
134 |
game_color_set_draw_color (renderer, oc); /* reset the colour */ |
|
135 |
}
|
|
136 |
||
137 |
void game_button_set_fill (GameButton * self, bool fill) { |
|
138 |
self->fill = fill; |
|
139 |
}
|
|
140 |
||
141 |
void game_button_free (GameButton * self) { |
|
142 |
fprintf (stdout, "Freeing GameButton: address: %ld\n", (long) self); |
|
143 |
free (self); |
|
144 |
}
|