-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.cpp
More file actions
101 lines (76 loc) · 3.71 KB
/
menu.cpp
File metadata and controls
101 lines (76 loc) · 3.71 KB
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
96
97
98
99
#include "menu.h"
void draw_text(SDL_Renderer* renderer, TTF_Font* font, const char* text, int x, int y, SDL_Color color) {
SDL_Surface* surface = TTF_RenderText_Solid(font, text, color);
if(!surface) return;
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect rect = {x, y, surface->w, surface->h};
SDL_RenderCopy(renderer, texture, NULL, &rect);
SDL_FreeSurface(surface);
SDL_DestroyTexture(texture);
}
void draw_menu(SDL_Renderer* renderer, TTF_Font* font, int selected) {
SDL_SetRenderDrawColor(renderer, 30, 30, 40, 255);
SDL_RenderClear(renderer);
SDL_Color white = {255, 255, 255, 255};
SDL_Color yellow = {255, 255, 0, 255};
draw_text(renderer, font, "TILEMAP EDITOR", 220, 100, white);
draw_text(renderer, font, " View Map", 250, 200, selected == 0 ? yellow : white);
draw_text(renderer, font, " Edit Map", 250, 250, selected == 1 ? yellow : white);
draw_text(renderer, font, " Quit", 250, 300, selected == 2 ? yellow : white);
draw_text(renderer, font, "Use Arrow Keys to navigate", 200, 400, white);
draw_text(renderer, font, "Press ENTER to select", 200, 430, white);
}
void draw_file_select(SDL_Renderer* renderer, TTF_Font* font, std::vector<std::string>& files, int selected, int scroll_offset, bool is_edit) {
SDL_SetRenderDrawColor(renderer, 30, 30, 40, 255);
SDL_RenderClear(renderer);
SDL_Color white = {255, 255, 255, 255};
SDL_Color yellow = {255, 255, 0, 255};
SDL_Color gray = {150, 150, 150, 255};
const char* title = is_edit ? "SELECT MAP TO EDIT" : "SELECT MAP TO VIEW";
draw_text(renderer, font, title, 180, 30, white);
draw_text(renderer, font, "Use Arrow Keys to navigate", 160, 70, gray);
draw_text(renderer, font, "Press ENTER to select, ESC to cancel", 100, 450, white);
if(selected >= scroll_offset && selected < scroll_offset + 10) {
SDL_SetRenderDrawColor(renderer, 80, 80, 100, 255);
SDL_Rect highlight = {130, 110 + (selected - scroll_offset) * 30, 380, 28};
SDL_RenderFillRect(renderer, &highlight);
}
int display_count = (files.size() < 10) ? files.size() : 10;
for(int i = 0; i < display_count; i++) {
int file_index = scroll_offset + i;
if(file_index >= static_cast<int>(files.size())) break;
char buf[256];
if(selected == file_index) {
snprintf(buf, sizeof(buf), "> %s", files[file_index].c_str());
draw_text(renderer, font, buf, 140, 115 + i*30, yellow);
} else {
snprintf(buf, sizeof(buf), " %s", files[file_index].c_str());
draw_text(renderer, font, buf, 140, 115 + i*30, white);
}
}
if(files.size() > 10) {
char count_buf[64];
snprintf(count_buf, sizeof(count_buf), "Showing %d-%d of %zu files", scroll_offset + 1,
std::min(scroll_offset + 10, static_cast<int>(files.size())), files.size());
draw_text(renderer, font, count_buf, 140, 420, gray);
}
}
std::vector<std::string> scan_map_files() {
std::vector<std::string> map_files;
namespace fs = std::filesystem;
try {
for(const auto& entry : fs::directory_iterator(".")) {
if(entry.is_regular_file()) {
std::string filename = entry.path().filename().string();
if(filename.find("map") != std::string::npos &&
filename.find(".txt") != std::string::npos) {
map_files.push_back(filename);
}
}
}
std::sort(map_files.begin(), map_files.end());
} catch(...) {
std::printf("error reading files in directory ");
}
return map_files;
}