summaryrefslogtreecommitdiffstats
path: root/editorlib/src/editorscene.cpp
blob: 6bf2a48ce1a5daedca241971c71c6f5628317341 (plain)
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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D Editor of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "editorscene.h"
#include "editorutils.h"
#include "editorsceneitem.h"
#include "editorsceneparser.h"
#include "editorsceneitemcomponentsmodel.h"
#include "editorviewportitem.h"
#include "ontopeffect.h"
#include "undohandler.h"

#include <Qt3DCore/QTransform>
#include <Qt3DRender/QMesh>
#include <Qt3DExtras/QCuboidMesh>
#include <Qt3DExtras/QDiffuseSpecularMapMaterial>
#include <Qt3DExtras/QPhongAlphaMaterial>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DRender/QDirectionalLight>
#include <Qt3DRender/QSpotLight>
#include <Qt3DRender/QPointLight>
#include <Qt3DRender/QParameter>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QCameraLens>
#include <Qt3DRender/QTexture>
#include <Qt3DRender/QRenderSettings>
#include <Qt3DExtras/QForwardRenderer>
#include <Qt3DRender/QObjectPicker>
#include <Qt3DRender/QPickEvent>
#include <Qt3DRender/QPickingSettings>

#include <Qt3DInput/QInputSettings>

#include <QtGui/QGuiApplication>
#include <QtGui/QWindow>
#include <QtGui/QKeySequence>
#include <QtGui/QMatrix4x4>

#include <QtCore/QDir>
#include <QtCore/QLibraryInfo>
#include <QtCore/QCoreApplication>
#include <QtCore/QtMath>

#include <QtQml/QQmlEngine>

#include <cfloat>

//#define TEST_SCENE // If a test scene is wanted instead of the default scene

#ifdef TEST_SCENE
#include <Qt3DRender/QCylinderMesh>
#include <Qt3DRender/QNormalDiffuseSpecularMapMaterial>
#endif

static const QString cameraVisibleEntityName = QStringLiteral("__internal camera visible entity");
static const QString lightVisibleEntityName = QStringLiteral("__internal light visible entity");
static const QString sceneLoaderSubEntityName = QStringLiteral("__internal sceneloader sub entity");
static const QString autoSavePostfix = QStringLiteral(".autosave");
static const QVector3D defaultLightDirection(0.0f, -1.0f, 0.0f);
static const float freeViewCameraNearPlane = 0.1f;
static const float freeViewCameraFarPlane = 10000.0f;
static const float freeViewCameraFov = 45.0f;
static const int dragCornerHandleCount = 8; // One handle for each selection box corner
static const QColor selectionBoxColor("#43adee");
static const QColor cameraFrustumColor("#c22555");
static const QColor helperPlaneColor("#585a5c");

EditorScene::EditorScene(QObject *parent)
    : QObject(parent)
    , m_rootEntity(nullptr)
    , m_componentCache(nullptr)
    , m_rootItem(nullptr)
    , m_sceneModel(new EditorSceneItemModel(this))
    , m_sceneParser(new EditorSceneParser(this))
    , m_renderSettings(nullptr)
    , m_renderer(nullptr)
    , m_sceneEntity(nullptr)
    , m_sceneEntityItem(nullptr)
    , m_selectedEntity(nullptr)
    , m_selectedEntityTransform(nullptr)
    , m_activeSceneCameraIndex(-1)
    , m_freeView(false)
    , m_freeViewCameraEntity(nullptr)
    , m_viewport(nullptr)
    , m_undoHandler(new UndoHandler(this))
    , m_helperPlane(nullptr)
    , m_helperPlaneTransform(nullptr)
    , m_meshCenterIndicatorLine(nullptr)
    , m_meshCenterIndicatorLineTransform(nullptr)
    , m_qtTranslator(new QTranslator(this))
    , m_appTranslator(new QTranslator(this))
    , m_dragHandlesTransform(nullptr)
    , m_dragHandleRotateTransform(nullptr)
    , m_dragHandleTranslateTransform(nullptr)
    , m_dragHandleIndex(0)
    , m_dragMode(DragNone)
    , m_ignoringInitialDrag(true)
    , m_viewCenterLocked(false)
    , m_pickedEntity(nullptr)
    , m_pickedDistance(-1.0f)
    , m_gridSize(3)
    , m_duplicateCount(0)
    , m_previousDuplicate(nullptr)
    , m_ctrlDownOnLastLeftPress(false)
    , m_clipboardOperation(ClipboardNone)
    , m_groupBoxUpdatePending(false)
{
    setLanguage(language());
    retranslateUi();
    createRootEntity();
    setupDefaultScene();

    // Install event filter to handle undo/redo globally, instead of each TextField having
    // their own stack.
    // TODO: This might need to be done differently if we make this Creator plugin
    qGuiApp->installEventFilter(this);
}

EditorScene::~EditorScene()
{
    // Remove all entities recursively to ensure the root item is last one to be deleted
    removeEntity(m_sceneEntity);

    // TODO: Check if it is necessary to delete rootentity and associated components, or do they get
    // TODO: properly deleted by aspect engine shutdown?

    delete m_componentCache;

    delete m_dragHandlesTransform;
    delete m_dragHandleRotateTransform;
    delete m_dragHandleTranslateTransform;
    qDeleteAll(m_dragHandleScaleTransforms);
}

EditorSceneItem *EditorScene::entityItem(Qt3DCore::QEntity *entity) const
{
    return m_sceneItems.value(entity->id());
}

void EditorScene::addEntity(Qt3DCore::QEntity *entity, int index, Qt3DCore::QEntity *parent)
{
    if (entity == nullptr)
        return;

    if (parent == nullptr) {
        //make sure that entity has a parent, otherwise make its parent the root entity
        if (entity->parentEntity() == nullptr)
            entity->setParent(m_rootEntity);
    } else if (entity->parentEntity() != parent) {
        entity->setParent(parent);
    }

    EditorSceneItem *item = m_sceneItems.value(entity->id(), nullptr);
    if (!item) {
        item = new EditorSceneItem(this, entity, m_sceneItems.value(entity->parentEntity()->id(),
                                                                    nullptr), index, this);

        if (entity == m_sceneEntity)
            m_sceneEntityItem = item;

        m_sceneItems.insert(entity->id(), item);
        connect(entity, &QObject::objectNameChanged,
                this, &EditorScene::handleEntityNameChange);

        Qt3DRender::QCamera *camera = qobject_cast<Qt3DRender::QCamera *>(entity);
        if (camera)
            handleCameraAdded(camera);
        else if (item->itemType() == EditorSceneItem::Light)
            handleLightAdded(entity);
        else if (entity->isEnabled() && item->itemType() != EditorSceneItem::SceneLoader)
            createObjectPickerForEntity(entity);
        // Note: Scene loader pickers are created asynchronously after scene is loaded fully

        item->componentsModel()->initializeModel();
    }

    if (item->itemType() != EditorSceneItem::SceneLoader) {
        foreach (QObject *child, entity->children()) {
            Qt3DCore::QEntity *childEntity = qobject_cast<Qt3DCore::QEntity *>(child);
            if (childEntity)
                addEntity(childEntity);
        }
    }
}

// Removed entity is deleted
void EditorScene::removeEntity(Qt3DCore::QEntity *entity)
{
    if (entity == nullptr || entity == m_rootEntity)
        return;

    if (entity->objectName() == m_clipboardEntityName)
        setClipboardOperation(ClipboardNone);

    if (entity == m_sceneEntity) {
        m_sceneEntity = nullptr;
        m_sceneEntityItem = nullptr;
    }

    disconnect(entity, 0, this, 0);

    EditorSceneItem *item = m_sceneItems.value(entity->id());

    if (item->itemType() != EditorSceneItem::SceneLoader) {
        foreach (QObject *child, entity->children()) {
            Qt3DCore::QEntity *childEntity = qobject_cast<Qt3DCore::QEntity *>(child);
            removeEntity(childEntity);
        }
    }

    Qt3DRender::QCamera *camera = qobject_cast<Qt3DRender::QCamera *>(entity);
    if (camera)
        handleCameraRemoved(camera);

    if (item && item->itemType() == EditorSceneItem::Light)
        handleLightRemoved(entity);

    m_sceneItems.remove(entity->id());

    if (m_selectedEntity == entity || multiSelection())
        queueEnsureSelection();

    queueUpdateGroupSelectionBoxes();

    delete item;
    delete entity;
}

void EditorScene::resetScene()
{
    clearSingleSelection();

    // Clear the existing scene
    setFrameGraphCamera(nullptr);
    m_undoHandler->clear();
    clearSceneCamerasAndLights();
    removeEntity(m_sceneEntity);

    // Create new scene root
    setSceneEntity();

    // Set up default scene
    setupDefaultScene();

    // Set other defaults
    setActiveSceneCameraIndex(0);
    m_freeView = true;
    resetFreeViewCamera();
    setFrameGraphCamera(m_freeViewCameraEntity);
    enableVisibleCameras(m_freeView);
    enableVisibleLights(m_freeView);

    emit freeViewChanged(m_freeView);

    // Reset entity tree
    m_sceneModel->clearExpandedItems();
    m_sceneModel->resetModel();

    setSelection(m_sceneEntity);
}

bool EditorScene::saveScene(const QUrl &fileUrl, bool autosave)
{
    QUrl url = fileUrl;
    if (!url.toString().endsWith(QStringLiteral(".qt3d.qrc"))) {
        QString filePath = url.toString() + QStringLiteral(".qt3d.qrc");
        url.setUrl(filePath);
    }

    Qt3DCore::QEntity *camera = nullptr;
    if (m_activeSceneCameraIndex >= 0 && m_activeSceneCameraIndex < m_sceneCameras.size())
        camera = m_sceneCameras.at(m_activeSceneCameraIndex).cameraEntity;
    bool retval = m_sceneParser->exportQmlScene(m_sceneEntity, url, camera, autosave);
    if (retval)
        m_undoHandler->setClean();
    else
        setError(m_saveFailString);
    return retval;
}

bool EditorScene::loadScene(const QUrl &fileUrl)
{
    Qt3DCore::QEntity *camera = nullptr;
    Qt3DCore::QEntity *newSceneEntity = m_sceneParser->importQmlScene(fileUrl, camera);

    if (newSceneEntity) {
        clearSingleSelection();
        if (!m_freeView)
            setFrameGraphCamera(nullptr);
        m_undoHandler->clear();
        clearSceneCamerasAndLights();
        removeEntity(m_sceneEntity);
        m_sceneEntity = newSceneEntity;
        addEntity(newSceneEntity);
        enableVisibleCameras(m_freeView);
        enableVisibleLights(m_freeView);
        m_activeSceneCameraIndex--; // To force change
        setActiveSceneCameraIndex(cameraIndexForEntity(camera));

        m_sceneModel->clearExpandedItems();
        m_sceneModel->resetModel();
    } else {
        setError(m_loadFailString);
    }

    return bool(newSceneEntity);
}

void EditorScene::deleteScene(const QUrl &fileUrl, bool autosave)
{
    // Remove qml file
    QString fileName = fileUrl.toLocalFile();
    if (autosave)
        fileName.append(autoSavePostfix);
    QFile::remove(fileName);

    // Remove resource directory
    QString qmlFinalFileAbsoluteFilePath = fileUrl.toLocalFile();
    QFile qmlFinalFile(qmlFinalFileAbsoluteFilePath);
    QFileInfo qmlFinalFileInfo(qmlFinalFile);
    QString resourceDirName = qmlFinalFileInfo.baseName() + QStringLiteral("_scene_res");
    if (autosave)
        resourceDirName.append(autoSavePostfix);
    QDir dir = QDir(resourceDirName);
    dir.removeRecursively();
}

QString EditorScene::cameraName(int index) const
{
    if (m_sceneCameras.size() > index)
        return m_sceneCameras.at(index).cameraEntity->objectName();
    else
        return QString();
}

void EditorScene::resetFreeViewCamera()
{
    if (m_viewport)
        m_freeViewCameraEntity->setAspectRatio(m_viewport->width() / qMax(m_viewport->height(), 1.0));
    else
        m_freeViewCameraEntity->setAspectRatio(16.0f / 9.0f);
    m_freeViewCameraEntity->setBottom(-0.5f);
    m_freeViewCameraEntity->setFarPlane(freeViewCameraFarPlane);
    m_freeViewCameraEntity->setFieldOfView(freeViewCameraFov);
    m_freeViewCameraEntity->setLeft(-0.5f);
    m_freeViewCameraEntity->setNearPlane(freeViewCameraNearPlane);
    m_freeViewCameraEntity->setPosition(QVector3D(20.0f, 20.0f, 20.0f));
    m_freeViewCameraEntity->setProjectionType(Qt3DRender::QCameraLens::PerspectiveProjection);
    m_freeViewCameraEntity->setRight(0.5f);
    m_freeViewCameraEntity->setTop(0.5f);
    m_freeViewCameraEntity->setUpVector(QVector3D(0, 1, 0));
    m_freeViewCameraEntity->setViewCenter(QVector3D(0, 0, 0));
}

void EditorScene::snapFreeViewCameraToActiveSceneCamera()
{
    // Set the freeview camera position and viewCenter to the active scene camera values
    Qt3DRender::QCamera *activeCam = m_sceneCameras.at(m_activeSceneCameraIndex).cameraEntity;
    m_freeViewCameraEntity->setViewCenter(activeCam->viewCenter());
    m_freeViewCameraEntity->setPosition(activeCam->position());
    // Need to reset upVector as well, as camera controls will keep updating it to actual
    // value, which won't work anymore if you move both camera viewCenter and position.
    m_freeViewCameraEntity->setUpVector(QVector3D(0, 1, 0));
}

QString EditorScene::duplicateEntity(Qt3DCore::QEntity *entity)
{
    QString duplicateName;

    if (m_previousDuplicate != entity) {
        m_duplicateCount = 0;
        m_previousDuplicate = entity;
    }

    QVector3D duplicateOffset =
            m_helperPlaneTransform->rotation().rotatedVector(QVector3D(0.5f, 0.5f, 0.0f)
                                                             * ++m_duplicateCount);

    Qt3DCore::QEntity *newEntity =
            m_sceneModel->duplicateEntity(entity, entity->parentEntity(), duplicateOffset);

    // Set name and add to scene
    duplicateName = EditorUtils::nameDuplicate(newEntity, entity, m_sceneModel);
    addEntity(newEntity);

    // Refresh entity tree
    m_sceneModel->resetModel();

    return duplicateName;
}

Qt3DRender::QCamera *EditorScene::inputCamera() const
{
    Qt3DRender::QCamera *inputCamera = nullptr;

    if (m_freeView)
        inputCamera =  m_freeViewCameraEntity;

    return inputCamera;
}

// Resolves a world position for given viewport position.
// The world position is the intersection of the eye ray at specified position and the active
// helper plane. If there is no intersection, (0, 0, 0) position is returned.
QVector3D EditorScene::getWorldPosition(int xPos, int yPos)
{
    QVector3D retVec;
    if (xPos >= 0 && yPos >= 0 && xPos < m_viewport->width() && yPos < m_viewport->height()) {
        QPoint pos(xPos, yPos);
        Qt3DRender::QCamera *camera = frameGraphCamera();
        if (camera) {
            QVector3D planeOrigin;
            QVector3D planeNormal = helperPlaneNormal();
            float cosAngle = QVector3D::dotProduct(planeOrigin.normalized(), planeNormal);
            float planeOffset = planeOrigin.length() * cosAngle;

            QVector3D ray = EditorUtils::unprojectRay(camera->viewMatrix(), camera->projectionMatrix(),
                                                      m_viewport->width(), m_viewport->height(),
                                                      pos);
            float t = 0.0f;
            QVector3D intersection = EditorUtils::findIntersection(camera->position(), ray,
                                                                   planeOffset, planeNormal, t);
            if (t > camera->nearPlane())
                retVec = intersection;
        }
    }

    return retVec;
}

// For some reason EditorUtils::InsertableEntities doesn't work as parameter type from QML here,
// so we use int and cast it.
void EditorScene::showPlaceholderEntity(const QString &name, int type)
{
    PlaceholderEntityData *data = m_placeholderEntityMap.value(name);
    EditorUtils::InsertableEntities insertableType = EditorUtils::InsertableEntities(type);
    if (!data) {
        data = new PlaceholderEntityData();
        data->entity = new Qt3DCore::QEntity(m_rootEntity);
        data->transform = new Qt3DCore::QTransform();
        Qt3DExtras::QPhongAlphaMaterial *material = new Qt3DExtras::QPhongAlphaMaterial();
        if (insertableType == EditorUtils::InsertableEntities::GroupEntity) {
            material->setAlpha(0.2f);
            material->setAmbient(selectionBoxColor);
        } else {
            material->setAlpha(0.4f);
            material->setAmbient("#53adee");
        }
        data->material = material;
        data->entity->addComponent(data->transform);
        data->entity->addComponent(material);
        m_placeholderEntityMap.insert(name, data);
    }

    if (data->type != insertableType) {
        data->type = insertableType;
        delete data->mesh;
        data->mesh = EditorUtils::createMeshForInsertableType(insertableType);
        if (!data->mesh) {
            if (insertableType == EditorUtils::LightEntity)
                data->mesh = EditorUtils::createLightMesh(EditorUtils::LightPoint);
            else if (insertableType == EditorUtils::CameraEntity)
                data->mesh = EditorUtils::createVisibleCameraMesh();
        }
        if (data->mesh)
            data->entity->addComponent(data->mesh);
    }

    data->transform->setTranslation(QVector3D());
    data->entity->setEnabled(true);
}

void EditorScene::movePlaceholderEntity(const QString &name, const QVector3D &worldPos)
{
    PlaceholderEntityData *data = m_placeholderEntityMap.value(name);
    if (data)
        data->transform->setTranslation(worldPos);
}

void EditorScene::hidePlaceholderEntity(const QString &name)
{
    PlaceholderEntityData *data = m_placeholderEntityMap.value(name);
    if (data)
        data->entity->setEnabled(false);
}

void EditorScene::destroyPlaceholderEntity(const QString &name)
{
    PlaceholderEntityData *data = m_placeholderEntityMap.value(name);
    if (data) {
        delete data->entity;
        delete data;
    }
}

void EditorScene::dragHandlePress(EditorScene::DragMode dragMode, const QPoint &pos, int handleIndex)
{
    Q_ASSERT(handleIndex >= 0 && handleIndex < m_dragHandleScaleTransforms.size());

    cancelDrag();
    m_previousMousePosition = pos;
    EditorSceneItem *selectedItem = m_sceneItems.value(m_selectedEntity->id(), nullptr);
    if (selectedItem) {
        m_dragHandleIndex = handleIndex;
        if (dragMode == DragTranslate && m_dragHandleTranslateTransform->isEnabled()) {
            Qt3DRender::QCamera *cameraEntity =
                    qobject_cast<Qt3DRender::QCamera *>(m_selectedEntity);
            if (cameraEntity) {
                if (m_cameraViewCenterSelected && !m_viewCenterLocked)
                    m_dragInitialWorldTranslationValue = cameraEntity->viewCenter();
                else
                    m_dragInitialWorldTranslationValue = cameraEntity->position();
            } else {
                if (handleIndex) {
                    m_dragInitialWorldTranslationValue = m_dragHandlesTransform->translation();
                } else {
                    m_dragInitialWorldTranslationValue = m_dragHandlesTransform->matrix()
                            * m_dragHandleTranslateTransform->matrix() * QVector3D();
                }
            }
            m_dragInitialEntityTranslationValue = m_selectedEntityTransform->translation();

            // Calculate snap point offset in world coordinates
            for (int i = 0; i < dragCornerHandleCount; ++i) {
                if (cameraEntity) {
                    m_dragEntitySnapOffsets[i] = QVector3D();
                } else {
                    QVector3D centerHandleAdj = handleIndex
                            ? selectedItem->entityMeshCenter()
                              * m_selectedEntityTransform->scale3D()
                              / selectedItem->selectionTransform()->scale3D()
                            : QVector3D();
                    QVector3D snapPos = (selectedItem->selectionTransform()->matrix()
                                         * (m_dragHandleCornerAdjustments.at(i)
                                            * QVector3D(0.5f, 0.5f, 0.5f) + centerHandleAdj));
                    snapPos -= selectedItem->selectionBoxCenter();
                    m_dragEntitySnapOffsets[i] = snapPos;
                }
            }

            m_dragEntity = m_selectedEntity;
            m_dragMode = DragTranslate;
        } else if (dragMode == DragRotate && m_dragHandleRotateTransform->isEnabled()) {
            Qt3DRender::QCamera *cameraEntity =
                    qobject_cast<Qt3DRender::QCamera *>(m_selectedEntity);
            if (cameraEntity) {
                // Store the initial upvector
                m_dragInitialRotateCustomVector = cameraEntity->upVector();
            } else if (selectedItem->itemType() == EditorSceneItem::Light) {
                LightData *lightData = m_sceneLights.value(m_selectedEntity->id());
                if (lightData) {
                    m_dragInitialRotateCustomVector =
                            EditorUtils::lightDirection(lightData->lightComponent);
                    if (m_dragInitialRotateCustomVector.isNull()) {
                        // Have some valid vector to rotate in case direction is 0,0,0
                        m_dragInitialRotateCustomVector = defaultLightDirection;
                    }
                }
            }
            m_dragEntity = m_selectedEntity;
            m_dragMode = DragRotate;
            m_dragInitialRotationValue = m_selectedEntityTransform->rotation();
            m_dragInitialHandleTranslation = m_dragHandlesTransform->rotation()
                    * m_dragHandleRotateTransform->translation();
            m_dragInitialCenterTranslation = m_dragHandlesTransform->translation();
        } else if (dragMode == DragScale
                   && m_dragHandleScaleTransforms.at(0)->isEnabled()) {
            m_dragMode = DragScale;
            m_dragEntity = m_selectedEntity;
            m_dragInitialScaleValue = m_selectedEntityTransform->scale3D();
            m_dragInitialCenterTranslation = m_dragHandlesTransform->translation();
            m_dragInitialHandleTranslation = m_dragHandlesTransform->rotation()
                    * m_dragHandleScaleTransforms.at(m_dragHandleIndex)->translation();
            m_dragHandleCornerTranslation = selectedItem->entityMeshExtents()
                    * m_dragHandleCornerAdjustments.at(m_dragHandleIndex) / 2.0f;
            m_dragInitialHandleCornerTranslation =
                    EditorUtils::totalAncestralScale(m_selectedEntity)
                    * m_dragInitialScaleValue * m_dragHandleCornerTranslation;
            m_dragInitialHandleMatrix = m_dragHandlesTransform->matrix();
        }
    }
}

void EditorScene::dragHandleMove(const QPoint &pos, bool shiftDown, bool ctrlDown, bool altDown)
{
    // Ignore initial minor drags
    if (m_ignoringInitialDrag) {
        QPoint delta = pos - m_previousMousePosition;
        if (delta.manhattanLength() > 10)
            m_ignoringInitialDrag = false;
    }
    if (!m_ignoringInitialDrag) {
        // If selected entity changes mid-drag, cancel drag.
        if (m_dragMode != DragNone && m_dragEntity != m_selectedEntity)
            cancelDrag();
        switch (m_dragMode) {
        case DragTranslate: {
            dragTranslateSelectedEntity(pos, shiftDown, ctrlDown, altDown);
            break;
        }
        case DragScale: {
            dragScaleSelectedEntity(pos, shiftDown, ctrlDown, altDown);
            break;
        }
        case DragRotate: {
            dragRotateSelectedEntity(pos, shiftDown, ctrlDown);
            break;
        }
        default:
            break;
        }
    }
}

void EditorScene::dragHandleRelease()
{
    cancelDrag();
}

int EditorScene::gridSize() const
{
    return m_gridSize;
}

void EditorScene::setGridSize(int size)
{
    if (m_gridSize != size) {
        delete m_helperPlane;
        m_gridSize = size;
        createHelperPlane();
        emit gridSizeChanged(size);
    }
}

const QString EditorScene::language() const
{
    if (m_language.isEmpty())
        return QLocale::system().name().left(2);
    else
        return m_language;
}

void EditorScene::setLanguage(const QString &language)
{
    if (!m_qtTranslator->isEmpty())
        QCoreApplication::removeTranslator(m_qtTranslator);
    if (!m_appTranslator->isEmpty())
        QCoreApplication::removeTranslator(m_appTranslator);

    if (m_qtTranslator->load("qt_" + language,
                             QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
        QCoreApplication::installTranslator(m_qtTranslator);
    }

    if (m_appTranslator->load(":/qt3deditorlib/editorlib_" + language)) {
        QCoreApplication::installTranslator(m_appTranslator);
        m_language = language;
    } else {
        m_language = "C";
    }

    emit languageChanged(m_language);
    emit translationChanged("");
    retranslateUi();
}

void EditorScene::retranslateUi()
{
    //: This string is entity name, no non-ascii characters allowed
    m_sceneRootString = tr("Scene root");
    m_saveFailString = tr("Failed to save the scene");
    m_loadFailString = tr("Failed to load a new scene");
    m_cameraString = tr("Camera");
    m_cubeString = tr("Cube");
    m_lightString = tr("Light");
}

const QString EditorScene::emptyString() const
{
    return QStringLiteral("");
}

void EditorScene::enableVisibleCameras(bool enable)
{
    for (int i = 0; i < m_sceneCameras.size(); i++)
        enableVisibleCamera(m_sceneCameras[i], enable, i == m_activeSceneCameraIndex);
}

void EditorScene::enableVisibleCamera(EditorScene::CameraData &cameraData,
                                      bool enable, bool isActiveCamera)
{
    enable = enable && cameraData.cameraEntity->isEnabled();
    cameraData.visibleEntity->setEnabled(enable);

    if (isActiveCamera) {
        m_activeSceneCameraFrustumData.frustumEntity->setEnabled(enable);
        m_activeSceneCameraFrustumData.viewCenterEntity->setEnabled(enable);
        m_activeSceneCameraFrustumData.viewVectorEntity->setEnabled(enable);
        if (enable) {
            if (!m_activeSceneCameraFrustumData.viewCenterPicker) {
                m_activeSceneCameraFrustumData.viewCenterPicker =
                        createObjectPickerForEntity(m_activeSceneCameraFrustumData.viewCenterEntity);
            }
        } else {
            delete m_activeSceneCameraFrustumData.viewCenterPicker;
            m_activeSceneCameraFrustumData.viewCenterPicker = nullptr;
        }
    }

    // Picker doesn't get disabled with the entity - we have to delete it to disable
    if (enable) {
        if (!cameraData.cameraPicker)
            cameraData.cameraPicker = createObjectPickerForEntity(cameraData.visibleEntity);
    } else {
        delete cameraData.cameraPicker;
        cameraData.cameraPicker = nullptr;
    }
}

void EditorScene::enableVisibleLights(bool enable)
{
    Q_FOREACH (LightData *lightData, m_sceneLights.values())
        enableVisibleLight(*lightData, enable);
}

void EditorScene::enableVisibleLight(EditorScene::LightData &lightData, bool enable)
{
    enable = enable && lightData.lightEntity->isEnabled();
    lightData.visibleEntity->setEnabled(enable);

    // Picker doesn't get disabled with the entity - we have to delete it to disable
    if (enable) {
        if (!lightData.visiblePicker)
            lightData.visiblePicker = createObjectPickerForEntity(lightData.visibleEntity);
    } else {
        delete lightData.visiblePicker;
        lightData.visiblePicker = nullptr;
    }
}

void EditorScene::clearSceneCamerasAndLights()
{
    Q_FOREACH (LightData *lightData, m_sceneLights.values()) {
        delete lightData->visibleEntity;
        delete lightData;
    }
    m_sceneLights.clear();

    for (int i = 0; i < m_sceneCameras.size(); i++)
        delete m_sceneCameras.at(i).visibleEntity;
    m_sceneCameras.clear();

    m_activeSceneCameraIndex = -1;
    m_sceneCamerasModel.setStringList(QStringList());
}

Qt3DRender::QObjectPicker *EditorScene::createObjectPickerForEntity(Qt3DCore::QEntity *entity)
{
    Qt3DRender::QObjectPicker *picker = nullptr;
    EditorSceneItem *item = m_sceneItems.value(entity->id());
    if (item && item->itemType() == EditorSceneItem::SceneLoader) {
        // Scene loaders need multiple pickers. Null picker is returned.
        createSceneLoaderChildPickers(entity, item->internalPickers());
    } else if (!item || item->itemType() != EditorSceneItem::Group) {
        // Group is not visible by itself (has no mesh), so no picker is needed
        picker = new Qt3DRender::QObjectPicker(entity);
        picker->setHoverEnabled(false);
        picker->setObjectName(QStringLiteral("__internal object picker ") + entity->objectName());
        entity->addComponent(picker);
        connect(picker, &Qt3DRender::QObjectPicker::pressed, this, &EditorScene::handlePickerPress);
    }

    return picker;
}

// Debug handle is useful for visually debugging calculated world positions
void EditorScene::showDebugHandle(bool show, int handleIndex, const QVector3D &worldPosition)
{
    QVector3D screenPoint;
    if (show) {
        Qt3DRender::QCamera *camera = frameGraphCamera();
        screenPoint = EditorUtils::projectRay(
                    camera->viewMatrix(), camera->projectionMatrix(),
                    m_viewport->width(), m_viewport->height(), worldPosition);
    }

    emit repositionDragHandle(DragDebug, QPoint(screenPoint.x(), screenPoint.y()),
                              show, handleIndex, 0);
}

void EditorScene::queueEnsureSelection()
{
    // Ensure that something is selected after the current pending events have executed
    if (m_sceneEntity && m_ensureSelectionEntityName.isEmpty()) {
        if (m_selectedEntity) {
            m_ensureSelectionEntityName = m_selectedEntity->objectName();
            clearSingleSelection();
        } else {
            m_ensureSelectionEntityName = m_sceneEntity->objectName();
        }
        QMetaObject::invokeMethod(this, "doEnsureSelection", Qt::QueuedConnection);
    }
}

void EditorScene::doEnsureSelection()
{
    if (!m_selectedEntity) {
        // If we have multiselection active, update the multiselection list
        const int count = m_selectedEntityNameList.size();
        if (count > 0) {
            QStringList newList;
            newList.reserve(count);
            for (int i = 0; i < count; ++i) {
                QString entityName = m_selectedEntityNameList.at(i);
                if (itemByName(entityName))
                    newList.append(entityName);
            }
            if (newList.size() == 1) {
                m_ensureSelectionEntityName = newList.at(0);
                newList.clear();
            }

            if (count != newList.size()) {
                m_selectedEntityNameList = newList;
                if (newList.size() == 0)
                    emit multiSelectionChanged(false);
            }
            checkMultiSelectionHighlights();
            emit multiSelectionListChanged();
        }
        if (!m_selectedEntityNameList.size()) {
            EditorSceneItem *item = itemByName(m_ensureSelectionEntityName);
            if (item)
                setSelection(item->entity());
            else
                setSelection(m_sceneEntity);
        }
    }
    m_ensureSelectionEntityName.clear();
}

void EditorScene::queueUpdateGroupSelectionBoxes()
{
    // Queue asynchronous update to group selection boxes
    if (!m_groupBoxUpdatePending) {
        m_groupBoxUpdatePending = true;
        QMetaObject::invokeMethod(this, "doUpdateGroupSelectionBoxes", Qt::QueuedConnection);
    }
}

void EditorScene::doUpdateGroupSelectionBoxes()
{
    const QList<EditorSceneItem *> items = m_sceneItems.values();
    for (int i = 0; i < items.size(); ++i) {
        EditorSceneItem *item = items.at(i);
        if (item && item->itemType() == EditorSceneItem::Group && item->isSelectionBoxShowing())
            item->updateGroupExtents();
    }
    m_groupBoxUpdatePending = false;
}

EditorSceneItem *EditorScene::itemByName(const QString &name)
{
    const QList<EditorSceneItem *> items = m_sceneItems.values();
    for (int i = 0; i < items.size(); ++i) {
        if (items.at(i)->entity()->objectName() == name)
            return items.at(i);
    }
    return nullptr;
}

void EditorScene::clearSingleSelection()
{
    if (m_selectedEntity) {
        EditorSceneItem *oldItem = m_sceneItems.value(m_selectedEntity->id(), nullptr);
        if (oldItem) {
            connectDragHandles(oldItem, false);
            oldItem->setShowSelectionBox(false);
        }
        m_selectedEntity = nullptr;
    }
}

int EditorScene::cameraIndexForEntity(Qt3DCore::QEntity *entity)
{
    int index = -1;
    if (entity) {
        for (int i = 0; i < m_sceneCameras.size(); i++) {
            if (m_sceneCameras.at(i).cameraEntity == entity) {
                index = i;
                break;
            }
        }
    }
    return index;
}

void EditorScene::updateVisibleSceneCameraMatrix(const EditorScene::CameraData &cameraData)
{
    QMatrix4x4 matrix = calculateVisibleSceneCameraMatrix(cameraData.cameraEntity);
    cameraData.visibleTransform->setMatrix(matrix);

    if (m_activeSceneCameraIndex >= 0
            && cameraData.cameraEntity == m_sceneCameras.at(m_activeSceneCameraIndex).cameraEntity) {
        m_activeSceneCameraFrustumData.viewVectorTransform->setScale3D(
                    QVector3D(1.0f, 1.0f, cameraData.cameraEntity->viewVector().length()));
        m_activeSceneCameraFrustumData.viewVectorTransform->setTranslation(
                    cameraData.cameraEntity->position());
        m_activeSceneCameraFrustumData.viewVectorTransform->setRotation(
                    cameraData.visibleTransform->rotation());

        EditorUtils::updateCameraFrustumMesh(m_activeSceneCameraFrustumData.frustumMesh,
                                             cameraData.cameraEntity);
        m_activeSceneCameraFrustumData.frustumTransform->setTranslation(
                    cameraData.cameraEntity->position());
        m_activeSceneCameraFrustumData.frustumTransform->setRotation(
                    cameraData.visibleTransform->rotation());

        m_activeSceneCameraFrustumData.viewCenterTransform->setTranslation(
                    cameraData.cameraEntity->viewCenter());
        resizeCameraViewCenterEntity();
    }
}

void EditorScene::connectDragHandles(EditorSceneItem *item, bool enable)
{
    if (item) {
        if (enable) {
            connect(item, &EditorSceneItem::selectionBoxTransformChanged,
                    this, &EditorScene::handleSelectionTransformChange);
        } else {
            disconnect(item, &EditorSceneItem::selectionBoxTransformChanged,
                       this, &EditorScene::handleSelectionTransformChange);
        }
    }
}

void EditorScene::dragTranslateSelectedEntity(const QPoint &newPos, bool shiftDown, bool ctrlDown,
                                              bool altDown)
{
    // By default, translate along helper plane
    // When shift is pressed, translate along camera plane
    // When ctrl is pressed, snap to grid
    // When alt is pressed, translate along helper plane normal (lock to one axis)

    Qt3DRender::QCamera *camera = frameGraphCamera();
    if (camera && m_selectedEntityTransform) {
        // For cameras, we need to use position instead of translation for correct results
        QVector3D entityTranslation = m_selectedEntityTransform->translation();
        Qt3DRender::QCamera *cameraEntity = qobject_cast<Qt3DRender::QCamera *>(m_selectedEntity);
        if (cameraEntity) {
            if (m_cameraViewCenterSelected && !m_viewCenterLocked)
                entityTranslation = cameraEntity->viewCenter();
            else
                entityTranslation = cameraEntity->position();
        }

        QVector3D helperNormal = helperPlaneNormal();
        QVector3D planeOrigin = m_dragInitialWorldTranslationValue;
        QVector3D planeNormal;
        const bool useCameraNormal = shiftDown || altDown;
        if (useCameraNormal)
            planeNormal = EditorUtils::cameraNormal(frameGraphCamera());
        else
            planeNormal = helperNormal;

        float cosAngle = QVector3D::dotProduct(planeOrigin.normalized(), planeNormal);
        float planeOffset = planeOrigin.length() * cosAngle;

        QVector3D ray = EditorUtils::unprojectRay(camera->viewMatrix(), camera->projectionMatrix(),
                                                  m_viewport->width(), m_viewport->height(),
                                                  newPos);
        float t = 0.0f;
        QVector3D intersection = EditorUtils::findIntersection(camera->position(), ray,
                                                               planeOffset, planeNormal, t);
        if (t > camera->nearPlane()) {
            EditorSceneItemComponentsModel::EditorSceneItemComponentTypes componentType =
                    EditorSceneItemComponentsModel::Transform;
            QString propertyName;

            if (cameraEntity) {
                componentType = EditorSceneItemComponentsModel::CameraEntity;
                if (m_cameraViewCenterSelected && !m_viewCenterLocked)
                    propertyName = QStringLiteral("viewCenter");
                else
                    propertyName = QStringLiteral("position");
            } else {
                propertyName = QStringLiteral("translation");
            }

            QVector3D newPosition = intersection;
            if (ctrlDown) {
                newPosition = snapPosition(intersection,
                                           useCameraNormal || helperNormal.x() < 0.5,
                                           useCameraNormal || helperNormal.y() < 0.5,
                                           useCameraNormal || helperNormal.z() < 0.5);
            }
            if (altDown) {
                QVector3D snapPos = newPosition;
                newPosition = m_dragInitialEntityTranslationValue;
                if (helperNormal.x() > 0.5)
                    newPosition.setX(snapPos.x());
                else if (helperNormal.y() > 0.5)
                    newPosition.setY(snapPos.y());
                else if (helperNormal.z() > 0.5)
                    newPosition.setZ(snapPos.z());
            } else {
                // If entity has parents with transfroms, those need to be applied in inverse
                QMatrix4x4 totalTransform = EditorUtils::totalAncestralTransform(m_selectedEntity);
                if (m_dragHandleIndex == 0 && !cameraEntity) {
                    newPosition = totalTransform.inverted()
                            * (newPosition + m_dragHandlesTransform->rotation()
                               * m_dragHandleTranslateTransform->translation());
                } else {
                    newPosition = totalTransform.inverted() * newPosition;
                }
            }

            m_undoHandler->createChangePropertyCommand(m_selectedEntity->objectName(), componentType,
                                                       propertyName, newPosition,
                                                       entityTranslation, true);
        }
    }
}

void EditorScene::dragScaleSelectedEntity(const QPoint &newPos, bool shiftDown, bool ctrlDown,
                                          bool altDown)
{
    // By default, scale each dimension individually.
    // Only the dragged corner moves freely when scaling, the opposite corner is anchored.
    // When shift is pressed, scale uniformly
    // When ctrl is pressed, scale in integers
    // When alt is pressed, scale along helper plane normal (lock to one axis)

    QVector3D posOffset = dragHandlePositionOffset(newPos);
    if (!posOffset.isNull()) {
        QVector3D rotatedPosOffset = m_dragHandlesTransform->rotation().inverted() * posOffset;
        QVector3D moveFactors =
                EditorUtils::absVector3D(
                    QVector3D((2.0f * m_dragInitialHandleCornerTranslation)
                              + rotatedPosOffset)) / 2.0f;

        // Divide by zero may cause an INFINITY. Fix it.
        if (m_dragInitialHandleCornerTranslation.x() != 0.0f)
            moveFactors.setX(moveFactors.x() / qAbs(m_dragInitialHandleCornerTranslation.x()));
        else
            moveFactors.setX(1.0f);
        if (m_dragInitialHandleCornerTranslation.y() != 0.0f)
            moveFactors.setY(moveFactors.y() / qAbs(m_dragInitialHandleCornerTranslation.y()));
        else
            moveFactors.setY(1.0f);
        if (m_dragInitialHandleCornerTranslation.z() != 0.0f)
            moveFactors.setZ(moveFactors.z() / qAbs(m_dragInitialHandleCornerTranslation.z()));
        else
            moveFactors.setZ(1.0f);

        if (shiftDown) {
            float averageFactor = (moveFactors.x() + moveFactors.y() + moveFactors.z()) / 3.0f;
            moveFactors = QVector3D(averageFactor, averageFactor, averageFactor);
        }

        QVector3D newScale = m_dragInitialScaleValue * EditorUtils::maxVector3D(moveFactors,
                                                                                0.0001f);
        if (ctrlDown) {
            newScale.setX(qMax(qRound(newScale.x()), 1));
            newScale.setY(qMax(qRound(newScale.y()), 1));
            newScale.setZ(qMax(qRound(newScale.z()), 1));
        }
        if (altDown) {
            QVector3D helperNormal = helperPlaneNormal();
            QVector3D snapScale = newScale;
            newScale = m_dragInitialScaleValue;
            if (helperNormal.x() > 0.5f)
                newScale.setX(snapScale.x());
            else if (helperNormal.y() > 0.5f)
                newScale.setY(snapScale.y());
            else if (helperNormal.z() > 0.5f)
                newScale.setZ(snapScale.z());
        }

        // Calculate the translate needed to keep opposite corner anchored
        QMatrix4x4 ancestralTransform = EditorUtils::totalAncestralTransform(m_selectedEntity);
        QVector3D ancestralScale =
                EditorUtils::totalAncestralScale(m_selectedEntity) * newScale;
        QVector3D newHandleCornerTranslation = ancestralScale * m_dragHandleCornerTranslation;
        EditorSceneItem *selectedItem = m_sceneItems.value(m_selectedEntity->id(), nullptr);
        if (selectedItem)
            newHandleCornerTranslation -= ancestralScale * selectedItem->entityMeshCenter();
        QVector3D newTranslation = ancestralTransform.inverted() * m_dragInitialHandleMatrix
                * (newHandleCornerTranslation - m_dragInitialHandleCornerTranslation);

        m_undoHandler->createChangePropertyCommand(m_selectedEntity->objectName(),
                                                   EditorSceneItemComponentsModel::Transform,
                                                   QStringLiteral("scale3D"), newScale,
                                                   m_selectedEntityTransform->scale3D(),
                                                   QStringLiteral("translation"), newTranslation,
                                                   m_selectedEntityTransform->translation(), true);
    }
}

void EditorScene::dragRotateSelectedEntity(const QPoint &newPos, bool shiftDown, bool ctrlDown)
{
    // By default, rotate around helper plane
    // When shift is pressed, rotate around camera plane.
    // When ctrl is pressed, rotate in 22.5 degree increments

    QVector3D posOffset = dragHandlePositionOffset(newPos);
    if (!posOffset.isNull()) {
        QVector3D unrotatedHandlePos = m_dragInitialHandleTranslation;
        QVector3D desiredPos = unrotatedHandlePos + posOffset;
        unrotatedHandlePos = projectVectorOnCameraPlane(unrotatedHandlePos);
        desiredPos = projectVectorOnCameraPlane(desiredPos);
        unrotatedHandlePos.normalize();
        desiredPos.normalize();
        QQuaternion newRotation;
        float d = QVector3D::dotProduct(unrotatedHandlePos, desiredPos) + 1.0f;
        if (ctrlDown) {
            // Rotate in larger increments
            // We need an additional check vector to determine which way the angle points
            QVector3D checkVec = EditorUtils::rotateVector(
                        unrotatedHandlePos, EditorUtils::cameraNormal(frameGraphCamera()),
                        M_PI / 2.0);
            bool largeAngle = QVector3D::dotProduct(checkVec, desiredPos) > 0.0f;
            qreal radsOrig = qAcos(d - 1.0f);
            if (largeAngle)
                radsOrig = (2.0 * M_PI) - radsOrig;
            qreal radsAdjusted = -(qreal(qRound(radsOrig * 8.0 / M_PI)) / 8.0) * M_PI;
            if (radsAdjusted == 0.0) {
                // Indicate rotation of 0 degrees
                d = 2.0f;
            } else if (radsAdjusted == -M_PI) {
                // Indicate rotation of 180 degrees
                d = 0.0f;
            } else {
                desiredPos = EditorUtils::rotateVector(
                            unrotatedHandlePos,
                            EditorUtils::cameraNormal(frameGraphCamera()),
                            radsAdjusted);
            }
        }
        EditorSceneItem *selectedItem = m_sceneItems.value(m_selectedEntity->id());
        Qt3DRender::QCamera *cameraEntity = qobject_cast<Qt3DRender::QCamera *>(m_selectedEntity);
        if (cameraEntity) {
            QVector3D newUpVector;
            if (qFuzzyIsNull(d)) {
                // Rotation of 180 degrees
                newUpVector = -m_dragInitialRotateCustomVector;
            } else if (qFuzzyCompare(d, 2.0f)) {
                // Rotation of zero degrees
                newUpVector = m_dragInitialRotateCustomVector;
            } else {
                // In case of camera, we rotate the upvector
                QVector3D cameraNormal = cameraEntity->viewVector().normalized();
                if (cameraNormal.distanceToPlane(
                            QVector3D(), EditorUtils::cameraNormal(frameGraphCamera())) < 0.0f) {
                    cameraNormal = -cameraNormal;
                }
                QVector3D initialUpVector =
                        EditorUtils::projectVectorOnPlane(m_dragInitialRotateCustomVector.normalized(),
                                                          cameraNormal);
                QQuaternion planeRotation =
                        QQuaternion::rotationTo(EditorUtils::cameraNormal(frameGraphCamera()),
                                                cameraNormal);
                unrotatedHandlePos = planeRotation.rotatedVector(unrotatedHandlePos);
                desiredPos = planeRotation.rotatedVector(desiredPos);
                newRotation = QQuaternion::rotationTo(unrotatedHandlePos, desiredPos);
                newUpVector = newRotation.rotatedVector(initialUpVector).normalized();
            }
            m_undoHandler->createChangePropertyCommand(m_selectedEntity->objectName(),
                                                       EditorSceneItemComponentsModel::CameraEntity,
                                                       QStringLiteral("upVector"), newUpVector,
                                                       cameraEntity->upVector(), true);
        } else if (selectedItem && selectedItem->itemType() == EditorSceneItem::Light) {
            QVector3D newDirection;
            QVector3D oldDirection;
            LightData *lightData = m_sceneLights.value(m_selectedEntity->id());
            if (lightData)
                oldDirection = EditorUtils::lightDirection(lightData->lightComponent);
            if (qFuzzyIsNull(d)) {
                // Rotation of 180 degrees
                QVector3D rotationAxis;
                if (shiftDown)
                    rotationAxis = EditorUtils::cameraNormal(frameGraphCamera());
                else
                    rotationAxis = helperPlaneNormal();
                newRotation = QQuaternion::fromAxisAndAngle(rotationAxis, 180.0f)
                        * m_dragInitialRotationValue;
                newDirection = newRotation.rotatedVector(
                            m_dragInitialRotateCustomVector.normalized()).normalized();
            } else if (qFuzzyCompare(d, 2.0f)) {
                // Rotation of zero degrees
                newDirection = m_dragInitialRotateCustomVector;
            } else {
                QVector3D rotationAxis;
                if (shiftDown) {
                    rotationAxis = EditorUtils::cameraNormal(frameGraphCamera());
                } else {
                    // Rotate vectors so that they lie on helper plane instead of camera plane
                    QQuaternion planeRotation =
                            QQuaternion::rotationTo(EditorUtils::cameraNormal(frameGraphCamera()),
                                                    helperPlaneNormal());
                    unrotatedHandlePos = planeRotation.rotatedVector(unrotatedHandlePos);
                    desiredPos = planeRotation.rotatedVector(desiredPos);
                    rotationAxis = helperPlaneNormal();
                }
                QVector3D checkVector =
                        EditorUtils::projectVectorOnPlane(m_dragInitialRotateCustomVector.normalized(),
                                                          rotationAxis);
                if (checkVector.length() > 0.001) {
                    newRotation = QQuaternion::rotationTo(unrotatedHandlePos, desiredPos);
                    newDirection = newRotation.rotatedVector(
                                m_dragInitialRotateCustomVector.normalized()).normalized();
                } else {
                    // Don't rotate at all if direction is paraller to rotation axis
                    newDirection = m_dragInitialRotateCustomVector;
                }
            }
            // In case of camera, we rotate the upvector, and in case of lights, the direction
            QString propertyName =
                    qobject_cast<Qt3DRender::QDirectionalLight *>(lightData->lightComponent)
                    ? QStringLiteral("worldDirection") : QStringLiteral("localDirection");
            m_undoHandler->createChangePropertyCommand(m_selectedEntity->objectName(),
                                                       EditorSceneItemComponentsModel::Light,
                                                       propertyName, newDirection,
                                                       oldDirection, true);
        } else {
            QQuaternion ancestralRotation =
                    EditorUtils::totalAncestralRotation(m_selectedEntity).inverted();
            if (qFuzzyIsNull(d)) {
                // Rotation of 180 degrees
                QVector3D rotationAxis;
                if (shiftDown)
                    rotationAxis = EditorUtils::cameraNormal(frameGraphCamera());
                else
                    rotationAxis = helperPlaneNormal();
                rotationAxis = ancestralRotation.rotatedVector(rotationAxis);
                newRotation = QQuaternion::fromAxisAndAngle(rotationAxis, 180.0f)
                        * m_dragInitialRotationValue;
            } else if (qFuzzyCompare(d, 2.0f)) {
                // Rotation of zero degrees
                newRotation = m_dragInitialRotationValue;
            } else {
                if (!shiftDown) {
                    // Rotate vectors so that they lie on helper plane instead of camera plane
                    QQuaternion planeRotation =
                            QQuaternion::rotationTo(EditorUtils::cameraNormal(frameGraphCamera()),
                                                    helperPlaneNormal());

                    planeRotation = ancestralRotation * planeRotation;
                    unrotatedHandlePos = planeRotation.rotatedVector(unrotatedHandlePos);
                    desiredPos = planeRotation.rotatedVector(desiredPos);
                } else {
                    unrotatedHandlePos = ancestralRotation.rotatedVector(unrotatedHandlePos);
                    desiredPos = ancestralRotation.rotatedVector(desiredPos);
                }
                newRotation = QQuaternion::rotationTo(unrotatedHandlePos, desiredPos)
                        * m_dragInitialRotationValue;
            }
            m_undoHandler->createChangePropertyCommand(m_selectedEntity->objectName(),
                                                       EditorSceneItemComponentsModel::Transform,
                                                       QStringLiteral("rotation"), newRotation,
                                                       m_selectedEntityTransform->rotation(), true);
        }
    }
}

// Returns world coordinate offset from drag handle position to cursor position on a plane
// that is defined by middle of selection box and reverse camera view direction.
QVector3D EditorScene::dragHandlePositionOffset(const QPoint &newPos)
{
    QVector3D posOffset;
    Qt3DRender::QCamera *camera = frameGraphCamera();
    if (camera) {
        // Find out a camera oriented plane that intersects middle of selection box
        QVector3D planeNormal = camera->position() - camera->viewCenter();
        planeNormal.normalize();

        QVector3D planeOrigin = m_dragInitialCenterTranslation;

        float cosAngle = QVector3D::dotProduct(planeOrigin.normalized(), planeNormal);
        float planeOffset = planeOrigin.length() * cosAngle;

        // Calculate intersection with plane and newPos
        QVector3D rayToNewPos = EditorUtils::unprojectRay(camera->viewMatrix(),
                                                          camera->projectionMatrix(),
                                                          m_viewport->width(), m_viewport->height(),
                                                          newPos);
        float t = 0.0f;
        QVector3D intersection = EditorUtils::findIntersection(camera->position(), rayToNewPos,
                                                               planeOffset, planeNormal, t);

        if (t > 0.0f) {
            posOffset = intersection - (m_dragInitialCenterTranslation
                                        + m_dragInitialHandleTranslation);
        }
    }
    return posOffset;
}

QMatrix4x4 EditorScene::calculateVisibleSceneCameraMatrix(Qt3DRender::QCamera *camera) const
{
    QMatrix4x4 matrix = EditorUtils::totalAncestralTransform(camera);

    QQuaternion rotation = QQuaternion::fromDirection(-camera->viewVector(),
                                                      camera->upVector());

    matrix.translate(camera->position());
    matrix.rotate(rotation);
    return matrix;
}

QMatrix4x4 EditorScene::calculateVisibleLightMatrix(Qt3DCore::QEntity *lightEntity) const
{
    QMatrix4x4 matrix;

    LightData *lightData = m_sceneLights.value(lightEntity->id());
    if (lightData) {
        QMatrix4x4 ancestralMatrix = EditorUtils::totalAncestralTransform(lightData->lightEntity);
        QMatrix4x4 lightMatrix;
        lightMatrix.translate(lightData->lightTransform->translation());
        QVector3D newPos = ancestralMatrix * lightMatrix * QVector3D();
        QVector3D direction = EditorUtils::lightDirection(lightData->lightComponent);
        matrix.translate(newPos);
        if (!direction.isNull()) {
            // Rotate using only pitch and yaw to keep light sensibly oriented
            direction.normalize();
            float pitch = qAsin(-direction.y()) * 180.0f / M_PI;
            float yaw = qAtan2(direction.x(), direction.z()) * 180.0f / M_PI;
            QQuaternion rotation = QQuaternion::fromEulerAngles(pitch, yaw, 0.0f);
            matrix.rotate(rotation);

        }
    }
    return matrix;
}

void EditorScene::handlePropertyLocking(EditorSceneItem *item, const QString &lockProperty,
                                        bool locked)
{
    // Disable/enable relevant drag handles when properties are locked/unlocked
    EditorSceneItem *selectedItem = m_sceneItems.value(m_selectedEntity->id(), nullptr);
    if (item && item == selectedItem) {
        if (item->itemType() == EditorSceneItem::Camera) {
            QString upVectorLock = QStringLiteral("upVector") + lockPropertySuffix();
            QString positionLock = QStringLiteral("position") + lockPropertySuffix();
            QString viewCenterLock = QStringLiteral("viewCenter") + lockPropertySuffix();
            if (lockProperty == upVectorLock)
                m_dragHandleRotateTransform->setEnabled(!locked);
            else if (lockProperty == positionLock)
                m_dragHandleTranslateTransform->setEnabled(!locked);
            else if (lockProperty == viewCenterLock)
                m_viewCenterLocked = locked;
        } else {
            if (lockProperty == lockTransformPropertyName()) {
                Qt3DCore::QTransform *transform =
                        EditorUtils::entityTransform(m_selectedEntity);
                if (item->itemType() == EditorSceneItem::Light) {
                    if (locked) {
                        m_dragHandleTranslateTransform->setEnabled(false);
                    } else {
                        m_dragHandleTranslateTransform->setEnabled(
                                    !isPropertyLocked(QStringLiteral("translation"), transform));
                    }
                } else {
                    if (locked) {
                        m_dragHandleTranslateTransform->setEnabled(false);
                        m_dragHandleScaleTransforms.at(0)->setEnabled(false);
                        m_dragHandleRotateTransform->setEnabled(false);
                    } else {
                        m_dragHandleTranslateTransform->setEnabled(
                                    !isPropertyLocked(QStringLiteral("translation"), transform));
                        m_dragHandleScaleTransforms.at(0)->setEnabled(
                                    !isPropertyLocked(QStringLiteral("scale3D"), transform));
                        m_dragHandleRotateTransform->setEnabled(
                                    !isPropertyLocked(QStringLiteral("rotation"), transform));
                    }
                }
            } else {
                QString translateLock = QStringLiteral("translation") + lockPropertySuffix();
                if (lockProperty == translateLock) {
                    m_dragHandleTranslateTransform->setEnabled(!locked);
                } else if (item->itemType() == EditorSceneItem::Light) {
                    if (item->canRotate()) {
                        QString directionLock = QStringLiteral("localDirection") + lockPropertySuffix();
                        QString worldDirectionLock = QStringLiteral("worldDirection") + lockPropertySuffix();
                        if (lockProperty == directionLock || lockProperty == worldDirectionLock)
                            m_dragHandleRotateTransform->setEnabled(!locked);
                    }
                } else {
                    QString scaleLock = QStringLiteral("scale3D") + lockPropertySuffix();
                    QString rotateLock = QStringLiteral("rotation") + lockPropertySuffix();
                    if (lockProperty == scaleLock)
                        m_dragHandleScaleTransforms.at(0)->setEnabled(!locked);
                    else if (lockProperty == rotateLock)
                        m_dragHandleRotateTransform->setEnabled(!locked);
                }
            }
        }
        handleSelectionTransformChange();
    }
}

void EditorScene::handleLightTypeChanged(EditorSceneItem *item)
{
    if (item) {
        Qt3DRender::QAbstractLight *light = EditorUtils::entityLight(item->entity());
        if (light) {
            LightData *lightData = m_sceneLights.value(item->entity()->id());
            if (lightData) {
                lightData->lightComponent = light;
                connect(light, &Qt3DRender::QAbstractLight::colorChanged,
                        lightData->visibleMaterial, &Qt3DExtras::QPhongAlphaMaterial::setAmbient);
                delete lightData->visibleMesh;
                Qt3DRender::QDirectionalLight *dirLight =
                        qobject_cast<Qt3DRender::QDirectionalLight *>(light);
                Qt3DRender::QSpotLight *spotLight = qobject_cast<Qt3DRender::QSpotLight *>(light);
                if (dirLight) {
                    lightData->visibleMesh = EditorUtils::createLightMesh(EditorUtils::LightDirectional);
                    connect(dirLight, &Qt3DRender::QDirectionalLight::worldDirectionChanged,
                            this, &EditorScene::handleLightTransformChange);
                    connect(dirLight, &Qt3DRender::QDirectionalLight::worldDirectionChanged,
                            item, &EditorSceneItem::updateSelectionBoxTransform);
                } else if (spotLight) {
                    lightData->visibleMesh = EditorUtils::createLightMesh(EditorUtils::LightSpot);
                    connect(spotLight, &Qt3DRender::QSpotLight::localDirectionChanged,
                            this, &EditorScene::handleLightTransformChange);
                    connect(spotLight, &Qt3DRender::QSpotLight::localDirectionChanged,
                            item, &EditorSceneItem::updateSelectionBoxTransform);
                } else if (qobject_cast<Qt3DRender::QPointLight *>(light)) {
                    lightData->visibleMesh = EditorUtils::createLightMesh(EditorUtils::LightPoint);
                }
                lightData->visibleEntity->addComponent(lightData->visibleMesh);
            }
            if (item->entity() == m_selectedEntity) {
                if (item->canRotate()) {
                    QString lockProperty =
                            qobject_cast<Qt3DRender::QDirectionalLight *>(lightData->lightComponent)
                            ? QStringLiteral("worldDirection") : QStringLiteral("localDirection");
                    m_dragHandleRotateTransform->setEnabled(
                                !isPropertyLocked(lockProperty, light));
                } else {
                    m_dragHandleRotateTransform->setEnabled(false);
                }
                item->updateSelectionBoxTransform();
                updateLightVisibleTransform(item->entity());
            }
        }
    }
}

void EditorScene::updateLightVisibleTransform(Qt3DCore::QEntity *lightEntity)
{
    if (lightEntity) {
        LightData *lightData = m_sceneLights.value(lightEntity->id());
        if (lightData)
            lightData->visibleTransform->setMatrix(calculateVisibleLightMatrix(lightEntity));
    }
}

void EditorScene::handleEnabledChanged(Qt3DCore::QEntity *entity, bool enabled)
{
    bool freeViewEnabled = enabled && m_freeView;
    Qt3DRender::QCamera *camera = qobject_cast<Qt3DRender::QCamera *>(entity);
    if (camera != nullptr) {
        int cameraIndex = cameraIndexForEntity(camera);
        if (cameraIndex >= 0) {
            enableVisibleCamera(m_sceneCameras[cameraIndex], freeViewEnabled,
                                cameraIndex == m_activeSceneCameraIndex);
        }

    } else if (EditorUtils::entityLight(entity) != nullptr) {
        LightData *lightData = m_sceneLights.value(entity->id());
        if (lightData)
            enableVisibleLight(*lightData, freeViewEnabled);

    } else {
        EditorSceneItem *item = m_sceneItems.value(entity->id());
        if (item && item->itemType() == EditorSceneItem::SceneLoader) {
            if (enabled) {
                if (item->internalPickers()->size() == 0)
                    createObjectPickerForEntity(entity);
            } else {
                Q_FOREACH (Qt3DRender::QObjectPicker *picker, *item->internalPickers())
                    delete picker;
                item->internalPickers()->clear();
            }
        } else {
            // Picker doesn't get disabled with the entity - we have to delete it to disable
            Qt3DRender::QObjectPicker *picker = EditorUtils::entityPicker(entity);
            // Other objects aren't affected by m_freeView, so just check enabled flag
            if (enabled) {
                if (!picker)
                    createObjectPickerForEntity(entity);
            } else {
                delete picker;
            }
        }
    }
}

void EditorScene::setError(const QString &errorString)
{
    m_errorString = errorString;
    emit errorChanged(m_errorString);
    qWarning() << m_errorString;
}

bool EditorScene::isRemovable(Qt3DCore::QEntity *entity) const
{
    if (entity == m_sceneEntity || entity == m_rootEntity)
        return false;

    return true;
}

void EditorScene::setupDefaultScene()
{
    // NOTE: Do not add components to an entity after addEntity call.
#ifdef TEST_SCENE
    // Camera
    Qt3DRender::QCamera *sceneCameraEntity = new Qt3DRender::QCamera(m_sceneEntity);
    sceneCameraEntity->setObjectName(QStringLiteral("camera"));

    sceneCameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
    sceneCameraEntity->setPosition(QVector3D(0, 0, -20.0f));
    sceneCameraEntity->setUpVector(QVector3D(0, 1, 0));
    sceneCameraEntity->setViewCenter(QVector3D(0, 0, 0));

    setFrameGraphCamera(sceneCameraEntity);
    addEntity(sceneCameraEntity);

    // Cylinder shape data
    Qt3DRender::QCylinderMesh *cylinder = new Qt3DRender::QCylinderMesh();
    cylinder->setRadius(1);
    cylinder->setLength(3);
    cylinder->setRings(100);
    cylinder->setSlices(20);

    // CylinderMesh Transform
    Qt3DCore::QTransform *cylinderTransform = new Qt3DCore::QTransform();
    cylinderTransform->setScale3D(QVector3D(1.5f, 1.5f, 1.5f));
    cylinderTransform->setRotation(QQuaternion::fromAxisAndAngle(
                                       QVector3D(1.0f, 0.0f, 0.0f), 45.0f));
    cylinderTransform->setTranslation(QVector3D(-2.0f, -5.0f, 0.0f));

    // Cylinder 1
    Qt3DCore::QEntity *cylinderEntity = new Qt3DCore::QEntity(m_sceneEntity);
    cylinderEntity->setObjectName(QStringLiteral("cylinder 1"));
    cylinderEntity->addComponent(cylinder);
    cylinderEntity->addComponent(cylinderTransform);

    Qt3DRender::QPhongMaterial *mat = new Qt3DRender::QPhongMaterial();
    mat->setDiffuse(Qt::red);
    mat->setSpecular(Qt::white);
    mat->setShininess(150.0f);
    cylinderEntity->addComponent(mat);

    Qt3DCore::QTransform *cylinderTransform2 = new Qt3DCore::QTransform();
    cylinderTransform2->setTranslation(QVector3D(5.0f, 5.0f, 0.0f));

    // Cylinder 2
    Qt3DCore::QEntity *cylinderEntity2 = new Qt3DCore::QEntity(cylinderEntity);
    cylinderEntity2->setObjectName(QStringLiteral("cylinder 2"));
    cylinderEntity2->addComponent(cylinder);
    cylinderEntity2->addComponent(cylinderTransform2);
    addEntity(cylinderEntity);

    // Cube 1
    Qt3DCore::QEntity *cubeEntity1 = new Qt3DCore::QEntity(m_sceneEntity);
    cubeEntity1->setObjectName(QStringLiteral("Cube 1"));

    //Cube matrix transform
    QMatrix4x4 cubeMatrix;
    cubeMatrix.rotate(90.0f, 1.0f, 0.0f, 1.0f);
    cubeMatrix.scale(1.4f);
    cubeMatrix.translate(0.0f, -3.0f, -4.0f);
    Qt3DCore::QTransform *cubeTransform = new Qt3DCore::QTransform();
    cubeTransform->setMatrix(cubeMatrix);

    //Cube Mesh
    Qt3DRender::QCuboidMesh *cubeMesh = new Qt3DRender::QCuboidMesh();

    Qt3DRender::QNormalDiffuseSpecularMapMaterial *diffuseMat
            = new Qt3DRender::QNormalDiffuseSpecularMapMaterial();
    Qt3DRender::QTextureImage *diffuseTextureImage = new Qt3DRender::QTextureImage();
    diffuseMat->diffuse()->addTextureImage(diffuseTextureImage);
    diffuseTextureImage->setSource(QUrl(QStringLiteral("qrc:/qt3deditorlib/images/qtlogo.png")));
    Qt3DRender::QTextureImage *normalTextureImage = new Qt3DRender::QTextureImage();
    diffuseMat->normal()->addTextureImage(normalTextureImage);
    normalTextureImage->setSource(QUrl(QStringLiteral("qrc:/qt3deditorlib/images/qtlogo_normal.png")));
    Qt3DRender::QTextureImage *specularTextureImage = new Qt3DRender::QTextureImage();
    diffuseMat->specular()->addTextureImage(specularTextureImage);
    specularTextureImage->setSource(QUrl(QStringLiteral("qrc:/qt3deditorlib/images/qtlogo_specular.png")));
    //diffuseMat->setSpecular(Qt::white);
    diffuseMat->setAmbient(Qt::black);
    diffuseMat->setShininess(150.0f);

    cubeEntity1->addComponent(diffuseMat);
    cubeEntity1->addComponent(cubeTransform);
    cubeEntity1->addComponent(cubeMesh);
    addEntity(cubeEntity1);

    // Light
    Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(m_sceneEntity);
    lightEntity->setObjectName(QStringLiteral("Light 1"));
    Qt3DRender::QAbstractLight *light = new Qt3DRender::QAbstractLight(m_sceneEntity);
    Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform();
    lightTransform->setTranslation(QVector3D(0.0f, 10.0f, -10.0f));
    lightEntity->addComponent(light);
    lightEntity->addComponent(lightTransform);
    addEntity(lightEntity);

#else
    // Camera
    Qt3DRender::QCamera *sceneCameraEntity = new Qt3DRender::QCamera(m_sceneEntity);
    sceneCameraEntity->setObjectName(m_cameraString);

    sceneCameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 50.0f);
    sceneCameraEntity->setPosition(QVector3D(0, 0, -15.0f));
    sceneCameraEntity->setUpVector(QVector3D(0, 1, 0));
    sceneCameraEntity->setViewCenter(QVector3D(0, 0, 0));

    setFrameGraphCamera(sceneCameraEntity);
    addEntity(sceneCameraEntity);

    // Cube
    Qt3DCore::QEntity *cubeEntity = new Qt3DCore::QEntity(m_sceneEntity);
    cubeEntity->setObjectName(m_cubeString);
    Qt3DExtras::QCuboidMesh *cubeMesh = new Qt3DExtras::QCuboidMesh();
    Qt3DCore::QTransform *cubeTransform = new Qt3DCore::QTransform();
    cubeTransform->setTranslation(QVector3D(0.0f, 0.0f, 5.0f));
    cubeTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0.0f, 0.0f, 1.0f), 180.0f));
    Qt3DExtras::QDiffuseSpecularMapMaterial *cubeMaterial
            = new Qt3DExtras::QDiffuseSpecularMapMaterial();
    Qt3DRender::QTextureImage *diffuseTextureImage = new Qt3DRender::QTextureImage();
    cubeMaterial->diffuse()->addTextureImage(diffuseTextureImage);
    diffuseTextureImage->setSource(QUrl(QStringLiteral("qrc:/qt3deditorlib/images/qtlogo.png")));
    Qt3DRender::QTextureImage *specularTextureImage = new Qt3DRender::QTextureImage();
    cubeMaterial->specular()->addTextureImage(specularTextureImage);
    specularTextureImage->setSource(QUrl(QStringLiteral("qrc:/qt3deditorlib/images/qtlogo_specular.png")));
    cubeMaterial->setAmbient(Qt::black);
    cubeMaterial->setShininess(150.0f);
    cubeEntity->addComponent(cubeMesh);
    cubeEntity->addComponent(cubeTransform);
    cubeEntity->addComponent(cubeMaterial);
    addEntity(cubeEntity);

    // Light
    Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(m_sceneEntity);
    lightEntity->setObjectName(m_lightString);
    Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(m_sceneEntity);
    Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform();
    lightTransform->setTranslation(QVector3D(0.0f, 10.0f, -5.0f));
    lightEntity->addComponent(light);
    lightEntity->addComponent(lightTransform);
    addEntity(lightEntity);
#endif
    setActiveSceneCameraIndex(0);
    m_sceneModel->clearExpandedItems();
    m_sceneModel->resetModel();
}

void EditorScene::createRootEntity()
{
    m_rootEntity = new Qt3DCore::QEntity();
    // Grab explicit ownership of the root entity, otherwise QML garbage collector may
    // clean it up.
    QQmlEngine::setObjectOwnership(m_rootEntity, QQmlEngine::CppOwnership);

    m_rootEntity->setObjectName(QStringLiteral("__internal root entity"));

    // Create a component cache for components that are needed after Load/New/possible other
    // reason for deleting scene root (m_sceneEntity)
    m_componentCache = new Qt3DCore::QEntity(m_rootEntity);
    m_componentCache->setObjectName("__internal component cache");
    m_componentCache->setEnabled(false);

    // Selection box material and mesh need to be created before any
    // EditorSceneItem are created
    Qt3DExtras::QPhongMaterial *selectionBoxMaterial = new Qt3DExtras::QPhongMaterial();
    selectionBoxMaterial->setAmbient(selectionBoxColor);
    selectionBoxMaterial->setDiffuse(QColor(Qt::black));
    selectionBoxMaterial->setSpecular(QColor(Qt::black));
    selectionBoxMaterial->setShininess(0);
    m_selectionBoxMaterial = selectionBoxMaterial;
    m_selectionBoxMesh = EditorUtils::createWireframeBoxMesh();

    m_meshCenterIndicatorLine = new Qt3DCore::QEntity();
    m_meshCenterIndicatorLine->setObjectName(QStringLiteral("__internal mesh center indicator line"));
    Qt3DRender::QGeometryRenderer *indicatorMesh = EditorUtils::createSingleLineMesh();

    Qt3DRender::QMaterial *meshCenterLineMaterial = new Qt3DRender::QMaterial();
    meshCenterLineMaterial->setEffect(new OnTopEffect());
    meshCenterLineMaterial->addParameter(new Qt3DRender::QParameter(QStringLiteral("handleColor"),
                                                                    selectionBoxColor));

    m_meshCenterIndicatorLineTransform = new Qt3DCore::QTransform();
    m_meshCenterIndicatorLine->addComponent(indicatorMesh);
    m_meshCenterIndicatorLine->addComponent(meshCenterLineMaterial);
    m_meshCenterIndicatorLine->addComponent(m_meshCenterIndicatorLineTransform);
    m_meshCenterIndicatorLine->setParent(m_rootEntity);
    m_meshCenterIndicatorLine->setEnabled(false);

    // Save to cache, as these are needed after Load/New
    m_componentCache->addComponent(m_selectionBoxMesh);
    m_componentCache->addComponent(m_selectionBoxMaterial);

    m_rootItem = new EditorSceneItem(this, m_rootEntity, nullptr, -1, this);

    m_sceneItems.insert(m_rootEntity->id(), m_rootItem);

    m_renderSettings = new Qt3DRender::QRenderSettings();
    m_renderSettings->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking);
    m_renderSettings->pickingSettings()->setPickResultMode(Qt3DRender::QPickingSettings::AllPicks);
    m_renderSettings->setObjectName(QStringLiteral("__internal Scene frame graph"));
    m_renderer = new Qt3DExtras::QForwardRenderer();
    m_renderer->setClearColor(Qt::lightGray);
    m_renderSettings->setActiveFrameGraph(m_renderer);

    // Setting the FrameGraph to actual root entity to protect it from accidental removal
    m_rootEntity->addComponent(m_renderSettings);

    m_rootEntity->addComponent(new Qt3DInput::QInputSettings());

    // Scene entity (i.e. the visible root)
    setSceneEntity();

    // Free view camera
    m_freeViewCameraEntity = new Qt3DRender::QCamera(m_rootEntity);
    m_freeViewCameraEntity->setObjectName(QStringLiteral("__internal free view camera"));
    resetFreeViewCamera();

    // Helper plane
    createHelperPlane();

    // The drag handles translation is same as the selection box + a specified distance
    // depending on the scale of the box.
    m_dragHandlesTransform = new Qt3DCore::QTransform();
    m_dragHandleRotateTransform = new Qt3DCore::QTransform();
    m_dragHandleTranslateTransform = new Qt3DCore::QTransform();
    // Grab explicit ownership of drag transforms as they are not going to be part of the scene
    QQmlEngine::setObjectOwnership(m_dragHandlesTransform, QQmlEngine::CppOwnership);
    QQmlEngine::setObjectOwnership(m_dragHandleRotateTransform, QQmlEngine::CppOwnership);
    QQmlEngine::setObjectOwnership(m_dragHandleTranslateTransform, QQmlEngine::CppOwnership);

    m_dragHandleScaleTransforms.resize(dragCornerHandleCount);
    m_dragHandleCornerAdjustments.resize(dragCornerHandleCount);
    for (int i = 0; i < dragCornerHandleCount; i++) {
        m_dragHandleScaleTransforms[i] = new Qt3DCore::QTransform();
        QQmlEngine::setObjectOwnership(m_dragHandleScaleTransforms.at(i), QQmlEngine::CppOwnership);
    }
    m_dragHandleCornerAdjustments[0] = QVector3D(-1.0f, -1.0f, -1.0f);
    m_dragHandleCornerAdjustments[1] = QVector3D(-1.0f, -1.0f,  1.0f);
    m_dragHandleCornerAdjustments[2] = QVector3D(-1.0f,  1.0f, -1.0f);
    m_dragHandleCornerAdjustments[3] = QVector3D(-1.0f,  1.0f,  1.0f);
    m_dragHandleCornerAdjustments[4] = QVector3D( 1.0f, -1.0f, -1.0f);
    m_dragHandleCornerAdjustments[5] = QVector3D( 1.0f, -1.0f,  1.0f);
    m_dragHandleCornerAdjustments[6] = QVector3D( 1.0f,  1.0f, -1.0f);
    m_dragHandleCornerAdjustments[7] = QVector3D( 1.0f,  1.0f,  1.0f);

    m_dragEntitySnapOffsets.resize(dragCornerHandleCount);

    // Active scene camera frustum visualization
    m_activeSceneCameraFrustumData.frustumEntity = new Qt3DCore::QEntity(m_rootEntity);
    m_activeSceneCameraFrustumData.viewVectorEntity = new Qt3DCore::QEntity(m_rootEntity);
    m_activeSceneCameraFrustumData.viewCenterEntity = new Qt3DCore::QEntity(m_rootEntity);

    m_activeSceneCameraFrustumData.frustumMesh = EditorUtils::createWireframeBoxMesh();
    Qt3DRender::QGeometryRenderer *viewVectorMesh = EditorUtils::createCameraViewVectorMesh();
    Qt3DRender::QGeometryRenderer *viewCenterMesh = EditorUtils::createCameraViewCenterMesh(1.0f);

    Qt3DExtras::QPhongMaterial *frustumMaterial = new Qt3DExtras::QPhongMaterial();
    frustumMaterial->setAmbient(cameraFrustumColor);
    frustumMaterial->setDiffuse(QColor(Qt::black));
    frustumMaterial->setSpecular(QColor(Qt::black));
    frustumMaterial->setShininess(0);

    m_activeSceneCameraFrustumData.frustumTransform = new Qt3DCore::QTransform();
    m_activeSceneCameraFrustumData.viewVectorTransform = new Qt3DCore::QTransform();
    m_activeSceneCameraFrustumData.viewCenterTransform = new Qt3DCore::QTransform();

    m_activeSceneCameraFrustumData.frustumEntity->addComponent(frustumMaterial);
    m_activeSceneCameraFrustumData.frustumEntity->addComponent(
                m_activeSceneCameraFrustumData.frustumMesh);
    m_activeSceneCameraFrustumData.frustumEntity->addComponent(
                m_activeSceneCameraFrustumData.frustumTransform);

    m_activeSceneCameraFrustumData.viewVectorEntity->addComponent(frustumMaterial);
    m_activeSceneCameraFrustumData.viewVectorEntity->addComponent(viewVectorMesh);
    m_activeSceneCameraFrustumData.viewVectorEntity->addComponent(
                m_activeSceneCameraFrustumData.viewVectorTransform);

    m_activeSceneCameraFrustumData.viewCenterEntity->addComponent(frustumMaterial);
    m_activeSceneCameraFrustumData.viewCenterEntity->addComponent(viewCenterMesh);
    m_activeSceneCameraFrustumData.viewCenterEntity->addComponent(
                m_activeSceneCameraFrustumData.viewCenterTransform);
}

void EditorScene::createHelperPlane()
{
    m_helperPlane = new Qt3DCore::QEntity();

    m_helperPlane->setObjectName(QStringLiteral("__internal helper plane"));

    // Helper plane origin must be at the meeting point of lines, hence the odd lineCount
    Qt3DRender::QGeometryRenderer *planeMesh = EditorUtils::createWireframePlaneMesh(51);

    Qt3DExtras::QPhongMaterial *helperPlaneMaterial = new Qt3DExtras::QPhongMaterial();
    helperPlaneMaterial->setAmbient(helperPlaneColor);
    helperPlaneMaterial->setDiffuse(QColor(Qt::black));
    helperPlaneMaterial->setSpecular(QColor(Qt::black));
    helperPlaneMaterial->setShininess(0);

    m_helperPlaneTransform = new Qt3DCore::QTransform();
    m_helperPlaneTransform->setScale3D(QVector3D(m_gridSize * 25.0f, m_gridSize * 25.0f, 1.0f));
    m_helperPlaneTransform->setRotation(
                m_helperPlaneTransform->fromAxisAndAngle(1.0f, 0.0f, 0.0f, 90.0f));
    m_helperPlane->addComponent(planeMesh);
    m_helperPlane->addComponent(helperPlaneMaterial);
    m_helperPlane->addComponent(m_helperPlaneTransform);
    m_helperPlane->setParent(m_rootEntity);
}

void EditorScene::setFrameGraphCamera(Qt3DCore::QEntity *cameraEntity)
{
    if (m_renderer) {
        Qt3DCore::QTransform *cameraTransform = nullptr;
        Qt3DRender::QCamera *currentCamera =
                qobject_cast<Qt3DRender::QCamera *>(m_renderer->camera());
        if (currentCamera) {
            cameraTransform = currentCamera->transform();
            if (cameraTransform) {
                disconnect(cameraTransform, &Qt3DCore::QTransform::matrixChanged,
                           this, &EditorScene::handleSelectionTransformChange);
            }
        }
        m_renderer->setCamera(cameraEntity);
        currentCamera = qobject_cast<Qt3DRender::QCamera *>(cameraEntity);
        if (currentCamera) {
            cameraTransform = currentCamera->transform();
            if (cameraTransform) {
                connect(cameraTransform, &Qt3DCore::QTransform::matrixChanged,
                        this, &EditorScene::handleSelectionTransformChange);
            }
        }
        // This will update drag handle positions if needed
        handleSelectionTransformChange();
    }
}

Qt3DRender::QCamera *EditorScene::frameGraphCamera() const
{
    if (m_renderer)
        return qobject_cast<Qt3DRender::QCamera *>(m_renderer->camera());
    else
        return nullptr;
}

void EditorScene::setSelection(Qt3DCore::QEntity *entity)
{
    // Setting selection implies end to multiSelection
    if (m_selectedEntityNameList.size())
        clearMultiSelection();
    EditorSceneItem *item = m_sceneItems.value(entity->id(), nullptr);
    if (item) {
        if (entity != m_selectedEntity) {
            clearSingleSelection();

            m_selectedEntity = entity;

            if (m_selectedEntity) {
                connectDragHandles(item, true);
                if (m_selectedEntity != m_sceneEntity)
                    item->setShowSelectionBox(true);
                m_selectedEntityTransform = EditorUtils::entityTransform(m_selectedEntity);
            }

            // Emit signal to highlight the entity from the list
            emit selectionChanged(m_selectedEntity);
        }
        m_dragHandlesTransform->setEnabled(item->isSelectionBoxShowing());

        if (item->itemType() == EditorSceneItem::Camera) {
            // Disable scale handles for cameras
            m_dragHandleScaleTransforms.at(0)->setEnabled(false);
            m_dragHandleRotateTransform->setEnabled(!isPropertyLocked(QStringLiteral("upVector"),
                                                                      m_selectedEntity));
            m_dragHandleTranslateTransform->setEnabled(
                        !isPropertyLocked(QStringLiteral("position"), m_selectedEntity));
            m_viewCenterLocked = isPropertyLocked(QStringLiteral("viewCenter"), m_selectedEntity);
        } else {
            Qt3DCore::QTransform *transform = EditorUtils::entityTransform(m_selectedEntity);
            bool transformPropertiesLocked = item->customProperty(m_selectedEntity,
                                                                  lockTransformPropertyName()).toBool();
            if (transformPropertiesLocked) {
                m_dragHandleTranslateTransform->setEnabled(false);
            } else {
                m_dragHandleTranslateTransform->setEnabled(
                            !isPropertyLocked(QStringLiteral("translation"), transform));
            }
            if (item->itemType() == EditorSceneItem::Light) {
                // Disable scale handles for lights
                m_dragHandleScaleTransforms.at(0)->setEnabled(false);
                // Some lights can rotate
                if (item->canRotate()) {
                    Qt3DRender::QAbstractLight *light = EditorUtils::entityLight(m_selectedEntity);
                    QString lockProperty =
                            qobject_cast<Qt3DRender::QDirectionalLight *>(light)
                            ? QStringLiteral("worldDirection") : QStringLiteral("localDirection");
                    m_dragHandleRotateTransform->setEnabled(
                                !isPropertyLocked(lockProperty, light));
                } else {
                    m_dragHandleRotateTransform->setEnabled(false);
                }
            } else {
                if (transformPropertiesLocked) {
                    m_dragHandleScaleTransforms.at(0)->setEnabled(false);
                    m_dragHandleRotateTransform->setEnabled(false);
                } else {
                    m_dragHandleScaleTransforms.at(0)->setEnabled(!isPropertyLocked(QStringLiteral("scale3D"),
                                                                                    transform));
                    m_dragHandleRotateTransform->setEnabled(!isPropertyLocked(QStringLiteral("rotation"),
                                                                              transform));
                }
            }
        }

        // Update drag handles transforms to initial state
        handleSelectionTransformChange();
    } else {
        m_dragHandlesTransform->setEnabled(false);
        if (m_selectedEntity != m_sceneEntity)
            setSelection(m_sceneEntity);
    }
}

void EditorScene::toggleEntityMultiSelection(const QString &name)
{
    // If the new one is already in, remove it. Otherwise add it.
    if (m_selectedEntityNameList.contains(name))
        removeEntityFromMultiSelection(name);
    else
        addEntityToMultiSelection(name);
}

void EditorScene::clearMultiSelection()
{
    if (m_selectedEntityNameList.size() > 0) {
        m_selectedEntityNameList.clear();
        checkMultiSelectionHighlights();
        emit multiSelectionChanged(false);
        emit multiSelectionListChanged();
        setSelection(m_sceneEntity);
    }
}

QVector3D EditorScene::getMultiSelectionCenter()
{
    QVector3D pos;
    for (int i = 0; i < m_selectedEntityNameList.size(); i++) {
        EditorSceneItem *item = itemByName(m_selectedEntityNameList.at(i));
        if (item) {
            item->doUpdateSelectionBoxTransform();
            pos += item->selectionBoxCenter();
        }
    }
    return m_selectedEntityNameList.size() ? (pos / m_selectedEntityNameList.size()) : QVector3D();
}

void EditorScene::addEntityToMultiSelection(const QString &name)
{
    const int oldSize = m_selectedEntityNameList.size();
    if (oldSize == 0) {
        // Do not add if multiselecting the currently selected entity as the first entity
        if (m_selectedEntity->objectName() == name)
            return;

        if (m_selectedEntity != m_sceneEntity) {
            m_selectedEntityNameList.append(m_selectedEntity->objectName());
            m_dragHandlesTransform->setEnabled(false);
            handleSelectionTransformChange();
            clearSingleSelection();
        } else {
            // Just single-select the new entity and return if the other entity was scene entity
            EditorSceneItem *item = m_sceneModel->getItemByName(name);
            if (item) {
                setSelection(item->entity());
                return;
            }
        }
    }
    m_selectedEntityNameList.append(name);

    checkMultiSelectionHighlights();

    if (oldSize == 0)
        emit multiSelectionChanged(true);
    emit multiSelectionListChanged();
}

void EditorScene::removeEntityFromMultiSelection(const QString &name)
{
    bool removed = m_selectedEntityNameList.removeOne(name);

    if (removed) {
        bool lastRemoved = m_selectedEntityNameList.size() == 1;
        EditorSceneItem *lastItem = nullptr;
        if (lastRemoved) {
            lastItem = m_sceneModel->getItemByName(m_selectedEntityNameList.at(0));
            m_selectedEntityNameList.clear();
            emit multiSelectionChanged(false);
        }
        checkMultiSelectionHighlights();
        emit multiSelectionListChanged();
        if (lastRemoved) {
            if (lastItem)
                setSelection(lastItem->entity());
            else
                setSelection(m_sceneEntity);
        }
    }
}

void EditorScene::renameEntityInMultiSelectionList(const QString &oldName, const QString &newName)
{
    int index = m_selectedEntityNameList.indexOf(oldName);
    if (index > 0)
        m_selectedEntityNameList.replace(index, newName);
}

void EditorScene::setClipboardOperation(ClipboardOperation operation)
{
    if (operation != m_clipboardOperation) {
        m_clipboardOperation = operation;
        emit clipboardOperationChanged(operation);
        if (operation == ClipboardNone) {
            m_clipboardEntityName.clear();
            emit clipboardContentChanged(m_clipboardEntityName);
        }
    }
}

void EditorScene::setClipboardContent(const QString &entityName)
{
    if (entityName != m_clipboardEntityName) {
        m_clipboardEntityName = entityName;
        emit clipboardContentChanged(entityName);
    }
}

void EditorScene::setActiveSceneCameraIndex(int index)
{
    int previousIndex = m_activeSceneCameraIndex;
    if (index >= 0 && index < m_sceneCameras.size())
        m_activeSceneCameraIndex = index;
    else if (m_sceneCameras.size())
        m_activeSceneCameraIndex = 0;
    else
        m_activeSceneCameraIndex = -1;

    // Reset camera even if index didn't change, as it might point to a different camera
    if (m_activeSceneCameraIndex >= 0) {
        if (!m_freeView)
            setFrameGraphCamera(m_sceneCameras.at(m_activeSceneCameraIndex).cameraEntity);
        updateVisibleSceneCameraMatrix(m_sceneCameras.at(m_activeSceneCameraIndex));
    } else {
        setFreeView(true);
    }

    if (m_freeView)
        enableVisibleCameras(bool(m_sceneCameras.size()));

    if (previousIndex != m_activeSceneCameraIndex)
        emit activeSceneCameraIndexChanged(m_activeSceneCameraIndex);
}

void EditorScene::setFreeView(bool enable)
{
    // Force freeview if no active scene cameras available
    if (!enable && (m_activeSceneCameraIndex < 0 || m_activeSceneCameraIndex >= m_sceneCameras.size()))
        enable = true;

    if (m_freeView != enable) {
        m_freeView = enable;

        // Set free view when trying to change to invalid camera
        if (m_freeView)
            setFrameGraphCamera(m_freeViewCameraEntity);
        else
            setFrameGraphCamera(m_sceneCameras.at(m_activeSceneCameraIndex).cameraEntity);
        enableVisibleCameras(m_freeView);
        enableVisibleLights(m_freeView);
    }
    // Show / hide light meshes, and notify UI. Need to be emitted always even if it doesn't change,
    // as otherwise the UI can change the checked status of the menu item on click even if
    // the status doesn't really change.
    emit freeViewChanged(m_freeView);
}

void EditorScene::setViewport(EditorViewportItem *viewport)
{
    if (m_viewport != viewport) {
        if (m_viewport)
            disconnect(m_viewport, 0, this, 0);

        m_viewport = viewport;
        connect(viewport, &EditorViewportItem::heightChanged,
                this, &EditorScene::handleViewportSizeChange);
        connect(viewport, &EditorViewportItem::widthChanged,
                this, &EditorScene::handleViewportSizeChange);
        handleViewportSizeChange();

        // Set the viewport up as a source of input events for the input aspect
        Qt3DInput::QInputSettings *inputSettings =
                m_rootEntity->findChild<Qt3DInput::QInputSettings *>();
        if (inputSettings) {
            inputSettings->setEventSource(viewport);
        } else {
            qWarning() << "No Input Settings found, keyboard and mouse events won't be handled";
        }

        emit viewportChanged(viewport);
    }
}

void EditorScene::clearSelectionBoxes(Qt3DCore::QEntity *skipEntity)
{
    Q_FOREACH (EditorSceneItem *item, m_sceneItems.values()) {
        if (item->entity() != skipEntity)
            item->setShowSelectionBox(false);
    }
}

void EditorScene::endSelectionHandling()
{
    if (m_dragMode == DragNone && m_pickedEntity) {
        if (m_ctrlDownOnLastLeftPress) {
            // Multiselection handling
            toggleEntityMultiSelection(m_pickedEntity->objectName());
        } else {
            // Single selection handling
            setSelection(m_pickedEntity);

            // Selecting an object also starts drag, if translate handle is enabled
            Qt3DRender::QCamera *cameraEntity = qobject_cast<Qt3DRender::QCamera *>(m_pickedEntity);
            bool viewCenterDrag = cameraEntity && m_cameraViewCenterSelected && !m_viewCenterLocked;
            bool entityDrag = m_dragHandleTranslateTransform->isEnabled()
                    && m_dragHandlesTransform->isEnabled()
                    && (!cameraEntity || !m_cameraViewCenterSelected);
            if (viewCenterDrag || entityDrag) {
                // The mouse position passed to dragHandlePress is irrelevant in this case
                dragHandlePress(DragTranslate, QPoint(0, 0), 0);
            }
        }
        m_pickedEntity = nullptr;
        m_pickedDistance = -1.0f;
    }
}

void EditorScene::handleSelectionTransformChange()
{
    EditorSceneItem *item = nullptr;
    if (m_selectedEntity && m_selectedEntity != m_sceneEntity)
        item = m_sceneItems.value(m_selectedEntity->id(), nullptr);
    QPoint translatePoint;
    QPoint centerPoint;
    QVector3D translateHandlePos;
    QVector3D itemCenterHandlePos;
    QVector3D rotateHandlePos;
    QVector3D cornerHandlePositions[dragCornerHandleCount];
    bool showCenterHandle = false;

    resizeCameraViewCenterEntity();

    if (item) {
        Qt3DRender::QCamera *camera = frameGraphCamera();

        m_dragHandlesTransform->setTranslation(item->selectionBoxCenter());
        m_dragHandlesTransform->setRotation(item->selectionTransform()->rotation());

        QVector3D translation = (item->selectionBoxExtents() / 2.0f);

        // m_dragHandleTranslateTransform indicates the mesh center position in drag handles
        // coordinates, i.e. the position of the secondary translate handle.
        // The primary handle at the center of the selection box always has zero translation.
        QMatrix4x4 entityMatrix = EditorUtils::totalAncestralTransform(item->entity())
                * item->entityTransform()->matrix();
        QVector3D meshCenter = m_dragHandlesTransform->matrix().inverted() * entityMatrix
                * QVector3D();
        m_dragHandleTranslateTransform->setTranslation(meshCenter);

        // Find out x/y viewport positions of drag handles

        translateHandlePos = EditorUtils::projectRay(
                    camera->viewMatrix(), camera->projectionMatrix(),
                    m_viewport->width(), m_viewport->height(),
                    m_dragHandlesTransform->matrix() * QVector3D());

        for (int i = 0; i < dragCornerHandleCount; i++) {
            m_dragHandleScaleTransforms.at(i)->setTranslation(
                        translation * m_dragHandleCornerAdjustments.at(i));

            cornerHandlePositions[i] = EditorUtils::projectRay(
                        camera->viewMatrix(), camera->projectionMatrix(),
                        m_viewport->width(), m_viewport->height(),
                        m_dragHandlesTransform->matrix()
                        * m_dragHandleScaleTransforms.at(i)->matrix() * QVector3D());
        }

        // Try to position rotate handle to the upper right corner of the selection box, as
        // drawn on the screen. However, we don't change the corner during drag-rotation.
        const float handleDistance = 12.0f;
        if (m_dragMode != DragRotate) {
            float maxDelta = 0.0f;
            int rotateHandleIndex = 0;
            for (int i = 0; i < dragCornerHandleCount; i++) {
                float delta = (cornerHandlePositions[i].x() - translateHandlePos.x())
                        + (translateHandlePos.y() - cornerHandlePositions[i].y());
                if (delta > maxDelta) {
                    rotateHandleIndex = i;
                    maxDelta = delta;
                }
            }
            m_dragHandleRotationAdjustment = m_dragHandleCornerAdjustments.at(rotateHandleIndex);
            rotateHandlePos = cornerHandlePositions[rotateHandleIndex]
                    + QVector3D(handleDistance, -handleDistance, 0.0f);
        }

        m_dragHandleRotateTransform->setTranslation(translation * m_dragHandleRotationAdjustment);

        if (m_dragMode == DragRotate) {
            // When drag-rotating, we can't rely on drag handle being on top right, so we need
            // to calculate the correct screen position so that it is always away from the
            // object center.
            rotateHandlePos = EditorUtils::projectRay(
                        camera->viewMatrix(), camera->projectionMatrix(),
                        m_viewport->width(), m_viewport->height(),
                        m_dragHandlesTransform->matrix() * m_dragHandleRotateTransform->matrix()
                        * QVector3D());

            // Get another point to help adjust rotation handle position
            QMatrix4x4 rotateHelperMatrix;
            rotateHelperMatrix.translate(translation * m_dragHandleRotationAdjustment * 1.1f);
            QVector3D rotateHelperPos = EditorUtils::projectRay(
                        camera->viewMatrix(), camera->projectionMatrix(),
                        m_viewport->width(), m_viewport->height(),
                        m_dragHandlesTransform->matrix() * rotateHelperMatrix * QVector3D());

            // Adjust rotate handle position number of pixels towards the helper point
            QVector3D adjustVector = rotateHelperPos - rotateHandlePos;
            adjustVector.normalize();
            adjustVector *= handleDistance * float(qSqrt(2.0));
            rotateHandlePos += adjustVector;
        }

        translatePoint = QPoint(translateHandlePos.x(), translateHandlePos.y());
        if (item->itemType() != EditorSceneItem::Camera
                && item->itemType() != EditorSceneItem::Light) {
            itemCenterHandlePos = EditorUtils::projectRay(
                        camera->viewMatrix(), camera->projectionMatrix(),
                        m_viewport->width(), m_viewport->height(),
                        m_dragHandlesTransform->matrix() * m_dragHandleTranslateTransform->matrix()
                        * QVector3D());
            centerPoint = QPoint(itemCenterHandlePos.x(), itemCenterHandlePos.y());
            showCenterHandle = translatePoint != centerPoint;
        }
        if (showCenterHandle) {
            QQuaternion rot = QQuaternion::rotationTo(QVector3D(0.0f, 0.0f, 1.0f),
                                                      meshCenter.normalized());
            m_meshCenterIndicatorLineTransform->setRotation(
                        m_dragHandlesTransform->rotation() * rot);
            m_meshCenterIndicatorLineTransform->setTranslation(
                        m_dragHandlesTransform->translation());
            m_meshCenterIndicatorLineTransform->setScale(meshCenter.length());
        }
    }
    m_meshCenterIndicatorLine->setEnabled(showCenterHandle && m_dragHandlesTransform->isEnabled());

    // Signal UI to reposition drag handles
    emit beginDragHandlesRepositioning();
    emit repositionDragHandle(DragTranslate, translatePoint,
                              m_dragHandlesTransform->isEnabled()
                              ? m_dragHandleTranslateTransform->isEnabled()
                                && translateHandlePos.z() > 0.0f : false, 0,
                              translateHandlePos.z());
    emit repositionDragHandle(DragTranslate, centerPoint,
                              m_dragHandlesTransform->isEnabled()
                              ? m_dragHandleTranslateTransform->isEnabled()
                                && itemCenterHandlePos.z() > 0.0f && showCenterHandle : false,
                              1, itemCenterHandlePos.z());
    emit repositionDragHandle(DragRotate, QPoint(rotateHandlePos.x(), rotateHandlePos.y()),
                              m_dragHandlesTransform->isEnabled()
                              ? m_dragHandleRotateTransform->isEnabled()
                                && rotateHandlePos.z() > 0.0f : false, 0, rotateHandlePos.z());
    for (int i = 0; i < dragCornerHandleCount; i++) {
        emit repositionDragHandle(DragScale,
                                  QPoint(cornerHandlePositions[i].x(),
                                         cornerHandlePositions[i].y()),
                                  m_dragHandlesTransform->isEnabled()
                                  ? m_dragHandleScaleTransforms.at(0)->isEnabled()
                                    && cornerHandlePositions[i].z() > 0.0f : false, i,
                                  cornerHandlePositions[i].z());
    }
    emit endDragHandlesRepositioning();
}

void EditorScene::handlePickerPress(Qt3DRender::QPickEvent *event)
{
    if (m_dragMode == DragNone && m_mouseButton == Qt::LeftButton) {
        Qt3DCore::QEntity *pressedEntity = qobject_cast<Qt3DCore::QEntity *>(sender()->parent());
        // If pressedEntity is not enabled, it typically means the pressedEntity is a drag handle
        // and the selection has changed to a different type of entity since the mouse press was
        // registered. Since the new entity is not the one we wanted to modify anyway, just
        // skip handling the pick event.
        if (pressedEntity->isEnabled()) {
            if (pressedEntity && (!m_pickedEntity || m_pickedDistance > event->distance())) {
                // Ignore presses that are farther away than the closest one
                m_pickedDistance = event->distance();
                bool select = false;
                EditorSceneItem *item = m_sceneItems.value(pressedEntity->id(), nullptr);
                if (item) {
                    select = true;
                } else if (m_freeView) {
                    if (pressedEntity == m_activeSceneCameraFrustumData.viewCenterEntity) {
                        // Select the active scene camera instead if clicked on view center
                        pressedEntity = m_sceneCameras.at(m_activeSceneCameraIndex).cameraEntity;
                        select = true;
                        m_cameraViewCenterSelected = true;
                    } else if (pressedEntity->objectName() == cameraVisibleEntityName) {
                        // Select the camera instead if clicked on camera cone
                        for (int i = 0; i < m_sceneCameras.size(); i++) {
                            if (m_sceneCameras.at(i).visibleEntity == pressedEntity) {
                                pressedEntity = m_sceneCameras.at(i).cameraEntity;
                                select = true;
                                m_cameraViewCenterSelected = false;
                                break;
                            }
                        }
                    } else if (pressedEntity->objectName() == lightVisibleEntityName) {
                        // Select the light instead if clicked on visible light mesh
                        Q_FOREACH (LightData *lightData, m_sceneLights.values()) {
                            if (lightData->visibleEntity == pressedEntity) {
                                pressedEntity = lightData->lightEntity;
                                select = true;
                                break;
                            }
                        }
                    } else if (pressedEntity->objectName() == sceneLoaderSubEntityName) {
                        // Select the scene loader entity instead when picking one of loader's
                        // internal mesh entites.
                        Qt3DCore::QEntity *parentEntity = pressedEntity->parentEntity();
                        while (parentEntity) {
                            EditorSceneItem *parentItem = m_sceneItems.value(parentEntity->id());
                            if (parentItem) {
                                pressedEntity = parentEntity;
                                select = true;
                                break;
                            } else {
                                parentEntity = parentEntity->parentEntity();
                            }
                        }
                    }
                }
                if (select && !m_pickedEntity)
                    QMetaObject::invokeMethod(this, "endSelectionHandling", Qt::QueuedConnection);
                m_pickedEntity = pressedEntity;
            }
        }
    }
    event->setAccepted(true);
}

bool EditorScene::handleMousePress(QMouseEvent *event)
{
    m_previousMousePosition = event->pos();
    m_mouseButton = event->button();
    if (m_mouseButton == Qt::LeftButton)
        m_ctrlDownOnLastLeftPress = event->modifiers() & Qt::ControlModifier;
    cancelDrag();
    return false; // Never consume press event
}

bool EditorScene::handleMouseRelease(QMouseEvent *event)
{
    if (event->button() == Qt::RightButton) {
        QPoint delta = event->pos() - m_previousMousePosition;
        if (delta.manhattanLength() < 5 && (m_dragMode == DragNone || m_ignoringInitialDrag))
            emit mouseRightButtonReleasedWithoutDragging();
    }
    m_cameraViewCenterSelected = false;
    cancelDrag();
    return false; // Never consume release event
}

bool EditorScene::handleMouseMove(QMouseEvent *event)
{
    dragHandleMove(event->pos(), event->modifiers() & Qt::ShiftModifier,
                   event->modifiers() & Qt::ControlModifier,
                   event->modifiers() & Qt::AltModifier);
    return (m_dragMode != DragNone);
}

// Find out the normal of the helper plane.
QVector3D EditorScene::helperPlaneNormal() const
{
    QVector3D helperPlaneNormal = m_helperPlaneTransform->matrix() * QVector3D(0.0f, 0.0f, 1.0f);
    helperPlaneNormal.setX(qAbs(helperPlaneNormal.x()));
    helperPlaneNormal.setY(qAbs(helperPlaneNormal.y()));
    helperPlaneNormal.setZ(qAbs(helperPlaneNormal.z()));
    return helperPlaneNormal.normalized();
}

// Projects vector to a plane defined by active frame graph camera
QVector3D EditorScene::projectVectorOnCameraPlane(const QVector3D &vector) const
{
    QVector3D projectionVector;
    Qt3DRender::QCamera *camera = frameGraphCamera();
    if (camera) {
        QVector3D planeNormal = camera->position() - camera->viewCenter();
        planeNormal.normalize();
        projectionVector = EditorUtils::projectVectorOnPlane(vector, planeNormal);
        // Have some valid vector at least if vector is too close to zero
        if (projectionVector.length() < 0.00001f) {
            projectionVector = QVector3D::crossProduct(planeNormal,
                                                       camera->upVector().normalized());
        }
    }
    return projectionVector;
}

void EditorScene::resizeCameraViewCenterEntity()
{
    if (frameGraphCamera()) {
        // Rescale the camera viewcenter entity according to distance, as it is draggable
        const float vcEntityAngle = 0.0045f;
        QVector3D vcPos = m_activeSceneCameraFrustumData.viewCenterTransform->translation();
        float distanceToVc = (vcPos - frameGraphCamera()->position()).length();
        float vcScale = vcEntityAngle * distanceToVc;
        m_activeSceneCameraFrustumData.viewCenterTransform->setScale(vcScale * 2.0f);
    }
}

bool EditorScene::isPropertyLocked(const QString &propertyName, QObject *obj)
{
    if (!obj)
        return false;
    QString lockProperty = propertyName + lockPropertySuffix();
    QByteArray nameArray = lockProperty.toLatin1();
    const char *namePtr = nameArray.constData();
    QVariant propertyVariant = obj->property(namePtr);
    if (propertyVariant.isValid())
        return propertyVariant.toBool();
    else
        return false;
}

void EditorScene::cancelDrag()
{
    m_dragMode = DragNone;
    m_pickedEntity = nullptr;
    m_pickedDistance = -1.0f;
    m_dragEntity = nullptr;
    m_ignoringInitialDrag = true;
    m_dragHandleIndex = 0;
}

void EditorScene::setSceneEntity(Qt3DCore::QEntity *newSceneEntity)
{
    if (newSceneEntity)
        m_sceneEntity = newSceneEntity;
    else
        m_sceneEntity = new Qt3DCore::QEntity();
    m_sceneEntity->setObjectName(m_sceneRootString);
    addEntity(m_sceneEntity);
}

void EditorScene::createSceneLoaderChildPickers(Qt3DCore::QEntity *entity,
                                                QList<Qt3DRender::QObjectPicker *> *pickers)
{
    if (EditorUtils::entityMesh(entity)) {
        pickers->append(createObjectPickerForEntity(entity));
        // Rename entity so we can identify it later
        entity->setObjectName(sceneLoaderSubEntityName);
    }

    Q_FOREACH (QObject *child, entity->children()) {
        Qt3DCore::QEntity *childEntity = qobject_cast<Qt3DCore::QEntity *>(child);
        if (childEntity)
            createSceneLoaderChildPickers(childEntity, pickers);
    }
}

void EditorScene::handleCameraAdded(Qt3DRender::QCamera *camera)
{
    Qt3DCore::QEntity *visibleEntity = new Qt3DCore::QEntity(m_rootEntity);

    visibleEntity->setObjectName(cameraVisibleEntityName);

    Qt3DRender::QGeometryRenderer *visibleMesh = EditorUtils::createVisibleCameraMesh();

    Qt3DExtras::QPhongMaterial *cameraMaterial = new Qt3DExtras::QPhongMaterial();
    cameraMaterial->setAmbient(QColor(Qt::black));
    cameraMaterial->setDiffuse(QColor(Qt::black));
    cameraMaterial->setSpecular(QColor(Qt::black));
    cameraMaterial->setShininess(0);

    Qt3DCore::QTransform *visibleTransform = new Qt3DCore::QTransform();

    visibleEntity->addComponent(visibleMesh);
    visibleEntity->addComponent(cameraMaterial);
    visibleEntity->addComponent(visibleTransform);

    CameraData newData(camera, visibleEntity, visibleTransform, nullptr);
    enableVisibleCamera(newData, m_freeView, false);
    m_sceneCameras.append(newData);

    connectSceneCamera(newData);
    updateVisibleSceneCameraMatrix(newData);

    int newRow = m_sceneCamerasModel.rowCount();
    m_sceneCamerasModel.insertRow(newRow);
    m_sceneCamerasModel.setData(m_sceneCamerasModel.index(newRow),
                                QVariant::fromValue(camera->objectName()),
                                Qt::DisplayRole);

    // Activate the newly added camera if it is the only existing scene camera
    if (m_sceneCameras.size() == 1)
        setActiveSceneCameraIndex(0);
}

void EditorScene::handleCameraRemoved(Qt3DRender::QCamera *camera)
{
    int removeIndex = cameraIndexForEntity(camera);

    if (removeIndex >= 0) {
        delete m_sceneCameras.at(removeIndex).visibleEntity;
        m_sceneCameras.removeAt(removeIndex);
        m_sceneCamerasModel.removeRow(removeIndex);
        if (removeIndex <= m_activeSceneCameraIndex)
            setActiveSceneCameraIndex(m_activeSceneCameraIndex - 1);
    }

    if (!m_sceneCameras.length()) {
        m_activeSceneCameraFrustumData.frustumEntity->setEnabled(false);
        m_activeSceneCameraFrustumData.viewCenterEntity->setEnabled(false);
        m_activeSceneCameraFrustumData.viewVectorEntity->setEnabled(false);
    }
}

void EditorScene::handleLightAdded(Qt3DCore::QEntity *lightEntity)
{
    Qt3DCore::QEntity *visibleEntity = new Qt3DCore::QEntity(m_rootEntity);

    visibleEntity->setObjectName(lightVisibleEntityName);

    Qt3DCore::QTransform *visibleTransform = new Qt3DCore::QTransform();

    Qt3DRender::QAbstractLight *lightComponent = EditorUtils::entityLight(lightEntity);

    Qt3DExtras::QPhongAlphaMaterial *visibleMaterial = new Qt3DExtras::QPhongAlphaMaterial();
    visibleMaterial->setDiffuse(Qt::black);
    visibleMaterial->setSpecular(Qt::black);
    visibleMaterial->setAmbient(lightComponent->color());
    visibleMaterial->setAlpha(0.5f);

    visibleEntity->addComponent(visibleMaterial);
    visibleEntity->addComponent(visibleTransform);

    Qt3DCore::QTransform *lightTransform = EditorUtils::entityTransform(lightEntity);
    if (lightTransform) {
        connect(lightTransform, &Qt3DCore::QTransform::translationChanged,
                this, &EditorScene::handleLightTransformChange);
    }

    LightData *newData = new LightData(lightEntity, lightComponent, lightTransform, visibleEntity,
                                       visibleTransform, visibleMaterial, nullptr, nullptr);
    enableVisibleLight(*newData, m_freeView);
    m_sceneLights.insert(lightEntity->id(), newData);

    QMatrix4x4 matrix = EditorUtils::totalAncestralTransform(lightEntity);
    matrix.translate(lightTransform->translation());
    visibleTransform->setMatrix(matrix);

    handleLightTypeChanged(m_sceneItems.value(lightEntity->id()));
}

void EditorScene::handleLightRemoved(Qt3DCore::QEntity *lightEntity)
{
    LightData *lightData = m_sceneLights.value(lightEntity->id());
    if (lightData) {
        m_sceneLights.remove(lightEntity->id());
        delete lightData->visibleEntity;
        delete lightData;
    }
}

void EditorScene::connectSceneCamera(const CameraData &cameraData)
{
    connect(cameraData.cameraEntity, &Qt3DRender::QCamera::projectionMatrixChanged,
            this, &EditorScene::handleCameraMatrixChange);
    connect(cameraData.cameraEntity, &Qt3DRender::QCamera::viewMatrixChanged,
            this, &EditorScene::handleCameraMatrixChange);
    connect(cameraData.cameraEntity, &Qt3DRender::QCamera::viewVectorChanged,
            this, &EditorScene::handleCameraMatrixChange);
}

void EditorScene::handleCameraMatrixChange()
{
    Qt3DRender::QCamera *camera = qobject_cast<Qt3DRender::QCamera *>(sender());
    if (camera) {
        int changedIndex = cameraIndexForEntity(camera);
        if (changedIndex >= 0)
            updateVisibleSceneCameraMatrix(m_sceneCameras[changedIndex]);
    }
}

void EditorScene::handleLightTransformChange()
{
    Qt3DCore::QComponent *component = qobject_cast<Qt3DCore::QComponent *>(sender());
    if (component) {
        QVector<Qt3DCore::QEntity *> entities = component->entities();
        Qt3DCore::QEntity *entity = entities.size() ? entities.at(0) : nullptr;
        updateLightVisibleTransform(entity);
    }
}

void EditorScene::handleViewportSizeChange()
{
    qreal aspectRatio = m_viewport->width() / qMax(m_viewport->height(), 1.0);
    m_freeViewCameraEntity->lens()->setPerspectiveProjection(
                freeViewCameraFov, aspectRatio, freeViewCameraNearPlane, freeViewCameraFarPlane);
    // Need to update drag handle positions
    handleSelectionTransformChange();
}

void EditorScene::handleEntityNameChange()
{
    Qt3DCore::QEntity *entity = qobject_cast<Qt3DCore::QEntity *>(sender());
    int cameraIndex = cameraIndexForEntity(entity);
    if (cameraIndex >= 0) {
        m_sceneCamerasModel.setData(m_sceneCamerasModel.index(cameraIndex),
                                    QVariant::fromValue(entity->objectName()),
                                    Qt::DisplayRole);
    }
}

bool EditorScene::eventFilter(QObject *obj, QEvent *event)
{
    Q_UNUSED(obj)
    // Filter undo and redo keysequences so TextFields don't get them
    switch (event->type()) {
    case QEvent::KeyPress: {
        QKeyEvent *ke = static_cast<QKeyEvent *>(event);
        if (ke == QKeySequence::Redo) {
            if (m_undoHandler->canRedo())
                m_undoHandler->redo();
            return true;
        } else  if (ke == QKeySequence::Undo) {
            if (m_undoHandler->canUndo())
                m_undoHandler->undo();
            return true;
        }
        break;
    }
    case QEvent::MouseButtonPress:
        if (obj == m_viewport)
            return handleMousePress(static_cast<QMouseEvent *>(event));
        break;
    case QEvent::MouseButtonRelease:
        if (obj == m_viewport)
            return handleMouseRelease(static_cast<QMouseEvent *>(event));
        break;
    case QEvent::MouseMove:
        if (obj == m_viewport)
            return handleMouseMove(static_cast<QMouseEvent *>(event));
        break;
    default:
        break;
    }

    return false;
}

void EditorScene::checkMultiSelectionHighlights()
{
    const QList<EditorSceneItem *> items = m_sceneItems.values();
    for (int i = 0; i < items.size(); ++i) {
        EditorSceneItem *item = items.at(i);
        if (m_selectedEntityNameList.contains(item->entity()->objectName()))
            item->setShowSelectionBox(true);
        else
            item->setShowSelectionBox(false);
    }
}

QVector3D EditorScene::snapPosition(const QVector3D &worldPos, bool x, bool y, bool z)
{
    QVector3D newPos = worldPos;
    float shortestLen = FLT_MAX;
    int index = 0;
    QVector3D snapPos;
    // Snap nearest corner to grid intersection
    for (int i = 0; i < dragCornerHandleCount; ++i) {
        QVector3D corner = worldPos;
        QVector3D currentPos = worldPos;
        if (x) {
            corner.setX(worldPos.x() + m_dragEntitySnapOffsets.at(i).x());
            currentPos.setX(qRound(corner.x() / m_gridSize) * m_gridSize);
        }
        if (y) {
            corner.setY(worldPos.y() + m_dragEntitySnapOffsets.at(i).y());
            currentPos.setY(qRound(corner.y() / m_gridSize) * m_gridSize);
        }
        if (z) {
            corner.setZ(worldPos.z() + m_dragEntitySnapOffsets.at(i).z());
            currentPos.setZ(qRound(corner.z() / m_gridSize) * m_gridSize);
        }
        float len = (corner - currentPos).length();
        if (len < shortestLen) {
            shortestLen = len;
            snapPos = currentPos;
            index = i;
        }
    }
    if (x)
        newPos.setX(snapPos.x() - m_dragEntitySnapOffsets.at(index).x());
    if (y)
        newPos.setY(snapPos.y() - m_dragEntitySnapOffsets.at(index).y());
    if (z)
        newPos.setZ(snapPos.z() - m_dragEntitySnapOffsets.at(index).z());

    return newPos;
}