Revision 293
generalized menu
menu.c | ||
---|---|---|
6 | 6 |
#include "rectangle.h" |
7 | 7 |
#include "proj_func.h" |
8 | 8 |
#include "proj_macros.h" |
9 |
#include "errors.h" |
|
9 | 10 |
|
11 |
const int W = 400; |
|
12 |
const int H = 60; |
|
13 |
const int W1 = 40; |
|
14 |
const int H1 = 40; |
|
15 |
const int H2 = 20; |
|
16 |
|
|
10 | 17 |
struct menu { |
18 |
const font_t *fnt; |
|
11 | 19 |
size_t sz; |
12 | 20 |
rectangle_t **r; |
13 | 21 |
text_t **t; |
... | ... | |
18 | 26 |
if(fnt == NULL) return NULL; |
19 | 27 |
menu_t *ret = (menu_t*)malloc(sizeof(menu_t)); |
20 | 28 |
if (ret == NULL) return NULL; |
29 |
ret->fnt = fnt; |
|
21 | 30 |
ret->sz = 0; |
22 | 31 |
ret->r = NULL; |
23 | 32 |
ret->t = NULL; |
... | ... | |
28 | 37 |
rectangle_set_outline_color(ret->frame, GRAPH_WHITE); |
29 | 38 |
rectangle_set_fill_trans(ret->frame, GRAPH_TRANSPARENT); |
30 | 39 |
|
40 |
menu_add_item(ret, "Play"); |
|
41 |
menu_add_item(ret, "Test"); |
|
42 |
menu_add_item(ret, "Exit"); |
|
31 | 43 |
|
32 |
ret->sz = 3; |
|
33 |
ret->r = realloc(ret->r, ret->sz*sizeof(rectangle_t*)); |
|
34 |
ret->t = realloc(ret->t, ret->sz*sizeof(text_t *)); |
|
35 |
ret->r[0] = rectangle_ctor(0, 0, 400, 100); |
|
36 |
ret->r[1] = rectangle_ctor(0, 0, 400, 100); |
|
37 |
ret->r[2] = rectangle_ctor(0, 0, 400, 100); |
|
38 |
ret->t[0] = text_ctor(fnt, "Play"); |
|
39 |
ret->t[1] = text_ctor(fnt, "Test"); |
|
40 |
ret->t[2] = text_ctor(fnt, "Exit"); |
|
44 |
return ret; |
|
45 |
} |
|
41 | 46 |
|
42 |
if (ret->r[0] == NULL || ret->r[1] == NULL || ret->r[2] == NULL || |
|
43 |
ret->t[0] == NULL || ret->t[1] == NULL || ret->t[2] == NULL || |
|
44 |
ret->frame == NULL) return NULL; |
|
45 |
// VISUAL |
|
46 |
rectangle_set_fill_color(ret->r[0], GRAPH_BLACK); |
|
47 |
rectangle_set_outline_width(ret->r[0], 2); |
|
48 |
rectangle_set_outline_color(ret->r[0], GRAPH_WHITE); |
|
49 |
rectangle_set_fill_trans(ret->r[0], GRAPH_TRANSPARENT); |
|
47 |
static int menu_recalculate(menu_t *menu){ |
|
48 |
const int Wtotal = 2*W1+W; |
|
49 |
const int Htotal = 2*H1+menu->sz*H+(menu->sz-1)*H2; |
|
50 |
int x = graph_get_XRes()/2-Wtotal/2; |
|
51 |
int y = graph_get_YRes()/2-Htotal/2; |
|
52 |
rectangle_set_pos (menu->frame, x, y); |
|
53 |
rectangle_set_size(menu->frame, Wtotal, Htotal); |
|
54 |
x += W1; y += H1; |
|
55 |
for(size_t i = 0; i < menu->sz; ++i, y+=H+H2){ |
|
56 |
rectangle_set_pos(menu->r[i], x, y); |
|
57 |
text_set_pos (menu->t[i], x+W/2, y+H/2); |
|
58 |
} |
|
59 |
return SUCCESS; |
|
60 |
} |
|
50 | 61 |
|
51 |
rectangle_set_fill_color(ret->r[1], GRAPH_BLACK); |
|
52 |
rectangle_set_outline_width(ret->r[1], 2); |
|
53 |
rectangle_set_outline_color(ret->r[1], GRAPH_WHITE); |
|
54 |
rectangle_set_fill_trans(ret->r[1], GRAPH_TRANSPARENT); |
|
62 |
int (menu_add_item)(menu_t *menu, const char *s){ |
|
63 |
rectangle_t *r = rectangle_ctor(0, 0, W, H); |
|
64 |
text_t *t = text_ctor(menu->fnt, s); |
|
65 |
if(r == NULL || t == NULL){ |
|
66 |
rectangle_dtor(r); |
|
67 |
text_dtor(t); |
|
68 |
return NULL_PTR; |
|
69 |
} |
|
70 |
rectangle_set_fill_color (r, GRAPH_BLACK); |
|
71 |
rectangle_set_outline_width(r, 2); |
|
72 |
rectangle_set_outline_color(r, GRAPH_WHITE); |
|
55 | 73 |
|
56 |
rectangle_set_fill_color(ret->r[2], GRAPH_BLACK); |
|
57 |
rectangle_set_outline_width(ret->r[2], 2); |
|
58 |
rectangle_set_outline_color(ret->r[2], GRAPH_WHITE); |
|
59 |
rectangle_set_fill_trans(ret->r[2], GRAPH_TRANSPARENT); |
|
74 |
text_set_valign(t, text_valign_center); |
|
75 |
text_set_halign(t, text_halign_center); |
|
76 |
text_set_color (t, TEXT_COLOR); |
|
60 | 77 |
|
61 |
text_set_valign(ret->t[0], text_valign_center); |
|
62 |
text_set_halign(ret->t[0], text_halign_center); |
|
63 |
text_set_color(ret->t[0], TEXT_COLOR); |
|
78 |
++menu->sz; |
|
79 |
menu->r = realloc(menu->r, menu->sz*sizeof(rectangle_t*)); |
|
80 |
menu->t = realloc(menu->t, menu->sz*sizeof(text_t *)); |
|
81 |
menu->r[menu->sz-1] = r; |
|
82 |
menu->t[menu->sz-1] = t; |
|
64 | 83 |
|
65 |
text_set_valign(ret->t[1], text_valign_center); |
|
66 |
text_set_halign(ret->t[1], text_halign_center); |
|
67 |
text_set_color(ret->t[1], TEXT_COLOR); |
|
68 |
|
|
69 |
text_set_valign(ret->t[2], text_valign_center); |
|
70 |
text_set_halign(ret->t[2], text_halign_center); |
|
71 |
text_set_color(ret->t[2], TEXT_COLOR); |
|
72 |
// POSITIONS |
|
73 |
rectangle_set_pos(ret->r[0], |
|
74 |
graph_get_XRes()/2 - rectangle_get_w(ret->r[0])/2, |
|
75 |
graph_get_YRes()*0.35 - rectangle_get_h(ret->r[0])/2); |
|
76 |
|
|
77 |
|
|
78 |
rectangle_set_pos(ret->r[1], |
|
79 |
graph_get_XRes()/2 - rectangle_get_w(ret->r[1])/2, |
|
80 |
graph_get_YRes()*0.5 - rectangle_get_h(ret->r[1])/2); |
|
81 |
|
|
82 |
|
|
83 |
rectangle_set_pos(ret->r[2], |
|
84 |
graph_get_XRes()/2 - rectangle_get_w(ret->r[2])/2, |
|
85 |
graph_get_YRes()*0.65 - rectangle_get_h(ret->r[2])/2); |
|
86 |
|
|
87 |
text_set_pos(ret->t[0], rectangle_get_x(ret->r[0])+rectangle_get_w(ret->r[0])/2, |
|
88 |
rectangle_get_y(ret->r[0])+rectangle_get_h(ret->r[0])/2); |
|
89 |
|
|
90 |
text_set_pos(ret->t[1], rectangle_get_x(ret->r[1])+rectangle_get_w(ret->r[1])/2, |
|
91 |
rectangle_get_y(ret->r[1])+rectangle_get_h(ret->r[1])/2); |
|
92 |
|
|
93 |
text_set_pos(ret->t[2], rectangle_get_x(ret->r[2])+rectangle_get_w(ret->r[2])/2, |
|
94 |
rectangle_get_y(ret->r[2])+rectangle_get_h(ret->r[2])/2); |
|
95 |
|
|
96 |
rectangle_set_pos(ret->frame, |
|
97 |
graph_get_XRes()/2 - rectangle_get_w(ret->frame)/2, |
|
98 |
graph_get_YRes()*0.5 - rectangle_get_h(ret->frame)/2); |
|
99 |
return ret; |
|
84 |
return menu_recalculate(menu); |
|
100 | 85 |
} |
101 | 86 |
|
102 | 87 |
int (menu_update_state)(menu_t *menu, int click) { |
103 |
|
|
104 |
if (rectangle_collide_point(menu->r[0], *get_mouse_X(), *get_mouse_Y())) { |
|
105 |
highlighted = GAME; |
|
106 |
if (click) return GAME; |
|
107 |
} else if (rectangle_collide_point(menu->r[1], *get_mouse_X(), *get_mouse_Y())) { |
|
108 |
highlighted = TEST; |
|
109 |
if (click) return TEST; |
|
110 |
} else if (rectangle_collide_point(menu->r[2], *get_mouse_X(), *get_mouse_Y())) { |
|
111 |
highlighted = EXIT; |
|
112 |
if (click) return EXIT; |
|
113 |
} else { |
|
114 |
highlighted = -1; |
|
115 |
} |
|
116 |
return MENU; |
|
88 |
if(!click) return -1; |
|
89 |
for(size_t i = 0; i < menu->sz; ++i) |
|
90 |
if(rectangle_collide_point(menu->r[i], *get_mouse_X(), *get_mouse_Y())) |
|
91 |
return i; |
|
92 |
return -1; |
|
117 | 93 |
} |
118 | 94 |
|
119 | 95 |
void (menu_draw)(menu_t *menu) { |
120 | 96 |
rectangle_draw(menu->frame); |
121 |
switch (highlighted) { |
|
122 |
case GAME: |
|
123 |
rectangle_set_fill_color(menu->r[0], HIGHLIGHT_COLOR); |
|
124 |
rectangle_draw(menu->r[0]); |
|
125 |
rectangle_draw(menu->r[1]); |
|
126 |
rectangle_draw(menu->r[2]); |
|
127 |
rectangle_set_fill_color(menu->r[0], GRAPH_BLACK); |
|
128 |
break; |
|
129 |
case TEST: |
|
130 |
rectangle_set_fill_color(menu->r[1], HIGHLIGHT_COLOR); |
|
131 |
rectangle_draw(menu->r[0]); |
|
132 |
rectangle_draw(menu->r[1]); |
|
133 |
rectangle_draw(menu->r[2]); |
|
134 |
rectangle_set_fill_color(menu->r[1], GRAPH_BLACK); |
|
135 |
break; |
|
136 |
case EXIT: |
|
137 |
rectangle_set_fill_color(menu->r[2], HIGHLIGHT_COLOR); |
|
138 |
rectangle_draw(menu->r[0]); |
|
139 |
rectangle_draw(menu->r[1]); |
|
140 |
rectangle_draw(menu->r[2]); |
|
141 |
rectangle_set_fill_color(menu->r[2], GRAPH_BLACK); |
|
142 |
break; |
|
143 |
default: |
|
144 |
rectangle_draw(menu->r[0]); |
|
145 |
rectangle_draw(menu->r[1]); |
|
146 |
rectangle_draw(menu->r[2]); |
|
147 |
break; |
|
97 |
int x = *get_mouse_X(), y = *get_mouse_Y(); |
|
98 |
for(size_t i = 0; i < menu->sz; ++i){ |
|
99 |
if(rectangle_collide_point(menu->r[i], x, y)) |
|
100 |
rectangle_set_fill_color(menu->r[i], HIGHLIGHT_COLOR); |
|
101 |
rectangle_draw(menu->r[i]); |
|
102 |
text_draw (menu->t[i]); |
|
148 | 103 |
} |
149 | 104 |
for(size_t i = 0; i < menu->sz; ++i) |
150 |
text_draw(menu->t[i]); |
|
105 |
if(rectangle_collide_point(menu->r[i], x, y)) |
|
106 |
rectangle_set_fill_color(menu->r[i], GRAPH_BLACK); |
|
151 | 107 |
} |
152 | 108 |
|
153 | 109 |
void (menu_dtor)(menu_t *p){ |
Also available in: Unified diff