Completed the button implementation

This commit is contained in:
Linus Probert 2018-02-02 17:05:41 +01:00
parent c2266b892d
commit 5b8a8030bd
5 changed files with 36 additions and 9 deletions

View File

@ -3,11 +3,13 @@
#include "gui_button.h" #include "gui_button.h"
GuiButton * GuiButton *
gui_button_create(SDL_Rect area) gui_button_create(SDL_Rect area, void (*event)(void*), void *usrdata)
{ {
GuiButton *button = ec_malloc(sizeof(GuiButton)); GuiButton *button = ec_malloc(sizeof(GuiButton));
button->area = area; button->area = area;
button->hover = false; button->hover = false;
button->usrdata = usrdata;
button->event = event;
return button; 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 && button->hover = r->x <= p->x && r->x + r->w >= p->x &&
r->y <= p->y && r->y + r->h >= p->y; 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) if (button->hover)
debug("Pointer is over button"); button->event(button->usrdata);
} }
void void

View File

@ -10,14 +10,19 @@
typedef struct GuiButton_t { typedef struct GuiButton_t {
SDL_Rect area; SDL_Rect area;
bool hover; bool hover;
void *usrdata;
void (*event)(void*);
} GuiButton; } GuiButton;
GuiButton * GuiButton *
gui_button_create(SDL_Rect); gui_button_create(SDL_Rect, void (*)(void*), void*);
void void
gui_button_check_pointer(GuiButton*, Pointer*); gui_button_check_pointer(GuiButton*, Pointer*);
void
gui_button_handle_event(GuiButton*, SDL_Event*);
void void
gui_button_destroy(GuiButton*); gui_button_destroy(GuiButton*);

View File

@ -26,7 +26,6 @@ static Map *gMap = NULL;
static RoomMatrix *gRoomMatrix = NULL; static RoomMatrix *gRoomMatrix = NULL;
static Gui *gGui = NULL; static Gui *gGui = NULL;
static Pointer *gPointer = NULL; static Pointer *gPointer = NULL;
static GuiButton *gButton = NULL;
static unsigned int cLevel = 1; static unsigned int cLevel = 1;
static double renderScale = 1.0; static double renderScale = 1.0;
static GameState gGameState; static GameState gGameState;
@ -136,7 +135,6 @@ bool init(void)
gGui = gui_create(gRenderer); gGui = gui_create(gRenderer);
item_builder_init(gRenderer); item_builder_init(gRenderer);
gPointer = pointer_create(gRenderer); gPointer = pointer_create(gRenderer);
gButton = gui_button_create((SDL_Rect) { 100, 100, 100, 100 });
} }
gGameState = PLAYING; gGameState = PLAYING;
@ -147,7 +145,7 @@ bool init(void)
static static
void loadMedia(void) void loadMedia(void)
{ {
gPlayer = player_create(PALADIN, gRenderer); gPlayer = player_create(WARRIOR, gRenderer);
} }
static static
@ -169,8 +167,6 @@ bool handle_events(void)
pointer_handle_event(gPointer, &event); pointer_handle_event(gPointer, &event);
} }
gui_button_check_pointer(gButton, gPointer);
return quit; return quit;
} }
@ -298,7 +294,6 @@ void close(void)
roommatrix_destroy(gRoomMatrix); roommatrix_destroy(gRoomMatrix);
gui_destroy(gGui); gui_destroy(gGui);
pointer_destroy(gPointer); pointer_destroy(gPointer);
gui_button_destroy(gButton);
item_builder_close(); item_builder_close();
SDL_DestroyRenderer(gRenderer); SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow); SDL_DestroyWindow(gWindow);

View File

@ -1,3 +1,4 @@
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include "pointer.h" #include "pointer.h"
#include "util.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 void
pointer_render(Pointer *p, Camera *cam) pointer_render(Pointer *p, Camera *cam)
{ {

View File

@ -15,6 +15,9 @@ pointer_create(SDL_Renderer *renderer);
void void
pointer_handle_event(Pointer*, SDL_Event *event); pointer_handle_event(Pointer*, SDL_Event *event);
void
pointer_toggle_clickable_pointer(Pointer *, bool clickable);
void void
pointer_render(Pointer*, Camera*); pointer_render(Pointer*, Camera*);