Baseline: Migrated from local arduino IDE project, compiles but contains dead code and needs refactoring
This commit is contained in:
92
src/Menu.cpp
Normal file
92
src/Menu.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
#include "Menu.h"
|
||||
#include "Programs/Snake/Snake.h"
|
||||
#include "Programs/Settings/Settings.h"
|
||||
#include "Programs/Asteroids/Asteroids.h"
|
||||
#include "Programs/Calculator/Calculator.h"
|
||||
|
||||
// ... include headers for your other programs here
|
||||
void requestProgramChange(Program* program);
|
||||
|
||||
namespace Menu {
|
||||
|
||||
// --- 1. Define the MenuItem struct ---
|
||||
struct MenuItem {
|
||||
const char* text;
|
||||
Program* program;
|
||||
};
|
||||
|
||||
// --- 2. Create the menuItems array ---
|
||||
MenuItem menuItems[] = {
|
||||
{"Snake", new Snake::Snake()},
|
||||
{"Settings", new Settings::Settings()},
|
||||
{"Asteroids", new Asteroids::Asteroids()},
|
||||
{"Calculator", new Calculator::Calculator()}
|
||||
};
|
||||
|
||||
const int TOTAL_MENU_ITEMS = sizeof(menuItems) / sizeof(menuItems[0]);
|
||||
const int MAX_VISIBLE_ITEMS = 6;
|
||||
const int FONT_HEIGHT = 8;
|
||||
|
||||
// --- 3. Implement the Menu class methods ---
|
||||
|
||||
int selectedIndex = 0;
|
||||
int viewOffset = 0;
|
||||
|
||||
void Menu::setup() {
|
||||
// Not needed for the menu
|
||||
}
|
||||
|
||||
void Menu::onEnter(Adafruit_SSD1351 *tft) {
|
||||
// Reset the menu state when we enter it
|
||||
selectedIndex = 0;
|
||||
viewOffset = 0;
|
||||
Menu::disableInfoBar = false;
|
||||
tft->fillRect(0, 0, 128, 128, BLACK);
|
||||
}
|
||||
|
||||
void Menu::onExit() {
|
||||
// Not needed for the menu
|
||||
}
|
||||
|
||||
void Menu::loop(Adafruit_SSD1351 *tft, inputData *userInputs) {
|
||||
// --- Input Handling ---
|
||||
static int previousJoystickCommand = COMMAND_NO;
|
||||
int currentJoystickCommand = userInputs->joystickCommand;
|
||||
|
||||
if ((currentJoystickCommand & COMMAND_DOWN) && !(previousJoystickCommand & COMMAND_DOWN)) {
|
||||
selectedIndex++;
|
||||
if (selectedIndex >= TOTAL_MENU_ITEMS) {
|
||||
selectedIndex = 0;
|
||||
}
|
||||
}
|
||||
if ((currentJoystickCommand & COMMAND_UP) && !(previousJoystickCommand & COMMAND_UP)) {
|
||||
selectedIndex--;
|
||||
if (selectedIndex < 0) {
|
||||
selectedIndex = TOTAL_MENU_ITEMS - 1;
|
||||
}
|
||||
}
|
||||
|
||||
previousJoystickCommand = currentJoystickCommand;
|
||||
|
||||
// --- Drawing ---
|
||||
for (int i = 0; i < MAX_VISIBLE_ITEMS; i++) {
|
||||
int itemIndex = i + viewOffset;
|
||||
if (itemIndex < TOTAL_MENU_ITEMS) {
|
||||
int yPos = 20 + i * (FONT_HEIGHT + 2);
|
||||
tft->setCursor(10, yPos);
|
||||
if (itemIndex == selectedIndex) {
|
||||
tft->setTextColor(BLACK, WHITE);
|
||||
} else {
|
||||
tft->setTextColor(WHITE, BLACK);
|
||||
}
|
||||
tft->println(menuItems[itemIndex].text);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Program Switching ---
|
||||
if (userInputs->buttonTop.isReleased()) {
|
||||
requestProgramChange(menuItems[selectedIndex].program);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user