- GameLauncher now displays only 4 games per page to keep menu in bounds - Added page navigation with page indicator (Page X/Y) - KEY0 navigates between pages and within page - KEY1 selects the highlighted game - Touch selection works on current page only - Helper methods: get_total_pages(), get_page_start_index(), get_page_end_index() - Updated both lib/ and emulator/ versions for consistency
147 lines
5.3 KiB
C++
147 lines
5.3 KiB
C++
// Copy of game_launcher.cpp for emulator build
|
|
#include "game_launcher.h"
|
|
#include "input_manager.h"
|
|
#include "../display/low_level_render.h"
|
|
#include "../display/low_level_gui.h"
|
|
#include <stdio.h>
|
|
extern Font font_5x5_obj;
|
|
GameLauncher::GameLauncher(uint16_t width, uint16_t height, LowLevelRenderer* renderer, LowLevelGUI* gui, InputManager* input_manager)
|
|
: width(width), height(height), renderer(renderer), gui(gui), input_manager(input_manager),
|
|
selected_index(0), selected_game(nullptr), current_page(0) {}
|
|
void GameLauncher::register_game(const char* name, const char* description,
|
|
std::function<Game*(uint16_t, uint16_t, LowLevelRenderer*, LowLevelGUI*, InputManager*)> factory) {
|
|
GameEntry entry;
|
|
entry.name = name;
|
|
entry.description = description;
|
|
entry.factory = factory;
|
|
games.push_back(entry);
|
|
printf("Registered game: %s - %s\n", name, description);
|
|
}
|
|
void GameLauncher::draw() {
|
|
LowLevelWindow* window = gui->draw_new_window(10, 10, width - 20, height - 20, "Game Launcher");
|
|
renderer->set_font(&font_5x5_obj);
|
|
|
|
int total_pages = get_total_pages();
|
|
char title[64];
|
|
snprintf(title, sizeof(title), "Select a Game: (Page %d/%d)", current_page + 1, total_pages);
|
|
renderer->draw_string_scaled(30, 40, title, 2);
|
|
|
|
int page_start = get_page_start_index();
|
|
int page_end = get_page_end_index();
|
|
|
|
for (int i = page_start; i < page_end && i < (int)games.size(); i++) {
|
|
int item_index = i - page_start;
|
|
int y = MENU_Y_START + (item_index * MENU_ITEM_HEIGHT);
|
|
bool is_selected = (i == selected_index);
|
|
gui->draw_button(window, 20, y, games[i].name, is_selected, true);
|
|
renderer->set_font(&font_5x5_obj);
|
|
renderer->set_text_color(true);
|
|
renderer->draw_string_scaled(50, y + 36, games[i].description, 1);
|
|
}
|
|
|
|
char nav_text[128];
|
|
if (total_pages > 1) {
|
|
snprintf(nav_text, sizeof(nav_text), "KEY0: Next Page | KEY1: Select");
|
|
} else {
|
|
snprintf(nav_text, sizeof(nav_text), "KEY0: Navigate | KEY1: Select");
|
|
}
|
|
|
|
const char* instructions;
|
|
if (input_manager->has_buttons()) {
|
|
instructions = nav_text;
|
|
} else {
|
|
instructions = "Touch game to play";
|
|
}
|
|
renderer->set_font(&font_5x5_obj);
|
|
renderer->draw_string_scaled(30, height - 35, instructions, 2);
|
|
}
|
|
bool GameLauncher::update(const InputEvent& event) {
|
|
bool needs_refresh = false;
|
|
switch (event.type) {
|
|
case INPUT_TOUCH_DOWN: {
|
|
int page_start = get_page_start_index();
|
|
int page_end = get_page_end_index();
|
|
|
|
for (int i = page_start; i < page_end && i < (int)games.size(); i++) {
|
|
int item_index = i - page_start;
|
|
int y = MENU_Y_START + (item_index * MENU_ITEM_HEIGHT);
|
|
if (event.y >= y - 5 && event.y < y + MENU_ITEM_HEIGHT - 5) {
|
|
selected_game = games[i].factory(width, height, renderer, gui, input_manager);
|
|
if (selected_game) {
|
|
selected_game->init();
|
|
return true;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case INPUT_BUTTON_0: {
|
|
int page_start = get_page_start_index();
|
|
int page_end = get_page_end_index();
|
|
int total_pages = get_total_pages();
|
|
|
|
if (page_end - page_start > 1) {
|
|
int old_index = selected_index;
|
|
selected_index++;
|
|
if (selected_index >= page_end) {
|
|
if (current_page + 1 < total_pages) {
|
|
current_page++;
|
|
selected_index = get_page_start_index();
|
|
} else {
|
|
current_page = 0;
|
|
selected_index = 0;
|
|
}
|
|
} else if (selected_index < page_start) {
|
|
selected_index = page_start;
|
|
}
|
|
} else {
|
|
if (current_page + 1 < total_pages) {
|
|
current_page++;
|
|
selected_index = get_page_start_index();
|
|
} else {
|
|
current_page = 0;
|
|
selected_index = 0;
|
|
}
|
|
}
|
|
needs_refresh = true;
|
|
break;
|
|
}
|
|
case INPUT_BUTTON_1: {
|
|
if (selected_index >= 0 && selected_index < (int)games.size()) {
|
|
selected_game = games[selected_index].factory(width, height, renderer, gui, input_manager);
|
|
if (selected_game) {
|
|
selected_game->init();
|
|
return true;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
return needs_refresh;
|
|
}
|
|
Game* GameLauncher::get_selected_game() { return selected_game; }
|
|
void GameLauncher::reset() {
|
|
if (selected_game) {
|
|
delete selected_game;
|
|
selected_game = nullptr;
|
|
}
|
|
selected_index = 0;
|
|
current_page = 0;
|
|
}
|
|
|
|
int GameLauncher::get_total_pages() const {
|
|
if (games.empty()) return 1;
|
|
return (games.size() + GAMES_PER_PAGE - 1) / GAMES_PER_PAGE;
|
|
}
|
|
|
|
int GameLauncher::get_page_start_index() const {
|
|
return current_page * GAMES_PER_PAGE;
|
|
}
|
|
|
|
int GameLauncher::get_page_end_index() const {
|
|
return (current_page + 1) * GAMES_PER_PAGE;
|
|
}
|