-
Notifications
You must be signed in to change notification settings - Fork 2
/
ui.c
714 lines (618 loc) · 16.9 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
#include "SDL/SDL_image.h"
#include "SDL/SDL_gfxPrimitives.h"
#include "SDL/SDL_ttf.h"
#include "ui.h"
#include "list.h"
#define MESSAGE_BOX_MARGIN 10
static int DispatchEvents(void);
static void Redraw(void);
// SDL stuff
TTF_Font* font;
TTF_Font* buttonFont;
static Ifel* currentEl;
static Ifel* elFocus;
static SDL_Surface* screen;
static list* ifels;
static int returnCode;
static int quit;
void SetFocus(Ifel* el)
{
elFocus = el;
}
Ifel* GetFocus(void)
{
return elFocus;
}
int DeleteIfel(Ifel* parent, Ifel* child)
{
list* l;
iterator iter;
// first delete all children
Ifel* el = GetFirstIfel(child, &iter);
while(el)
{
//el->delete(el); - JKN must add this
el = GetNextIfel(&iter);
}
// now delete self
if (!parent)
l = ifels;
else
l = parent->ifels;
ListRemoveNode(l, child);
SDL_FreeSurface(child->surface);
child->surface = NULL;
return 0;
}
// set the iterator and return the first ifel's data
Ifel* GetFirstIfel(Ifel* parent, iterator* iter)
{
//fprintf(stderr, "GetFirstIfel %i\n", parent);
if (!parent)
{
if (ifels->size == 0)
return NULL;
//fprintf(stderr, "size: %i first: %i\n", ifels->size, ifels->first);
*iter = ifels->first;
}
else
{
if (parent->ifels->size == 0)
return NULL;
*iter = parent->ifels->first;
}
return (Ifel*)ListGetData(*iter);
}
Ifel* GetNextIfel(iterator* item)
{
//fprintf(stderr, "GetNextIfel\n");
node* n = *item;
*item = n->next;
if (n->next)
return (Ifel*)ListGetData(n->next);
else
return NULL;
}
Ifel* CreateIfel(int id, Ifel* parent, Ifel* me, IfelType type, IfelDrawFn draw)
{
//fprintf(stderr, "CreateIfel\n");
me->id = id;
me->type = type;
me->draw = draw;
me->ifels = ListCreate();
// create the drawing surface
me->surface = SDL_CreateRGBSurface(SDL_SWSURFACE, me->loc.w, me->loc.h, 32, 0, 0, 0, 0);
if (!me->surface)
{
fprintf(stderr, "Error creating window surface\n");
return NULL;
}
SDL_SetColorKey(me->surface, SDL_SRCCOLORKEY, 0);
// add it to the list of windows to draw
if (!parent)
ListAddNode(ifels, me);
else
ListAddNode(parent->ifels, me);
// if there is no element with focus, set it now
if (!elFocus)
elFocus = me;
return me;
}
void DrawIfelChildren(Ifel* i)
{
iterator iter;
Ifel* el = GetFirstIfel(i, &iter);
while(el)
{
if (el->active && el->draw)
{
el->draw(el);
DrawIfelChildren(el);
SDL_BlitSurface(el->surface, NULL, i->surface, &el->loc);
}
el = GetNextIfel(&iter);
}
}
static void ModalOnKeyPressed(Ifel* el, int key)
{
//fprintf(stderr, "ModalOnKeyPressed\n");
if (key == 'y' || key == 'Y')
returnCode = MB_YES;
if (key == 'n' || key == 'N')
returnCode = MB_NO;
}
int ModalMessageBox(int id, const char* msg)
{
//fprintf(stderr, "ModalMessageBox\n");
MessageBox* mb = CreateMessageBox(id, msg);
mb->el.OnKeyPressed = ModalOnKeyPressed;
Ifel* prevFocus = GetFocus();
SetFocus(&mb->el);
// wait until there is a valid input from the user
returnCode = MB_UNDEFINED;
while (returnCode == MB_UNDEFINED)
{
if (!DispatchEvents())
{
quit = 1;
break;
}
Redraw();
}
// clean up and return
SetFocus(prevFocus);
DeleteMessageBox(mb);
return returnCode;
}
void DrawMessageBox(Ifel* i)
{
//MessageBox* mb = (MessageBox*)i;
//fprintf(stderr, "DrawMessageBox\n");
// render the messagebox to the ifel surface
//SDL_BlitSurface(mb->surface, NULL, i->surface, NULL);
}
MessageBox* CreateMessageBox(int id, const char* msg)
{
SDL_Surface* text;
if (!font)
{
return NULL;
}
MessageBox* mb = (MessageBox*)malloc(sizeof(MessageBox));
//fprintf(stderr, "Creating messagebox\n");
if (!mb)
{
fprintf(stderr, "Error creating messagebox\n");
return NULL;
}
memset(mb, 0, sizeof(MessageBox));
// render text to determine its size
SDL_Color textColor;
textColor.r = 0xFF;
textColor.g = 0;
textColor.b = 0;
text = TTF_RenderText_Solid(font, msg, textColor);
if (!text)
{
fprintf(stderr, "Error rendering text\n");
free(mb);
return NULL;
}
// calculate windows size based on text & icons
mb->el.loc.x = 0;
mb->el.loc.y = 0;
mb->el.loc.w = text->w + (MESSAGE_BOX_MARGIN * 2);
mb->el.loc.h = text->h + (MESSAGE_BOX_MARGIN * 2);
// set the location of the window on the screen
mb->el.loc.x = (screen->w - mb->el.loc.w) / 2;
mb->el.loc.y = (screen->h - mb->el.loc.h) / 2;
// add it to the list of windows to draw
CreateIfel(id, NULL, (Ifel*)mb, IFEL_MESSAGEBOX, DrawMessageBox);
// draw the message and the box
roundedBoxRGBA(mb->el.surface, 0, 0, mb->el.loc.w, mb->el.loc.h, 10, 0xF0, 0, 0, 0x30);
SDL_Rect loc;
loc.x = (mb->el.loc.w - text->w) / 2;
loc.y = (mb->el.loc.h - text->h) / 2;
loc.w = text->w;
loc.h = text->h;
SDL_BlitSurface(text, NULL, mb->el.surface, &loc);
SDL_FreeSurface(text);
mb->el.active = 1;
return mb;
}
void DeleteMessageBox(MessageBox* mb)
{
//fprintf(stderr, "DeleteMessageBox\n");
mb->el.active = 0;
DeleteIfel(NULL, &mb->el);
free(mb);
}
void DrawImage(Ifel* i)
{
Image* img = (Image*)i;
SDL_BlitSurface(img->surface, NULL, i->surface, NULL);
}
Image* CreateImage(int id, Ifel* parent, const char* bitmap)
{
//fprintf(stderr, "Creating image id %i\n", id);
// create the object
Image* img = (Image*)malloc(sizeof(Image));
if (!img)
{
fprintf(stderr, "Can't create image %s\n", bitmap);
return NULL;
}
memset(img, 0, sizeof(Image));
// load the image from disk
img->surface = IMG_Load(bitmap);
if (!img->surface)
{
fprintf(stderr, "Can't load image %s\n", bitmap);
free(img);
return NULL;
}
img->el.loc.x = 0;
img->el.loc.y = 0;
img->el.loc.w = img->surface->w;
img->el.loc.h = img->surface->h;
// add it to the list of windows to draw
CreateIfel(id, parent, &img->el, IFEL_IMAGE, DrawImage);
img->el.active = 1;
return img;
}
void DeleteImage(Image* img)
{
fprintf(stderr, "DeleteImage\n");
if (!img)
{
fprintf(stderr, "null image!\n");
return;
}
DeleteIfel(NULL, &img->el);
SDL_FreeSurface(img->surface);
free(img);
}
void DrawButton(Ifel* i)
{
Button* btn = (Button*)i;
//fprintf(stderr, "DrawButton\n");
switch(btn->state)
{
case BUTTON_DISABLED:
case BUTTON_NORMAL:
SDL_BlitSurface(btn->up, NULL, i->surface, NULL);
break;
case BUTTON_PRESSED:
SDL_BlitSurface(btn->down, NULL, i->surface, NULL);
break;
case BUTTON_HOVER:
SDL_BlitSurface(btn->hover, NULL, i->surface, NULL);
break;
}
}
void DrawTextButton(Ifel* i)
{
SDL_Surface* text;
SDL_Color textColor;
Button* btn = (Button*)i;
//fprintf(stderr, "DrawTextButton\n");
switch(btn->state)
{
case BUTTON_NORMAL:
textColor.r = 100;
textColor.g = 0;
textColor.b = 0;
roundedBoxRGBA(i->surface, 0, 0, i->surface->w, i->surface->h, 10, 100, 100, 0, 255);
break;
case BUTTON_PRESSED:
textColor.r = 50;
textColor.g = 0;
textColor.b = 0;
roundedBoxRGBA(i->surface, 0, 0, i->surface->w, i->surface->h, 10, 90, 90, 0, 255);
break;
case BUTTON_HOVER:
textColor.r = 150;
textColor.g = 0;
textColor.b = 0;
roundedBoxRGBA(i->surface, 0, 0, i->surface->w, i->surface->h, 10, 100, 100, 0, 255);
break;
case BUTTON_DISABLED:
textColor.r = 50;
textColor.g = 50;
textColor.b = 50;
roundedBoxRGBA(i->surface, 0, 0, i->surface->w, i->surface->h, 10, 100, 100, 100, 255);
break;
}
text = TTF_RenderText_Solid(buttonFont, btn->text, textColor);
if (!text)
{
fprintf(stderr, "Error rendering text\n");
return;
}
SDL_Rect loc;
loc.x = (i->surface->w - text->w) / 2;
loc.y = 0;
SDL_BlitSurface(text, NULL, i->surface, &loc);
}
Button* CreateTextButton(int id, Ifel* parent, SDL_Rect* size, const char* msg)
{
Button* button = (Button*)malloc(sizeof(Button));
if (!button)
{
fprintf(stderr, "Error allocating button\n");
return NULL;
}
memset(button, 0, sizeof(Button));
// set the internal variables
button->state = BUTTON_NORMAL;
button->el.loc.x = size->x;
button->el.loc.y = size->y;
button->el.loc.w = size->w;
button->el.loc.h = size->h;
// set button text
button->text = (char*)malloc(strlen(msg) + 1);
strcpy(button->text, msg);
// add it to the list of windows to draw
CreateIfel(id, parent, &button->el, IFEL_BUTTON, DrawTextButton);
button->el.active = 1;
return button;
}
Button* CreatePngButton(int id, Ifel* parent, int x, int y, const char* img_up, const char* img_down, const char* img_hover)
{
//fprintf(stderr, "CreateButton\n");
Button* button = (Button*)malloc(sizeof(Button));
if (!button)
{
fprintf(stderr, "Error allocating button\n");
return NULL;
}
memset(button, 0, sizeof(Button));
button->up = IMG_Load(img_up);
if (!button->up)
{
fprintf(stderr, "Can't load image %s\n", img_up);
goto img_up_fail;
}
button->down = IMG_Load(img_down);
if (!button->down)
{
fprintf(stderr, "Can't load image %s\n", img_down);
goto img_down_fail;
}
button->hover = IMG_Load(img_hover);
if (!button->hover)
{
fprintf(stderr, "Can't load image %s\n", img_hover);
goto img_hover_fail;
}
// set the internal variables
button->state = BUTTON_NORMAL;
button->el.loc.x = x;
button->el.loc.y = y;
button->el.loc.w = button->up->w;
button->el.loc.h = button->up->h;
// add it to the list of windows to draw
CreateIfel(id, parent, &button->el, IFEL_BUTTON, DrawButton);
button->el.active = 1;
return button;
SDL_FreeSurface(button->hover);
img_hover_fail:
SDL_FreeSurface(button->down);
img_down_fail:
SDL_FreeSurface(button->up);
img_up_fail:
free(button);
return NULL;
}
void EnableButton(Button* btn)
{
btn->state = BUTTON_NORMAL;
}
void DisableButton(Button* btn)
{
fprintf(stderr, "Disable Button\n");
btn->state = BUTTON_DISABLED;
}
void DeleteButton(Button* btn)
{
if (!btn)
return;
DeleteIfel(NULL, (Ifel*)btn);
SDL_FreeSurface(btn->up);
SDL_FreeSurface(btn->down);
SDL_FreeSurface(btn->hover);
free(btn);
}
int InitUI(void)
{
int returnCode = 0;
const char* fontName = "/usr/share/fonts/truetype/freefont/FreeSans.ttf";
fprintf(stderr, "InitUI\n");
//Start SDL
SDL_Init(SDL_INIT_EVERYTHING);
// Set up screen
screen = SDL_SetVideoMode(1024, 800, 32, SDL_SWSURFACE | SDL_SRCCOLORKEY);
if (!screen)
{
fprintf(stderr, "Error setting video mode\n");
returnCode = 1;
}
// TTF is 'true type fonts'
if (TTF_Init() != 0)
{
fprintf(stderr, "Error initializing TTF\n");
returnCode = 2;
}
font = TTF_OpenFont(fontName, 24);
if (!font)
{
fprintf(stderr, "Can't load font %s\n", fontName);
returnCode = 3;
}
buttonFont = TTF_OpenFont(fontName, 40);
if (!buttonFont)
{
fprintf(stderr, "Can't load font %s\n", fontName);
returnCode = 3;
}
ifels = ListCreate();
if (!ifels)
{
fprintf(stderr, "Can't create list\n");
returnCode = 4;
}
return returnCode;
}
int DestroyUI(void)
{
iterator i;
Ifel* el;
//fprintf(stderr, "DestroyUI\n");
// see if there are any ifels that weren't properly released
el = GetFirstIfel(NULL, &i);
while (el)
{
fprintf(stderr, "ifel %i (type %i) was not released properly\n", el->id, el->type);
el = GetNextIfel(&i);
}
// stop TTF
TTF_Quit();
//Quit SDL
SDL_Quit();
return 0;
}
static void Redraw(void)
{
// now redraw the screen by running through all Ifels in DFS manner
//fprintf(stderr, "Redraw\n");
if (elFocus)
fprintf(stderr, "Focus: %i\n", elFocus->id);
iterator i;
Ifel* el = GetFirstIfel(NULL, &i);
while(el)
{
if (el->active && el->draw)
{
el->draw(el);
DrawIfelChildren(el);
rectangleColor(el->surface, el->loc.x, el->loc.y,
el->loc.x + el->loc.w - 1, el->loc.y + el->loc.h - 1, 0xFF00FF);
SDL_BlitSurface(el->surface, NULL, screen, &el->loc);
}
el = GetNextIfel(&i);
}
SDL_Flip(screen);
}
// the main event loop (dispatcher) function
void Run(void)
{
fprintf(stderr, "Run\n");
while (!quit)
{
if (!DispatchEvents())
{
quit = 1;
break;
}
Redraw();
}
}
void Quit(void)
{
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
}
/* This is the main event loop for the UI. The SDL subsystem captures all
input devices (mouse, keyboard) and converts input into events. This
function handles those events by passing them to the appropriate handler
depending on various parameters such as the object with focus, or
location of the mouse. */
static int DispatchEvents(void)
{
SDL_Event event;
Ifel* el;
iterator i;
#ifdef DEBUG
//fprintf(stderr, "Dispatch\n");
#endif // DEBUG
// first handle any events
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
//fprintf(stderr, "key down\n");
if(elFocus->OnKeyPressed)
{
//fprintf(stderr, "object id:%i type:%i OnKeyPressed\n", elFocus->id, elFocus->type);
elFocus->OnKeyPressed(elFocus, event.key.keysym.sym);
}
#ifdef DEBUG
else
fprintf(stderr, "object with focus doesn't have OnKeyPressed method\n");
#endif // DEBUG
break;
case SDL_MOUSEMOTION:
//fprintf(stderr, "mouse move\n");
el = GetFirstIfel(NULL, &i);
while (el)
{
// mark the new element
if (event.button.x >= el->loc.x &&
event.button.x <= el->loc.x + el->loc.w &&
event.button.y >= el->loc.y &&
event.button.y <= el->loc.y + el->loc.h)
{
if (el->type == IFEL_BUTTON && ((Button*)el)->state != BUTTON_DISABLED)
{
// if the button isn't pressed, mark it as hover
if (((Button*)el)->state != BUTTON_PRESSED)
((Button*)el)->state = BUTTON_HOVER;
}
}
else
{
// unmark all other elements
if (el->type == IFEL_BUTTON && ((Button*)el)->state == BUTTON_HOVER)
((Button*)el)->state = BUTTON_NORMAL;
}
el = GetNextIfel(&i);
}
break;
case SDL_MOUSEBUTTONDOWN:
//fprintf(stderr, "mouse down\n");
el = GetFirstIfel(NULL, &i);
while (el)
{
if (event.button.x >= el->loc.x &&
event.button.x <= el->loc.x + el->loc.w &&
event.button.y >= el->loc.y &&
event.button.y <= el->loc.y + el->loc.h)
{
currentEl = el; // remember where the button went down
//fprintf(stderr, "mouse down on %i (state %i)\n", el->id, ((Button*)el)->state);
//fprintf(stderr, "event.x = %i event.y = %i\n", event.button.x, event.button.y);
//fprintf(stderr, "el.x = %i el.y = %i\n", el->loc.x, el->loc.y);
if (el->type == IFEL_BUTTON && ((Button*)el)->state != BUTTON_DISABLED)
((Button*)el)->state = BUTTON_PRESSED;
break;
}
el = GetNextIfel(&i);
}
break;
case SDL_MOUSEBUTTONUP:
//fprintf(stderr, "mouse up\n");
el = GetFirstIfel(NULL, &i);
while (el)
{
if (event.button.x >= el->loc.x &&
event.button.x <= el->loc.x + el->loc.w &&
event.button.y >= el->loc.y &&
event.button.y <= el->loc.y + el->loc.h)
{
// if the button came up on the same object on which it went down
if (currentEl == el)
{
if (el->type == IFEL_BUTTON)
{
if (((Button*)el)->state == BUTTON_DISABLED)
break;
else
((Button*)el)->state = BUTTON_HOVER;
}
if(el->OnMouseClick)
el->OnMouseClick(el);
}
currentEl = NULL;
break;
}
el = GetNextIfel(&i);
}
break;
case SDL_QUIT:
return 0;
}
}
return 1;
}