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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "main.h"
#include "list.h"
#include "mouse_buffer.h"
static list_t *list_event;
typedef struct mouse_event_struct
{
Uint8 button;
int x;
int y;
} mouse_event_t;
static mouse_event_t* mouse_event_new(int x, int y, Uint8 button)
{
mouse_event_t *mouse_event;
mouse_event = malloc(sizeof(mouse_event_t));
mouse_event->x = x;
mouse_event->y = y;
mouse_event->button = button;
return mouse_event;
}
static void mouse_event_destroy(mouse_event_t *mouse_event)
{
assert(mouse_event != NULL);
free(mouse_event);
}
int mouse_buffer_init()
{
list_event = list_new();
return 0;
}
int mouse_buffer_event(SDL_MouseButtonEvent *button)
{
assert(button != NULL);
assert(list_event != NULL);
switch(button->button) {
case SDL_BUTTON_LEFT:
case SDL_BUTTON_RIGHT:
//printf("mouse button - pos(%d,%d)\n", button->x, button->y);
list_add(list_event, mouse_event_new(button->x, button->y, button->button));
return 1;
break;
default:
break;
}
return 0;
}
int mouse_buffer_clean()
{
assert(list_event != NULL);
list_do_empty(list_event);
return 0;
}
bool_t mouse_buffer_is_on_area(int x, int y, int w, int h, unsigned int flag)
{
mouse_event_t *mouse_event;
int i;
assert(list_event != NULL);
if (flag & MOUSE_BUF_MOTION) {
int mouse_x, mouse_y;
SDL_GetMouseState(&mouse_x, &mouse_y);
if (mouse_x >= x && mouse_y >= y &&
mouse_x < x+w && mouse_y < y+h) {
return TRUE;
}
} else {
for (i = 0; i<list_event->count; i++) {
mouse_event = (mouse_event_t *)list_event->list[i];
if ((mouse_event->x >= x && mouse_event->y >= y && mouse_event->x < x+w && mouse_event->y < y+h) ||
(flag & MOUSE_BUF_AREA_NONE)) {
if (flag & MOUSE_BUF_CLICK_LEFT) {
if (mouse_event->button == SDL_BUTTON_LEFT) {
return TRUE;
} else {
return FALSE;
}
}
if (flag & MOUSE_BUF_CLICK_RIGHT) {
if (mouse_event->button == SDL_BUTTON_RIGHT) {
return TRUE;
} else {
return FALSE;
}
}
if (flag & MOUSE_BUF_CLICK) {
if (mouse_event->button == SDL_BUTTON_LEFT || mouse_event->button == SDL_BUTTON_RIGHT) {
return TRUE;
} else {
return FALSE;
}
}
return TRUE;
}
}
}
return FALSE;
}
int mouse_buffer_quit()
{
assert(list_event != NULL);
list_destroy_item(list_event, mouse_event_destroy);
return 0;
}
|