generated from 32blit/32blit-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.cpp
49 lines (37 loc) · 1.36 KB
/
menu.cpp
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
#include "menu.hpp"
#include "engine/engine.hpp"
Menu::Menu(std::string_view title, std::vector<Item> items, const blit::Font &font) : blit::Menu(title, nullptr, 0, font), items_vec(std::move(items)) {
this->items = items_vec.data();
num_items = items_vec.size();
item_h = font.char_h + 10;
item_padding_x = 9;
item_adjust_y = 0;
header_h = title.empty() ? 0 : item_h;
footer_h = 0;
margin_y = 0;
background_colour = blit::Pen(0x11, 0x11, 0x11);
foreground_colour = blit::Pen(0xF7, 0xF7, 0xF7);
selected_item_background = blit::Pen(0x22, 0x22, 0x22);
}
void Menu::add_item(Item &&item) {
items_vec.emplace_back(std::move(item));
items = items_vec.data();
num_items = items_vec.size();
}
void Menu::set_on_item_activated(std::function<void(const Item &)> func) {
on_item_pressed = func;
}
void Menu::render_item(const Item &item, int y, int index) const {
blit::Menu::render_item(item, y, index);
if(index == current_item) {
blit::screen.pen = foreground_colour;
int x = display_rect.x + 2;
int size = item_h / 3;
int pointer_y = y + (item_h - size) / 2;
blit::screen.triangle({x, pointer_y}, {x + size / 2, pointer_y + size / 2}, {x, pointer_y + size});
}
}
void Menu::item_activated(const Item &item) {
if(on_item_pressed)
on_item_pressed(item);
}