-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.c
95 lines (81 loc) · 2.75 KB
/
menu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "menu.h"
#include "menu_element.h"
#include "menu_text.h"
#include "utils.h"
Menu *create_menu(SDL_Rect menuArea, Uint32 bgColor) {
Menu *menu = (Menu *) malloc(sizeof(Menu));
menu->area = menuArea;
set_color(bgColor, &menu->bgColor);
menu->textCount = 0;
menu->texts = NULL;
menu->elementCount = 0;
menu->elements = NULL;
menu->foundElement = NULL;
menu->selTextField = NULL;
return menu;
}
/*
* Kiszámolja a menüben levő menüelemek interact színéhez használt átlátszóságot.
* (Hover animáció)
*/
void calc_interact_alphas(Menu *menu) {
for (int i = 0; i < menu->elementCount; ++i) {
MenuElement *element = menu->elements[i];
if (element == menu->foundElement) {
if (element->interactAlpha < 120 && !element->selected) element->interactAlpha += 15;
} else {
if (element->interactAlpha > 0) element->interactAlpha -= 15;
}
}
}
void draw_menu(SDL_Renderer *renderer, Menu *menu) {
calc_interact_alphas(menu);
fill_rect(renderer, &menu->area, menu->bgColor);
Vector2s offset = {(short) menu->area.x, (short) menu->area.y};
for (int i = 0; i < menu->elementCount; ++i) {
draw_element(renderer, menu->elements[i], offset);
}
for (int i = 0; i < menu->textCount; ++i) {
draw_text(renderer, menu->texts[i], offset);
}
}
void free_menu(Menu *menu) {
for (int i = 0; i < menu->elementCount; ++i) {
if (menu->elements[i]->type == TEXTFIELD)
free(menu->elements[i]->text->text);
}
for (int i = 0; i < menu->textCount; ++i) {
free_text(menu->texts[i]);
}
for (int i = 0; i < menu->elementCount; ++i) {
free_element(menu->elements[i]);
}
free(menu->texts);
free(menu->elements);
free(menu);
}
void add_text(Menu *menu, Text *text) {
menu->texts = (Text **) realloc(menu->texts, sizeof(Text *) * (menu->textCount + 1));
menu->texts[menu->textCount++] = text;
}
void add_element(Menu *menu, MenuElement *element) {
menu->elements = (MenuElement **) realloc(menu->elements, sizeof(MenuElement *) * (menu->elementCount + 1));
menu->elements[menu->elementCount++] = element;
add_text(menu, element->text);
}
MenuElement *find_element(Menu *menu, SDL_Point point) {
for (int i = 0; i < menu->elementCount; ++i) {
if (inside_rect(&menu->elements[i]->area, &point, &menu->area)) {
menu->foundElement = menu->elements[i];
return menu->foundElement;
}
}
menu->foundElement = NULL;
return NULL;
}
MenuElement *search_element(Menu *menu, MenuAction action) {
for (int i = 0; i < menu->elementCount; ++i) {
if (menu->elements[i]->action == action) return menu->elements[i];
}
return NULL;
}