-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui.c
executable file
·369 lines (329 loc) · 14.5 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* ui.c */
#include <stdio.h>
#include <string.h>
#include "ui.h"
#include "undo.h"
static int cmpfunc(const void* a, const void *b) {
char * const * aa = a;
char * const * bb = b;
return strcmp(*aa, *bb);
}
#if 0
static void qsortfiles(char** files, int count) {
// Sort files by name
// https://github.com/raysan5/raygui/blob/master/examples/custom_file_dialog/gui_file_dialog.h
// https://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Quicksort#C
if (count > 1)
{
const unsigned int MAX = 64;
unsigned int left = 0, stack[64], pos = 0, seed = rand(), len = count;
for (;;)
{
for (; left + 1 < len; len++) // Sort left to len - 1
{
if (pos == MAX) len = stack[pos = 0]; // Stack overflow, reset
char *pivot = files[left + seed%(len - left)]; // Pick random pivot
seed = seed*69069 + 1; // Next pseudo-random number
stack[pos++] = len; // Sort right part later
for (unsigned int right = left - 1;;) // Inner loop: partitioning
{
while (strcmp(files[++right], pivot) < 0); // Look for greater element
while (strcmp(pivot, files[--len]) < 0); // Look for smaller element
if (right >= len) break; // Partition point found?
char *temp = files[right];
files[right] = files[len]; // The only swap
files[len] = temp;
} // Partitioned, continue left part
}
if (pos == 0) break; // Stack empty?
left = len; // Left to right is sorted
len = stack[--pos]; // Get next range to sort
}
}
}
#endif
struct Spritesheet *ssIcons;
Font fontRobotoMedium24 = {0};
Font fontRobotoRegular14 = {0};
struct UI* UiNew(void)
{
extern Font fontRobotoRegular14, fontRobotoMedium24;
struct UI *self = calloc(1, sizeof(struct UI));
if ( self ) {
fontRobotoMedium24 = LoadFontEx("assets/Roboto-Medium.ttf", 24, 0, 0);
fontRobotoRegular14 = LoadFontEx("assets/Roboto-Regular.ttf", 14, 0, 0);
// https://draeton.github.io/stitches/
ssIcons = SpritesheetNew("assets/icons.png", 36, 36, 5);
self->containers = ArrayNew(8);
self->titleBar = TitleBarNew();
if ( self->titleBar ) {
struct IconWidget *iw = IconWidgetNew((struct Container*)self->titleBar, -1, MENU, BaizeToggleNavDrawerCommand, NULL);
if ( iw ) {
self->titleBar->super.widgets = ArrayPush(self->titleBar->super.widgets, iw);
}
struct TextWidget *tw = TextWidgetNew((struct Container*)self->titleBar, NONE, &fontRobotoMedium24, 24.0f, 0, NULL, NULL);
if ( tw ) {
TextWidgetSetText(tw, "Variant Title");
self->titleBar->super.widgets = ArrayPush(self->titleBar->super.widgets, tw);
}
iw = IconWidgetNew((struct Container*)self->titleBar, 1, UNDO, BaizeUndoCommand, NULL);
if ( iw ) {
self->titleBar->super.widgets = ArrayPush(self->titleBar->super.widgets, iw);
}
iw = IconWidgetNew((struct Container*)self->titleBar, 1, DONE, BaizeCollectCommand, NULL);
if ( iw ) {
self->titleBar->super.widgets = ArrayPush(self->titleBar->super.widgets, iw);
}
self->containers = ArrayPush(self->containers, self->titleBar);
TitleBarLayoutWidgets((struct Container*)self->titleBar);
}
self->statusBar = StatusBarNew();
if ( self->statusBar ) {
struct TextWidget *tw = TextWidgetNew((struct Container*)self->statusBar, NONE, &fontRobotoRegular14, 14.0f, -1, NULL, NULL);
if ( tw ) {
TextWidgetSetText(tw, "STOCK:");
self->statusBar->super.widgets = ArrayPush(self->statusBar->super.widgets, tw);
}
tw = TextWidgetNew((struct Container*)self->statusBar, NONE, &fontRobotoRegular14, 14.0f, 0, NULL, NULL);
if ( tw ) {
// TextWidgetSetText(tw, "MOVES");
self->statusBar->super.widgets = ArrayPush(self->statusBar->super.widgets, tw);
}
tw = TextWidgetNew((struct Container*)self->statusBar, NONE, &fontRobotoRegular14, 14.0f, 1, NULL, NULL);
if ( tw ) {
TextWidgetSetText(tw, "PERCENT COMPLETE");
self->statusBar->super.widgets = ArrayPush(self->statusBar->super.widgets, tw);
}
self->containers = ArrayPush(self->containers, self->statusBar);
StatusBarLayoutWidgets((struct Container*)self->statusBar);
}
/*
NewNavItem(n, "star", "New deal", ebiten.KeyN),
NewNavItem(n, "restore", "Restart deal", ebiten.KeyR),
NewNavItem(n, "search", "Find game...", ebiten.KeyF),
NewNavItem(n, "bookmark_add", "Bookmark", ebiten.KeyS),
NewNavItem(n, "bookmark", "Goto bookmark", ebiten.KeyL),
NewNavItem(n, "list", "Rules...", ebiten.KeyF1),
NewNavItem(n, "info", "Statistics...", ebiten.KeyF4),
NewNavItem(n, "settings", "Settings...", ebiten.KeyF3),
*/
self->navDrawer = NavDrawerNew();
if ( self->navDrawer ) {
struct TextWidget *tw = TextWidgetNew((struct Container*)self->navDrawer, STAR, &fontRobotoMedium24, 24.0f, -1, BaizeNewDealCommand, NULL);
if ( tw ) {
TextWidgetSetText(tw, "New deal");
self->navDrawer->super.super.widgets = ArrayPush(self->navDrawer->super.super.widgets, tw);
}
tw = TextWidgetNew((struct Container*)self->navDrawer, RESTORE, &fontRobotoMedium24, 24.0f, -1, BaizeRestartDealCommand, NULL);
if ( tw ) {
TextWidgetSetText(tw, "Restart deal");
self->navDrawer->super.super.widgets = ArrayPush(self->navDrawer->super.super.widgets, tw);
}
tw = TextWidgetNew((struct Container*)self->navDrawer, SEARCH, &fontRobotoMedium24, 24.0f, -1, BaizeFindVariantCommand, NULL);
if ( tw ) {
TextWidgetSetText(tw, "Find game...");
self->navDrawer->super.super.widgets = ArrayPush(self->navDrawer->super.super.widgets, tw);
}
tw = TextWidgetNew((struct Container*)self->navDrawer, BOOKMARK_ADD, &fontRobotoMedium24, 24.0f, -1, BaizeSavePositionCommand, NULL);
if ( tw ) {
TextWidgetSetText(tw, "Bookmark");
self->navDrawer->super.super.widgets = ArrayPush(self->navDrawer->super.super.widgets, tw);
}
tw = TextWidgetNew((struct Container*)self->navDrawer, BOOKMARK, &fontRobotoMedium24, 24.0f, -1, BaizeLoadPositionCommand, NULL);
if ( tw ) {
TextWidgetSetText(tw, "Goto bookmark");
self->navDrawer->super.super.widgets = ArrayPush(self->navDrawer->super.super.widgets, tw);
}
tw = TextWidgetNew((struct Container*)self->navDrawer, INFO, &fontRobotoMedium24, 24.0f, -1, BaizeWikipediaCommand, NULL);
if ( tw ) {
TextWidgetSetText(tw, "Wikipedia...");
self->navDrawer->super.super.widgets = ArrayPush(self->navDrawer->super.super.widgets, tw);
}
self->containers = ArrayPush(self->containers, self->navDrawer);
}
self->variantDrawer = NavDrawerNew();
if (self->variantDrawer) {
// raylib 4.0 -> 4.5 changed the GetDirectoryFiles() API to GetDirectoryFiles()
FilePathList fpl = LoadDirectoryFiles("variants");
if ( fpl.count ) {
// qsort(files, count, sizeof(char*), cmpfunc);
// qsortfiles(files, count);
qsort(fpl.paths, fpl.count, sizeof(char*), cmpfunc);
for ( unsigned int i=0; i<fpl.count; i++ ) {
// fprintf(stdout, "FILE: %s\n", files[i]);
if (IsFileExtension(fpl.paths[i], ".lua")) {
const char* vname = GetFileNameWithoutExt(fpl.paths[i]);
if (vname[0] == '~') {
continue;
}
struct TextWidget *tw = TextWidgetNew((struct Container*)self->variantDrawer, NONE, &fontRobotoMedium24, 24.0f, -1, BaizeChangeVariantCommand, UtilStrDup(vname));
if ( tw ) {
// TextWidgetSetText(tw, GetFileNameWithoutExt(files[i]));
TextWidgetSetText(tw, GetFileNameWithoutExt(fpl.paths[i]));
self->variantDrawer->super.super.widgets = ArrayPush(self->variantDrawer->super.super.widgets, tw);
}
}
}
}
UnloadDirectoryFiles(fpl);
self->containers = ArrayPush(self->containers, self->variantDrawer);
}
self->toastManager = ToastManagerNew();
}
return self;
}
void UiToast(struct UI *const self, const char* message)
{
if ( message == NULL || message[0] == '\0' ) {
fprintf(stderr, "WARNING: empty toast\n");
return;
}
struct Toast *const t = ToastNew(message, 60 * (6 + ArrayLen(self->toastManager->toasts)));
if ( t ) {
ToastManagerAdd(self->toastManager, t);
}
}
void UiUpdateTitleBar(struct UI *const self, const char* center)
{
struct TextWidget *tw = ArrayGet(((struct Container*)self->titleBar)->widgets, 1); // TODO parameterize
TextWidgetSetText(tw, center);
}
void UiShowNavDrawer(struct UI *const self)
{
if (!DrawerVisible((struct Drawer*)self->navDrawer)) {
DrawerShow((struct Drawer*)self->navDrawer);
}
}
void UiHideNavDrawer(struct UI *const self)
{
if (DrawerVisible((struct Drawer*)self->navDrawer)) {
DrawerHide((struct Drawer*)self->navDrawer);
}
}
void UiToggleNavDrawer(struct UI *const self)
{
if (DrawerVisible((struct Drawer*)self->navDrawer)) {
DrawerHide((struct Drawer*)self->navDrawer);
} else {
DrawerShow((struct Drawer*)self->navDrawer);
}
}
void UiShowVariantDrawer(struct UI *const self)
{
if (!DrawerVisible((struct Drawer*)self->variantDrawer)) {
DrawerShow((struct Drawer*)self->variantDrawer);
}
}
void UiHideVariantDrawer(struct UI *const self)
{
if (DrawerVisible((struct Drawer*)self->variantDrawer)) {
DrawerHide((struct Drawer*)self->variantDrawer);
}
}
void UiToggleVariantDrawer(struct UI *const self)
{
if (DrawerVisible((struct Drawer*)self->variantDrawer)) {
DrawerHide((struct Drawer*)self->variantDrawer);
} else {
DrawerShow((struct Drawer*)self->variantDrawer);
}
}
void UiHideDrawers(struct UI *const self)
{
UiHideNavDrawer(self);
UiHideVariantDrawer(self);
}
void UiUpdateStatusBar(struct UI *const self, const char* left, const char* center, const char *right)
{
struct TextWidget *tw = ArrayGet(((struct Container*)self->statusBar)->widgets, 0); // TODO parameterize
TextWidgetSetText(tw, left);
tw = ArrayGet(((struct Container*)self->statusBar)->widgets, 1); // TODO parameterize
TextWidgetSetText(tw, center);
tw = ArrayGet(((struct Container*)self->statusBar)->widgets, 2); // TODO parameterize
TextWidgetSetText(tw, right);
}
struct Container* UiFindContainerAt(struct UI *const self, Vector2 pos)
{
size_t cindex;
for ( struct Container *con = ArrayFirst(self->containers, &cindex); con; con = ArrayNext(self->containers, &cindex) ) {
if ( CheckCollisionPointRec(pos, con->rect) ) {
return con;
}
}
return NULL;
}
struct Widget* UiFindWidgetAt(struct UI *const self, Vector2 pos)
{
#if 1
struct Container *con = UiFindContainerAt(self, pos);
if (con) {
size_t windex;
for ( struct Widget *w = ArrayFirst(con->widgets, &windex); w; w = ArrayNext(con->widgets, &windex) ) {
// widget screen rects are relative to their container's rect
// Rectangle r = w->rect;
// r.x += con->rect.x;
// r.y += con->rect.y;
Rectangle r = WidgetScreenRect(w);
if ( CheckCollisionPointRec(pos, r) ) {
// fprintf(stdout, "Tapped on a widget\n");
return w;
}
}
}
#else
size_t cindex, windex;
for ( struct Container *con = ArrayFirst(self->containers, &cindex); con; con = ArrayNext(self->containers, &cindex) ) {
if ( CheckCollisionPointRec(pos, con->rect) ) {
// fprintf(stdout, "Tapped on a container\n");
for ( struct Widget *w = ArrayFirst(con->widgets, &windex); w; w = ArrayNext(con->widgets, &windex) ) {
Rectangle r = w->rect;
r.x += con->rect.x;
r.y += con->rect.y;
if ( CheckCollisionPointRec(pos, r) ) {
// fprintf(stdout, "Tapped on a widget\n");
return w;
}
}
}
}
#endif
return NULL;
}
void UiLayout(struct UI *const self, const int windowWidth, const int windowHeight)
{
size_t index;
for ( struct Container *con = ArrayFirst(self->containers, &index); con; con = ArrayNext(self->containers, &index) ) {
con->vtable->Layout(con, windowWidth, windowHeight);
}
}
void UiUpdate(struct UI *const self)
{
size_t index;
for ( struct Container *con = ArrayFirst(self->containers, &index); con; con = ArrayNext(self->containers, &index) ) {
con->vtable->Update(con);
}
ToastManagerUpdate(self->toastManager);
}
void UiDraw(struct UI *const self)
{
size_t index;
for ( struct Container *con = ArrayFirst(self->containers, &index); con; con = ArrayNext(self->containers, &index) ) {
con->vtable->Draw(con);
}
ToastManagerDraw(self->toastManager);
}
void UiFree(struct UI *const self)
{
if (self) {
size_t index;
for ( struct Container *con = ArrayFirst(self->containers, &index); con; con = ArrayNext(self->containers, &index) ) {
con->vtable->Free(con);
}
ArrayFree(self->containers);
ToastManagerFree(self->toastManager);
free(self);
}
SpritesheetFree(ssIcons);
if (fontRobotoMedium24.baseSize) UnloadFont(fontRobotoMedium24);
if (fontRobotoRegular14.baseSize) UnloadFont(fontRobotoRegular14);
}