forked from schellingb/dosbox-pure
-
Notifications
You must be signed in to change notification settings - Fork 11
/
dosbox_pure_osd.h
2146 lines (1963 loc) · 95.4 KB
/
dosbox_pure_osd.h
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
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2020-2024 Bernhard Schelling
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
enum DBP_OSDMode { DBPOSD_MAIN, DBPOSD_OSK, DBPOSD_MAPPER, _DBPOSD_COUNT, DBPOSD_CLOSED };
static void DBP_StartOSD(DBP_OSDMode mode, struct DBP_PureMenuState* in_main = NULL);
static void DBP_CloseOSD();
static bool DBP_FullscreenOSD;
static bool DBP_MenuInterceptorRefreshSystem;
struct DBP_BufferDrawing : DBP_Buffer
{
enum { CW = 8 }; // char width
enum { MWIDTH = 234 + 10*4 + 2*3 };
enum EColors : Bit32u
{
BGCOL_SELECTION = 0x117EB7, BGCOL_SCROLL = 0x093F5B, BGCOL_MENU = 0x1A1E20, BGCOL_HEADER = 0x582204, BGCOL_STARTMENU = 0xFF111111,
COL_MENUTITLE = 0xFFFBD655, COL_CONTENT = 0xFFFFAB91,
COL_LINEBOX = 0xFFFF7126,
COL_HIGHLIGHT = 0xFFBDCDFB, COL_NORMAL = 0xFF4DCCF5, COL_DIM = 0xFF4B7A93, COL_WHITE = 0xFFFFFFFF, COL_WARN = COL_LINEBOX, COL_HEADER = 0xFF9ECADE,
BGCOL_BTNOFF = 0x5F3B27, BGCOL_BTNON = 0xAB6037, BGCOL_BTNHOVER = 0x895133, COL_BTNTEXT = 0xFFFBC6A3,
BGCOL_KEY = BGCOL_BTNOFF, BGCOL_KEYHOVER = BGCOL_BTNON, BGCOL_KEYPRESS = 0xE46E2E, BGCOL_KEYHELD = 0xC9CB35, BGCOL_KEYOUTLINE = 0x000000, COL_KEYTEXT = 0xFFF8EEE8,
};
Bit32u GetThickness() { return (width < (MWIDTH + 10) ? 1 : ((int)width - 10) / MWIDTH); }
void PrintOutlined(int lh, int x, int y, const char* msg, Bit32u col = 0xFFFFFFFF, Bit32u colol = 0xFF000000)
{
if ((colol & 0xFF000000) != 0xFF000000) colol = (((colol >> 24) / 2) << 24) | (colol & 0xFFFFFF);
for (int i = 0; i < 9; i++) if (i != 4) Print(lh, x+((i%3)-1), y+((i/3)-1), msg, colol);
Print(lh, x, y, msg, col);
}
void PrintCenteredOutlined(int lh, int x, int w, int y, const char* msg, Bit32u col = 0xFFFFFFFF, Bit32u colol = 0xFF000000)
{
PrintOutlined(lh, x + (int)((w-strlen(msg)*CW)/2), y, msg, col, colol);
}
void Print(int lh, int x, int y, const char* msg, Bit32u col = 0xFFFFFFFF, int maxw = 0xFFFFFF)
{
DBP_ASSERT(y >= 0 && y < (int)height);
const Bit8u* fnt = (lh == 8 ? int10_font_08 : int10_font_14);
const int ch = (lh == 8 ? 8 : 14);
for (const char* p = msg, *pEnd = p + (maxw / CW); *p && p != pEnd; p++)
DrawChar(fnt, ch, x+CW*(int)(p - msg), y, *p, col);
}
void DrawChar(const Bit8u* fnt, int ch, int x, int y, int c, Bit32u col = 0xFFFFFFFF)
{
if (x < 0 || x + CW >= (int)width) return;
const Bit8u* ltr = &fnt[(Bit8u)c * ch], *ltrend = ltr + ch;
Bit32u w = width, *py = video + (w * y) + (x);
if ((col & 0xFF000000) == 0xFF000000)
{ for (; ltr != ltrend; py += w, ltr++) for (Bit32u *px = py, bit = 256; bit; px++) if (*ltr & (bit>>=1)) *px = col; }
else
{ for (AlphaBlendPrep a(col); ltr != ltrend; py += w, ltr++) for (Bit32u *px = py, bit = 256; bit; px++) if (*ltr & (bit>>=1)) AlphaBlend(px, a); }
}
void DrawBox(int x, int y, int w, int h, Bit32u colfill, Bit32u colline)
{
if (w < 8) { DBP_ASSERT(false); return; }
int y1 = y, y2 = y1+4, y4 = y1+h, y3 = y4-4, yBox = y3-y2, yAll = y4-y1;
int x1 = x, x2 = x1+4, x4 = x1+w, x3 = x4-4, xBox = x3-x2, xAll = x4-x1;
Bit32u *v = video + width * y1 + x1;
AlphaBlendFillRect(x1,y1,xAll,yAll, colfill);
AlphaBlendPrep lna(colline);
for (Bit32u *p = v+4, *pEnd = p + xBox, *p2 = p + width * (yAll-1); p != pEnd; p++, p2++) { AlphaBlend(p, lna); AlphaBlend(p2, lna); }
for (Bit32u *p = v+4*width, *pEnd = p + yBox*width; p != pEnd; p+=width) { AlphaBlend(p, lna); AlphaBlend(p+xAll-1, lna); }
for (int i = 0; i != 16; i++)
{
int a = i % 4, b = i/4, c = a*b<3;
if (!c) continue;
AlphaBlend(v+a+b*width, lna);
AlphaBlend(v+xAll-1-a+b*width, lna);
AlphaBlend(v+xAll-1-a+(yAll-1-b)*width, lna);
AlphaBlend(v+a+(yAll-1-b)*width, lna);
}
}
void DrawRect(int x, int y, int w, int h, Bit32u col)
{
for (Bit32u *p = video + width * y + x, *pEnd = p + w, *p2 = p + width * (h-1); p != pEnd; p++, p2++) *p = *p2 = col;
for (Bit32u *p = video + width * y + x, *pEnd = p + width * h; p != pEnd; p+=width) *p = p[w-1] = col;
}
void FillRect(int x, int y, int w, int h, Bit32u col)
{
for (Bit32u *py = video + width * y + x; h--; py += width)
for (Bit32u *p = py, *pEnd = p + w; p != pEnd; p++)
*p = col;
}
void AlphaBlendFillRect(int x, int y, int w, int h, Bit32u col)
{
AlphaBlendPrep a((((col >> 24) ? (col >> 24) : dbp_alphablend_base) << 24) | (col & 0xFFFFFF));
for (Bit32u *py = video + width * y + x; h--; py += width)
for (Bit32u *p = py, *pEnd = p + w; p != pEnd; p++)
AlphaBlend(p, a);
}
void AlphaBlendDrawRect(int x, int y, int w, int h, Bit32u col)
{
DBP_ASSERT((col >> 24) == 0xFF || (col >> 24) == 0);
AlphaBlendPrep a((((col >> 24) ? (col >> 24) : dbp_alphablend_base) << 24) | (col & 0xFFFFFF));
for (Bit32u *p = video + width * y + x, *pEnd = p + w, *p2 = p + width * (h-1); p != pEnd; p++, p2++) AlphaBlend(p, a), AlphaBlend(p2, a);
for (Bit32u *p = video + width * y + x, *pEnd = p + width * h; p != pEnd; p+=width) AlphaBlend(p, a), AlphaBlend(p+w-1, a);
}
void AlphaBlendFillCircle(int cx, int cy, int rad, Bit32u col)
{
AlphaBlendPrep a(col);
for (int radsq = rad*rad, w = width, y = (cy - rad < 0 ? 0 : cy - rad), ymax = (cy + rad >= (int)height ? (int)height : cy + rad); y < ymax; y++)
for (int x = (cx - rad < 0 ? 0 : cx - rad), xmax = (cx + rad >= (int)width ? (int)width : cx + rad); x < xmax; x++)
{
int dx = abs(x - cx), dy = abs(y - cy), dsq = (dx*dx)+(dy*dy);
if (dsq >= radsq) continue;
AlphaBlend(video + y*w + x, a);
}
}
void AlphaBlendDrawCircle(int cx, int cy, int rad1, int rad2, Bit32u col)
{
AlphaBlendPrep a(col);
for (int rad1sq = rad1*rad1, rad2sq = rad2*rad2, w = width, y = (cy - rad2 < 0 ? 0 : cy - rad2), ymax = (cy + rad2 >= (int)height ? (int)height : cy + rad2); y < ymax; y++)
for (int x = (cx - rad2 < 0 ? 0 : cx - rad2), xmax = (cx + rad2 >= (int)width ? (int)width : cx + rad2); x < xmax; x++)
{
int dx = abs(x - cx), dy = abs(y - cy), dsq = (dx*dx)+(dy*dy);
if (dsq < rad1sq || dsq >= rad2sq) continue;
AlphaBlend(video + y*w + x, a);
}
}
struct AlphaBlendPrep
{
Bit32u na, arb, aag;
explicit INLINE AlphaBlendPrep(Bit32u col) { Bit32u a = (col & 0xFF000000) >> 24; na = (255 - a); arb = (a * (col & 0x00FF00FF)); aag = (a * (0x01000000 | ((col & 0x0000FF00) >> 8))); }
};
static INLINE void AlphaBlend(Bit32u* p1, const AlphaBlendPrep& a)
{
*p1 = (((((a.na * (*p1 & 0x00FF00FF)) + a.arb) >> 8) & 0x00FF00FF) | (((a.na * ((*p1 & 0xFF00FF00) >> 8)) + a.aag) & 0xFF00FF00));
}
bool DrawButtonAt(Bit32u blend, int btny, int lh, int padu, int padd, int btnx, int btnr, bool on, const struct DBP_MenuMouse& m, const char* txt, Bit32u blendtxt = 0xFF000000);
INLINE bool DrawButton(Bit32u blend, int btny, int lh, int i, int n, int mrgn, bool on, const struct DBP_MenuMouse& m, const char* txt, Bit32u blendtxt = 0xFF000000)
{ int w = width-mrgn*2; return DrawButtonAt(blend, btny, lh, 4, 4, (!i ? mrgn : mrgn + (w*i/n + 2)), (i == (n-1) ? mrgn + w : mrgn + (w*(i+1)/n - 2)), on, m, txt, blendtxt); }
};
DBP_STATIC_ASSERT(sizeof(DBP_BufferDrawing) == sizeof(DBP_Buffer)); // drawing is just for function expansions, we're just casting one to the other
struct DBP_MenuMouse
{
float x, y, jx, jy; Bit16u bw, bh; Bit16s mx, my; Bit8s kx, ky, mspeed; Bit8u realmouse : 1, left_pressed : 1, left_up : 1, right_up : 1, wheel_down : 1, wheel_up : 1, ignoremove : 1;
DBP_MenuMouse() { memset(this, 0, sizeof(*this)); }
void Reset() { mspeed = 2; left_pressed = false; if (realmouse) mx = dbp_mouse_x, my = dbp_mouse_y; }
void Input(DBP_Event_Type type, int val, int val2)
{
switch (type)
{
case DBPET_MOUSEUP:
if (val == 0) left_pressed = false, left_up = true; //left
if (val == 1) right_up = true; //right
break;
case DBPET_MOUSEDOWN:
if (val == 0) left_pressed = true; //left
/* fall through */
case DBPET_MOUSEMOVE:
mx = dbp_mouse_x;
my = dbp_mouse_y;
break;
case DBPET_KEYDOWN:
switch ((KBD_KEYS)val)
{
case KBD_left: case KBD_kp4: kx = -1; break;
case KBD_right: case KBD_kp6: kx = 1; break;
case KBD_up: case KBD_kp8: ky = -1; break;
case KBD_down: case KBD_kp2: ky = 1; break;
case KBD_kpminus: wheel_up = true; break; // mouse wheel up
case KBD_kpplus: wheel_down = true; break; // mouse wheel down
}
break;
case DBPET_KEYUP:
switch ((KBD_KEYS)val)
{
case KBD_left: case KBD_kp4: case KBD_right: case KBD_kp6: kx = 0; break;
case KBD_up: case KBD_kp8: case KBD_down: case KBD_kp2: ky = 0; break;
}
break;
case DBPET_JOY1X: case DBPET_JOY2X: case DBPET_JOYMX: jx = DBP_GET_JOY_ANALOG_VALUE(val); break;
case DBPET_JOY1Y: case DBPET_JOY2Y: case DBPET_JOYMY: jy = DBP_GET_JOY_ANALOG_VALUE(val); break;
case DBPET_MOUSESETSPEED: mspeed = (val > 0 ? 4 : 1); break;
case DBPET_MOUSERESETSPEED: mspeed = 2; break;
}
}
bool Update(DBP_BufferDrawing& buf, bool joykbd)
{
float oldmx = x, oldmy = y;
Bit32u newres = buf.width * buf.height;
if (bw != buf.width || bh != buf.height)
{
x = (bw ? (x * buf.width / bw) : (buf.width * .5f));
y = (bh ? (y * buf.height / bh) : (buf.height * .75f));
bw = buf.width, bh = buf.height;
}
if (mx || my)
{
float newx = (float)(mx+0x7fff)*buf.width/0xFFFE, newy = (float)(my+0x7fff)*buf.height/0xFFFE;
mx = my = 0;
realmouse = true;
if (newx == x && newy == y) return false;
x = newx, y = newy;
}
else if (jx || kx || jy || ky)
{
realmouse = false;
if (!joykbd) return false;
x += (jx + kx) * mspeed * buf.width / 320;
y += (jy + ky) * mspeed * buf.height / 240;
}
else return false;
if (x < 1) x = (float)1;
if (x > buf.width-2) x = (float)(buf.width-2);
if (y < 1) y = (float)1;
if (y > buf.height-2) y = (float)(buf.height-2);
if (ignoremove) { ignoremove = false; return false; }
return true;
}
void Draw(DBP_BufferDrawing& buf, bool joykbd)
{
// Draw white mouse cursor with black outline
left_up = right_up = wheel_up = wheel_down = false;
if (!realmouse && !joykbd) return;
for (Bit32u thick = buf.GetThickness(), midc = 6*thick, maxc = 8*thick, *v = buf.video, w = buf.width, h = buf.height, i = 0; i != 9; i++)
{
Bit32u n = (i < 4 ? i : (i < 8 ? i+1 : 4)), px = (Bit32u)x + (n%3)-1, py = (Bit32u)y + (n/3)-1, ccol = (n == 4 ? 0xFFFFFFFF : 0xFF000000);
Bit32u *pp, *p = v + py * w + px, *pendx = p - px + w, *pendy = v + w * h;
for (Bit32u c = 0; c != maxc; c++)
{
if (c < midc && (pp = (p + c * w)) < pendy) *pp = ccol; // line down
if ((pp = (p + c)) < pendx)
{
if (c < midc) *pp = ccol; // line right
if ((pp += c * w) < pendy) *pp = ccol; // line diagonal
}
}
}
}
};
bool DBP_BufferDrawing::DrawButtonAt(Bit32u blend, int btny, int lh, int padu, int padd, int btnx, int btnr, bool on, const DBP_MenuMouse& m, const char* txt, Bit32u blendtxt)
{
int btnd = btny+lh+padu+padd, btnw = btnr - btnx;
bool hover = (m.y >= btny && m.y < btnd && m.x >= btnx && m.x < btnr && m.realmouse);
DrawBox(btnx, btny, btnw, btnd - btny, (on ? BGCOL_BTNON : (hover ? BGCOL_BTNHOVER : BGCOL_BTNOFF)) | blend, (on ? BGCOL_BTNOFF : BGCOL_BTNON) | blendtxt);
PrintCenteredOutlined(lh, btnx, btnw, btny+padu, txt, (COL_BTNTEXT & 0xFFFFFF) | blendtxt, blendtxt);
return (hover && !on);
}
struct DBP_MenuState
{
enum ItemType : Bit8u { IT_NONE, _IT_CUSTOM, };
enum Result : Bit8u { RES_NONE, RES_OK, RES_CANCEL, RES_CLOSESCREENKEYBOARD, RES_CHANGEMOUNTS, RES_REFRESHSYSTEM };
bool refresh_mousesel, scroll_unlocked, hide_sel, show_popup;
int sel, scroll, joyx, joyy, scroll_jump, click_sel;
Bit32u open_ticks;
DBP_Event_Type held_event; KBD_KEYS held_key; Bit32u held_ticks;
struct Item { Bit8u type, pad; Bit16s info; std::string str; INLINE Item() {} INLINE Item(Bit8u t, Bit16s i = 0, const char* s = "") : type(t), info(i), str(s) {} };
std::vector<Item> list;
DBP_MenuState() : refresh_mousesel(true), scroll_unlocked(false), hide_sel(false), show_popup(false), sel(0), scroll(-1), joyx(0), joyy(0), scroll_jump(0), click_sel(-1), open_ticks(DBP_GetTicks()), held_event(_DBPET_MAX) { }
void Input(DBP_Event_Type type, int val, int val2)
{
Result res = RES_NONE;
int sel_change = 0, x_change = 0;
switch (type)
{
case DBPET_KEYDOWN:
switch ((KBD_KEYS)val)
{
case KBD_left: case KBD_kp4: x_change--; break;
case KBD_right: case KBD_kp6: x_change++; break;
case KBD_up: case KBD_kp8: sel_change--; break;
case KBD_down: case KBD_kp2: sel_change++; break;
case KBD_pageup: sel_change -= 12; break;
case KBD_pagedown: sel_change += 12; break;
case KBD_home: sel_change -= 99999; break;
case KBD_end: sel_change += 99999; break;
}
break;
case DBPET_KEYUP:
switch ((KBD_KEYS)val)
{
case KBD_enter: case KBD_kpenter: case KBD_space: res = RES_OK; break;
case KBD_esc: case KBD_backspace: res = RES_CANCEL; break;
}
if (held_event == DBPET_KEYDOWN) held_event = _DBPET_MAX;
break;
case DBPET_ONSCREENKEYBOARDUP: res = RES_CLOSESCREENKEYBOARD; break;
case DBPET_CHANGEMOUNTS: res = RES_CHANGEMOUNTS; break;
case DBPET_REFRESHSYSTEM: res = RES_REFRESHSYSTEM; break;
case DBPET_MOUSEMOVE:
scroll_unlocked = true;
break;
case DBPET_MOUSEDOWN:
if (val == 0) click_sel = (hide_sel ? -1 : sel);
break;
case DBPET_MOUSEUP:
if (val == 0 && click_sel == sel) res = RES_OK; // left
if (val == 1) res = RES_CANCEL; // right
break;
case DBPET_JOY1X: case DBPET_JOY2X:
if (joyx < 16000 && val >= 16000) x_change++;
if (joyx > -16000 && val <= -16000) x_change--;
if (held_event == type && val > -16000 && val < 16000) held_event = _DBPET_MAX;
joyx = val;
break;
case DBPET_JOY1Y: case DBPET_JOY2Y:
if (joyy < 16000 && val >= 16000) sel_change += (type == DBPET_JOY1Y ? 1 : 12);
if (joyy > -16000 && val <= -16000) sel_change -= (type == DBPET_JOY1Y ? 1 : 12);
if (held_event == type && val > -16000 && val < 16000) held_event = _DBPET_MAX;
joyy = val;
break;
case DBPET_JOY1DOWN: case DBPET_JOY2DOWN: if (val == 0) res = RES_OK; break; // B/Y button
case DBPET_JOY1UP: case DBPET_JOY2UP: if (val == 1) res = RES_CANCEL; break; // A/X button
}
if (res && (DBP_GetTicks() - open_ticks) < 200U)
res = RES_NONE; // ignore already pressed buttons when opening
if (sel_change || x_change)
{
if (held_event == _DBPET_MAX) { held_event = type; held_ticks = DBP_GetTicks() + 300; }
if (sel_change == -1) held_key = KBD_up;
else if (sel_change == 1) held_key = KBD_down;
else if (sel_change == -12) held_key = KBD_pageup;
else if (sel_change == 12) held_key = KBD_pagedown;
else if (x_change == -1) held_key = KBD_left;
else if (x_change == 1) held_key = KBD_right;
else held_event = _DBPET_MAX;
scroll_unlocked = false;
}
DBP_ASSERT(list.size());
for (int count = (int)list.size(); !res && sel_change && !show_popup;)
{
if (hide_sel) { hide_sel = false; break; }
sel += sel_change;
if (sel >= 0 && sel < count) { }
else if (sel_change > 1) sel = count - 1;
else if (sel_change == -1) sel = count - 1;
else sel = scroll = 0;
if (list[sel].type != RES_NONE) break;
sel_change = (sel_change == -1 ? -1 : 1);
}
if (hide_sel && res != RES_CANCEL && res != RES_CLOSESCREENKEYBOARD && res != RES_CHANGEMOUNTS && res != RES_REFRESHSYSTEM) return;
if (sel_change || x_change || res) DoInput(res, (res == RES_OK ? list[sel].type : IT_NONE), x_change);
}
void ResetSel(int setsel, bool do_refresh_mousesel = false)
{
sel = setsel;
scroll = -1;
hide_sel = false;
refresh_mousesel = do_refresh_mousesel;
}
virtual void DoInput(Result res, Bit8u ok_type, int x_change) = 0;
int DrawMenuBase(DBP_BufferDrawing& buf, Bit32u blend, int lh, int rows, const DBP_MenuMouse& m, bool mouseMoved, int menul, int menur, int menuu, bool centerv = false)
{
int count = (int)list.size(), xtra = (lh == 8 ? 0 : 1), scrx = menur - 11, menuh = rows * lh + xtra;
bool scrollbar = (count > rows);
int listu = menuu + ((centerv && rows > count) ? ((rows - count) * lh) / 3 : 0);
if (!show_popup)
{
if (scrollbar && m.left_pressed && (m.x >= scrx || click_sel == -2) && m.y >= menuu && m.y < menuu+menuh && scroll != -1)
{
int scrollh = menuh * rows / count / 2;
scroll_jump = (((count - rows) * ((int)m.y - menuu - scrollh) / (menuh-scrollh-scrollh)) - scroll);
click_sel = -2;
}
if (scroll == -1 && m.realmouse) mouseMoved = refresh_mousesel; // refresh when switching tab
// Update Scroll
if (count <= rows) scroll = 0;
else if (scroll == -1)
{
scroll = sel - rows/2;
scroll = (scroll < 0 ? 0 : scroll > count - rows ? count - rows : scroll);
}
else
{
if (m.realmouse && m.y >= menuu && m.y < menuu+menuh)
{
if (m.wheel_up) { scroll_unlocked = true; scroll_jump -= 4; }
if (m.wheel_down) { scroll_unlocked = true; scroll_jump += 4; }
}
if (scroll_jump)
{
int old_scroll = scroll;
scroll += scroll_jump;
scroll_jump = 0;
if (scroll < 0) scroll = 0;
if (scroll > count - rows) scroll = count - rows;
sel += (scroll - old_scroll);
}
if (!scroll_unlocked)
{
if (sel < scroll+ 4) scroll = (sel < 4 ? 0 : sel - 4);
if (sel > scroll+rows-5) scroll = (sel > count - 5 ? count - rows : sel - rows + 5);
}
}
if (mouseMoved)
{
int my = (int)(m.y+0.499f), mx = (int)(m.x+0.499f);
sel = scroll + (((int)my - listu) / lh);
if (my < listu) { sel = scroll; hide_sel = true; }
else if (sel >= count) { sel = count - 1; hide_sel = true; }
else if (mx >= scrx && scrollbar) { hide_sel = true; }
else if (my >= listu+rows*lh) { sel = scroll+rows-1; hide_sel = true; }
else { hide_sel = false; }
scroll_unlocked = true;
}
}
buf.DrawBox(menul, menuu - 3, menur - menul, menuh + 6, buf.BGCOL_MENU | blend, buf.COL_LINEBOX);
if (list[sel].type != IT_NONE && !hide_sel)
buf.AlphaBlendFillRect(menul+3, listu + (sel - scroll)*lh, menur - menul - 6 - (scrollbar ? 10 : 0), lh + xtra, buf.BGCOL_SELECTION | blend);
if (scrollbar)
{
int scrollu = menuh * scroll / count, scrolld = menuh * (scroll+rows) / count;
buf.AlphaBlendFillRect(scrx, menuu, 8, menuh, buf.BGCOL_SCROLL | blend);
buf.AlphaBlendFillRect(scrx, menuu + scrollu, 8, scrolld - scrollu, buf.BGCOL_SELECTION | blend);
}
return listu;
}
void UpdateHeld()
{
if (held_event == _DBPET_MAX) return;
Bit32u t = DBP_GetTicks();
if ((Bit32s)(t - held_ticks) < 60) return;
held_ticks = (t - held_ticks > 120 ? t : held_ticks + 60);
Input(DBPET_KEYDOWN, (int)held_key, 1);
}
};
static const Bit8u DBP_MapperJoypadNums[] = { RETRO_DEVICE_ID_JOYPAD_UP, RETRO_DEVICE_ID_JOYPAD_DOWN, RETRO_DEVICE_ID_JOYPAD_LEFT, RETRO_DEVICE_ID_JOYPAD_RIGHT, RETRO_DEVICE_ID_JOYPAD_B, RETRO_DEVICE_ID_JOYPAD_A, RETRO_DEVICE_ID_JOYPAD_Y, RETRO_DEVICE_ID_JOYPAD_X, RETRO_DEVICE_ID_JOYPAD_SELECT, RETRO_DEVICE_ID_JOYPAD_START, RETRO_DEVICE_ID_JOYPAD_L, RETRO_DEVICE_ID_JOYPAD_R, RETRO_DEVICE_ID_JOYPAD_L2, RETRO_DEVICE_ID_JOYPAD_R2, RETRO_DEVICE_ID_JOYPAD_L3, RETRO_DEVICE_ID_JOYPAD_R3 };
static const char* DBP_MapperJoypadNames[] = { "Up", "Down", "Left", "Right", "B (Down)", "A (Right)", "Y (Left)", "X (Up)", "SELECT", "START", "L", "R", "L2", "R2", "L3", "R3" };
struct DBP_MapperMenuState : DBP_MenuState
{
enum ItemType : Bit8u { IT_CANCEL = _IT_CUSTOM, IT_PRESET, IT_SELECT, IT_EDIT, IT_ADD, IT_DEL, IT_DEVICE, IT_DIVIDER };
enum EditMode : Bit8u { NOT_EDITING, EDIT_EXISTING, EDIT_NEW, EDIT_ADDITIONAL };
int main_sel = 0;
Bit16s edit_info;
EditMode edit_mode;
Bit8u bind_port = 0, bind_dev, changed = 0;
DBP_MapperMenuState() { menu_top(); }
~DBP_MapperMenuState() { if (changed) DBP_PadMapping::Save(); }
enum { JOYPAD_MAX = (sizeof(DBP_MapperJoypadNums)/sizeof(DBP_MapperJoypadNums[0])) };
DBP_InputBind BindFromPadNum(Bit8u i)
{
if (i < JOYPAD_MAX) return { bind_port, RETRO_DEVICE_JOYPAD, 0, DBP_MapperJoypadNums[i], _DBPET_MAX };
else { Bit8u n = i-JOYPAD_MAX; return { bind_port, RETRO_DEVICE_ANALOG, (Bit8u)(n/4), (Bit8u)(1-(n/2)%2), _DBPET_MAX }; }
}
void Add(const DBP_InputBind& b, Bit16s item_info, Bit8u apart = 0, bool* boundActionWheel = NULL)
{
int key = -1;
if (b.evt == DBPET_KEYDOWN)
key = b.meta;
else if (b.evt == DBPET_AXISMAPPAIR)
key = DBP_MAPPAIR_GET(apart?1:-1, b.meta);
else for (const DBP_SpecialMapping& sm : DBP_SpecialMappings)
if (sm.evt == b.evt && sm.meta == (b.device == RETRO_DEVICE_ANALOG ? (apart ? 1 : -1) : b.meta))
{ key = DBP_SPECIALMAPPINGS_KEY + (int)(&sm - DBP_SpecialMappings); break; }
if (key < 0) { DBP_ASSERT(false); return; }
const char *desc_dev = DBP_GETKEYDEVNAME(key);
list.emplace_back(IT_EDIT, item_info, " [Edit]");
(((list.back().str += (desc_dev ? " " : "")) += (desc_dev ? desc_dev : "")) += ' ') += DBP_GETKEYNAME(key);
if (boundActionWheel && key == DBP_SPECIALMAPPINGS_ACTIONWHEEL) *boundActionWheel = true;
}
void menu_top(int x_change = 0)
{
if (x_change)
{
int maxport = 1;
while (maxport != DBP_MAX_PORTS && dbp_port_mode[maxport]) maxport++;
for (DBP_InputBind& b : dbp_input_binds) if (b.evt == DBPET_SHIFTPORT && b.meta >= maxport) maxport = b.meta + 1;
bind_port = (bind_port + maxport + x_change) % maxport;
main_sel = 0;
}
list.clear();
if (DBP_PadMapping::CalcPortMode(bind_port) != DBP_PadMapping::MODE_MAPPER)
{
list.emplace_back(IT_NONE);
list.emplace_back(IT_NONE, 11, " Gamepad Mapper is disabled");
list.emplace_back(IT_NONE, 11, " for this controller port");
list.emplace_back(IT_NONE);
list.emplace_back(IT_NONE, 11, " Set 'Use Gamepad Mapper'");
list.emplace_back(IT_NONE, 11, " in the Controls menu");
}
else
{
list.emplace_back(IT_NONE, 0, "Preset: ");
list.emplace_back(IT_PRESET, 0, " "); list.back().str += DBP_PadMapping::GetPortPresetName(bind_port);
list.emplace_back(IT_NONE, 2);
bool boundActionWheel = false, haveWheelOptions = false;
for (Bit8u i = 0; i != JOYPAD_MAX + 8; i++)
{
bool a = (i>=JOYPAD_MAX); Bit8u apart = (a ? (i-JOYPAD_MAX)%2 : 0);
const DBP_InputBind pad = BindFromPadNum(i);
const Bit32u padpdii = PORT_DEVICE_INDEX_ID(pad);
list.emplace_back(IT_NONE);
if (!a) list.back().str = DBP_MapperJoypadNames[i];
else ((list.back().str = DBP_MapperJoypadNames[2+pad.index]) += " Analog ") += DBP_MapperJoypadNames[(i-JOYPAD_MAX)%4];
size_t numBefore = list.size();
for (const DBP_InputBind& b : dbp_input_binds)
if (PORT_DEVICE_INDEX_ID(b) == padpdii)
Add(b, (Bit16s)(((&b - &dbp_input_binds[0])<<1)|apart), apart, &boundActionWheel);
if (list.size() - numBefore == 0) list.emplace_back(IT_ADD, i, " [Create Binding]");
if (const char* action = DBP_PadMapping::GetBoundAutoMapButtonLabel(padpdii, a))
{
list.emplace_back(IT_NONE, 1, " Function: ");
list.back().str.append(action);
}
}
list.emplace_back(IT_NONE, 2);
list.emplace_back(IT_NONE, 0, "Wheel Options: ");
for (const DBP_WheelItem& it : dbp_wheelitems)
{
if (it.port != bind_port) continue;
for (int i = 0; i != it.key_count; i++)
{
DBP_InputBind bnd = DBP_PadMapping::BindForWheel(bind_port, it.k[i]);
if (bnd.device == RETRO_DEVICE_NONE) continue;
Add(bnd, (Bit16s)(&it - &dbp_wheelitems[0] + 1) * -4 - ((4 - i) & 3)); // encode so (info & 3) gives i
}
if (const char* action = DBP_PadMapping::GetWheelAutoMapButtonLabel(it))
{
list.emplace_back(IT_NONE, 1, " Function: ");
list.back().str.append(action);
}
list.emplace_back(IT_NONE, 0);
haveWheelOptions = true;
}
list.emplace_back(IT_ADD, -1, " Add Option");
if (haveWheelOptions != boundActionWheel)
{
list.emplace_back(IT_NONE, 0, "Warning:");
list.emplace_back(IT_NONE, -1, " Wheel is inaccessible because no");
list.emplace_back(IT_NONE, -1, (haveWheelOptions ? " button was bound to Action Wheel" : " options have been defined here"));
}
list.emplace_back(IT_NONE, 0);
}
if (!DBP_FullscreenOSD)
{
list.emplace_back(IT_NONE);
list.emplace_back(IT_CANCEL, 0, " Close Mapper");
}
if (main_sel >= (int)list.size()) main_sel = (int)list.size()-1;
while (main_sel && list[main_sel].type == IT_NONE) main_sel--;
ResetSel((main_sel < 1 ? 1 : main_sel), (main_sel < 1)); // skip top IT_NONE entry
edit_mode = NOT_EDITING;
bind_dev = 0;
}
INLINE bool is_mapper_disabled_top() { return list[1].info == 11; } // see menu_top
INLINE bool is_presets_menu() { return list[0].info == 22; } // see menu_presets
void menu_presets(Bit16s info)
{
main_sel = 0;
if (info)
{
if (info == 9999)
DBP_PadMapping::FillGenericKeys(bind_port);
else
DBP_PadMapping::SetPreset(bind_port, (DBP_PadMapping::EPreset)info);
changed = true;
menu_top();
return;
}
bool have_add = false;
for (Item& it : list) { if (it.type == IT_ADD) { have_add = true; break; } }
list.clear();
list.emplace_back(IT_NONE, 22, "Select Preset");
list.emplace_back(IT_NONE);
Bit16s off = (dbp_auto_mapping ? 0 : 1), n = 1 + off;
for (const char* p; (p = DBP_PadMapping::GetPresetName((DBP_PadMapping::EPreset)n)) != NULL;) list.emplace_back(IT_PRESET, n++, p);
if (have_add)
{
list.emplace_back(IT_NONE);
list.emplace_back(IT_PRESET, 9999, "Fill Unbound with Generic Keys");
}
if (DBP_PadMapping::IsCustomized(bind_port))
{
list.emplace_back(IT_NONE);
list.emplace_back(IT_DEL, 0, "[Reset Mapping]");
}
ResetSel(2 + DBP_PadMapping::GetPreset(bind_port) - 1 - off);
}
void menu_devices(Bit8u ok_type)
{
if (edit_mode == NOT_EDITING)
{
main_sel = sel;
edit_mode = (ok_type == IT_ADD ? EDIT_NEW : EDIT_EXISTING);
edit_info = list[sel].info;
int sel_header = sel - 1; while (list[sel_header].type != IT_NONE) sel_header--;
(list[0].str = list[sel_header].str);
list[1].str = std::string(" >") += list[sel].str;
list[0].type = list[1].type = IT_NONE;
}
else if (ok_type == IT_ADD)
{
edit_mode = EDIT_ADDITIONAL;
(list[1].str = " >") += " [Additional Binding]";
}
list.resize(2);
list.emplace_back(IT_NONE);
list.emplace_back(IT_DEVICE, 1, " "); list.back().str += DBPDEV_Keyboard;
list.emplace_back(IT_DEVICE, 2, " "); list.back().str += DBPDEV_Mouse;
list.emplace_back(IT_DEVICE, 3, " "); list.back().str += DBPDEV_Joystick;
for (const DBP_SpecialMapping& sm : DBP_SpecialMappings)
{
if (sm.dev || (sm.evt != DBPET_ONSCREENKEYBOARD && edit_info < 0)) continue; // no wheel no port shift as wheel options
if (sm.evt == DBPET_SHIFTPORT && sm.meta == bind_port) continue;
list.emplace_back(IT_SELECT, (Bit16s)(DBP_SPECIALMAPPINGS_KEY + (&sm - DBP_SpecialMappings)), " ");
list.back().str += sm.name;
}
if (edit_mode == EDIT_EXISTING)
{
list.emplace_back(IT_NONE);
list.emplace_back(IT_DEL, 0, " [Remove Binding]");
int count = 0;
if (edit_info >= 0) // editing bind
{
const DBP_InputBind& edit = dbp_input_binds[edit_info>>1];
for (const DBP_InputBind& b : dbp_input_binds)
if (PORT_DEVICE_INDEX_ID(b) == PORT_DEVICE_INDEX_ID(edit))
count++;
}
else // editing wheel
{
count = dbp_wheelitems[edit_info / -4 - 1].key_count;
}
if (count < 4)
{
list.emplace_back(IT_NONE);
list.emplace_back(IT_ADD, 0, " [Additional Binding]");
}
}
list.emplace_back(IT_NONE);
list.emplace_back(IT_CANCEL, 0, "Cancel");
char device = *(list[1].str.c_str() + (sizeof(" > [Edit] ")-1));
ResetSel(device == 'J' ? 5 : (device == 'M' ? 4 : 3));
bind_dev = 0;
}
void menu_keys()
{
bind_dev = (Bit8u)list[sel].info;
(list[2].str = " > ") += list[sel].str;
list.resize(3);
list.emplace_back(IT_NONE);
if (bind_dev == 1) for (Bit8u i = KBD_NONE + 1; i != KBD_LAST; i++)
{
static Bit8u sortedKeys[KBD_f1] = { KBD_NONE,
KBD_a, KBD_b, KBD_c, KBD_d, KBD_e, KBD_f, KBD_g, KBD_h, KBD_i, KBD_j, KBD_k,
KBD_l, KBD_m, KBD_n, KBD_o, KBD_p, KBD_q, KBD_r, KBD_s, KBD_t, KBD_u, KBD_v,
KBD_w, KBD_x, KBD_y, KBD_z,
KBD_1, KBD_2, KBD_3, KBD_4, KBD_5, KBD_6, KBD_7, KBD_8, KBD_9, KBD_0,
};
Bit8u key = (i < KBD_f1 ? sortedKeys[i] : i);
list.emplace_back(IT_SELECT, key, " ");
list.back().str += DBP_KBDNAMES[key];
if (const char* mapname = DBP_PadMapping::GetKeyAutoMapButtonLabel(key))
{
list.emplace_back(IT_NONE, 0, " Function: ");
list.back().str += mapname;
list.emplace_back(IT_NONE, 0);
}
}
else for (const DBP_SpecialMapping& sm : DBP_SpecialMappings)
{
if (sm.dev != (bind_dev == 2 ? DBPDEV_Mouse : DBPDEV_Joystick)) continue;
list.emplace_back(IT_SELECT, (Bit16s)(DBP_SPECIALMAPPINGS_KEY + (&sm - DBP_SpecialMappings)), " ");
list.back().str += sm.name;
}
list.emplace_back(IT_NONE);
list.emplace_back(IT_CANCEL, 0, "Cancel");
ResetSel(4, true); // skip top IT_NONE entry
if (!strncmp(list[1].str.c_str() + (sizeof(" > [Edit] ")-1), list[2].str.c_str() + (sizeof(" > ")-1), list[2].str.size() - (sizeof(" > ")-1)))
{
const char* keyname = list[1].str.c_str() + (sizeof(" > [Edit] ")-1) + list[2].str.size() - (sizeof(" > ")-1) + 1;
for (const Item& it : list)
if (it.str.size() > 2 && !strcmp(it.str.c_str() + 2, keyname))
{ ResetSel((int)(&it - &list[0])); break; }
}
}
virtual void DoInput(Result res, Bit8u ok_type, int x_change)
{
if (res == RES_CANCEL) ok_type = IT_CANCEL;
if (x_change && edit_mode == NOT_EDITING)
{
menu_top(x_change);
}
if ((ok_type == IT_SELECT || ok_type == IT_DEL) && edit_mode != NOT_EDITING)
{
if (edit_info >= 0) // editing bind
{
bool isNew = (ok_type == IT_SELECT && (edit_mode == EDIT_NEW || edit_mode == EDIT_ADDITIONAL)); // add new entry
bool isEdit = (ok_type == IT_SELECT && edit_mode == EDIT_EXISTING); // add new entry
bool isDelete = (ok_type == IT_DEL); // deleting entry
DBP_InputBind copy = (edit_mode == EDIT_NEW ? BindFromPadNum((Bit8u)edit_info) : dbp_input_binds[edit_info>>1]);
DBP_PadMapping::EditBind((isNew ? copy : dbp_input_binds[edit_info>>1]), isNew, isEdit, isDelete, (Bit8u)(edit_info&1), (Bit8u)list[sel].info);
}
else // editing wheel
{
DBP_WheelItem* edit = (edit_mode == EDIT_NEW ? NULL : &dbp_wheelitems[edit_info / -4 - 1]);
if (ok_type == IT_SELECT && edit_mode == EDIT_NEW) // add new entry
{
dbp_wheelitems.push_back({ bind_port, 1, { (Bit8u)list[sel].info } });
}
else if (ok_type == IT_SELECT && edit_mode == EDIT_ADDITIONAL)
{
edit->k[edit->key_count++] = (Bit8u)list[sel].info;
}
else if (ok_type == IT_SELECT && edit_mode == EDIT_EXISTING) // edit entry
{
edit->k[edit_info & 3] = (Bit8u)list[sel].info;
}
else if (ok_type == IT_DEL) // deleting entry
{
if (!--edit->key_count) dbp_wheelitems.erase(dbp_wheelitems.begin() + (edit_info / -4 - 1));
else memmove(edit->k + (edit_info & 3), edit->k + (edit_info & 3) + 1, 3 - (edit_info & 3));
}
}
changed = true;
menu_top();
}
else if (ok_type == IT_EDIT || ok_type == IT_ADD)
{
menu_devices(ok_type);
}
else if (ok_type == IT_DEVICE)
{
menu_keys();
}
else if (ok_type == IT_CANCEL && bind_dev)
{
menu_devices(ok_type);
}
else if (ok_type == IT_CANCEL && edit_mode == EDIT_ADDITIONAL)
{
menu_top();
menu_devices(IT_EDIT);
}
else if (ok_type == IT_CANCEL && (edit_mode != NOT_EDITING || is_presets_menu()))
{
menu_top();
}
else if (ok_type == IT_DEL)
{
menu_presets((Bit16s)(DBP_PadMapping::DefaultPreset(bind_port)));
}
else if (ok_type == IT_PRESET)
{
menu_presets(list[sel].info);
}
else if ((ok_type == IT_CANCEL || res == RES_CLOSESCREENKEYBOARD) && !DBP_FullscreenOSD)
{
DBP_CloseOSD();
}
}
void DrawMenu(DBP_BufferDrawing& buf, Bit32u blend, int lh, int w, int h, int ftr, bool mouseMoved, const DBP_MenuMouse& m)
{
UpdateHeld();
if ((DBP_PadMapping::CalcPortMode(bind_port) == DBP_PadMapping::MODE_MAPPER) == is_mapper_disabled_top())
menu_top();
int hdr = lh*3, rows = (h - hdr - ftr) / lh-1, count = (int)list.size(), l = w/2 - 150, r = w/2 + 150, xtra = (lh == 8 ? 0 : 1), wide = edit_mode == NOT_EDITING && !is_presets_menu() && w > 500;
if (l < 0) { l = 0, r = w; }
buf.DrawBox(l, hdr-7-lh*2, r-l, lh+3, buf.BGCOL_HEADER | blend, buf.COL_LINEBOX);
buf.PrintCenteredOutlined(lh, 0, w, hdr-lh*2-5, "Gamepad Mapper", buf.COL_MENUTITLE);
char num[32];
sprintf(num, "Controller Port %d", bind_port + 1);
buf.DrawBox(l-(wide?50:0), hdr-5-lh, r-l+(wide?100:0), lh+3, buf.BGCOL_HEADER | blend, buf.COL_LINEBOX);
buf.PrintCenteredOutlined(lh, 0, w, hdr-lh-3, num, buf.COL_CONTENT);
if (wide)
{
buf.DrawBox(l-100, hdr - 3, 201, rows * lh + 6 + xtra, buf.BGCOL_MENU | blend, buf.COL_LINEBOX);
DrawMenuBase(buf, blend, lh, rows, m, mouseMoved, l + 100, r + 100, hdr);
for (int ihdr = -1, i = scroll, inxt, se = (hide_sel ? -1 : sel), maxw = r-l-11; i != count && i != (scroll + rows); i++)
{
Bit8u itype = list[i].type;
if (itype == IT_NONE && !list[i].info) { ihdr = -1; continue; }
int y = hdr + (i - scroll)*lh;
if (ihdr == -1)
{
for (ihdr = i - 1; list[ihdr].type != IT_NONE; ihdr--) {}
for (inxt = i + 1; inxt < (int)list.size() && list[inxt].type != IT_NONE; inxt++) {}
if (list[sel].type != IT_NONE && !hide_sel && sel > ihdr && sel < inxt)
buf.AlphaBlendFillRect(l-97, y, 195, lh+xtra, buf.BGCOL_SELECTION | blend);
buf.Print(lh, l-84, y, list[ihdr].str.c_str(), buf.COL_HEADER);
ihdr = ihdr;
}
buf.Print(lh, l+100, y, list[i].str.c_str(), ((i == se || itype == IT_NONE) ? buf.COL_HIGHLIGHT : itype == IT_ADD ? buf.COL_DIM : buf.COL_NORMAL), maxw);
if (itype == IT_NONE && list[i].info == 2) // draw separator line
{
Bit32u *v = buf.video + buf.width * (y + lh) + l - 100;
for (Bit32u *p = v, *pEnd = p + r+189-l; p != pEnd; p++) *p = buf.COL_LINEBOX;
}
}
}
else
{
DrawMenuBase(buf, blend, lh, rows, m, mouseMoved, l, r, hdr);
for (int i = scroll, se = (hide_sel ? -1 : sel), maxw = r-l-27; i != count && i != (scroll + rows); i++)
{
Bit8u itype = list[i].type;
buf.Print(lh, l+16, hdr + (i - scroll)*lh, list[i].str.c_str(), (itype != IT_NONE ? (itype == IT_DEL ? buf.COL_WARN : i == se ? buf.COL_HIGHLIGHT : itype == IT_ADD ? buf.COL_DIM : buf.COL_NORMAL) : buf.COL_HEADER), maxw);
if (itype == IT_NONE && list[i].info == 2)
{
Bit32u *v = buf.video + buf.width * ((hdr + (i - scroll)*lh) + lh/2) + l;
for (Bit32u *p = v, *pEnd = p + r-12-l; p != pEnd; p++) *p = buf.COL_LINEBOX;
}
}
}
if (edit_mode == NOT_EDITING && !is_presets_menu())
{
int x_change = 0, x1 = l-(wide?50:0), x2 = r-25+(wide?50:0);
if (buf.DrawButtonAt(blend, hdr-lh-6, lh, 3, 2, x1, x1+25, false, m, "\x1B") && m.left_up) x_change = -1;
if (buf.DrawButtonAt(blend, hdr-lh-6, lh, 3, 2, x2, x2+25, false, m, "\x1A") && m.left_up) x_change = 1;
if (x_change) menu_top(x_change);
if (m.y >= 0 && m.y <= hdr)
{
if (m.wheel_up) DoInput(RES_NONE, IT_NONE, 1);
if (m.wheel_down) DoInput(RES_NONE, IT_NONE, -1);
}
}
}
};
struct DBP_OnScreenKeyboardState
{
enum { KWR = 10, KWTAB = 15, KWCAPS = 20, KWLS = 17, KWRSHIFT = 33, KWCTRL = 16, KWZERO = 22, KWBS = 28, KWSPACEBAR = 88, KWENTR = 18, KWPLUS, KWMAPPER = KWR*4+2*3 };
enum { KXX = 100+KWR+2, SPACEFF = 109, KSPLIT = 255, KSPLIT1 = 192, KSPLIT2 = 234, KWIDTH = KSPLIT2 + KWR*4 + 2*3 };
Bit32u pressed_time;
KBD_KEYS hovered_key, pressed_key;
bool held[KBD_LAST+1];
void GFX(DBP_BufferDrawing& buf, const DBP_MenuMouse& mo)
{
static const Bit8u keyboard_rows[6][25] =
{
{ KWR, KXX ,KWR,KWR,KWR,KWR, SPACEFF, KWR,KWR,KWR,KWR, SPACEFF, KWR,KWR,KWR,KWR , KSPLIT , KWR,KWR,KWR , KSPLIT , KWMAPPER },
{ KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWBS , KSPLIT , KWR,KWR,KWR , KSPLIT , KWR,KWR,KWR,KWR },
{ KWTAB, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWENTR , KSPLIT , KWR,KWR,KWR , KSPLIT , KWR,KWR,KWR,KWPLUS },
{ KWCAPS, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KSPLIT , KSPLIT , KWR,KWR,KWR },
{ KWLS, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWR, KWRSHIFT , KSPLIT , KXX,KWR,KXX , KSPLIT , KWR,KWR,KWR,KWPLUS },
{ KWCTRL, KXX, KWCTRL, KWSPACEBAR, KWCTRL, KXX, KWCTRL , KSPLIT , KWR,KWR,KWR , KSPLIT , KWZERO ,KWR },
};
static const KBD_KEYS keyboard_keys[6][25] =
{
{ KBD_esc,KBD_NONE,KBD_f1,KBD_f2,KBD_f3,KBD_f4,KBD_NONE,KBD_f5,KBD_f6,KBD_f7,KBD_f8,KBD_NONE,KBD_f9,KBD_f10,KBD_f11,KBD_f12,KBD_NONE,KBD_printscreen,KBD_scrolllock,KBD_pause,KBD_NONE,KBD_LAST },
{ KBD_grave, KBD_1, KBD_2, KBD_3, KBD_4, KBD_5, KBD_6, KBD_7, KBD_8, KBD_9, KBD_0, KBD_minus, KBD_equals, KBD_backspace ,KBD_NONE,KBD_insert,KBD_home,KBD_pageup ,KBD_NONE,KBD_numlock,KBD_kpdivide,KBD_kpmultiply,KBD_kpminus },
{ KBD_tab,KBD_q,KBD_w,KBD_e,KBD_r,KBD_t,KBD_y,KBD_u,KBD_i,KBD_o,KBD_p,KBD_leftbracket,KBD_rightbracket, KBD_enter ,KBD_NONE,KBD_delete,KBD_end,KBD_pagedown,KBD_NONE,KBD_kp7,KBD_kp8,KBD_kp9,KBD_kpplus },
{ KBD_capslock,KBD_a,KBD_s,KBD_d,KBD_f,KBD_g,KBD_h,KBD_j,KBD_k,KBD_l,KBD_semicolon,KBD_quote,KBD_backslash ,KBD_NONE , KBD_NONE,KBD_kp4,KBD_kp5,KBD_kp6 },
{ KBD_leftshift,KBD_extra_lt_gt,KBD_z,KBD_x,KBD_c,KBD_v,KBD_b,KBD_n,KBD_m,KBD_comma,KBD_period,KBD_slash,KBD_rightshift ,KBD_NONE, KBD_NONE,KBD_up,KBD_NONE ,KBD_NONE,KBD_kp1,KBD_kp2,KBD_kp3,KBD_kpenter },
{ KBD_leftctrl,KBD_NONE,KBD_leftalt, KBD_space, KBD_rightalt,KBD_NONE,KBD_rightctrl ,KBD_NONE, KBD_left,KBD_down,KBD_right ,KBD_NONE,KBD_kp0,KBD_kpperiod },
};
DBP_STATIC_ASSERT((int)KWIDTH == (int)buf.MWIDTH);
int thickness = buf.GetThickness();
float fx = (buf.width < KWIDTH ? (buf.width - 10) / (float)KWIDTH : (float)thickness);
float fy = fx * buf.ratio * buf.height / buf.width; if (fy < 1) fy = 1;
int thicknessy = (int)(fy + .95f);
int oskx = (int)(buf.width / fx / 2) - (KWIDTH / 2);
int osky = (mo.y && mo.y < (buf.height / 2) ? 3 : (int)(buf.height / fy) - 3 - 85);
if (pressed_key && (DBP_GetTicks() - pressed_time) > 500 && pressed_key != KBD_LAST)
{
held[pressed_key] = true;
pressed_key = KBD_NONE;
}
// Draw keys and check hovered state
int cX = (int)mo.x, cY = (int)mo.y;
hovered_key = KBD_NONE;
for (int row = 0; row != 6; row++)
{
int x = 0, y = (row ? 3 + (row * 10) : 0);
for (const Bit8u *k = keyboard_rows[row], *k_end = k + 25; k != k_end; k++)
{
int draww = *k, drawh = 8;
switch (*k)
{
case KWENTR:
x += 5;
drawh = 18;
break;
case KWPLUS:
draww = KWR;
drawh = 18;
break;
case KXX:case SPACEFF:
x += (*k - 100);
continue;
case KSPLIT:
x = (x < KSPLIT1 ? KSPLIT1 : KSPLIT2);
continue;
case 0: continue;
default: break;
}
DBP_ASSERT(draww);
int rl = (int)((oskx + x) * fx), rr = (int)((oskx + x + draww) * fx), rt = (int)((osky + y) * fy), rb = (int)((osky + y + drawh) * fy);
bool hovered = (cX >= rl-1 && cX <= rr && cY >= rt-1 && cY <= rb);
KBD_KEYS kbd_key = keyboard_keys[row][k - keyboard_rows[row]];
if (hovered) hovered_key = kbd_key;
buf.AlphaBlendFillRect(rl, rt, rr-rl, rb-rt, (pressed_key == kbd_key ? buf.BGCOL_KEYPRESS : (held[kbd_key] ? buf.BGCOL_KEYHELD : (hovered ? buf.BGCOL_KEYHOVER : buf.BGCOL_KEY))));
buf.AlphaBlendDrawRect(rl-1, rt-1, rr-rl+2, rb-rt+2, buf.BGCOL_KEYOUTLINE);
x += (draww + 2);
}
}
// Draw key letters
static Bit32u keyboard_letters[] = { 1577058307U,1886848880,3790471177U,216133148,985906176,3850940,117534959,1144626176,456060646,34095104,19009569,1078199360,2147632160U,1350912080,85984328,2148442146U,1429047626,77381,3692151160U,3087023553U,2218277763U,250609715,2332749995U,96707584,693109268,3114968401U,553648172,138445064,276889604,152060169,354484736,2148081986U,2072027207,2720394752U,85530487,285483008,8456208,555880480,1073816068,675456032,135266468,1074335764,580142244,112418,3793625220U,3288338464U,1078204481,2265448472U,1954875508,518111744,1955070434,633623176,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,117454849,1879539784,2150631296U,15367,3221282816U,537001987,1208036865,8392705,2102016,151060488,2147549200U,2156923136U,234881028,252228004,1092891456,2818085,2415940610U,8389633,235003932,3222274272U,9444864,1132462094,2818649873U,78141314,2098592,2147497991U,67110912,604110880,2359552,4610,170823736,2429878333U,2751502090U,10486784,2148532224U,67141632,268730432,1077937280,2,10536274,559026848,1075085330,8704,15729152,117473294,1610678368,7868160,968884224,1409292203,25432643,528016,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,58749752,469823556,1078200256,25169922,939638844,0,3087238168U,805797891,2449475456U,142641170,537165826,4720768,75515906,262152,3036676096U,9766672,2416509056U,30556160,62984209,2684616816U,4196608,16814089,128,772972544,268440225,1966272,44059592,301991978,537395328,18876417,268443678,0,1545880276,604045314,1224737280,88089609,268582913,2359552,4203521,3758227460U,1249902720,4752520,1074036752,15278080,31477771,537002056,2097920,58722307,298057840,2534092803U,16779024,983136,0,0,0,0,0,2575232,0,0,262144,0,0,0,0,268435456,1097,0,0,448,0,0,0,0,2300706816U,0,0,268435456,0,0,0,0,0,1451456,0,0,12582912,503341056,3223191664U,2178945537U,4100,131136,0,0,470007826,250848256,302006290,1074004032,5251074,134217730,64,0,37748736,2147500040U,37769856,2013413496,7865984,4195844,268435464,0,0,117471232,3725590584U,134248507,2415984712U,1082132736,2049,131072,0,0,151060488,67785216,151060489,538050592,4723201,8193,128,0,16777216,2147557408U,18932089,67166268,2149843328U,31459585,268435460,0,0,58728448,24,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,538182528,8916736,117475334,1114256,8388608,1515896,12582912,2148532224U,532690947,131665,18878721,369172497,864,553652224,528,15360,8389120,3626977288U,1074790432,35652609,1409499164,0,4005421057U,3221225472U,1073741839,14682112,134831401,2148532480U,75514880,557128,2097152,545952,6291456,2148007936U,2684362752U,268566826,9438464,151031813,537002256,2483028480U,266,3072,524544,1163361284,270401536,4197377,570499086,1073741888,3243438080U,2147483648U,536870913,7343872,8,0,0,0,0,0,0,0,16777216,0,0,0,0,0,0,0,0,7680,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1279262720,1275068480,2,0,0,9408,268451916,2097920,61440,185105920,4866048,0,0,2751463424U,138412036,1610809355,3072,536870930,70537,7496,0,0,30703616,18057216,4027319280U,37748739,553910272,788529186,1,0,0,4848,2113937953,8259552,18432,71573504,2433024,0,0,1375731712,1142947842,2013364228,1536,4026531849U,35609,6308,0,0,25837568,9115648,1074135072,31457280,2097280 };
for (Bit32u p = 0; p != 59*277; p++)
{
if (!(keyboard_letters[p>>5] & (1<<(p&31)))) continue;
int lx = (int)((oskx + 1 + (p%277)) * fx), ly = (int)((osky + 1 + (p/277)) * fy);
for (int y = ly; y != ly + thicknessy; y++)
for (int x = lx; x != lx + thickness; x++)
*(buf.video + y * buf.width + x) = buf.COL_KEYTEXT;
}
}
void Input(DBP_Event_Type type, int val, int val2)
{
switch (type)
{
case DBPET_MOUSEDOWN: