2018-02-09 09:39:31 +01:00
|
|
|
#include <stdlib.h>
|
2018-02-08 17:01:38 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include "menu.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "sprite.h"
|
|
|
|
#include "defines.h"
|
2018-02-09 06:53:06 +01:00
|
|
|
#include "gui_button.h"
|
2018-02-12 10:55:36 +01:00
|
|
|
#include "keyboard.h"
|
2018-02-14 16:04:40 +01:00
|
|
|
#include "mixer.h"
|
2018-02-08 17:01:38 +01:00
|
|
|
|
|
|
|
typedef struct MenuItems_t {
|
|
|
|
Sprite *sprite;
|
|
|
|
Sprite *hsprite;
|
2018-02-09 06:53:06 +01:00
|
|
|
GuiButton *button;
|
2018-02-08 17:01:38 +01:00
|
|
|
} MenuItem;
|
|
|
|
|
|
|
|
Menu *
|
|
|
|
menu_create(void)
|
|
|
|
{
|
|
|
|
Menu *menu = ec_malloc(sizeof(Menu));
|
|
|
|
menu->items = linkedlist_create();
|
|
|
|
menu->selected = 0;
|
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
2018-02-14 23:14:30 +01:00
|
|
|
static void
|
|
|
|
handle_keyboard_event(Menu *m, SDL_Event *event)
|
2018-02-08 17:01:38 +01:00
|
|
|
{
|
2018-02-14 23:14:30 +01:00
|
|
|
int lastSelect = -1;
|
2018-02-09 06:53:06 +01:00
|
|
|
|
2018-02-12 10:55:36 +01:00
|
|
|
if (keyboard_direction_press(UP, event)) {
|
2018-02-14 23:14:30 +01:00
|
|
|
lastSelect = m->selected;
|
2018-02-12 10:55:36 +01:00
|
|
|
m->selected--;
|
|
|
|
} else if (keyboard_direction_press(DOWN, event)) {
|
2018-02-14 23:14:30 +01:00
|
|
|
lastSelect = m->selected;
|
2018-02-12 10:55:36 +01:00
|
|
|
m->selected++;
|
|
|
|
} else if (keyboard_press(SDLK_RETURN, event)) {
|
2018-02-09 10:18:22 +01:00
|
|
|
MenuItem *item = linkedlist_get(&m->items, m->selected);
|
|
|
|
if (item->button->event)
|
|
|
|
item->button->event(item->button->usrdata);
|
|
|
|
return;
|
2018-02-14 23:14:30 +01:00
|
|
|
} else {
|
|
|
|
return;
|
2018-02-09 10:18:22 +01:00
|
|
|
}
|
2018-02-14 23:14:30 +01:00
|
|
|
m->selected = m->selected % linkedlist_size(m->items);
|
2018-02-09 10:18:22 +01:00
|
|
|
|
2018-02-14 23:14:30 +01:00
|
|
|
if (lastSelect != -1)
|
2018-02-14 16:04:40 +01:00
|
|
|
mixer_play_effect(CLICK);
|
|
|
|
|
2018-02-14 23:14:30 +01:00
|
|
|
((MenuItem*) linkedlist_get(&m->items, lastSelect))->button->hover = false;
|
|
|
|
((MenuItem*) linkedlist_get(&m->items, m->selected))->button->hover = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_mouse_motion_event(Menu *m, SDL_Event *event)
|
|
|
|
{
|
|
|
|
LinkedList *items;
|
|
|
|
MenuItem *item;
|
|
|
|
int current_select;
|
|
|
|
bool activeItemFound = false;
|
|
|
|
|
2018-02-09 06:53:06 +01:00
|
|
|
items = m->items;
|
2018-02-14 23:14:30 +01:00
|
|
|
current_select = 0;
|
2018-02-09 07:26:44 +01:00
|
|
|
while (items) {
|
2018-02-14 23:14:30 +01:00
|
|
|
item = items->data;
|
2018-02-09 07:26:44 +01:00
|
|
|
items = items->next;
|
2018-02-09 10:18:22 +01:00
|
|
|
|
2018-02-14 23:14:30 +01:00
|
|
|
item->button->hover = false;
|
2018-02-09 07:26:44 +01:00
|
|
|
gui_button_handle_event(item->button, event);
|
2018-02-09 10:18:22 +01:00
|
|
|
if (item->button->hover) {
|
2018-02-14 23:14:30 +01:00
|
|
|
if (current_select != m->selected) {
|
|
|
|
mixer_play_effect(CLICK);
|
|
|
|
m->selected = current_select;
|
|
|
|
}
|
|
|
|
activeItemFound = true;
|
2018-02-09 10:18:22 +01:00
|
|
|
}
|
|
|
|
current_select++;
|
2018-02-09 06:53:06 +01:00
|
|
|
}
|
2018-02-09 10:18:22 +01:00
|
|
|
|
2018-02-14 23:14:30 +01:00
|
|
|
if (!activeItemFound)
|
2018-02-09 10:18:22 +01:00
|
|
|
((MenuItem*) linkedlist_get(&m->items, m->selected))->button->hover = true;
|
2018-02-08 17:01:38 +01:00
|
|
|
}
|
|
|
|
|
2018-02-14 23:14:30 +01:00
|
|
|
static void
|
|
|
|
handle_mouse_button_event(Menu *m, SDL_Event *event)
|
|
|
|
{
|
|
|
|
/* NOTE: In some cases the button/item is destroyed by the click action
|
|
|
|
* make sure you don't 'use' items after a click event has fired. It
|
|
|
|
* might break. */
|
|
|
|
|
|
|
|
MenuItem *item = linkedlist_get(&m->items, m->selected);
|
|
|
|
gui_button_handle_event(item->button, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
menu_handle_event(Menu *m, SDL_Event *event)
|
|
|
|
{
|
|
|
|
if (event->type == SDL_KEYDOWN)
|
|
|
|
handle_keyboard_event(m, event);
|
|
|
|
else if (event->type == SDL_MOUSEMOTION)
|
|
|
|
handle_mouse_motion_event(m, event);
|
|
|
|
else if (event->type == SDL_MOUSEBUTTONDOWN)
|
|
|
|
handle_mouse_button_event(m, event);
|
|
|
|
}
|
|
|
|
|
2018-02-08 17:01:38 +01:00
|
|
|
void
|
2018-02-09 07:26:44 +01:00
|
|
|
menu_item_add(Menu *m, Sprite *s1, Sprite *s2, void (*event)(void*))
|
2018-02-08 17:01:38 +01:00
|
|
|
{
|
|
|
|
MenuItem *item = ec_malloc(sizeof(MenuItem));
|
|
|
|
item->sprite = s1;
|
|
|
|
item->hsprite = s2;
|
2018-02-09 06:53:06 +01:00
|
|
|
|
|
|
|
SDL_Rect area = {
|
|
|
|
item->sprite->pos.x,
|
|
|
|
item->sprite->pos.y,
|
|
|
|
item->sprite->textures[0]->dim.width,
|
|
|
|
item->sprite->textures[0]->dim.height
|
|
|
|
};
|
2018-02-09 07:26:44 +01:00
|
|
|
item->button = gui_button_create(area, event, NULL);
|
2018-02-14 23:14:30 +01:00
|
|
|
if (linkedlist_size(m->items) == 0)
|
|
|
|
item->button->hover = true;
|
2018-02-08 17:01:38 +01:00
|
|
|
linkedlist_append(&m->items, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
menu_render(Menu *m, Camera *cam)
|
|
|
|
{
|
|
|
|
LinkedList *items = m->items;
|
|
|
|
|
|
|
|
while (items) {
|
|
|
|
MenuItem *item = items->data;
|
2018-02-09 09:36:24 +01:00
|
|
|
items = items->next;
|
2018-02-09 10:18:22 +01:00
|
|
|
if (item->button->hover)
|
2018-02-09 06:53:06 +01:00
|
|
|
sprite_render(item->hsprite, cam);
|
|
|
|
else
|
|
|
|
sprite_render(item->sprite, cam);
|
2018-02-08 17:01:38 +01:00
|
|
|
}
|
2018-02-09 09:36:24 +01:00
|
|
|
|
2018-02-08 17:01:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
menu_item_destroy(MenuItem *item)
|
|
|
|
{
|
2018-02-09 06:53:06 +01:00
|
|
|
if (item->sprite)
|
|
|
|
sprite_destroy(item->sprite);
|
|
|
|
if (item->hsprite)
|
|
|
|
sprite_destroy(item->hsprite);
|
|
|
|
gui_button_destroy(item->button);
|
2018-02-08 17:01:38 +01:00
|
|
|
free(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
menu_destroy(Menu *m)
|
|
|
|
{
|
2018-02-09 09:36:24 +01:00
|
|
|
while (m->items)
|
|
|
|
menu_item_destroy(linkedlist_pop(&m->items));
|
|
|
|
|
2018-02-08 17:01:38 +01:00
|
|
|
free(m);
|
|
|
|
}
|
|
|
|
|