2025-12-13 21:17:34 +11:00
|
|
|
#include "EpubReaderChapterSelectionScreen.h"
|
|
|
|
|
|
|
|
|
|
#include <GfxRenderer.h>
|
|
|
|
|
#include <SD.h>
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
constexpr int PAGE_ITEMS = 24;
|
|
|
|
|
constexpr int SKIP_PAGE_MS = 700;
|
|
|
|
|
|
|
|
|
|
void EpubReaderChapterSelectionScreen::taskTrampoline(void* param) {
|
|
|
|
|
auto* self = static_cast<EpubReaderChapterSelectionScreen*>(param);
|
|
|
|
|
self->displayTaskLoop();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-15 19:39:07 +03:00
|
|
|
void EpubReaderChapterSelectionScreen::rebuildVisibleSpineIndices() {
|
|
|
|
|
visibleSpineIndices.clear();
|
|
|
|
|
if (!epub) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int spineCount = epub->getSpineItemsCount();
|
|
|
|
|
visibleSpineIndices.reserve(spineCount);
|
|
|
|
|
for (int i = 0; i < spineCount; i++) {
|
|
|
|
|
if (epub->getTocIndexForSpineIndex(i) != -1) {
|
|
|
|
|
visibleSpineIndices.push_back(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-13 21:17:34 +11:00
|
|
|
void EpubReaderChapterSelectionScreen::onEnter() {
|
|
|
|
|
if (!epub) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderingMutex = xSemaphoreCreateMutex();
|
2025-12-15 19:39:07 +03:00
|
|
|
rebuildVisibleSpineIndices();
|
|
|
|
|
|
|
|
|
|
selectorIndex = 0;
|
|
|
|
|
if (!visibleSpineIndices.empty()) {
|
|
|
|
|
for (size_t i = 0; i < visibleSpineIndices.size(); i++) {
|
|
|
|
|
if (visibleSpineIndices[i] == currentSpineIndex) {
|
|
|
|
|
selectorIndex = static_cast<int>(i);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (selectorIndex >= static_cast<int>(visibleSpineIndices.size())) {
|
|
|
|
|
selectorIndex = static_cast<int>(visibleSpineIndices.size()) - 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-13 21:17:34 +11:00
|
|
|
|
|
|
|
|
// Trigger first update
|
|
|
|
|
updateRequired = true;
|
|
|
|
|
xTaskCreate(&EpubReaderChapterSelectionScreen::taskTrampoline, "EpubReaderChapterSelectionScreenTask",
|
|
|
|
|
2048, // Stack size
|
|
|
|
|
this, // Parameters
|
|
|
|
|
1, // Priority
|
|
|
|
|
&displayTaskHandle // Task handle
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EpubReaderChapterSelectionScreen::onExit() {
|
|
|
|
|
// Wait until not rendering to delete task to avoid killing mid-instruction to EPD
|
|
|
|
|
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
|
|
|
|
if (displayTaskHandle) {
|
|
|
|
|
vTaskDelete(displayTaskHandle);
|
|
|
|
|
displayTaskHandle = nullptr;
|
|
|
|
|
}
|
|
|
|
|
vSemaphoreDelete(renderingMutex);
|
|
|
|
|
renderingMutex = nullptr;
|
2025-12-15 19:39:07 +03:00
|
|
|
visibleSpineIndices.clear();
|
2025-12-13 21:17:34 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EpubReaderChapterSelectionScreen::handleInput() {
|
|
|
|
|
const bool prevReleased =
|
|
|
|
|
inputManager.wasReleased(InputManager::BTN_UP) || inputManager.wasReleased(InputManager::BTN_LEFT);
|
|
|
|
|
const bool nextReleased =
|
|
|
|
|
inputManager.wasReleased(InputManager::BTN_DOWN) || inputManager.wasReleased(InputManager::BTN_RIGHT);
|
|
|
|
|
|
|
|
|
|
const bool skipPage = inputManager.getHeldTime() > SKIP_PAGE_MS;
|
|
|
|
|
|
|
|
|
|
if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
|
2025-12-15 19:39:07 +03:00
|
|
|
if (!visibleSpineIndices.empty()) {
|
|
|
|
|
if (selectorIndex >= static_cast<int>(visibleSpineIndices.size())) {
|
|
|
|
|
selectorIndex = static_cast<int>(visibleSpineIndices.size()) - 1;
|
|
|
|
|
}
|
|
|
|
|
onSelectSpineIndex(visibleSpineIndices[selectorIndex]);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inputManager.wasPressed(InputManager::BTN_BACK)) {
|
2025-12-13 21:17:34 +11:00
|
|
|
onGoBack();
|
2025-12-15 19:39:07 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int chapterCount = static_cast<int>(visibleSpineIndices.size());
|
|
|
|
|
if (chapterCount == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (selectorIndex >= chapterCount) {
|
|
|
|
|
selectorIndex = chapterCount - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (prevReleased) {
|
2025-12-13 21:17:34 +11:00
|
|
|
if (skipPage) {
|
2025-12-15 19:39:07 +03:00
|
|
|
const int totalPages = (chapterCount + PAGE_ITEMS - 1) / PAGE_ITEMS;
|
|
|
|
|
int currentPage = selectorIndex / PAGE_ITEMS;
|
|
|
|
|
currentPage = (currentPage + totalPages - 1) % totalPages;
|
|
|
|
|
selectorIndex = currentPage * PAGE_ITEMS;
|
|
|
|
|
if (selectorIndex >= chapterCount) {
|
|
|
|
|
selectorIndex = chapterCount - 1;
|
|
|
|
|
}
|
2025-12-13 21:17:34 +11:00
|
|
|
} else {
|
2025-12-15 19:39:07 +03:00
|
|
|
selectorIndex = (selectorIndex + chapterCount - 1) % chapterCount;
|
2025-12-13 21:17:34 +11:00
|
|
|
}
|
|
|
|
|
updateRequired = true;
|
|
|
|
|
} else if (nextReleased) {
|
|
|
|
|
if (skipPage) {
|
2025-12-15 19:39:07 +03:00
|
|
|
const int totalPages = (chapterCount + PAGE_ITEMS - 1) / PAGE_ITEMS;
|
|
|
|
|
int currentPage = selectorIndex / PAGE_ITEMS;
|
|
|
|
|
currentPage = (currentPage + 1) % totalPages;
|
|
|
|
|
selectorIndex = currentPage * PAGE_ITEMS;
|
|
|
|
|
if (selectorIndex >= chapterCount) {
|
|
|
|
|
selectorIndex = chapterCount - 1;
|
|
|
|
|
}
|
2025-12-13 21:17:34 +11:00
|
|
|
} else {
|
2025-12-15 19:39:07 +03:00
|
|
|
selectorIndex = (selectorIndex + 1) % chapterCount;
|
2025-12-13 21:17:34 +11:00
|
|
|
}
|
|
|
|
|
updateRequired = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EpubReaderChapterSelectionScreen::displayTaskLoop() {
|
|
|
|
|
while (true) {
|
|
|
|
|
if (updateRequired) {
|
|
|
|
|
updateRequired = false;
|
|
|
|
|
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
|
|
|
|
renderScreen();
|
|
|
|
|
xSemaphoreGive(renderingMutex);
|
|
|
|
|
}
|
|
|
|
|
vTaskDelay(10 / portTICK_PERIOD_MS);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EpubReaderChapterSelectionScreen::renderScreen() {
|
|
|
|
|
renderer.clearScreen();
|
|
|
|
|
|
|
|
|
|
const auto pageWidth = renderer.getScreenWidth();
|
|
|
|
|
renderer.drawCenteredText(READER_FONT_ID, 10, "Select Chapter", true, BOLD);
|
|
|
|
|
|
2025-12-15 19:39:07 +03:00
|
|
|
const int chapterCount = static_cast<int>(visibleSpineIndices.size());
|
|
|
|
|
if (chapterCount == 0) {
|
|
|
|
|
renderer.drawText(UI_FONT_ID, 20, 60, "No chapters available");
|
|
|
|
|
renderer.displayBuffer();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (selectorIndex >= chapterCount) {
|
|
|
|
|
selectorIndex = chapterCount - 1;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-13 21:17:34 +11:00
|
|
|
const auto pageStartIndex = selectorIndex / PAGE_ITEMS * PAGE_ITEMS;
|
|
|
|
|
renderer.fillRect(0, 60 + (selectorIndex % PAGE_ITEMS) * 30 + 2, pageWidth - 1, 30);
|
2025-12-15 19:39:07 +03:00
|
|
|
for (int i = pageStartIndex; i < chapterCount && i < pageStartIndex + PAGE_ITEMS; i++) {
|
|
|
|
|
const int spineIndex = visibleSpineIndices[i];
|
|
|
|
|
const int tocIndex = epub->getTocIndexForSpineIndex(spineIndex);
|
2025-12-13 21:17:34 +11:00
|
|
|
if (tocIndex == -1) {
|
2025-12-15 19:39:07 +03:00
|
|
|
continue; // Filtered chapters should not reach here, but skip defensively.
|
2025-12-13 21:17:34 +11:00
|
|
|
}
|
2025-12-15 19:39:07 +03:00
|
|
|
auto item = epub->getTocItem(tocIndex);
|
|
|
|
|
renderer.drawText(UI_FONT_ID, 20 + (item.level - 1) * 15, 60 + (i % PAGE_ITEMS) * 30, item.title.c_str(),
|
|
|
|
|
i != selectorIndex);
|
2025-12-13 21:17:34 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderer.displayBuffer();
|
|
|
|
|
}
|