diff --git a/src/gui_button.c b/src/gui_button.c index 424c9d0..676d7c5 100644 --- a/src/gui_button.c +++ b/src/gui_button.c @@ -3,11 +3,13 @@ #include "gui_button.h" GuiButton * -gui_button_create(SDL_Rect area) +gui_button_create(SDL_Rect area, void (*event)(void*), void *usrdata) { GuiButton *button = ec_malloc(sizeof(GuiButton)); button->area = area; button->hover = false; + button->usrdata = usrdata; + button->event = event; return button; } @@ -20,8 +22,20 @@ gui_button_check_pointer(GuiButton *button, Pointer *pointer) button->hover = r->x <= p->x && r->x + r->w >= p->x && r->y <= p->y && r->y + r->h >= p->y; + pointer_toggle_clickable_pointer(pointer, button->hover); +} + +void +gui_button_handle_event(GuiButton *button, SDL_Event *event) +{ + if (event->type != SDL_MOUSEBUTTONDOWN) + return; + + if (event->button.button != SDL_BUTTON_LEFT) + return; + if (button->hover) - debug("Pointer is over button"); + button->event(button->usrdata); } void diff --git a/src/gui_button.h b/src/gui_button.h index 7dd263f..fbbc2af 100644 --- a/src/gui_button.h +++ b/src/gui_button.h @@ -10,14 +10,19 @@ typedef struct GuiButton_t { SDL_Rect area; bool hover; + void *usrdata; + void (*event)(void*); } GuiButton; GuiButton * -gui_button_create(SDL_Rect); +gui_button_create(SDL_Rect, void (*)(void*), void*); void gui_button_check_pointer(GuiButton*, Pointer*); +void +gui_button_handle_event(GuiButton*, SDL_Event*); + void gui_button_destroy(GuiButton*); diff --git a/src/main.c b/src/main.c index c8e8ee9..4de82bb 100644 --- a/src/main.c +++ b/src/main.c @@ -26,7 +26,6 @@ static Map *gMap = NULL; static RoomMatrix *gRoomMatrix = NULL; static Gui *gGui = NULL; static Pointer *gPointer = NULL; -static GuiButton *gButton = NULL; static unsigned int cLevel = 1; static double renderScale = 1.0; static GameState gGameState; @@ -136,7 +135,6 @@ bool init(void) gGui = gui_create(gRenderer); item_builder_init(gRenderer); gPointer = pointer_create(gRenderer); - gButton = gui_button_create((SDL_Rect) { 100, 100, 100, 100 }); } gGameState = PLAYING; @@ -147,7 +145,7 @@ bool init(void) static void loadMedia(void) { - gPlayer = player_create(PALADIN, gRenderer); + gPlayer = player_create(WARRIOR, gRenderer); } static @@ -169,8 +167,6 @@ bool handle_events(void) pointer_handle_event(gPointer, &event); } - gui_button_check_pointer(gButton, gPointer); - return quit; } @@ -298,7 +294,6 @@ void close(void) roommatrix_destroy(gRoomMatrix); gui_destroy(gGui); pointer_destroy(gPointer); - gui_button_destroy(gButton); item_builder_close(); SDL_DestroyRenderer(gRenderer); SDL_DestroyWindow(gWindow); diff --git a/src/pointer.c b/src/pointer.c index af9887a..4cc6fc8 100644 --- a/src/pointer.c +++ b/src/pointer.c @@ -1,3 +1,4 @@ +#include #include #include "pointer.h" #include "util.h" @@ -27,6 +28,15 @@ pointer_handle_event(Pointer *p, SDL_Event *event) } } +void +pointer_toggle_clickable_pointer(Pointer *p, bool clickable) +{ + if (clickable) + p->sprite->clip.x = 32; + else + p->sprite->clip.x = 0; +} + void pointer_render(Pointer *p, Camera *cam) { diff --git a/src/pointer.h b/src/pointer.h index d92baad..8a73d72 100644 --- a/src/pointer.h +++ b/src/pointer.h @@ -15,6 +15,9 @@ pointer_create(SDL_Renderer *renderer); void pointer_handle_event(Pointer*, SDL_Event *event); +void +pointer_toggle_clickable_pointer(Pointer *, bool clickable); + void pointer_render(Pointer*, Camera*);