-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.c
116 lines (89 loc) · 2.31 KB
/
screen.c
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
#include <stdlib.h>
#include <SDL2/SDL.h>
#include "screen.h"
#include "widget.h"
struct Item
{
void* widget;
struct Item* next; // TAIL
};
static struct Item* last_widget;
void screen_attach(void* widget)
{
SDL_assert(widget != NULL);
struct Item* l = calloc(1, sizeof * l);
SDL_assert(l != NULL);
l->widget = widget;
if (last_widget != NULL)
l->next = last_widget;
last_widget = l;
}
void screen_detach_all()
{
struct Item* actual = last_widget;
while (actual != NULL) {
struct Item* next = actual->next;
free(actual);
actual = next;
}
}
void screen_draw(SDL_Renderer* renderer)
{
struct Item* actual = last_widget;
while (actual != NULL) {
struct Widget* w = actual->widget;
SDL_assert(w != NULL);
w->draw(w, renderer);
actual = actual->next;
}
}
void screen_mouse_move(int x, int y)
{
struct Item* actual = last_widget;
while (actual != NULL) {
struct Widget* w = actual->widget;
/* Check if mouse is in rect */
if (!w->disabled) {
if (SDL_PointInRect(&(SDL_Point) {x, y}, &w->rect) == SDL_TRUE) {
w->mouse_on = SDL_TRUE;
} else {
w->mouse_on = SDL_FALSE;
w->mouse_down = SDL_FALSE;
}
}
actual = actual->next;
}
}
void screen_mouse_down(int x, int y)
{
struct Item* actual = last_widget;
while (actual != NULL) {
struct Widget* w = actual->widget;
/* Check if mouse is in rect */
if (!w->disabled) {
if (SDL_PointInRect(&(SDL_Point) {x, y}, &w->rect) == SDL_TRUE) {
w->mouse_down = SDL_TRUE;
} else {
w->mouse_down = SDL_FALSE;
}
}
actual = actual->next;
}
}
void screen_mouse_up(int x, int y)
{
struct Item* actual = last_widget;
while (actual != NULL) {
struct Widget* w = actual->widget;
/* Check if mouse is in rect */
if (!w->disabled) {
if (SDL_PointInRect(&(SDL_Point) {x, y}, &w->rect) == SDL_TRUE) {
if (w->mouse_down == SDL_TRUE) {
w->mouse_down = SDL_FALSE;
w->click(w);
}
}
}
actual = actual->next;
}
}