Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (3.13 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
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
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
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
    ret->fnt = fnt;
30
    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
    menu_add_item(ret, "Play");
41
    menu_add_item(ret, "Test");
42
    menu_add_item(ret, "Exit");
43

    
44
    return ret;
45
}
46

    
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
}
61

    
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);
73

    
74
    text_set_valign(t, text_valign_center);
75
    text_set_halign(t, text_halign_center);
76
    text_set_color (t, TEXT_COLOR);
77

    
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;
83

    
84
    return menu_recalculate(menu);
85
}
86

    
87
int (menu_update_state)(menu_t *menu, int click) {
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;
93
}
94

    
95
void (menu_draw)(menu_t *menu) {
96
    rectangle_draw(menu->frame);
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]);
103
    }
104
    for(size_t i = 0; i < menu->sz; ++i)
105
        if(rectangle_collide_point(menu->r[i], x, y))
106
            rectangle_set_fill_color(menu->r[i], GRAPH_BLACK);
107
}
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
}