80 lines
2.4 KiB
C
80 lines
2.4 KiB
C
|
|
#pragma once
|
||
|
|
#include <Epub/Page.h>
|
||
|
|
#include <freertos/FreeRTOS.h>
|
||
|
|
#include <freertos/semphr.h>
|
||
|
|
#include <freertos/task.h>
|
||
|
|
|
||
|
|
#include <functional>
|
||
|
|
#include <memory>
|
||
|
|
#include <string>
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
#include "../Activity.h"
|
||
|
|
|
||
|
|
/**
|
||
|
|
* EpubWordSelectionActivity allows selecting a word from the current EPUB page.
|
||
|
|
* Displays the page with a cursor that can navigate between words.
|
||
|
|
*/
|
||
|
|
class EpubWordSelectionActivity final : public Activity {
|
||
|
|
// Word info for selection
|
||
|
|
struct WordInfo {
|
||
|
|
std::string text; // Named 'text' to avoid Arduino's 'word' macro
|
||
|
|
int16_t x;
|
||
|
|
int16_t y;
|
||
|
|
int16_t width;
|
||
|
|
int16_t height;
|
||
|
|
EpdFontFamily::Style style;
|
||
|
|
};
|
||
|
|
|
||
|
|
TaskHandle_t displayTaskHandle = nullptr;
|
||
|
|
SemaphoreHandle_t renderingMutex = nullptr;
|
||
|
|
bool updateRequired = false;
|
||
|
|
|
||
|
|
const std::unique_ptr<Page> page;
|
||
|
|
const int fontId;
|
||
|
|
const int xOffset;
|
||
|
|
const int yOffset;
|
||
|
|
const std::function<void(const std::string&)> onWordSelected;
|
||
|
|
const std::function<void()> onCancel;
|
||
|
|
|
||
|
|
// Word navigation state
|
||
|
|
std::vector<WordInfo> allWords;
|
||
|
|
int selectedWordIndex = 0;
|
||
|
|
int currentLineIndex = 0;
|
||
|
|
|
||
|
|
static void taskTrampoline(void* param);
|
||
|
|
[[noreturn]] void displayTaskLoop();
|
||
|
|
void render() const;
|
||
|
|
void buildWordList();
|
||
|
|
int findWordIndexForLine(int lineIndex) const;
|
||
|
|
int findLineForWordIndex(int wordIndex) const;
|
||
|
|
|
||
|
|
public:
|
||
|
|
/**
|
||
|
|
* Constructor
|
||
|
|
* @param renderer Graphics renderer
|
||
|
|
* @param mappedInput Input manager
|
||
|
|
* @param page The current page to select words from (ownership transferred)
|
||
|
|
* @param fontId Font ID used for rendering
|
||
|
|
* @param xOffset X offset for rendering
|
||
|
|
* @param yOffset Y offset for rendering
|
||
|
|
* @param onWordSelected Callback when a word is selected
|
||
|
|
* @param onCancel Callback when selection is cancelled
|
||
|
|
*/
|
||
|
|
explicit EpubWordSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::unique_ptr<Page> page,
|
||
|
|
int fontId, int xOffset, int yOffset,
|
||
|
|
const std::function<void(const std::string&)>& onWordSelected,
|
||
|
|
const std::function<void()>& onCancel)
|
||
|
|
: Activity("EpubWordSelection", renderer, mappedInput),
|
||
|
|
page(std::move(page)),
|
||
|
|
fontId(fontId),
|
||
|
|
xOffset(xOffset),
|
||
|
|
yOffset(yOffset),
|
||
|
|
onWordSelected(onWordSelected),
|
||
|
|
onCancel(onCancel) {}
|
||
|
|
|
||
|
|
void onEnter() override;
|
||
|
|
void onExit() override;
|
||
|
|
void loop() override;
|
||
|
|
};
|