87 lines
2.7 KiB
C++
87 lines
2.7 KiB
C++
#include "LanguageSelectActivity.h"
|
|
|
|
#include <GfxRenderer.h>
|
|
#include <I18n.h>
|
|
|
|
#include <algorithm>
|
|
#include <iterator>
|
|
|
|
#include "I18nKeys.h"
|
|
#include "MappedInputManager.h"
|
|
#include "fontIds.h"
|
|
|
|
void LanguageSelectActivity::onEnter() {
|
|
Activity::onEnter();
|
|
|
|
// Set current selection based on current language
|
|
const auto currentLang = static_cast<uint8_t>(I18N.getLanguage());
|
|
const auto* begin = std::begin(SORTED_LANGUAGE_INDICES);
|
|
const auto* end = std::end(SORTED_LANGUAGE_INDICES);
|
|
const auto* it = std::find(begin, end, currentLang);
|
|
selectedIndex = (it != end) ? std::distance(begin, it) : 0;
|
|
|
|
requestUpdate();
|
|
}
|
|
|
|
void LanguageSelectActivity::onExit() { Activity::onExit(); }
|
|
|
|
void LanguageSelectActivity::loop() {
|
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
|
onBack();
|
|
return;
|
|
}
|
|
|
|
if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
|
|
handleSelection();
|
|
return;
|
|
}
|
|
|
|
// Handle navigation
|
|
buttonNavigator.onNextRelease([this] {
|
|
selectedIndex = ButtonNavigator::nextIndex(static_cast<int>(selectedIndex), totalItems);
|
|
requestUpdate();
|
|
});
|
|
|
|
buttonNavigator.onPreviousRelease([this] {
|
|
selectedIndex = ButtonNavigator::previousIndex(static_cast<int>(selectedIndex), totalItems);
|
|
requestUpdate();
|
|
});
|
|
}
|
|
|
|
void LanguageSelectActivity::handleSelection() {
|
|
{
|
|
RenderLock lock(*this);
|
|
I18N.setLanguage(static_cast<Language>(SORTED_LANGUAGE_INDICES[selectedIndex]));
|
|
}
|
|
|
|
// Return to previous page
|
|
onBack();
|
|
}
|
|
|
|
void LanguageSelectActivity::render(RenderLock&&) {
|
|
renderer.clearScreen();
|
|
|
|
auto metrics = UITheme::getInstance().getMetrics();
|
|
const Rect contentRect = UITheme::getContentRect(renderer, true, false);
|
|
|
|
GUI.drawHeader(renderer, Rect{contentRect.x, metrics.topPadding, contentRect.width, metrics.headerHeight},
|
|
tr(STR_LANGUAGE));
|
|
|
|
// Current language marker
|
|
const int contentTop = metrics.topPadding + metrics.headerHeight + metrics.verticalSpacing;
|
|
const int contentHeight = contentRect.height - contentTop - metrics.verticalSpacing;
|
|
const auto currentLang = static_cast<uint8_t>(I18N.getLanguage());
|
|
GUI.drawList(
|
|
renderer, Rect{contentRect.x, contentTop, contentRect.width, contentHeight}, totalItems, selectedIndex,
|
|
[this](int index) { return I18N.getLanguageName(static_cast<Language>(SORTED_LANGUAGE_INDICES[index])); },
|
|
nullptr, nullptr,
|
|
[this, currentLang](int index) { return SORTED_LANGUAGE_INDICES[index] == currentLang ? tr(STR_SELECTED) : ""; },
|
|
true);
|
|
|
|
// Button hints
|
|
const auto labels = mappedInput.mapLabels(tr(STR_BACK), tr(STR_SELECT), tr(STR_DIR_UP), tr(STR_DIR_DOWN));
|
|
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
|
|
|
renderer.displayBuffer();
|
|
}
|