Login
1 branch 0 tags
Ben (Desktop/Arch) Navigation/metadata 7c7ae19 1 month ago 20 Commits
moon / firmware / src / screens / games_screen.c
#include "games_screen.h"
#include <stdlib.h>

static void list_event_cb(lv_event_t *e) {
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED) {
        lv_obj_t *btn = lv_event_get_target(e);
        screen_type_t screen = (screen_type_t)(uintptr_t)lv_obj_get_user_data(btn);
        navigate_to(screen);
    }
}

static void cancel_event_cb(lv_event_t *e) {
    (void)e;
    navigate_to(SCREEN_HOME);
}

ui_state_t setup_games_screen(void) {
    games_screen_state_t *state = calloc(1, sizeof(games_screen_state_t));

    // Create a list that fills the screen
    state->list = lv_list_create(lv_screen_active());
    lv_obj_set_size(state->list, LV_PCT(100), LV_PCT(100));
    lv_obj_center(state->list);

    // Add list header with styled background
    lv_obj_t *header = lv_list_add_text(state->list, "Games");
    lv_obj_set_style_bg_color(header, lv_color_hex(0x0288D1), 0);
    lv_obj_set_style_bg_opa(header, LV_OPA_COVER, 0);
    lv_obj_set_style_text_color(header, lv_color_hex(0xFFFFFF), 0);

    // Add Demo entry
    lv_obj_t *demo_btn = lv_list_add_button(state->list, LV_SYMBOL_PLAY, "Demo");
    lv_obj_set_user_data(demo_btn, (void *)(uintptr_t)SCREEN_DEMO);
    lv_obj_add_event_cb(demo_btn, list_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(demo_btn, cancel_event_cb, LV_EVENT_CANCEL, NULL);

    // Add Breakout entry
    lv_obj_t *breakout_btn = lv_list_add_button(state->list, LV_SYMBOL_IMAGE, "Breakout");
    lv_obj_set_user_data(breakout_btn, (void *)(uintptr_t)SCREEN_BREAKOUT);
    lv_obj_add_event_cb(breakout_btn, list_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(breakout_btn, cancel_event_cb, LV_EVENT_CANCEL, NULL);

    // Add Back entry
    lv_obj_t *back_btn = lv_list_add_button(state->list, LV_SYMBOL_LEFT, "Back");
    lv_obj_set_user_data(back_btn, (void *)(uintptr_t)SCREEN_HOME);
    lv_obj_add_event_cb(back_btn, list_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(back_btn, cancel_event_cb, LV_EVENT_CANCEL, NULL);

    // Add all buttons to group for keyboard navigation
    lv_group_add_obj(lv_group_get_default(), demo_btn);
    lv_group_add_obj(lv_group_get_default(), breakout_btn);
    lv_group_add_obj(lv_group_get_default(), back_btn);
    lv_group_focus_obj(demo_btn);

    return (ui_state_t){.type = SCREEN_GAMES, .games = state};
}

void free_games_screen(games_screen_state_t *state) {
    if (!state) return;

    // Get all buttons from list and remove from group
    uint32_t child_count = lv_obj_get_child_count(state->list);
    for (uint32_t i = 0; i < child_count; i++) {
        lv_obj_t *child = lv_obj_get_child(state->list, i);
        lv_group_remove_obj(child);
    }

    lv_obj_delete(state->list);
    free(state);
}

void update_games_screen(games_screen_state_t *state) {
    (void)state;  // LVGL handles updates via timer
}