-
Notifications
You must be signed in to change notification settings - Fork 2
/
sdl_file_chooser.cpp
177 lines (135 loc) · 4.68 KB
/
sdl_file_chooser.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "sdl_file_chooser.h"
#include <cmath>
#include <stdint.h>
void FileChooser::getFileList(std::string directory)
{
auto files{std::filesystem::recursive_directory_iterator{
directory,
std::filesystem::directory_options::skip_permission_denied}};
for (auto &file : files)
{
if (std::filesystem::is_regular_file(file))
fileList.push_back(file.path().c_str());
}
std::sort(fileList.begin(), fileList.end());
}
void FileChooser::drawTitle(const std::string &title)
{
SDL_Surface *textSurface = TTF_RenderText_Solid(font, title.c_str(), {255, 255, 255, 255});
SDL_Rect sourceRect{0, 0, textSurface->w, textSurface->h};
SDL_Rect targetRect{10, 10, textSurface->w / 3, textSurface->h / 3};
SDL_Texture* textTexture{SDL_CreateTextureFromSurface(renderer, textSurface)};
SDL_RenderCopy(renderer, textTexture, &sourceRect, &targetRect);
SDL_DestroyTexture(textTexture);
SDL_FreeSurface(textSurface);
}
void FileChooser::drawFileList()
{
for (int i{}; i < static_cast<int>(fileList.size()); ++i)
{
int y{500-chosenFileI*30+i*30};
if (y < 1000 && y > 0)
{
SDL_Surface *textSurface = TTF_RenderText_Blended(
font,
fileList[i].c_str(),
{255, 255, 255, static_cast<uint8_t>(255-abs(500-y)/2)});
SDL_Rect sourceRect{0, 0, textSurface->w, textSurface->h};
SDL_Rect targetRect{0, y, textSurface->w / 5, textSurface->h / 5};
SDL_Texture* textTexture{SDL_CreateTextureFromSurface(renderer, textSurface)};
SDL_RenderCopy(renderer, textTexture, &sourceRect, &targetRect);
SDL_DestroyTexture(textTexture);
SDL_FreeSurface(textSurface);
}
}
}
void FileChooser::drawSelector()
{
SDL_SetRenderDrawColor(renderer, 100, 100, 100, 100);
SDL_Rect selectorRect{0, 500, 800, 25};
SDL_RenderFillRect(renderer, &selectorRect);
}
FileChooser::FileChooser(std::string directory)
{
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 1000, SDL_WINDOW_ALLOW_HIGHDPI);
if (!window)
{
std::cerr << "Unable to create window" << '\n';
std::exit(2);
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer)
{
std::cerr << "Unable to create renderer" << '\n';
std::exit(2);
}
font = TTF_OpenFont("./Anonymous_Pro.ttf", 100);
if (!font)
{
std::cerr << "Unable to open font file." << '\n';
std::exit(2);
}
drawTitle("Loading...");
SDL_RenderPresent(renderer);
getFileList(directory);
bool isRunning{true};
while (isRunning)
{
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_QUIT:
chosenFileI = -1;
isRunning = false;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
chosenFileI = -1;
isRunning = false;
break;
case SDLK_DOWN:
chosenFileI += 1;
if (chosenFileI > static_cast<int>(fileList.size())-1)
chosenFileI = fileList.size()-1;
break;
case SDLK_UP:
chosenFileI -= 1;
if (chosenFileI < 0)
chosenFileI = 0;
break;
case SDLK_RETURN:
isRunning = false;
deinit();
return;
}
break;
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
drawSelector();
drawTitle(FILECHOOSER_TITLE);
drawFileList();
SDL_RenderPresent(renderer);
SDL_Delay(20);
}
deinit();
}
std::string FileChooser::get()
{
if (chosenFileI != -1)
return fileList.at(chosenFileI);
return ""; // return an empty string if the user closed the window
}
void FileChooser::deinit()
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
}