-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.c
241 lines (201 loc) · 5.66 KB
/
ui.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <jo/jo.h>
#include "ui.h"
/* Current screen widgets */
static jo_list *widgets;
/* Current active widget */
static jo_node *currentWidget;
void WidgetsClear(WidgetsWidget *widget)
{
jo_node *node = JO_NULL;
for (node = widgets->first; node != JO_NULL && widgets->count != 0; node = node->next)
{
// Find widget
if (node->data.ptr == widget)
{
// De-select
if (currentWidget->data.ptr == widget)
currentWidget = JO_NULL;
// Send free mesage and clear widget
WidgetsWidget *toFree = ((WidgetsWidget *)node->data.ptr);
toFree->wProc(toFree, WIDGET_FREE);
jo_free(toFree);
// clear entry from list
jo_node *prev = node->prev;
jo_node *next = node->next;
if (prev != JO_NULL)
{
if (next != JO_NULL)
{
prev->next = next;
next->prev = prev;
}
else
{
prev->next = JO_NULL;
widgets->last = prev;
}
}
else
{
if (next != JO_NULL)
{
next->prev = JO_NULL;
widgets->first = next;
}
else
{
widgets->first = JO_NULL;
widgets->last = JO_NULL;
}
}
jo_free(node);
--widgets->count;
return;
}
}
}
void WidgetsClearAll()
{
jo_node *node = JO_NULL;
for (node = widgets->first; node != JO_NULL; node = node->next)
{
WidgetsWidget *widget = ((WidgetsWidget *)node->data.ptr);
widget->wProc(widget, WIDGET_FREE);
jo_free(widget);
}
currentWidget = JO_NULL;
jo_list_clear(widgets);
}
WidgetsWidget *WidgetsCreateWithData(int x, int y, void (*wProc)(WidgetsWidget *, WidgetMessages), void *data)
{
WidgetsWidget *created = jo_malloc(sizeof(WidgetsWidget));
if (created == JO_NULL)
{
jo_core_error("Out of memory");
}
created->x = x;
created->y = y;
created->IsVisible = true;
created->IsSelectable = true;
created->wProc = wProc;
created->data = data;
created->wProc(created, WIDGET_INIT);
jo_list_add_ptr(widgets, created);
return created;
}
WidgetsWidget *WidgetsCreate(int x, int y, void (*wProc)(WidgetsWidget *, WidgetMessages))
{
return WidgetsCreateWithData(x, y, wProc, JO_NULL);
}
void WidgetsInvokeInput()
{
if (currentWidget != JO_NULL)
{
WidgetsWidget *widget = ((WidgetsWidget *)currentWidget->data.ptr);
widget->wProc(widget, WIDGET_INPUT);
}
}
WidgetsWidget *WidgetsById(const int id)
{
if (id < 0 || id >= widgets->count)
{
return JO_NULL;
}
return (WidgetsWidget *)jo_list_at(widgets, id)->data.ptr;
}
int WidgetsGetId(const WidgetsWidget * widget)
{
jo_node *node;
int counter = 0;
for (node = widgets->first; node != NULL && widgets->count != 0; node = node->next)
{
if ((WidgetsWidget *)node->data.ptr == widget)
{
return counter;
}
counter++;
}
return -1;
}
jo_list *WidgetsGetAll()
{
return widgets;
}
WidgetsWidget *WidgetsGetCurrent()
{
return currentWidget != JO_NULL ? ((WidgetsWidget *)currentWidget->data.ptr) : JO_NULL;
}
WidgetsWidget *WidgetsGetNextSelectable()
{
if (currentWidget != JO_NULL)
{
jo_node *node;
for (node = currentWidget->next; node != NULL && widgets->count != 0; node = node->next)
{
if (((WidgetsWidget *)node->data.ptr)->IsVisible && ((WidgetsWidget *)node->data.ptr)->IsSelectable)
{
return (WidgetsWidget *)node->data.ptr;
}
}
}
return JO_NULL;
}
WidgetsWidget *WidgetsGetPrevSelectable()
{
if (currentWidget != JO_NULL)
{
jo_node *node;
for (node = currentWidget->prev; node != NULL && widgets->count != 0; node = node->prev)
{
if (((WidgetsWidget *)node->data.ptr)->IsVisible && ((WidgetsWidget *)node->data.ptr)->IsSelectable)
{
return (WidgetsWidget *)node->data.ptr;
}
}
}
return JO_NULL;
}
void WidgetsInitialize()
{
widgets = jo_malloc(sizeof(jo_list));
if (widgets == JO_NULL)
{
jo_core_error("Out of memory");
}
jo_list_init(widgets);
}
void WidgetsRedraw()
{
jo_node *node;
for (node = widgets->first; node != JO_NULL && widgets->count != 0; node = node->next)
{
WidgetsWidget *widget = ((WidgetsWidget *)node->data.ptr);
if (widget->IsVisible)
{
widget->wProc(widget, WIDGET_DRAW);
}
}
}
bool WidgetsSetCurrent(WidgetsWidget *widget)
{
if (widget != JO_NULL)
{
jo_node *node;
WidgetsWidget *current = currentWidget != JO_NULL ? (WidgetsWidget *)currentWidget->data.ptr : JO_NULL;
for (node = widgets->first; node != JO_NULL && widgets->count != 0; node = node->next)
{
if (((WidgetsWidget *)node->data.ptr) == widget &&
((WidgetsWidget *)node->data.ptr)->IsSelectable &&
((WidgetsWidget *)node->data.ptr)->IsVisible)
{
currentWidget = node;
// Send deselect and select messages
if (current != JO_NULL)
current->wProc(current, WIDGET_DESELECTED);
widget->wProc(widget, WIDGET_SELECTED);
return true;
}
}
}
return false;
}