root / proj / libs / graph / src / menu.c @ 342
History | View | Annotate | Download (3.12 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 |
#include "errors.h" |
10 |
|
11 |
static const int W = 400; |
12 |
static const int H = 60; |
13 |
static const int W1 = 40; |
14 |
static const int H1 = 40; |
15 |
static const int H2 = 20; |
16 |
|
17 |
struct menu {
|
18 |
const font_t *fnt;
|
19 |
size_t sz; |
20 |
rectangle_t **r; |
21 |
text_t **t; |
22 |
rectangle_t *frame; |
23 |
}; |
24 |
|
25 |
static int menu_recalculate(menu_t *menu){ |
26 |
const uint16_t Wtotal = 2*W1+W; |
27 |
const uint16_t Htotal = (uint16_t)(2*H1+menu->sz*H+(menu->sz-1)*H2); |
28 |
int16_t x = graph_get_XRes()/2-Wtotal/2; |
29 |
int16_t y = graph_get_YRes()/2-Htotal/2; |
30 |
rectangle_set_pos (menu->frame, x, y); |
31 |
rectangle_set_size(menu->frame, Wtotal, Htotal); |
32 |
x += W1; y += H1; |
33 |
for(size_t i = 0; i < menu->sz; ++i, y+=H+H2){ |
34 |
rectangle_set_pos(menu->r[i], x, y); |
35 |
text_set_pos (menu->t[i], x+W/2, y+H/2); |
36 |
} |
37 |
return SUCCESS;
|
38 |
} |
39 |
|
40 |
menu_t* (menu_ctor)(const font_t *fnt){
|
41 |
if(fnt == NULL) return NULL; |
42 |
menu_t *ret = (menu_t*)malloc(sizeof(menu_t));
|
43 |
if (ret == NULL) return NULL; |
44 |
ret->fnt = fnt; |
45 |
ret->sz = 0;
|
46 |
ret->r = NULL;
|
47 |
ret->t = NULL;
|
48 |
|
49 |
ret->frame = rectangle_ctor(0, 0, 0, 0); |
50 |
rectangle_set_fill_color(ret->frame, GRAPH_BLACK); |
51 |
rectangle_set_outline_width(ret->frame, 6);
|
52 |
rectangle_set_outline_color(ret->frame, GRAPH_WHITE); |
53 |
rectangle_set_fill_trans(ret->frame, GRAPH_TRANSPARENT); |
54 |
|
55 |
menu_recalculate(ret); |
56 |
|
57 |
return ret;
|
58 |
} |
59 |
|
60 |
int (menu_add_item)(menu_t *menu, const char *s){ |
61 |
rectangle_t *r = rectangle_ctor(0, 0, W, H); |
62 |
text_t *t = text_ctor(menu->fnt, s); |
63 |
if(r == NULL || t == NULL){ |
64 |
rectangle_dtor(r); |
65 |
text_dtor(t); |
66 |
return NULL_PTR;
|
67 |
} |
68 |
rectangle_set_fill_color (r, GRAPH_BLACK); |
69 |
rectangle_set_outline_width(r, 2);
|
70 |
rectangle_set_outline_color(r, GRAPH_WHITE); |
71 |
|
72 |
text_set_valign(t, text_valign_center); |
73 |
text_set_halign(t, text_halign_center); |
74 |
text_set_color (t, TEXT_COLOR); |
75 |
|
76 |
++menu->sz; |
77 |
menu->r = realloc(menu->r, menu->sz*sizeof(rectangle_t*));
|
78 |
menu->t = realloc(menu->t, menu->sz*sizeof(text_t *));
|
79 |
menu->r[menu->sz-1] = r;
|
80 |
menu->t[menu->sz-1] = t;
|
81 |
|
82 |
return menu_recalculate(menu);
|
83 |
} |
84 |
|
85 |
int (menu_update_state)(menu_t *menu, int click) { |
86 |
if(!click) return -1; |
87 |
for(size_t i = 0; i < menu->sz; ++i) |
88 |
if(rectangle_collide_point(menu->r[i], *get_mouse_X(), *get_mouse_Y()))
|
89 |
return (int)i; |
90 |
return -1; |
91 |
} |
92 |
|
93 |
void (menu_draw)(menu_t *menu) {
|
94 |
rectangle_draw(menu->frame); |
95 |
int x = *get_mouse_X(), y = *get_mouse_Y();
|
96 |
for(size_t i = 0; i < menu->sz; ++i){ |
97 |
if(rectangle_collide_point(menu->r[i], x, y))
|
98 |
rectangle_set_fill_color(menu->r[i], HIGHLIGHT_COLOR); |
99 |
rectangle_draw(menu->r[i]); |
100 |
text_draw (menu->t[i]); |
101 |
} |
102 |
for(size_t i = 0; i < menu->sz; ++i) |
103 |
if(rectangle_collide_point(menu->r[i], x, y))
|
104 |
rectangle_set_fill_color(menu->r[i], GRAPH_BLACK); |
105 |
} |
106 |
|
107 |
void (menu_dtor)(menu_t *p){
|
108 |
for(size_t i = 0; i < p->sz; ++i){ |
109 |
rectangle_dtor(p->r[i]); |
110 |
text_dtor (p->t[i]); |
111 |
} |
112 |
rectangle_dtor(p->frame); |
113 |
free(p); |
114 |
} |