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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
|
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <game/math.h>
extern "C" {
#include <engine/system.h>
#include <engine/interface.h>
#include <engine/config.h>
#include <engine/client/ui.h>
}
#include "../mapres.h"
#include "../version.h"
#include "mapres_image.h"
#include "mapres_tilemap.h"
#include "data.h"
#include <mastersrv/mastersrv.h>
extern data_container *data;
/********************************************************
MENU
*********************************************************/
enum gui_tileset_enum
{
tileset_regular,
tileset_hot,
tileset_active,
tileset_inactive
};
void draw_area(gui_tileset_enum tileset, int areax, int areay, int areaw, int areah, float x, float y, float w, float h)
{
/*
const float tex_w = 512.0, tex_h = 512.0;
switch (tileset)
{
case tileset_regular:
break;
case tileset_hot:
areax += 192; areay += 192; break;
case tileset_active:
areay += 192; break;
case tileset_inactive:
areax += 192; break;
default:
dbg_msg("menu", "invalid tileset given to draw_part");
}
float ts_x = areax / tex_w;
float ts_y = areay / tex_h;
float te_x = (areax + areaw) / tex_w;
float te_y = (areay + areah) / tex_h;
gfx_blend_normal();
gfx_texture_set(data->images[IMAGE_GUI_WIDGETS].id);
gfx_quads_begin();
gfx_setcolor(1,1,1,1);
gfx_quads_setsubset(
ts_x, // startx
ts_y, // starty
te_x, // endx
te_y); // endy
gfx_quads_drawTL(x,y,w,h);
gfx_quads_end();
*/
}
void draw_part(int part_type, gui_tileset_enum tileset, float x, float y, float w, float h)
{
gui_box part = data->gui.misc[part_type];
draw_area(tileset, part.x, part.y, part.w, part.h, x, y, w, h);
//draw_part(parts[part], tileset, x, y, w, h);
}
void draw_part(int part_type, gui_tileset_enum tileset, float x, float y)
{
gui_box part = data->gui.misc[part_type];
draw_part(part_type, tileset, x, y, part.w, part.h);
}
void draw_box(int box_type, gui_tileset_enum tileset, float x, float y, float w, float h)
{
gui_compositebox box = data->gui.boxes[box_type];
/* A composite box consists of 9 parts. To get the coordinates for all corners, we need A, B, C and D:
* A----+----+----+
* | tl | tm | tr |
* +----B----+----+
* | ml | mm | mr |
* +----+----C----+
* | bl | bm | br |
* +----+----+----D
*/
int ax = box.rect.x;
int ay = box.rect.y;
int bx = box.center.x;
int by = box.center.y;
int cx = box.center.x + box.center.w;
int cy = box.center.y + box.center.h;
int dx = box.rect.x + box.rect.w;
int dy = box.rect.y + box.rect.h;
draw_area(tileset, ax, ay, bx-ax, by-ay, x, y, bx-ax, by-ay);
draw_area(tileset, bx, ay, cx-bx, by-ay, x+bx-ax, y, w-(bx-ax)-(dx-cx), by-ay);
draw_area(tileset, cx, ay, dx-cx, by-ay, x+w-(dx-cx), y, dx-cx, by-ay);
draw_area(tileset, ax, by, bx-ax, min(int(h-(by-ay)-(dy-cy)), cy-by), x, y+(by-ay), bx-ax, h-(by-ay)-(dy-cy));
draw_area(tileset, bx, by, cx-bx, cy-by, x+bx-ax, y+(by-ay), w-(bx-ax)-(dx-cx), h-(by-ay)-(dy-cy));
draw_area(tileset, cx, by, dx-cx, min(int(h-(by-ay)-(dy-cy)), cy-by), x+w-(dx-cx), y+(by-ay), dx-cx, h-(by-ay)-(dy-cy));
draw_area(tileset, ax, cy, bx-ax, dy-cy, x, y+h-(dy-cy), bx-ax, dy-cy);
draw_area(tileset, bx, cy, cx-bx, dy-cy, x+bx-ax, y+h-(dy-cy), w-(bx-ax)-(dx-cx), dy-cy);
draw_area(tileset, cx, cy, dx-cx, dy-cy, x+w-(dx-cx), y+h-(dy-cy), dx-cx, dy-cy);
}
extern "C"
{
struct pretty_font
{
float m_CharStartTable[256];
float m_CharEndTable[256];
int font_texture;
};
extern pretty_font *current_font;
}
extern void render_sun(float x, float y);
extern void select_sprite(int id, int flags=0, int sx=0, int sy=0);
extern void draw_sprite(float x, float y, float size);
void draw_background(float t)
{
// background color
gfx_clear(0.65f,0.78f,0.9f);
gfx_blend_normal();
render_sun(170, 170);
gfx_texture_set(data->images[IMAGE_CLOUDS].id);
gfx_quads_begin();
select_sprite(SPRITE_CLOUD1);
draw_sprite(3500 - fmod(t * 20 + 2000, 4524), 250, 512);
select_sprite(SPRITE_CLOUD2);
draw_sprite(3000 - fmod(t * 50 + 2000, 4024), 150+250, 512);
select_sprite(SPRITE_CLOUD3);
draw_sprite(4000 - fmod(t * 60 + 500, 4512), 300+130, 256);
gfx_quads_end();
/*
gfx_texture_set(data->images[IMAGE_MENU_BACKGROUND].id);
gfx_quads_begin();
gfx_quads_drawTL(0, 430, 1600, 1600/2);
gfx_quads_end();
*/
/*
int frame = int(t * 10) % 3;
//float x_path = -t
float x_nudge = 3*cos(t*10);
float y_nudge = 8*sin(t*3);
x_nudge += 150 * cos(t/3);
y_nudge += 30 * sin(t/3);
float angl = t/3;
angl = fmod(angl, 2*pi);
bool flip = angl > pi;
gfx_texture_set(data->images[IMAGE_MENU_BUTTERFLY].id);
gfx_quads_begin();
gfx_setcolor(1, 1, 1, 1);
gfx_quads_setsubset(
flip ? (frame + 1) * 0.25f : frame * 0.25f, // startx
0.0f, // starty
flip ? frame * 0.25f : (frame + 1) * 0.25f, // endx
0.5f); // endy
gfx_quads_drawTL(1250 + x_nudge, 480 + y_nudge, 64, 64);
gfx_quads_end();
*/
}
void draw_image_button(const void *id, const char *text, int checked, float x, float y, float w, float h, void *extra)
{
ui_do_image(*(int *)id, x, y, w, h);
}
void draw_single_part_button(const void *id, const char *text, int checked, float x, float y, float w, float h, void *extra)
{
gui_tileset_enum tileset;
if (ui_active_item() == id && ui_hot_item() == id)
tileset = tileset_active;
else if (ui_hot_item() == id)
tileset = tileset_hot;
else
tileset = tileset_regular;
draw_part((int)((char*)extra-(char*)0), tileset, x, y, w, h);
}
void draw_menu_button(const void *id, const char *text, int checked, float x, float y, float w, float h, void *extra)
{
int box_type;
if ((int)((char*)extra-(char*)0))
box_type = GUI_BOX_SCREEN_INFO;
else
box_type = GUI_BOX_SCREEN_LIST;
draw_box(box_type, tileset_regular, x, y, w, h);
ui_do_label(x + 10, y, text, 28);
}
void draw_teewars_button(const void *id, const char *text, int checked, float x, float y, float w, float h, void *extra)
{
const float font_size = h-6.0f;//42.0f;
float text_width = gfx_pretty_text_width(font_size, text, -1);
gui_tileset_enum tileset;
if (ui_active_item() == id)
{
int inside = ui_mouse_inside(x, y, w, h);
tileset = inside ? tileset_active : tileset_hot;
}
else if (ui_hot_item() == id)
tileset = tileset_hot;
else
tileset = tileset_regular;
if ((int)((char*)extra-(char*)0) == 1)
tileset = tileset_inactive;
draw_box(GUI_BOX_BUTTON, tileset, x, y, w, h);
ui_do_label(x + w/2 - text_width/2, y + 2, text, font_size);
}
/*
struct server_info
{
int version;
int players;
int max_players;
netaddr4 address;
char name[129];
char map[65];
};*/
struct server_list
{
int active_count, info_count;
int scroll_index;
int selected_index;
};
int ui_do_key_reader(void *id, float x, float y, float w, float h, int key)
{
// process
static bool mouse_released = true;
int inside = ui_mouse_inside(x, y, w, h);
int new_key = key;
if(!ui_mouse_button(0))
mouse_released = true;
if(ui_active_item() == id)
{
int k = inp_last_key();
if (k)
{
new_key = k;
ui_set_active_item(0);
mouse_released = false;
}
}
else if(ui_hot_item() == id)
{
if(ui_mouse_button(0) && mouse_released)
ui_set_active_item(id);
}
if(inside)
ui_set_hot_item(id);
// draw
int box_type;
if (ui_active_item() == id || ui_hot_item() == id)
box_type = GUI_BOX_SCREEN_INFO;
else
box_type = GUI_BOX_SCREEN_TEXTBOX;
draw_box(box_type, tileset_regular, x, y, w, h);
const char *str = inp_key_name(key);
ui_do_label(x + 10, y, str, 36);
if (ui_active_item() == id)
{
float w = gfx_pretty_text_width(36.0f, str, -1);
ui_do_label(x + 10 + w, y, "_", 36);
}
return new_key;
}
int ui_do_combo_box(void *id, float x, float y, float w, const char **lines, int line_count, int selected_index)
{
float line_height = 36.0f;
float height = line_count * line_height;
int inside = (ui_active_item() == id) ? ui_mouse_inside(x, y, w, height) : ui_mouse_inside(x, y, w, line_height);
int hover_index = (int)((ui_mouse_y() - y) / line_height);
if (ui_active_item() == id)
{
if (!ui_mouse_button(0))
{
ui_set_active_item(0);
if (inside)
selected_index = hover_index;
}
}
else if(ui_hot_item() == id)
{
if (ui_mouse_button(0))
{
ui_set_active_item(id);
}
}
if (inside)
{
ui_set_hot_item(id);
}
if (ui_active_item() == id)
{
for (int i = 0; i < line_count; i++)
{
int box_type;
if (inside && hover_index == i)
box_type = GUI_BOX_SCREEN_INFO;
else
box_type = GUI_BOX_SCREEN_LIST;
draw_box(box_type, tileset_regular, x, y + i * line_height, w, line_height);
ui_do_label(x + 10 + 10, y + i * line_height, lines[i], 36);
if (selected_index == i)
ui_do_label(x + 10, y + i * line_height, "-", 36);
}
}
else
{
int box_type;
if (ui_active_item() == id || ui_hot_item() == id)
box_type = GUI_BOX_SCREEN_INFO;
else
box_type = GUI_BOX_SCREEN_TEXTBOX;
draw_box(box_type, tileset_regular, x, y, w, line_height);
ui_do_label(x + 10, y, lines[selected_index], 36);
}
return selected_index;
}
int ui_do_edit_box(void *id, float x, float y, float w, float h, char *str, int str_size)
{
int inside = ui_mouse_inside(x, y, w, h);
int r = 0;
static int at_index = 0;
if(ui_last_active_item() == id)
{
int c = inp_last_char();
int k = inp_last_key();
int len = strlen(str);
if (inside && ui_mouse_button(0))
{
int mx_rel = (int)(ui_mouse_x() - x);
for (int i = 1; i <= len; i++)
{
if (gfx_pretty_text_width(36.0f, str, i) + 10 > mx_rel)
{
at_index = i - 1;
break;
}
if (i == len)
at_index = len;
}
}
if (at_index > len)
at_index = len;
if (!(c >= 0 && c < 32))
{
if (len < str_size - 1 && at_index < str_size - 1)
{
memmove(str + at_index + 1, str + at_index, len - at_index + 1);
str[at_index] = c;
at_index++;
}
}
if (k == KEY_BACKSPACE && at_index > 0)
{
memmove(str + at_index - 1, str + at_index, len - at_index + 1);
at_index--;
}
else if (k == KEY_DEL && at_index < len)
memmove(str + at_index, str + at_index + 1, len - at_index);
else if (k == KEY_ENTER)
ui_clear_last_active_item();
else if (k == KEY_LEFT && at_index > 0)
at_index--;
else if (k == KEY_RIGHT && at_index < len)
at_index++;
else if (k == KEY_HOME)
at_index = 0;
else if (k == KEY_END)
at_index = len;
r = 1;
}
int box_type;
if (ui_active_item() == id || ui_hot_item() == id || ui_last_active_item() == id)
box_type = GUI_BOX_SCREEN_INFO;
else
box_type = GUI_BOX_SCREEN_TEXTBOX;
bool just_got_active = false;
if(ui_active_item() == id)
{
if(!ui_mouse_button(0))
ui_set_active_item(0);
}
else if(ui_hot_item() == id)
{
if(ui_mouse_button(0))
{
if (ui_last_active_item() != id)
just_got_active = true;
ui_set_active_item(id);
}
}
if(inside)
ui_set_hot_item(id);
draw_box(box_type, tileset_regular, x, y, w, h);
ui_do_label(x + 10, y, str, 36);
if (ui_last_active_item() == id && !just_got_active)
{
float w = gfx_pretty_text_width(36.0f, str, at_index);
ui_do_label(x + 10 + w, y, "_", 36);
}
return r;
}
int ui_do_check_box(void *id, float x, float y, float w, float h, int value)
{
int inside = ui_mouse_inside(x, y, w, h);
int r = value;
if(ui_active_item() == id)
{
if(!ui_mouse_button(0))
{
ui_set_active_item(0);
r = r ? 0 : 1;
}
}
else if(ui_hot_item() == id)
{
if(ui_mouse_button(0))
ui_set_active_item(id);
}
if(inside)
ui_set_hot_item(id);
// render
gui_tileset_enum tileset;
int part_type;
if (ui_active_item() == id)
tileset = tileset_active;
else if (ui_hot_item() == id)
tileset = tileset_hot;
else
tileset = tileset_regular;
part_type = r ? GUI_MISC_RADIO_CHECKED : GUI_MISC_RADIO_UNCHECKED;
draw_part(part_type, tileset, x, y, w, h);
return r;
}
int do_scroll_bar_horiz(void *id, float x, float y, float width, int steps, int last_index)
{
int r = last_index;
static int up_button;
static int down_button;
if (ui_do_button(&up_button, "", 0, x, y + 8, 16, 16, draw_single_part_button, (void *)GUI_MISC_SLIDER_BIG_ARROW_LEFT))
{
if (r > 0)
--r;
}
if (ui_do_button(&down_button, "", 0, x + width - 16, y + 8, 16, 16, draw_single_part_button, (void *)GUI_MISC_SLIDER_BIG_ARROW_RIGHT))
{
if (r < steps)
++r;
}
if (steps > 0) // only if there's actually stuff to scroll through
{
int inside = ui_mouse_inside(x + 16, y, width - 32, 32);
if (inside && (!ui_active_item() || ui_active_item() == id))
ui_set_hot_item(id);
if(ui_active_item() == id)
{
if (ui_mouse_button(0))
{
float pos = ui_mouse_x() - x - 16;
float perc = pos / (width - 32);
r = (int)((steps + 1) * perc);
if (r < 0)
r = 0;
else if (r > steps)
r = steps;
}
else
ui_set_active_item(0);
}
else if (ui_hot_item() == id && ui_mouse_button(0))
ui_set_active_item(id);
else if (inside && (!ui_active_item() || ui_active_item() == id))
ui_set_hot_item(id);
}
draw_part(GUI_MISC_SLIDER_BIG_HORIZ_BEGIN, tileset_regular, x + 16, y + 8, 16, 16);
draw_part(GUI_MISC_SLIDER_BIG_HORIZ_MID, tileset_regular, x + 32, y + 8, width - 32 - 32, 16);
draw_part(GUI_MISC_SLIDER_BIG_HORIZ_END, tileset_regular, x + width - 32, y + 8, 16, 16);
draw_part(GUI_MISC_SLIDER_BIG_HANDLE_HORIZ, tileset_regular, x + 16 + r * ((width - 64) / steps), y + 8, 32, 16);
return r;
}
int do_scroll_bar_vert(void *id, float x, float y, float height, int steps, int last_index)
{
int r = last_index;
static int up_button;
static int down_button;
if (ui_do_button(&up_button, "", 0, x + 8, y, 16, 16, draw_single_part_button, (void *)GUI_MISC_SLIDER_BIG_ARROW_UP))
{
if (r > 0)
--r;
}
if (ui_do_button(&down_button, "", 0, x + 8, y + height - 16, 16, 16, draw_single_part_button, (void *)GUI_MISC_SLIDER_BIG_ARROW_DOWN))
{
if (r < steps)
++r;
}
if (steps > 0) // only if there's actually stuff to scroll through
{
int inside = ui_mouse_inside(x, y + 16, 16, height - 32);
if (inside && (!ui_active_item() || ui_active_item() == id))
ui_set_hot_item(id);
if(ui_active_item() == id)
{
if (ui_mouse_button(0))
{
float pos = ui_mouse_y() - y - 32;
float perc = pos / (height - 32);
r = (int)((steps + 1) * perc);
if (r < 0)
r = 0;
else if (r > steps)
r = steps;
}
else
ui_set_active_item(0);
}
else if (ui_hot_item() == id && ui_mouse_button(0))
ui_set_active_item(id);
else if (inside && (!ui_active_item() || ui_active_item() == id))
ui_set_hot_item(id);
}
draw_part(GUI_MISC_SLIDER_BIG_VERT_BEGIN, tileset_regular, x + 8, y + 16, 16, 16);
draw_part(GUI_MISC_SLIDER_BIG_VERT_MID, tileset_regular, x + 8, y + 32, 16, height - 32 - 32);
draw_part(GUI_MISC_SLIDER_BIG_VERT_END, tileset_regular, x + 8, y + height - 32, 16, 16);
draw_part(GUI_MISC_SLIDER_BIG_HANDLE_VERT, tileset_regular, x + 8, y + 16 + r * ((height - 64) / steps), 16, 32);
return r;
}
int ui_do_button_rect(const void *id, const char *text, int checked, const struct rect *r, draw_button_callback draw_func, void *extra)
{
return ui_do_button((void *)id, text, checked, r->x, r->y, r->w, r->h, draw_func, extra);
}
void ui_do_label_rect(const struct rect *r, char *str)
{
float size = r->h;
ui_do_label(r->x + 3, r->y + r->h/2 - size/2, str, size);
}
void ui_do_edit_box_rect(const void *id, const struct rect *r, char *buffer, int bufferSize)
{
ui_do_edit_box((void *)id, r->x, r->y, r->w, r->h, buffer, bufferSize);
}
int ui_do_check_box_rect(const void *id, const struct rect *r, int checked)
{
return ui_do_check_box((void *)id, r->x, r->y, 32, 32, checked);
}
int ui_do_key_reader_rect(const void *id, const struct rect *r, int key)
{
return ui_do_key_reader((void *)id, r->x, r->y, r->w, r->h, key);
}
int do_scroll_bar_horiz_rect(const void *id, const struct rect *r, int steps, int last_index)
{
return do_scroll_bar_horiz((void *)id, r->x, r->y, r->w, steps, last_index);
//return do_scroll_bar_horiz((void *)id, r->x, r->y, r->w, r->h, steps, last_index);
}
static int do_server_list(float x, float y, int *scroll_index, int *selected_index, int visible_items)
{
const float spacing = 3.f;
const float item_height = 28;
const float item_width = 728;
const float real_width = item_width + 20;
const float real_height = item_height * visible_items + spacing * (visible_items - 1);
int num_servers = client_serverbrowse_sorted_num();
int r = -1;
for (int i = 0; i < visible_items; i++)
{
int item_index = i + *scroll_index;
if (item_index >= num_servers)
;
//ui_do_image(empty_item_texture, x, y + i * item_height + i * spacing, item_width, item_height);
else
{
SERVER_INFO *item = client_serverbrowse_sorted_get(item_index);
bool clicked = false;
clicked = ui_do_button(item, item->name, 0, x, y + i * item_height + i * spacing, item_width, item_height,
draw_menu_button, (*selected_index == item_index) ? (void *)1 : 0);
char temp[64]; // plenty of extra room so we don't get sad :o
sprintf(temp, "%i/%i %3d", item->num_players, item->max_players, item->latency);
ui_do_label(x + 600, y + i * item_height + i * spacing, temp, item_height);
ui_do_label(x + 360, y + i * item_height + i * spacing, item->map, item_height);
if (clicked)
{
r = item_index;
*selected_index = item_index;
}
}
}
*scroll_index = do_scroll_bar_vert(scroll_index, x + real_width - 16, y, real_height,
max(num_servers - visible_items, 0), *scroll_index);
return r;
}
enum
{
SCREEN_SERVERS,
SCREEN_NEWS,
SCREEN_HOST,
SCREEN_DISCONNECTED,
SCREEN_CONNECTING,
SCREEN_SETTINGS_GENERAL,
SCREEN_SETTINGS_CONTROLS,
SCREEN_SETTINGS_VIDEO,
SCREEN_SETTINGS_VIDEO_SELECT_MODE,
SCREEN_SETTINGS_VIDEO_CUSTOM,
SCREEN_SETTINGS_SOUND,
SCREEN_KERNING
};
static int screen = SCREEN_SERVERS;
static CONFIGURATION config_copy;
const float column1_x = 250;
const float column2_x = column1_x + 170;
const float column3_x = column2_x + 170;
const float row1_y = 180;
const float row2_y = row1_y + 40;
const float row3_y = row2_y + 40;
const float row4_y = row3_y + 40;
const float row5_y = row4_y + 40;
const float row6_y = row5_y + 40;
const float row7_y = row6_y + 40;
static char address[128] = "localhost:8303";
static float colors[7][3] =
{
{ 0, 0, 1 },
{ 0, 1, 0 },
{ 0, 1, 1 },
{ 1, 0, 0 },
{ 1, 0, 1 },
{ 1, 1, 0 },
{ 1, 1, 1 },
};
static void draw_rect(const struct rect *r)
{
float *color = colors[rand()%7];
gfx_setcolor(color[0], color[1], color[2], 1);
gfx_lines_draw(r->x, r->y, r->x+r->w, r->y);
gfx_lines_draw(r->x+r->w, r->y, r->x+r->w, r->y+r->h);
gfx_lines_draw(r->x+r->w, r->y+r->h, r->x, r->y+r->h);
gfx_lines_draw(r->x, r->y+r->h, r->x, r->y);
}
void ui_settings_general_render(const struct rect *r)
{
static struct rect row1;
static struct rect row2;
static struct rect rest;
static struct rect cells[4];
ui_hsplit_t(r, 50, &row1, &rest);
ui_hsplit_t(&rest, 50, &row2, &rest);
ui_vsplit_l(&row1, 100, &cells[0], &cells[1]);
ui_vsplit_l(&row2, 100, &cells[2], &cells[3]);
// NAME
ui_do_label_rect(&cells[0], "Name:");
ui_do_edit_box_rect(config_copy.player_name, &cells[1], config_copy.player_name, sizeof(config_copy.player_name));
// Dynamic camera
ui_do_label_rect(&cells[2], "Dynamic Camera:");
config_set_dynamic_camera(&config_copy, ui_do_check_box_rect(&config_copy.dynamic_camera, &cells[3], config_copy.dynamic_camera));
}
typedef void (*assign_func_callback)(CONFIGURATION *config, int value);
struct key_thing
{
char name[32];
int *key;
assign_func_callback assign_func;
};
static void ui_settings_controls_render(const struct rect *r)
{
static int scroll_index = 0;
const key_thing keys[] =
{
{ "Move Left:", &config_copy.key_move_left, config_set_key_move_left },
{ "Move Right:", &config_copy.key_move_right, config_set_key_move_right },
{ "Jump:", &config_copy.key_jump, config_set_key_jump },
{ "Fire:", &config_copy.key_fire, config_set_key_fire },
{ "Hook:", &config_copy.key_hook, config_set_key_hook },
{ "Hammer:", &config_copy.key_weapon1, config_set_key_weapon1 },
{ "Pistol:", &config_copy.key_weapon2, config_set_key_weapon2 },
{ "Shotgun:", &config_copy.key_weapon3, config_set_key_weapon3 },
{ "Grenade:", &config_copy.key_weapon4, config_set_key_weapon4 },
{ "Next Weapon:", &config_copy.key_next_weapon, config_set_key_next_weapon },
{ "Prev. Weapon:", &config_copy.key_prev_weapon, config_set_key_prev_weapon },
{ "Emoticon:", &config_copy.key_emoticon, config_set_key_emoticon },
{ "Screenshot:", &config_copy.key_screenshot, config_set_key_screenshot },
};
const int key_count = sizeof(keys) / sizeof(key_thing);
struct rect rest = *r;
for (int i = 0; i < key_count; i++)
{
struct rect row;
struct rect left, right;
ui_hsplit_t(&rest, 32, &row, &rest);
ui_vsplit_l(&row, 128, &left, &right);
key_thing key = keys[i + scroll_index];
ui_do_label_rect(&left, key.name);
key.assign_func(&config_copy, ui_do_key_reader_rect(key.key, &right, *key.key));
}
}
void ui_settings_video_render(const struct rect *r)
{
}
void ui_settings_video_render_select_mode(const struct rect *r)
{
}
void ui_settings_video_render_custom(const struct rect *r)
{
}
void ui_settings_sound_render(const struct rect *r)
{
struct rect row;
struct rect left, right;
ui_hsplit_t(r, 32, &row, 0x0);
ui_vsplit_l(&row, 128, &left, &right);
ui_do_label_rect(&left, "Volume:");
config_set_volume(&config_copy, do_scroll_bar_horiz_rect(&config_copy.volume, &right, 256, config_copy.volume));
snd_set_master_volume(config_copy.volume / 255.0f);
}
static void tab_menu_button_render(const struct rect *r, char *name, int s)
{
if (ui_do_button_rect(r, name, 0, r, draw_teewars_button, 0))
screen = s;
}
static void tab_menu_render(const struct rect *r)
{
static struct rect button_news;
static struct rect button_servers;
static struct rect button_host;
static struct rect button_settings;
static struct rect rest;
ui_vsplit_l(r, 130, &button_news, &rest);
ui_vsplit_l(&rest, 130, &button_servers, &rest);
ui_vsplit_l(&rest, 130, &button_host, &rest);
ui_vsplit_l(&rest, 130, &button_settings, &rest);
tab_menu_button_render(&button_news, "News", SCREEN_NEWS);
tab_menu_button_render(&button_servers, "Servers", SCREEN_SERVERS);
tab_menu_button_render(&button_host, "Host", SCREEN_HOST);
tab_menu_button_render(&button_settings, "Settings", SCREEN_SETTINGS_GENERAL);
}
static void settings_tab_menu_render(const struct rect *r)
{
static struct rect button_general;
static struct rect button_controls;
static struct rect button_video;
static struct rect button_sound;
static struct rect rest;
ui_hsplit_t(r, 60, &button_general, &rest);
ui_hsplit_t(&rest, 60, &button_controls, &rest);
ui_hsplit_t(&rest, 60, &button_video, &rest);
ui_hsplit_t(&rest, 60, &button_sound, &rest);
tab_menu_button_render(&button_general, "General", SCREEN_SETTINGS_GENERAL);
tab_menu_button_render(&button_controls, "Controls", SCREEN_SETTINGS_CONTROLS);
tab_menu_button_render(&button_video, "Video", SCREEN_SETTINGS_VIDEO);
tab_menu_button_render(&button_sound, "Sound", SCREEN_SETTINGS_SOUND);
}
static void ui_settings_render(const struct rect *r)
{
static struct rect sub_menu_selector;
static struct rect center;
ui_vsplit_l(r, 100, &sub_menu_selector, ¢er);
settings_tab_menu_render(&sub_menu_selector);
switch (screen)
{
case SCREEN_SETTINGS_GENERAL: ui_settings_general_render(¢er); break;
case SCREEN_SETTINGS_CONTROLS: ui_settings_controls_render(¢er); break;
case SCREEN_SETTINGS_VIDEO: ui_settings_video_render(¢er); break;
case SCREEN_SETTINGS_VIDEO_SELECT_MODE: ui_settings_video_render_select_mode(¢er); break;
case SCREEN_SETTINGS_VIDEO_CUSTOM: ui_settings_video_render_custom(¢er); break;
case SCREEN_SETTINGS_SOUND: ui_settings_sound_render(¢er); break;
}
}
static void middle_render(const struct rect *r)
{
bool ingame = false;
switch (screen)
{
case SCREEN_SERVERS:
{
if (ingame)
{}
//return ingame_main_render();
else
{
static struct rect browser;
static bool inited = false;
ui_margin(r, 5, &browser);
{
struct rect button_row;
static struct rect button_refresh, button_connect;
ui_hsplit_b(&browser, 30, &browser, &button_row);
ui_vsplit_l(&button_row, 100, &button_refresh, &button_row);
ui_vsplit_l(&button_row, 100, &button_connect, &button_row);
if (ui_do_button_rect(&button_refresh, "Refresh", 0, &button_refresh, draw_teewars_button, 0))
client_serverbrowse_refresh(0);
if (ui_do_button_rect(&button_connect, "Connect", 0, &button_connect, draw_teewars_button, 0))
{}
}
if (!inited)
{
client_serverbrowse_refresh(0);
inited = true;
}
{
int server_count = client_serverbrowse_sorted_num();
int i;
struct rect rest = browser;
struct rect button_row;
static struct rect button_name, button_players, button_players_max, button_map, button_latency, button_progression;
ui_hsplit_t(&rest, 32, &button_row, &rest);
ui_margin(&button_row, 1, &button_row);
ui_vsplit_l(&button_row, 400, &button_name, &button_row);
ui_vsplit_l(&button_row, 40, &button_players, &button_row);
ui_vsplit_l(&button_row, 40, &button_players_max, &button_row);
ui_vsplit_l(&button_row, 80, &button_map, &button_row);
ui_vsplit_l(&button_row, 40, &button_latency, &button_row);
ui_vsplit_l(&button_row, 40, &button_progression, &button_row);
if (ui_do_button_rect(&button_name, "Name", 0, &button_name, draw_teewars_button, 0))
config.b_sort = BROWSESORT_NAME;
if (ui_do_button_rect(&button_players, "Players", 0, &button_players, draw_teewars_button, 0))
config.b_sort = BROWSESORT_NUMPLAYERS;
if (ui_do_button_rect(&button_players_max, "Max Players", 0, &button_players_max, draw_teewars_button, 0))
config.b_sort = BROWSESORT_NUMPLAYERS; // TODO: real enum here
if (ui_do_button_rect(&button_map, "Map", 0, &button_map, draw_teewars_button, 0))
config.b_sort = BROWSESORT_MAP;
if (ui_do_button_rect(&button_latency, "Ping", 0, &button_latency, draw_teewars_button, 0))
config.b_sort = BROWSESORT_PING;
if (ui_do_button_rect(&button_progression, "Progression", 0, &button_progression, draw_teewars_button, 0))
config.b_sort = BROWSESORT_PING; // TODO: real enum here
for (i = 0; i < server_count; i++)
{
SERVER_INFO *info = client_serverbrowse_sorted_get(i);
struct rect row;
struct rect col_name, col_players, col_players_max, col_map, col_latency, col_progression;
char temp[16];
ui_hsplit_t(&rest, 32, &row, &rest);
if (rest.h < 0)
break;
ui_margin(&row, 1, &row);
ui_vsplit_l(&row, 400, &col_name, &row);
ui_vsplit_l(&row, 40, &col_players, &row);
ui_vsplit_l(&row, 40, &col_players_max, &row);
ui_vsplit_l(&row, 80, &col_map, &row);
ui_vsplit_l(&row, 40, &col_latency, &row);
ui_vsplit_l(&row, 40, &col_progression, &row);
ui_do_label_rect(&col_name, info->name);
sprintf(temp, "%i", info->num_players);
ui_do_label_rect(&col_players, temp);
sprintf(temp, "%i", info->max_players);
ui_do_label_rect(&col_players_max, temp);
ui_do_label_rect(&col_map, info->map);
sprintf(temp, "%i", info->latency);
ui_do_label_rect(&col_latency, temp);
sprintf(temp, "%i", info->progression);
ui_do_label_rect(&col_progression, temp);
}
}
}
break;
}
case SCREEN_NEWS:
{
break;
}
case SCREEN_HOST:
{
break;
//return disconnected_render();
}
case SCREEN_CONNECTING:
{
break;
//return connecting_render();
}
case SCREEN_SETTINGS_GENERAL:
case SCREEN_SETTINGS_CONTROLS:
case SCREEN_SETTINGS_VIDEO:
case SCREEN_SETTINGS_VIDEO_SELECT_MODE:
case SCREEN_SETTINGS_VIDEO_CUSTOM:
case SCREEN_SETTINGS_SOUND:
{
ui_settings_render(r);
break;
}
case SCREEN_KERNING:
{
break;
}
default: dbg_msg("menu", "invalid screen selected..."); break;
}
}
static int ui_menu_render(const struct rect *r)
{
static struct rect top;
static struct rect middle;
static struct rect bottom;
ui_hsplit_t(r, 48, &top, &middle);
ui_hsplit_b(&middle, 32, &middle, &bottom);
tab_menu_render(&top);
middle_render(&middle);
return -ui_do_button_rect(&bottom, "Quit", 0, &bottom, draw_teewars_button, 0);
}
static int main_render()
{
static bool inited = false;
if (!inited)
{
inited = true;
client_serverbrowse_refresh(0);
}
static int scoll_index = 0, selected_index = -1;
int last_selected_index = selected_index;
do_server_list(20, 160, &scoll_index, &selected_index, 8);
ui_do_edit_box(address, 280, 425, 300, 36, address, sizeof(address));
if (last_selected_index != selected_index && selected_index != -1)
{
SERVER_INFO *server;
server = client_serverbrowse_get(selected_index);
strcpy(address, server->address);
}
static int refresh_button, join_button, quit_button;
static int use_lan = 0;
if (ui_do_button(&refresh_button, "Refresh", 0, 20, 460, 170, 48, draw_teewars_button, 0))
client_serverbrowse_refresh(use_lan);
ui_do_label(60, 420, "Search LAN ", 36);
int last_lan = use_lan;
use_lan = ui_do_check_box(&use_lan, 20, 424, 32, 32, use_lan);
if (use_lan != last_lan)
client_serverbrowse_refresh(use_lan);
if (ui_do_button(&join_button, "Join", 0, 620, 420, 128, 48, draw_teewars_button, 0))
{
client_connect(address);
return 1;
}
if (ui_do_button(&quit_button, "Quit", 0, 620, 490, 128, 48, draw_teewars_button, 0))
return -1;
static int settings_button;
if (ui_do_button(&settings_button, "Settings", 0, 400, 490, 170, 48, draw_teewars_button, 0))
{
config_copy = config;
screen = SCREEN_SETTINGS_GENERAL;
}
// render status text
if(client_serverbrowse_num_requests())
{
char buf[512];
sprintf(buf, "Refreshing %d servers...", client_serverbrowse_num_requests());
ui_do_label(20, 400, buf, 28);
}
else
{
char buf[512];
sprintf(buf, "%d of %d servers", client_serverbrowse_sorted_num(), client_serverbrowse_num());
ui_do_label(20, 400, buf, 28);
}
ui_do_edit_box(&config.b_filter_string, 20+150, 600-80, 200, 36, config.b_filter_string, sizeof(config.b_filter_string));
config.b_filter_empty = ui_do_check_box(&config.b_filter_empty, 20, 600-80, 32, 32, config.b_filter_empty);
config.b_filter_full = ui_do_check_box(&config.b_filter_full, 20+50, 600-80, 32, 32, config.b_filter_full);
config.b_filter_pw = ui_do_check_box(&config.b_filter_pw, 20+100, 600-80, 32, 32, config.b_filter_pw);
return 0;
}
static int settings_general_render()
{
// NAME
ui_do_label(column1_x, row1_y, "Name:", 36);
ui_do_edit_box(config_copy.player_name, column2_x, row1_y, 300, 36, config_copy.player_name, sizeof(config_copy.player_name));
// Dynamic camera
ui_do_label(column1_x, row2_y, "Dynamic Camera:", 36);
config_set_dynamic_camera(&config_copy, ui_do_check_box(&config_copy.dynamic_camera, column2_x + 55, row2_y + 4, 32, 32, config_copy.dynamic_camera));
return 0;
}
static int settings_controls_render()
{
static int scroll_index = 0;
const key_thing keys[] =
{
{ "Move Left:", &config_copy.key_move_left, config_set_key_move_left },
{ "Move Right:", &config_copy.key_move_right, config_set_key_move_right },
{ "Jump:", &config_copy.key_jump, config_set_key_jump },
{ "Fire:", &config_copy.key_fire, config_set_key_fire },
{ "Hook:", &config_copy.key_hook, config_set_key_hook },
{ "Hammer:", &config_copy.key_weapon1, config_set_key_weapon1 },
{ "Pistol:", &config_copy.key_weapon2, config_set_key_weapon2 },
{ "Shotgun:", &config_copy.key_weapon3, config_set_key_weapon3 },
{ "Grenade:", &config_copy.key_weapon4, config_set_key_weapon4 },
{ "Next Weapon:", &config_copy.key_next_weapon, config_set_key_next_weapon },
{ "Prev. Weapon:", &config_copy.key_prev_weapon, config_set_key_prev_weapon },
{ "Emoticon:", &config_copy.key_emoticon, config_set_key_emoticon },
{ "Screenshot:", &config_copy.key_screenshot, config_set_key_screenshot },
};
const int key_count = sizeof(keys) / sizeof(key_thing);
for (int i = 0; i < 6; i++)
{
key_thing key = keys[i + scroll_index];
ui_do_label(column1_x, row1_y + 40 * i, key.name, 36);
key.assign_func(&config_copy, ui_do_key_reader(key.key, column2_x, row1_y + 40 * i, 150, 36, *key.key));
}
scroll_index = do_scroll_bar_vert(&scroll_index, 600, row1_y, 40 * 6, key_count - 6, scroll_index);
ui_do_label(column1_x, row1_y + 40 * 6, "Scroll wheel weapon switch", 22);
config_set_scroll_weapon(&config_copy, ui_do_check_box(&config_copy.scroll_weapon, column2_x + 50, row1_y + 40 * 6, 22, 22, config_copy.scroll_weapon));
return 0;
}
static const int MAX_RESOLUTIONS = 128;
static int settings_video_render_select_mode()
{
static VIDEO_MODE modes[MAX_RESOLUTIONS];
static int num_modes = -1;
if(num_modes == -1)
num_modes = gfx_get_video_modes(modes, MAX_RESOLUTIONS);
static int scroll_index = 0;
scroll_index = do_scroll_bar_vert(&scroll_index, 500, row1_y, 40 * 7, num_modes - 7, scroll_index);
for (int i = 0; i < 7; i++)
{
int index = i + scroll_index;
if(index >= num_modes)
break;
//key_thing key = keys[i + scroll_index];
int depth = modes[index].red+modes[index].green+modes[index].blue;
if(depth < 16)
depth = 16;
else if(depth > 16)
depth = 24;
char buf[128];
int s = 0;
if(modes[index].width == config_copy.gfx_screen_width &&
modes[index].height == config_copy.gfx_screen_height &&
depth == config_copy.gfx_color_depth)
{
s = 1;
}
sprintf(buf, "%c %dx%d %d bit %c", s?'>':' ', modes[index].width, modes[index].height, depth, s?'<':' ');
if(ui_do_button((void*)&modes[index], buf, 0,
column1_x, row1_y + 40 * i, 250, 32.0f, draw_teewars_button, 0))
{
// select
config_set_gfx_color_depth(&config_copy, depth);
config_set_gfx_screen_width(&config_copy, modes[index].width);
config_set_gfx_screen_height(&config_copy, modes[index].height);
screen = SCREEN_SETTINGS_VIDEO;
}
}
static int back_button = 0;
if(ui_do_button(&back_button, "Back", 0, column3_x, row7_y, 150, 32, draw_teewars_button, 0))
screen = SCREEN_SETTINGS_VIDEO;
return 0;
}
static int settings_video_render_custom()
{
ui_do_label(column1_x, row1_y, "Quality Textures:", 36);
config_set_gfx_texture_quality(&config_copy, ui_do_check_box(&config_copy.gfx_texture_quality, column3_x, row1_y + 5, 32, 32, config_copy.gfx_texture_quality));
ui_do_label(column1_x, row2_y, "Texture Compression:", 36);
config_set_gfx_texture_compression(&config_copy, ui_do_check_box(&config_copy.gfx_texture_compression, column3_x, row2_y + 5, 32, 32, config_copy.gfx_texture_compression));
ui_do_label(column1_x, row3_y, "High Detail:", 36);
config_set_gfx_high_detail(&config_copy, ui_do_check_box(&config_copy.gfx_high_detail, column3_x, row3_y + 5, 32, 32, config_copy.gfx_high_detail));
static int back_button = 0;
if(ui_do_button(&back_button, "Back", 0, column3_x, row7_y, 150, 32, draw_teewars_button, 0))
screen = SCREEN_SETTINGS_VIDEO;
return 0;
}
static int settings_video_render()
{
ui_do_label(column1_x, row1_y, "Mode:", 36);
char buf[128];
sprintf(buf, "%dx%d %d bit", config_copy.gfx_screen_width, config_copy.gfx_screen_height, config_copy.gfx_color_depth);
static int select_button = 0;
if(ui_do_button(&select_button, buf, 0, column2_x, row1_y, 300, 32, draw_teewars_button, 0))
screen = SCREEN_SETTINGS_VIDEO_SELECT_MODE;
// we need to draw these bottom up, to make overlapping work correctly
ui_do_label(column1_x, row2_y, "Fullscreen:", 36);
config_set_gfx_fullscreen(&config_copy, ui_do_check_box(&config_copy.gfx_fullscreen, column2_x, row2_y + 5, 32, 32, config_copy.gfx_fullscreen));
ui_do_label(column1_x, row3_y, "V-sync:", 36);
config_set_gfx_vsync(&config_copy, ui_do_check_box(&config_copy.gfx_vsync, column2_x, row3_y + 5, 32, 32, config_copy.gfx_vsync));
int current_level =
(config_copy.gfx_texture_quality<<8)|
(config_copy.gfx_texture_compression<<4)|
(config_copy.gfx_high_detail);
static const int opt_levels[3] = {0x101,0x001,0x010};
static const char *opts[] = {
"High",
"Medium",
"Low",
"Custom"};
int selected = 0; // custom per default
for(; selected < 3; selected++)
{
if(current_level == opt_levels[selected])
break;
}
ui_do_label(column1_x, row4_y, "Quality:", 36);
int new_level = ui_do_combo_box(&config_copy.gfx_texture_quality, column2_x, row4_y, 150, opts, 4, selected);
if(new_level < 3)
{
config_set_gfx_texture_quality(&config_copy, (opt_levels[new_level]>>8)&1);
config_set_gfx_texture_compression(&config_copy, (opt_levels[new_level]>>4)&1);
config_set_gfx_high_detail(&config_copy, opt_levels[new_level]&1);
}
static int custom_button=0;
if(ui_do_button(&custom_button, "Customize", 0, column3_x, row4_y, 130, 32, draw_teewars_button, 0))
screen = SCREEN_SETTINGS_VIDEO_CUSTOM;
ui_do_label(column1_x, row6_y + 50, "(A restart of the game is required for these settings to take effect.)", 20);
return 0;
}
static int settings_sound_render()
{
ui_do_label(column1_x, row1_y, "Volume:", 36);
config_set_volume(&config_copy, do_scroll_bar_horiz(&config_copy.volume, column2_x, row1_y, 200, 255, config_copy.volume));
snd_set_master_volume(config_copy.volume / 255.0f);
return 0;
}
extern void draw_round_rect(float x, float y, float w, float h, float r);
extern void send_info(bool);
static int settings_render(bool ingame)
{
if (ingame)
{
gfx_blend_normal();
gfx_texture_set(-1);
gfx_quads_begin();
gfx_setcolor(0,0,0,0.5f);
draw_round_rect(10, 120, 780, 460, 30.0f);
gfx_quads_end();
}
static int general_button, controls_button, video_button, sound_button;
if (ui_do_button(&general_button, "General", 0, 30, 200, 170, 48, draw_teewars_button, 0))
screen = SCREEN_SETTINGS_GENERAL;
if (ui_do_button(&controls_button, "Controls", 0, 30, 250, 170, 48, draw_teewars_button, 0))
screen = SCREEN_SETTINGS_CONTROLS;
if (ui_do_button(&video_button, "Video", 0, 30, 300, 170, 48, draw_teewars_button, 0))
screen = SCREEN_SETTINGS_VIDEO;
if (ui_do_button(&sound_button, "Sound", 0, 30, 350, 170, 48, draw_teewars_button, 0))
screen = SCREEN_SETTINGS_SOUND;
switch (screen)
{
case SCREEN_SETTINGS_GENERAL: settings_general_render(); break;
case SCREEN_SETTINGS_CONTROLS: settings_controls_render(); break;
case SCREEN_SETTINGS_VIDEO: settings_video_render(); break;
case SCREEN_SETTINGS_VIDEO_SELECT_MODE: settings_video_render_select_mode(); break;
case SCREEN_SETTINGS_VIDEO_CUSTOM: settings_video_render_custom(); break;
case SCREEN_SETTINGS_SOUND: settings_sound_render(); break;
}
// SAVE BUTTON
static int save_button;
if (ui_do_button(&save_button, "Save", 0, 482, 490, 128, 48, draw_teewars_button, 0))
{
// did we change our name?
bool name_changed = strcmp(config.player_name, config_copy.player_name) != 0;
config = config_copy;
if (ingame && name_changed)
send_info(false);
#ifdef CONF_PLATFORM_MACOSX
config_save("~/.teewars");
#else
config_save("default.cfg");
#endif
screen = SCREEN_SERVERS;
}
// CANCEL BUTTON
static int cancel_button;
if (ui_do_button(&cancel_button, "Cancel", 0, 620, 490, 150, 48, draw_teewars_button, 0))
{
snd_set_master_volume(config.volume / 255.0f);
screen = SCREEN_SERVERS;
}
return 0;
}
extern int gametype;
static int ingame_main_render()
{
static int menu_resume, menu_quit, menu_settings;
/*if (gametype == GAMETYPE_TDM)
{
char buf[128];
// Switch team
ui_do_label(100,100,"Switch Team",40);
sprintf(buf,"Team: %s",local_player->team ? "A" : "B");
if (ui_do_button(&menu_team, buf, 0, 30, 150, 170, 48, draw_teewars_button))
{
msg_pack_start(MSG_SWITCHTEAM, MSGFLAG_VITAL);
msg_pack_end();
client_send_msg();
menu_active = false;
}
}*/
const int column1_x = 275;
const int row1_y = 200;
const int row2_y = row1_y + 60;
const int row3_y = row2_y + 60;
const int row4_y = row3_y + 60;
gfx_blend_normal();
gfx_texture_set(-1);
gfx_quads_begin();
gfx_setcolor(0,0,0,0.5f);
draw_round_rect(170, 120, 460, 360, 30.0f);
gfx_quads_end();
ui_do_image(data->images[IMAGE_BANNER].id, 214, 150, 384, 96);
if (ui_do_button(&menu_resume, "Resume Game", 0, column1_x, row2_y, 250, 48, draw_teewars_button, 0))
{
return 1;
}
if (ui_do_button(&menu_quit, "Disconnect", 0, column1_x, row4_y, 250, 48, draw_teewars_button, 0))
{
client_disconnect();
return 1;
}
if (ui_do_button(&menu_settings, "Settings", 0, column1_x, row3_y, 250, 48, draw_teewars_button, 0))
{
config_copy = config;
screen = SCREEN_SETTINGS_GENERAL;
}
return 0;
}
extern "C" double extra_kerning[256*256];
static int kerning_render()
{
static bool loaded = false;
static char text[32] = {0};
if (!loaded)
{
// TODO: fix me
/*
file_stream file;
if (file.open_r("kerning.txt"))
{
line_stream lstream(&file);
int i = 0;
char *line;
while ((line = lstream.get_line()))
extra_kerning[i++] = atof(line);
file.close();
}
if (file.open_r("tracking.txt"))
{
line_stream lstream(&file);
char *line;
for (int i = 0; i < 256; i++)
{
line = lstream.get_line();
current_font->m_CharStartTable[i] = atof(line);
line = lstream.get_line();
current_font->m_CharEndTable[i] = atof(line);
}
file.close();
}
*/
loaded = true;
}
ui_do_edit_box(text, 160, 20, 300, 36, text, sizeof(text));
ui_do_label(160, 250, text, 70);
int len = strlen(text);
for (int i = 0; i < len-1; i++)
{
char s[3] = {0};
s[0] = text[i];
s[1] = text[i+1];
ui_do_label(10, 30 * i + 10, s, 45);
int index = s[0] + s[1] * 256;
// less
if (ui_do_button((void *)(100 + i * 2), "", 0, 50, 30 * i + 10 + 20, 16, 16, draw_single_part_button, (void *)GUI_MISC_SLIDER_BIG_ARROW_LEFT))
{
extra_kerning[index] -= 0.01;
}
// more
if (ui_do_button((void *)(100 + i * 2 + 1), "", 0, 66, 30 * i + 10 + 20, 16, 16, draw_single_part_button, (void *)GUI_MISC_SLIDER_BIG_ARROW_RIGHT))
{
extra_kerning[index] += 0.01;
}
char num[16];
sprintf(num, "(%f)", extra_kerning[index]);
ui_do_label(84, 30 * i + 30, num, 12);
}
for (int i = 0; i < len; i++)
{
char s[2] = {0};
s[0] = text[i];
ui_do_label(700, 35 * i + 10, s, 45);
gfx_blend_normal();
gfx_texture_set(-1);
gfx_quads_begin();
gfx_setcolor(0,0,0,0.5);
gfx_quads_drawTL(700,35*i+20,1,30);
gfx_quads_drawTL(700+45*(current_font->m_CharEndTable[(int)s[0]]-current_font->m_CharStartTable[(int)s[0]]),35*i+20,1,30);
gfx_quads_end();
// less
if (ui_do_button((void *)(200 + i * 2), "", 0, 650, 35 * i + 10 + 15, 16, 16, draw_single_part_button, (void *)GUI_MISC_SLIDER_BIG_ARROW_LEFT))
{
current_font->m_CharStartTable[(int)s[0]] -= 0.01f;
}
// more
if (ui_do_button((void *)(200 + i * 2 + 1), "", 0, 666, 35 * i + 10 + 15, 16, 16, draw_single_part_button, (void *)GUI_MISC_SLIDER_BIG_ARROW_RIGHT))
{
current_font->m_CharStartTable[(int)s[0]] += 0.01f;
}
char num[16];
sprintf(num, "(%f)", current_font->m_CharStartTable[(int)s[0]]);
ui_do_label(645, 35 * i + 40, num, 12);
// less
if (ui_do_button((void *)(300 + i * 2), "", 0, 750, 35 * i + 10 + 15, 16, 16, draw_single_part_button, (void *)GUI_MISC_SLIDER_BIG_ARROW_LEFT))
{
current_font->m_CharEndTable[(int)s[0]] -= 0.01f;
}
// more
if (ui_do_button((void *)(300 + i * 2 + 1), "", 0, 766, 35 * i + 10 + 15, 16, 16, draw_single_part_button, (void *)GUI_MISC_SLIDER_BIG_ARROW_RIGHT))
{
current_font->m_CharEndTable[(int)s[0]] += 0.01f;
}
sprintf(num, "(%f)", current_font->m_CharEndTable[(int)s[0]]);
ui_do_label(745, 35 * i + 40, num, 12);
}
// SAVE BUTTON
static int save_button;
if (ui_do_button(&save_button, "Save", 0, 482, 520, 128, 48, draw_teewars_button, 0))
{
// TODO: fix or remove me
/*
file_stream file;
if (file.open_w("kerning.txt"))
{
char t[16];
for (int i = 0; i < 256*256; i++)
{
sprintf(t, "%f\n", extra_kerning[i]);
file.write(t, strlen(t));
}
file.close();
}
if (file.open_w("tracking.txt"))
{
char t[16];
for (int i = 0; i < 256; i++)
{
sprintf(t, "%f\n", current_font->m_CharStartTable[i]);
file.write(t, strlen(t));
sprintf(t, "%f\n", current_font->m_CharEndTable[i]);
file.write(t, strlen(t));
}
file.close();
}
*/
//screen = 0;
}
// CANCEL BUTTON
static int cancel_button;
if (ui_do_button(&cancel_button, "Cancel", 0, 620, 520, 150, 48, draw_teewars_button, 0))
screen = SCREEN_SERVERS;
return 0;
}
int render_popup(const char *caption, const char *text, const char *button_text)
{
float tw;
float w = 700;
float h = 300;
float x = 800/2-w/2;
float y = 600/2-h/2;
gfx_blend_normal();
gfx_texture_set(-1);
gfx_quads_begin();
gfx_setcolor(0,0,0,0.50f);
draw_round_rect(x, y, w, h, 40.0f);
gfx_quads_end();
tw = gfx_pretty_text_width(48.0f, caption, -1);
ui_do_label(x+w/2-tw/2, y+20, caption, 48.0f);
tw = gfx_pretty_text_width(32.0f, text, -1);
gfx_pretty_text(x+w/2-tw/2, y+130, 32.0f, text, -1);
if(button_text)
{
static int back_button = 0;
if(ui_do_button(&back_button, button_text, 0, x+w/2-100, y+220, 200, 48, draw_teewars_button, 0))
return 1;
if(inp_key_down(KEY_ESC) || inp_key_down(KEY_ENTER))
return 1;
}
return 0;
}
static int disconnected_render()
{
if(strlen(client_error_string()) == 0)
screen = SCREEN_SERVERS;
else
{
if(render_popup("Disconnected", client_error_string(), "Back"))
screen = SCREEN_SERVERS;
}
return 0;
}
static int connecting_render()
{
char buf[256];
sprintf(buf, "Server: %s", address);
if(render_popup("Connecting", buf, "Abort"))
{
client_disconnect();
screen = SCREEN_SERVERS;
}
return 0;
}
void menu_do_disconnected()
{
screen = SCREEN_DISCONNECTED;
}
void menu_do_connecting()
{
screen = SCREEN_CONNECTING;
}
void menu_do_connected()
{
screen = SCREEN_SERVERS;
}
static int menu_render(bool ingame)
{
if (!ingame)
{
// background color
gfx_clear(0.65f,0.78f,0.9f);
//gfx_clear(89/255.f,122/255.f,0.0);
// GUI coordsys
gfx_mapscreen(0,0,800.0f,600.0f);
static int64 start = time_get();
float t = double(time_get() - start) / double(time_freq());
gfx_mapscreen(0,0,1600.0f,1200.0f);
draw_background(t);
gfx_mapscreen(0,0,800.0f,600.0f);
if (screen != SCREEN_KERNING)
{
ui_do_image(data->images[IMAGE_BANNER].id, 200, 20, 512, 128);
ui_do_label(20.0f, 600.0f-40.0f, "Version: " TEEWARS_VERSION, 36);
if(config.debug)
ui_do_label(20.0f, 600.0f-60.0f, "Nethash: " TEEWARS_NETVERSION_HASH, 24);
}
}
else
{
gfx_mapscreen(0, 0, 800, 600);
}
if(config.dbg_new_gui)
{
struct rect *screen = ui_screen();
static float scale = 1.0f;
if (inp_key_pressed('I'))
scale += 0.01;
if (inp_key_pressed('O'))
scale -= 0.01;
ui_scale(scale);
int retn = ui_menu_render(screen);
/*gfx_texture_set(-1);
gfx_lines_begin();
ui_foreach_rect(draw_rect);
gfx_lines_end();*/
return retn;
}
else
{
switch (screen)
{
case SCREEN_SERVERS: return ingame ? ingame_main_render() : main_render();
case SCREEN_DISCONNECTED: return disconnected_render();
case SCREEN_CONNECTING: return connecting_render();
case SCREEN_SETTINGS_GENERAL:
case SCREEN_SETTINGS_CONTROLS:
case SCREEN_SETTINGS_VIDEO:
case SCREEN_SETTINGS_VIDEO_SELECT_MODE:
case SCREEN_SETTINGS_VIDEO_CUSTOM:
case SCREEN_SETTINGS_SOUND: return settings_render(ingame);
case SCREEN_KERNING: return kerning_render();
default: dbg_msg("menu", "invalid screen selected..."); return 0;
}
}
}
extern "C" void modmenu_init() // TODO: nastyness
{
// TODO: should be removed
current_font->font_texture = gfx_load_texture("data/big_font.png");
}
void modmenu_shutdown()
{
}
extern int menu2_render();
extern "C" int modmenu_render(int ingame) // TODO: nastyness
{
static int mouse_x = 0;
static int mouse_y = 0;
// handle mouse movement
float mx, my;
{
int rx, ry;
inp_mouse_relative(&rx, &ry);
mouse_x += rx;
mouse_y += ry;
if(mouse_x < 0) mouse_x = 0;
if(mouse_y < 0) mouse_y = 0;
if(mouse_x > gfx_screenwidth()) mouse_x = gfx_screenwidth();
if(mouse_y > gfx_screenheight()) mouse_y = gfx_screenheight();
// update the ui
mx = (mouse_x/(float)gfx_screenwidth())*800.0f;
my = (mouse_y/(float)gfx_screenheight())*600.0f;
int buttons = 0;
if(inp_key_pressed(KEY_MOUSE_1)) buttons |= 1;
if(inp_key_pressed(KEY_MOUSE_2)) buttons |= 2;
if(inp_key_pressed(KEY_MOUSE_3)) buttons |= 4;
ui_update(mx,my,mx*3.0f,my*3.0f,buttons);
}
//int r = menu_render(server_address, str, max_len);
//int r = menu2_render(ingame);
(void)menu_render;
int r = menu2_render();
gfx_texture_set(data->images[IMAGE_CURSOR].id);
gfx_quads_begin();
gfx_setcolor(1,1,1,1);
gfx_quads_drawTL(mx,my,24,24);
gfx_quads_end();
inp_clear();
return r;
}
|