summaryrefslogtreecommitdiff
path: root/src/widget/widget_catchkey.c
blob: 7443358937db05613776869fe3f4e05c8d7e3f48 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

#include <stdlib.h>

#include "main.h"
#include "interface.h"
#include "font.h"
#include "widget_catchkey.h"

widget_catchkey_t* newWidgetCatchkey(int x, int y, int key)
{
	widget_catchkey_t *new;

	new = malloc( sizeof(widget_catchkey_t) );

	new->x = x;
	new->y = y;
	new->key = key;

	return new;
}

int getWidgetCatchKey(widget_catchkey_t *p)
{
	return p->key;
}

void setWidgetCatchKey(widget_catchkey_t *p, int key)
{
	p->key = key;
}

void drawWidgetCatchkey(widget_catchkey_t *p)
{
	char *name;

	name = NULL;

	if( p->key != WIDGET_CATCHKEY_NOKEY )
	{
		name = (char *) SDL_GetKeyName(p->key);
	}

	if( name == NULL )
	{
		name = "no key";
	}

	drawFont(name, p->x, p->y, COLOR_WHITE);
}

static int getPessAnyKey()
{
	Uint8 *mapa;
	int i;

	mapa = SDL_GetKeyState(NULL);

	for( i = SDLK_FIRST ; i <= SDLK_F15 ; i++ )
	{
		if( mapa[i] == SDL_PRESSED )
		{
			return i;
		}
	}

	return WIDGET_CATCHKEY_NOKEY;
}

void eventWidgetCatchkey(widget_catchkey_t *p)
{
	int x, y;

	getMousePosition(&x, &y);

	if( isMouseClicked() )
	{
		if( x >= p->x && x <= p->x+WIDGET_CATCHKEY_WIDTH && 
		    y >= p->y && y <= p->y+WIDGET_CATCHKEY_HEIGHT )
		{
			p->active = TRUE;
		}
		else
		{
			p->active = FALSE;
		}
	}

	if( p->active == TRUE )
	{
		int key;

		key = getPessAnyKey();

		if( key != WIDGET_CATCHKEY_NOKEY )
		{
			p->key = key;
		}
	}
}

void destroyWidgetCatchkey(widget_catchkey_t *p)
{
	free(p);
}