Reintroduces the pointer and mouse menu operation.

This commit is contained in:
Linus Probert 2018-05-20 17:25:53 +02:00
parent 123ae90f59
commit a66f16de79
2 changed files with 56 additions and 5 deletions

View File

@ -62,6 +62,7 @@ static Map *gMap = NULL;
static RoomMatrix *gRoomMatrix = NULL;
static Gui *gGui = NULL;
static SkillBar *gSkillBar = NULL;
static Pointer *gPointer = NULL;
static unsigned int cLevel = 1;
static float deltaTime = 1.0;
static double renderScale = 1.0;
@ -197,6 +198,7 @@ initGame(void)
gGui = gui_create(gCamera);
gSkillBar = skillbar_create(gRenderer);
item_builder_init(gRenderer);
gPointer = pointer_create(gRenderer);
particle_engine_init();
menuTimer = timer_create();
actiontextbuilder_init(gRenderer);
@ -544,6 +546,7 @@ run_game(void)
SDL_RenderFillRect(gRenderer, &dimmer);
menu_render(inGameMenu, gCamera);
}
pointer_render(gPointer, gCamera);
SDL_RenderPresent(gRenderer);
@ -584,6 +587,7 @@ run_menu(void)
SDL_RenderSetViewport(gRenderer, NULL);
menu_render(mainMenu, gCamera);
pointer_render(gPointer, gCamera);
SDL_RenderPresent(gRenderer);
}
@ -603,6 +607,7 @@ void run(void)
quit = handle_events();
handle_main_input();
pointer_handle_input(gPointer, &input);
switch (gGameState) {
case PLAYING:
@ -653,6 +658,7 @@ void close(void)
roommatrix_destroy(gRoomMatrix);
gui_destroy(gGui);
skillbar_destroy(gSkillBar);
pointer_destroy(gPointer);
actiontextbuilder_close();
item_builder_close();
particle_engine_close();

View File

@ -25,6 +25,7 @@
#include "gui_button.h"
#include "keyboard.h"
#include "mixer.h"
#include "collisions.h"
typedef struct MenuItems_t {
Sprite *sprite;
@ -41,7 +42,7 @@ menu_create(void)
return menu;
}
static void
static bool
handle_keyboard_input(Menu *m, Input *input)
{
int lastSelect = -1;
@ -56,9 +57,9 @@ handle_keyboard_input(Menu *m, Input *input)
MenuItem *item = linkedlist_get(&m->items, m->selected);
if (item->button->event)
item->button->event(item->button->usrdata);
return;
return true;
} else {
return;
return false;
}
m->selected = m->selected % linkedlist_size(m->items);
@ -67,12 +68,54 @@ handle_keyboard_input(Menu *m, Input *input)
((MenuItem*) linkedlist_get(&m->items, lastSelect))->button->hover = false;
((MenuItem*) linkedlist_get(&m->items, m->selected))->button->hover = true;
return false;
}
static void
handle_mouse_motion(Menu *m, Input *input)
{
if (!input_mouse_moved(input))
return;
Position p = { input->mouseX, input->mouseY };
LinkedList *items = m->items;
int index = 0;
while (items) {
MenuItem *item = items->data;
items = items->next;
if (position_in_rect(&p, &item->button->area)) {
m->selected = index;
break;
}
index++;
}
}
void
menu_update(Menu *m, Input *input)
{
handle_keyboard_input(m, input);
if (handle_keyboard_input(m, input)) {
return;
}
handle_mouse_motion(m, input);
LinkedList *items = m->items;
while (items) {
MenuItem *item = items->data;
items = items->next;
Position p = { input->mouseX, input->mouseY };
if (position_in_rect(&p, &item->button->area)
&& input_mousebutton_is_pressed(input, MBUTTON_LEFT))
{
item->button->event(NULL);
return;
}
}
}
void
@ -99,13 +142,15 @@ menu_render(Menu *m, Camera *cam)
{
LinkedList *items = m->items;
int index = 0;
while (items) {
MenuItem *item = items->data;
items = items->next;
if (item->button->hover)
if (m->selected == index)
sprite_render(item->hsprite, cam);
else
sprite_render(item->sprite, cam);
index++;
}
}