-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathFolderBrowserOverlay.cpp
More file actions
164 lines (147 loc) · 5.91 KB
/
Copy pathFolderBrowserOverlay.cpp
File metadata and controls
164 lines (147 loc) · 5.91 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
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
/*
* This file is part of Checkpoint
* Copyright (C) 2017-2026 Bernardo Giordano, FlagBrew
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include "FolderBrowserOverlay.hpp"
#include "colors.hpp"
#include "common.hpp"
#include "directory.hpp"
#include "gfxutils.hpp"
#include "i18n.hpp"
#include "shapes.hpp"
#include "uikit.hpp"
#include <algorithm>
namespace {
constexpr int PANEL_X = 290, PANEL_Y = 90, PANEL_W = 700, PANEL_H = 540;
constexpr int LIST_X = PANEL_X + 20;
constexpr int LIST_Y = PANEL_Y + 80;
constexpr int LIST_W = PANEL_W - 40;
constexpr int ROW_H = 44;
constexpr int ROW_GAP = 6;
constexpr int VISIBLE = (PANEL_H - 80 - 20) / (ROW_H + ROW_GAP);
}
FolderBrowserOverlay::FolderBrowserOverlay(Screen& screen, const std::string& heading, std::function<void(const std::string&)> onPick)
: Overlay(screen), mHeading(heading), mOnPick(std::move(onPick))
{
readFolders();
}
std::string FolderBrowserOverlay::currentPath(void) const
{
// mCurrent is SD-relative ("/" == root); config paths carry the "sdmc:"
// prefix, matching what the old keyboard prompt produced.
return "sdmc:" + mCurrent;
}
void FolderBrowserOverlay::readFolders(void)
{
mFolders.clear();
mCursor = 0;
mScroll = 0;
Directory dir(currentPath());
if (dir.good()) {
for (size_t i = 0, sz = dir.size(); i < sz; i++) {
// Sub-folders only; skip "." / ".." and dotfiles.
if (dir.folder(i) && dir.entry(i).size() && dir.entry(i)[0] != '.') {
mFolders.push_back(dir.entry(i));
}
}
}
std::sort(mFolders.begin(), mFolders.end());
}
void FolderBrowserOverlay::draw(void) const
{
Gfx::DrawRect(0, 0, 1280, 720, COLOR_SCRIM);
Shapes::cardRound(PANEL_X, PANEL_Y, PANEL_W, PANEL_H, 0, COLOR_SURFACE, COLOR_STROKE2, 1);
// Heading + a right-aligned "i / n" counter on the same line.
Gfx::DrawText(20, LIST_X, PANEL_Y + 18, COLOR_TEXT, mHeading.c_str());
if (!mFolders.empty()) {
std::string counter = StringUtils::format("%d / %d", mCursor + 1, (int)mFolders.size());
u32 cw, ch;
Gfx::GetTextDimensions(13, counter.c_str(), &cw, &ch);
Gfx::DrawText(13, PANEL_X + PANEL_W - 20 - (int)cw, PANEL_Y + 24, COLOR_TEXT3, counter.c_str());
}
// Current path, then a hairline under the header.
Gfx::DrawText(13, LIST_X, PANEL_Y + 48, COLOR_ACCENT_LIGHT, trimToFit(currentPath(), LIST_W, 13).c_str());
Gfx::DrawRect(LIST_X, PANEL_Y + 70, LIST_W, 1, COLOR_STROKE1);
if (mFolders.empty()) {
Gfx::DrawText(15, LIST_X, LIST_Y + 8, COLOR_TEXT2, i18n::t("overlay.no_subfolders").c_str());
Gfx::DrawText(13, LIST_X, LIST_Y + 34, COLOR_TEXT3, i18n::t("overlay.use_folder_hint").c_str());
}
for (int i = mScroll; i < (int)mFolders.size() && i < mScroll + VISIBLE; i++) {
const int y = LIST_Y + (i - mScroll) * (ROW_H + ROW_GAP);
const bool focus = i == mCursor;
Shapes::fillRound(LIST_X, y, LIST_W, ROW_H, 0, focus ? COLOR_ACCENT_TINT : COLOR_FILL1);
u32 nh;
Gfx::GetTextDimensions(15, "Ag", NULL, &nh);
std::string name = trimToFit(mFolders[i], LIST_W - 28, 15);
Gfx::DrawText(15, LIST_X + 14, y + (ROW_H - (int)nh) / 2, focus ? COLOR_TEXT : COLOR_TEXT2, name.c_str());
if (focus) {
Shapes::focusRing(LIST_X, y, LIST_W, ROW_H, 0, COLOR_ACCENT);
}
}
UiKit::drawHintBar({
{"A", i18n::t("overlay.open")},
{"X", i18n::t("overlay.use_folder")},
{"B", mCurrent == "/" ? i18n::t("common.cancel") : i18n::t("overlay.up")},
});
}
void FolderBrowserOverlay::update(const InputState& input)
{
const u64 kdown = input.kDown;
if (kdown & HidNpadButton_X) {
// Copy to the stack: me.reset() destroys *this before the callback runs.
auto pick = mOnPick;
std::string path = currentPath();
me.reset();
if (pick)
pick(path);
return;
}
if (kdown & HidNpadButton_B) {
if (mCurrent == "/") {
me.reset();
return;
}
size_t slash = mCurrent.find_last_of('/');
mCurrent = slash == 0 ? "/" : mCurrent.substr(0, slash);
readFolders();
return;
}
if (!mFolders.empty()) {
if (kdown & HidNpadButton_Up) {
mCursor = mCursor > 0 ? mCursor - 1 : (int)mFolders.size() - 1;
}
else if (kdown & HidNpadButton_Down) {
mCursor = mCursor < (int)mFolders.size() - 1 ? mCursor + 1 : 0;
}
if (mCursor < mScroll)
mScroll = mCursor;
else if (mCursor >= mScroll + VISIBLE)
mScroll = mCursor - VISIBLE + 1;
if (kdown & HidNpadButton_A) {
mCurrent += (mCurrent == "/" ? "" : "/") + mFolders[mCursor];
readFolders();
return;
}
}
}