2018-02-02 16:16:55 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "util.h"
|
|
|
|
#include "gui_button.h"
|
2018-02-09 06:53:06 +01:00
|
|
|
#include "collisions.h"
|
2018-02-02 16:16:55 +01:00
|
|
|
|
|
|
|
GuiButton *
|
2018-02-02 17:05:41 +01:00
|
|
|
gui_button_create(SDL_Rect area, void (*event)(void*), void *usrdata)
|
2018-02-02 16:16:55 +01:00
|
|
|
{
|
|
|
|
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;
|
2018-02-02 16:16:55 +01:00
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gui_button_check_pointer(GuiButton *button, Pointer *pointer)
|
|
|
|
{
|
2018-02-09 06:53:06 +01:00
|
|
|
button->hover = position_in_rect(&pointer->sprite->pos, &button->area);
|
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)
|
|
|
|
{
|
2018-02-09 06:53:06 +01:00
|
|
|
if (event->type == SDL_MOUSEBUTTONDOWN) {
|
2018-02-02 17:05:41 +01:00
|
|
|
|
2018-02-09 06:53:06 +01:00
|
|
|
if (event->button.button != SDL_BUTTON_LEFT)
|
|
|
|
return;
|
2018-02-02 17:05:41 +01:00
|
|
|
|
2018-02-09 07:26:44 +01:00
|
|
|
if (button->hover && button->event)
|
2018-02-09 06:53:06 +01:00
|
|
|
button->event(button->usrdata);
|
2018-02-09 07:26:44 +01:00
|
|
|
|
|
|
|
} else if (event->type == SDL_MOUSEMOTION) {
|
|
|
|
Position p = { event->motion.x, event->motion.y };
|
|
|
|
button->hover = position_in_rect(&p, &button->area);
|
2018-02-09 06:53:06 +01:00
|
|
|
}
|
2018-02-02 16:16:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gui_button_destroy(GuiButton *button)
|
|
|
|
{
|
|
|
|
free(button);
|
|
|
|
}
|