eBookReaderSwitch/source/menus/book-chooser/MenuChooser.cpp

101 lines
2.5 KiB
C++
Raw Normal View History

extern "C" {
#include "MenuChooser.h"
2019-09-02 16:24:09 -04:00
#include "menu_book_reader.h"
#include "common.h"
#include "SDL_helper.h"
}
#include <switch.h>
#include <iostream>
2019-09-02 16:24:09 -04:00
/*#include <dirent.h>
#include <sys/types.h>*/
#include <filesystem>
2019-09-02 16:24:09 -04:00
using namespace std;
namespace fs = filesystem;
//using namespace std;
2019-09-02 16:24:09 -04:00
//extern bool readingBook;
2019-09-02 16:24:09 -04:00
void Menu_StartChoosing() {
int choosenIndex = 0;
bool readingBook = false;
2019-09-02 16:24:09 -04:00
string path = "/switch/eBookReader/books";
while(appletMainLoop()) {
2019-09-02 16:24:09 -04:00
if (readingBook) {
break;
}
SDL_ClearScreen(RENDERER, WHITE);
SDL_RenderClear(RENDERER);
2019-09-02 16:24:09 -04:00
hidScanInput();
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
u64 kHeld = hidKeysHeld(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS) {
break;
}
if (kDown & KEY_A) {
int bookIndex = 0;
for (const auto & entry : fs::directory_iterator(path)) {
string filename = entry.path().filename().string();
string extention = filename.substr(filename.length() - 3);
if (extention == "pdf") {
if (bookIndex == choosenIndex) {
char* book = (char *) entry.path().string().c_str();
Menu_OpenBook(book);
readingBook = true;
break;
}
choosenIndex++;
}
}
}
if (kDown & KEY_DUP) {
if (choosenIndex != 0) choosenIndex--;
}
if (kDown & KEY_DDOWN) {
choosenIndex++;
}
int space_index = 0;
2019-09-02 16:24:09 -04:00
for (const auto & entry : fs::directory_iterator(path)) {
string filename = entry.path().filename().string();
string extention = filename.substr(filename.length() - 3);
if (extention == "pdf") {
if (choosenIndex == space_index) {
SDL_DrawRect(RENDERER, 15, 20 + (40 * space_index), 1265, 40, VERY_LIGHT_GRAY);
}
SDL_DrawText(RENDERER, ARIAL, 20, 20 + (40 * space_index), BLACK, entry.path().filename().c_str());
space_index++;
}
}
SDL_RenderPresent(RENDERER);
}
}
/*void list_dir(const char *path) {
struct dirent *entry;
DIR *dir = opendir(path);
if (dir == NULL) {
return;
}
while ((entry = readdir(dir)) != NULL) {
entry->
//cout << entry->d_name << endl;
}
closedir(dir);
}*/