Project

General

Profile

Statistics
| Revision:

root / proj / libs / graph / src / menu.c @ 296

History | View | Annotate | Download (3.03 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
    return ret;
41
}
42 291 up20180642
43 293 up20180642
static int menu_recalculate(menu_t *menu){
44
    const int Wtotal = 2*W1+W;
45
    const int Htotal = 2*H1+menu->sz*H+(menu->sz-1)*H2;
46
    int x = graph_get_XRes()/2-Wtotal/2;
47
    int y = graph_get_YRes()/2-Htotal/2;
48
    rectangle_set_pos (menu->frame, x, y);
49
    rectangle_set_size(menu->frame, Wtotal, Htotal);
50
    x += W1; y += H1;
51
    for(size_t i = 0; i < menu->sz; ++i, y+=H+H2){
52
        rectangle_set_pos(menu->r[i], x, y);
53
        text_set_pos     (menu->t[i], x+W/2, y+H/2);
54
    }
55
    return SUCCESS;
56
}
57 291 up20180642
58 293 up20180642
int (menu_add_item)(menu_t *menu, const char *s){
59
    rectangle_t *r = rectangle_ctor(0, 0, W, H);
60
    text_t      *t = text_ctor(menu->fnt, s);
61
    if(r == NULL || t == NULL){
62
        rectangle_dtor(r);
63
        text_dtor(t);
64
        return NULL_PTR;
65
    }
66
    rectangle_set_fill_color   (r, GRAPH_BLACK);
67
    rectangle_set_outline_width(r, 2);
68
    rectangle_set_outline_color(r, GRAPH_WHITE);
69 291 up20180642
70 293 up20180642
    text_set_valign(t, text_valign_center);
71
    text_set_halign(t, text_halign_center);
72
    text_set_color (t, TEXT_COLOR);
73 291 up20180642
74 293 up20180642
    ++menu->sz;
75
    menu->r = realloc(menu->r, menu->sz*sizeof(rectangle_t*));
76
    menu->t = realloc(menu->t, menu->sz*sizeof(text_t     *));
77
    menu->r[menu->sz-1] = r;
78
    menu->t[menu->sz-1] = t;
79 291 up20180642
80 293 up20180642
    return menu_recalculate(menu);
81 291 up20180642
}
82
83
int (menu_update_state)(menu_t *menu, int click) {
84 293 up20180642
    if(!click) return -1;
85
    for(size_t i = 0; i < menu->sz; ++i)
86
        if(rectangle_collide_point(menu->r[i], *get_mouse_X(), *get_mouse_Y()))
87
            return i;
88
    return -1;
89 291 up20180642
}
90
91
void (menu_draw)(menu_t *menu) {
92
    rectangle_draw(menu->frame);
93 293 up20180642
    int x = *get_mouse_X(), y = *get_mouse_Y();
94
    for(size_t i = 0; i < menu->sz; ++i){
95
        if(rectangle_collide_point(menu->r[i], x, y))
96
            rectangle_set_fill_color(menu->r[i], HIGHLIGHT_COLOR);
97
        rectangle_draw(menu->r[i]);
98
        text_draw     (menu->t[i]);
99 291 up20180642
    }
100
    for(size_t i = 0; i < menu->sz; ++i)
101 293 up20180642
        if(rectangle_collide_point(menu->r[i], x, y))
102
            rectangle_set_fill_color(menu->r[i], GRAPH_BLACK);
103 291 up20180642
}
104
105
void (menu_dtor)(menu_t *p){
106
    for(size_t i = 0; i < p->sz; ++i){
107
        rectangle_dtor(p->r[i]);
108
        text_dtor     (p->t[i]);
109
    }
110
    rectangle_dtor(p->frame);
111
    free(p);
112
}