Introduce common helper functions

This commit is contained in:
jpirnay
2026-04-12 20:30:14 +02:00
parent df3fec574d
commit 54807911e7
16 changed files with 790 additions and 729 deletions
+70
View File
@@ -0,0 +1,70 @@
#include "MenuListActivity.h"
#include <I18n.h>
#include "MappedInputManager.h"
#include "components/UITheme.h"
void MenuListActivity::initMenuList() {
const int count = static_cast<int>(menuItems.size());
const auto pred = UITheme::makeSelectablePredicate(count, [this](int i) { return menuItems[i].getTitle(); });
buttonNavigator.setSelectablePredicate(pred, count);
if (count > 0 && !pred(selectedIndex)) {
selectedIndex = buttonNavigator.nextIndex(selectedIndex);
}
}
void MenuListActivity::onEnter() {
Activity::onEnter();
initMenuList();
requestUpdate();
}
void MenuListActivity::handleNavigation() {
buttonNavigator.onNext([this] {
selectedIndex = buttonNavigator.nextIndex(selectedIndex);
requestUpdate();
});
buttonNavigator.onPrevious([this] {
selectedIndex = buttonNavigator.previousIndex(selectedIndex);
requestUpdate();
});
}
void MenuListActivity::toggleCurrentItem() {
if (selectedIndex < 0 || selectedIndex >= static_cast<int>(menuItems.size())) return;
auto& item = menuItems[selectedIndex];
if (item.isSeparator) return;
if (item.type == SettingType::ACTION) {
onActionSelected(selectedIndex);
return;
}
item.toggleValue();
onSettingToggled(selectedIndex);
requestUpdate();
}
std::string MenuListActivity::getItemValueString(int index) const {
return menuItems[index].getDisplayValue();
}
void MenuListActivity::drawMenuList(const Rect& rect) {
const int count = static_cast<int>(menuItems.size());
GUI.drawList(
renderer, rect, count, selectedIndex, [this](int index) { return menuItems[index].getTitle(); }, nullptr, nullptr,
[this](int index) { return getItemValueString(index); }, true);
}
void MenuListActivity::loop() {
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
onBackPressed();
return;
}
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
toggleCurrentItem();
return;
}
handleNavigation();
}