feat(menu): add menu class
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#define MENU_ITEM_COUNT 4
|
||||
#include <Arduino.h>
|
||||
#include "joystick.hpp"
|
||||
|
||||
typedef struct {
|
||||
String name;
|
||||
void (*action)();
|
||||
} Item;
|
||||
|
||||
class Menu {
|
||||
public:
|
||||
Menu(Item* items);
|
||||
void updateCurrentItem(JoystickDirection& direction);
|
||||
void execute();
|
||||
|
||||
Item& getItemAt(size_t index);
|
||||
size_t getCurrentItemIndex() const;
|
||||
private:
|
||||
Item items[MENU_ITEM_COUNT];
|
||||
int currentItem;
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "menu.hpp"
|
||||
|
||||
Menu::Menu(Item* items) : currentItem(0) {
|
||||
for (int i = 0; i < MENU_ITEM_COUNT; i++) {
|
||||
this->items[i] = items[i];
|
||||
}
|
||||
}
|
||||
|
||||
void Menu::updateCurrentItem(JoystickDirection &direction) {
|
||||
switch (direction) {
|
||||
case JoystickDirection::UP:
|
||||
currentItem = (currentItem - 1 + MENU_ITEM_COUNT) % MENU_ITEM_COUNT;
|
||||
break;
|
||||
case JoystickDirection::DOWN:
|
||||
currentItem = (currentItem + 1) % MENU_ITEM_COUNT;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Menu::execute() {
|
||||
if (items[currentItem].action) {
|
||||
items[currentItem].action();
|
||||
}
|
||||
}
|
||||
|
||||
Item& Menu::getItemAt(size_t index) {
|
||||
return items[index];
|
||||
}
|
||||
|
||||
size_t Menu::getCurrentItemIndex() const {
|
||||
return currentItem;
|
||||
}
|
||||
Reference in New Issue
Block a user