Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (5.42 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
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
static int highlighted = -1;
103
104
int (menu_update_state)(menu_t *menu, int click) {
105
106
    if (rectangle_collide_point(menu->r[0], *get_mouse_X(), *get_mouse_Y())) {
107
        highlighted = GAME;
108
        if (click) return GAME;
109
    } else if (rectangle_collide_point(menu->r[1], *get_mouse_X(), *get_mouse_Y())) {
110
        highlighted = TEST;
111
        if (click) return TEST;
112
    } else if (rectangle_collide_point(menu->r[2], *get_mouse_X(), *get_mouse_Y())) {
113
        highlighted = EXIT;
114
        if (click) return EXIT;
115
    } else {
116
        highlighted = -1;
117
    }
118
    return MENU;
119
}
120
121
void (menu_draw)(menu_t *menu) {
122
    rectangle_draw(menu->frame);
123
    switch (highlighted) {
124
    case GAME:
125
        rectangle_set_fill_color(menu->r[0], HIGHLIGHT_COLOR);
126
        rectangle_draw(menu->r[0]);
127
        rectangle_draw(menu->r[1]);
128
        rectangle_draw(menu->r[2]);
129
        rectangle_set_fill_color(menu->r[0], GRAPH_BLACK);
130
        break;
131
    case TEST:
132
        rectangle_set_fill_color(menu->r[1], HIGHLIGHT_COLOR);
133
        rectangle_draw(menu->r[0]);
134
        rectangle_draw(menu->r[1]);
135
        rectangle_draw(menu->r[2]);
136
        rectangle_set_fill_color(menu->r[1], GRAPH_BLACK);
137
        break;
138
    case EXIT:
139
        rectangle_set_fill_color(menu->r[2], HIGHLIGHT_COLOR);
140
        rectangle_draw(menu->r[0]);
141
        rectangle_draw(menu->r[1]);
142
        rectangle_draw(menu->r[2]);
143
        rectangle_set_fill_color(menu->r[2], GRAPH_BLACK);
144
        break;
145
    default:
146
        rectangle_draw(menu->r[0]);
147
        rectangle_draw(menu->r[1]);
148
        rectangle_draw(menu->r[2]);
149
        break;
150
    }
151
    for(size_t i = 0; i < menu->sz; ++i)
152
        text_draw(menu->t[i]);
153
}
154
155
void (menu_dtor)(menu_t *p){
156
    for(size_t i = 0; i < p->sz; ++i){
157
        rectangle_dtor(p->r[i]);
158
        text_dtor     (p->t[i]);
159
    }
160
    rectangle_dtor(p->frame);
161
    free(p);
162
}