1 | #pragma once |
---|---|
2 | #include <functional> |
3 | #include <SDL2/SDL.h> |
4 | #include <string> |
5 | #include <vector> |
6 | #include "gui.hpp" |
7 | |
8 | namespace GUI { |
9 | |
10 | |
11 | class Entry |
12 | { |
13 | std::string label; |
14 | std::function<void()> callback; |
15 | |
16 | bool selected = false; |
17 | SDL_Texture* whiteTexture = nullptr; |
18 | SDL_Texture* redTexture = nullptr; |
19 | |
20 | public: |
21 | Entry(std::string label, std::function<void()> callback = []{}); |
22 | ~Entry(); |
23 | |
24 | void set_label(std::string label); |
25 | inline std::string& get_label() { return label; } |
26 | |
27 | virtual void select() { selected = true; }; |
28 | virtual void unselect() { selected = false; }; |
29 | void trigger() { callback(); }; |
30 | virtual void render(int x, int y); |
31 | }; |
32 | |
33 | class ControlEntry : public Entry |
34 | { |
35 | SDL_Scancode* key; |
36 | int* button; |
37 | Entry* keyEntry; |
38 | |
39 | public: |
40 | ControlEntry(std::string action, SDL_Scancode* key); |
41 | ControlEntry(std::string action, int* button); |
42 | void select() { Entry::select(); keyEntry->select(); } |
43 | void unselect() { Entry::unselect(); keyEntry->unselect(); } |
44 | void render(int x, int y) { Entry::render(x, y); keyEntry->render(TEXT_RIGHT, y); } |
45 | }; |
46 | |
47 | class Menu |
48 | { |
49 | const int MAX_ENTRY = GUI::HEIGHT / FONT_SZ - 2; |
50 | int cursor = 0; |
51 | int top = 0; |
52 | int bottom = MAX_ENTRY; |
53 | |
54 | public: |
55 | std::vector<Entry*> entries; |
56 | Entry* errorMessage = nullptr; |
57 | |
58 | void add(Entry* entry); |
59 | void clear(); |
60 | void clear_error(); |
61 | void sort_by_label(); |
62 | void update(u8 const* keys); |
63 | void render(); |
64 | }; |
65 | |
66 | class FileMenu : public Menu |
67 | { |
68 | void change_dir(std::string dir); |
69 | void load_rom(std::string path); |
70 | |
71 | public: |
72 | FileMenu(); |
73 | }; |
74 | |
75 | |
76 | } |
77 |