nesemu/gui/components/window_menu.h

111 lines
2.9 KiB
C

//
// Created by william on 8/23/24.
//
#ifndef NES_EMULATOR_WINDOW_MENU_H
#define NES_EMULATOR_WINDOW_MENU_H
#include "window.h"
#include "linked_list.h"
#include "component.h"
#define MENU_HEIGHT 24
#define MENU_VISIBLE_HEIGHT 64;
#define MENU_BACKGROUND_COLOR 0xff353535
#define MENU_HIGHLIGHT_COLOR 0xff4d4d4d
#define MENU_TEXT_COLOR {0xff, 0xff, 0xff}
#define MENU_ITEM_MARGIN_X 20
#define MENU_ITEM_MARGIN_Y 4
typedef void (*on_click_callback)();
typedef struct menu_item_component {
char *label;
on_click_callback on_click;
bool is_highlighted;
LinkedList sub_items;
SDL_Texture *label_texture;
SDL_Rect draw_rect;
SDL_Rect collision_rect;
} MenuItemComponent;
typedef struct menu_component {
int window_width;
bool visible;
LinkedList items;
MenuItemComponent *highlight_item;
SDL_Renderer *renderer;
TTF_Font *font;
SDL_Texture *background_texture;
SDL_Texture *highlight_texture;
} MenuComponent;
/**
* Creates a menu fow a window.
* @param window A reference to the window
* @param font A reference to the TTF font to use to render text
* @return A reference to the menu component
*/
MenuComponent *menu_create(Window *window, TTF_Font *font);
/**
* Creates a menu item. Can be configured with a callback function which will be called when the menu item is clicked.
* Note that the callback function will be overridden if the menu item has sub items.
* @param label The label of the menu item
* @param on_click The callback function to call when clicked
* @return A reference to the menu item
*/
MenuItemComponent *menu_item_create(char *label, on_click_callback on_click);
/**
* Adds an item to a menu.
* @param menu A reference to the menu
* @param menu_item A reference to the menu item to add to the menu
*/
void menu_append(MenuComponent *menu, MenuItemComponent *menu_item);
/**
* Adds an sub-item to a menu item.
* Note that this will have the effect of preventing the callback to be called.
* @param menu_item A reference to the menu item
* @param sub_item A reference to the sub item to add to the menu item
*/
void menu_item_append(MenuItemComponent *menu_item, MenuItemComponent *sub_item);
/**
* Builds the ressources needed for a menu to be displayed. (ex: textures)
* @param menu The menu to build
*/
void menu_build(MenuComponent *menu);
/**
* Renders a menu to its window.
* @param menu A reference to the menu to render
*/
void menu_render(MenuComponent *menu);
/**
* Destroys a menu, freeing its memory.
* @param menu A reference to the menu to destroy
*/
void menu_destroy(MenuComponent *menu);
/**
* Destroys a menu item to free its memory.
* @param menu_item A reference to the menu item to destroy
*/
void menu_item_destroy(MenuItemComponent *menu_item);
/**
* Returns a menu as a component.
* @param menu The menu
* @return
*/
Component *menu_as_component(MenuComponent *menu);
#endif //NES_EMULATOR_WINDOW_MENU_H