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

View File

@ -25,6 +25,7 @@
#include "gui_button.h" #include "gui_button.h"
#include "keyboard.h" #include "keyboard.h"
#include "mixer.h" #include "mixer.h"
#include "collisions.h"
typedef struct MenuItems_t { typedef struct MenuItems_t {
Sprite *sprite; Sprite *sprite;
@ -41,7 +42,7 @@ menu_create(void)
return menu; return menu;
} }
static void static bool
handle_keyboard_input(Menu *m, Input *input) handle_keyboard_input(Menu *m, Input *input)
{ {
int lastSelect = -1; int lastSelect = -1;
@ -56,9 +57,9 @@ handle_keyboard_input(Menu *m, Input *input)
MenuItem *item = linkedlist_get(&m->items, m->selected); MenuItem *item = linkedlist_get(&m->items, m->selected);
if (item->button->event) if (item->button->event)
item->button->event(item->button->usrdata); item->button->event(item->button->usrdata);
return; return true;
} else { } else {
return; return false;
} }
m->selected = m->selected % linkedlist_size(m->items); 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, lastSelect))->button->hover = false;
((MenuItem*) linkedlist_get(&m->items, m->selected))->button->hover = true; ((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 void
menu_update(Menu *m, Input *input) 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 void
@ -99,13 +142,15 @@ menu_render(Menu *m, Camera *cam)
{ {
LinkedList *items = m->items; LinkedList *items = m->items;
int index = 0;
while (items) { while (items) {
MenuItem *item = items->data; MenuItem *item = items->data;
items = items->next; items = items->next;
if (item->button->hover) if (m->selected == index)
sprite_render(item->hsprite, cam); sprite_render(item->hsprite, cam);
else else
sprite_render(item->sprite, cam); sprite_render(item->sprite, cam);
index++;
} }
} }