File: hud_editor.c

package info (click to toggle)
ezquake 2.2%2Bgit20150324-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 6,996 kB
  • ctags: 16,582
  • sloc: ansic: 143,243; makefile: 339; tcl: 107; sh: 28
file content (2786 lines) | stat: -rw-r--r-- 77,043 bytes parent folder | download
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
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
/*
Copyright (C) 2011 ezQuake team

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, see <http://www.gnu.org/licenses/>.
*/
/**
	HUD Editor module

	Initial concept code jogihoogi, rewritten by Cokeman, Feb 2007
*/

#include "quakedef.h"
#include "qsound.h"
#include "utils.h"
#include "menu.h"
#include "keys.h"
#include "Ctrl.h"
#include "ez_controls.h"
#include "ez_button.h"
#include "ez_label.h"
#include "ez_scrollbar.h"
#include "ez_scrollpane.h"
#include "ez_slider.h"
#include "ez_listview.h"
#include "ez_window.h"
#include "hud.h"
#include "hud_editor.h"

ez_tree_t help_control_tree;

extern mpic_t		*scr_cursor_icon;	// cl_screen.c
extern cvar_t		hud_planmode;		// hud_common.c

mpic_t				*hud_editor_move_icon	= NULL;
mpic_t				*hud_editor_resize_icon	= NULL;
mpic_t				*hud_editor_align_icon	= NULL;
mpic_t				*hud_editor_place_icon	= NULL;

cvar_t				hud_editor_allowresize	= {"hud_editor_allowresize", "1"};	// Show resize handles / allow resizing.
cvar_t				hud_editor_allowmove	= {"hud_editor_allowmove", "1"};	// Allow moving...
cvar_t				hud_editor_allowplace	= {"hud_editor_allowplace", "1"};	// Allow placing HUDs.
cvar_t				hud_editor_allowalign	= {"hud_editor_allowalign", "1"};	// Allow aligning HUDs.

extern hud_t		*hud_huds;										// The list of HUDs..
hud_t				*selected_hud			= NULL;					// The currently selected HUD.

qbool				hud_editor				= false;				// If we're in HUD editor mode or not.
hud_editor_mode_t	hud_editor_mode			= hud_editmode_off;		// The current mode the HUD editor is in.
hud_editor_mode_t	hud_editor_prevmode		= hud_editmode_off;		// The previous mode the HUD editor was in.

qbool				hud_editor_showhelp		= false;				// Show the help plaque or not?
qbool				hud_editor_showoutlines	= true;					// Show guidelines around the HUD elements.

float				hud_mouse_x;									// The screen coordinates of the mouse cursor.
float				hud_mouse_y;

// Macros for what mouse buttons are clicked.
#define MOUSEDOWN			( keydown[K_MOUSE1] ||  keydown[K_MOUSE2]|| keydown[K_MOUSE3])
#define MOUSEDOWN_1_2		( keydown[K_MOUSE1] &&  keydown[K_MOUSE2])
#define MOUSEDOWN_1_3		( keydown[K_MOUSE1] &&  keydown[K_MOUSE3])
#define MOUSEDOWN_1_2_3		( keydown[K_MOUSE1] &&  keydown[K_MOUSE2] &&  keydown[K_MOUSE3])
#define MOUSEDOWN_1_ONLY	( keydown[K_MOUSE1] && !keydown[K_MOUSE2] && !keydown[K_MOUSE3])
#define MOUSEDOWN_2_ONLY	(!keydown[K_MOUSE1] &&  keydown[K_MOUSE2] && !keydown[K_MOUSE3])
#define MOUSEDOWN_3_ONLY	(!keydown[K_MOUSE1] && !keydown[K_MOUSE2] &&  keydown[K_MOUSE3])
#define MOUSEDOWN_1_2_ONLY	( keydown[K_MOUSE1] &&  keydown[K_MOUSE2] && !keydown[K_MOUSE3])
#define MOUSEDOWN_1_3_ONLY	( keydown[K_MOUSE1] && !keydown[K_MOUSE2] &&  keydown[K_MOUSE3])
#define MOUSEDOWN_2_3_ONLY	(!keydown[K_MOUSE1] &&  keydown[K_MOUSE2] &&  keydown[K_MOUSE3])
#define MOUSEDOWN_NONE		(!keydown[K_MOUSE1] && !keydown[K_MOUSE2] && !keydown[K_MOUSE3])

#define HUD_CENTER_X(h)		((h)->lx + (h)->lw / 2)	// Gets the coordinates for the center point of the specified hud.
#define HUD_CENTER_Y(h)		((h)->ly + (h)->lh / 2)

//
// HUD align polygons.
//
#define HUD_ALIGN_POLYCOUNT_CORNER	5
#define HUD_ALIGN_POLYCOUNT_EDGE	4
#define HUD_ALIGN_POLYCOUNT_CENTER	8
#define HUD_ALIGN_POLYCOUNT_CONSOLE	4

vec3_t *hud_align_current_poly = NULL;	// The current alignment polygon when in alignment mode.
int hud_align_current_polycount = 0;	// Number of vertices in teh polygon.

vec3_t hud_align_topright_poly[HUD_ALIGN_POLYCOUNT_CORNER];
vec3_t hud_align_top_poly[HUD_ALIGN_POLYCOUNT_EDGE];
vec3_t hud_align_topleft_poly[HUD_ALIGN_POLYCOUNT_CORNER];
vec3_t hud_align_left_poly[HUD_ALIGN_POLYCOUNT_EDGE];
vec3_t hud_align_bottomleft_poly[HUD_ALIGN_POLYCOUNT_CORNER];
vec3_t hud_align_bottom_poly[HUD_ALIGN_POLYCOUNT_EDGE];
vec3_t hud_align_bottomright_poly[HUD_ALIGN_POLYCOUNT_CORNER];
vec3_t hud_align_right_poly[HUD_ALIGN_POLYCOUNT_EDGE];
vec3_t hud_align_center_poly[HUD_ALIGN_POLYCOUNT_CENTER];

vec3_t hud_align_consoleleft_poly[HUD_ALIGN_POLYCOUNT_CONSOLE];
vec3_t hud_align_console_poly[HUD_ALIGN_POLYCOUNT_CONSOLE];
vec3_t hud_align_consoleright_poly[HUD_ALIGN_POLYCOUNT_CONSOLE];

typedef enum hud_alignmode_s
{
	hud_align_center,
	hud_align_top,
	hud_align_topleft,
	hud_align_left,
	hud_align_bottomleft,
	hud_align_bottom,
	hud_align_bottomright,
	hud_align_right,
	hud_align_topright,
	hud_align_consoleleft,
	hud_align_consoleright,
	hud_align_console
	// Not including before/after for now.
} hud_alignmode_t;

hud_alignmode_t	hud_alignmode = hud_align_center;

// Possible positions for a grep handle.
typedef enum hud_greppos_s
{
	pos_visible,	// On screen.
	pos_top,
	pos_left,
	pos_bottom,
	pos_right
} hud_greppos_t;

// A Grep handle for when a HUD goes offscreen.
typedef struct hud_grephandle_s
{
	hud_t					*hud;			// HUD associated with this grep handle.
	int						x;				// The position in screen coordinates for the grephandle.
	int						y;
	int						width;
	int						height;
	qbool					highlighted;	// Should this grephandle be drawn highlighted?
	hud_greppos_t			pos;			// The offscreen position of the HUD associated with this handle.
	struct hud_grephandle_s	*next;
	struct hud_grephandle_s	*previous;
} hud_grephandle_t;

hud_grephandle_t	*hud_greps = NULL;					// The list of "grep handles" that are shown if a HUD element is moved offscreen.

hud_grephandle_t	*hud_hoverlist = NULL;				// The list of HUDs the mouse is hovering.
int					hud_hoverlist_count = 0;			// The number of hovered HUDs...
qbool				hud_hoverlist_pos_is_set = false;	// Has the coordinates been set for the hoverlist yet?
float				hud_hoverlist_x;					// The x position of the hover list.
float				hud_hoverlist_y;					// The y position of the hover list.
hud_grephandle_t	hud_containers[MAX_HUD_ELEMENTS];	// List of HUDs which have been hovered.

//
// Adds a new HUD to the list of HUDs that are being hovered.
//
static void HUD_Editor_AddHoverHud(hud_grephandle_t *hud_container)
{
	// Nothing to add.
	if(!hud_container)
	{
		return;
	}

	hud_container->next = hud_hoverlist;
	if(hud_hoverlist)
	{
		hud_hoverlist->previous = hud_container;
	}

	hud_hoverlist = hud_container;

	hud_hoverlist_count++;
}

//
// Associates ("Creates") a HUD with a HUD container.
//
static hud_grephandle_t *HUD_Editor_CreateHoverHud(hud_t *hud)
{
	static int i = 0;
	int j = 0;

	if(i >= MAX_HUD_ELEMENTS - 1)
	{
		return NULL;
	}

	// Check if the HUD already exists and use that one if that's the case...
	for(j = 0; j < MAX_HUD_ELEMENTS && hud_containers[j].hud; j++)
	{
		if(hud_containers[j].hud == hud)
		{
			hud_containers[j].next = NULL;
			hud_containers[j].previous = NULL;
			return &hud_containers[j];
		}
	}

	// The HUD wasn't found so add it to the end of the list.
	hud_containers[i].hud = hud;
	hud_containers[i].next = NULL;
	hud_containers[i].previous = NULL;
	i++;

	return &hud_containers[i - 1];
}

//
// Find the HUD that is being selected in the hover list.
//
static hud_t *HUD_Editor_FindHoverListSelection(hud_grephandle_t *list)
{
	hud_t *match = NULL;
	hud_grephandle_t *hud_iter = list;

	while(hud_iter)
	{
		if(hud_iter->hud
			&& hud_mouse_x > hud_iter->x
			&& hud_mouse_x < (hud_iter->x + hud_iter->width)
			&& hud_mouse_y > hud_iter->y
			&& hud_mouse_y < (hud_iter->y + hud_iter->height))
		{
			hud_iter->highlighted = true;
			match = hud_iter->hud;
		}
		else
		{
			hud_iter->highlighted = false;
		}

		hud_iter = hud_iter->next;
	}

	return match;
}

//
// Draw the hover list...
//
static qbool HUD_Editor_DrawHoverList(int x, int y, hud_grephandle_t *list)
{
	#define PADDING 5
	#define LINEGAP	1
	int width = 0;
	int height = 0;
	int i = 0;
	clrinfo_t color, highlight;
	hud_grephandle_t *hud_iter = list;

	// No point in showing a list if there's only one hud under the cursor
	// or if the user hasn't right-clicked.
	if (hud_hoverlist_count <= 1 || hud_editor_mode != hud_editmode_hoverlist)
	{
		return false;
	}

	// Get the width of to draw with.
	while (hud_iter)
	{
		width = max(width, (PADDING + 2 + strlen(hud_iter->hud->name)) * 8);
		hud_iter = hud_iter->next;
	}

	// Draw the background fill.
	height = ((hud_hoverlist_count - 1) * (8 + LINEGAP)) + (2 * PADDING);
	Draw_AlphaFillRGB(x, y - height, width, height, RGBA_TO_COLOR(0, 0, 0, 255));

	hud_iter = list;

	// Highlight color (yellow)...
	highlight.c = RGBA_TO_COLOR(0, 255, 0, 255);
	highlight.i = 0;

	// Orange.
	color.c = RGBA_TO_COLOR(255, 150, 0, 255);
	color.i = 0;

	// Draw the list items.
	while(hud_iter)
	{
		// Calculate the size and position of the HUD in the list.
		hud_iter->x = x;
		hud_iter->y = y - height + (i * 8) + LINEGAP;
		hud_iter->width = width;
		hud_iter->height = 8;

		// Draw the z-order + name of the HUD.
		Draw_ColoredString3(hud_iter->x, hud_iter->y,
			va("%2d - %s", hud_iter->hud->order->integer, hud_iter->hud->name),
			(hud_iter->highlighted ? &highlight : &color), 1, 0);

		hud_iter = hud_iter->next;
		i++;
	}

	return true;
}

//
// Sets a new HUD Editor mode (and saves the previous one).
//
static void HUD_Editor_SetMode(hud_editor_mode_t newmode)
{
	hud_editor_prevmode = hud_editor_mode;
	hud_editor_mode = newmode;
}

//
// Draws a tool tip with a background next to the cursor.
//
static void HUD_Editor_DrawTooltip(int x, int y, char *string, color_t color) //float r, float g, float b, float a)
{
	int len = strlen(string) * 8;

	y -= 9;

	// Make sure we're drawing within the screen.
	if (x + len > vid.width)
	{
		x -= len;
	}

	if (y + 9 > vid.height)
	{
		y -= 9;
	}

	Draw_AlphaFillRGB(x, y, len, 8, color);
	Draw_String(x, y, string);
}

//
// Gets an alignment string for a specified alignmode enum.
//
static char *HUD_Editor_GetAlignmentString(hud_alignmode_t align)
{
	switch(hud_alignmode)
	{
		case hud_align_center :		return "center center";
		case hud_align_top :		return "center top";
		case hud_align_topleft :	return "left top";
		case hud_align_left :		return "left center";
		case hud_align_bottomleft :	return "left bottom";
		case hud_align_bottom :		return "center bottom";
		case hud_align_bottomright :return "right bottom";
		case hud_align_right :		return "right center";
		case hud_align_topright :	return "right top";
		case hud_align_consoleleft :return "left console";
		case hud_align_console :	return "center console";
		case hud_align_consoleright:return "right console";
		default :					return "";
	}
}

//
// Returns an alignment enum based on a specified alignment string.
//
static hud_alignmode_t HUD_Editor_GetAlignmentFromString(char *alignstr)
{
	if(!strcmp(alignstr, "center center"))
	{
		return hud_align_center;
	}
	else if(!strcmp(alignstr, "center top"))
	{
		return hud_align_top;
	}
	else if(!strcmp(alignstr, "left top"))
	{
		return hud_align_topleft;
	}
	else if(!strcmp(alignstr, "left center"))
	{
		return hud_align_left;
	}
	else if(!strcmp(alignstr, "left bottom"))
	{
		return hud_align_bottomleft;
	}
	else if(!strcmp(alignstr, "center bottom"))
	{
		return hud_align_bottom;
	}
	else if(!strcmp(alignstr, "right bottom"))
	{
		return hud_align_bottomright;
	}
	else if(!strcmp(alignstr, "right center"))
	{
		return hud_align_right;
	}
	else if(!strcmp(alignstr, "right top"))
	{
		return hud_align_topright;
	}
	else if(!strcmp(alignstr, "left console"))
	{
		return hud_align_consoleleft;
	}
	else if(!strcmp(alignstr, "center console"))
	{
		return hud_align_console;
	}
	else if(!strcmp(alignstr, "right console"))
	{
		return hud_align_consoleright;
	}

	return hud_align_center;
}

//
// Returns the alignment we are trying to align the selected HUD to at it's placement
// when in alignment mode.
//
static hud_alignmode_t HUD_Editor_GetAlignment(int x, int y, hud_t *hud_element)
{
	// For less clutter.
	float	mid_x		= 0.0;
	float	mid_y		= 0.0;
	int		offset_x	= 0;
	int		offset_y	= 0;

	if(hud_element)
	{
		// Aligns for HUD elements.
		offset_x = hud_element->lx;
		offset_y = hud_element->ly;
		mid_x = hud_element->lw / 2.0;
		mid_y = hud_element->lh / 2.0;
	}
	else
	{
		// Aligns for Screen.
		offset_x = 0;
		offset_y = (int)(vid.height * 0.05);
		mid_x = vid.width / 2.0;
		mid_y = (vid.height + offset_y) / 2.0;
	}

	if(!hud_element)
	{
		// Console left.
		{
			hud_align_consoleleft_poly[0][0] = 0;
			hud_align_consoleleft_poly[0][1] = 0;
			hud_align_consoleleft_poly[0][2] = 0;

			hud_align_consoleleft_poly[1][0] = 0;
			hud_align_consoleleft_poly[1][1] = offset_y;
			hud_align_consoleleft_poly[1][2] = 0;

			hud_align_consoleleft_poly[2][0] = mid_x / 2.0;
			hud_align_consoleleft_poly[2][1] = offset_y;
			hud_align_consoleleft_poly[2][2] = 0;

			hud_align_consoleleft_poly[3][0] = mid_x / 2.0;
			hud_align_consoleleft_poly[3][1] = 0;
			hud_align_consoleleft_poly[3][2] = 0;

			if (IsPointInPolygon(HUD_ALIGN_POLYCOUNT_CONSOLE, hud_align_consoleleft_poly, x, y))
			{
				hud_align_current_poly = hud_align_consoleleft_poly;
				hud_align_current_polycount = HUD_ALIGN_POLYCOUNT_CONSOLE;
				return hud_align_consoleleft;
			}
		}

		// Console.
		{
			hud_align_console_poly[0][0] = mid_x / 2.0;
			hud_align_console_poly[0][1] = 0;
			hud_align_console_poly[0][2] = 0;

			hud_align_console_poly[1][0] = mid_x / 2.0;
			hud_align_console_poly[1][1] = offset_y;
			hud_align_console_poly[1][2] = 0;

			hud_align_console_poly[2][0] = mid_x + (mid_x / 2.0);
			hud_align_console_poly[2][1] = offset_y;
			hud_align_console_poly[2][2] = 0;

			hud_align_console_poly[3][0] = mid_x + (mid_x / 2.0);
			hud_align_console_poly[3][1] = 0;
			hud_align_console_poly[3][2] = 0;

			if (IsPointInPolygon(HUD_ALIGN_POLYCOUNT_CONSOLE, hud_align_console_poly, x, y))
			{
				hud_align_current_poly = hud_align_console_poly;
				hud_align_current_polycount = HUD_ALIGN_POLYCOUNT_CONSOLE;
				return hud_align_console;
			}
		}

		// Console right.
		{
			hud_align_consoleright_poly[0][0] = mid_x + (mid_x / 2.0);
			hud_align_consoleright_poly[0][1] = 0;
			hud_align_consoleright_poly[0][2] = 0;

			hud_align_consoleright_poly[1][0] = mid_x + (mid_x / 2.0);
			hud_align_consoleright_poly[1][1] = offset_y;
			hud_align_consoleright_poly[1][2] = 0;

			hud_align_consoleright_poly[2][0] = 2 * mid_x;
			hud_align_consoleright_poly[2][1] = offset_y;
			hud_align_consoleright_poly[2][2] = 0;

			hud_align_consoleright_poly[3][0] = 2 * mid_x;
			hud_align_consoleright_poly[3][1] = 0;
			hud_align_consoleright_poly[3][2] = 0;

			if (IsPointInPolygon(HUD_ALIGN_POLYCOUNT_CONSOLE, hud_align_consoleright_poly, x, y))
			{
				hud_align_current_poly = hud_align_consoleright_poly;
				hud_align_current_polycount = HUD_ALIGN_POLYCOUNT_CONSOLE;
				return hud_align_consoleright;
			}
		}
	}

	// Top right.
	{
		hud_align_topright_poly[0][0] = mid_x + (mid_x / 2.0);
		hud_align_topright_poly[0][1] = 0;
		hud_align_topright_poly[0][2] = 0;

		hud_align_topright_poly[1][0] = mid_x + (mid_x / 4.0);
		hud_align_topright_poly[1][1] = mid_y - (mid_y / 2.0);
		hud_align_topright_poly[1][2] = 0;

		hud_align_topright_poly[2][0] = mid_x + (mid_x / 2.0);
		hud_align_topright_poly[2][1] = mid_y - (mid_y / 4.0);
		hud_align_topright_poly[2][2] = 0;

		hud_align_topright_poly[3][0] = 2 * mid_x;
		hud_align_topright_poly[3][1] = mid_y - (mid_y / 2.0);
		hud_align_topright_poly[3][2] = 0;

		hud_align_topright_poly[4][0] = 2 * mid_x;
		hud_align_topright_poly[4][1] = 0;
		hud_align_topright_poly[4][2] = 0;

		if (IsPointInPolygon(HUD_ALIGN_POLYCOUNT_CORNER, hud_align_topright_poly, x - offset_x, y - offset_y))
		{
			hud_align_current_poly = hud_align_topright_poly;
			hud_align_current_polycount = HUD_ALIGN_POLYCOUNT_CORNER;
			return hud_align_topright;
		}
	}

	// Top.
	{
		hud_align_top_poly[0][0] = mid_x / 2.0;
		hud_align_top_poly[0][1] = 0;
		hud_align_top_poly[0][2] = 0;

		hud_align_top_poly[1][0] = mid_x - (mid_x / 4.0);
		hud_align_top_poly[1][1] = mid_y - (mid_y / 2.0);
		hud_align_top_poly[1][2] = 0;

		hud_align_top_poly[2][0] = mid_x + (mid_x / 4.0);
		hud_align_top_poly[2][1] = mid_y - (mid_y / 2.0);
		hud_align_top_poly[2][2] = 0;

		hud_align_top_poly[3][0] = mid_x + (mid_x / 2.0);
		hud_align_top_poly[3][1] = 0;
		hud_align_top_poly[3][2] = 0;

		if (IsPointInPolygon(HUD_ALIGN_POLYCOUNT_EDGE, hud_align_top_poly, x - offset_x, y - offset_y))
		{
			hud_align_current_poly = hud_align_top_poly;
			hud_align_current_polycount = HUD_ALIGN_POLYCOUNT_EDGE;
			return hud_align_top;
		}
	}

	// Top Left.
	{
		hud_align_topleft_poly[0][0] = 0;
		hud_align_topleft_poly[0][1] = 0;
		hud_align_topleft_poly[0][2] = 0;

		hud_align_topleft_poly[1][0] = 0;
		hud_align_topleft_poly[1][1] = mid_y - (mid_y / 2.0);
		hud_align_topleft_poly[1][2] = 0;

		hud_align_topleft_poly[2][0] = mid_x - (mid_x / 2.0);
		hud_align_topleft_poly[2][1] = mid_y - (mid_y / 4.0);
		hud_align_topleft_poly[2][2] = 0;

		hud_align_topleft_poly[3][0] = mid_x - (mid_x / 4.0);
		hud_align_topleft_poly[3][1] = mid_y - (mid_y / 2.0);
		hud_align_topleft_poly[3][2] = 0;

		hud_align_topleft_poly[4][0] = mid_x - (mid_x / 2.0);
		hud_align_topleft_poly[4][1] = 0;
		hud_align_topleft_poly[4][2] = 0;

		if (IsPointInPolygon(HUD_ALIGN_POLYCOUNT_CORNER, hud_align_topleft_poly, x - offset_x, y - offset_y))
		{
			hud_align_current_poly = hud_align_topleft_poly;
			hud_align_current_polycount = HUD_ALIGN_POLYCOUNT_CORNER;
			return hud_align_topleft;
		}
	}

	// Left.
	{
		hud_align_left_poly[0][0] = 0;
		hud_align_left_poly[0][1] = mid_y / 2.0;
		hud_align_left_poly[0][2] = 0;

		hud_align_left_poly[1][0] = 0;
		hud_align_left_poly[1][1] = mid_y + (mid_y / 2.0);
		hud_align_left_poly[1][2] = 0;

		hud_align_left_poly[2][0] = mid_x / 2.0;
		hud_align_left_poly[2][1] = mid_y + (mid_y / 4.0);
		hud_align_left_poly[2][2] = 0;

		hud_align_left_poly[3][0] = mid_x / 2.0;
		hud_align_left_poly[3][1] = mid_y - (mid_y / 4.0);
		hud_align_left_poly[3][2] = 0;

		if (IsPointInPolygon(HUD_ALIGN_POLYCOUNT_EDGE, hud_align_left_poly, x - offset_x, y - offset_y))
		{
			hud_align_current_poly = hud_align_left_poly;
			hud_align_current_polycount = HUD_ALIGN_POLYCOUNT_EDGE;
			return hud_align_left;
		}
	}

	// Bottom Left.
	{
		hud_align_bottomleft_poly[0][0] = 0;
		hud_align_bottomleft_poly[0][1] = mid_y + (mid_y / 2.0);
		hud_align_bottomleft_poly[0][2] = 0;

		hud_align_bottomleft_poly[1][0] = 0;
		hud_align_bottomleft_poly[1][1] = 2 * mid_y;
		hud_align_bottomleft_poly[1][2] = 0;

		hud_align_bottomleft_poly[2][0] = mid_x / 2.0;
		hud_align_bottomleft_poly[2][1] = 2 * mid_y;
		hud_align_bottomleft_poly[2][2] = 0;

		hud_align_bottomleft_poly[3][0] = mid_x - (mid_x / 4.0);
		hud_align_bottomleft_poly[3][1] = mid_y + (mid_y / 2.0);
		hud_align_bottomleft_poly[3][2] = 0;

		hud_align_bottomleft_poly[4][0] = mid_x / 2.0;
		hud_align_bottomleft_poly[4][1] = mid_y + (mid_y / 4.0);
		hud_align_bottomleft_poly[4][2] = 0;

		if (IsPointInPolygon(HUD_ALIGN_POLYCOUNT_CORNER, hud_align_bottomleft_poly, x - offset_x, y - offset_y))
		{
			hud_align_current_poly = hud_align_bottomleft_poly;
			hud_align_current_polycount = HUD_ALIGN_POLYCOUNT_CORNER;
			return hud_align_bottomleft;
		}
	}

	// Bottom.
	{
		hud_align_bottom_poly[0][0] = mid_x - (mid_x / 2.0);
		hud_align_bottom_poly[0][1] = 2 * mid_y;
		hud_align_bottom_poly[0][2] = 0;

		hud_align_bottom_poly[1][0] = mid_x + (mid_x / 2.0);
		hud_align_bottom_poly[1][1] = 2 * mid_y;
		hud_align_bottom_poly[1][2] = 0;

		hud_align_bottom_poly[2][0] = mid_x + (mid_x / 4.0);
		hud_align_bottom_poly[2][1] = mid_y + (mid_y / 2.0);
		hud_align_bottom_poly[2][2] = 0;

		hud_align_bottom_poly[3][0] = mid_x - (mid_x / 4.0);
		hud_align_bottom_poly[3][1] = mid_y + (mid_y / 2.0);
		hud_align_bottom_poly[3][2] = 0;

		if (IsPointInPolygon(HUD_ALIGN_POLYCOUNT_EDGE, hud_align_bottom_poly, x - offset_x, y - offset_y))
		{
			hud_align_current_poly = hud_align_bottom_poly;
			hud_align_current_polycount = HUD_ALIGN_POLYCOUNT_EDGE;
			return hud_align_bottom;
		}
	}

	// Bottom Right.
	{
		hud_align_bottomright_poly[0][0] = mid_x + (mid_x / 2.0);
		hud_align_bottomright_poly[0][1] = 2 * mid_y;
		hud_align_bottomright_poly[0][2] = 0;

		hud_align_bottomright_poly[1][0] = 2 * mid_x;
		hud_align_bottomright_poly[1][1] = 2 * mid_y;
		hud_align_bottomright_poly[1][2] = 0;

		hud_align_bottomright_poly[2][0] = 2 * mid_x;
		hud_align_bottomright_poly[2][1] = mid_y + (mid_y / 2.0);
		hud_align_bottomright_poly[2][2] = 0;

		hud_align_bottomright_poly[3][0] = mid_x + (mid_x / 2.0);
		hud_align_bottomright_poly[3][1] = mid_y + (mid_y / 4.0);
		hud_align_bottomright_poly[3][2] = 0;

		hud_align_bottomright_poly[4][0] = mid_x + (mid_x / 4.0);
		hud_align_bottomright_poly[4][1] = mid_y + (mid_y / 2.0);
		hud_align_bottomright_poly[4][2] = 0;

		if (IsPointInPolygon(HUD_ALIGN_POLYCOUNT_CORNER, hud_align_bottomright_poly, x - offset_x, y - offset_y))
		{
			hud_align_current_poly = hud_align_bottomright_poly;
			hud_align_current_polycount = HUD_ALIGN_POLYCOUNT_CORNER;
			return hud_align_bottomright;
		}
	}

	// Right.
	{
		hud_align_right_poly[0][0] = 2 * mid_x;
		hud_align_right_poly[0][1] = mid_y + (mid_y / 2.0);
		hud_align_right_poly[0][2] = 0;

		hud_align_right_poly[1][0] = 2 * mid_x;
		hud_align_right_poly[1][1] = mid_y / 2.0;
		hud_align_right_poly[1][2] = 0;

		hud_align_right_poly[2][0] = mid_x + (mid_x / 2.0);
		hud_align_right_poly[2][1] = mid_y - (mid_y / 4.0);
		hud_align_right_poly[2][2] = 0;

		hud_align_right_poly[3][0] = mid_x + (mid_x / 2.0);
		hud_align_right_poly[3][1] = mid_y + (mid_y / 4.0);
		hud_align_right_poly[3][2] = 0;

		if (IsPointInPolygon(HUD_ALIGN_POLYCOUNT_EDGE, hud_align_right_poly, x - offset_x, y - offset_y))
		{
			hud_align_current_poly = hud_align_right_poly;
			hud_align_current_polycount = HUD_ALIGN_POLYCOUNT_EDGE;
			return hud_align_right;
		}
	}

	// Center.
	{
		hud_align_center_poly[0][0] = mid_x - (mid_x / 4.0);
		hud_align_center_poly[0][1] = mid_y / 2.0;
		hud_align_center_poly[0][2] = 0;

		hud_align_center_poly[1][0] = mid_x / 2.0;
		hud_align_center_poly[1][1] = mid_y - (mid_y / 4.0);
		hud_align_center_poly[1][2] = 0;

		hud_align_center_poly[2][0] = mid_x / 2.0;
		hud_align_center_poly[2][1] = mid_y + (mid_y / 4.0);
		hud_align_center_poly[2][2] = 0;

		hud_align_center_poly[3][0] = mid_x - (mid_x / 4.0);
		hud_align_center_poly[3][1] = mid_y + (mid_y / 2.0);
		hud_align_center_poly[3][2] = 0;

		hud_align_center_poly[4][0] = mid_x + (mid_x / 4.0);
		hud_align_center_poly[4][1] = mid_y + (mid_y / 2.0);
		hud_align_center_poly[4][2] = 0;

		hud_align_center_poly[5][0] = mid_x + (mid_x / 2.0);
		hud_align_center_poly[5][1] = mid_y + (mid_y / 4.0);
		hud_align_center_poly[5][2] = 0;

		hud_align_center_poly[6][0] = mid_x + (mid_x / 2.0);
		hud_align_center_poly[6][1] = mid_y - (mid_y / 4.0);
		hud_align_center_poly[6][2] = 0;

		hud_align_center_poly[7][0] = mid_x + (mid_x / 4.0);
		hud_align_center_poly[7][1] = mid_y / 2.0;
		hud_align_center_poly[7][2] = 0;

		if (IsPointInPolygon(HUD_ALIGN_POLYCOUNT_CENTER, hud_align_center_poly, x - offset_x, y - offset_y))
		{
			hud_align_current_poly = hud_align_center_poly;
			hud_align_current_polycount = HUD_ALIGN_POLYCOUNT_CENTER;
			return hud_align_center;
		}
	}

	// Default to center.
	hud_align_current_poly = hud_align_center_poly;
	hud_align_current_polycount = HUD_ALIGN_POLYCOUNT_CENTER;
	return hud_align_center;
}

//
// Finds the next child of the specified HUD element.
//
static hud_t *HUD_Editor_FindNextChild(hud_t *hud_element)
{
	static hud_t	*hud_it = NULL;
	static hud_t	*parent = NULL;
	hud_t			*hud_result = NULL;

	// Reset everything if we're given a NULL argument.
	if(!hud_element)
	{
		hud_it = NULL;
		parent = NULL;
		return NULL;
	}

	// There's a new parent so start searching from the beginning.
	if(!parent || strcmp(parent->name, hud_element->name))
	{
		parent = hud_element;
		hud_it = hud_huds;
	}

	while(hud_it)
	{
		// Check if this HUD is placed at the parent, if so we've found a child.
		if(hud_it->place_hud && !strcmp(hud_it->place_hud->name, parent->name))
		{
			hud_result = hud_it;
			hud_it = hud_it->next;
			return hud_result;
		}

		hud_it = hud_it->next;
	}

	parent = NULL;

	// No children found.
	return NULL;
}

//
// Moves a HUD element.
//
static void HUD_Editor_Move(float dx, float dy, hud_t *hud_element)
{
	if(!hud_element)
	{
		return;
	}

	Cvar_SetValue(hud_element->pos_x, hud_element->pos_x->value + dx);
	Cvar_SetValue(hud_element->pos_y, hud_element->pos_y->value + dy);
}

//
// Draw the current alignment.
//
static void HUD_Editor_DrawAlignment(hud_t *hud_parent)
{
	extern void Draw_Polygon(int x, int y, vec3_t *vertices, int num_vertices, qbool fill, color_t color);
	color_t c = RGBA_TO_COLOR(255, 255, 0, 50);

	if(hud_parent)
	{
		//Draw_Polygon(hud_parent->lx, hud_parent->ly, hud_align_current_poly, hud_align_current_polycount, true, RGBA_TO_COLOR(255, 255, 0, 50));
		Draw_Polygon(hud_parent->lx, hud_parent->ly, hud_align_current_poly, hud_align_current_polycount, true, c);
	}
	else
	{
		//Draw_Polygon(0, 0, hud_align_current_poly, hud_align_current_polycount, true, RGBA_TO_COLOR(255, 255, 0, 50));
		Draw_Polygon(0, 0, hud_align_current_poly, hud_align_current_polycount, true, c);
	}
}

typedef struct hud_resize_handle_s
{
	int x;
	int y;
	int width;
	int height;
} hud_resize_handle_t;

#define HUD_RESIZEHANDLE_THICKNESS		5
#define HUD_RESIZEHANDLE_SIZEFACTOR		0.3
#define HUD_RESIZEHANDLE_COUNT			8
#define HUD_RESIZE_MAGICSCALE			200 // HACK : This seems to work nice though...

#define HUD_RESIZEHANDLE_NONE			-1
#define HUD_RESIZEHANDLE_TOPLEFT		0
#define HUD_RESIZEHANDLE_TOP			1
#define HUD_RESIZEHANDLE_TOPRIGHT		2
#define HUD_RESIZEHANDLE_RIGHT			3
#define HUD_RESIZEHANDLE_BOTTOMRIGHT	4
#define HUD_RESIZEHANDLE_BOTTOM			5
#define HUD_RESIZEHANDLE_BOTTOMLEFT		6
#define HUD_RESIZEHANDLE_LEFT			7

//
// Resizes the height/width of a HUD element by a delta size and alignment.
//
static void HUD_Editor_ResizeDelta(cvar_t *size, float delta_size, hud_alignmode_t alignment)
{
	if(!size)
	{
		return;
	}

	switch(alignment)
	{
		case hud_align_right :
		case hud_align_bottom :
			Cvar_SetValue(size, size->value + delta_size);
			break;
		case hud_align_left :
		case hud_align_top :
			Cvar_SetValue(size, size->value - delta_size);
			break;
		// unhandled
		case hud_align_center:
		case hud_align_topleft:
		case hud_align_bottomleft:
		case hud_align_bottomright:
		case hud_align_topright:
		case hud_align_consoleleft:
		case hud_align_consoleright:
		case hud_align_console:
			break;
	}
}

//
// Scales a HUD element.
//
static void HUD_EditorScaleDelta(cvar_t *scale, float delta_scale, hud_alignmode_t alignment)
{
	if(!scale)
	{
		return;
	}

	switch(alignment)
	{
		case hud_align_topleft :
		case hud_align_bottomleft :
			Cvar_SetValue(scale, scale->value - delta_scale);
			break;
		case hud_align_topright :
		case hud_align_bottomright :
			Cvar_SetValue(scale, scale->value + delta_scale);
			break;
		// unhandled
		case hud_align_center:
		case hud_align_top:
		case hud_align_left:
		case hud_align_bottom:
		case hud_align_right:
		case hud_align_consoleleft:
		case hud_align_consoleright:
		case hud_align_console:
			break;
	}

	if(scale->value < 0)
	{
		Cvar_SetValue(scale, 0);
	}
}

//
// Checks if the HUD element we're hovering has any dimension variables
// and handles resizing for it if it does by drawing resize handles
// that the user can click.
//
static qbool HUD_Editor_Resizing(hud_t *hud_hover)
{
	extern float mouse_x, mouse_y;		// in_win.c, delta mouse.
	int i					= 0;
	qbool found_handle		= false;	// Did we find any handle under the cursor?
	static cvar_t *width	= NULL;
	static cvar_t *height	= NULL;
	static cvar_t *scale	= NULL;
	hud_resize_handle_t *resize_handles[HUD_RESIZEHANDLE_COUNT];
	static int last_resize_handle = HUD_RESIZEHANDLE_NONE;

	// Is this mode turned on?
	if(!hud_editor_allowresize.value)
	{
		return false;
	}

	// Select a HUD if it hasn't already been done.
	if(hud_hover && hud_editor_mode == hud_editmode_move_resize
		&& !selected_hud && hud_hover)
	{
		selected_hud = hud_hover;
		return true;
	}

	memset(resize_handles, 0, sizeof(resize_handles));

	// Try getting any available dimension variables
	// that are available for this HUD element, these
	// aren't mandatory for all HUD elements, so only
	// some will have them.
	if(hud_hover)
	{
		width	= HUD_FindVar(hud_hover, "width");
		height	= HUD_FindVar(hud_hover, "height");
		scale	= HUD_FindVar(hud_hover, "scale");

		if(width)
		{
			// Right & left.
			static hud_resize_handle_t right;
			static hud_resize_handle_t left;

			right.width		= HUD_RESIZEHANDLE_THICKNESS;
			right.height	= hud_hover->lh * HUD_RESIZEHANDLE_SIZEFACTOR;
			right.x			= hud_hover->lw - right.width;
			right.y			= (hud_hover->lh - right.height) / 2;
			resize_handles[HUD_RESIZEHANDLE_RIGHT] = &right;

			left.width		= HUD_RESIZEHANDLE_THICKNESS;
			left.height		= hud_hover->lh * HUD_RESIZEHANDLE_SIZEFACTOR;
			left.x			= 0;
			left.y			= (hud_hover->lh - left.height) / 2;
			resize_handles[HUD_RESIZEHANDLE_LEFT] = &left;
		}

		if(height)
		{
			// Top & bottom
			static hud_resize_handle_t top;
			static hud_resize_handle_t bottom;

			top.width		= hud_hover->lw * HUD_RESIZEHANDLE_SIZEFACTOR;
			top.height		= HUD_RESIZEHANDLE_THICKNESS;
			top.x			= (hud_hover->lw - top.width) / 2;
			top.y			= 0;
			resize_handles[HUD_RESIZEHANDLE_TOP] = &top;

			bottom.width		= hud_hover->lw * HUD_RESIZEHANDLE_SIZEFACTOR;
			bottom.height		= HUD_RESIZEHANDLE_THICKNESS;
			bottom.x			= (hud_hover->lw - bottom.width) / 2;
			bottom.y			= hud_hover->lh - bottom.height;
			resize_handles[HUD_RESIZEHANDLE_BOTTOM] = &bottom;
		}

		if(scale)
		{
			// Top left, top right, bottom left & bottom right.
			static hud_resize_handle_t topleft;
			static hud_resize_handle_t bottomleft;
			static hud_resize_handle_t topright;
			static hud_resize_handle_t bottomright;

			topleft.width		= HUD_RESIZEHANDLE_THICKNESS;
			topleft.height		= HUD_RESIZEHANDLE_THICKNESS;
			topleft.x			= 0;
			topleft.y			= 0;
			resize_handles[HUD_RESIZEHANDLE_TOPLEFT] = &topleft;

			bottomleft.width	= HUD_RESIZEHANDLE_THICKNESS;
			bottomleft.height	= HUD_RESIZEHANDLE_THICKNESS;
			bottomleft.x		= 0;
			bottomleft.y		= hud_hover->lh - bottomleft.height;
			resize_handles[HUD_RESIZEHANDLE_BOTTOMLEFT] = &bottomleft;

			topright.width		= HUD_RESIZEHANDLE_THICKNESS;
			topright.height		= HUD_RESIZEHANDLE_THICKNESS;
			topright.x			= hud_hover->lw - topright.width;
			topright.y			= 0;
			resize_handles[HUD_RESIZEHANDLE_TOPRIGHT] = &topright;

			bottomright.width	= HUD_RESIZEHANDLE_THICKNESS;
			bottomright.height	= HUD_RESIZEHANDLE_THICKNESS;
			bottomright.x		= hud_hover->lw - bottomright.width;
			bottomright.y		= hud_hover->lh - bottomright.height;
			resize_handles[HUD_RESIZEHANDLE_BOTTOMRIGHT] = &bottomright;
		}
	}

	for(i = 0; i < HUD_RESIZEHANDLE_COUNT; i++)
	{
		// Non existant for this HUD element.
		if(!resize_handles[i])
		{
			continue;
		}

		if(hud_editor_mode == hud_editmode_move_resize)
		{
			// We're in resize mode, so check if we're clicking any of the
			// resize handles.

			if((selected_hud && (last_resize_handle == i)) ||
				  (hud_mouse_x >= (selected_hud->lx + resize_handles[i]->x)
				&& (hud_mouse_x <= (selected_hud->lx + resize_handles[i]->x + resize_handles[i]->width))
				&& (hud_mouse_y >= (selected_hud->ly + resize_handles[i]->y))
				&& (hud_mouse_y <= (selected_hud->ly + resize_handles[i]->y + resize_handles[i]->height))))
			{
				// Keep track of which resize handle we're grabbing
				// so that it doesn't matter if the mouse "slips" outside
				// it's bounding box as long as the mouse is still pressed.
				if(last_resize_handle < 0 || last_resize_handle != i)
				{
					last_resize_handle = i;
				}

				found_handle = true;

				// Draw the resize handle highlighted.
				Draw_AlphaFillRGB(
					selected_hud->lx + resize_handles[last_resize_handle]->x,
					selected_hud->ly + resize_handles[last_resize_handle]->y,
					resize_handles[last_resize_handle]->width,
					resize_handles[last_resize_handle]->height,
					RGBA_TO_COLOR(255, 255, 0, 125));

				// Check which resize handle that has been selected
				// and resize the HUD element accordingly.
				switch(i)
				{
					case HUD_RESIZEHANDLE_RIGHT :
						HUD_Editor_ResizeDelta(width, mouse_x, hud_align_right);
						break;
					case HUD_RESIZEHANDLE_LEFT :
						HUD_Editor_ResizeDelta(width, mouse_x, hud_align_left);
						break;
					case HUD_RESIZEHANDLE_TOP :
						HUD_Editor_ResizeDelta(height, mouse_y, hud_align_top);
						break;
					case HUD_RESIZEHANDLE_BOTTOM :
						HUD_Editor_ResizeDelta(height, mouse_y, hud_align_bottom);
						break;
					case HUD_RESIZEHANDLE_TOPRIGHT :
						// HACK : Dividing by 200 seems to work fine, but this could probably be done a lot nicer.
						HUD_EditorScaleDelta(scale, (mouse_x - mouse_y) / HUD_RESIZE_MAGICSCALE, hud_align_topright);
						break;
					case HUD_RESIZEHANDLE_BOTTOMRIGHT :
						HUD_EditorScaleDelta(scale, (mouse_x + mouse_y) / HUD_RESIZE_MAGICSCALE, hud_align_bottomright);
						break;
					case HUD_RESIZEHANDLE_TOPLEFT :
						HUD_EditorScaleDelta(scale, (mouse_x + mouse_y) / HUD_RESIZE_MAGICSCALE, hud_align_topleft);
						break;
					case HUD_RESIZEHANDLE_BOTTOMLEFT :
						HUD_EditorScaleDelta(scale, (mouse_x - mouse_y) / HUD_RESIZE_MAGICSCALE, hud_align_bottomleft);
						break;
				}

				// Recalculate all HUD elements.
				HUD_Recalculate();
				return true;
			}
		}
		else if(hud_editor_prevmode == hud_editmode_move_resize)
		{
			// We left resize mode.
			selected_hud = NULL;
			last_resize_handle = HUD_RESIZEHANDLE_NONE;
		}
		else if(hud_hover)
		{
			// If we're hovering a HUD, always draw all it's resize handles.
			if((hud_mouse_x >= (hud_hover->lx + resize_handles[i]->x)
				&& hud_mouse_x <= (hud_hover->lx + resize_handles[i]->x + resize_handles[i]->width)
				&& hud_mouse_y >= (hud_hover->ly + resize_handles[i]->y)
				&& hud_mouse_y <= (hud_hover->ly + resize_handles[i]->y + resize_handles[i]->height)))
			{
				// Highlight the resize handle under the cursor.
				Draw_AlphaFillRGB(hud_hover->lx + resize_handles[i]->x, hud_hover->ly + resize_handles[i]->y,
					resize_handles[i]->width, resize_handles[i]->height,
					RGBA_TO_COLOR(255, 255, 0, 125));

				// Set the resize cursor icon since we're hovering a resize handle.
				scr_cursor_icon = hud_editor_resize_icon;
				found_handle = true;
			}
			else
			{
				// Normal resize handle.
				Draw_AlphaFillRGB(hud_hover->lx + resize_handles[i]->x, hud_hover->ly + resize_handles[i]->y,
				resize_handles[i]->width, resize_handles[i]->height,
				RGBA_TO_COLOR(255, 255, 0, 50));
			}
		}
	}

	// We didn't hover any resize handle so show no icon.
	if(!found_handle)
	{
		scr_cursor_icon = NULL;
	}

	// We didn't perform any action, so let others try.
	return false;
}

static qbool hud_editor_locked_axis_is_x = true;	// Are we locking X- or Y-axis movement?
static qbool hud_editor_lockaxis_found= false;		// Have we found what axist to lock on to?
static qbool hud_editor_finding_lockaxis = false;	// Are we in the process of finding the lock axis?

//
// Check if we're supposed to be moving anything.
//
static qbool HUD_Editor_Moving(hud_t *hud_hover)
{
	// Mouse delta (in_win.c)
	extern float mouse_x, mouse_y;

	// Is this mode allowed?
	if(!hud_editor_allowmove.value)
	{
		return false;
	}

	// Set the move cursor icon if we're hovering a HUD element
	// unless it's not already set (resize may have set it if
	// a resize handle is being hovered).
	if(hud_hover && !scr_cursor_icon)
	{
		scr_cursor_icon = hud_editor_move_icon;
	}
	else if(!hud_hover)
	{
		// Not hovering anything so show no cursor.
		scr_cursor_icon = NULL;
	}

	// Left mousebutton down = lets move it !
	if (hud_editor_mode == hud_editmode_move_resize || hud_editor_mode == hud_editmode_move_lockedaxis)
	{
		// If we just entered movement mode, nothing is selected
		// so select the hud we're hovering to start with.
		if(!selected_hud && hud_hover)
		{
			selected_hud = hud_hover;
			return true;
		}

		if(selected_hud)
		{
			// Move using the mouse delta instead of the absolute
			// mouse cursor coordinates on the screen.

			// Lock the movement to the axis that the user starts
			// dragging the HUD element along if we're in locked-axis mode.
			if(hud_editor_mode == hud_editmode_move_lockedaxis)
			{
				// We haven't found the axis.
				if(!hud_editor_lockaxis_found)
				{
					static float last_mouse_x = 0.0;
					static float last_mouse_y = 0.0;

					if(!hud_editor_finding_lockaxis)
					{
						// Start trying to find the lock axis.
						hud_editor_finding_lockaxis = true;
						last_mouse_x = 0;
						last_mouse_y = 0;
					}
					else
					{
						// Get the delta of the mouse movement from when the
						// user clicked the mouse button and started dragging.
						float mouse_delta_x = fabs(last_mouse_x - mouse_x);
						float mouse_delta_y = fabs(last_mouse_y - mouse_y);

						// Increment the mouse movement since last frame.
						last_mouse_x += mouse_x;
						last_mouse_y += mouse_y;

						// If the mouse has moved more than 5 pixels in any
						// direction we decide to lock onto that axis.
						if(mouse_delta_x > 5 || mouse_delta_y > 5)
						{
							hud_editor_locked_axis_is_x = (mouse_delta_x > mouse_delta_y);
							hud_editor_lockaxis_found = true;
						}
					}
				}

				// Move while locked to the axis.
				if(hud_editor_locked_axis_is_x)
				{
					// Move only along X-axis.
					HUD_Editor_Move(mouse_x, 0, hud_hover);
				}
				else
				{
					// Move only along Y-axis.
					HUD_Editor_Move(0, mouse_y, hud_hover);
				}
			}
			else
			{
				// Ordinary move.
				HUD_Editor_Move(mouse_x, mouse_y, hud_hover);
			}

			HUD_Recalculate();
			return true;
		}
	}
	else if((hud_editor_prevmode == hud_editmode_move_resize && hud_editor_mode != hud_editmode_move_lockedaxis)
		 || (hud_editor_prevmode == hud_editmode_move_lockedaxis && hud_editor_mode != hud_editmode_move_resize))
	{
		// Only deselect if we're going from a move mode to a non-move mode (Such as align/place).

		// We've left move mode, so deselect.
		selected_hud = NULL;
		hud_editor_lockaxis_found = false;
		hud_editor_finding_lockaxis = false;
	}

	// We haven't handled the input.
	return false;
}

//
// Handles input from mouse if in alignment mode.
//
static qbool HUD_Editor_Aligning(hud_t *hud_hover)
{
	// Is this mode allowed?
	if(!hud_editor_allowalign.value)
	{
		return false;
	}

	if(hud_editor_mode == hud_editmode_align)
	{
		// If we just entered alignment mode, nothing is selected
		// so select the hud we're hovering to start with.
		if(!selected_hud && hud_hover)
		{
			selected_hud = hud_hover;
			return true;
		}

		// Set the align icon for the cursor.
		scr_cursor_icon = hud_editor_align_icon;

		// We have something selected so show some visual
		// feedback for when aligning to the user.
		if(selected_hud)
		{
			if(selected_hud->place_hud)
			{
				// Placed at another HUD, so align onto that.
				hud_alignmode = HUD_Editor_GetAlignment(hud_mouse_x, hud_mouse_y, selected_hud->place_hud);
				HUD_Editor_DrawAlignment(selected_hud->place_hud);
			}
			else
			{
				// Placed on the screen.
				hud_alignmode = HUD_Editor_GetAlignment(hud_mouse_x, hud_mouse_y, NULL);
				HUD_Editor_DrawAlignment(NULL);
			}
		}
	}
	else if(hud_editor_prevmode == hud_editmode_align && keydown[K_ALT])
	{
		// The user just released the mousebutton but is still holding
		// down ALT. If the user releases ALT before the mouse button
		// the operation will be cancelled. So commit the users actions.

		// We must have something to align.
		if(selected_hud)
		{
			// Reset position.
			Cvar_Set(selected_hud->pos_x, "0");
			Cvar_Set(selected_hud->pos_y, "0");

			// Align to the area the mouse is placed over (previously set in hud_alignmode).
			Cbuf_AddText(va("align %s %s\n", selected_hud->name, HUD_Editor_GetAlignmentString(hud_alignmode)));
			HUD_Recalculate();

			// Free selection.
			selected_hud = NULL;
			return true;
		}
	}

	return false;
}

//
// Handles feedback/commiting of actions when in placement mode.
//
static qbool HUD_Editor_Placing(hud_t *hud_hover)
{
	// Is this mode allowed?
	if(!hud_editor_allowplace.value)
	{
		return false;
	}

	// Show the place icon at the cursor if ctrl is pressed
	// while hovering a HUD element.
	if(hud_hover && keydown[K_CTRL])
	{
		// Set the place cursor icon.
		scr_cursor_icon = hud_editor_place_icon;
	}
	else if(!hud_hover)
	{
		scr_cursor_icon = NULL;
	}

	if(hud_editor_mode == hud_editmode_place)
	{
		// If we just entered placement mode, nothing is selected
		// so select the hud we're hovering to start with.
		if(!selected_hud && hud_hover)
		{
			selected_hud = hud_hover;
			return true;
		}

		// Set the place cursor icon.
		scr_cursor_icon = hud_editor_place_icon;

		if(selected_hud)
		{
			// Find the center of the selected HUD so we know where
			// to draw the line from.

			if(hud_hover)
			{
				// We're trying to place the HUD on itself or on the HUD it's already placed at.
				if(hud_hover == selected_hud || (selected_hud->place_hud && selected_hud->place_hud == hud_hover))
				{
					// Red "not allowed".
					Draw_AlphaRectangleRGB(hud_hover->lx, hud_hover->ly, hud_hover->lw, hud_hover->lh, 1, true, RGBA_TO_COLOR(255, 0, 0, 25));
					Draw_AlphaRectangleRGB(hud_hover->lx, hud_hover->ly, hud_hover->lw, hud_hover->lh, 1, false, RGBA_TO_COLOR(255, 0, 0, 255));
				}
				else
				{
					// Green "allowed" placement.
					Draw_AlphaRectangleRGB(hud_hover->lx, hud_hover->ly, hud_hover->lw, hud_hover->lh, 1, true, RGBA_TO_COLOR(0, 255, 0, 25));
				}

				return true;
			}

			// Placement at the screen (We don't care about stuff like IFREE, HBAR, SBAR and so on).
			if(!strcmp(selected_hud->place->string, "screen"))
			{
				// Not allowed, already placed there.
				Draw_AlphaRectangleRGB(0, 0, vid.width, vid.height, 1, true, RGBA_TO_COLOR(255, 0, 0, 25));
			}
			else
			{
				// Allowed.
				Draw_AlphaRectangleRGB(0, 0, vid.width, vid.height, 1, true, RGBA_TO_COLOR(0, 255, 0, 25));
			}
		}
	}
	else if(hud_editor_prevmode == hud_editmode_place && keydown[K_CTRL])
	{
		// We've just exited placement mode, but control is still pressed,
		// that means we should place the selected_hud.
		// (If you release ctrl before you release Mouse 1, you cancel the place operation).

		if(selected_hud)
		{
			// If we're hovering a HUD place it there.
			if(hud_hover)
			{
				// We're trying to place the HUD on itself or on the HUD it's already placed at so do nothing.
				if(hud_hover == selected_hud || (selected_hud->place_hud && selected_hud->place_hud == hud_hover))
				{
					return true;
				}

				// Place at other HUD.
				Cvar_Set(selected_hud->align_x, "center");
				Cvar_Set(selected_hud->align_y, "center");
				Cvar_Set(selected_hud->pos_x, "0");
				Cvar_Set(selected_hud->pos_y, "0");
				Cvar_Set(selected_hud->place, hud_hover->name);

				// Make sure the child has a higher order.
				if((int)selected_hud->order->value <= (int)hud_hover->order->value)
				{
					Cvar_SetValue(selected_hud->order, hud_hover->order->value + 1);
				}

				HUD_Recalculate();

				// Free selection.
				selected_hud = NULL;
				return true;
			}
			else
			{
				// Mouse button was released on on the "screen".

				if(!strcmp(selected_hud->place->string, "screen"))
				{
					return false;
				}

				// Place at other HUD.
				Cvar_Set(selected_hud->align_x, "center");
				Cvar_Set(selected_hud->align_y, "center");
				Cvar_Set(selected_hud->pos_x, "0");
				Cvar_Set(selected_hud->pos_y, "0");
				Cvar_Set(selected_hud->place, "screen");
				HUD_Recalculate();

				// Free selection.
				selected_hud = NULL;
				return true;
			}
		}
	}

	// We weren't placing something so check other states also.
	return false;
}

// =============================================================================
//							HUD Editor Grep Handles.
// =============================================================================
// These show up when a HUD element is moved completly offscreen (by accident
// most likely), so that the user can still grab it and move it back onto the
// screen again.
// =============================================================================

//
// Finds a HUD grephandle (if the hud element has gone offscreen).
//
static hud_grephandle_t *HUD_Editor_FindGrep(hud_t *hud_element)
{
	hud_grephandle_t *greps_it = NULL;
	greps_it = hud_greps;

	while(greps_it)
	{
		if(!strcmp(greps_it->hud->name, hud_element->name))
		{
			return greps_it;
		}

		greps_it = greps_it->next;
	}

	return NULL;
}

//
// Returns an "offscreen" arrow in the correct direction based on where
// offscreen the HUD element is located.
//
static char *HUD_Editor_GetGrepArrow(hud_grephandle_t *grep)
{
	switch(grep->pos)
	{
		case pos_top :		return "/\\";
		case pos_bottom :	return "\\/";
		case pos_left :		return "<<<";
		case pos_right :	return ">>>";
		default :			return "";
	}
}

//
// Draws the grephandles.
//
static void HUD_Editor_DrawGreps()
{
	clrinfo_t color, highlight;
	hud_grephandle_t *greps_it = NULL;
	greps_it = hud_greps;

	// Highlight color (yellow).
	highlight.c = RGBA_TO_COLOR(0, 255, 0, 255);
	highlight.i = 0;

	// Orange.
	color.c = RGBA_TO_COLOR(255, 150, 0, 255);
	color.i = 0;

	while(greps_it)
	{
		Draw_ColoredString3(greps_it->x, greps_it->y,
			va("%s %s", HUD_Editor_GetGrepArrow(greps_it), greps_it->hud->name),
			(greps_it->highlighted ? &highlight : &color), 1, 0);

		greps_it = greps_it->next;
	}
}

//
// Get's the position offscreen for a HUD element
// left/right/top/bottom or visible if it's not offscreen.
//
static hud_greppos_t HUD_Editor_GetHudGrepPosition(hud_t *hud)
{
	if(hud->lx + hud->lw <= 0)
	{
		return pos_left;
	}
	else if(hud->lx >= (signed)vid.width)
	{
		return pos_right;
	}
	else if(hud->ly + hud->lh <= 0)
	{
		return pos_top;
	}
	else if(hud->ly >= (signed)vid.height)
	{
		return pos_bottom;
	}

	return pos_visible;
}

//
// Positions a grephandle based on it's position and where the HUD element
// it's associated with is located.
//
static void HUD_Editor_PositionGrep(hud_t *hud_element, hud_grephandle_t *grep)
{
	// Get the position of the grephandle.
	grep->pos = HUD_Editor_GetHudGrepPosition(hud_element);

	// Position the grephandle on screen.
	switch(grep->pos)
	{
		case pos_top :
			grep->x	= hud_element->lx;
			grep->y	= 5;
			break;
		case pos_bottom :
			grep->x	= hud_element->lx;
			grep->y	= vid.height - grep->height - 5;
			break;
		case pos_left :
			grep->x = 5;
			grep->y = hud_element->ly;
			break;
		case pos_right :
			grep->x = vid.width - grep->width - 5;
			grep->y = hud_element->ly;
			break;
		default :
			grep->x = hud_element->lx;
			grep->y = hud_element->ly;
			break;
	}
}

//
// Creates a new grephandle and associates it with a HUD element that is offscreen.
//
static hud_grephandle_t *HUD_Editor_CreateGrep(hud_t *hud_element)
{
	hud_grephandle_t *grep = NULL;

	grep			= Q_malloc(sizeof(hud_grephandle_t));
	memset(grep, 0, sizeof(*grep));

	grep->width		= 8 * (4 + strlen(hud_element->name));
	grep->height	= 8;
	grep->hud		= hud_element;
	grep->previous	= NULL;
	grep->next		= hud_greps;
	if(hud_greps)
	{
		hud_greps->previous = grep;
	}

	hud_greps = grep;

	HUD_Editor_PositionGrep(hud_element, grep);

	return grep;
}

//
// Destroys a grephandle (called if it's no longer offscreen).
//
static void HUD_Editor_DestroyGrep(hud_grephandle_t *grep)
{
	// Already destroyed.
	if(!grep)
	{
		return;
	}

	// Relink any neighbours in the list.
	if(grep->next && grep->previous)
	{
		grep->previous->next = grep->next;
		grep->next->previous = grep->previous;
	}
	else if(grep->next)
	{
		grep->next->previous = NULL;
		hud_greps = grep->next;
	}
	else if(grep->previous)
	{
		grep->previous->next = NULL;
	}
	else
	{
		hud_greps = NULL;
	}

	memset(grep, 0, sizeof(*grep));

	Q_free(grep);
}

//
// Finds a HUD element associated with the grephandle under the mouse cursor.
//
static hud_t *HUD_Editor_FindHudByGrep()
{
	hud_grephandle_t *greps_it = NULL;
	greps_it = hud_greps;

	while(greps_it)
	{
		if (greps_it->hud
			&& hud_mouse_x >= greps_it->x
			&& hud_mouse_x <= (greps_it->x + greps_it->width)
			&& hud_mouse_y >= greps_it->y
			&& hud_mouse_y <= (greps_it->y + greps_it->height))
		{
			greps_it->highlighted = true;
			return greps_it->hud;
		}

		greps_it->highlighted = false;

		greps_it = greps_it->next;
	}

	return NULL;
}

//
// Draws the outline of the visible HUD elements.
//
static void HUD_Editor_DrawOutlines(void)
{
	hud_t *temp_hud	= hud_huds;

	if(!temp_hud || !hud_editor_showoutlines)
	{
		return;
	}

	while(temp_hud->next)
	{
		// Check if the item is visible.
		if (!temp_hud->show->value || (temp_hud->place_hud && !temp_hud->place_hud->show->value))
		{
			temp_hud = temp_hud->next;
			continue;
		}

		// Draw an outline for all hud elements (faint).
		Draw_AlphaRectangleRGB(temp_hud->lx, temp_hud->ly, temp_hud->lw, temp_hud->lh, 1, false, RGBA_TO_COLOR(0, 255, 0, 25));

		temp_hud = temp_hud->next;
	}
}

//
// Finds if there's any HUD under the cursor and draws outlines for all HUD elements.
//
static qbool HUD_Editor_FindHudUnderCursor(hud_t **hud)
{
	hud_grephandle_t	*grep		= NULL;
	hud_greppos_t		pos			= pos_visible;
	hud_t				*temp_hud	= hud_huds;
	qbool				found		= false;

	if(!temp_hud)
	{
		return false;
	}

	// Check if we already had something selected since last time and was moving.
	if(selected_hud && (hud_editor_mode == hud_editmode_move_resize || hud_editor_mode == hud_editmode_move_lockedaxis))
	{
		found = true;
		(*hud) = selected_hud;
	}

	// If the hover list is being showed, only look for HUDs in that
	// not the HUD's that are below the cursor.
	if(hud_editor_mode == hud_editmode_hoverlist && hud_hoverlist_count > 0)
	{
		(*hud) = HUD_Editor_FindHoverListSelection(hud_hoverlist);
		return (hud != NULL);
	}

	// Reset the hoverlist since we might be hovering something new.
	hud_hoverlist_count = 0;
	hud_hoverlist = NULL;

	while(temp_hud)
	{
		// Not visible.
		if (!temp_hud->show->value || (temp_hud->place_hud && !temp_hud->place_hud->show->value))
		{
			temp_hud = temp_hud->next;
			continue;
		}

		// We found one.
		if (hud_mouse_x >= temp_hud->lx
			&& hud_mouse_x <= (temp_hud->lx + temp_hud->lw)
			&& hud_mouse_y >= temp_hud->ly
			&& hud_mouse_y <= (temp_hud->ly + temp_hud->lh))
		{
			// If we're moving/resizing something only continue checking for
			// more HUD elements we have the mouse over if we haven't already
			// found one. If we don't do this when you drag a HUD element
			// over another HUD element that has a greater Z-order the selection
			// will jump to that HUD element, and you'll start moving that instead.
			//
			// Vice versa if we would skip any HUD item after we've found one
			// when not already moving an item, it would mean that we could only
			// select HUD elements that are topmost in the Z-order, so an item
			// placed within another item would not be selectable.
			if(((hud_editor_mode == hud_editmode_move_resize || hud_editor_mode == hud_editmode_move_lockedaxis) && !found)
				|| (hud_editor_mode != hud_editmode_move_resize && hud_editor_mode != hud_editmode_move_lockedaxis))
			{
				found = true;
				(*hud) = temp_hud;

				// Add this HUD to the list of HUDs under the cursor.
				HUD_Editor_AddHoverHud(HUD_Editor_CreateHoverHud(temp_hud));
			}
		}

		// Check if the hud element is offscreen and
		// if there's any grep handle for this hud element
		{
			pos = HUD_Editor_GetHudGrepPosition(temp_hud);
			grep = HUD_Editor_FindGrep(temp_hud);

			if(pos != pos_visible)
			{
				// We didn't find any grep handle so create one.
				if(!grep)
				{
					grep = HUD_Editor_CreateGrep(temp_hud);
				}

				// Position the grep if we got one.
				if(grep)
				{
					HUD_Editor_PositionGrep(temp_hud, grep);
				}
			}
			else
			{
				// The HUD element is visbile, so no need for a grep handle for it.
				HUD_Editor_DestroyGrep(grep);
			}
		}

		temp_hud = temp_hud->next;
	}

	// We didn't find any HUD's under the cursor, but
	// what about "grep handles" (for offscreen HUDs).
	if(!found)
	{
		temp_hud = HUD_Editor_FindHudByGrep();

		if(temp_hud)
		{
			found = true;
			(*hud) = temp_hud;
		}
	}

	// Nothing found, make sure result is NULL.
	if (!found)
	{
		(*hud) = NULL;
	}

	return found;
}

//
// Returns the point a HUD is aligned to on it's parent in screen coordinates.
//
static void HUD_Editor_GetAlignmentPoint(hud_t *hud, int *x, int *y)
{
	extern float scr_con_current; // Console height. console.c
	int parent_x = 0;
	int parent_y = 0;
	int parent_w = 0;
	int parent_h = 0;
	hud_alignmode_t alignmode = HUD_Editor_GetAlignmentFromString(va("%s %s", hud->align_x->string, hud->align_y->string));

	if(hud->place_hud)
	{
		// Placed at another HUD.
		parent_x = hud->place_hud->lx;
		parent_y = hud->place_hud->ly;
		parent_w = hud->place_hud->lw;
		parent_h = hud->place_hud->lh;

		(*x) = HUD_CENTER_X(hud->place_hud);
		(*y) = HUD_CENTER_Y(hud->place_hud);
	}
	else
	{
		// Placed at "screen".
		parent_x = 0;
		parent_y = 0;
		parent_w = vid.width;
		parent_h = vid.height;

		(*x) = vid.width / 2;
		(*y) = vid.height / 2;
	}

	switch(alignmode)
	{
		default:
		case hud_align_center:
			// Already set.
			break;
		case hud_align_right:
			(*x) = parent_x + parent_w;
			(*y) = parent_y + (parent_h / 2);
			break;
		case hud_align_topright:
			(*x) = parent_x + parent_w;
			(*y) = parent_y;
			break;
		case hud_align_top:
			(*x) = parent_x + (parent_w / 2);
			(*y) = parent_y;
			break;
		case hud_align_topleft:
			(*x) = parent_x;
			(*y) = parent_y;
			break;
		case hud_align_left:
			(*x) = parent_x;
			(*y) = parent_y + (parent_h / 2);
			break;
		case hud_align_bottomleft:
			(*x) = parent_x;
			(*y) = parent_y + parent_h;
			break;
		case hud_align_bottom:
			(*x) = parent_x + (parent_w / 2);
			(*y) = parent_y + parent_h;
			break;
		case hud_align_bottomright:
			(*x) = parent_x + parent_w;
			(*y) = parent_y + parent_h;
			break;
		case hud_align_consoleleft:
			(*x) = parent_x;
			(*y) = scr_con_current;
			break;
		case hud_align_console:
			(*x) = parent_x + (parent_w / 2);
			(*y) = scr_con_current;
			break;
		case hud_align_consoleright:
			(*x) = parent_x + parent_w;
			(*y) = scr_con_current;
			break;
	}
}

//
// Draws a green line to each corner of a HUD element from a specified point.
//
/*
static void HUD_Editor_DrawLinesToEachCorner(hud_t *hud, int x, int y)
{
	Draw_AlphaLineRGB(hud->lx, hud->ly,						x, y, 1, RGBA_TO_COLOR(0, 255, 0, 25));
	Draw_AlphaLineRGB(hud->lx, hud->ly + hud->lh,			x, y, 1, RGBA_TO_COLOR(0, 255, 0, 25));
	Draw_AlphaLineRGB(hud->lx + hud->lw, hud->ly,			x, y, 1, RGBA_TO_COLOR(0, 255, 0, 25));
	Draw_AlphaLineRGB(hud->lx + hud->lw, hud->ly + hud->lh, x, y, 1, RGBA_TO_COLOR(0, 255, 0, 25));
}
*/

//
// Draws connections to/from a HUD element.
//
static void HUD_Editor_DrawConnections(hud_t *hud_hover)
{
	hud_t *child = NULL;
	int align_x = 0.0;
	int align_y = 0.0;

	if (!hud_hover || !hud_editor_showoutlines)
	{
		return;
	}

	// Get the alignment point at the parent in screen coordinates.
	HUD_Editor_GetAlignmentPoint(hud_hover, &align_x, &align_y);

	// Draw a line to the parent of the HUD we're hovering.
	Draw_AlphaLineRGB(HUD_CENTER_X(hud_hover), HUD_CENTER_Y(hud_hover), align_x, align_y, 1, RGBA_TO_COLOR(0, 255, 0, 25));

	// Draw a line to all children of the HUD we're hovering.
	while((child = HUD_Editor_FindNextChild(hud_hover)))
	{
		// Don't bother with hidden children.
		if(!child->show->value)
		{
			continue;
		}

		// Draw a line from the alignment point on the parent to the child.
		HUD_Editor_GetAlignmentPoint(child, &align_x, &align_y);
		Draw_AlphaLineRGB(align_x, align_y, HUD_CENTER_X(child), HUD_CENTER_Y(child), 1, RGBA_TO_COLOR(0, 255, 0, 25));
	}
}

//
// Evaluates the current mouse/keyboard state and sets the appropriate mode.
//
static void HUD_Editor_EvaluateState(hud_t *hud_hover)
{
	// Mouse 1			= Move + Resize
	// Mouse 2			= Toggle menu
	// Ctrl  + Mouse 1	= Place
	// Alt	 + Mouse 1	= Align
	// Shift + Mouse 1	= Lock moving to one axis (If you start dragging along x-axis, it will stick to that)

	if (MOUSEDOWN)
	{
		// Turn of help on mouse click.
		hud_editor_showhelp = false;
	}

	if (hud_editor_mode == hud_editmode_hoverlist)
	{
		if (!MOUSEDOWN)
		{
			// Stay in hoverlist mode until the user clicks something.
			return;
		}
	}

	if (hud_hover && MOUSEDOWN_1_ONLY && keydown[K_SHIFT])
	{
		// Move (Locked to an axis).
		HUD_Editor_SetMode(hud_editmode_move_lockedaxis);
	}
	else if ((hud_hover || hud_editor_prevmode == hud_editmode_place) && MOUSEDOWN_1_ONLY && keydown[K_CTRL])
	{
		// Place.
		HUD_Editor_SetMode(hud_editmode_place);
	}
	else if ((hud_hover || hud_editor_prevmode == hud_editmode_align) && MOUSEDOWN_1_ONLY && keydown[K_ALT])
	{
		// Align.
		HUD_Editor_SetMode(hud_editmode_align);
	}
	else if ((hud_hover || selected_hud) && MOUSEDOWN_1_ONLY)
	{
		// Move + resize.
		HUD_Editor_SetMode(hud_editmode_move_resize);
	}
	else if (hud_hoverlist_count > 1 && MOUSEDOWN_2_ONLY)
	{
		// Hover list for when hovering more than one HUD.
		HUD_Editor_SetMode(hud_editmode_hoverlist);
	}
	else if (hud_hover && MOUSEDOWN_2_ONLY)
	{
		// HUD element menu for the HUD element we have the mouse over.
		HUD_Editor_SetMode(hud_editmode_hudmenu);
	}
	else if (MOUSEDOWN_2_ONLY)
	{
		// Main menu for adding HUDs if we right click non-occupied space.
		HUD_Editor_SetMode(hud_editmode_menu);
	}
	else
	{
		// Nothing special happening.
		HUD_Editor_SetMode(hud_editmode_normal);
	}
}

//
// Draws the tooltips for a HUD element based on the state we're in.
//
static void HUD_Editor_DrawTooltips(hud_t *hud_hover)
{
	char *message = NULL;
	byte color[4] = {0, 0, 0, 0};

	if(!hud_hover)
	{
		return;
	}

	if (selected_hud)
	{
		switch(hud_editor_mode)
		{
			case hud_editmode_move_lockedaxis :
			case hud_editmode_move_resize :
			{
				message = va("(%d, %d) moving %s", (int)selected_hud->pos_x->value, (int)selected_hud->pos_y->value, selected_hud->name);
				color[0] = 255;
				color[3] = 125;
				break;
			}
			case hud_editmode_align :
			{
				char *align = NULL;

				align = HUD_Editor_GetAlignmentString(hud_alignmode);

				message = va("align %s to %s", selected_hud->name, align);
				color[1] = 255;
				color[2] = 255;
				color[3] = 125;

				break;
			}
			case hud_editmode_place :
			{
				message = va("placing %s", selected_hud->name);
				color[0] = 255;
				color[3] = 125;
				break;
			}
			case hud_editmode_normal :
			{
				message = hud_hover->name;
				color[2] = 255;
				color[3] = 125;
				break;
			}
			// unhandled
			case hud_editmode_off:
			case hud_editmode_resize:
			case hud_editmode_hudmenu:
			case hud_editmode_menu:
			case hud_editmode_hoverlist:
				break;
		}
	}

	if(!message)
	{
		message = hud_hover->name;
		color[2] = 255;
		color[3] = 125;
	}

	HUD_Editor_DrawTooltip(hud_mouse_x, hud_mouse_y, message, RGBA_TO_COLOR(color[0], color[1], color[2], color[3]));
}

//
// Draws a help window.
//
static void HUD_Editor_DrawHelp()
{
	#define HUD_EDITOR_HELP_BORDER	32
	#define HUD_EDITOR_HELP_WIDTH	min(vid.conwidth - (2 * HUD_EDITOR_HELP_BORDER), 500)
	#define HUD_EDITOR_HELP_HEIGHT	(vid.conheight - (2 * HUD_EDITOR_HELP_BORDER))
	#define HUD_EDITOR_HELP_X		((vid.conwidth - HUD_EDITOR_HELP_WIDTH) / 2)
	#define HUD_EDITOR_HELP_Y		HUD_EDITOR_HELP_BORDER
	#define HUD_EDITOR_HELP_TITLE	"&cfd0HUD EDITOR HELP"

	Draw_TextBox(HUD_EDITOR_HELP_X, HUD_EDITOR_HELP_Y, HUD_EDITOR_HELP_WIDTH / 8, HUD_EDITOR_HELP_HEIGHT / 8);

	Draw_ColoredString(
		HUD_EDITOR_HELP_X + ((HUD_EDITOR_HELP_WIDTH - strlen(HUD_EDITOR_HELP_TITLE) * 8) / 2),
		HUD_EDITOR_HELP_Y + 10,
		HUD_EDITOR_HELP_TITLE, 1);

	UI_PrintTextBlock(
		HUD_EDITOR_HELP_X + 10,
		HUD_EDITOR_HELP_Y + 30,
		HUD_EDITOR_HELP_WIDTH,
		HUD_EDITOR_HELP_HEIGHT - 30,
		"The HUD Editor helps you to customize your Heads Up Display. "
		"When you move the cursor over a HUD element it will be "
		"highlighted and it's name will be shown. When hovering a HUD "
		"you can perform the following actions:\n"
		"\n"
		"&cfd0MOVE&r relative to the HUD elements parent/alignment by "
		"holding down &c0dfMOUSE 1&r and &c0dfdragging&r.\n"
		"(Lock movement to a specific axis by holding down &c0dfSHIFT&r).\n"
		"\n"
		"&cfd0RESIZE&r the HUD element by clicking on one of the "
		"&c0dfresize handles&r that appears when hovering and item and "
		"&c0dfdragging&r. (Not all HUD elements are resizeable/scaleable).\n"
		"\n"
		"&cfd0PLACE&r the HUD element at another HUD element or a location "
		"such as the screen/console by holding down &c0dfCTRL&r when "
		"dragging. The target that the element will be placed in "
		"will turn green if you can place it there, red otherwise.\n"
		"\n"
		"&cfd0ALIGN&r the HUD element in different ways to it's parent by "
		"holding down &c0dfALT&r when dragging. Doing this will show "
		"yellow highlights at the position you're about to align to.\n"
		"\n"
		"  &cfd0Keyboard shortcuts:\n"
		"  &c0dfP&r      Toggle HUD planmode on/off (default on).\n"
		"  &c0dfH&r      Toggle this help.\n"
		"  &c0dfF1&r     Toggle if moving should be allowed.\n"
		"  &c0dfF2&r     Toggle resizing.\n"
		"  &c0dfF3&r     Toggle aligning.\n"
		"  &c0dfF4&r     Toggle placing.\n"
		"  &c0dfSPACE&r  Toggle outlines/guidelines.\n",
		0);
}

int Test_OnGotFocus(ez_control_t *self, void *payload, void *ext_event_info)
{
	EZ_control_SetBackgroundColor(self, self->background_color[0], self->background_color[1], self->background_color[2], 200);
	return 0;
}

int Test_OnLostFocus(ez_control_t *self, void *payload, void *ext_event_info)
{
	EZ_control_SetBackgroundColor(self, self->background_color[0], self->background_color[1], self->background_color[2], 100);
	return 0;
}

ez_control_t *root = NULL;
ez_control_t *child1 = NULL;
ez_control_t *child2 = NULL;
ez_button_t *button = NULL;
ez_label_t *label = NULL;
ez_label_t *label2 = NULL;
ez_slider_t *slider = NULL;
ez_scrollbar_t *scrollbar = NULL;
ez_scrollpane_t *scrollpane = NULL;
ez_listview_t *listview = NULL;
ez_window_t *window = NULL;

int Test_OnButtonDraw(ez_control_t *self, void *payload, void *ext_event_info)
{
	int x, y;
	EZ_control_GetDrawingPosition(self, &x, &y);

	return 0;
}

int Test_OnSliderPositionChanged(ez_control_t *self, void *payload, void *ext_event_info)
{
	ez_slider_t *slider = (ez_slider_t *)self;

	int slider_pos = EZ_slider_GetPosition(slider);

	EZ_label_SetText(label2, va("%i", slider_pos));

	return 0;
}

int Test_OnControlDraw(ez_control_t *self, void *payload, void *ext_event_info)
{
	int x, y; //, i;
	EZ_control_GetDrawingPosition(self, &x, &y);

	/*for (i = 0; i < 30; i++)
	{
		Draw_String(x, y + i*8, va("%d%d%d%d", i, i, i, i));
	}*/

	/*
	{
		char str[] = "Hello this is a sentence that's supposed to fit within a box of stuff, I hope this works...";
		char line[1024];
		int last_index = 0;
		int i = 0;

		while (Util_GetNextWordwrapString(str, line, last_index, &last_index, sizeof(str) / sizeof(char), self->width, 8))
		{
			Draw_String(x, y + i*8, line);
			i++;
		}
	}
	*/

	/*{
		clrinfo_t color;
		color.i = 0;
		color.c = RGBA_TO_COLOR(0, 255, 0, 255);
		Draw_BigString(x, y, "Hej", &color, 1, 1, 1, 0);
	}*/

	return 0;
}

static hud_t *hud_hover = NULL;

//
// Main HUD Editor function.
//
static void HUD_Editor(void)
{
	qbool found = false;

	// If we just entered hoverlist mode we want to keep the mouse coordinates
	// so we know where to draw the list until the user has picked a HUD.
	if (!hud_hoverlist_pos_is_set
		&& hud_editor_mode == hud_editmode_hoverlist
		&& hud_editor_prevmode != hud_editmode_hoverlist)
	{
		hud_hoverlist_x = cursor_x;
		hud_hoverlist_y = cursor_y;
		hud_hoverlist_pos_is_set = true;
	}
	else if (hud_editor_mode != hud_editmode_hoverlist)
	{
		hud_hoverlist_pos_is_set = false;
	}

	// Find the HUD we're moving or have the cursor over.
	found = HUD_Editor_FindHudUnderCursor(&hud_hover);

	// Draw faint outlines for all visible hud elements.
	HUD_Editor_DrawOutlines();

	// Draw the "grep handles" for offscreen HUDs.
	HUD_Editor_DrawGreps();

	// Draw a rectangle around the currently active HUD element.
	if(found && hud_hover)
	{
		Draw_AlphaRectangleRGB(hud_hover->lx, hud_hover->ly, hud_hover->lw, hud_hover->lh, 1, false, RGBA_TO_COLOR(0, 255, 0, 255));
	}

	// If we are realigning draw a green outline for the selected hud element.
	if (selected_hud)
	{
		Draw_AlphaRectangleRGB(selected_hud->lx, selected_hud->ly, selected_hud->lw, selected_hud->lh, 2, false, RGBA_TO_COLOR(0, 255, 0, 255));
	}

	// Check the mouse/keyboard states and if we're hovering above a hud or not.
	HUD_Editor_EvaluateState(hud_hover);

	// Draw the child/parent connections the hud we're hovering has.
	HUD_Editor_DrawConnections(hud_hover);

	// Draw a red line from selected hud to cursor.
	if (selected_hud)
	{
		Draw_AlphaLineRGB(hud_mouse_x, hud_mouse_y, HUD_CENTER_X(selected_hud), HUD_CENTER_Y(selected_hud), 1, RGBA_TO_COLOR(255, 0, 0, 255));
	}

	// Check if we're performing any action.
	// (Only perform one at any given time).
	(void)
	(HUD_Editor_DrawHoverList(hud_hoverlist_x, hud_hoverlist_y, hud_hoverlist)
		 || HUD_Editor_Resizing(hud_hover)
		 || HUD_Editor_Moving(hud_hover)
		 || HUD_Editor_Placing(hud_hover)
		 || HUD_Editor_Aligning(hud_hover));

	// Draw tooltips for the HUD.
	HUD_Editor_DrawTooltips(hud_hover);

	// Show the help window?
	if(hud_editor_showhelp)
	{
		HUD_Editor_DrawHelp();
	}

	EZ_tree_EventLoop(&help_control_tree);
}

//
// Toggles the HUD Editor on or off.
//
void HUD_Editor_Toggle_f(void)
{
	extern cvar_t scr_newHud;
	// static keydest_t key_dest_prev = key_game;
	static int old_hud_planmode = 0;

	if (cls.state != ca_active)
	{
		// We can't turn on the hud editor when disconnected.
		if(!hud_editor)
		{
			Com_Printf("You need to be in game to use the HUD editor.\n");
		}

		// If the hud editor managed to still be on while disconnected.
		hud_editor = false;
	}
	else if (!scr_newHud.value)
	{
		Com_Printf("You have to have scr_newHud turned on to use the HUD editor.\n");
		hud_editor = false;
	}
	else
	{
		// Toggle.
		hud_editor = !hud_editor;
		S_LocalSound("misc/basekey.wav");
	}

	if (hud_editor)
	{
		// Start HUD Editor.

		key_dest = key_hudeditor;
		HUD_Editor_SetMode(hud_editmode_normal);

		// Set planmode by default.
		old_hud_planmode = hud_planmode.value;
		Cvar_SetValue(&hud_planmode, 1.0);

		// Start showing the help plaque so the user learns the controls.
		hud_editor_showhelp = true;
	}
	else
	{
		// Exit the HUD Editor.

		key_dest = key_game;
		key_dest_beforecon = key_game;
		HUD_Editor_SetMode(hud_editmode_off);
		scr_cursor_icon = NULL;

		// Reset to the old value for HUD planmode.
		Cvar_SetValue(&hud_planmode, old_hud_planmode);
	}
}

//
// Handles mouse events sent to the HUD editor.
//
qbool HUD_Editor_MouseEvent(mouse_state_t *ms)
{
	// Updating cursor location.
	if(hud_editor_mode == hud_editmode_move_lockedaxis)
	{
		// Don't update the HUD Editor cursor if the axis is locked
		// so that we avoid explicit checks for that.
		// The cursor will still move around the screen properly.
		if(hud_editor_locked_axis_is_x)
		{
			hud_mouse_x = ms->x;

			// Draw a line that indicates that the movement is locked to the X-axis.
			if (selected_hud)
			{
				Draw_AlphaLineRGB(0, HUD_CENTER_Y(selected_hud), vid.width, HUD_CENTER_Y(selected_hud), 1, RGBA_TO_COLOR(255, 0, 0, 75));
			}
		}
		else
		{
			hud_mouse_y = ms->y;

			if (selected_hud)
			{
				Draw_AlphaLineRGB(HUD_CENTER_X(selected_hud), 0, HUD_CENTER_X(selected_hud), vid.height, 1, RGBA_TO_COLOR(255, 0, 0, 75));
			}
		}
	}
	else
	{
		// Normal operation, always update cursor.
		hud_mouse_x = ms->x;
		hud_mouse_y = ms->y;
	}

	return EZ_tree_MouseEvent(&help_control_tree, ms);
}

//
// Handles key events sent to the HUD editor.
//
void HUD_Editor_Key(int key, int unichar, qbool down)
{
	static int planmode = 1;
	int togglekeys[2];

	EZ_tree_KeyEvent(&help_control_tree, key, unichar, down);

	M_FindKeysForCommand("toggleconsole", togglekeys);
	if ((key == togglekeys[0]) || (key == togglekeys[1]))
	{
		Con_ToggleConsole_f();
		return;
	}

	if (down)
	{
		switch (key)
		{
			case K_ESCAPE:
				HUD_Editor_Toggle_f();
				break;
			case 'p' :
				// Toggle HUD plan mode.
				planmode = !planmode;
				Cvar_SetValue(&hud_planmode, planmode);
				break;
			case 'h' :
				// Toggle the help window.
				hud_editor_showhelp = !hud_editor_showhelp;
				break;
			case K_SPACE :
				// Toggle hud element outlines.
				hud_editor_showoutlines = !hud_editor_showoutlines;
				break;
			case K_F1 :
				// Toggle moving.
				Cvar_SetValue(&hud_editor_allowmove, !hud_editor_allowmove.value);
				break;
			case K_F2 :
				// Toggle resizing.
				Cvar_SetValue(&hud_editor_allowresize, !hud_editor_allowresize.value);
				break;
			case K_F3 :
				// Toggle aligning.
				Cvar_SetValue(&hud_editor_allowalign, !hud_editor_allowalign.value);
				break;
			case K_F4 :
				// Toggle placing.
				Cvar_SetValue(&hud_editor_allowplace, !hud_editor_allowplace.value);
				break;
			case K_UPARROW :
				// TODO : Add "nudging" in hud editor.
				break;
			case K_DOWNARROW :
				// TODO : Add "nudging" in hud editor.
				break;
			case K_LEFTARROW :
				// TODO : Add "nudging" in hud editor.
				break;
			case K_RIGHTARROW :
				// TODO : Add "nudging" in hud editor.
				break;
		}
	}
}

//
// Inits HUD Editor.
//
void HUD_Editor_Init(void)
{
	extern mpic_t *SCR_LoadCursorImage(char *cursorimage);

#if 0
	clrinfo_t color;

	color.c = RGBA_TO_COLOR(255, 255, 255, 255);
	color.i = 0;

	// Root
	{
		root = EZ_control_Create(&help_control_tree, NULL, "Test window", "Test", 50, 50, 400, 400, control_focusable | control_movable | control_resize_h | control_resize_v);
		EZ_control_SetBackgroundColor(root, 0, 0, 0, 100);
		EZ_control_SetSize(root, 400, 400);
	}

	// Child 1
	{
		child1 = EZ_control_Create(&help_control_tree, root, "Child 1", "Test", 10, 10, 50, 50, control_focusable | control_movable | control_contained | control_scrollable | control_resizeable);

		EZ_control_AddOnGotFocus(child1, Test_OnGotFocus, NULL);
		EZ_control_AddOnLostFocus(child1, Test_OnLostFocus, NULL);
		EZ_control_AddOnDraw(child1, Test_OnControlDraw, NULL);

		EZ_control_SetMinVirtualSize(child1, child1->width * 3, child1->height * 3);
		//EZ_control_SetVirtualSize(child1, child1->width * 4, child1->height * 2);

		EZ_control_SetBackgroundColor(child1, 150, 150, 0, 100);
	}

	// Child 2
	{
		child2 = EZ_control_Create(&help_control_tree, root, "Child 2", "Test", 30, 50, 50, 20, control_focusable | control_contained);

		EZ_control_AddOnGotFocus(child2, Test_OnGotFocus, NULL);
		EZ_control_AddOnLostFocus(child2, Test_OnLostFocus, NULL);

		EZ_control_SetBackgroundColor(child2, 150, 150, 200, 100);
	}

	// Button.
	{
		button = EZ_button_Create(&help_control_tree, child1, "button", "A crazy button!", 15, -15, 80, 60, control_contained | control_resizeable);
		EZ_control_AddOnDraw((ez_control_t *)button, Test_OnButtonDraw, NULL);

		EZ_button_SetFocusedColor(button, 255, 0, 0, 255);
		EZ_button_SetNormalColor(button, 255, 255, 0, 100);
		EZ_button_SetPressedColor(button, 255, 255, 0, 255);
		EZ_button_SetHoverColor(button, 255, 0, 0, 150);

		EZ_button_SetToggleable(button, true);

		EZ_button_SetText(button, "Button");
		EZ_button_SetTextAlignment(button, middle_center);

		EZ_control_SetAnchor((ez_control_t *)button, (anchor_left | anchor_right | anchor_bottom));
	}

	// Label.
	{
		label = EZ_label_Create(&help_control_tree, root,
			"label", "A crazy label!", 200, 200, 250, 80,
			control_focusable | control_contained | control_resizeable | control_scrollable /*| control_movable */ | control_resize_h | control_resize_v,
			label_wraptext | label_autosize,
			"Hello\nthis is a test are you fine because I am bla bla bla this is a very long string and it's plenty of fun haha!");

		EZ_label_SetTextScale(label, 2.0);
		//EZ_label_SetTextFlags(label, LABEL_READONLY);

		EZ_control_SetBackgroundColor((ez_control_t *)label, 150, 150, 0, 50);
		//EZ_control_SetAnchor((ez_control_t *)label, anchor_top | anchor_right | anchor_bottom);
	}

	// Label 2.
	{
		label2 = EZ_label_Create(&help_control_tree, root,
			"label2", "A crazy label!", 100, 50, 32, 16,
			control_focusable | control_contained | control_resizeable,
			0, "");
	}

	// Slider.
	{
		slider = EZ_slider_Create(&help_control_tree, root,
			"slider", "Slider omg", 50, 100, 150, 8, control_focusable | control_contained | control_resizeable);

		EZ_control_SetAnchor((ez_control_t *)slider, anchor_left | anchor_right);

		EZ_slider_SetMax(slider, 100);
		EZ_slider_SetMin(slider, 50);
		EZ_slider_SetPosition(slider, 5);
		EZ_slider_SetScale(slider, 1.0);

		EZ_slider_AddOnSliderPositionChanged(slider, Test_OnSliderPositionChanged, NULL);
	}

	/*
	// Scrollbar.
	{
		ez_control_t *label_ctrl = (ez_control_t *)label;

		scrollbar = EZ_scrollbar_Create(&help_control_tree, root, "Scrollbar", "",
			30, 150, 10, 150, control_anchor_viewport);

		EZ_scrollbar_SetTargetParent(scrollbar, false);
		//EZ_control_SetContained((ez_control_t *)scrollbar, false);
		EZ_control_SetAnchor((ez_control_t *)scrollbar, anchor_right | anchor_top | anchor_bottom);
		EZ_control_SetMovable((ez_control_t *)scrollbar, false);
	}
	*/

	// Listview
	{
		listview = EZ_listview_Create(&help_control_tree, root, "Listview", "", 50, 50, 200, 200,
			control_resize_h | control_resize_v | control_resizeable);

		EZ_listview_SetHeaderText(listview, 0, "Hej");
		EZ_listview_SetHeaderText(listview, 1, "Hej 2");

		EZ_listview_SetColumnWidth(listview, 0, 80);
		EZ_listview_SetColumnWidth(listview, 1, 50);
	}

	// Scrollpane
	{
		scrollpane = EZ_scrollpane_Create(&help_control_tree, root, "Scrollpane", "", -10, -20, 150, 150,
			control_resize_h | control_resize_v | control_resizeable);

		EZ_control_SetBackgroundColor((ez_control_t *)scrollpane, 255, 0, 0, 100);

		//EZ_scrollpane_SetTarget(scrollpane, child1);
		EZ_scrollpane_SetTarget(scrollpane, (ez_control_t *)listview);
	}

	// Window.
	{
		window = EZ_window_Create(&help_control_tree, root, "Window", NULL, 20, 20, 150, 150,
			control_movable | control_focusable | control_resize_h | control_resize_v | control_contained);

		EZ_control_SetBackgroundColor((ez_control_t *)window, 0, 100, 0, 100);

		EZ_window_SetWindowAreaMinVirtualSize(window, 200, 200);

		//EZ_window_AddChild(window, (ez_control_t *)scrollpane);
	}

	/*
	// Test.
	{
		ez_control_t *c = EZ_control_Create(&help_control_tree, root, "C test 1", "Test", 10, 10, 150, 150,
			control_resize_h | control_focusable | control_movable | control_contained | control_scrollable | control_resizeable);

		ez_control_t *c2 = EZ_control_Create(&help_control_tree, c, "C test 1", "Test", 0, 10, 30, 10,
			control_focusable | control_movable | control_contained | control_scrollable | control_resizeable);

		EZ_control_SetAnchor(c2, anchor_top | anchor_right);
		EZ_control_SetBackgroundColor(c2, 150, 0, 20, 100);

		EZ_control_SetBackgroundColor(c, 50, 40, 50, 100);
	}
	*/

	EZ_tree_Refresh(&help_control_tree);

#endif

	// Register commands.
	Cmd_AddCommand("hud_editor", HUD_Editor_Toggle_f);

	// Register variables.
	Cvar_SetCurrentGroup(CVAR_GROUP_HUD);
	Cvar_Register(&hud_editor_allowresize);
	Cvar_Register(&hud_editor_allowmove);
	Cvar_Register(&hud_editor_allowplace);
	Cvar_Register(&hud_editor_allowalign);
    Cvar_ResetCurrentGroup();

	// Load HUD editor cursor icons.
	hud_editor_move_icon = SCR_LoadCursorImage("gfx/hud_move_icon");
	hud_editor_resize_icon = SCR_LoadCursorImage("gfx/hud_resize_icon");
	hud_editor_align_icon = SCR_LoadCursorImage("gfx/hud_align_icon");
	hud_editor_place_icon = SCR_LoadCursorImage("gfx/hud_place_icon");

	hud_editor = false;
	HUD_Editor_SetMode(hud_editmode_off);
}

//
// Draws the HUD Editor if it's on.
//
void HUD_Editor_Draw(void)
{
	if (!hud_editor)
		return;

	HUD_Editor();
}

//
// Should this HUD element be fully drawn or not when in align mode
// when using the HUD editor?
//
qbool HUD_Editor_ConfirmDraw(hud_t *hud)
{
	if(hud_editor_mode == hud_editmode_align || hud_editor_mode == hud_editmode_place)
	{
		// If this is the selected hud, or the parent of the selected hud then draw it.
		if((selected_hud && !strcmp(selected_hud->name, hud->name))
			|| (selected_hud && hud->place_hud && !strcmp(selected_hud->name, hud->place_hud->name)))
		{
			return true;
		}

		return false;
	}

	return true;
}