breakhack/src/gui_button.c

46 lines
907 B
C
Raw Normal View History

#include <stdlib.h>
#include "util.h"
#include "gui_button.h"
GuiButton *
2018-02-02 17:05:41 +01:00
gui_button_create(SDL_Rect area, void (*event)(void*), void *usrdata)
{
GuiButton *button = ec_malloc(sizeof(GuiButton));
button->area = area;
button->hover = false;
2018-02-02 17:05:41 +01:00
button->usrdata = usrdata;
button->event = event;
return button;
}
void
gui_button_check_pointer(GuiButton *button, Pointer *pointer)
{
SDL_Rect *r = &button->area;
Position *p = &pointer->sprite->pos;
button->hover = r->x <= p->x && r->x + r->w >= p->x &&
r->y <= p->y && r->y + r->h >= p->y;
2018-02-02 17:05:41 +01:00
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)
2018-02-02 17:05:41 +01:00
button->event(button->usrdata);
}
void
gui_button_destroy(GuiButton *button)
{
free(button);
}