mirror of https://github.com/mgba-emu/mgba.git
GUI: Improve file browsing with proper filters and current directory listing
This commit is contained in:
parent
278b17e56f
commit
d67844e95f
|
@ -108,7 +108,8 @@ int main() {
|
||||||
|
|
||||||
while (aptMainLoop()) {
|
while (aptMainLoop()) {
|
||||||
char path[256];
|
char path[256];
|
||||||
if (!selectFile(¶ms, "/", path, sizeof(path), "gba")) {
|
char currentPath[256] = "";
|
||||||
|
if (!selectFile(¶ms, "/", path, currentPath, sizeof(path), GBAIsROM)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
_drawStart();
|
_drawStart();
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
#include "psp2-context.h"
|
#include "psp2-context.h"
|
||||||
|
|
||||||
|
#include "gba/gba.h"
|
||||||
#include "util/gui.h"
|
#include "util/gui.h"
|
||||||
#include "util/gui/font.h"
|
#include "util/gui/font.h"
|
||||||
#include "util/gui/file-select.h"
|
#include "util/gui/file-select.h"
|
||||||
|
@ -69,7 +70,8 @@ int main() {
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
char path[256];
|
char path[256];
|
||||||
if (!selectFile(¶ms, "cache0:", path, sizeof(path), "gba")) {
|
char currentPath[256] = "";
|
||||||
|
if (!selectFile(¶ms, "cache0:", path, currentPath, sizeof(path), GBAIsROM)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!GBAPSP2LoadROM(path)) {
|
if (!GBAPSP2LoadROM(path)) {
|
||||||
|
|
|
@ -185,6 +185,7 @@ struct VFile* _vdsceOpenFile(struct VDir* vd, const char* path, int mode) {
|
||||||
const char* dir = vdsce->path;
|
const char* dir = vdsce->path;
|
||||||
char* combined = malloc(sizeof(char) * (strlen(path) + strlen(dir) + strlen(PATH_SEP) + 1));
|
char* combined = malloc(sizeof(char) * (strlen(path) + strlen(dir) + strlen(PATH_SEP) + 1));
|
||||||
sprintf(combined, "%s%s%s", dir, PATH_SEP, path);
|
sprintf(combined, "%s%s%s", dir, PATH_SEP, path);
|
||||||
|
printf("Opening %s\n", combined);
|
||||||
|
|
||||||
struct VFile* file = VFileOpen(combined, mode);
|
struct VFile* file = VFileOpen(combined, mode);
|
||||||
free(combined);
|
free(combined);
|
||||||
|
|
|
@ -167,6 +167,7 @@ int main() {
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
char path[256];
|
char path[256];
|
||||||
|
char currentPath[256] = "";
|
||||||
guOrtho(proj, -20, 240, 0, 352, 0, 300);
|
guOrtho(proj, -20, 240, 0, 352, 0, 300);
|
||||||
GX_LoadProjectionMtx(proj, GX_ORTHOGRAPHIC);
|
GX_LoadProjectionMtx(proj, GX_ORTHOGRAPHIC);
|
||||||
|
|
||||||
|
@ -174,7 +175,7 @@ int main() {
|
||||||
352, 230,
|
352, 230,
|
||||||
font, _drawStart, _drawEnd, _pollInput
|
font, _drawStart, _drawEnd, _pollInput
|
||||||
};
|
};
|
||||||
if (!selectFile(¶ms, "/", path, sizeof(path), "gba") || !GBAWiiLoadGame(path)) {
|
if (!selectFile(¶ms, "/", path, currentPath, sizeof(path), GBAIsROM) || !GBAWiiLoadGame(path)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
GBAContextStart(&context);
|
GBAContextStart(&context);
|
||||||
|
|
|
@ -37,7 +37,7 @@ static void _upDirectory(char* currentPath) {
|
||||||
// TODO: What if there was a trailing slash?
|
// TODO: What if there was a trailing slash?
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool _refreshDirectory(const char* currentPath, struct FileList* currentFiles) {
|
static bool _refreshDirectory(const char* currentPath, struct FileList* currentFiles, bool (*filter)(struct VFile*)) {
|
||||||
_cleanFiles(currentFiles);
|
_cleanFiles(currentFiles);
|
||||||
|
|
||||||
struct VDir* dir = VDirOpen(currentPath);
|
struct VDir* dir = VDirOpen(currentPath);
|
||||||
|
@ -46,24 +46,37 @@ static bool _refreshDirectory(const char* currentPath, struct FileList* currentF
|
||||||
}
|
}
|
||||||
struct VDirEntry* de;
|
struct VDirEntry* de;
|
||||||
while ((de = dir->listNext(dir))) {
|
while ((de = dir->listNext(dir))) {
|
||||||
if (de->name(de)[0] == '.') {
|
const char* name = de->name(de);
|
||||||
|
if (name[0] == '.') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
*FileListAppend(currentFiles) = strdup(de->name(de));
|
if (de->type(de) == VFS_FILE) {
|
||||||
|
struct VFile* vf = dir->openFile(dir, name, O_RDONLY);
|
||||||
|
if (!vf) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!filter || filter(vf)) {
|
||||||
|
*FileListAppend(currentFiles) = strdup(name);
|
||||||
|
}
|
||||||
|
vf->close(vf);
|
||||||
|
} else {
|
||||||
|
*FileListAppend(currentFiles) = strdup(name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
dir->close(dir);
|
dir->close(dir);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool selectFile(const struct GUIParams* params, const char* basePath, char* outPath, size_t outLen, const char* suffix) {
|
bool selectFile(const struct GUIParams* params, const char* basePath, char* outPath, char* currentPath, size_t outLen, bool (*filter)(struct VFile*)) {
|
||||||
char currentPath[256];
|
if (!currentPath[0]) {
|
||||||
strncpy(currentPath, basePath, sizeof(currentPath));
|
strncpy(currentPath, basePath, outLen);
|
||||||
|
}
|
||||||
size_t fileIndex = 0;
|
size_t fileIndex = 0;
|
||||||
size_t start = 0;
|
size_t start = 0;
|
||||||
|
|
||||||
struct FileList currentFiles;
|
struct FileList currentFiles;
|
||||||
FileListInit(¤tFiles, 0);
|
FileListInit(¤tFiles, 0);
|
||||||
_refreshDirectory(currentPath, ¤tFiles);
|
_refreshDirectory(currentPath, ¤tFiles, filter);
|
||||||
|
|
||||||
int inputHistory[GUI_INPUT_MAX] = { 0 };
|
int inputHistory[GUI_INPUT_MAX] = { 0 };
|
||||||
|
|
||||||
|
@ -98,27 +111,27 @@ bool selectFile(const struct GUIParams* params, const char* basePath, char* outP
|
||||||
FileListDeinit(¤tFiles);
|
FileListDeinit(¤tFiles);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (newInput & (1 << GUI_INPUT_SELECT)) {
|
if (newInput & (1 << GUI_INPUT_SELECT) && FileListSize(¤tFiles)) {
|
||||||
size_t len = strlen(currentPath);
|
size_t len = strlen(currentPath);
|
||||||
const char* sep = PATH_SEP;
|
const char* sep = PATH_SEP;
|
||||||
if (currentPath[len - 1] == *sep) {
|
if (currentPath[len - 1] == *sep) {
|
||||||
sep = "";
|
sep = "";
|
||||||
}
|
}
|
||||||
snprintf(currentPath, sizeof(currentPath), "%s%s%s", currentPath, sep, *FileListGetPointer(¤tFiles, fileIndex));
|
snprintf(outPath, outLen, "%s%s%s", currentPath, sep, *FileListGetPointer(¤tFiles, fileIndex));
|
||||||
if (!_refreshDirectory(currentPath, ¤tFiles)) {
|
if (!_refreshDirectory(outPath, ¤tFiles, filter)) {
|
||||||
strncpy(outPath, currentPath, outLen);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
strncpy(currentPath, outPath, outLen);
|
||||||
fileIndex = 0;
|
fileIndex = 0;
|
||||||
}
|
}
|
||||||
if (newInput & (1 << GUI_INPUT_BACK)) {
|
if (newInput & (1 << GUI_INPUT_BACK)) {
|
||||||
if (strncmp(currentPath, basePath, sizeof(currentPath)) == 0) {
|
if (strncmp(currentPath, basePath, outLen) == 0) {
|
||||||
_cleanFiles(¤tFiles);
|
_cleanFiles(¤tFiles);
|
||||||
FileListDeinit(¤tFiles);
|
FileListDeinit(¤tFiles);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
_upDirectory(currentPath);
|
_upDirectory(currentPath);
|
||||||
_refreshDirectory(currentPath, ¤tFiles);
|
_refreshDirectory(currentPath, ¤tFiles, filter);
|
||||||
fileIndex = 0;
|
fileIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,8 @@
|
||||||
|
|
||||||
#include "util/gui.h"
|
#include "util/gui.h"
|
||||||
|
|
||||||
bool selectFile(const struct GUIParams*, const char* basePath, char* outPath, size_t outLen, const char* suffix);
|
struct VFile;
|
||||||
|
|
||||||
|
bool selectFile(const struct GUIParams*, const char* basePath, char* outPath, char* currentPath, size_t outLen, bool (*filter)(struct VFile*));
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue