refactor MappedInputManager

This commit is contained in:
Arthur Tazhitdinov 2026-01-26 12:54:59 +05:00
parent 0952152879
commit f5db832853
5 changed files with 61 additions and 75 deletions

View File

@ -2,81 +2,72 @@
#include "CrossPointSettings.h" #include "CrossPointSettings.h"
decltype(InputManager::BTN_BACK) MappedInputManager::mapButton(const Button button) const { namespace {
using ButtonIndex = uint8_t;
struct FrontLayoutMap {
ButtonIndex back;
ButtonIndex confirm;
ButtonIndex left;
ButtonIndex right;
};
struct SideLayoutMap {
ButtonIndex pageBack;
ButtonIndex pageForward;
};
// Order matches CrossPointSettings::FRONT_BUTTON_LAYOUT.
constexpr FrontLayoutMap kFrontLayouts[] = {
{InputManager::BTN_BACK, InputManager::BTN_CONFIRM, InputManager::BTN_LEFT, InputManager::BTN_RIGHT},
{InputManager::BTN_LEFT, InputManager::BTN_RIGHT, InputManager::BTN_BACK, InputManager::BTN_CONFIRM},
{InputManager::BTN_CONFIRM, InputManager::BTN_LEFT, InputManager::BTN_BACK, InputManager::BTN_RIGHT},
};
// Order matches CrossPointSettings::SIDE_BUTTON_LAYOUT.
constexpr SideLayoutMap kSideLayouts[] = {
{InputManager::BTN_UP, InputManager::BTN_DOWN},
{InputManager::BTN_DOWN, InputManager::BTN_UP},
};
} // namespace
bool MappedInputManager::mapButton(const Button button, bool (InputManager::*fn)(uint8_t) const) const {
const auto frontLayout = static_cast<CrossPointSettings::FRONT_BUTTON_LAYOUT>(SETTINGS.frontButtonLayout); const auto frontLayout = static_cast<CrossPointSettings::FRONT_BUTTON_LAYOUT>(SETTINGS.frontButtonLayout);
const auto sideLayout = static_cast<CrossPointSettings::SIDE_BUTTON_LAYOUT>(SETTINGS.sideButtonLayout); const auto sideLayout = static_cast<CrossPointSettings::SIDE_BUTTON_LAYOUT>(SETTINGS.sideButtonLayout);
const auto& front = kFrontLayouts[frontLayout];
const auto& side = kSideLayouts[sideLayout];
switch (button) { switch (button) {
case Button::Back: case Button::Back:
switch (frontLayout) { return (inputManager.*fn)(front.back);
case CrossPointSettings::LEFT_RIGHT_BACK_CONFIRM:
return InputManager::BTN_LEFT;
case CrossPointSettings::LEFT_BACK_CONFIRM_RIGHT:
return InputManager::BTN_CONFIRM;
case CrossPointSettings::BACK_CONFIRM_LEFT_RIGHT:
default:
return InputManager::BTN_BACK;
}
case Button::Confirm: case Button::Confirm:
switch (frontLayout) { return (inputManager.*fn)(front.confirm);
case CrossPointSettings::LEFT_RIGHT_BACK_CONFIRM:
return InputManager::BTN_RIGHT;
case CrossPointSettings::LEFT_BACK_CONFIRM_RIGHT:
return InputManager::BTN_LEFT;
case CrossPointSettings::BACK_CONFIRM_LEFT_RIGHT:
default:
return InputManager::BTN_CONFIRM;
}
case Button::Left: case Button::Left:
switch (frontLayout) { return (inputManager.*fn)(front.left);
case CrossPointSettings::LEFT_RIGHT_BACK_CONFIRM:
case CrossPointSettings::LEFT_BACK_CONFIRM_RIGHT:
return InputManager::BTN_BACK;
case CrossPointSettings::BACK_CONFIRM_LEFT_RIGHT:
default:
return InputManager::BTN_LEFT;
}
case Button::Right: case Button::Right:
switch (frontLayout) { return (inputManager.*fn)(front.right);
case CrossPointSettings::LEFT_RIGHT_BACK_CONFIRM:
return InputManager::BTN_CONFIRM;
case CrossPointSettings::BACK_CONFIRM_LEFT_RIGHT:
case CrossPointSettings::LEFT_BACK_CONFIRM_RIGHT:
default:
return InputManager::BTN_RIGHT;
}
case Button::Up: case Button::Up:
return InputManager::BTN_UP; return (inputManager.*fn)(InputManager::BTN_UP);
case Button::Down: case Button::Down:
return InputManager::BTN_DOWN; return (inputManager.*fn)(InputManager::BTN_DOWN);
case Button::Power: case Button::Power:
return InputManager::BTN_POWER; return (inputManager.*fn)(InputManager::BTN_POWER);
case Button::PageBack: case Button::PageBack:
switch (sideLayout) { return (inputManager.*fn)(side.pageBack);
case CrossPointSettings::NEXT_PREV:
return InputManager::BTN_DOWN;
case CrossPointSettings::PREV_NEXT:
default:
return InputManager::BTN_UP;
}
case Button::PageForward: case Button::PageForward:
switch (sideLayout) { return (inputManager.*fn)(side.pageForward);
case CrossPointSettings::NEXT_PREV:
return InputManager::BTN_UP;
case CrossPointSettings::PREV_NEXT:
default:
return InputManager::BTN_DOWN;
}
} }
return InputManager::BTN_BACK; return false;
} }
bool MappedInputManager::wasPressed(const Button button) const { return inputManager.wasPressed(mapButton(button)); } bool MappedInputManager::wasPressed(const Button button) const { return mapButton(button, &InputManager::wasPressed); }
bool MappedInputManager::wasReleased(const Button button) const { return inputManager.wasReleased(mapButton(button)); } bool MappedInputManager::wasReleased(const Button button) const {
return mapButton(button, &InputManager::wasReleased);
}
bool MappedInputManager::isPressed(const Button button) const { return inputManager.isPressed(mapButton(button)); } bool MappedInputManager::isPressed(const Button button) const { return mapButton(button, &InputManager::isPressed); }
bool MappedInputManager::wasAnyPressed() const { return inputManager.wasAnyPressed(); } bool MappedInputManager::wasAnyPressed() const { return inputManager.wasAnyPressed(); }

View File

@ -25,5 +25,6 @@ class MappedInputManager {
private: private:
InputManager& inputManager; InputManager& inputManager;
decltype(InputManager::BTN_BACK) mapButton(Button button) const;
bool mapButton(Button button, bool (InputManager::*fn)(uint8_t) const) const;
}; };

View File

@ -169,14 +169,12 @@ void EpubReaderActivity::loop() {
mappedInput.wasPressed(MappedInputManager::Button::Left)) mappedInput.wasPressed(MappedInputManager::Button::Left))
: (mappedInput.wasReleased(MappedInputManager::Button::PageBack) || : (mappedInput.wasReleased(MappedInputManager::Button::PageBack) ||
mappedInput.wasReleased(MappedInputManager::Button::Left)); mappedInput.wasReleased(MappedInputManager::Button::Left));
const bool powerPageTurn = SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::PAGE_TURN &&
mappedInput.wasReleased(MappedInputManager::Button::Power);
const bool nextTriggered = usePressForPageTurn const bool nextTriggered = usePressForPageTurn
? (mappedInput.wasPressed(MappedInputManager::Button::PageForward) || ? (mappedInput.wasPressed(MappedInputManager::Button::PageForward) || powerPageTurn ||
(SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::PAGE_TURN &&
mappedInput.wasReleased(MappedInputManager::Button::Power)) ||
mappedInput.wasPressed(MappedInputManager::Button::Right)) mappedInput.wasPressed(MappedInputManager::Button::Right))
: (mappedInput.wasReleased(MappedInputManager::Button::PageForward) || : (mappedInput.wasReleased(MappedInputManager::Button::PageForward) || powerPageTurn ||
(SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::PAGE_TURN &&
mappedInput.wasReleased(MappedInputManager::Button::Power)) ||
mappedInput.wasReleased(MappedInputManager::Button::Right)); mappedInput.wasReleased(MappedInputManager::Button::Right));
if (!prevTriggered && !nextTriggered) { if (!prevTriggered && !nextTriggered) {

View File

@ -113,14 +113,12 @@ void TxtReaderActivity::loop() {
mappedInput.wasPressed(MappedInputManager::Button::Left)) mappedInput.wasPressed(MappedInputManager::Button::Left))
: (mappedInput.wasReleased(MappedInputManager::Button::PageBack) || : (mappedInput.wasReleased(MappedInputManager::Button::PageBack) ||
mappedInput.wasReleased(MappedInputManager::Button::Left)); mappedInput.wasReleased(MappedInputManager::Button::Left));
const bool powerPageTurn = SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::PAGE_TURN &&
mappedInput.wasReleased(MappedInputManager::Button::Power);
const bool nextTriggered = usePressForPageTurn const bool nextTriggered = usePressForPageTurn
? (mappedInput.wasPressed(MappedInputManager::Button::PageForward) || ? (mappedInput.wasPressed(MappedInputManager::Button::PageForward) || powerPageTurn ||
(SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::PAGE_TURN &&
mappedInput.wasReleased(MappedInputManager::Button::Power)) ||
mappedInput.wasPressed(MappedInputManager::Button::Right)) mappedInput.wasPressed(MappedInputManager::Button::Right))
: (mappedInput.wasReleased(MappedInputManager::Button::PageForward) || : (mappedInput.wasReleased(MappedInputManager::Button::PageForward) || powerPageTurn ||
(SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::PAGE_TURN &&
mappedInput.wasReleased(MappedInputManager::Button::Power)) ||
mappedInput.wasReleased(MappedInputManager::Button::Right)); mappedInput.wasReleased(MappedInputManager::Button::Right));
if (!prevTriggered && !nextTriggered) { if (!prevTriggered && !nextTriggered) {

View File

@ -117,14 +117,12 @@ void XtcReaderActivity::loop() {
mappedInput.wasPressed(MappedInputManager::Button::Left)) mappedInput.wasPressed(MappedInputManager::Button::Left))
: (mappedInput.wasReleased(MappedInputManager::Button::PageBack) || : (mappedInput.wasReleased(MappedInputManager::Button::PageBack) ||
mappedInput.wasReleased(MappedInputManager::Button::Left)); mappedInput.wasReleased(MappedInputManager::Button::Left));
const bool powerPageTurn = SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::PAGE_TURN &&
mappedInput.wasReleased(MappedInputManager::Button::Power);
const bool nextTriggered = usePressForPageTurn const bool nextTriggered = usePressForPageTurn
? (mappedInput.wasPressed(MappedInputManager::Button::PageForward) || ? (mappedInput.wasPressed(MappedInputManager::Button::PageForward) || powerPageTurn ||
(SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::PAGE_TURN &&
mappedInput.wasReleased(MappedInputManager::Button::Power)) ||
mappedInput.wasPressed(MappedInputManager::Button::Right)) mappedInput.wasPressed(MappedInputManager::Button::Right))
: (mappedInput.wasReleased(MappedInputManager::Button::PageForward) || : (mappedInput.wasReleased(MappedInputManager::Button::PageForward) || powerPageTurn ||
(SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::PAGE_TURN &&
mappedInput.wasReleased(MappedInputManager::Button::Power)) ||
mappedInput.wasReleased(MappedInputManager::Button::Right)); mappedInput.wasReleased(MappedInputManager::Button::Right));
if (!prevTriggered && !nextTriggered) { if (!prevTriggered && !nextTriggered) {