2018-02-16 18:11:26 +01:00
|
|
|
/*
|
|
|
|
* BreakHack - A dungeone crawler RPG
|
|
|
|
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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
|
2018-05-20 00:02:39 +02:00
|
|
|
gui_button_update(GuiButton *button, Input *input)
|
2018-02-02 17:05:41 +01:00
|
|
|
{
|
2018-05-20 00:02:39 +02:00
|
|
|
Position p = { input->mouseX, input->mouseY };
|
|
|
|
button->hover = position_in_rect(&p, &button->area);
|
2018-02-02 17:05:41 +01:00
|
|
|
|
2018-05-20 00:02:39 +02:00
|
|
|
if (input_mousebutton_is_pressed(input, MBUTTON_LEFT)) {
|
2018-02-14 23:14:30 +01:00
|
|
|
if (position_in_rect(&p, &button->area) && button->event)
|
2018-02-09 06:53:06 +01:00
|
|
|
button->event(button->usrdata);
|
|
|
|
}
|
2018-02-02 16:16:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gui_button_destroy(GuiButton *button)
|
|
|
|
{
|
|
|
|
free(button);
|
|
|
|
}
|