107 lines
3.5 KiB
C++
107 lines
3.5 KiB
C++
#include "EpubReaderPercentSelectionActivity.h"
|
|
|
|
#include <GfxRenderer.h>
|
|
#include <I18n.h>
|
|
|
|
#include "MappedInputManager.h"
|
|
#include "components/UITheme.h"
|
|
#include "fontIds.h"
|
|
|
|
namespace {
|
|
// Fine/coarse slider step sizes for percent adjustments.
|
|
constexpr int kSmallStep = 1;
|
|
constexpr int kLargeStep = 10;
|
|
} // namespace
|
|
|
|
void EpubReaderPercentSelectionActivity::onEnter() {
|
|
Activity::onEnter();
|
|
// Set up rendering task and mark first frame dirty.
|
|
requestUpdate();
|
|
}
|
|
|
|
void EpubReaderPercentSelectionActivity::onExit() { Activity::onExit(); }
|
|
|
|
void EpubReaderPercentSelectionActivity::adjustPercent(const int delta) {
|
|
// Apply delta and clamp within 0-100.
|
|
percent += delta;
|
|
if (percent < 0) {
|
|
percent = 0;
|
|
} else if (percent > 100) {
|
|
percent = 100;
|
|
}
|
|
requestUpdate();
|
|
}
|
|
|
|
void EpubReaderPercentSelectionActivity::loop() {
|
|
// Back cancels, confirm selects, arrows adjust the percent.
|
|
ButtonEventManager::ButtonEvent ev;
|
|
while (buttonEvents.consumeEvent(ev)) {
|
|
if (ev.button == MappedInputManager::Button::Back && ev.type == ButtonEventManager::PressType::Short) {
|
|
ActivityResult result;
|
|
result.isCancelled = true;
|
|
setResult(std::move(result));
|
|
finish();
|
|
return;
|
|
}
|
|
|
|
if (ev.button == MappedInputManager::Button::Confirm && ev.type == ButtonEventManager::PressType::Short) {
|
|
setResult(PercentResult{percent});
|
|
finish();
|
|
return;
|
|
}
|
|
|
|
if ((ev.button == MappedInputManager::Button::PageBack || ev.button == MappedInputManager::Button::Left) &&
|
|
ev.type == ButtonEventManager::PressType::Short) {
|
|
adjustPercent(-kSmallStep);
|
|
return;
|
|
}
|
|
|
|
if ((ev.button == MappedInputManager::Button::PageForward || ev.button == MappedInputManager::Button::Right) &&
|
|
ev.type == ButtonEventManager::PressType::Short) {
|
|
adjustPercent(kSmallStep);
|
|
return;
|
|
}
|
|
}
|
|
|
|
buttonNavigator.onPressAndContinuous({MappedInputManager::Button::Up}, [this] { adjustPercent(kLargeStep); });
|
|
buttonNavigator.onPressAndContinuous({MappedInputManager::Button::Down}, [this] { adjustPercent(-kLargeStep); });
|
|
}
|
|
|
|
void EpubReaderPercentSelectionActivity::render(RenderLock&&) {
|
|
renderer.clearScreen();
|
|
|
|
// Title and numeric percent value.
|
|
renderer.drawCenteredText(UI_12_FONT_ID, 15, tr(STR_GO_TO_PERCENT), true, EpdFontFamily::BOLD);
|
|
|
|
const std::string percentText = std::to_string(percent) + "%";
|
|
renderer.drawCenteredText(UI_12_FONT_ID, 90, percentText.c_str(), true, EpdFontFamily::BOLD);
|
|
|
|
// Draw slider track.
|
|
const Rect contentRect = UITheme::getContentRect(renderer, true, false);
|
|
constexpr int barWidth = 360;
|
|
constexpr int barHeight = 16;
|
|
const int barX = contentRect.x + (contentRect.width - barWidth) / 2;
|
|
const int barY = 140;
|
|
|
|
renderer.drawRect(barX, barY, barWidth, barHeight);
|
|
|
|
// Fill slider based on percent.
|
|
const int fillWidth = (barWidth - 4) * percent / 100;
|
|
if (fillWidth > 0) {
|
|
renderer.fillRect(barX + 2, barY + 2, fillWidth, barHeight - 4);
|
|
}
|
|
|
|
// Draw a simple knob centered at the current percent.
|
|
const int knobX = barX + 2 + fillWidth - 2;
|
|
renderer.fillRect(knobX, barY - 4, 4, barHeight + 8, true);
|
|
|
|
// Hint text for step sizes.
|
|
renderer.drawCenteredText(SMALL_FONT_ID, barY + 30, tr(STR_PERCENT_STEP_HINT), true);
|
|
|
|
// Button hints follow the current front button layout.
|
|
const auto labels = mappedInput.mapLabels(tr(STR_BACK), tr(STR_SELECT), "-", "+");
|
|
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
|
|
|
renderer.displayBuffer();
|
|
}
|