Paginate book list and avoid out of bounds rendering
This commit is contained in:
parent
0d32d21d75
commit
dfd7ac2a1a
@ -5,8 +5,10 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
constexpr int PAGE_ITEMS = 24;
|
constexpr int PAGE_ITEMS = 24;
|
||||||
constexpr int SKIP_PAGE_MS = 700;
|
constexpr int SKIP_PAGE_MS = 700;
|
||||||
|
} // namespace
|
||||||
|
|
||||||
void EpubReaderChapterSelectionActivity::taskTrampoline(void* param) {
|
void EpubReaderChapterSelectionActivity::taskTrampoline(void* param) {
|
||||||
auto* self = static_cast<EpubReaderChapterSelectionActivity*>(param);
|
auto* self = static_cast<EpubReaderChapterSelectionActivity*>(param);
|
||||||
|
|||||||
@ -5,6 +5,11 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
constexpr int PAGE_ITEMS = 23;
|
||||||
|
constexpr int SKIP_PAGE_MS = 700;
|
||||||
|
} // namespace
|
||||||
|
|
||||||
void sortFileList(std::vector<std::string>& strs) {
|
void sortFileList(std::vector<std::string>& strs) {
|
||||||
std::sort(begin(strs), end(strs), [](const std::string& str1, const std::string& str2) {
|
std::sort(begin(strs), end(strs), [](const std::string& str1, const std::string& str2) {
|
||||||
if (str1.back() == '/' && str2.back() != '/') return true;
|
if (str1.back() == '/' && str2.back() != '/') return true;
|
||||||
@ -73,10 +78,12 @@ void FileSelectionActivity::onExit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void FileSelectionActivity::loop() {
|
void FileSelectionActivity::loop() {
|
||||||
const bool prevPressed =
|
const bool prevReleased =
|
||||||
inputManager.wasPressed(InputManager::BTN_UP) || inputManager.wasPressed(InputManager::BTN_LEFT);
|
inputManager.wasReleased(InputManager::BTN_UP) || inputManager.wasReleased(InputManager::BTN_LEFT);
|
||||||
const bool nextPressed =
|
const bool nextReleased =
|
||||||
inputManager.wasPressed(InputManager::BTN_DOWN) || inputManager.wasPressed(InputManager::BTN_RIGHT);
|
inputManager.wasReleased(InputManager::BTN_DOWN) || inputManager.wasReleased(InputManager::BTN_RIGHT);
|
||||||
|
|
||||||
|
const bool skipPage = inputManager.getHeldTime() > SKIP_PAGE_MS;
|
||||||
|
|
||||||
if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
|
if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
|
||||||
if (files.empty()) {
|
if (files.empty()) {
|
||||||
@ -101,11 +108,19 @@ void FileSelectionActivity::loop() {
|
|||||||
// At root level, go back home
|
// At root level, go back home
|
||||||
onGoHome();
|
onGoHome();
|
||||||
}
|
}
|
||||||
} else if (prevPressed) {
|
} else if (prevReleased) {
|
||||||
selectorIndex = (selectorIndex + files.size() - 1) % files.size();
|
if (skipPage) {
|
||||||
|
selectorIndex = ((selectorIndex / PAGE_ITEMS - 1) * PAGE_ITEMS + files.size()) % files.size();
|
||||||
|
} else {
|
||||||
|
selectorIndex = (selectorIndex + files.size() - 1) % files.size();
|
||||||
|
}
|
||||||
updateRequired = true;
|
updateRequired = true;
|
||||||
} else if (nextPressed) {
|
} else if (nextReleased) {
|
||||||
selectorIndex = (selectorIndex + 1) % files.size();
|
if (skipPage) {
|
||||||
|
selectorIndex = ((selectorIndex / PAGE_ITEMS + 1) * PAGE_ITEMS) % files.size();
|
||||||
|
} else {
|
||||||
|
selectorIndex = (selectorIndex + 1) % files.size();
|
||||||
|
}
|
||||||
updateRequired = true;
|
updateRequired = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -126,21 +141,27 @@ void FileSelectionActivity::render() const {
|
|||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
|
|
||||||
const auto pageWidth = GfxRenderer::getScreenWidth();
|
const auto pageWidth = GfxRenderer::getScreenWidth();
|
||||||
renderer.drawCenteredText(READER_FONT_ID, 10, "CrossPoint Reader", true, BOLD);
|
renderer.drawCenteredText(READER_FONT_ID, 10, "Books", true, BOLD);
|
||||||
|
|
||||||
// Help text
|
// Help text
|
||||||
renderer.drawText(SMALL_FONT_ID, 20, GfxRenderer::getScreenHeight() - 30, "Press BACK for Home");
|
renderer.drawText(SMALL_FONT_ID, 20, GfxRenderer::getScreenHeight() - 30, "Press BACK for Home");
|
||||||
|
|
||||||
if (files.empty()) {
|
if (files.empty()) {
|
||||||
renderer.drawText(UI_FONT_ID, 20, 60, "No EPUBs found");
|
renderer.drawText(UI_FONT_ID, 20, 60, "No EPUBs found");
|
||||||
} else {
|
renderer.displayBuffer();
|
||||||
// Draw selection
|
return;
|
||||||
renderer.fillRect(0, 60 + selectorIndex * 30 + 2, pageWidth - 1, 30);
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < files.size(); i++) {
|
const auto pageStartIndex = selectorIndex / PAGE_ITEMS * PAGE_ITEMS;
|
||||||
const auto file = files[i];
|
renderer.fillRect(0, 60 + (selectorIndex % PAGE_ITEMS) * 30 + 2, pageWidth - 1, 30);
|
||||||
renderer.drawText(UI_FONT_ID, 20, 60 + i * 30, file.c_str(), i != selectorIndex);
|
for (int i = pageStartIndex; i < files.size() && i < pageStartIndex + PAGE_ITEMS; i++) {
|
||||||
|
auto item = files[i];
|
||||||
|
int itemWidth = renderer.getTextWidth(UI_FONT_ID, item.c_str());
|
||||||
|
while (itemWidth > renderer.getScreenWidth() - 40 && item.length() > 8) {
|
||||||
|
item.replace(item.length() - 5, 5, "...");
|
||||||
|
itemWidth = renderer.getTextWidth(UI_FONT_ID, item.c_str());
|
||||||
}
|
}
|
||||||
|
renderer.drawText(UI_FONT_ID, 20, 60 + (i % PAGE_ITEMS) * 30, item.c_str(), i != selectorIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
renderer.displayBuffer();
|
renderer.displayBuffer();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user