mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2025-12-18 15:17:42 +03:00
Add chapter selection screen
This commit is contained in:
parent
ead39fd04b
commit
8994953254
107
src/screens/EpubReaderChapterSelectionScreen.cpp
Normal file
107
src/screens/EpubReaderChapterSelectionScreen.cpp
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
#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();
|
||||||
|
}
|
||||||
|
|
||||||
|
void EpubReaderChapterSelectionScreen::onEnter() {
|
||||||
|
if (!epub) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
renderingMutex = xSemaphoreCreateMutex();
|
||||||
|
selectorIndex = currentSpineIndex;
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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)) {
|
||||||
|
onSelectSpineIndex(selectorIndex);
|
||||||
|
} else if (inputManager.wasPressed(InputManager::BTN_BACK)) {
|
||||||
|
onGoBack();
|
||||||
|
} else if (prevReleased) {
|
||||||
|
if (skipPage) {
|
||||||
|
selectorIndex =
|
||||||
|
((selectorIndex / PAGE_ITEMS - 1) * PAGE_ITEMS + epub->getSpineItemsCount()) % epub->getSpineItemsCount();
|
||||||
|
} else {
|
||||||
|
selectorIndex = (selectorIndex + epub->getSpineItemsCount() - 1) % epub->getSpineItemsCount();
|
||||||
|
}
|
||||||
|
updateRequired = true;
|
||||||
|
} else if (nextReleased) {
|
||||||
|
if (skipPage) {
|
||||||
|
selectorIndex = ((selectorIndex / PAGE_ITEMS + 1) * PAGE_ITEMS) % epub->getSpineItemsCount();
|
||||||
|
} else {
|
||||||
|
selectorIndex = (selectorIndex + 1) % epub->getSpineItemsCount();
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
|
||||||
|
const auto pageStartIndex = selectorIndex / PAGE_ITEMS * PAGE_ITEMS;
|
||||||
|
renderer.fillRect(0, 60 + (selectorIndex % PAGE_ITEMS) * 30 + 2, pageWidth - 1, 30);
|
||||||
|
for (int i = pageStartIndex; i < epub->getSpineItemsCount() && i < pageStartIndex + PAGE_ITEMS; i++) {
|
||||||
|
const int tocIndex = epub->getTocIndexForSpineIndex(i);
|
||||||
|
if (tocIndex == -1) {
|
||||||
|
renderer.drawText(UI_FONT_ID, 20, 60 + (i % PAGE_ITEMS) * 30, "Unnamed", i != selectorIndex);
|
||||||
|
} else {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
renderer.displayBuffer();
|
||||||
|
}
|
||||||
38
src/screens/EpubReaderChapterSelectionScreen.h
Normal file
38
src/screens/EpubReaderChapterSelectionScreen.h
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Epub.h>
|
||||||
|
#include <freertos/FreeRTOS.h>
|
||||||
|
#include <freertos/semphr.h>
|
||||||
|
#include <freertos/task.h>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "Screen.h"
|
||||||
|
|
||||||
|
class EpubReaderChapterSelectionScreen final : public Screen {
|
||||||
|
std::shared_ptr<Epub> epub;
|
||||||
|
TaskHandle_t displayTaskHandle = nullptr;
|
||||||
|
SemaphoreHandle_t renderingMutex = nullptr;
|
||||||
|
int currentSpineIndex = 0;
|
||||||
|
int selectorIndex = 0;
|
||||||
|
bool updateRequired = false;
|
||||||
|
const std::function<void()> onGoBack;
|
||||||
|
const std::function<void(int newSpineIndex)> onSelectSpineIndex;
|
||||||
|
|
||||||
|
static void taskTrampoline(void* param);
|
||||||
|
[[noreturn]] void displayTaskLoop();
|
||||||
|
void renderScreen();
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit EpubReaderChapterSelectionScreen(GfxRenderer& renderer, InputManager& inputManager,
|
||||||
|
const std::shared_ptr<Epub>& epub, const int currentSpineIndex,
|
||||||
|
const std::function<void()>& onGoBack,
|
||||||
|
const std::function<void(int newSpineIndex)>& onSelectSpineIndex)
|
||||||
|
: Screen(renderer, inputManager),
|
||||||
|
epub(epub),
|
||||||
|
currentSpineIndex(currentSpineIndex),
|
||||||
|
onGoBack(onGoBack),
|
||||||
|
onSelectSpineIndex(onSelectSpineIndex) {}
|
||||||
|
void onEnter() override;
|
||||||
|
void onExit() override;
|
||||||
|
void handleInput() override;
|
||||||
|
};
|
||||||
@ -5,6 +5,7 @@
|
|||||||
#include <SD.h>
|
#include <SD.h>
|
||||||
|
|
||||||
#include "Battery.h"
|
#include "Battery.h"
|
||||||
|
#include "EpubReaderChapterSelectionScreen.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
constexpr int PAGES_PER_REFRESH = 15;
|
constexpr int PAGES_PER_REFRESH = 15;
|
||||||
@ -65,6 +66,37 @@ void EpubReaderScreen::onExit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderScreen::handleInput() {
|
void EpubReaderScreen::handleInput() {
|
||||||
|
// Pass input responsibility to sub screen if exists
|
||||||
|
if (subScreen) {
|
||||||
|
subScreen->handleInput();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enter chapter selection screen
|
||||||
|
if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
|
||||||
|
// Don't start screen transition while rendering
|
||||||
|
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||||
|
subScreen.reset(new EpubReaderChapterSelectionScreen(
|
||||||
|
this->renderer, this->inputManager, epub, currentSpineIndex,
|
||||||
|
[this] {
|
||||||
|
subScreen->onExit();
|
||||||
|
subScreen.reset();
|
||||||
|
updateRequired = true;
|
||||||
|
},
|
||||||
|
[this](const int newSpineIndex) {
|
||||||
|
if (currentSpineIndex != newSpineIndex) {
|
||||||
|
currentSpineIndex = newSpineIndex;
|
||||||
|
nextPageNumber = 0;
|
||||||
|
section.reset();
|
||||||
|
}
|
||||||
|
subScreen->onExit();
|
||||||
|
subScreen.reset();
|
||||||
|
updateRequired = true;
|
||||||
|
}));
|
||||||
|
subScreen->onEnter();
|
||||||
|
xSemaphoreGive(renderingMutex);
|
||||||
|
}
|
||||||
|
|
||||||
if (inputManager.wasPressed(InputManager::BTN_BACK)) {
|
if (inputManager.wasPressed(InputManager::BTN_BACK)) {
|
||||||
onGoHome();
|
onGoHome();
|
||||||
return;
|
return;
|
||||||
|
|||||||
@ -12,6 +12,7 @@ class EpubReaderScreen final : public Screen {
|
|||||||
std::unique_ptr<Section> section = nullptr;
|
std::unique_ptr<Section> section = nullptr;
|
||||||
TaskHandle_t displayTaskHandle = nullptr;
|
TaskHandle_t displayTaskHandle = nullptr;
|
||||||
SemaphoreHandle_t renderingMutex = nullptr;
|
SemaphoreHandle_t renderingMutex = nullptr;
|
||||||
|
std::unique_ptr<Screen> subScreen = nullptr;
|
||||||
int currentSpineIndex = 0;
|
int currentSpineIndex = 0;
|
||||||
int nextPageNumber = 0;
|
int nextPageNumber = 0;
|
||||||
int pagesUntilFullRefresh = 0;
|
int pagesUntilFullRefresh = 0;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user