summaryrefslogtreecommitdiff
path: root/src/widget_button.c
blob: 1024091b020ee5d2720e54419bd1d1a841a5ba2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

#include "main.h"
#include "interface.h"
#include "font.h"
#include "widget_button.h"
#include "image.h"

widget_button_t* newWidgetButton(char *text, int x, int y, void (*fce_event)(void *))
{
	widget_button_t *new;

	new = malloc( sizeof(widget_button_t) );
	new->text = strdup(text);
	new->x = x;
	new->y = y;
	new->fce_event = fce_event;
	getTextSize(text, &new->w, &new->h);

	return new;
}

void drawWidgetButton(widget_button_t *p)
{
	static SDL_Surface *g_button0 = NULL;
	static SDL_Surface *g_button1 = NULL;
	int x, y;

	getMousePosition(&x, &y);

	if( g_button0 == NULL )
	{
		g_button0 = addImageData("button0.png", IMAGE_ALPHA, "button0", IMAGE_GROUP_BASE);
	}

	if( g_button1 == NULL )
	{
		g_button1 = addImageData("button1.png", IMAGE_ALPHA, "button1", IMAGE_GROUP_BASE);
	}

	if( x >= p->x && x <= p->x+WIDGET_BUTTON_WIDTH && 
	    y >= p->y && y <= p->y+WIDGET_BUTTON_HEIGHT )
	{
		drawImage(g_button1, p->x, p->y, 0, 0, g_button0->w, g_button0->h);
	}
	else
	{
		drawImage(g_button0, p->x, p->y, 0, 0, g_button0->w, g_button0->h);
	}

	drawFont(p->text, p->x+WIDGET_BUTTON_WIDTH/2-p->w/2, p->y+p->h/2, COLOR_WHITE);
}

void eventWidgetButton(widget_button_t *p)
{
	static int time = 0;
	int x, y;

	if( time > 0)
	{
		time--;
		return;
	}

	getMousePosition(&x, &y);

	if( x >= p->x && x <= p->x+WIDGET_BUTTON_WIDTH && 
	    y >= p->y && y <= p->y+WIDGET_BUTTON_HEIGHT &&
	    isMouseClicked() )
	{
		time = WIDGET_BUTTON_TIME;
		p->fce_event(p);
	}
}

void destroyWidgetButton(widget_button_t *p)
{
	free(p->text);
	free(p);
}