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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//===========================================================================//
#include "stdafx.h"
#include "collisionutils.h"
#include "fgdlib/gdclass.h"
#include "IEditorTexture.h"
#include "GlobalFunctions.h"
#include "hammer_mathlib.h"
#include "HelperFactory.h"
#include "MapAlignedBox.h"
#include "MapSweptPlayerHull.h"
#include "MapDefs.h"
#include "MapDoc.h"
#include "MapEntity.h"
#include "MapAnimator.h"
#include "MapSolid.h"
#include "MapView2D.h" // dvs FIXME: For HitTest2D implementation
#include "MapViewLogical.h"
#include "MapWorld.h"
#include "Options.h"
#include "Render2D.h"
#include "SaveInfo.h"
#include "VisGroup.h"
#include "MapSprite.h"
#include "camera.h"
#include "hammer.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
IMPLEMENT_MAPCLASS(CMapEntity)
#define LOGICAL_BOX_WIDTH 300
#define LOGICAL_BOX_HEIGHT 300
#define LOGICAL_BOX_INNER_OFFSET 10
#define LOGICAL_BOX_CONNECTOR_INPUT_WIDTH 50
#define LOGICAL_BOX_CONNECTOR_OUTPUT_WIDTH 50
#define LOGICAL_BOX_CONNECTOR_RADIUS 10
#define LOGICAL_BOX_ARROW_LENGTH 25
#define LOGICAL_BOX_ARROW_HEIGHT 10
class CMapAnimator;
class CMapKeyFrame;
bool CMapEntity::s_bShowEntityNames = true;
bool CMapEntity::s_bShowEntityConnections = false;
bool CMapEntity::s_bShowUnconnectedEntities = true;
static CMapObjectList FoundEntities;
//-----------------------------------------------------------------------------
// Purpose:
// Input : pEntity -
// pKV -
// Output :
//-----------------------------------------------------------------------------
static BOOL FindKeyValue(CMapEntity *pEntity, MDkeyvalue *pKV)
{
LPCTSTR pszValue = pEntity->GetKeyValue(pKV->szKey);
if (!pszValue || strcmpi(pszValue, pKV->szValue))
{
return TRUE;
}
FoundEntities.AddToTail(pEntity);
return TRUE;
}
//-----------------------------------------------------------------------------
// Purpose: Compares two entity names, allowing wildcards in EITHER string.
// Assumes that the wildcard character '*' marks the end of comparison,
// in other words, these are identical:
//
// test*
// test*stuff
//
// Input : szName1 -
// szName2 -
// Output : int
//-----------------------------------------------------------------------------
int CompareEntityNames(const char *szName1, const char *szName2)
{
int nCompareLen = -1;
const char *pszWildcard1 = strchr(szName1, '*');
if (pszWildcard1)
{
nCompareLen = pszWildcard1 - szName1;
}
const char *pszWildcard2 = strchr(szName2, '*');
if (pszWildcard2)
{
if (nCompareLen == -1)
{
nCompareLen = pszWildcard2 - szName2;
}
else
{
// Wildcards in both strings -- use the shorter pattern.
nCompareLen = min(nCompareLen, pszWildcard2 - szName2);
}
}
if (nCompareLen != -1)
{
if (nCompareLen > 0)
{
return strnicmp(szName1, szName2, nCompareLen);
}
// One of the strings had a wildcard as the first character.
return 0;
}
return stricmp(szName1, szName2);
}
//-----------------------------------------------------------------------------
// Replaces references to the old node ID with references to the new node ID.
//-----------------------------------------------------------------------------
static void ReplaceNodeIDRecursive(CMapClass *pRoot, int nOldNodeID, int nNewNodeID)
{
CMapEntity *pEntity = dynamic_cast <CMapEntity *>(pRoot);
if (pEntity)
{
GDclass *pClass = pEntity->GetClass();
if (!pClass)
return;
int nVarCount = pClass->GetVariableCount();
for (int i = 0; i < nVarCount; i++)
{
GDinputvariable *pVar = pClass->GetVariableAt(i);
if (pVar->GetType() == ivNodeDest)
{
const char *pszValue = pEntity->GetKeyValue(pVar->GetName());
if (pszValue && (atoi(pszValue) == nOldNodeID))
{
char szValue[100];
itoa(nNewNodeID, szValue, 10);
pEntity->SetKeyValue(pVar->GetName(), szValue);
}
}
}
}
else
{
const CMapObjectList *pChildren = pRoot->GetChildren();
FOR_EACH_OBJ( *pChildren, pos )
{
ReplaceNodeIDRecursive(pChildren->Element(pos), nOldNodeID, nNewNodeID);
}
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
static void ReplaceNodeIDRefs(CMapObjectList &newList, int nOldNodeID, int nNewNodeID)
{
// If they are the same, do nothing. This can happen when pasting from one
// map to another map.
if (nOldNodeID == nNewNodeID)
return;
FOR_EACH_OBJ( newList, pos )
{
CMapClass *pNew = newList.Element(pos);
ReplaceNodeIDRecursive(pNew, nOldNodeID, nNewNodeID);
}
}
//-----------------------------------------------------------------------------
// Purpose: Constructor.
//-----------------------------------------------------------------------------
CMapEntity::CMapEntity(void) : flags(0)
{
m_pMoveParent = NULL;
m_pAnimatorChild = NULL;
m_vecLogicalPosition.Init( COORD_NOTINIT, COORD_NOTINIT );
CalculateTypeFlags();
}
//-----------------------------------------------------------------------------
// Purpose: Destructor.
//-----------------------------------------------------------------------------
CMapEntity::~CMapEntity(void)
{
SignalChanged();
}
//-----------------------------------------------------------------------------
// Purpose: Adds a bounding box helper to this entity. If this entity's class
// specifies a bounding box, it will be the correct size.
// Input : pClass -
//-----------------------------------------------------------------------------
void CMapEntity::AddBoundBoxForClass(GDclass *pClass, bool bLoading)
{
Vector Mins;
Vector Maxs;
//
// If we have a class and it specifies a class, use that bounding box.
//
if ((pClass != NULL) && (pClass->HasBoundBox()))
{
pClass->GetBoundBox(Mins, Maxs);
}
//
// Otherwise, use a default bounding box.
//
else
{
VectorFill(Mins, -8);
VectorFill(Maxs, 8);
}
//
// Create the box and add it as one of our children.
//
CMapAlignedBox *pBox = new CMapAlignedBox(Mins, Maxs);
pBox->SetOrigin(m_Origin);
pBox->SetSelectionState(GetSelectionState());
//
// HACK: Make sure that the new child gets properly linked into the world.
// This is not correct because it bypasses the doc's AddObjectToWorld code.
//
// Don't call AddObjectToWorld during VMF load because we don't want to call
// OnAddToWorld during VMF load. We update our helpers during PostloadWorld.
//
CMapWorld *pWorld = (CMapWorld *)GetWorldObject(this);
if ((!bLoading) && (pWorld != NULL))
{
pWorld->AddObjectToWorld(pBox, this);
}
else
{
AddChild(pBox);
}
}
//-----------------------------------------------------------------------------
// Purpose: Sets our child's render color to our render color.
// Input : pChild - Child object being added.
//-----------------------------------------------------------------------------
void CMapEntity::AddChild(CMapClass *pChild)
{
CMapClass::AddChild(pChild);
//
// Notify the new child of all our keys. Don't bother for solids.
//
if (dynamic_cast<CMapSolid*>(pChild) == NULL)
{
for ( int i=GetFirstKeyValue(); i != GetInvalidKeyValue(); i=GetNextKeyValue( i ) )
{
MDkeyvalue KeyValue = m_KeyValues.GetKeyValue(i);
pChild->OnParentKeyChanged( KeyValue.szKey, KeyValue.szValue );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Adds a helper object as a child of this entity.
// Input : pHelper - The helper object.
// bLoading - True if this is being called from Postload, false otherwise.
//-----------------------------------------------------------------------------
void CMapEntity::AddHelper(CMapClass *pHelper, bool bLoading)
{
if (!IsPlaceholder())
{
//
// Solid entities have no origin, so place the helper at our center.
//
Vector vecCenter;
m_Render2DBox.GetBoundsCenter(vecCenter);
pHelper->SetOrigin(vecCenter);
}
else
{
pHelper->SetOrigin(m_Origin);
}
pHelper->SetSelectionState(GetSelectionState());
//
// If we have a game data class, set the child's render color to the color
// dictated by the game data class.
//
// Note: in AddChild, where it calls OnParentKeyChanged for everything, the color in the helper can
// get set to something else based on the entity's properties (CMapLightCone does this, for example).
//
GDclass *pClass = GetClass();
if ( pClass )
{
color32 rgbColor = pClass->GetColor();
pHelper->SetRenderColor(rgbColor);
}
//
// HACK: Make sure that the new child gets properly linked into the world.
// This is not correct because it bypasses the doc's AddObjectToWorld code.
//
// Don't call AddObjectToWorld during VMF load because we don't want to call
// OnAddToWorld during VMF load. We update our helpers during PostloadWorld.
//
CMapWorld *pWorld = (CMapWorld *)GetWorldObject(this);
if ((!bLoading) && (pWorld != NULL))
{
pWorld->AddObjectToWorld(pHelper, this);
}
else
{
AddChild(pHelper);
}
//
// dvs: HACK for animator children. Better for CMapEntity to have a SetAnimatorChild
// function that the CMapAnimator could call. Better still, eliminate the knowledge
// that CMapEntity has about its animator child.
//
CMapAnimator *pAnim = dynamic_cast<CMapAnimator *>(pHelper);
if (pAnim != NULL)
{
m_pAnimatorChild = pAnim;
}
}
//-----------------------------------------------------------------------------
// Purpose: Creates all helper objects defined by the FGD and adds them as
// children of this entity. Helper objects perform rendering, UI, and
// bookkeeping functions for their parent entities. If the class
// definition does not specify any helpers, or none of the helpers
// could be added, a box helper is added so that the entity has some
// visual representation.
// Inputs : pClass -
// bLoading - True if this is being called from Postload, false otherwise.
//-----------------------------------------------------------------------------
void CMapEntity::AddHelpersForClass(GDclass *pClass, bool bLoading)
{
bool bAddedOneVisual = false;
if (((pClass != NULL) && (pClass->HasBoundBox())))
{
AddBoundBoxForClass(pClass, bLoading);
bAddedOneVisual = true;
}
//
// If we have a game class from the FGD, add whatever helpers are declared in that
// class definition.
//
if (pClass != NULL)
{
//
// Add all the helpers that this class declares in the FGD.
//
GDclass *pClassLocal = GetClass();
//
// For every helper in the class definition...
//
int nHelperCount = pClassLocal->GetHelperCount();
for (int i = 0; i < nHelperCount; i++)
{
CHelperInfo *pHelperInfo = pClassLocal->GetHelper(i);
//
// Create the helper and attach it to this entity.
//
CMapClass *pHelper = CHelperFactory::CreateHelper(pHelperInfo, this);
if (pHelper != NULL)
{
AddHelper(pHelper, bLoading);
if (pHelper->IsVisualElement())
{
bAddedOneVisual = true;
}
}
}
//
// Look for keys that define helpers.
//
// FIXME: make this totally data driven like the helper factory, or better
// yet, like the LINK_ENTITY_TO_CLASS stuff in the game DLL
int nVarCount = pClassLocal->GetVariableCount();
for (int i = 0; i < nVarCount; i++)
{
GDinputvariable *pVar = pClassLocal->GetVariableAt(i);
GDIV_TYPE eType = pVar->GetType();
CHelperInfo HelperInfo;
bool bCreate = false;
switch (eType)
{
case ivOrigin:
{
const char *pszKey = pVar->GetName();
HelperInfo.SetName("origin");
HelperInfo.AddParameter(pszKey);
bCreate = true;
break;
}
case ivVecLine:
{
const char *pszKey = pVar->GetName();
HelperInfo.SetName("vecline");
HelperInfo.AddParameter(pszKey);
bCreate = true;
break;
}
case ivAxis:
{
const char *pszKey = pVar->GetName();
HelperInfo.SetName("axis");
HelperInfo.AddParameter(pszKey);
bCreate = true;
break;
}
}
//
// Create the helper and attach it to this entity.
//
if (bCreate)
{
CMapClass *pHelper = CHelperFactory::CreateHelper(&HelperInfo, this);
if (pHelper != NULL)
{
AddHelper(pHelper, bLoading);
if (pHelper->IsVisualElement())
{
bAddedOneVisual = true;
}
}
}
}
}
//
// Any solid children we have will also work as visual elements.
//
if (!IsPlaceholder())
{
bAddedOneVisual = true;
}
//
// If we have no game class and we are a point entity, add an "obsolete" sprite helper
// so level designers know to update the entity.
//
else if (pClass == NULL)
{
CHelperInfo HelperInfo;
HelperInfo.SetName("iconsprite");
HelperInfo.AddParameter("sprites/obsolete.spr");
CMapClass *pSprite = CHelperFactory::CreateHelper(&HelperInfo, this);
if (pSprite != NULL)
{
AddHelper(pSprite, bLoading);
bAddedOneVisual = true;
}
}
//
// If we still haven't added any visible helpers, we need to add a bounding box so that there
// is some visual representation for this entity. We also add the bounding box if the
// entity's class specifies a bounding box.
//
if (!bAddedOneVisual)
{
AddBoundBoxForClass(pClass, bLoading);
}
if ( !CMapClass::s_bLoadingVMF )
{
CalcBounds(TRUE);
PostUpdate(Notify_Changed);
}
}
//-----------------------------------------------------------------------------
// Purpose: Returns a deep copy of this object.
// Output : Returns a pointer to the new allocated object.
//-----------------------------------------------------------------------------
CMapClass *CMapEntity::Copy(bool bUpdateDependencies)
{
CMapEntity *pNew = new CMapEntity;
pNew->CopyFrom(this, bUpdateDependencies);
return pNew;
}
//-----------------------------------------------------------------------------
// Purpose: Performs a deep copy of a given object into this object.
// Input : pobj - Object to copy from.
// Output : Returns a pointer to this object.
//-----------------------------------------------------------------------------
CMapClass *CMapEntity::CopyFrom(CMapClass *pobj, bool bUpdateDependencies)
{
Assert(pobj->IsMapClass(MAPCLASS_TYPE(CMapEntity)));
CMapEntity *pFrom = (CMapEntity*) pobj;
flags = pFrom->flags;
m_Origin = pFrom->m_Origin;
m_vecLogicalPosition = pFrom->m_vecLogicalPosition;
CMapClass::CopyFrom(pobj, bUpdateDependencies);
//
// Copy our keys. If our targetname changed we must relink all targetname pointers.
//
const char *pszOldTargetName = CEditGameClass::GetKeyValue("targetname");
char szOldTargetName[MAX_IO_NAME_LEN];
if (pszOldTargetName != NULL)
{
strcpy(szOldTargetName, pszOldTargetName);
}
CEditGameClass::CopyFrom(pFrom);
const char *pszNewTargetName = CEditGameClass::GetKeyValue("targetname");
if ((bUpdateDependencies) && (pszNewTargetName != NULL))
{
if (stricmp(szOldTargetName, pszNewTargetName) != 0)
{
UpdateAllDependencies(this);
}
}
CalculateTypeFlags();
SignalChanged();
return(this);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : bFullUpdate -
//-----------------------------------------------------------------------------
void CMapEntity::CalcBounds(BOOL bFullUpdate)
{
CMapClass::CalcBounds(bFullUpdate);
//
// If we are a solid entity, set our origin to our bounds center.
//
if (IsSolidClass())
{
m_Render2DBox.GetBoundsCenter(m_Origin);
}
}
//-----------------------------------------------------------------------------
// Purpose: Debugging hook.
//-----------------------------------------------------------------------------
#pragma warning (disable:4189)
void CMapEntity::Debug(void)
{
int i = m_KeyValues.GetFirst();
MDkeyvalue &KeyValue = m_KeyValues.GetKeyValue(i);
}
#pragma warning (default:4189)
//-----------------------------------------------------------------------------
// Purpose: If this entity has a name key, returns a string with "<name> <classname>"
// in it. Otherwise returns a buffer with "<classname>" in it.
// Output : String description of the entity.
//-----------------------------------------------------------------------------
const char* CMapEntity::GetDescription(void)
{
static char szBuf[128];
const char *pszName = GetKeyValue("targetname");
if (pszName != NULL)
{
sprintf(szBuf, "%s - %s", pszName, GetClassName());
}
else
{
V_strcpy_safe( szBuf, GetClassName() );
}
return(szBuf);
}
//-----------------------------------------------------------------------------
// Purpose: Returns the color that this entity should use for rendering.
//-----------------------------------------------------------------------------
void CMapEntity::GetRenderColor( CRender2D *pRender, unsigned char &red, unsigned char &green, unsigned char &blue )
{
if ( IsSelected() )
{
red = GetRValue(Options.colors.clrSelection);
green = GetGValue(Options.colors.clrSelection);
blue = GetBValue(Options.colors.clrSelection);
}
else
{
GDclass *pClass = GetClass();
if (pClass)
{
color32 rgbColor = pClass->GetColor();
red = rgbColor.r;
green = rgbColor.g;
blue = rgbColor.b;
}
else
{
red = GetRValue(Options.colors.clrEntity);
green = GetGValue(Options.colors.clrEntity);
blue = GetBValue(Options.colors.clrEntity);
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Returns the color that this entity should use for rendering.
//-----------------------------------------------------------------------------
color32 CMapEntity::GetRenderColor( CRender2D *pRender )
{
color32 clr;
GetRenderColor( pRender, clr.r, clr.g, clr.b );
return clr;
}
//-----------------------------------------------------------------------------
// Purpose: Returns the size of this object.
// Output : Size, in bytes, of this object, not including any dynamically
// allocated data members.
//-----------------------------------------------------------------------------
size_t CMapEntity::GetSize(void)
{
return(sizeof(*this));
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pFile -
// Output : ChunkFileResult_t
//-----------------------------------------------------------------------------
ChunkFileResult_t CMapEntity::LoadVMF(CChunkFile *pFile)
{
//
// Set up handlers for the subchunks that we are interested in.
//
CChunkHandlerMap Handlers;
Handlers.AddHandler("solid", (ChunkHandler_t)LoadSolidCallback, this);
Handlers.AddHandler("hidden", (ChunkHandler_t)LoadHiddenCallback, this);
Handlers.AddHandler("editor", (ChunkHandler_t)LoadEditorCallback, this);
Handlers.AddHandler("connections", (ChunkHandler_t)LoadConnectionsCallback, (CEditGameClass *)this);
pFile->PushHandlers(&Handlers);
ChunkFileResult_t eResult = pFile->ReadChunk((KeyHandler_t)LoadKeyCallback, this);
pFile->PopHandlers();
return(eResult);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *szKey -
// *szValue -
// *pEntity -
// Output : ChunkFileResult_t
//-----------------------------------------------------------------------------
ChunkFileResult_t CMapEntity::LoadKeyCallback(const char *szKey, const char *szValue, CMapEntity *pEntity)
{
if (!stricmp(szKey, "id"))
{
pEntity->SetID(atoi(szValue));
}
else
{
//
// While loading, set key values directly rather than via SetKeyValue. This avoids
// all the unnecessary bookkeeping that goes on in SetKeyValue.
//
pEntity->m_KeyValues.SetValue(szKey, szValue);
}
pEntity->CalculateTypeFlags();
pEntity->SignalChanged();
return(ChunkFile_Ok);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : bVisible -
// Output : ChunkFileResult_t
//-----------------------------------------------------------------------------
ChunkFileResult_t CMapEntity::LoadHiddenCallback(CChunkFile *pFile, CMapEntity *pEntity)
{
//
// Set up handlers for the subchunks that we are interested in.
//
CChunkHandlerMap Handlers;
Handlers.AddHandler("solid", (ChunkHandler_t)LoadSolidCallback, pEntity);
Handlers.AddHandler("editor", (ChunkHandler_t)LoadEditorCallback, pEntity);
pFile->PushHandlers(&Handlers);
ChunkFileResult_t eResult = pFile->ReadChunk();
pFile->PopHandlers();
return(eResult);
}
ChunkFileResult_t CMapEntity::LoadEditorKeyCallback( const char *szKey, const char *szValue, CMapEntity *pMapEntity )
{
if ( !stricmp( szKey, "logicalpos" ) )
{
CChunkFile::ReadKeyValueVector2(szValue, pMapEntity->m_vecLogicalPosition );
return ChunkFile_Ok;
}
return CMapClass::LoadEditorKeyCallback( szKey, szValue, pMapEntity );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
ChunkFileResult_t CMapEntity::LoadEditorCallback(CChunkFile *pFile, CMapEntity *pObject)
{
return pFile->ReadChunk( (KeyHandler_t)LoadEditorKeyCallback, pObject );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pFile -
// *pEntity -
// Output : ChunkFileResult_t
//-----------------------------------------------------------------------------
ChunkFileResult_t CMapEntity::LoadSolidCallback(CChunkFile *pFile, CMapEntity *pEntity)
{
CMapSolid *pSolid = new CMapSolid;
bool bValid;
ChunkFileResult_t eResult = pSolid->LoadVMF(pFile, bValid);
if ((eResult == ChunkFile_Ok) && (bValid))
{
pEntity->AddChild(pSolid);
}
else
{
delete pSolid;
}
return(eResult);
}
//-----------------------------------------------------------------------------
// Purpose: Sets this entity's origin and updates the bounding box.
// Input : o - Origin to set.
//-----------------------------------------------------------------------------
void CMapEntity::SetOrigin(Vector& o)
{
Vector vecOrigin;
GetOrigin(vecOrigin);
if (vecOrigin == o)
return;
CMapClass::SetOrigin(o);
// dvs: is this still necessary?
if (!(flags & flagPlaceholder))
{
// not a placeholder.. no origin.
return;
}
if ( !CMapClass::s_bLoadingVMF )
{
CalcBounds( TRUE );
PostUpdate(Notify_Changed);
SignalChanged();
}
}
//-----------------------------------------------------------------------------
// Purpose: Removes all of this entity's helpers.
// Input : bRemoveSolidChildren - Whether to also remove any solid children. This
// is true when changing from a solid entity to a point entity.
//-----------------------------------------------------------------------------
void CMapEntity::RemoveHelpers(bool bRemoveSolids)
{
for( int pos=m_Children.Count()-1; pos>=0; pos-- )
{
CMapClass *pChild = m_Children[pos];
if (bRemoveSolids || ((dynamic_cast <CMapSolid *> (pChild)) == NULL))
{
m_Children.FastRemove(pos);
}
// LEAKLEAK: need to KeepForDestruction to avoid undo crashes, but how? where?
//delete pChild;
}
}
//-----------------------------------------------------------------------------
// Building targetnames which deal with *
//-----------------------------------------------------------------------------
static inline void BuildNewTargetName( const char *pOldName, const char *pNewName, char *pBuffer )
{
strcpy(pBuffer, pNewName);
// If we matched a key value that contains wildcards, preserve the
// wildcards when we replace the name.
//
// For example, "oldname*" would become "newname*" instead of just "newname"
// FIXME: ??? handle different-length names with wildcards, eg. "old_vort*" => "new_weasel*"
const char *pszWildcard = strchr(pOldName, '*');
if (pszWildcard)
{
strcpy(&pBuffer[pszWildcard - pOldName], "*");
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CMapEntity::ReplaceTargetname(const char *szOldName, const char *szNewName)
{
// NOTE: Case-sensitive compare because people might want to replace one case with another.
if ( !Q_strcmp( szOldName, szNewName ) )
{
// The names already match. There is nothing to do!
return;
}
char szTempName[MAX_KEYVALUE_LEN];
//
// Replace any keys whose value matches the old name.
//
for ( int i=GetFirstKeyValue(); i != GetInvalidKeyValue(); i=GetNextKeyValue( i ) )
{
MDkeyvalue KeyValue = m_KeyValues.GetKeyValue(i);
if (!CompareEntityNames(KeyValue.szValue, szOldName))
{
BuildNewTargetName( KeyValue.szValue, szNewName, szTempName );
SetKeyValue( KeyValue.szKey, szTempName );
}
}
//
// Replace any connections that target the old name.
//
int nConnCount = Connections_GetCount();
for (int i = 0; i < nConnCount; i++)
{
CEntityConnection *pConn = Connections_Get(i);
if (!CompareEntityNames( pConn->GetTargetName(), szOldName ))
{
BuildNewTargetName( pConn->GetTargetName(), szNewName, szTempName );
pConn->SetTargetName(szTempName);
}
if (!CompareEntityNames( pConn->GetSourceName(), szOldName ))
{
BuildNewTargetName( pConn->GetSourceName(), szNewName, szTempName );
pConn->SetSourceName(szTempName);
}
if ( !CompareEntityNames( pConn->GetParam(), szOldName ))
{
BuildNewTargetName( pConn->GetParam(), szNewName, szTempName );
pConn->SetParam(szTempName);
}
}
CMapClass::ReplaceTargetname(szOldName, szNewName);
}
//-----------------------------------------------------------------------------
// Purpose:
// Inputs : pszClass -
// bLoading - True if this is being called from Postload, false otherwise.
//-----------------------------------------------------------------------------
void CMapEntity::SetClass(LPCTSTR pszClass, bool bLoading)
{
Assert(pszClass);
//
// If we are just setting to the same class, don't do anything.
//
if (IsClass(pszClass))
{
return;
}
//
// Copy class name & resolve GDclass pointer.
//
CEditGameClass::SetClass(pszClass, bLoading);
UpdateObjectColor();
//
// If our new class is defined in the FGD, set our color and our default keys
// from the class.
//
if (IsClass())
{
SetPlaceholder(!IsSolidClass());
GetDefaultKeys();
if (IsNodeClass() && (GetNodeID() == 0))
{
AssignNodeID();
}
}
//
// If not, use whether or not we have solid children to determine whether
// we are a point entity or a solid entity.
//
else
{
SetPlaceholder(HasSolidChildren() ? FALSE : TRUE);
}
//
// Add whatever helpers our class requires, or a default bounding box if
// our class is unknown and we are a point entity.
//
UpdateHelpers(bLoading);
CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
if ( !pDoc->IsLoading() )
{
pDoc->RemoveFromAutoVisGroups( this );
pDoc->AddToAutoVisGroup( this );
}
//
// HACK: If we are now a decal, make sure we have a valid texture.
//
if (!strcmp(pszClass, "infodecal"))
{
if (!GetKeyValue("texture"))
{
SetKeyValue("texture", "clip");
}
}
CalculateTypeFlags();
SignalChanged();
}
//-----------------------------------------------------------------------------
// Purpose: Assigns the next unique node ID to this entity.
//-----------------------------------------------------------------------------
void CMapEntity::AssignNodeID(void)
{
char szID[80];
CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
itoa(pDoc->GetNextNodeID(), szID, 10);
SetKeyValue("nodeid", szID);
}
struct CClassNameFlagsMatcher
{
char const *m_pClassname;
int m_nFlagsToOR;
};
static CClassNameFlagsMatcher s_ClassFlagsTable[]={
{ "light_environment", ENTITY_FLAG_IS_LIGHT },
{ "light", ENTITY_FLAG_IS_LIGHT },
{ "light_spot", ENTITY_FLAG_IS_LIGHT },
{ "prop_static", ENTITY_FLAG_SHOW_IN_LPREVIEW2 },
{ "func_instance", ENTITY_FLAG_IS_INSTANCE },
};
void CMapEntity::CalculateTypeFlags( void )
{
m_EntityTypeFlags = 0;
const char *pszClassName = GetClassName();
if (pszClassName != NULL)
for(int i=0; i<NELEMS( s_ClassFlagsTable ); i++)
if ( ! stricmp( pszClassName, s_ClassFlagsTable[i].m_pClassname ) )
m_EntityTypeFlags |= s_ClassFlagsTable[i].m_nFlagsToOR;
}
void CMapEntity::SignalChanged( void )
{
if ( m_EntityTypeFlags & ENTITY_FLAG_IS_LIGHT )
SignalUpdate( EVTYPE_LIGHTING_CHANGED );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMapEntity::EnsureUniqueNodeID(CMapWorld *pWorld)
{
bool bBuildNewNodeID = true;
int nOurNodeID = GetNodeID();
if (nOurNodeID != 0)
{
//
// We already have a node ID. Make sure that it is unique. If not,
// we need to generate a new one.
//
bBuildNewNodeID = false;
EnumChildrenPos_t pos;
CMapClass *pChild = pWorld->GetFirstDescendent(pos);
while (pChild != NULL)
{
CMapEntity *pEntity = dynamic_cast <CMapEntity *> (pChild);
if ((pEntity != NULL) && (pEntity != this))
{
int nThisNodeID = pEntity->GetNodeID();
if (nThisNodeID)
{
if (nThisNodeID == nOurNodeID)
{
bBuildNewNodeID = true;
break;
}
}
}
pChild = pWorld->GetNextDescendent(pos);
}
}
if (bBuildNewNodeID)
{
AssignNodeID();
}
}
//-----------------------------------------------------------------------------
// Purpose: Called after the entire map has been loaded. This allows the object
// to perform any linking with other map objects or to do other operations
// that require all world objects to be present.
//-----------------------------------------------------------------------------
void CMapEntity::PostloadWorld(CMapWorld *pWorld)
{
int nIndex;
//
// Set our origin from our "origin" key and discard the key.
//
const char *pszValue = m_KeyValues.GetValue("origin", &nIndex);
if (pszValue != NULL)
{
Vector Origin;
sscanf(pszValue, "%f %f %f", &Origin[0], &Origin[1], &Origin[2]);
SetOrigin(Origin);
}
//
// Set our angle from our "angle" key and discard the key.
//
pszValue = m_KeyValues.GetValue("angle", &nIndex);
if (pszValue != NULL)
{
ImportAngle(atoi(pszValue));
RemoveKey(nIndex);
}
//
// Set the class name from our "classname" key and discard the key.
// This also adds the helpers appropriate for the class.
//
pszValue = m_KeyValues.GetValue("classname", &nIndex);
if (pszValue != NULL)
{
//
// Copy the classname to a temp buffer because SetClass mucks with the
// keyvalues and our pointer might become bad.
//
char szClassName[MAX_CLASS_NAME_LEN];
strcpy(szClassName, pszValue);
SetClass(szClassName, true);
//
// Need to re-get the index of the classname key since it may have changed
// as a result of the above SetClass call.
//
pszValue = m_KeyValues.GetValue("classname", &nIndex);
if (pszValue != NULL)
{
RemoveKey(nIndex);
}
}
//
// Now that we have set the class, remove the origin key if this entity isn't
// supposed to expose it in the keyvalues list.
//
if (IsPlaceholder() && (!IsClass() || GetClass()->VarForName("origin") == NULL))
{
pszValue = m_KeyValues.GetValue("origin", &nIndex);
if (pszValue != NULL)
{
RemoveKey(nIndex);
}
}
//
// Must do this after assigning the class.
//
if (IsNodeClass() && (GetKeyValue("nodeid") == NULL))
{
AssignNodeID();
}
// Set a reasonable default
Vector2D vecLogicalPos = GetLogicalPosition();
if ( vecLogicalPos.x == COORD_NOTINIT )
{
CMapDoc::GetActiveMapDoc()->GetDefaultNewLogicalPosition( vecLogicalPos );
SetLogicalPosition( vecLogicalPos );
}
//
// Call in all our children (some of which were created above).
//
CMapClass::PostloadWorld(pWorld);
CalculateTypeFlags();
}
//-----------------------------------------------------------------------------
// Purpose: Insures that the entity has all the helpers that it needs (and no more
// than it should) given its class.
//-----------------------------------------------------------------------------
void CMapEntity::UpdateHelpers(bool bLoading)
{
//
// If we have any helpers, delete them. Delete any solid children if we are
// a point class.
//
RemoveHelpers(IsPlaceholder() == TRUE);
//
// Add the helpers appropriate for our current class.
//
AddHelpersForClass(GetClass(), bLoading);
}
//-----------------------------------------------------------------------------
// Safely sets the move parent. Will assert and not set it if pEnt is equal to this ent,
// or if this ent is already a parent of pEnt.
//-----------------------------------------------------------------------------
void CMapEntity::SetMoveParent( CMapEntity *pEnt )
{
// Make sure pEnt is not already parented to (or identical to) me.
CMapEntity *pCur = pEnt;
for ( int i=0; i < 300; i++ )
{
if ( pCur == NULL )
{
break;
}
else if ( pCur == this )
{
Assert( !"SetMoveParent: recursive parenting!" );
m_pMoveParent = NULL;
return;
}
pCur = pCur->m_pMoveParent;
}
m_pMoveParent = pEnt;
}
//-----------------------------------------------------------------------------
// Purpose: Allows the entity to update its key values based on a change in one
// of its children. The child exposes the property as a key value pair.
// Input : pChild - The child whose property changed.
// szKey - The name of the property that changed.
// szValue - The new value of the property.
//-----------------------------------------------------------------------------
void CMapEntity::NotifyChildKeyChanged(CMapClass *pChild, const char *szKey, const char *szValue)
{
m_KeyValues.SetValue(szKey, szValue);
//
// Notify all our other non-solid children that a key has changed.
//
FOR_EACH_OBJ( m_Children, pos )
{
CMapClass *pObject = m_Children.Element(pos);
if ((pObject != pChild) && (pChild != NULL) && (dynamic_cast<CMapSolid *>(pObject) == NULL))
{
pObject->OnParentKeyChanged(szKey, szValue);
}
}
CalcBounds();
CalculateTypeFlags();
SignalChanged();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMapEntity::DeleteKeyValue(LPCSTR pszKey)
{
char szOldValue[KEYVALUE_MAX_VALUE_LENGTH];
const char *pszOld = GetKeyValue(pszKey);
if (pszOld != NULL)
{
strcpy(szOldValue, pszOld);
}
else
{
szOldValue[0] = '\0';
}
CEditGameClass::DeleteKeyValue(pszKey);
OnKeyValueChanged(pszKey, szOldValue, "");
CalculateTypeFlags();
SignalChanged();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMapEntity::SetKeyValue(LPCSTR pszKey, LPCSTR pszValue)
{
//
// Get the current value so we can tell if it is changing.
//
char szOldValue[KEYVALUE_MAX_VALUE_LENGTH];
const char *pszOld = GetKeyValue(pszKey);
if (pszOld != NULL)
{
V_strcpy_safe(szOldValue, pszOld);
}
else
{
szOldValue[0] = '\0';
}
CEditGameClass::SetKeyValue(pszKey, pszValue);
OnKeyValueChanged(pszKey, szOldValue, pszValue);
SignalChanged();
}
//-----------------------------------------------------------------------------
// Purpose: Notifies the entity that it has been cloned.
// Input : pClone -
//-----------------------------------------------------------------------------
void CMapEntity::OnPreClone(CMapClass *pClone, CMapWorld *pWorld, const CMapObjectList &OriginalList, CMapObjectList &NewList)
{
CMapClass::OnPreClone(pClone, pWorld, OriginalList, NewList);
if (OriginalList.Count() == 1)
{
// dvs: TODO: make this FGD-driven instead of hardcoded, see also MapKeyFrame.cpp
// dvs: TODO: use letters of the alphabet between adjacent numbers, ie path2a path2b, etc.
if (!stricmp(GetClassName(), "path_corner") || !stricmp(GetClassName(), "path_track"))
{
//
// Generate a new name for the clone.
//
CMapEntity *pNewEntity = dynamic_cast<CMapEntity*>(pClone);
Assert(pNewEntity != NULL);
if (!pNewEntity)
return;
// create a new targetname for the clone
char newName[128];
const char *oldName = GetKeyValue("targetname");
if (!oldName || oldName[0] == 0)
oldName = "path";
pWorld->GenerateNewTargetname(oldName, newName, sizeof(newName), true, NULL);
pNewEntity->SetKeyValue("targetname", newName);
}
}
if (IsNodeClass())
{
((CMapEntity *)pClone)->AssignNodeID();
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pClone -
// pWorld -
// OriginalList -
// NewList -
//-----------------------------------------------------------------------------
void CMapEntity::OnClone(CMapClass *pClone, CMapWorld *pWorld, const CMapObjectList &OriginalList, CMapObjectList &NewList)
{
CMapClass::OnClone(pClone, pWorld, OriginalList, NewList);
if (OriginalList.Count() == 1)
{
if (!stricmp(GetClassName(), "path_corner") || !stricmp(GetClassName(), "path_track"))
{
// dvs: TODO: make this FGD-driven instead of hardcoded, see also MapKeyFrame.cpp
// dvs: TODO: use letters of the alphabet between adjacent numbers, ie path2a path2b, etc.
CMapEntity *pNewEntity = dynamic_cast<CMapEntity*>(pClone);
Assert(pNewEntity != NULL);
if (!pNewEntity)
return;
// Point the clone at what we were pointing at.
const char *pszNext = GetKeyValue("target");
if (pszNext)
{
pNewEntity->SetKeyValue("target", pszNext);
}
// Point this path corner at the clone.
SetKeyValue("target", pNewEntity->GetKeyValue("targetname"));
}
}
if (IsNodeClass())
{
ReplaceNodeIDRefs(NewList, GetNodeID(), ((CMapEntity *)pClone)->GetNodeID());
}
}
//-----------------------------------------------------------------------------
// Purpose: Notifies the object that a copy of it is being pasted from the
// clipboard before the copy is added to the world.
// Input : pCopy - The copy of this object that is being added to the world.
// pSourceWorld - The world that the originals were in.
// pDestWorld - The world that the copies are being added to.
// OriginalList - The list of original objects that were copied.
// NewList - The list of copied.
//-----------------------------------------------------------------------------
void CMapEntity::OnPrePaste( CMapClass *pCopy, CMapWorld *pSourceWorld, CMapWorld *pDestWorld, const CMapObjectList &OriginalList, CMapObjectList &NewList )
{
if (IsNodeClass())
{
// Generate a new node ID.
((CMapEntity *)pCopy)->AssignNodeID();
}
CMapClass::OnPrePaste(pCopy, pSourceWorld, pDestWorld, OriginalList, NewList);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pCopy -
// pSourceWorld -
// pDestWorld -
// OriginalList -
// NewList -
//-----------------------------------------------------------------------------
void CMapEntity::OnPaste(CMapClass *pCopy, CMapWorld *pSourceWorld, CMapWorld *pDestWorld, const CMapObjectList &OriginalList, CMapObjectList &NewList)
{
if (IsNodeClass())
{
ReplaceNodeIDRefs(NewList, GetNodeID(), ((CMapEntity *)pCopy)->GetNodeID());
}
CMapClass::OnPaste(pCopy, pSourceWorld, pDestWorld, OriginalList, NewList);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pszKey -
// pszOldValue -
// pszValue -
//-----------------------------------------------------------------------------
void CMapEntity::OnKeyValueChanged(const char *pszKey, const char *pszOldValue, const char *pszValue)
{
// notify all our children that a key has changed
FOR_EACH_OBJ( m_Children, pos )
{
CMapClass *pChild = m_Children.Element( pos );
if ( pChild != NULL )
{
pChild->OnParentKeyChanged( pszKey, pszValue );
}
}
//
// Changing our movement parent. Store a pointer to the movement parent
// for when we're playing animations.
//
if ( !stricmp(pszKey, "parentname") )
{
CMapWorld *pWorld = (CMapWorld *)GetWorldObject( this );
if (pWorld != NULL)
{
CMapEntity *pMoveParent = (CMapEntity *)UpdateDependency(m_pMoveParent, pWorld->FindEntityByName( pszValue));
SetMoveParent( pMoveParent );
}
}
//
// Changing our model - rebuild the helpers from scratch.
// dvs: this could probably go away - move support into the helper code.
//
else if (!stricmp(pszKey, "model"))
{
if (stricmp(pszOldValue, pszValue) != 0)
{
// We don't call SetKeyValue during VMF load.
UpdateHelpers(false);
}
}
//
// If our targetname has changed, we have to relink EVERYTHING, not
// just our dependents, because someone else may point to our new targetname.
//
else if (!stricmp(pszKey, "targetname") && (stricmp(pszOldValue, pszValue) != 0))
{
UpdateAllDependencies(this);
}
SignalChanged();
}
//-----------------------------------------------------------------------------
// Purpose: Returns true if this entity has any solid children. Entities of
// classes that are not in the FGD are considered solid entities if
// they have at least one solid child, point entities if not.
//-----------------------------------------------------------------------------
bool CMapEntity::HasSolidChildren(void)
{
FOR_EACH_OBJ( m_Children, pos )
{
CMapClass *pChild = m_Children.Element(pos);
if ((dynamic_cast <CMapSolid *> (pChild)) != NULL)
{
return(true);
}
}
return(false);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CMapEntity::OnApply( void )
{
FOR_EACH_OBJ( m_Children, pos )
{
CMapClass *pChild = m_Children.Element(pos);
if ( pChild )
{
pChild->OnApply();
}
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Called after this object is added to the world.
//
// NOTE: This function is NOT called during serialization. Use PostloadWorld
// to do similar bookkeeping after map load.
//
// Input : pWorld - The world that we have been added to.
//-----------------------------------------------------------------------------
void CMapEntity::OnAddToWorld(CMapWorld *pWorld)
{
CMapClass::OnAddToWorld(pWorld);
//
// If we are a node class, we must insure that we have a valid unique ID.
//
if (IsNodeClass())
{
EnsureUniqueNodeID(pWorld);
}
//
// If we have a targetname, relink all the targetname pointers in the world
// because someone might be looking for our targetname.
//
if (GetKeyValue("targetname") != NULL)
{
UpdateAllDependencies(this);
}
}
//-----------------------------------------------------------------------------
// Purpose: Called before this object is deleted from the world.
//
// Input : pWorld - The world that we have been added to.
// b
//-----------------------------------------------------------------------------
void CMapEntity::OnRemoveFromWorld(CMapWorld *pWorld, bool bNotifyChildren)
{
// Disconnect this now removed entity from the rest of the world
Connections_FixBad(false);
Upstream_FixBad();
CMapClass::OnRemoveFromWorld(pWorld, bNotifyChildren);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pObject - The object that changed.
//-----------------------------------------------------------------------------
void CMapEntity::OnNotifyDependent(CMapClass *pObject, Notify_Dependent_t eNotifyType)
{
CMapClass::OnNotifyDependent(pObject, eNotifyType);
if (eNotifyType == Notify_Removed)
{
//
// Check for our move parent going away.
//
if (pObject == m_pMoveParent)
{
CMapWorld *pWorld = (CMapWorld *)GetWorldObject(this);
const char *pszParentName = CEditGameClass::GetKeyValue("parentname");
if ((pWorld != NULL) && (pszParentName != NULL))
{
CMapEntity *pMoveParent = (CMapEntity *)UpdateDependency(m_pMoveParent, pWorld->FindEntityByName( pszParentName));
SetMoveParent( pMoveParent );
}
else
{
CMapEntity *pMoveParent = (CMapEntity *)UpdateDependency(m_pMoveParent, NULL);
SetMoveParent( pMoveParent );
}
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Iterates through an object, and all it's children, looking for an
// entity with a matching key and value
// Input : key -
// value -
// Output : Returns a pointer to the entity found.
//-----------------------------------------------------------------------------
CMapEntity *CMapEntity::FindChildByKeyValue( LPCSTR key, LPCSTR value, bool *bIsInInstance, VMatrix *InstanceMatrix )
{
if ((key == NULL) || (value == NULL))
{
return(NULL);
}
int index;
LPCSTR val = CEditGameClass::GetKeyValue(key, &index);
if ( val && value && !stricmp(value, val) )
{
return this;
}
return CMapClass::FindChildByKeyValue( key, value, bIsInInstance, InstanceMatrix );
}
//-----------------------------------------------------------------------------
// Purpose: Returns a coordinate frame to render in, if the entity is animating
// Input : matrix -
// Output : returns true if a new matrix is returned, false if it is just the identity
//-----------------------------------------------------------------------------
bool CMapEntity::GetTransformMatrix( VMatrix& matrix )
{
bool gotMatrix = false;
// if we have a move parent, get its transformation matrix
if ( m_pMoveParent )
{
if ( m_pMoveParent == this )
{
Assert( !"Recursive parenting." );
}
else
{
gotMatrix = m_pMoveParent->GetTransformMatrix( matrix );
}
}
if ( m_pAnimatorChild )
{
// return a matrix that will transform any vector into our (animated) space
if ( gotMatrix )
{
// return ParentMatrix * OurMatrix
VMatrix tmpMat, animatorMat;
bool gotAnimMatrix = m_pAnimatorChild->GetTransformMatrix( animatorMat );
if ( !gotAnimMatrix )
{
// since we didn't get a new matrix from our child just return our parent's
return true;
}
matrix = matrix * animatorMat;
}
else
{
// no parent, we're at the top of the game
gotMatrix = m_pAnimatorChild->GetTransformMatrix( matrix );
}
}
return gotMatrix;
}
//-----------------------------------------------------------------------------
// Saves editor data
//-----------------------------------------------------------------------------
ChunkFileResult_t CMapEntity::SaveEditorData(CChunkFile *pFile)
{
#ifndef SDK_BUILD
ChunkFileResult_t eResult = pFile->WriteKeyValueVector2("logicalpos", m_vecLogicalPosition);
if (eResult != ChunkFile_Ok)
return eResult;
#endif // SDK_BUILD
return BaseClass::SaveEditorData( pFile );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
ChunkFileResult_t CMapEntity::SaveVMF(CChunkFile *pFile, CSaveInfo *pSaveInfo)
{
//
// Check rules before saving this object.
//
if (!pSaveInfo->ShouldSaveObject(this))
{
return(ChunkFile_Ok);
}
ChunkFileResult_t eResult = ChunkFile_Ok;
//
// If it's a solidentity but it doesn't have any solids,
// don't save it.
//
if (!IsPlaceholder() && !m_Children.Count())
{
return(ChunkFile_Ok);
}
//
// If we are hidden, place this object inside of a hidden chunk.
//
if (!IsVisible())
{
eResult = pFile->BeginChunk("hidden");
}
//
// Begin this entity's scope.
//
eResult = pFile->BeginChunk("entity");
//
// Save the entity's ID.
//
if (eResult == ChunkFile_Ok)
{
eResult = pFile->WriteKeyValueInt("id", GetID());
}
//
// Save our keys.
//
if (eResult == ChunkFile_Ok)
{
eResult = CEditGameClass::SaveVMF(pFile, pSaveInfo);
}
//
// If this is a point entity of an unknown type or a point entity that doesn't
// declare an origin key, save our origin.
//
if (IsPlaceholder() && (!IsClass() || GetClass()->VarForName("origin") == NULL))
{
char szOrigin[80];
sprintf(szOrigin, "%g %g %g", (double)m_Origin[0], (double)m_Origin[1], (double)m_Origin[2]);
pFile->WriteKeyValue("origin", szOrigin);
}
//
// Save all our descendents.
//
eResult = ChunkFile_Ok;
EnumChildrenPos_t pos;
CMapClass *pChild = GetFirstDescendent(pos);
while ((pChild != NULL) && (eResult == ChunkFile_Ok))
{
if ( pChild->ShouldSerialize() )
{
eResult = pChild->SaveVMF(pFile, pSaveInfo);
}
pChild = GetNextDescendent(pos);
}
//
// Save our base class' information within our chunk.
//
if (eResult == ChunkFile_Ok)
{
eResult = CMapClass::SaveVMF(pFile, pSaveInfo);
}
//
// End this entity's scope.
//
if (eResult == ChunkFile_Ok)
{
pFile->EndChunk();
}
//
// End the hidden chunk if we began it.
//
if (!IsVisible())
{
eResult = pFile->EndChunk();
}
return(eResult);
}
//-----------------------------------------------------------------------------
// Purpose: Overloaded to use the color from our FGD definition.
// Output : Returns true if the color was specified by this call, false if not.
//-----------------------------------------------------------------------------
bool CMapEntity::UpdateObjectColor()
{
if (!BaseClass::UpdateObjectColor())
{
if (IsClass())
{
color32 rgbColor = m_pClass->GetColor();
SetRenderColor(rgbColor);
return true;
}
}
else
{
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pWorld -
// pObject -
//-----------------------------------------------------------------------------
void CMapEntity::UpdateDependencies(CMapWorld *pWorld, CMapClass *pObject)
{
CMapClass::UpdateDependencies(pWorld, pObject);
//
// If we have a movement parent, relink to our movement parent.
//
const char *pszParentName = CEditGameClass::GetKeyValue("parentname");
if (pszParentName != NULL)
{
CMapEntity *pMoveParent = (CMapEntity *)UpdateDependency(m_pMoveParent, pWorld->FindEntityByName( pszParentName));
SetMoveParent( pMoveParent );
}
else
{
CMapEntity *pMoveParent = (CMapEntity *)UpdateDependency(m_pMoveParent, NULL);
SetMoveParent( pMoveParent );
}
CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
if ( pDoc && !pDoc->IsLoading() )
{
// Update any downstream/upstream connections objects associated with this entity
Connections_FixBad();
Upstream_FixBad();
}
}
//-----------------------------------------------------------------------------
// Purpose: Places the entity properly on a plane surface, at a given location
// Input: pos - position on the plane
// plane - surface plane to align to
// align - alignment type (top, bottom)
// Output:
//-----------------------------------------------------------------------------
#define ALIGN_EPSILON 1 // World units
void CMapEntity::AlignOnPlane( Vector& pos, PLANE *plane, alignType_e align )
{
float fOffset = 0.0f;
Vector vecNewPos;
//Depending on the alignment type, get the offset from the surface
switch ( align )
{
case ALIGN_BOTTOM:
fOffset = m_Origin[2] - m_Render2DBox.bmins[2];
break;
case ALIGN_TOP:
fOffset = m_Render2DBox.bmaxs[2] - m_Origin[2];
break;
}
//Push our point out and away from this surface
VectorMA( pos, fOffset + ALIGN_EPSILON, plane->normal, vecNewPos );
//Update the entity and children
SetOrigin( vecNewPos );
SignalChanged();
}
//-----------------------------------------------------------------------------
// Purpose: Looks for an input with a given name in the entity list. ALL entities
// in the list must have the given input for a match to be found.
// Input : szInput - Name of the input.
// Output : Returns true if the input name was found in all entities, false if not.
//-----------------------------------------------------------------------------
bool MapEntityList_HasInput(const CMapEntityList *pList, const char *szInput, InputOutputType_t eType)
{
GDclass *pLastClass = NULL;
FOR_EACH_OBJ( *pList, pos )
{
CMapEntity *pEntity = pList->Element(pos);
GDclass *pClass = pEntity->GetClass();
if ((pClass != pLastClass) && (pClass != NULL))
{
CClassInput *pInput = pClass->FindInput(szInput);
if (!pInput)
{
return false;
}
if ((eType != iotInvalid) && (pInput->GetType() != eType))
{
return false;
}
//
// Cheap optimization to help minimize redundant checks.
//
pLastClass = pClass;
}
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Returns a pointer to the object that should be added to the selection
// list because this object was clicked on with a given selection mode.
// Input : eSelectMode -
//-----------------------------------------------------------------------------
CMapClass *CMapEntity::PrepareSelection(SelectMode_t eSelectMode)
{
//
// Select up the hierarchy when in Groups selection mode if we belong to a group.
//
if ((eSelectMode == selectGroups) && (m_pParent != NULL) && !IsWorldObject(m_pParent))
{
return GetParent()->PrepareSelection(eSelectMode);
}
//
// Don't select solid entities when in Solids selection mode. We'll select
// their solid children.
//
if ((eSelectMode == selectSolids) && !IsPlaceholder())
{
return NULL;
}
return this;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pRender -
//-----------------------------------------------------------------------------
void CMapEntity::Render2D(CRender2D *pRender)
{
// Render all our children (helpers & solids)
BaseClass::Render2D(pRender);
CMapView2D *pView = (CMapView2D*)pRender->GetView();
Vector vecMins, vecMaxs;
GetRender2DBox(vecMins, vecMaxs);
if ( pRender->GetInstanceRendering() )
{
Vector vecExpandedMins, vecExpandedMaxs;
pRender->TransformInstanceAABB( vecMins, vecMaxs, vecExpandedMins, vecExpandedMaxs );
vecMins = vecExpandedMins;
vecMaxs = vecExpandedMaxs;
}
Vector2D pt, pt2;
pView->WorldToClient(pt, vecMins);
pView->WorldToClient(pt2, vecMaxs);
color32 rgbColor = GetRenderColor( pRender );
pRender->SetDrawColor( rgbColor.r, rgbColor.g, rgbColor.b );
// Render the entity's name and class name if enabled.
if (s_bShowEntityNames && pView->GetZoom() >= 1)
{
pRender->SetTextColor( rgbColor.r, rgbColor.g, rgbColor.b );
const char *pszTargetName = GetKeyValue("targetname");
if (pszTargetName != NULL)
{
pRender->DrawText(pszTargetName, pt.x, pt.y + 2, CRender2D::TEXT_JUSTIFY_BOTTOM );
}
const char *pszClassName = GetClassName();
if (pszClassName != NULL)
{
pRender->DrawText(pszClassName, pt.x, pt2.y - 2, CRender2D::TEXT_JUSTIFY_TOP );
}
}
//
// Draw the connections between entities and their targets if enabled.
//
if (s_bShowEntityConnections)
{
LPCTSTR pszTarget = GetKeyValue("target");
if (pszTarget != NULL)
{
CMapWorld *pWorld = GetWorldObject(this);
MDkeyvalue kv("targetname", pszTarget);
CMapObjectList FoundEntitiesTarget;
FoundEntitiesTarget.RemoveAll();
pWorld->EnumChildren((ENUMMAPCHILDRENPROC)FindKeyValue, (DWORD)&kv, MAPCLASS_TYPE(CMapEntity));
Vector vCenter1,vCenter2;
GetBoundsCenter( vCenter1 );
FOR_EACH_OBJ( FoundEntitiesTarget, p )
{
CMapClass *pEntity = (CMapEntity *)FoundEntitiesTarget.Element(p);
pEntity->GetBoundsCenter(vCenter2);
pRender->DrawLine( vCenter1, vCenter2 );
}
}
}
// Draw the forward vector if we have an "angles" key and we're selected.
// HACK: don't draw the forward vector for lights, they negate pitch. The model helper will handle it.
if ((GetSelectionState() != SELECT_NONE) &&
(!GetClassName() || (strnicmp(GetClassName(), "light_", 6) != 0)) &&
(GetKeyValue("angles") != NULL))
{
Vector vecOrigin;
GetOrigin(vecOrigin);
QAngle vecAngles;
GetAngles(vecAngles);
Vector vecForward;
AngleVectors(vecAngles, &vecForward);
pRender->SetDrawColor( 255, 255, 0 );
pRender->DrawLine(vecOrigin, vecOrigin + vecForward * 24);
}
}
//-----------------------------------------------------------------------------
// Gets the 2D logical view bounding box
//-----------------------------------------------------------------------------
void CMapEntity::GetRenderLogicalBox( Vector2D &mins, Vector2D &maxs )
{
mins.x = m_vecLogicalPosition.x;
maxs.x = m_vecLogicalPosition.x + LOGICAL_BOX_WIDTH + LOGICAL_BOX_CONNECTOR_INPUT_WIDTH + LOGICAL_BOX_CONNECTOR_OUTPUT_WIDTH;
mins.y = m_vecLogicalPosition.y;
maxs.y = m_vecLogicalPosition.y + LOGICAL_BOX_HEIGHT;
}
//-----------------------------------------------------------------------------
// Logical position accessor
//-----------------------------------------------------------------------------
const Vector2D& CMapEntity::GetLogicalPosition( )
{
return m_vecLogicalPosition;
}
void CMapEntity::SetLogicalPosition( const Vector2D &vecPosition )
{
m_vecLogicalPosition = vecPosition;
}
//-----------------------------------------------------------------------------
// Returns a logical position
//-----------------------------------------------------------------------------
void CMapEntity::GetLogicalConnectionPosition( LogicalConnection_t i, Vector2D &vecPosition )
{
Vector2D vecMins, vecMaxs;
GetRenderLogicalBox( vecMins, vecMaxs );
vecPosition.y = ( vecMins.y + vecMaxs.y ) * 0.5f;
if ( i == LOGICAL_CONNECTION_INPUT )
{
vecPosition.x = vecMins.x;
}
else
{
vecPosition.x = vecMaxs.x;
}
}
//-----------------------------------------------------------------------------
// Renders into the logical view
//-----------------------------------------------------------------------------
void CMapEntity::RenderLogical( CRender2D *pRender )
{
// Render all our children (helpers & solids)
BaseClass::RenderLogical(pRender);
Vector2D vecMins, vecMaxs;
GetRenderLogicalBox( vecMins, vecMaxs );
Vector2D vecBoxMins = vecMins;
Vector2D vecBoxMaxs = vecMaxs;
vecBoxMins.x += LOGICAL_BOX_CONNECTOR_INPUT_WIDTH;
vecBoxMaxs.x -= LOGICAL_BOX_CONNECTOR_OUTPUT_WIDTH;
// Define the entity highlight/lowlight edges
Vector2D vecInnerMins = vecBoxMins, vecInnerMaxs = vecBoxMaxs;
vecInnerMins.x += LOGICAL_BOX_INNER_OFFSET;
vecInnerMins.y += LOGICAL_BOX_INNER_OFFSET;
vecInnerMaxs.x -= LOGICAL_BOX_INNER_OFFSET;
vecInnerMaxs.y -= LOGICAL_BOX_INNER_OFFSET;
// Get the entity render color
color32 rgbColor = GetRenderColor( pRender );
color32 rgbHighlight = {(byte)(7*rgbColor.r/8), (byte)(7*rgbColor.g/8), (byte)(7*rgbColor.b/8), (byte)255 };
color32 rgbLowlight = { (byte)(5*rgbColor.r/8), (byte)(5*rgbColor.g/8), (byte)(5*rgbColor.b/8), (byte)255 };
color32 rgbEdgeColor = { (byte)(3*rgbColor.r/8), (byte)(3*rgbColor.g/8), (byte)(3*rgbColor.b/8), (byte)255 };
color32 rgbInterior = { (byte)(2*rgbColor.r/8), (byte)(2*rgbColor.g/8), (byte)(2*rgbColor.b/8), (byte)255 };
// Draw an inside UpperLeft highlight rect (leading edge highlight)
pRender->SetDrawColor( rgbHighlight.r, rgbHighlight.g, rgbHighlight.b );
pRender->DrawRectangle( Vector( vecBoxMins.x, vecBoxMins.y, 0.0f ), Vector( vecBoxMaxs.x, vecBoxMaxs.y, 0.0f ), true, 0 );
// Draw an inside LowerRight lowlight rect (trailing edge lowlight)
pRender->SetDrawColor( rgbLowlight.r, rgbLowlight.g, rgbLowlight.b );
pRender->DrawRectangle( Vector( vecInnerMins.x, vecBoxMins.y, 0.0f ), Vector( vecBoxMaxs.x, vecInnerMaxs.y, 0.0f ), true, 0 );
// Draw an outside border rect in the entities render color
pRender->SetDrawColor( rgbEdgeColor.r, rgbEdgeColor.g, rgbEdgeColor.b );
pRender->DrawRectangle( Vector( vecBoxMins.x, vecBoxMins.y, 0.0f ), Vector( vecBoxMaxs.x, vecBoxMaxs.y, 0.0f ), false, 0 );
// Draw the small diagonals connecting the outer and inner corners
pRender->DrawLine( Vector( vecBoxMins.x, vecBoxMins.y, 0.0f ), Vector( vecBoxMaxs.x, vecBoxMaxs.y, 0.0f ) );
pRender->DrawLine( Vector( vecBoxMins.x, vecBoxMaxs.y, 0.0f ), Vector( vecBoxMaxs.x, vecBoxMins.y, 0.0f ) );
// Draw interior background first
pRender->SetDrawColor( rgbInterior.r, rgbInterior.g, rgbInterior.b );
pRender->DrawRectangle( Vector( vecInnerMins.x, vecInnerMins.y, 0.0f ), Vector( vecInnerMaxs.x, vecInnerMaxs.y, 0.0f ), true, 0 );
// Draws the sprite helper(s) (if it has them)
bool bFoundSpriteHelper = false;
FOR_EACH_OBJ( m_Children, pos )
{
CMapSprite *pSprite = dynamic_cast<CMapSprite*>( m_Children[pos] );
if ( pSprite )
{
// Render the sprite on top of the background
pSprite->RenderLogicalAt( pRender, vecInnerMins, vecInnerMaxs );
bFoundSpriteHelper = true;
}
}
// Fill in the interior with entity color if no sprite was found
if ( !bFoundSpriteHelper )
{
// Redraw the interior with the entity's render color
pRender->SetDrawColor( rgbColor.r, rgbColor.g, rgbColor.b );
pRender->DrawRectangle( Vector( vecInnerMins.x, vecInnerMins.y, 0.0f ), Vector( vecInnerMaxs.x, vecInnerMaxs.y, 0.0f ), true, 0 );
// Put an inner border around the entity color block
pRender->SetDrawColor( rgbEdgeColor.r, rgbEdgeColor.g, rgbEdgeColor.b );
pRender->DrawRectangle( Vector( vecInnerMins.x, vecInnerMins.y, 0.0f ), Vector( vecInnerMaxs.x, vecInnerMaxs.y, 0.0f ), false, 0 );
}
// Draw the rest of the entity in the entity color
pRender->SetDrawColor( rgbColor.r, rgbColor.g, rgbColor.b );
// Draws the connectors
float flConnectorY = ( vecMins.y + vecMaxs.y ) * 0.5f;
pRender->DrawCircle( Vector( vecMins.x + LOGICAL_BOX_CONNECTOR_RADIUS, flConnectorY, 0.0f ), LOGICAL_BOX_CONNECTOR_RADIUS );
pRender->MoveTo( Vector( vecMins.x + 2 * LOGICAL_BOX_CONNECTOR_RADIUS, flConnectorY, 0.0f ) );
pRender->DrawLineTo( Vector( vecBoxMins.x, flConnectorY, 0.0f ) );
pRender->MoveTo( Vector( vecBoxMaxs.x, flConnectorY, 0.0f ) );
pRender->DrawLineTo( Vector( vecMaxs.x - LOGICAL_BOX_ARROW_LENGTH, flConnectorY, 0.0f ) );
pRender->DrawLineTo( Vector( vecMaxs.x - LOGICAL_BOX_ARROW_LENGTH, flConnectorY + LOGICAL_BOX_ARROW_HEIGHT, 0.0f ) );
pRender->DrawLineTo( Vector( vecMaxs.x, flConnectorY, 0.0f ) );
pRender->DrawLineTo( Vector( vecMaxs.x - LOGICAL_BOX_ARROW_LENGTH, flConnectorY - LOGICAL_BOX_ARROW_HEIGHT, 0.0f ) );
pRender->DrawLineTo( Vector( vecMaxs.x - LOGICAL_BOX_ARROW_LENGTH, flConnectorY, 0.0f ) );
// Stop drawing the text once the entity itself gets too small.
Vector2D pt, pt2;
pRender->GetView()->WorldToClient( pt, Vector( vecBoxMins.x, vecBoxMins.y, 0.0f ) );
pRender->GetView()->WorldToClient( pt2, Vector( vecBoxMaxs.x, vecBoxMaxs.y, 0.0f ) );
if ( fabs( pt.y - pt2.y ) < 32 )
return;
// Render the entity's name and class name if enabled.
pRender->SetTextColor( rgbColor.r, rgbColor.g, rgbColor.b );
// Draw the inputs and outputs
const char *pszTargetName = GetKeyValue("targetname");
if (pszTargetName != NULL)
{
pRender->DrawText( pszTargetName, Vector2D( (vecMins.x+vecMaxs.x)/2, vecMaxs.y ), 0, -1, CRender2D::TEXT_JUSTIFY_TOP | CRender2D::TEXT_JUSTIFY_HORZ_CENTER );
}
if ( fabs( pt.y - pt2.y ) < 50 )
return;
const char *pszClassName = GetClassName();
if (pszClassName != NULL)
{
pRender->DrawText( pszClassName, Vector2D( (vecMins.x+vecMaxs.x)/2, vecMins.y ), 0, 1, CRender2D::TEXT_JUSTIFY_BOTTOM | CRender2D::TEXT_JUSTIFY_HORZ_CENTER );
}
}
//-----------------------------------------------------------------------------
// Purpose: Returns whether this entity snaps to half grid or not. Some entities,
// such as hinges, need to snap to a 0.5 grid to center on geometry.
//-----------------------------------------------------------------------------
bool CMapEntity::ShouldSnapToHalfGrid()
{
return (GetClass() && GetClass()->ShouldSnapToHalfGrid());
}
//-----------------------------------------------------------------------------
// Purpose: Returns the integer value of the nodeid key of this entity.
//-----------------------------------------------------------------------------
int CMapEntity::GetNodeID(void)
{
int nNodeID = 0;
const char *pszNodeID = GetKeyValue("nodeid");
if (pszNodeID)
{
nNodeID = atoi(pszNodeID);
}
return nNodeID;
}
//-----------------------------------------------------------------------------
// Purpose: Returns whether this object should be hidden based on the given
// cordon bounds.
// Output : Returns true to cull the object, false to keep it.
//-----------------------------------------------------------------------------
bool CMapEntity::IsCulledByCordon(const Vector &vecMins, const Vector &vecMaxs)
{
// Point entities are culled by their origin, not by their bounding box.
// An exception to that is swept hulls, such as ladders, that are more like solid ents.
if ( IsPointClass() && !IsSweptHullClass( this ) )
{
Vector vecOrigin;
GetOrigin(vecOrigin);
return !IsPointInBox(vecOrigin, vecMins, vecMaxs);
}
return !IsIntersectingBox(vecMins, vecMaxs);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pView -
// vecPoint -
// nHitData -
// Output :
//-----------------------------------------------------------------------------
bool CMapEntity::HitTest2D(CMapView2D *pView, const Vector2D &point, HitInfo_t &HitData)
{
if ( !IsVisible() )
return false;
if ( BaseClass::HitTest2D(pView, point, HitData) )
return true;
//
// Only check point entities; brush entities are selected via their brushes.
//
if ( !IsPointClass() )
return false;
// First check center X.
Vector vecCenter, vecViewPoint;
GetBoundsCenter(vecCenter);
Vector2D vecClientCenter;
pView->WorldToClient(vecClientCenter, vecCenter);
pView->GetCamera()->GetViewPoint( vecViewPoint );
HitData.pObject = this;
HitData.nDepth = vecViewPoint[pView->axThird]-vecCenter[pView->axThird];
if ( pView->CheckDistance( point, vecClientCenter, HANDLE_RADIUS) )
{
HitData.uData = 0;
return true;
}
else if (!Options.view2d.bSelectbyhandles)
{
//
// See if any edges of the bbox are within a certain distance from the the point.
//
int iSelUnits = 2;
int x1 = point.x - iSelUnits;
int x2 = point.x + iSelUnits;
int y1 = point.y - iSelUnits;
int y2 = point.y + iSelUnits;
Vector vecMins;
Vector vecMaxs;
GetRender2DBox(vecMins, vecMaxs);
Vector2D vecClientMins;
Vector2D vecClientMaxs;
pView->WorldToClient(vecClientMins, vecMins);
pView->WorldToClient(vecClientMaxs, vecMaxs);
Vector2D vecEdges[4] =
{
Vector2D(vecClientMins.x, vecClientMins.y),
Vector2D(vecClientMaxs.x, vecClientMins.y),
Vector2D(vecClientMaxs.x, vecClientMaxs.y),
Vector2D(vecClientMins.x, vecClientMaxs.y),
};
for (int i = 0; i < 4; i++)
{
if (IsLineInside(vecEdges[i], vecEdges[(i + 1) % 4], x1, y1, x2, y2))
{
HitData.uData = i+1;
return true;
}
}
}
return false;
}
//-----------------------------------------------------------------------------
// Hit test for the logical view
//-----------------------------------------------------------------------------
bool CMapEntity::HitTestLogical( CMapViewLogical *pView, const Vector2D &vecPoint, HitInfo_t &hitData )
{
if ( !IsVisible() || !IsLogical() || !IsVisibleLogical() )
return false;
if ( BaseClass::HitTestLogical( pView, vecPoint, hitData ) )
return true;
// Is the point inside the box?
Vector2D vecMins;
Vector2D vecMaxs;
GetRenderLogicalBox( vecMins, vecMaxs );
Vector2D vecClientMins;
Vector2D vecClientMaxs;
pView->WorldToClient(vecClientMins, vecMins);
pView->WorldToClient(vecClientMaxs, vecMaxs);
NormalizeBox( vecClientMins, vecClientMaxs );
if ( IsPointInside( vecPoint, vecClientMins, vecClientMaxs ) )
{
hitData.pObject = this;
hitData.uData = 0;
hitData.nDepth = 0.0f;
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Is this logical?
//-----------------------------------------------------------------------------
bool CMapEntity::IsLogical(void)
{
GDclass *pClass = GetClass();
return pClass && (( pClass->GetInputCount() > 0 ) || ( pClass->GetOutputCount() > 0 )) || (m_Connections.Count() || m_Upstream.Count());
}
//-----------------------------------------------------------------------------
// Is it visible in the logical view?
//-----------------------------------------------------------------------------
bool CMapEntity::IsVisibleLogical(void)
{
return IsVisible();
}
//-----------------------------------------------------------------------------
// Purpose: Returns true if this entity's name matches the given name, considering
// wildcards.
// Input : szName -
//-----------------------------------------------------------------------------
bool CMapEntity::NameMatches(const char *szName)
{
const char *pszTargetName = GetKeyValue( "targetname" );
if (pszTargetName)
{
return !CompareEntityNames(pszTargetName, szName);
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Returns true if this entity's classname matches the given name, considering
// wildcards.
// Input : szName -
//-----------------------------------------------------------------------------
bool CMapEntity::ClassNameMatches(const char *szName)
{
const char *pszClassName = GetClassName();
if (pszClassName)
{
return !CompareEntityNames(pszClassName, szName);
}
return false;
}
|