forked from od-contrib/commander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
panel.cpp
executable file
·469 lines (426 loc) · 12.8 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include <iostream>
#include <sstream>
#include "panel.h"
#include "resourceManager.h"
#include "screen.h"
#include "sdlutils.h"
#include "fileutils.h"
CPanel::CPanel(const std::string &p_path, const Sint16 p_x):
m_currentPath(""),
m_camera(0),
m_x(p_x),
m_highlightedLine(0),
resources_(CResourceManager::instance()),
m_fonts(resources_.getFonts())
{
// 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) { }
SDL_Surface *CPanel::icon_dir() const
{
return resources_.getSurface(CResourceManager::T_SURFACE_FOLDER);
}
SDL_Surface *CPanel::icon_file() const
{
return resources_.getSurface(CResourceManager::T_SURFACE_FILE);
}
SDL_Surface *CPanel::icon_img() const
{
return resources_.getSurface(CResourceManager::T_SURFACE_FILE_IMAGE);
}
SDL_Surface *CPanel::icon_ipk() const
{
return resources_.getSurface(
CResourceManager::T_SURFACE_FILE_INSTALLABLE_PACKAGE);
}
SDL_Surface *CPanel::icon_opk() const
{
return resources_.getSurface(CResourceManager::T_SURFACE_FILE_PACKAGE);
}
SDL_Surface *CPanel::icon_symlink() const
{
return resources_.getSurface(CResourceManager::T_SURFACE_FILE_IS_SYMLINK);
}
SDL_Surface *CPanel::icon_up() const
{
return resources_.getSurface(CResourceManager::T_SURFACE_UP);
}
SDL_Surface *CPanel::cursor1() const
{
return resources_.getSurface(CResourceManager::T_SURFACE_CURSOR1);
}
SDL_Surface *CPanel::cursor2() const
{
return resources_.getSurface(CResourceManager::T_SURFACE_CURSOR2);
}
int CPanel::list_y() const { return Y_LIST_PHYS; }
int CPanel::line_height() const
{
return LINE_HEIGHT_PHYS;
}
int CPanel::header_height() const
{
return HEADER_H_PHYS;
}
int CPanel::header_padding_top() const
{
return HEADER_PADDING_TOP_PHYS;
}
int CPanel::footer_height() const
{
return FOOTER_H_PHYS;
}
int CPanel::footer_y() const { return screen.actual_h - footer_height(); }
int CPanel::footer_padding_top() const
{
return FOOTER_PADDING_TOP_PHYS;
}
int CPanel::width() const
{
return (screen.actual_w - static_cast<int>(2 * screen.ppu_x)) / 2;
}
int CPanel::list_height() const
{
return screen.actual_h - header_height() - footer_height();
}
void CPanel::render(const bool p_active) const
{
// Draw panel
const int l_x = m_x + icon_dir()->w + 2 * screen.ppu_x;
const unsigned int l_nbTotal = m_fileLister.getNbTotal();
int l_y = list_y();
SDL_Surface *l_surfaceTmp = NULL;
const SDL_Color *l_color = NULL;
SDL_Rect l_rect;
// Current dir
l_surfaceTmp = SDL_utils::renderText(
m_fonts, m_currentPath, Globals::g_colorTextTitle, { COLOR_TITLE_BG });
if (l_surfaceTmp->w > width()) {
l_rect.x = l_surfaceTmp->w - width();
l_rect.y = 0;
l_rect.w = width();
l_rect.h = l_surfaceTmp->h;
SDL_utils::applyPpuScaledSurface(
m_x, header_padding_top(), l_surfaceTmp, screen.surface, &l_rect);
} else {
SDL_utils::applyPpuScaledSurface(
m_x, header_padding_top(), l_surfaceTmp, screen.surface);
}
SDL_FreeSurface(l_surfaceTmp);
SDL_Rect clip_contents_rect = SDL_utils::Rect(0, list_y(), screen.actual_w, list_height());
// Content
SDL_SetClipRect(screen.surface, &clip_contents_rect);
// Draw cursor
SDL_utils::applyPpuScaledSurface(m_x - static_cast<int>(1 * screen.ppu_x),
list_y() + (m_highlightedLine - m_camera) * line_height(),
p_active ? cursor1() : cursor2(), screen.surface);
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 = icon_up();
else
l_surfaceTmp = icon_dir();
// Color
if (m_selectList.find(l_i) != m_selectList.end())
l_color = &Globals::g_colorTextSelected;
else
l_color = &Globals::g_colorTextDir;
}
else
{
// Icon
const std::string &ext = m_fileLister[l_i].m_ext;
if (SDL_utils::isSupportedImageExt(ext))
l_surfaceTmp = icon_img();
else if (ext == "ipk")
l_surfaceTmp = icon_ipk();
else if (ext == "opk")
l_surfaceTmp = icon_opk();
else
l_surfaceTmp = icon_file();
// Color
if (m_selectList.find(l_i) != m_selectList.end())
l_color = &Globals::g_colorTextSelected;
else
l_color = &Globals::g_colorTextNormal;
}
SDL_utils::applyPpuScaledSurface(m_x, l_y, l_surfaceTmp, screen.surface);
if (m_fileLister[l_i].is_symlink)
SDL_utils::applyPpuScaledSurface(m_x, l_y, icon_symlink(), screen.surface);
// Text
SDL_Color l_bg;
if (l_i == m_highlightedLine) {
if (p_active)
l_bg = {COLOR_CURSOR_1};
else
l_bg = {COLOR_CURSOR_2};
} else {
static const SDL_Color kLineBg[2] = {{COLOR_BG_1}, {COLOR_BG_2}};
l_bg = kLineBg[(l_i - m_camera) % 2];
}
l_surfaceTmp = SDL_utils::renderText(m_fonts, m_fileLister[l_i].m_name, *l_color, l_bg);
const int max_name_width = width() - static_cast<int>(18 * screen.ppu_x);
SDL_Rect *text_clip_rect = nullptr;
if (l_surfaceTmp->w > max_name_width)
{
l_rect.x = 0;
l_rect.y = 0;
l_rect.w = max_name_width;
l_rect.h = l_surfaceTmp->h;
text_clip_rect = &l_rect;
}
SDL_utils::applyPpuScaledSurface(l_x,
l_y + static_cast<int>(2 * screen.ppu_y), l_surfaceTmp,
screen.surface, text_clip_rect);
SDL_FreeSurface(l_surfaceTmp);
// Next line
l_y += line_height();
}
SDL_SetClipRect(screen.surface, nullptr);
// 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::applyPpuScaledText(m_x + static_cast<int>(2 * screen.ppu_x),
footer_y() + footer_padding_top(), screen.surface, m_fonts,
"Size:", Globals::g_colorTextTitle, { COLOR_TITLE_BG });
SDL_utils::applyPpuScaledText(
m_x + width() - static_cast<int>(2 * screen.ppu_x),
footer_y() + footer_padding_top(), screen.surface, m_fonts, l_footer,
Globals::g_colorTextTitle, { COLOR_TITLE_BG },
SDL_utils::T_TEXT_ALIGN_RIGHT);
}
const bool CPanel::moveCursorUp(unsigned char p_step)
{
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
return true;
}
return false;
}
const bool CPanel::moveCursorDown(unsigned char p_step)
{
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
return true;
}
return false;
}
int CPanel::getNumVisibleListItems() const
{
return std::min(
static_cast<decltype(m_fileLister.getNbTotal())>(NB_VISIBLE_LINES),
m_fileLister.getNbTotal() - m_camera);
}
int CPanel::getLineAt(int x, int y) const
{
if (x < 0 || y < list_y()) return -1;
const int y0 = list_y();
const int line_h = line_height();
if (y - y0 >= getNumVisibleListItems() * line_h) return -1;
return (y - y0) / line_h;
}
void CPanel::moveCursorToVisibleLineIndex(int index)
{
m_highlightedLine = m_camera + index;
}
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_FULLY_VISIBLE_LINES - 1)
m_camera = m_highlightedLine - NB_FULLY_VISIBLE_LINES + 1;
}
const std::string &CPanel::getHighlightedItem(void) const
{
return m_fileLister[m_highlightedLine].m_name;
}
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);
}