2019-09-02 02:55:49 +00:00
|
|
|
extern "C" {
|
|
|
|
#include "MenuChooser.h"
|
2019-09-02 20:24:09 +00:00
|
|
|
#include "menu_book_reader.h"
|
2019-09-02 02:55:49 +00:00
|
|
|
#include "SDL_helper.h"
|
2019-09-03 22:17:53 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include "textures.h"
|
2019-09-20 00:12:38 +00:00
|
|
|
#include "config.h"
|
2019-09-02 02:55:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include <switch.h>
|
|
|
|
#include <iostream>
|
2019-09-02 20:24:09 +00:00
|
|
|
#include <filesystem>
|
2019-09-03 22:17:53 +00:00
|
|
|
#include <bits/stdc++.h>
|
|
|
|
|
|
|
|
#include <SDL2/SDL_image.h>
|
2019-09-02 02:55:49 +00:00
|
|
|
|
2019-09-02 20:24:09 +00:00
|
|
|
using namespace std;
|
|
|
|
namespace fs = filesystem;
|
2019-09-02 02:55:49 +00:00
|
|
|
|
2019-09-22 20:49:19 +00:00
|
|
|
template <typename T> bool contains(list<T> & listOfElements, const T & element) {
|
|
|
|
auto it = find(listOfElements.begin(), listOfElements.end(), element);
|
2019-09-03 22:17:53 +00:00
|
|
|
return it != listOfElements.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern TTF_Font *ARIAL, *ARIAL_35, *ARIAL_25, *ARIAL_15;
|
2019-09-02 02:55:49 +00:00
|
|
|
|
2019-09-02 20:24:09 +00:00
|
|
|
void Menu_StartChoosing() {
|
|
|
|
int choosenIndex = 0;
|
|
|
|
bool readingBook = false;
|
2019-09-03 22:17:53 +00:00
|
|
|
list<string> allowedExtentions = {".pdf", ".epub", ".cbz", ".xps"};
|
|
|
|
list<string> warnedExtentions = {".epub", ".cbz", ".xps"};
|
2019-09-02 02:55:49 +00:00
|
|
|
|
2019-09-02 20:24:09 +00:00
|
|
|
string path = "/switch/eBookReader/books";
|
2019-09-03 22:17:53 +00:00
|
|
|
|
|
|
|
// Count the amount of allowed files
|
|
|
|
int amountOfFiles = 0;
|
|
|
|
for (const auto & entry : fs::directory_iterator(path)) {
|
|
|
|
string filename = entry.path().filename().string();
|
|
|
|
string extention = filename.substr(filename.find_last_of("."));
|
|
|
|
|
|
|
|
if (contains(allowedExtentions, extention)) {
|
|
|
|
amountOfFiles++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isWarningOnScreen = false;
|
|
|
|
int windowX, windowY;
|
|
|
|
SDL_GetWindowSize(WINDOW, &windowX, &windowY);
|
|
|
|
int warningWidth = 700;
|
|
|
|
int warningHeight = 300;
|
2019-09-02 02:55:49 +00:00
|
|
|
while(appletMainLoop()) {
|
2019-09-02 20:24:09 +00:00
|
|
|
if (readingBook) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-09-20 00:12:38 +00:00
|
|
|
SDL_Color textColor = configDarkMode ? WHITE : BLACK;
|
|
|
|
SDL_Color backColor = configDarkMode ? BACK_BLACK : BACK_WHITE;
|
|
|
|
|
|
|
|
SDL_ClearScreen(RENDERER, backColor);
|
2019-09-02 02:55:49 +00:00
|
|
|
SDL_RenderClear(RENDERER);
|
|
|
|
|
2019-09-02 20:24:09 +00:00
|
|
|
hidScanInput();
|
|
|
|
|
|
|
|
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
|
|
|
|
u64 kHeld = hidKeysHeld(CONTROLLER_P1_AUTO);
|
|
|
|
|
2019-09-20 00:12:38 +00:00
|
|
|
/*if (!isWarningOnScreen && kDown & KEY_PLUS) {
|
2019-09-02 20:24:09 +00:00
|
|
|
break;
|
2019-09-20 00:12:38 +00:00
|
|
|
}*/
|
2019-09-02 20:24:09 +00:00
|
|
|
|
2019-09-03 22:17:53 +00:00
|
|
|
if (kDown & KEY_B) {
|
2019-09-20 00:12:38 +00:00
|
|
|
if (!isWarningOnScreen) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
isWarningOnScreen = false;
|
|
|
|
}
|
2019-09-03 22:17:53 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 20:24:09 +00:00
|
|
|
if (kDown & KEY_A) {
|
|
|
|
int bookIndex = 0;
|
|
|
|
for (const auto & entry : fs::directory_iterator(path)) {
|
|
|
|
string filename = entry.path().filename().string();
|
2019-09-03 22:17:53 +00:00
|
|
|
string extention = filename.substr(filename.find_last_of("."));
|
|
|
|
|
|
|
|
if (contains(allowedExtentions, extention)) {
|
2019-09-02 20:24:09 +00:00
|
|
|
if (bookIndex == choosenIndex) {
|
2019-09-03 22:17:53 +00:00
|
|
|
if (contains(warnedExtentions, extention)) {
|
2019-09-20 00:12:38 +00:00
|
|
|
/*#ifdef EXPERIMENTAL
|
2019-09-03 22:17:53 +00:00
|
|
|
SDL_DrawImage(RENDERER, warning, 5, 10 + (40 * choosingIndex));
|
2019-09-20 00:12:38 +00:00
|
|
|
#endif*/
|
2019-09-03 22:17:53 +00:00
|
|
|
if (isWarningOnScreen) {
|
|
|
|
goto OPEN_BOOK;
|
|
|
|
} else {
|
|
|
|
isWarningOnScreen = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
OPEN_BOOK:
|
|
|
|
string book = path + "/" + filename;
|
|
|
|
cout << "Opening book: " << book << endl;
|
|
|
|
|
|
|
|
Menu_OpenBook((char*) book.c_str());
|
|
|
|
readingBook = true;
|
|
|
|
break;
|
|
|
|
}
|
2019-09-02 20:24:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 22:17:53 +00:00
|
|
|
bookIndex++;
|
2019-09-02 20:24:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kDown & KEY_DUP) {
|
2019-09-20 00:12:38 +00:00
|
|
|
if (choosenIndex != 0 && !isWarningOnScreen) {
|
|
|
|
choosenIndex--;
|
|
|
|
} else if (choosenIndex == 0) {
|
|
|
|
choosenIndex = amountOfFiles-1;
|
|
|
|
}
|
2019-09-02 20:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (kDown & KEY_DDOWN) {
|
2019-09-20 00:12:38 +00:00
|
|
|
if (choosenIndex == amountOfFiles-1) {
|
|
|
|
choosenIndex = 0;
|
|
|
|
} else if (choosenIndex < amountOfFiles-1 && !isWarningOnScreen) {
|
|
|
|
choosenIndex++;
|
|
|
|
}
|
2019-09-02 20:24:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-20 00:12:38 +00:00
|
|
|
if (kDown & KEY_MINUS) {
|
|
|
|
configDarkMode = !configDarkMode;
|
|
|
|
}
|
|
|
|
|
2019-09-22 18:45:18 +00:00
|
|
|
SDL_DrawText(RENDERER, ARIAL_25, windowX - 123, windowY - 45, textColor, "\"B\" - Exit");
|
2019-09-22 20:49:19 +00:00
|
|
|
SDL_DrawText(RENDERER, ARIAL_25, windowX - 200, windowY - 80, textColor, "\"-\" - Switch theme");
|
2019-09-20 00:12:38 +00:00
|
|
|
|
2019-09-03 22:17:53 +00:00
|
|
|
int choosingIndex = 0;
|
2019-09-02 20:24:09 +00:00
|
|
|
for (const auto & entry : fs::directory_iterator(path)) {
|
|
|
|
string filename = entry.path().filename().string();
|
2019-09-03 22:17:53 +00:00
|
|
|
string extention = filename.substr(filename.find_last_of("."));
|
|
|
|
|
|
|
|
if (contains(allowedExtentions, extention)) {
|
|
|
|
if (choosenIndex == choosingIndex) {
|
2019-09-20 00:12:38 +00:00
|
|
|
SDL_DrawRect(RENDERER, 15, 15 + (40 * choosingIndex), 1265, 40, configDarkMode ? SELECTOR_COLOUR_DARK : SELECTOR_COLOUR_LIGHT);
|
2019-09-02 20:24:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 22:17:53 +00:00
|
|
|
#ifdef EXPERIMENTAL
|
|
|
|
if (contains(warnedExtentions, extention)) {
|
2019-09-22 20:49:19 +00:00
|
|
|
SDL_DrawImage(RENDERER, warning, 25, 18 + (40 * choosingIndex));
|
2019-09-03 22:17:53 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-09-22 20:49:19 +00:00
|
|
|
SDL_DrawText(RENDERER, ARIAL_25, 70, 20 + (40 * choosingIndex), textColor, entry.path().filename().c_str());
|
2019-09-03 22:17:53 +00:00
|
|
|
|
|
|
|
if (isWarningOnScreen) {
|
2019-09-20 00:12:38 +00:00
|
|
|
if (!configDarkMode) { // Display a dimmed background if on light mode
|
|
|
|
SDL_DrawRect(RENDERER, 0, 0, 1280, 720, SDL_MakeColour(50, 50, 50, 150));
|
|
|
|
}
|
2019-09-03 22:17:53 +00:00
|
|
|
|
2019-09-20 00:12:38 +00:00
|
|
|
SDL_DrawRect(RENDERER, (windowX - warningWidth) / 2, (windowY - warningHeight) / 2, warningWidth, warningHeight, configDarkMode ? HINT_COLOUR_DARK : HINT_COLOUR_LIGHT);
|
|
|
|
SDL_DrawText(RENDERER, ARIAL_30, (windowX - warningWidth) / 2 + 15, (windowY - warningHeight) / 2 + 15, textColor, "This file is not yet fully supported, and may");
|
|
|
|
SDL_DrawText(RENDERER, ARIAL_30, (windowX - warningWidth) / 2 + 15, (windowY - warningHeight) / 2 + 50, textColor, "cause a system, or app crash.");
|
|
|
|
SDL_DrawText(RENDERER, ARIAL_20, (windowX - warningWidth) / 2 + warningWidth - 250, (windowY - warningHeight) / 2 + warningHeight - 30, textColor, "\"A\" - Read");
|
|
|
|
SDL_DrawText(RENDERER, ARIAL_20, (windowX - warningWidth) / 2 + warningWidth - 125, (windowY - warningHeight) / 2 + warningHeight - 30, textColor, "\"B\" - Cancel.");
|
2019-09-03 22:17:53 +00:00
|
|
|
}
|
2019-09-02 20:24:09 +00:00
|
|
|
|
2019-09-03 22:17:53 +00:00
|
|
|
choosingIndex++;
|
2019-09-02 20:24:09 +00:00
|
|
|
}
|
2019-09-02 02:55:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_RenderPresent(RENDERER);
|
|
|
|
}
|
2019-09-03 22:17:53 +00:00
|
|
|
}
|