root / proj / libs / graph / src / menu.c @ 293
History | View | Annotate | Download (3.13 KB)
1 | 291 | up20180642 | #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 | 293 | up20180642 | #include "errors.h" |
10 | 291 | up20180642 | |
11 | 293 | up20180642 | 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 | |||
17 | 291 | up20180642 | struct menu {
|
18 | 293 | up20180642 | const font_t *fnt;
|
19 | 291 | up20180642 | size_t sz; |
20 | rectangle_t **r; |
||
21 | text_t **t; |
||
22 | rectangle_t *frame; |
||
23 | }; |
||
24 | |||
25 | menu_t* (menu_ctor)(const font_t *fnt){
|
||
26 | if(fnt == NULL) return NULL; |
||
27 | menu_t *ret = (menu_t*)malloc(sizeof(menu_t));
|
||
28 | if (ret == NULL) return NULL; |
||
29 | 293 | up20180642 | ret->fnt = fnt; |
30 | 291 | up20180642 | ret->sz = 0;
|
31 | ret->r = NULL;
|
||
32 | ret->t = NULL;
|
||
33 | |||
34 | ret->frame = rectangle_ctor(0, 0, 800, 500); |
||
35 | rectangle_set_fill_color(ret->frame, GRAPH_BLACK); |
||
36 | rectangle_set_outline_width(ret->frame, 6);
|
||
37 | rectangle_set_outline_color(ret->frame, GRAPH_WHITE); |
||
38 | rectangle_set_fill_trans(ret->frame, GRAPH_TRANSPARENT); |
||
39 | |||
40 | 293 | up20180642 | menu_add_item(ret, "Play");
|
41 | menu_add_item(ret, "Test");
|
||
42 | menu_add_item(ret, "Exit");
|
||
43 | 291 | up20180642 | |
44 | 293 | up20180642 | return ret;
|
45 | } |
||
46 | 291 | up20180642 | |
47 | 293 | up20180642 | 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 | } |
||
61 | 291 | up20180642 | |
62 | 293 | up20180642 | 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); |
||
73 | 291 | up20180642 | |
74 | 293 | up20180642 | text_set_valign(t, text_valign_center); |
75 | text_set_halign(t, text_halign_center); |
||
76 | text_set_color (t, TEXT_COLOR); |
||
77 | 291 | up20180642 | |
78 | 293 | up20180642 | ++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;
|
||
83 | 291 | up20180642 | |
84 | 293 | up20180642 | return menu_recalculate(menu);
|
85 | 291 | up20180642 | } |
86 | |||
87 | int (menu_update_state)(menu_t *menu, int click) { |
||
88 | 293 | up20180642 | 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; |
||
93 | 291 | up20180642 | } |
94 | |||
95 | void (menu_draw)(menu_t *menu) {
|
||
96 | rectangle_draw(menu->frame); |
||
97 | 293 | up20180642 | 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]); |
||
103 | 291 | up20180642 | } |
104 | for(size_t i = 0; i < menu->sz; ++i) |
||
105 | 293 | up20180642 | if(rectangle_collide_point(menu->r[i], x, y))
|
106 | rectangle_set_fill_color(menu->r[i], GRAPH_BLACK); |
||
107 | 291 | up20180642 | } |
108 | |||
109 | void (menu_dtor)(menu_t *p){
|
||
110 | for(size_t i = 0; i < p->sz; ++i){ |
||
111 | rectangle_dtor(p->r[i]); |
||
112 | text_dtor (p->t[i]); |
||
113 | } |
||
114 | rectangle_dtor(p->frame); |
||
115 | free(p); |
||
116 | } |