root / proj / libs / graph / src / menu.c @ 292
History | View | Annotate | Download (5.39 KB)
1 |
#include <lcom/lcf.h> |
---|---|
2 |
|
3 |
#include "menu.h" |
4 |
|
5 |
#include "graph.h" |
6 |
#include "rectangle.h" |
7 |
#include "proj_func.h" |
8 |
#include "proj_macros.h" |
9 |
|
10 |
struct menu {
|
11 |
size_t sz; |
12 |
rectangle_t **r; |
13 |
text_t **t; |
14 |
rectangle_t *frame; |
15 |
}; |
16 |
|
17 |
menu_t* (menu_ctor)(const font_t *fnt){
|
18 |
if(fnt == NULL) return NULL; |
19 |
menu_t *ret = (menu_t*)malloc(sizeof(menu_t));
|
20 |
if (ret == NULL) return NULL; |
21 |
ret->sz = 0;
|
22 |
ret->r = NULL;
|
23 |
ret->t = NULL;
|
24 |
|
25 |
ret->frame = rectangle_ctor(0, 0, 800, 500); |
26 |
rectangle_set_fill_color(ret->frame, GRAPH_BLACK); |
27 |
rectangle_set_outline_width(ret->frame, 6);
|
28 |
rectangle_set_outline_color(ret->frame, GRAPH_WHITE); |
29 |
rectangle_set_fill_trans(ret->frame, GRAPH_TRANSPARENT); |
30 |
|
31 |
|
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"); |
41 |
|
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);
|
50 |
|
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);
|
55 |
|
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);
|
60 |
|
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);
|
64 |
|
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;
|
100 |
} |
101 |
|
102 |
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;
|
117 |
} |
118 |
|
119 |
void (menu_draw)(menu_t *menu) {
|
120 |
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;
|
148 |
} |
149 |
for(size_t i = 0; i < menu->sz; ++i) |
150 |
text_draw(menu->t[i]); |
151 |
} |
152 |
|
153 |
void (menu_dtor)(menu_t *p){
|
154 |
for(size_t i = 0; i < p->sz; ++i){ |
155 |
rectangle_dtor(p->r[i]); |
156 |
text_dtor (p->t[i]); |
157 |
} |
158 |
rectangle_dtor(p->frame); |
159 |
free(p); |
160 |
} |