forked from jbanes/rs97-commander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanel.cpp
executable file
·339 lines (318 loc) · 9.58 KB
/
panel.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <iostream>
#include <sstream>
#include "panel.h"
#include "resourceManager.h"
#include "sdlutils.h"
#include "fileutils.h"
#define PANEL_SIZE 158
#define NAME_SIZE 140
CPanel::CPanel(const std::string &p_path, const Sint16 p_x):
m_currentPath(""),
m_camera(0),
m_x(p_x),
m_highlightedLine(0),
m_iconDir(CResourceManager::instance().getSurface(CResourceManager::T_SURFACE_FOLDER)),
m_iconFile(CResourceManager::instance().getSurface(CResourceManager::T_SURFACE_FILE)),
m_iconUp(CResourceManager::instance().getSurface(CResourceManager::T_SURFACE_UP)),
m_cursor1(CResourceManager::instance().getSurface(CResourceManager::T_SURFACE_CURSOR1)),
m_cursor2(CResourceManager::instance().getSurface(CResourceManager::T_SURFACE_CURSOR2)),
m_font(CResourceManager::instance().getFont())
{
// List the given path
if (m_fileLister.list(p_path))
{
// Path OK
m_currentPath = p_path;
}
else
{
// The path is wrong => take default
m_fileLister.list(PATH_DEFAULT);
m_currentPath = PATH_DEFAULT;
}
}
CPanel::~CPanel(void)
{
}
void CPanel::render(const bool p_active) const
{
// Draw cursor
SDL_utils::applySurface(m_x - 1, Y_LIST + (m_highlightedLine - m_camera) * LINE_HEIGHT, p_active ? m_cursor1 : m_cursor2, Globals::g_screen);
// Draw panel
const Sint16 l_x = m_x + m_iconDir->w + 2;
const unsigned int l_nbTotal = m_fileLister.getNbTotal();
Sint16 l_y = Y_LIST;
SDL_Surface *l_surfaceTmp = NULL;
const SDL_Color *l_color = NULL;
SDL_Rect l_rect;
// Current dir
l_surfaceTmp = SDL_utils::renderText(m_font, m_currentPath, Globals::g_colorTextTitle);
if (l_surfaceTmp->w > PANEL_SIZE)
{
l_rect.x = l_surfaceTmp->w - PANEL_SIZE;
l_rect.y = 0;
l_rect.w = PANEL_SIZE;
l_rect.h = l_surfaceTmp->h;
SDL_utils::applySurface(m_x, Y_HEADER, l_surfaceTmp, Globals::g_screen, &l_rect);
}
else
{
SDL_utils::applySurface(m_x, Y_HEADER, l_surfaceTmp, Globals::g_screen);
}
SDL_FreeSurface(l_surfaceTmp);
// Content
for (unsigned int l_i = m_camera; l_i < m_camera + NB_VISIBLE_LINES && l_i < l_nbTotal; ++l_i)
{
// Icon and color
if (m_fileLister.isDirectory(l_i))
{
// Icon
if (m_fileLister[l_i].m_name == "..")
l_surfaceTmp = m_iconUp;
else
l_surfaceTmp = m_iconDir;
// Color
if (m_selectList.find(l_i) != m_selectList.end())
l_color = &Globals::g_colorTextSelected;
else
l_color = &Globals::g_colorTextDir;
}
else
{
// Icon
l_surfaceTmp = m_iconFile;
// Color
if (m_selectList.find(l_i) != m_selectList.end())
l_color = &Globals::g_colorTextSelected;
else
l_color = &Globals::g_colorTextNormal;
}
SDL_utils::applySurface(m_x, l_y, l_surfaceTmp, Globals::g_screen);
// Text
l_surfaceTmp = SDL_utils::renderText(m_font, m_fileLister[l_i].m_name, *l_color);
if (l_surfaceTmp->w > NAME_SIZE)
{
l_rect.x = 0;
l_rect.y = 0;
l_rect.w = NAME_SIZE;
l_rect.h = l_surfaceTmp->h;
SDL_utils::applySurface(l_x, l_y + 2, l_surfaceTmp, Globals::g_screen, &l_rect);
}
else
{
SDL_utils::applySurface(l_x, l_y + 2, l_surfaceTmp, Globals::g_screen);
}
SDL_FreeSurface(l_surfaceTmp);
// Next line
l_y += LINE_HEIGHT;
}
// Footer
std::string l_footer("-");
if (!m_fileLister.isDirectory(m_highlightedLine))
{
std::ostringstream l_s;
l_s << m_fileLister[m_highlightedLine].m_size;
l_footer = l_s.str();
File_utils::formatSize(l_footer);
}
SDL_utils::applyText(m_x + 2, Y_FOOTER, Globals::g_screen, m_font, "Size:", Globals::g_colorTextTitle);
SDL_utils::applyText(m_x + PANEL_SIZE - 2, Y_FOOTER, Globals::g_screen, m_font, l_footer, Globals::g_colorTextTitle, SDL_utils::T_TEXT_ALIGN_RIGHT);
}
const bool CPanel::moveCursorUp(unsigned char p_step)
{
bool l_ret(false);
if (m_highlightedLine)
{
// Move cursor
if (m_highlightedLine > p_step)
m_highlightedLine -= p_step;
else
m_highlightedLine = 0;
// Adjust camera
adjustCamera();
// Return true for new render
l_ret = true;
}
return l_ret;
}
const bool CPanel::moveCursorDown(unsigned char p_step)
{
bool l_ret(false);
const unsigned int l_nb = m_fileLister.getNbTotal();
if (m_highlightedLine < l_nb - 1)
{
// Move cursor
if (m_highlightedLine + p_step > l_nb - 1)
m_highlightedLine = l_nb - 1;
else
m_highlightedLine += p_step;
// Adjust camera
adjustCamera();
// Return true for new render
l_ret = true;
}
return l_ret;
}
const bool CPanel::open(const std::string &p_path)
{
bool l_ret(false);
std::string l_newPath("");
std::string l_oldDir("");
if (p_path.empty())
{
// Open highlighted dir
if (m_fileLister[m_highlightedLine].m_name == "..")
{
// Go to parent dir
size_t l_pos = m_currentPath.rfind('/');
// Remove the last dir in the path
l_newPath = m_currentPath.substr(0, l_pos);
if (l_newPath.empty())
// We're at /
l_newPath = "/";
l_oldDir = m_currentPath.substr(l_pos + 1);
}
else
{
l_newPath = m_currentPath + (m_currentPath == "/" ? "" : "/") + m_fileLister[m_highlightedLine].m_name;
}
}
else
{
// Open given dir
if (p_path == m_currentPath)
return false;
l_newPath = p_path;
}
// List the new path
if (m_fileLister.list(l_newPath))
{
// Path OK
m_currentPath = l_newPath;
// If it's a back movement, restore old dir
if (!l_oldDir.empty())
m_highlightedLine = m_fileLister.searchDir(l_oldDir);
else
m_highlightedLine = 0;
// Camera
adjustCamera();
// Clear select list
m_selectList.clear();
// New render
l_ret = true;
}
INHIBIT(std::cout << "open - new current path: " << m_currentPath << std::endl;)
return l_ret;
}
const bool CPanel::goToParentDir(void)
{
bool l_ret(false);
// Select ".." and open it
if (m_currentPath != "/")
{
m_highlightedLine = 0;
l_ret = open();
}
return l_ret;
}
void CPanel::adjustCamera(void)
{
if (m_fileLister.getNbTotal() <= NB_VISIBLE_LINES)
m_camera = 0;
else if (m_highlightedLine < m_camera)
m_camera = m_highlightedLine;
else if (m_highlightedLine > m_camera + NB_VISIBLE_LINES - 1)
m_camera = m_highlightedLine - NB_VISIBLE_LINES + 1;
}
const std::string &CPanel::getHighlightedItem(void) const
{
return m_fileLister[m_highlightedLine].m_name;
}
const std::string CPanel::getHighlightedItemFull(void) const
{
return m_currentPath + (m_currentPath == "/" ? "" : "/") + m_fileLister[m_highlightedLine].m_name;
}
const std::string &CPanel::getCurrentPath(void) const
{
return m_currentPath;
}
const unsigned int &CPanel::getHighlightedIndex(void) const
{
return m_highlightedLine;
}
const unsigned int CPanel::getHighlightedIndexRelative(void) const
{
return m_highlightedLine - m_camera;
}
void CPanel::refresh(void)
{
// List current path
if (m_fileLister.list(m_currentPath))
{
// Adjust selected line
if (m_highlightedLine > m_fileLister.getNbTotal() - 1)
m_highlightedLine = m_fileLister.getNbTotal() - 1;
}
else
{
// Current path doesn't exist anymore => default
m_fileLister.list(PATH_DEFAULT);
m_currentPath = PATH_DEFAULT;
m_highlightedLine = 0;
}
// Camera
adjustCamera();
// Clear select list
m_selectList.clear();
}
const bool CPanel::addToSelectList(const bool p_step)
{
if (m_fileLister[m_highlightedLine].m_name != "..")
{
// Search highlighted element in select list
std::set<unsigned int>::iterator l_it = m_selectList.find(m_highlightedLine);
if (l_it == m_selectList.end())
// Element not present => we add it
m_selectList.insert(m_highlightedLine);
else
// Element present => we remove it from the list
m_selectList.erase(m_highlightedLine);
if (p_step)
moveCursorDown(1);
return true;
}
else
{
return false;
}
}
const std::set<unsigned int> &CPanel::getSelectList(void) const
{
return m_selectList;
}
void CPanel::getSelectList(std::vector<std::string> &p_list) const
{
p_list.clear();
// Insert full path of selected files
for (std::set<unsigned int>::const_iterator l_it = m_selectList.begin(); l_it != m_selectList.end(); ++l_it)
{
if (m_currentPath == "/")
p_list.push_back(m_currentPath + m_fileLister[*l_it].m_name);
else
p_list.push_back(m_currentPath + "/" + m_fileLister[*l_it].m_name);
}
}
void CPanel::selectAll(void)
{
const unsigned int l_nb = m_fileLister.getNbTotal();
for (unsigned int l_i = 1; l_i < l_nb; ++l_i)
m_selectList.insert(l_i);
}
void CPanel::selectNone(void)
{
m_selectList.clear();
}
const bool CPanel::isDirectoryHighlighted(void) const
{
return m_fileLister.isDirectory(m_highlightedLine);
}