Source

Open PS2 Loader / src / dia.c

Full commit
  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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
/*
  Copyright 2009-2010 volca
  Licenced under Academic Free License version 3.0
  Review OpenUsbLd README & LICENSE files for further details.  
*/

#include "include/usbld.h"
#include "include/dia.h"
#include "include/gui.h"
#include "include/lang.h"
#include "include/pad.h"
#include "include/renderman.h"
#include "include/fntsys.h"
#include "include/themes.h"
#include "include/util.h"

// UI spacing of the dialogues (pixels between consecutive items)
#define UI_SPACING_H 10
#define UI_SPACING_V 2
// spacer ui element width (simulates tab)
#define UI_SPACER_WIDTH 50
// minimal pixel width of spacer
#define UI_SPACER_MINIMAL 30
// length of breaking line in pixels
#define UI_BREAK_LEN 600
// scroll speed (delay in ms!) when in dialogs
#define DIA_SCROLL_SPEED 300
// scroll speed (delay in ms!) when setting int value
#define DIA_INT_SET_SPEED 100

static int screenWidth;
static int screenHeight;

// Utility stuff
#define KEYB_MODE		2
#define KEYB_WIDTH 		12
#define KEYB_HEIGHT		4
#define KEYB_ITEMS		(KEYB_WIDTH * KEYB_HEIGHT)

static void diaDrawBoundingBox(int x, int y, int w, int h, int focus) {
		if (focus)
			rmDrawRect(x - 5, y, w + 10, h + 10, gTheme->selTextColor & gColFocus);
		else
			rmDrawRect(x - 5, y, w + 10, h + 10, gTheme->textColor & gColFocus);
}

int diaShowKeyb(char* text, int maxLen) {
	int i, j, len = strlen(text), selkeyb = 0, x, w;
	int selchar = 0, selcommand = -1;
	char c[2] = "\0\0";
	char keyb0[KEYB_ITEMS] = {
		'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
		'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']',
		'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '\\',
		'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', '`', ':' };

	char keyb1[KEYB_ITEMS] = {
		'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
		'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}',
		'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '|',
		'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', '~', '.' };
	char *keyb = keyb0;

	char *commands[KEYB_HEIGHT] = {_l(_STR_BACKSPACE), _l(_STR_SPACE), _l(_STR_ENTER), _l(_STR_MODE)};
	GSTEXTURE *cmdicons[KEYB_HEIGHT];
	cmdicons[0] = thmGetTexture(SQUARE_ICON);
	cmdicons[1] = thmGetTexture(TRIANGLE_ICON);
	cmdicons[2] = thmGetTexture(START_ICON);
	cmdicons[3] = thmGetTexture(SELECT_ICON);

	while(1) {
		readPads();
		
		rmStartFrame();
		guiDrawBGPlasma();
		rmDrawRect(0, 0, screenWidth, screenHeight, gColDarker);

		//Text
		fntRenderString(FNT_DEFAULT, 50, 120, ALIGN_NONE, 0, 0, text, gTheme->textColor);
		
		// separating line for simpler orientation
		rmDrawLine(25, 138, 615, 138, gColWhite);
		rmDrawLine(25, 139, 615, 139, gColWhite);

		for (j = 0; j < KEYB_HEIGHT; j++) {
			for (i = 0; i < KEYB_WIDTH; i++) {
				c[0] = keyb[i + j * KEYB_WIDTH];

				x = 50 + i * 31;
				w = fntRenderString(FNT_DEFAULT, x, 170 + 3 * UI_SPACING_H * j, ALIGN_NONE, 0, 0, c, gTheme->uiTextColor) - x;
				if ((i + j * KEYB_WIDTH) == selchar)
					diaDrawBoundingBox(x, 170 + 3 * UI_SPACING_H * j, w, UI_SPACING_H, 0);
			}
		}
		
		// Commands
		for (i = 0; i < KEYB_HEIGHT; i++) {
			if (cmdicons[i])
				rmDrawPixmap(cmdicons[i], 436, 170 + 3 * UI_SPACING_H * i, ALIGN_NONE, cmdicons[i]->Width, cmdicons[i]->Height, SCALING_RATIO, gDefaultCol);
			
			x = 477;
			w = fntRenderString(FNT_DEFAULT, x, 170 + 3 * UI_SPACING_H * i, ALIGN_NONE, 0, 0, commands[i], gTheme->uiTextColor) - x;
			if (i == selcommand)
				diaDrawBoundingBox(x, 170 + 3 * UI_SPACING_H * i, w, UI_SPACING_H, 0);
		}
		
		rmEndFrame();
		
		if (getKey(KEY_LEFT)) {
			if (selchar > -1) {
				if (selchar % KEYB_WIDTH)
					selchar--;
				else {
					selcommand = selchar / KEYB_WIDTH;
					selchar = -1;
				}
			} else {
				selchar = (selcommand + 1) * KEYB_WIDTH - 1;
				selcommand = -1;
			}
		} else if (getKey(KEY_RIGHT)) {
			if (selchar > -1) {
				if ((selchar + 1) % KEYB_WIDTH)
					selchar++;
				else {
					selcommand = selchar / KEYB_WIDTH;
					selchar = -1;
				}
			} else {
				selchar = selcommand * KEYB_WIDTH;
				selcommand = -1;
			}
		} else if (getKey(KEY_UP)) {
			if (selchar > -1)
				selchar = (selchar + KEYB_ITEMS - KEYB_WIDTH) % KEYB_ITEMS;
			else
				selcommand = (selcommand + KEYB_HEIGHT - 1) % KEYB_HEIGHT;
		} else if (getKey(KEY_DOWN)) {
			if (selchar > -1)
				selchar = (selchar + KEYB_WIDTH) % KEYB_ITEMS;
			else
				selcommand = (selcommand + 1) % KEYB_HEIGHT;
		} else if (getKeyOn(KEY_CROSS)) {
			if (len < (maxLen - 1) && selchar > -1) {
				len++;
				c[0] = keyb[selchar];
				strcat(text,c);
			} else if (selcommand == 0) {
				if (len > 0) { // BACKSPACE
					len--;
					text[len] = 0;
				}		
			} else if (selcommand == 1) {
				if (len < (maxLen - 1)) { // SPACE
					len++;
					c[0] = ' ';
					strcat(text,c);
				}
			} else if (selcommand == 2) {
				return 1; //ENTER
			} else if (selcommand == 3) {
				selkeyb = (selkeyb + 1) % KEYB_MODE; // MODE
				if (selkeyb == 0)
					keyb = keyb0;
				if (selkeyb == 1)
					keyb = keyb1;
			}
		} else if (getKey(KEY_SQUARE)) {
			if (len>0) { // BACKSPACE
				len--;
				text[len] = 0;
			}
		} else if (getKey(KEY_TRIANGLE)) {
			if (len < (maxLen - 1) && selchar > -1) { // SPACE
				len++;
				c[0] = ' ';
				strcat(text,c);
			}
		} else if (getKeyOn(KEY_START)) {
			return 1; //ENTER
		} else if (getKeyOn(KEY_SELECT)) {
			selkeyb = (selkeyb + 1) % KEYB_MODE; // MODE
			if (selkeyb == 0)
				keyb = keyb0;
			if (selkeyb == 1)
				keyb = keyb1;
		}
		
		if (getKey(KEY_CIRCLE))
			break;
	}
	
	return 0;
}

static int colPadSettings[16];

static int diaShowColSel(unsigned char *r, unsigned char *g, unsigned char *b) {
	int selc = 0;
	int ret = 0;
	unsigned char col[3];

	padStoreSettings(colPadSettings);

	col[0] = *r; col[1] = *g; col[2] = *b;
	setButtonDelay(KEY_LEFT, 1);
	setButtonDelay(KEY_RIGHT, 1);

	while(1) {
		readPads();
		
		rmStartFrame();
		guiDrawBGPlasma();
		rmDrawRect(0, 0,  screenWidth, screenHeight, gColDarker);
		
		// "Color selection"
		fntRenderString(FNT_DEFAULT, 50, 50, ALIGN_NONE, 0, 0, _l(_STR_COLOR_SELECTION), GS_SETREG_RGBA(0x060, 0x060, 0x060, 0x060));

		// 3 bars representing the colors...
		size_t co;
		int x, y;
		
		for (co = 0; co < 3; ++co) {
			unsigned char cc[3] = {0,0,0};
			cc[co] = col[co];
			
			x = 75;
			y = 75 + co * 25;

			u64 dcol = GS_SETREG_RGBA(cc[0], cc[1], cc[2], 0x80);

			if (selc == co)
				rmDrawRect(x, y, 200, 20, GS_SETREG_RGBA(0x060, 0x060, 0x060, 0x60));
			else
				rmDrawRect(x, y, 200, 20, GS_SETREG_RGBA(0x020, 0x020, 0x020, 0x60));
				
			rmDrawRect(x + 2, y + 2, 190.0f*(cc[co]*100/255)/100, 16, dcol);
		}

		// target color itself
		u64 dcol = GS_SETREG_RGBA(col[0],col[1],col[2], 0x80);
		
		x = 300;
		y = 75;
		
		rmDrawRect(x, y, 70, 70, GS_SETREG_RGBA(0x060, 0x060, 0x060, 0x60));
		rmDrawRect(x + 5, y + 5, 60, 60, dcol);

		rmEndFrame();
		
		if (getKey(KEY_LEFT)) {
			if (col[selc] > 0)
				col[selc]--;
		} else if (getKey(KEY_RIGHT)) {
			if (col[selc] < 255)
				col[selc]++;
		} else if (getKey(KEY_UP)) {
			if (selc > 0)
				selc--;		
		} else if (getKey(KEY_DOWN)) {
			if (selc < 2)
				selc++;		
		} else if (getKeyOn(KEY_CROSS)) {
			*r = col[0];
			*g = col[1];
			*b = col[2];
			ret = 1;
			break;
		} else if (getKeyOn(KEY_CIRCLE)) {
			ret = 0;
			break;
		}
	}

	padRestoreSettings(colPadSettings);
	return ret;
}



// ----------------------------------------------------------------------------
// --------------------------- Dialogue handling ------------------------------
// ----------------------------------------------------------------------------
static const char *diaGetLocalisedText(const char* def, int id) {
	if (id >= 0)
		return _l(id);
	
	return def;
}

/// returns true if the item is controllable (e.g. a value can be changed on it)
static int diaIsControllable(struct UIItem *ui) {
	return (ui->enabled && (ui->type >= UI_OK));
}

/// returns true if the given item should be preceded with nextline
static int diaShouldBreakLine(struct UIItem *ui) {
	return (ui->type == UI_SPLITTER || ui->type == UI_OK || ui->type == UI_BREAK);
}

/// returns true if the given item should be superseded with nextline
static int diaShouldBreakLineAfter(struct UIItem *ui) {
	return (ui->type == UI_SPLITTER);
}

static void diaDrawHint(int text_id) {
	int x, y;
	
	char* text = _l(text_id);
	
	x = screenWidth - fntCalcDimensions(FNT_DEFAULT, text) - 10;
	y = gTheme->usedHeight - 40;
	
	// render hint on the lower side of the screen.
	rmDrawRect(x, y, screenWidth - x, MENU_ITEM_HEIGHT + 10, gColDarker);
	fntRenderString(FNT_DEFAULT, x + 5, y + 5, ALIGN_NONE, 0, 0, text, gTheme->textColor);
}

/// renders an ui item (either selected or not)
/// sets width and height of the render into the parameters
static void diaRenderItem(int x, int y, struct UIItem *item, int selected, int haveFocus, int *w, int *h) {
	*h = UI_SPACING_H;
	
	// all texts are rendered up from the given point!
	u64 txtcol;
	
	if (diaIsControllable(item))
		txtcol = gTheme->uiTextColor;
	else
		txtcol = gTheme->textColor;

	// let's see what do we have here?
	switch (item->type) {
		case UI_TERMINATOR:
			return;

		case UI_BUTTON:	
		case UI_LABEL: {
			// width is text length in pixels...
			const char *txt = diaGetLocalisedText(item->label.text, item->label.stringId);
			if(txt && strlen(txt))
				*w = fntRenderString(FNT_DEFAULT, x, y, ALIGN_NONE, 0, 0, txt, txtcol) - x;
			else
				*w = fntRenderString(FNT_DEFAULT, x, y, ALIGN_NONE, 0, 0, _l(_STR_NOT_SET), txtcol) - x;

			break;
		}

		case UI_SPLITTER: {
			// a line. Thanks to the font rendering, we need to shift up by one font line
			*w = 0; // nothing to render at all
			int ypos = y - UI_SPACING_V / 2; //  gsFont->CharHeight +

			// to ODD lines
			ypos &= ~1;

			// two lines for lesser eye strain :)
			rmDrawLine(x, ypos    , x + UI_BREAK_LEN, ypos    , gColWhite);
			rmDrawLine(x, ypos + 1, x + UI_BREAK_LEN, ypos + 1, gColWhite);
			break;
		}

		case UI_BREAK:
			*w = 0; // nothing to render at all
			break;

		case UI_SPACER: {
			// next column divisible by spacer
			*w = (UI_SPACER_WIDTH - x % UI_SPACER_WIDTH);

			if (*w < UI_SPACER_MINIMAL) {
				x += *w + UI_SPACER_MINIMAL;
				*w += (UI_SPACER_WIDTH - x % UI_SPACER_WIDTH);
			}

			*h = 0;
			break;
		}

		case UI_OK: {
			const char *txt = _l(_STR_OK);
			*w = fntRenderString(FNT_DEFAULT, x, y, ALIGN_NONE, 0, 0, txt, txtcol) - x;
			break;
		}

		case UI_INT: {
			char tmp[10];

			snprintf(tmp, 10, "%d", item->intvalue.current);
			*w = fntRenderString(FNT_DEFAULT, x, y, ALIGN_NONE, 0, 0, tmp, txtcol) - x;
			break;
		}

		case UI_STRING: {
			if(strlen(item->stringvalue.text))
				*w = fntRenderString(FNT_DEFAULT, x, y, ALIGN_NONE, 0, 0, item->stringvalue.text, txtcol) - x;
			else
				*w = fntRenderString(FNT_DEFAULT, x, y, ALIGN_NONE, 0, 0, _l(_STR_NOT_SET), txtcol) - x;
			break;
		}

		case UI_PASSWORD: {
			char stars[32];
			int i;

			int len = min(strlen(item->stringvalue.text), 30);
			for (i = 0; i < len; ++i)
				stars[i] = '*';

			stars[i] = '\0';
			*w = fntRenderString(FNT_DEFAULT, x, y, ALIGN_NONE, 0, 0, stars, txtcol) - x;
			break;
		}

		case UI_BOOL: {
			const char *txtval = _l((item->intvalue.current) ? _STR_ON : _STR_OFF);
			*w = fntRenderString(FNT_DEFAULT, x, y, ALIGN_NONE, 0, 0, txtval, txtcol) - x;
			break;
		}

		case UI_ENUM: {
			const char* tv = item->intvalue.enumvalues[item->intvalue.current];

			if (!tv)
				tv = _l(_STR_NO_ITEMS);

			*w = fntRenderString(FNT_DEFAULT, x, y, ALIGN_NONE, 0, 0, tv, txtcol) - x;
			break;
		}

		case UI_COLOUR: {
			rmDrawRect(x, y + 3, 25, 17, txtcol);
			u64 dcol = GS_SETREG_RGBA(item->colourvalue.r, item->colourvalue.g, item->colourvalue.b, 0x80);
			rmDrawRect(x + 2, y + 5, 21, 13, dcol);

			*w = 25;
			*h = 17;
			break;
		}
	}

	if (selected)
		diaDrawBoundingBox(x, y, *w, *h, haveFocus);

	if (item->fixedWidth != 0) {
		int newSize;
		if (item->fixedWidth < 0)
			newSize = item->fixedWidth * screenWidth / -100;
		else
			newSize = item->fixedWidth;
		if (*w < newSize)
			*w = newSize;
	}

	if (item->fixedHeight != 0) {
		int newSize;
		if (item->fixedHeight < 0)
			newSize = item->fixedHeight * screenHeight / -100;
		else
			newSize = item->fixedHeight;
		if (*h < newSize)
			*h = newSize;
	}
}

/// renders whole ui screen (for given dialog setup)
void diaRenderUI(struct UIItem *ui, short inMenu, struct UIItem *cur, int haveFocus) {
	guiDrawBGPlasma();

	int x0 = 20;
	int y0 = 20;
	
	// render all items
	struct UIItem *rc = ui;
	int x = x0, y = y0, hmax = 0;
	
	while (rc->type != UI_TERMINATOR) {
		int w = 0, h = 0;

		if (diaShouldBreakLine(rc)) {
			x = x0;
			
			if (hmax > 0)
				y += hmax + UI_SPACING_H;
			
			hmax = 0;
		}
		
		diaRenderItem(x, y, rc, rc == cur, haveFocus, &w, &h);
		
		if (w > 0)
			x += w + UI_SPACING_V;
		
		hmax = (h > hmax) ? h : hmax;
		
		if (diaShouldBreakLineAfter(rc)) {
			x = x0;
			
			if (hmax > 0)
				y += hmax + UI_SPACING_H; 
			
			hmax = 0;
		}
		
		rc++;
	}

	if ((cur != NULL) && (!haveFocus) && (cur->hintId != -1)) {
		diaDrawHint(cur->hintId);
	}
}

/// sets the ui item value to the default again
static void diaResetValue(struct UIItem *item) {
	switch(item->type) {
		case UI_INT:
		case UI_BOOL:
			item->intvalue.current = item->intvalue.def;
			return;
		case UI_STRING:
		case UI_PASSWORD:
			strncpy(item->stringvalue.text, item->stringvalue.def, 32);
			return;
		default:
			return;
	}
}

static int diaHandleInput(struct UIItem *item, int *modified) {
	// circle loses focus, sets old values first
	if (getKeyOn(KEY_CIRCLE)) {
		diaResetValue(item);
		return 0;
	}
	
	// cross loses focus without setting default
	if (getKeyOn(KEY_CROSS)) {
		*modified = 0;
		return 0;
	}
	
	// UI item type dependant part:
	if (item->type == UI_BOOL) {
		// a trick. Set new value, lose focus
		item->intvalue.current = !item->intvalue.current;
		return 0;
	} if (item->type == UI_INT) {
		// to be sure
		setButtonDelay(KEY_UP, DIA_INT_SET_SPEED);
		setButtonDelay(KEY_DOWN, DIA_INT_SET_SPEED);

		// up and down
		if (getKey(KEY_UP) && (item->intvalue.current < item->intvalue.max))
			item->intvalue.current++;
		else if (getKey(KEY_DOWN) && (item->intvalue.current > item->intvalue.min))
			item->intvalue.current--;
		else
			*modified = 0;
	} else if ((item->type == UI_STRING) || (item->type == UI_PASSWORD)) {
		char tmp[32];
		strncpy(tmp, item->stringvalue.text, 32);

		if (item->stringvalue.handler) {
			if (item->stringvalue.handler(tmp, 32))
				strncpy(item->stringvalue.text, tmp, 32);
		} else {
			if (diaShowKeyb(tmp, 32))
				strncpy(item->stringvalue.text, tmp, 32);
		}

		return 0;
	} else if (item->type == UI_ENUM) {
		int cur = item->intvalue.current;

		if (item->intvalue.enumvalues[cur] == NULL) {
			if (cur > 0)
				item->intvalue.current--;
			else
				return 0;
		}

		if (getKey(KEY_UP) && (item->intvalue.current > 0))
			item->intvalue.current--;
		else if (getKey(KEY_DOWN) && (item->intvalue.enumvalues[item->intvalue.current + 1] != NULL))
			item->intvalue.current++;
		else
			*modified = 0;
	} else if (item->type == UI_COLOUR) {
		if (!diaShowColSel(&item->colourvalue.r, &item->colourvalue.g, &item->colourvalue.b))
			*modified = 0;

		return 0;
	}
	
	return 1;
}

static struct UIItem *diaGetFirstControl(struct UIItem *ui) {
	struct UIItem *cur = ui;
	
	while (!diaIsControllable(cur)) {
		if (cur->type == UI_TERMINATOR)
			return ui; 
		
		cur++;
	}
	
	return cur;
}

static struct UIItem *diaGetLastControl(struct UIItem *ui) {
	struct UIItem *last = diaGetFirstControl(ui);
	struct UIItem *cur = last;
	
	while (cur->type != UI_TERMINATOR) {
		cur++;
		
		if (diaIsControllable(cur))
			last = cur;
	}
	
	return last;
}

static struct UIItem *diaGetNextControl(struct UIItem *cur, struct UIItem* dflt) {
	while (cur->type != UI_TERMINATOR) {
		cur++;
		
		if (diaIsControllable(cur))
			return cur;
	}
	
	return dflt;
}

static struct UIItem *diaGetPrevControl(struct UIItem* cur, struct UIItem *ui) {
	struct UIItem *newf = cur;
	
	while (newf != ui) {
		newf--;
		
		if (diaIsControllable(newf))
			return newf;
	}
	
	return cur;
}

/// finds first control on previous line...
static struct UIItem *diaGetPrevLine(struct UIItem* cur, struct UIItem *ui) {
	struct UIItem *newf = cur;
	
	int lb = 0;
	int hadCtrl = 0; // had the scanned line any control?
	
	while (newf != ui) {
		newf--;
		
		if ((lb > 0) && (diaIsControllable(newf)))
			hadCtrl = 1;
		
		if (diaShouldBreakLine(newf)) {// is this a line break?
			if (hadCtrl || lb == 0) {
				lb++;
				hadCtrl = 0;
			}
		}
		
		// twice the break? find first control
		if (lb == 2) 
			return diaGetFirstControl(newf);
	}
	
	return cur;
}

static struct UIItem *diaGetNextLine(struct UIItem* cur, struct UIItem *ui) {
	struct UIItem *newf = cur;
	
	int lb = 0;
	
	while (newf->type != UI_TERMINATOR) {
		newf++;
		
		if (diaShouldBreakLine(newf)) {// is this a line break?
				lb++;
		}
		
		if (lb == 1)
			return diaGetNextControl(newf, cur);
	}
	
	return cur;
}

static int diaPadSettings[16];

static void diaStoreScrollSpeed(void) {
	padStoreSettings(diaPadSettings);
}

static void diaRestoreScrollSpeed(void) {
	padRestoreSettings(diaPadSettings);
}

static struct UIItem* diaFindByID(struct UIItem* ui, int id) {
	while (ui->type != UI_TERMINATOR) {
		if (ui->id == id)
			return ui;

		ui++;
	}

	return NULL;
}

int diaExecuteDialog(struct UIItem *ui, int uiId, short inMenu, int (*updater)(int modified)) {
	rmGetScreenExtents(&screenWidth, &screenHeight);
	
	struct UIItem *cur = NULL;
	if (uiId != -1)
		cur = diaFindByID(ui, uiId);

	if (!cur)
		cur = diaGetFirstControl(ui);
	
	// what? no controllable item? Exit!
	if (cur == ui)
		return -1;
	
	int haveFocus = 0, modified;
	
	diaStoreScrollSpeed();
	
	// slower controls for dialogs
	setButtonDelay(KEY_UP, DIA_SCROLL_SPEED);
	setButtonDelay(KEY_DOWN, DIA_SCROLL_SPEED);

	// okay, we have the first selectable item
	// we can proceed with rendering etc. etc.
	while (1) {
		rmStartFrame();
		diaRenderUI(ui, inMenu, cur, haveFocus);
		rmEndFrame();
		
		readPads();
		
		if (haveFocus) {
			modified = 1;
			haveFocus = diaHandleInput(cur, &modified);
			
			if (!haveFocus) {
				setButtonDelay(KEY_UP, DIA_SCROLL_SPEED);
				setButtonDelay(KEY_DOWN, DIA_SCROLL_SPEED);
			}
		} else {
			modified = 0;
			if (getKey(KEY_LEFT)) {
				struct UIItem *newf = diaGetPrevControl(cur, ui);
				
				if (newf == cur)
					cur = diaGetLastControl(ui);
				else 
					cur = newf;
			}
			
			if (getKey(KEY_RIGHT)) {
				struct UIItem *newf = diaGetNextControl(cur, cur);
				
				if (newf == cur)
					cur = diaGetFirstControl(ui);
				else 
					cur = newf;
			}
			
			if(getKey(KEY_UP)) {
				// find
				struct UIItem *newf = diaGetPrevLine(cur, ui);
				
				if (newf == cur)
					cur = diaGetLastControl(ui);
				else 
					cur = newf;
			}
			
			if(getKey(KEY_DOWN)) {
				// find
				struct UIItem *newf = diaGetNextLine(cur, ui);
				
				if (newf == cur)
					cur = diaGetFirstControl(ui);
				else 
					cur = newf;
			}
			
			// circle breaks focus or exits with false result
			if (getKeyOn(KEY_CIRCLE)) {
				diaRestoreScrollSpeed();
				return 0;
			}
			
			// see what key events we have
			if (getKeyOn(KEY_CROSS)) {
				haveFocus = 1;
				
				if (cur->type == UI_BUTTON) {
					diaRestoreScrollSpeed();
					return cur->id;
				}

				if (cur->type == UI_OK) {
					diaRestoreScrollSpeed();
					return 1;
				}
			}
		}

		if (updater) {
			int updResult = updater(modified);
			if (updResult)
				return updResult;
		}
	}
}

void diaSetEnabled(struct UIItem* ui, int id, int enabled) {
	struct UIItem *item = diaFindByID(ui, id);

	if (!item)
		return;

	item->enabled = enabled;
}

int diaGetInt(struct UIItem* ui, int id, int *value) {
	struct UIItem *item = diaFindByID(ui, id);
	
	if (!item)
		return 0;
	
	if ((item->type == UI_INT) || (item->type == UI_BOOL) || (item->type == UI_ENUM)) {
		*value = item->intvalue.current;
		return 1;
	}
	
	return 0;
}

int diaSetInt(struct UIItem* ui, int id, int value) {
	struct UIItem *item = diaFindByID(ui, id);
	
	if (!item)
		return 0;
	
	if ((item->type == UI_INT) || (item->type == UI_BOOL) || (item->type == UI_ENUM)) {
		item->intvalue.def = value;
		item->intvalue.current = value;
		return 1;
	}
	
	return 0;
}

int diaGetString(struct UIItem* ui, int id, char *value) {
	struct UIItem *item = diaFindByID(ui, id);
	
	if (!item)
		return 0;
	
	if ((item->type == UI_STRING) || (item->type == UI_PASSWORD)) {
		strncpy(value, item->stringvalue.text, 32);
		return 1;
	}

	return 0;
}

int diaSetString(struct UIItem* ui, int id, const char *text) {
	struct UIItem *item = diaFindByID(ui, id);
	
	if (!item)
		return 0;
	
	if ((item->type == UI_STRING) || (item->type == UI_PASSWORD)) {
		strncpy(item->stringvalue.def, text, 32);
		strncpy(item->stringvalue.text, text, 32);
		return 1;
	}
	
	return 0;
}

int diaGetColor(struct UIItem* ui, int id, unsigned char *col) {
	struct UIItem *item = diaFindByID(ui, id);
	
	if (!item)
		return 0;
	
	if (item->type != UI_COLOUR)
		return 0;

	col[0] = item->colourvalue.r;
	col[1] = item->colourvalue.g;
	col[2] = item->colourvalue.b;
	return 1;
}

int diaSetColor(struct UIItem* ui, int id, const unsigned char *col) {
	struct UIItem *item = diaFindByID(ui, id);
	
	if (!item)
		return 0;
	
	if (item->type != UI_COLOUR)
		return 0;

	item->colourvalue.r = col[0];
	item->colourvalue.g = col[1];
	item->colourvalue.b = col[2];
	return 1;
}

int diaSetU64Color(struct UIItem* ui, int id, u64 col) {
	struct UIItem *item = diaFindByID(ui, id);

	if (!item)
		return 0;

	if (item->type != UI_COLOUR)
		return 0;

	item->colourvalue.r = col & 0xFF;
	col >>= 8;
	item->colourvalue.g = col & 0xFF;
	col >>= 8;
	item->colourvalue.b = col & 0xFF;
	return 1;
}

int diaSetLabel(struct UIItem* ui, int id, const char *text) {
	struct UIItem *item = diaFindByID(ui, id);
	
	if (!item)
		return 0;
	
	if ((item->type == UI_LABEL) || (item->type == UI_BUTTON)) {
		item->label.text = text;
		return 1;
	}
	
	return 0;
}

int diaSetEnum(struct UIItem* ui, int id, const char **enumvals) {
	struct UIItem *item = diaFindByID(ui, id);
	
	if (!item)
		return 0;
	
	if (item->type != UI_ENUM)
		return 0;
	
	item->intvalue.enumvalues = enumvals;
	return 1;
}