aboutsummaryrefslogtreecommitdiffstats
path: root/Game/Code/Screens/UScreenEditSub.pas
blob: 184a0a6ca408ad0ce0a56397feb5911e88a12ed2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
unit UScreenEditSub;

interface

uses
  UMenu,
  UVideo,
  TextGL,
  UMusic,
  URecord,
  SDL,
  SysUtils, UFiles, UTime, USongs, UIni, ULog, UTexture, UMenuText,
  ULyrics, Math, gl, UThemes, MidiOut, UHelp;

type
  TVidVis = (none, windowed, full);

  TMedleyNotes = record
    start: TPos;
    end_: TPos;
    Preview: TPos;
    isStart: boolean;   //start beat is declared
    isEnd: boolean;     //end beat is declared
  end;

  TVoicePitch = record
    beat:   integer;
    pitch:  integer;
  end;

  TScreenEditSub = class(TMenu)
    private
      PitchRecOn:   boolean;

      cRB, cGB, cBB:  GLfloat;
      cRR, cGR, cBR:  GLfloat;

      offset:       array[0..1] of integer;

      AktBeat:      integer;
      //Variable is True if no SOng is loaded
      Error:        Boolean;
      MP3Volume:    Integer;
      
      TextNote:     integer;
      TextSentence: integer;
      TextTitle:    integer;
      TextArtist:   integer;
      //TextMp3:      integer;
      TextBPM:      integer;
      TextGAP:      integer;
      TextDebug:    integer;
      TextNStart:   integer;
      TextNDlugosc: integer;
      TextNTon:     integer;
      TextNText:    integer;
      TextVideoGap:integer;
      AktNuta:      array[0..1] of integer;

      CP:           integer; //actual singer: 0 or 1 (for duet)
      EditorLyric:  array[0..1] of TLyric;

      PlaySentence:     boolean;
      PlaySentenceMidi: boolean;
      PlayOneNote:      boolean;
      PlayOneNoteMidi:  boolean;
      PlayOneSentence:  boolean;  //for mp3 and midi

      PlayStopTime: real;
      LastClick:    integer;
      Click:        boolean;
      CopySrcLine:  integer;
      CopySrcCP:    integer;

      MidiOut:      TMidiOutput;
      MidiStart:    real;
      MidiStop:     real;
      MidiTime:     real;
      MidiPos:      real;
      MidiLastNote: integer;


      TextEditMode: boolean;
      BPMEditMode:  boolean;

      MedleyNotes:  TMedleyNotes;

      editText:     string; //backup of current text in text-edit-mode
      noteStart:    integer; //Start note when playing sentence
      lineStart:    integer; //Start line when playing sentence
      cpStart:      integer; //Start singer when playing sentence
      LineChanged:  array[0..1] of boolean;

      VidVis:       TVidVis; //video visiability
      PlayVideo:    boolean;
      StartTry:     boolean;
      PlayTime:     real;

      ActTonePitch: integer;

      procedure DrawPitch(x, y, Width, Height: single; beat: integer);
      procedure StartVideo;
      procedure StartVideoPreview;
      procedure NewBeat;
      procedure ChangeBPM(newBPM: real);
      procedure CzesciDivide;
      procedure CzesciMultiply;
      procedure LyricsCapitalize;
      procedure LyricsCorrectSpaces;
      procedure FixTimings;
      procedure DivideSentence;
      procedure JoinSentence;
      procedure DivideNote;
      procedure DeleteNote;
      procedure DeleteSentence;
      procedure TransposeNote(Transpose: integer);
      procedure ChangeWholeTone(Tone: integer);
      procedure ChangeWholeToneActLine(Tone: integer);
      procedure MoveAllToEnd(Move: integer);
      procedure MoveTextToRight;
      procedure MarkSrc;
      procedure PasteText;
      procedure CopySentence(Src, Dst: integer);
      procedure CopySentences(Src, Dst, Num: integer);
      //Note Name Mod
      function GetNoteName(Note: Integer): String;
      function GetMedleyLength: real; //returns if availible the length of the medley in seconds, else 0
      procedure DrawInfoBar(P, x, y, w, h: integer);
      procedure DrawStatics;
      procedure SelectNextNote;
      procedure SelectPrevNote;
      procedure MakeSingle;
      procedure MakeDuet;
      function DuetCopyLine: boolean;
      procedure DuetMoveLine;
      procedure CopyNote(Pf, Cf, Nf, Pt, Ct, Nt: integer);
      procedure CopyLine(Pf, Cf, Pt, Ct: integer);
      procedure Refresh;
    public
      Pitches:            array of TVoicePitch;
      Tex_Background:     TTexture;
      FadeOut:            boolean;
      Path:               string;
      FileName:           string;
      SongIndex:          integer; //SongIndex from CatSongs.Song
      constructor Create; override;
      procedure onShow; override;
      function ParseInput(PressedKey: Cardinal; ScanCode: byte; PressedDown: Boolean): Boolean; override;
      function ParseInputEditText(PressedKey: Cardinal; ScanCode: byte; PressedDown: Boolean): Boolean;
      function ParseInputEditBPM(PressedKey: Cardinal; ScanCode: byte; PressedDown: Boolean): Boolean;
      function Draw: boolean; override;
      procedure onHide; override;
  end;

const
  ID='ID_001';   //for help system
  NumHalftones = 36;

implementation
uses UGraphic, UDisplay, UDraw, UMain, USkins, ULanguage;

// Method for input parsing. If False is returned, GetNextWindow
// should be checked to know the next window to load;
function TScreenEditSub.ParseInput(PressedKey: Cardinal; ScanCode: byte; PressedDown: Boolean): Boolean;
var
  SDL_ModState:  Word;
  R:          real;
  SResult:    boolean;
  temp:       integer;

begin
  Result := true;
  PlayOneSentence := false;
  Text[TextDebug].Text := '';

  if TextEditMode then
    Result := ParseInputEditText(PressedKey, ScanCode, PressedDown)
  else if BPMEditMode then
    Result := ParseInputEditBPM(PressedKey, ScanCode, PressedDown)
  else
  begin

  SDL_ModState := SDL_GetModState and (KMOD_LSHIFT + KMOD_RSHIFT
    + KMOD_LCTRL + KMOD_RCTRL + KMOD_LALT  + KMOD_RALT {+ KMOD_CAPS});

  If (PressedDown) then begin // Key Down
    PitchRecOn := false;
    case PressedKey of
      SDLK_TAB:
        begin
          ScreenPopupHelp.ShowPopup();
        end;

      SDLK_ESCAPE:
        begin
          CheckFadeTo(@ScreenSong,'Do you really want to quit?');
          if (Display.NextScreen <> nil) then
          begin
            Music.Close;
            acClose;
          end;
        end;

      SDLK_Q:
        begin
          //Result := false;
        end;

      SDLK_BACKQUOTE:
        begin
          // Increase Note Length (same as Alt + Right)
          Inc(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Dlugosc);
          if AktNuta[CP] = Czesci[CP].Czesc[Czesci[CP].Akt].HighNut then
            Inc(Czesci[CP].Czesc[Czesci[CP].Akt].Koniec);
        end;

      SDLK_EQUALS:
        begin
          // Increase BPM
          if SDL_ModState = 0 then
            AktSong.BPM[0].BPM := Round((AktSong.BPM[0].BPM * 5) + 1) / 5; // (1/20)
          if SDL_ModState = KMOD_LSHIFT then
            AktSong.BPM[0].BPM := AktSong.BPM[0].BPM + 4; // (1/1)
          if SDL_ModState = KMOD_LCTRL then
            AktSong.BPM[0].BPM := Round((AktSong.BPM[0].BPM * 25) + 1) / 25; // (1/100)
        end;

      SDLK_MINUS:
        begin
          // Decrease BPM
          if SDL_ModState = 0 then
            AktSong.BPM[0].BPM := Round((AktSong.BPM[0].BPM * 5) - 1) / 5;
          if SDL_ModState = KMOD_LSHIFT then
            AktSong.BPM[0].BPM := AktSong.BPM[0].BPM - 4;
          if SDL_ModState = KMOD_LCTRL then
            AktSong.BPM[0].BPM := Round((AktSong.BPM[0].BPM * 25) - 1) / 25;
        end;

      SDLK_0:
        begin
          // Increase GAP
          if SDL_ModState = 0 then
            AktSong.GAP := AktSong.GAP + 10;
          if SDL_ModState = KMOD_LSHIFT then
            AktSong.GAP := AktSong.GAP + 1000;
        end;

      SDLK_9:
        begin
          // Decrease GAP
          if SDL_ModState = 0 then
            AktSong.GAP := AktSong.GAP - 10;
          if SDL_ModState = KMOD_LSHIFT then
            AktSong.GAP := AktSong.GAP - 1000;
        end;

      SDLK_8:
        begin
          temp := 0;
          // Increase VideoGAP
          if SDL_ModState = 0 then
            temp := 1;         //10ms
          if SDL_ModState = KMOD_LSHIFT then
            temp := 10;        //100ms
          if SDL_ModState = KMOD_LCTRL then
            temp := 100;       //1000ms

          AktSong.VideoGap := (round(AktSong.VideoGAP*100) + temp)/100;

          if PlayVideo then
            acSkip2(AktSong.VideoGap, Czas.Teraz);
            //StartVideo;
        end;

      SDLK_7:
        begin
          temp := 0;
          // Decrease VideoGAP
          if SDL_ModState = 0 then
            temp := -1;        //10ms
          if SDL_ModState = KMOD_LSHIFT then
            temp := -10;       //100ms
          if SDL_ModState = KMOD_LCTRL then
            temp := -100;      //1000ms

          AktSong.VideoGap := (round(AktSong.VideoGAP*100) + temp)/100;

          if PlayVideo then
            acSkip2(AktSong.VideoGap, Czas.Teraz);
            //StartVideo;
        end;

      SDLK_KP_PLUS:
        begin
          // Increase tone of all notes
          if SDL_ModState = 0 then
            ChangeWholeTone(1);
          if SDL_ModState = KMOD_LSHIFT then
            ChangeWholeTone(12);
        end;

      SDLK_KP_MINUS:
        begin
          // Decrease tone of all notes
          if SDL_ModState = 0 then
            ChangeWholeTone(-1);
          if SDL_ModState = KMOD_LSHIFT then
            ChangeWholeTone(-12);
        end;

      SDLK_SLASH:
        begin
          if SDL_ModState = 0 then
          begin
            // Insert start of sentece
            if (AktNuta[CP] > 0) then
              DivideSentence;
            {else if AktSong.isDuet and
              (Length(Czesci[(CP+1) mod 2].Czesc[Czesci[CP].Akt].Nuta)>0) then
            begin
              if (Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Start >
                Czesci[(CP+1) mod 2].Czesc[Czesci[CP].Akt].Nuta[0].Start+
                Czesci[(CP+1) mod 2].Czesc[Czesci[CP].Akt].Nuta[0].Dlugosc) then
                DivideSentence;
            end;}
          end;

          if SDL_ModState = KMOD_LSHIFT then
          begin
            // Join next sentence with current
            if Czesci[CP].Akt < Czesci[CP].High  then
              JoinSentence;
          end;

          if SDL_ModState = KMOD_LCTRL then
          begin
            // divide note
            DivideNote;
          end;

        end;


      SDLK_S:
        begin
          //Medley MOD:
          if AktSong.isDuet then
          begin
            AktSong.Medley.Source := msNone;
          end
          else if (MedleyNotes.isStart and MedleyNotes.isEnd) and
            (MedleyNotes.start.line < MedleyNotes.end_.line) and
            (Length(Czesci[0].Czesc)> MedleyNotes.end_.line) and
            (Length(Czesci[0].Czesc[MedleyNotes.end_.line].Nuta)>MedleyNotes.end_.note) and
            (Length(Czesci[0].Czesc[MedleyNotes.start.line].Nuta)>MedleyNotes.start.note) then
          begin
            AktSong.Medley.Source := msTag;
            AktSong.Medley.StartBeat:=Czesci[0].Czesc[MedleyNotes.start.line].Nuta[MedleyNotes.start.note].Start;
            AktSong.Medley.EndBeat:=Czesci[0].Czesc[MedleyNotes.end_.line].Nuta[MedleyNotes.end_.note].Start +
              Czesci[0].Czesc[MedleyNotes.end_.line].Nuta[MedleyNotes.end_.note].Dlugosc;
            AktSong.Medley.FadeIn_time := DEFAULT_FADE_IN_TIME;
            AktSong.Medley.FadeOut_time := DEFAULT_FADE_OUT_TIME;
          end else
          begin
            AktSong.Medley.Source := msNone;
            AktSong.Medley.StartBeat:=0;
            AktSong.Medley.EndBeat:=0;
          end;

          // Save Song
          if SDL_ModState = KMOD_LSHIFT then
          begin
            if (AktSong.Medley.Source = msTag) then
            begin
              ScreenPopupError.ShowPopup('Medley with Relative is not supported!');
              Exit;
            end;

            if (AktSong.isDuet) then
            begin
              ScreenPopupError.ShowPopup('Duet with Relative is not supported!');
              Exit;
            end;

            SResult := SaveSong(AktSong, Czesci, Path + FileName, true); //save with relative
          end else
            SResult := SaveSong(AktSong, Czesci, Path + FileName, false);

          if SResult then
          begin
            Text[TextDebug].Text := Language.Translate('INFO_FILE_SAVED');
            CatSongs.Song[SongIndex] := AktSong;
          end else
          begin
            ScreenPopupError.ShowPopup(Language.Translate('ERROR_SAVE_FILE_FAILED'));
          end;

          Exit;
        end;

      // set Medley tags
      SDLK_A:
        begin
          if AktSong.Relative then
          begin
            ScreenPopupError.ShowPopup('Medley with Relative is not supported!');
            Exit;
          end;

          if AktSong.isDuet then
          begin
            ScreenPopupError.ShowPopup('Medley with Duet is not supported!');
            Exit;
          end;

          if SDL_ModState = KMOD_LSHIFT then //Medley End Note
          begin
            if MedleyNotes.isEnd then
            begin
              if (Czesci[0].Akt=MedleyNotes.end_.line) and (AktNuta[0]=MedleyNotes.end_.note) then
              begin
                MedleyNotes.isEnd := false;
                Czesci[0].Czesc[Czesci[0].Akt].Nuta[AktNuta[0]].IsMedley := false;
              end else
              begin
                Czesci[0].Czesc[Czesci[0].Akt].Nuta[AktNuta[0]].IsMedley := true;
                if (Length(Czesci[0].Czesc)> MedleyNotes.end_.line) and
                  (Length(Czesci[0].Czesc[MedleyNotes.end_.line].Nuta)>MedleyNotes.end_.note) then
                  Czesci[0].Czesc[MedleyNotes.end_.line].Nuta[MedleyNotes.end_.note].IsMedley := false;
                MedleyNotes.end_.line := Czesci[0].Akt;
                MedleyNotes.end_.note := AktNuta[0];
              end;
            end else
            begin
              MedleyNotes.isEnd := true;
              Czesci[0].Czesc[Czesci[0].Akt].Nuta[AktNuta[0]].IsMedley := true;
              MedleyNotes.end_.line := Czesci[0].Akt;
              MedleyNotes.end_.note := AktNuta[0];
            end;
          end else
          begin        //Medley Start Note
            if MedleyNotes.isStart then
            begin
              if (Czesci[0].Akt=MedleyNotes.start.line) and (AktNuta[0]=MedleyNotes.start.note) then
              begin
                MedleyNotes.isStart := false;
                Czesci[0].Czesc[Czesci[0].Akt].Nuta[AktNuta[0]].IsMedley := false;
              end else
              begin
                Czesci[0].Czesc[Czesci[0].Akt].Nuta[AktNuta[0]].IsMedley := true;
                if (Length(Czesci[0].Czesc)> MedleyNotes.start.line) and
                  (Length(Czesci[0].Czesc[MedleyNotes.start.line].Nuta)>MedleyNotes.start.note) then
                  Czesci[0].Czesc[MedleyNotes.start.line].Nuta[MedleyNotes.start.note].IsMedley := false;
                MedleyNotes.start.line := Czesci[0].Akt;
                MedleyNotes.start.note := AktNuta[0];
              end;
            end else
            begin
              Czesci[0].Czesc[Czesci[0].Akt].Nuta[AktNuta[0]].IsMedley := true;
              MedleyNotes.isStart := true;
              MedleyNotes.start.line := Czesci[0].Akt;
              MedleyNotes.start.note := AktNuta[0];
            end;
          end;

          //show length of medley
          Text[TextDebug].Text := FormatFloat('MedleyLength: #0.00s', GetMedleyLength);
          Exit;
        end;

      // jump to Medley tags
      SDLK_J:
        begin
          if AktSong.Relative then
          begin
            ScreenPopupError.ShowPopup('Medley with Relative is not supported!');
            Exit;
          end;

          if AktSong.isDuet then
          begin
            ScreenPopupError.ShowPopup('Medley with Duet is not supported!');
            Exit;
          end;

          if (SDL_ModState = KMOD_LSHIFT) and MedleyNotes.IsEnd then //Medley End Note
          begin
            MidiOut.PutShort($81, Czesci[0].Czesc[Czesci[0].Akt].Nuta[MidiLastNote].Ton + 60, 127);
            PlaySentenceMidi := false;
            PlayOneNoteMidi := false;
            Music.Stop;
            PlaySentence := false;
            PlayOneNote := false;

            if (Length(Czesci[0].Czesc)> MedleyNotes.end_.line) and
              (Length(Czesci[0].Czesc[MedleyNotes.end_.line].Nuta)>MedleyNotes.end_.note) then
            begin
              Czesci[0].Czesc[Czesci[0].Akt].Nuta[AktNuta[0]].Color := 0;
              Czesci[0].Akt := MedleyNotes.end_.line;
              AktNuta[0] := MedleyNotes.end_.note;
              Czesci[0].Czesc[Czesci[0].Akt].Nuta[AktNuta[0]].Color := 2;

              EditorLyric[0].AddCzesc(0, Czesci[0].Akt);
              EditorLyric[0].Selected := AktNuta[0];
            end;
          end else if MedleyNotes.IsStart then
          begin
            MidiOut.PutShort($81, Czesci[0].Czesc[Czesci[0].Akt].Nuta[MidiLastNote].Ton + 60, 127);
            PlaySentenceMidi := false;
            PlayOneNoteMidi := false;
            Music.Stop;
            PlaySentence := false;
            PlayOneNote := false;

            if (Length(Czesci[0].Czesc)> MedleyNotes.start.line) and
              (Length(Czesci[0].Czesc[MedleyNotes.start.line].Nuta)>MedleyNotes.start.note) then
            begin
              Czesci[0].Czesc[Czesci[0].Akt].Nuta[AktNuta[0]].Color := 0;
              Czesci[0].Akt := MedleyNotes.start.line;
              AktNuta[0] := MedleyNotes.start.note;
              Czesci[0].Czesc[Czesci[0].Akt].Nuta[AktNuta[0]].Color := 2;

              EditorLyric[0].AddCzesc(0, Czesci[0].Akt);
              EditorLyric[0].Selected := AktNuta[0];
            end;
          end;

          if (SDL_ModState = KMOD_LALT) then
          begin
            PlaySentenceMidi := false;
            PlayOneNoteMidi := false;
            PlayOneNote := false;
            Music.Stop;
            LineChanged[0]:=false;
            LineChanged[1]:=false;

            if (MedleyNotes.isStart and MedleyNotes.isEnd) and
              (MedleyNotes.start.line < MedleyNotes.end_.line) and
              (Length(Czesci[0].Czesc)> MedleyNotes.end_.line) and
              (Length(Czesci[0].Czesc[MedleyNotes.end_.line].Nuta)>MedleyNotes.end_.note) and
              (Length(Czesci[0].Czesc[MedleyNotes.start.line].Nuta)>MedleyNotes.start.note) then
            begin
              R := GetTimeFromBeat(Czesci[0].Czesc[MedleyNotes.start.line].Nuta[MedleyNotes.start.note].Start);
              if R <= Music.Length then
              begin
                Music.MoveTo(R);

                noteStart := AktNuta[0];
                lineStart := Czesci[0].Akt;
                cpStart := 0;

                PlayStopTime := GetTimeFromBeat(
                  Czesci[0].Czesc[MedleyNotes.end_.line].Nuta[MedleyNotes.end_.note].Start +
                  Czesci[0].Czesc[MedleyNotes.end_.line].Nuta[MedleyNotes.end_.note].Dlugosc);
                PlaySentence := true;
                Music.Play;
                LastClick := Czesci[0].Czesc[MedleyNotes.start.line].Nuta[MedleyNotes.start.note].Start-1;
              end;
            end;
          end;

          //show length of medley
          Text[TextDebug].Text := FormatFloat('MedleyLength: #0.00s', GetMedleyLength);
          Exit;
        end;

      SDLK_K: //Preview Start
        begin
          if (SDL_ModState = KMOD_LSHIFT) then    //jump to...
          begin
            MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[MidiLastNote].Ton + 60, 127);
            PlaySentenceMidi := false;
            PlayOneNoteMidi := false;

            Refresh;

            CP := MedleyNotes.Preview.CP;
            Czesci[CP].Akt := MedleyNotes.Preview.line;
            if AktSong.isDuet then
              Czesci[(CP+1) mod 2].Akt := Czesci[CP].Akt;

            AktNuta[CP] := MedleyNotes.Preview.note;
            if AktSong.isDuet then
              AktNuta[(CP+1) mod 2] := 0;

            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;

            EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
            EditorLyric[CP].Selected := AktNuta[CP];
            if AktSong.isDuet then
            begin
              EditorLyric[(CP+1) mod 2].AddCzesc((CP+1) mod 2, Czesci[(CP+1) mod 2].Akt);
              EditorLyric[(CP+1) mod 2].Selected := -1;
            end;
            Music.Stop;
            PlaySentence := false;
            PlayOneNote := false;
          end else
          begin
            if (CP = MedleyNotes.Preview.CP) and
              (Czesci[CP].Akt = MedleyNotes.Preview.line) and
              (AktNuta[CP] = MedleyNotes.Preview.note) and
              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].IsStartPreview then //reset
            begin
              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].IsStartPreview := false;
              AktSong.PreviewStart := 0;
            end else //set
            begin
              if (Length(Czesci[MedleyNotes.Preview.CP].Czesc[MedleyNotes.Preview.line].Nuta)>MedleyNotes.Preview.note) then
                Czesci[MedleyNotes.Preview.CP].Czesc[MedleyNotes.Preview.line].Nuta[MedleyNotes.Preview.note].IsStartPreview := false;
              MedleyNotes.Preview.CP := CP;
              MedleyNotes.Preview.line := Czesci[CP].Akt;
              MedleyNotes.Preview.note := AktNuta[CP];
              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].IsStartPreview := true;
              AktSong.PreviewStart := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].start);
            end;
          end;
        end;


      SDLK_D:
        begin
          // Divide lengths by 2
          if (SDL_ModState = KMOD_LSHIFT) then
          begin
            CzesciDivide;
            Text[TextDebug].Text := 'BPM and note lengths halfed';
          end;

          if (SDL_ModState = KMOD_LCTRL or KMOD_LSHIFT) then
          begin
            if AktSong.isDuet then
            begin
              MakeSingle;
              Text[TextDebug].Text := 'Converted duet into normal song';
            end else
            begin
              MakeDuet;
              Text[TextDebug].Text := 'Created duet song';
            end;
          end;
        end;

      SDLK_M:
        begin
          // Multiply lengths by 2
          if (SDL_ModState = KMOD_LSHIFT) then
          begin
            CzesciMultiply;
            Text[TextDebug].Text := 'BPM and note lengths doubled';
          end;
        end;

      SDLK_N:
        begin
          if (SDL_ModState = KMOD_LSHIFT) then
          begin
            // one line, mp3
            MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[MidiLastNote].Ton + 60, 127);
            PlaySentenceMidi := false;
            PlayOneNoteMidi := false;
            PlayOneSentence := true;
            Click := false;
            Music.Stop;
            R := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].StartNote);
            if R <= Music.Length then
            begin
              Music.MoveTo(R);
              PlayStopTime := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].Koniec)+
                Ini.LipSync*0.01 + (120 + Ini.Delay*10) / 1000;
              PlaySentence := true;
              PlayOneNote := false;
              Music.Play;
              LastClick := Czesci[CP].Czesc[Czesci[CP].Akt].StartNote-1;
            end;
          end;

          if (SDL_ModState = KMOD_LALT) then
          begin
            // Play whole file
            MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[MidiLastNote].Ton + 60, 127);
            PlaySentenceMidi := false;
            PlayOneNoteMidi := false;
            Click := false;
            Music.Stop;
            R := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].StartNote);
            if R <= Music.Length then
            begin
              Music.MoveTo(R);
              PlayStopTime := Music.Length;
              PlaySentence := true;
              PlayOneSentence := false;
              PlayOneNote := false;
              Music.Play;
              LastClick := Czesci[CP].Czesc[Czesci[CP].Akt].StartNote-1;
            end;
          end;

          if PlaySentence then
          begin
            SetLength(Pitches, 0);
            PitchRecOn := true;
            noteStart := AktNuta[CP];
            lineStart := Czesci[CP].Akt;
            cpStart := CP;
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 0;
            AktNuta[CP] := 0;
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
            EditorLyric[CP].Selected := AktNuta[CP];
            LineChanged[0]:=false;
            LineChanged[1]:=false;
            PlayVideo := false;
          end;

          if (SDL_ModState = 0) then
          begin
            // Set actual note over pitch detection
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Ton := ActTonePitch;
          end;
        end;

      SDLK_C:
        begin
          // Capitalize letter at the beginning of line
          if SDL_ModState = 0 then
          begin
            LyricsCapitalize;
            Text[TextDebug].Text := Language.Translate('EDITOR_CAPITALIZE_LETTER');
            EditorLyric[CP].Selected := AktNuta[CP];
          end;

          // Correct spaces
          if SDL_ModState = KMOD_LSHIFT then
          begin
            LyricsCorrectSpaces;
            Text[TextDebug].Text := 'Corrected lyric spaces';
          end;

          // Copy sentence
          if SDL_ModState = KMOD_LCTRL then
          begin
            MarkSrc;
            Text[TextDebug].Text := 'Line marked'
          end;
        end;

      SDLK_R:   //reload
        begin
          MidiOut.Close;
          MidiOut.Free;
          Music.Close;
          acClose;

          onShow;
          Text[TextDebug].Text := 'Song reloaded!';
        end;

      SDLK_V:
        begin
          // Paste text
          if SDL_ModState = KMOD_LCTRL then
          begin
            if (Length(Czesci[CopySrcCP].Czesc)>CopySrcLine) then
            begin
              if Czesci[CP].Czesc[Czesci[CP].Akt].IlNut >= Czesci[CopySrcCP].Czesc[CopySrcLine].IlNut then
              begin
                PasteText;
                Text[TextDebug].Text := 'Text pasted';
              end else
                beep;
            end else
              beep;
          end;

          if (SDL_ModState = KMOD_LCTRL + KMOD_LSHIFT) then
          begin
            CopySentence(CopySrcLine, Czesci[CP].Akt);
            Text[TextDebug].Text := 'Line pasted';
          end;

          if SDL_ModState = 0 then
            StartVideo;

          if SDL_ModState = KMOD_LSHIFT then
          begin
            StartVideo;
            Click := true;
          end;
        end;

      SDLK_4:
        begin
          if AktSong.isDuet then //TODO !!!
            Exit;

          if SDL_ModState = KMOD_LCTRL + KMOD_LSHIFT then
          begin
            CopySentence(CopySrcLine, Czesci[0].Akt);
            CopySentence(CopySrcLine+1, Czesci[0].Akt+1);
            CopySentence(CopySrcLine+2, Czesci[0].Akt+2);
            CopySentence(CopySrcLine+3, Czesci[0].Akt+3);
          end;

          if SDL_ModState = KMOD_LCTRL + KMOD_LSHIFT + KMOD_LALT then
          begin
            CopySentences(CopySrcLine, Czesci[0].Akt, 4);
          end;
        end;
      SDLK_5:
        begin
          if AktSong.isDuet then //TODO !!!
            Exit;

          if SDL_ModState = KMOD_LCTRL + KMOD_LSHIFT then
          begin
            CopySentence(CopySrcLine, Czesci[0].Akt);
            CopySentence(CopySrcLine+1, Czesci[0].Akt+1);
            CopySentence(CopySrcLine+2, Czesci[0].Akt+2);
            CopySentence(CopySrcLine+3, Czesci[0].Akt+3);
            CopySentence(CopySrcLine+4, Czesci[0].Akt+4);
          end;

          if SDL_ModState = KMOD_LCTRL + KMOD_LSHIFT + KMOD_LALT then
          begin
            CopySentences(CopySrcLine, Czesci[0].Akt, 5);
          end;
        end;

      SDLK_T:
        begin
          MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[MidiLastNote].Ton + 60, 127);
          PlaySentenceMidi := false;
          PlayOneNoteMidi := false;
          Music.Stop;
          LineChanged[0]:=false;
          LineChanged[1]:=false;
          PlaySentence := false;
          PlayOneNote := false;
          // Fixes timings between sentences
          FixTimings;
          Text[TextDebug].Text := Language.Translate('EDITOR_FIX_TIMINGS');
          EditorLyric[CP].Selected := AktNuta[CP];
        end;

      SDLK_F4:
        begin
          MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[MidiLastNote].Ton + 60, 127);
          PlaySentenceMidi := false;
          PlayOneNoteMidi := false;
          Music.Stop;
          LineChanged[0]:=false;
          LineChanged[1]:=false;
          PlaySentence := false;
          PlayOneNote := false;
          // Enter Text Edit Mode
          editText := Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Tekst;
          TextEditMode := true;
        end;

      SDLK_F5:
        begin
          // Enter BPM Edit Mode
          MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[MidiLastNote].Ton + 60, 127);
          PlaySentenceMidi := false;
          PlayOneNoteMidi := false;
          Music.Stop;
          LineChanged[0]:=false;
          LineChanged[1]:=false;
          PlaySentence := false;
          PlayOneNote := false;
          Text[TextBPM].Text := Text[TextBPM].Text + '|';
          BPMEditMode := true;
        end;

      SDLK_P:
        begin
          // one line, mp3 + clicks
          if SDL_ModState = 0 then
          begin
            MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[MidiLastNote].Ton + 60, 127);
            PlaySentenceMidi := false;
            PlayOneNoteMidi := false;
            PlayOneSentence := true;
            Click := true;
            Music.Stop;
            R := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].StartNote);
            if R <= Music.Length then
            begin
              Music.MoveTo(R);
              PlayStopTime := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].Koniec);
              PlaySentence := true;
              PlayOneNote := false;
              Music.Play;
              LastClick := Czesci[CP].Czesc[Czesci[CP].Akt].StartNote-1;
            end;
          end;

          // one line, midi
          if SDL_ModState = KMOD_LSHIFT then
          begin
            PlaySentenceMidi := true;
            PlayOneNoteMidi := false;
            PlayOneSentence := true;
            MidiTime := USTime.GetTime;
            Music.Stop;
            PlaySentence := false;
            PlayOneNote := false;
            MidiStart := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].StartNote);
            MidiStop := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].Koniec);

            LastClick := Czesci[CP].Czesc[Czesci[CP].Akt].StartNote-1;
          end;

          // one line midi + mp3
          if SDL_ModState = KMOD_LSHIFT or KMOD_LCTRL then
          begin
            PlaySentenceMidi := true;
            PlayOneNoteMidi := false;
            PlayOneSentence := true;
            MidiTime := USTime.GetTime;
            MidiStart := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].StartNote);
            MidiStop := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].Koniec);

            PlaySentence := true;
            PlayOneNote := false;
            Music.Stop;
            Music.MoveTo(GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].StartNote));
            PlayStopTime := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].Koniec);
            Music.Play;
            LastClick := Czesci[CP].Czesc[Czesci[CP].Akt].StartNote-1;
          end;

          //new: play hole file + LALT
          if SDL_ModState = KMOD_LALT then
          begin
            // Play Sentence
            MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[MidiLastNote].Ton + 60, 127);
            PlaySentenceMidi := false;
            PlayOneNoteMidi := false;
            Click := true;
            Music.Stop;
            R := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].StartNote);
            if R <= Music.Length then
            begin
              Music.MoveTo(R);
              PlayStopTime := Music.Length;
              PlaySentence := true;
              PlayOneNote := false;
              Music.Play;
              LastClick := Czesci[CP].Czesc[Czesci[CP].Akt].StartNote-1;
            end;
          end;

          if SDL_ModState = KMOD_LSHIFT or KMOD_LALT then
          begin
            PlaySentenceMidi := true;
            PlayOneNoteMidi := false;
            Music.Stop;
            PlaySentence := false;
            PlayOneNote := false;
            MidiTime := USTime.GetTime;
            MidiStart := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].StartNote);
            MidiStop := Music.Length;

            LastClick := Czesci[CP].Czesc[Czesci[CP].Akt].StartNote-1;
          end;

          if SDL_ModState = KMOD_LSHIFT or KMOD_LCTRL or KMOD_LALT then
          begin
            PlaySentenceMidi := true;
            PlayOneNoteMidi := false;
            MidiTime := USTime.GetTime;
            MidiStart := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].StartNote);
            MidiStop := Music.Length;

            PlaySentence := true;
            PlayOneNote := false;
            Music.Stop;
            Music.MoveTo(GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].StartNote)+0{-0.10});
            PlayStopTime := Music.Length;
            Music.Play;
            LastClick := Czesci[CP].Czesc[Czesci[CP].Akt].StartNote-1;
          end;

          if PlaySentenceMidi or PlaySentence then
          begin
            noteStart := AktNuta[CP];
            lineStart := Czesci[CP].Akt;
            cpStart := CP;
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 0;
            AktNuta[CP] := 0;
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
            EditorLyric[CP].Selected := AktNuta[CP];
            LineChanged[0]:=false;
            LineChanged[1]:=false;
            PlayVideo := false;
          end;

        end;

      SDLK_SPACE:
        begin
          //Thx to f1fth_freed0m for his One Note Midi Playback
          if SDL_ModState = KMOD_LSHIFT then
          begin //Play One Notes Midi [Shift + Space]
            PlaySentenceMidi := false;
            PlayOneNoteMidi := true;
            Music.Stop;
            PlaySentence := false;
            PlayOneNote := false;
            MidiTime := USTime.GetTime;
            MidiStart := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Start);
            MidiStop := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Dlugosc);
            LastClick := Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Start-1;
          end

          else if SDL_ModState = KMOD_LSHIFT or KMOD_LCTRL then
          begin
            //Play One Notes Midi + MP3 [CTRL + Shift + Space]
            PlaySentenceMidi := false;
            PlayOneNoteMidi := true;
            MidiTime := USTime.GetTime;
            MidiStart := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Start);
            MidiStop := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Dlugosc);

            PlaySentence := false;
            PlayOneNote := true;
            Click := true;
            Music.Stop;
            Music.MoveTo(GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Start));
            PlayStopTime := (GetTimeFromBeat(
                             Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Start +
                             Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Dlugosc));
            Music.Play;
            LastClick := Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Start-1;
          end

          Else
          begin
            // Play One Notes MP3 [Space]
            MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[MidiLastNote].Ton + 60, 127);
            PlaySentenceMidi := false; // stop midi
            PlayOneNoteMidi := false;
            PlaySentence := false;
            PlayOneNote := true;
            Click := false;
            Music.Stop;

            Music.MoveTo(GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Start));
            PlayStopTime := (GetTimeFromBeat(
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Start +
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Dlugosc));
            Music.Play;
            LastClick := Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Start-1;
          end;
        end;
      SDLK_RETURN:
        begin
        end;

      SDLK_LCTRL:
        begin
        end;

      SDLK_DELETE:
        begin
          if SDL_ModState = KMOD_LCTRL then
          begin
            // moves text to right in current sentence
            DeleteNote;
            Text[TextDebug].Text := 'Note deleted';
          end;

          if SDL_ModState = KMOD_LSHIFT then
          begin
            DeleteSentence;
            Text[TextDebug].Text := 'Line deleted';
          end;
        end;

      SDLK_PERIOD:
        begin
          // moves text to right in current sentence
          MoveTextToRight;
        end;

      SDLK_RIGHT:
        begin
          if PlaySentenceMidi or PlaySentence then
          begin
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 0;

            if (cpStart = CP) and (lineStart = Czesci[CP].Akt) then
              AktNuta[CP] := noteStart;

            Dec(AktNuta[CP]);
            if AktNuta[CP] = -1 then AktNuta[CP] := Czesci[CP].Czesc[Czesci[CP].Akt].HighNut;
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
            EditorLyric[CP].Selected := AktNuta[CP];
          end;
          MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[MidiLastNote].Ton + 60, 127);
          PlaySentenceMidi := false;
          PlayOneNoteMidi := false;
          Music.Stop;
          LineChanged[0]:=false;
          LineChanged[1]:=false;
          PlaySentence := false;
          PlayOneNote := false;

          // right
          if SDL_ModState = 0 then
          begin
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 0;
            Inc(AktNuta[CP]);

            if AktNuta[CP] = Czesci[CP].Czesc[Czesci[CP].Akt].IlNut then AktNuta[CP] := 0;
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
            EditorLyric[CP].Selected := AktNuta[CP];
          end;

          // ctrl + right
          if SDL_ModState = KMOD_LCTRL then
          begin
            if Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Dlugosc > 1 then
            begin
              Dec(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Dlugosc);
              Inc(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Start);
              if AktNuta[CP] = 0 then
              begin
                Inc(Czesci[CP].Czesc[Czesci[CP].Akt].Start);
                Inc(Czesci[CP].Czesc[Czesci[CP].Akt].StartNote);
              end;
              FixTimings;
            end;
          end;

          // shift + right
          if SDL_ModState = KMOD_LSHIFT then
          begin
            Inc(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Start);
            if AktNuta[CP] = 0 then
            begin
              Inc(Czesci[CP].Czesc[Czesci[CP].Akt].Start);
              Inc(Czesci[CP].Czesc[Czesci[CP].Akt].StartNote);
            end;
            if AktNuta[CP] = Czesci[CP].Czesc[Czesci[CP].Akt].HighNut then
              Inc(Czesci[CP].Czesc[Czesci[CP].Akt].Koniec);
            FixTimings;
          end;

          // alt + right
          if SDL_ModState = KMOD_LALT then
          begin
            Inc(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Dlugosc);
            if AktNuta[CP] = Czesci[CP].Czesc[Czesci[CP].Akt].HighNut then
              Inc(Czesci[CP].Czesc[Czesci[CP].Akt].Koniec);
            FixTimings;
          end;

          // alt + ctrl + shift + right = move all from cursor to right
          if SDL_ModState = KMOD_LALT + KMOD_LCTRL + KMOD_LSHIFT then
          begin
            MoveAllToEnd(1);
            FixTimings;
          end;
        end;

      SDLK_LEFT:
        begin
          if PlaySentenceMidi or PlaySentence then
          begin
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 0;
            if (cpStart = CP) and (lineStart = Czesci[CP].Akt) then
              AktNuta[CP] := noteStart;

            Inc(AktNuta[CP]);
            if AktNuta[CP] = Czesci[CP].Czesc[Czesci[CP].Akt].IlNut then AktNuta[CP] := 0;
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
            EditorLyric[CP].Selected := AktNuta[CP];
          end;
          MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[MidiLastNote].Ton + 60, 127);
          PlaySentenceMidi := false;
          PlayOneNoteMidi := false;
          Music.Stop;
          LineChanged[0]:=false;
          LineChanged[1]:=false;
          PlaySentence := false;
          PlayOneNote := false;

          // left
          if SDL_ModState = 0 then
          begin
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 0;
            Dec(AktNuta[CP]);
            if AktNuta[CP] = -1 then
              AktNuta[CP] := Czesci[CP].Czesc[Czesci[CP].Akt].HighNut;
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
            EditorLyric[CP].Selected := AktNuta[CP];
          end;

          // ctrl + left
          if SDL_ModState = KMOD_LCTRL then
          begin
            Dec(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Start);
            Inc(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Dlugosc);
            if AktNuta[CP] = 0 then begin
              Dec(Czesci[CP].Czesc[Czesci[CP].Akt].Start);
              Dec(Czesci[CP].Czesc[Czesci[CP].Akt].StartNote);
            end;
            FixTimings;
          end;

          // shift + left
          if SDL_ModState = KMOD_LSHIFT then
          begin
            Dec(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Start);

            // resizing sentences
            if AktNuta[CP] = 0 then
            begin
              Dec(Czesci[CP].Czesc[Czesci[CP].Akt].Start);
              Dec(Czesci[CP].Czesc[Czesci[CP].Akt].StartNote);
            end;

            if AktNuta[CP] = Czesci[CP].Czesc[Czesci[CP].Akt].HighNut then
              Dec(Czesci[CP].Czesc[Czesci[CP].Akt].Koniec);
            FixTimings;
          end;

          // alt + left
          if SDL_ModState = KMOD_LALT then
          begin
            if Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Dlugosc > 1 then
            begin
              Dec(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Dlugosc);
              if AktNuta[CP] = Czesci[CP].Czesc[Czesci[CP].Akt].HighNut then
                Dec(Czesci[CP].Czesc[Czesci[CP].Akt].Koniec);
            end;
            FixTimings;
          end;

          // alt + ctrl + shift + right = move all from cursor to left
          if SDL_ModState = KMOD_LALT + KMOD_LCTRL + KMOD_LSHIFT then
          begin
            MoveAllToEnd(-1);
            FixTimings;
          end;
        end;

      SDLK_DOWN:
        begin
          MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[MidiLastNote].Ton + 60, 127);
          PlaySentenceMidi := false;
          PlayOneNoteMidi := false;
          Music.Stop;
          LineChanged[0]:=false;
          LineChanged[1]:=false;
          PlaySentence := false;
          PlayOneNote := false;

          // skip to next sentence
          if SDL_ModState = 0 then
          begin
            Refresh;
            Inc(Czesci[CP].Akt);
            AktNuta[CP] := 0;
            //AktNuta[1] := 0;
            if Czesci[CP].Akt > Czesci[CP].High then
              Czesci[CP].Akt := 0;

            SelectNextNote;
          end;

          // decrease tone
          if SDL_ModState = KMOD_LCTRL then
          begin
            TransposeNote(-1);
          end;

          // select singer 2 notes if possible
          if (SDL_ModState = KMOD_LSHIFT) and AktSong.isDuet then
          begin
            if (Length(Czesci[1].Czesc[Czesci[1].Akt].Nuta)>0) then
            begin
              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 0;
              CP := 1;
              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
            end;
          end;

          if AktSong.isDuet and (SDL_ModState = KMOD_LSHIFT or KMOD_LCTRL) and (CP=0) then
            DuetCopyLine;

          if AktSong.isDuet and (SDL_ModState = KMOD_LSHIFT or KMOD_LCTRL or KMOD_LALT) and (CP=0) then
            DuetMoveLine;
        end;

      SDLK_UP:
        begin
          MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[MidiLastNote].Ton + 60, 127);
          PlaySentenceMidi := false;
          PlayOneNoteMidi := false;
          Music.Stop;
          LineChanged[0]:=false;
          LineChanged[1]:=false;
          PlaySentence := false;
          PlayOneNote := false;

          // skip to previous sentence
          if SDL_ModState = 0 then
          begin
            Refresh;
            Dec(Czesci[CP].Akt);
            AktNuta[CP] := 0;
            if Czesci[CP].Akt = -1 then
              Czesci[CP].Akt := Czesci[CP].High;

            SelectPrevNote;
          end;

          // increase tone
          if SDL_ModState = KMOD_LCTRL then
          begin
            TransposeNote(1);
          end;

          // select singer 1 notes if possible
          if (SDL_ModState = KMOD_LSHIFT) and AktSong.isDuet then
          begin
            if (Length(Czesci[0].Czesc[Czesci[0].Akt].Nuta)>0) then
            begin
              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 0;
              CP := 0;
              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
            end;
          end;

          if AktSong.isDuet and (SDL_ModState = KMOD_LSHIFT or KMOD_LCTRL) and (CP=1) then
            DuetCopyLine;

          if AktSong.isDuet and (SDL_ModState = KMOD_LSHIFT or KMOD_LCTRL or KMOD_LALT) and (CP=1) then
            DuetMoveLine;
        end;

      // Golden Note Patch
      SDLK_G:
        begin
          case Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Wartosc of
            0, 1: Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Wartosc := 2;
            2:    Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Wartosc := 1;
          end; // case
          Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Freestyle := False;
        end;

      // Freestyle Note Patch
      SDLK_F:
        begin
           case Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Wartosc of
            0:
            begin;
              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Wartosc := 1;
              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Freestyle := False;
            end;
            1,2:
            begin;
              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Wartosc := 0;
              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Freestyle := True;
            end;
          end; // case
          EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
          EditorLyric[CP].Selected := AktNuta[CP];
        end;

      //MP3-Volume Up
      SDLK_PAGEUP:
        begin
          if (SDL_ModState = 0) then
          begin
            if (MP3Volume<100) then
              MP3Volume := MP3Volume+5;
            Music.SetMusicVolume(MP3Volume);
            Text[TextDebug].Text := 'MP3 Volume: ' + IntToStr(MP3Volume) + '%';
          end;

          // Increase tone of all notes
          if (SDL_ModState = KMOD_LCTRL or KMOD_LALT) then
            ChangeWholeTone(1);
          if (SDL_ModState = KMOD_LCTRL or KMOD_LSHIFT or KMOD_LALT) then
            ChangeWholeTone(12);

          // Increase tone of all notes of actual line
          if (SDL_ModState = KMOD_LCTRL) then
            ChangeWholeToneActLine(1);
          if (SDL_ModState = KMOD_LCTRL or KMOD_LSHIFT) then
            ChangeWholeToneActLine(12);
        end;

      //MP3-Volume Down
      SDLK_PAGEDOWN:
        begin
          if (SDL_ModState = 0) then
          begin
            if (MP3Volume>0) then
              MP3Volume := MP3Volume-5;
            Music.SetMusicVolume(MP3Volume);
            Text[TextDebug].Text := 'MP3 Volume: ' + IntToStr(MP3Volume) + '%';
          end;

          // Decrease tone of all notes
          if (SDL_ModState = KMOD_LCTRL or KMOD_LALT) then
            ChangeWholeTone(-1);
          if (SDL_ModState = KMOD_LCTRL or KMOD_LSHIFT or KMOD_LALT) then
            ChangeWholeTone(-12);

          // Decrease tone of all notes of actual line
          if (SDL_ModState = KMOD_LCTRL) then
            ChangeWholeToneActLine(-1);
          if (SDL_ModState = KMOD_LCTRL or KMOD_LSHIFT) then
            ChangeWholeToneActLine(-12);
        end;
      end;
    end;
  end; // if
end;

function TScreenEditSub.ParseInputEditText(PressedKey: Cardinal; ScanCode: byte; PressedDown: Boolean): Boolean;
var
  SDL_ModState:  Word;
begin
  // used when in Text Edit Mode
  Result := true;

  SDL_ModState := SDL_GetModState and (KMOD_LSHIFT + KMOD_RSHIFT
    + KMOD_LCTRL + KMOD_RCTRL + KMOD_LALT  + KMOD_RALT {+ KMOD_CAPS});

  if Ini.Debug=1 then
    Text[TextDebug].Text := 'PressedKey: ' + IntToStr(PressedKey) + ' ScanCode: ' + IntToStr(ScanCode);
    
  // check normal keys
    if not (ScanCode in [0..31, 127..159]) then //=isPrintable
    begin
      Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Tekst :=
      Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Tekst + chr(ScanCode);

      //EditorLyric[CP].ChangeCurText(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Tekst);
      EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
      EditorLyric[CP].Selected := AktNuta[CP];
      Exit;
    end;

  If (PressedDown) Then
  begin // Key Down
    case PressedKey of
      SDLK_ESCAPE:
        begin
          Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Tekst := editText;
          EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
          TextEditMode := false;
        end;
      SDLK_F4, SDLK_RETURN:
        begin
          // Exit Text Edit Mode
          TextEditMode := false;
        end;

      SDLK_BACKSPACE:
        begin
          Delete(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Tekst,
            Length(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Tekst), 1);

          //EditorLyric[CP].ChangeCurText(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Tekst);
          EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
          EditorLyric[CP].Selected := AktNuta[CP];
        end;
      SDLK_RIGHT:
        begin
          // right
          if SDL_ModState = 0 then
          begin
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 0;
            Inc(AktNuta[CP]);
            if AktNuta[CP] = Czesci[CP].Czesc[Czesci[CP].Akt].IlNut then
              AktNuta[CP] := 0;
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;

            EditorLyric[CP].Selected := AktNuta[CP];
            editText := Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Tekst;
          end;
        end;
      SDLK_LEFT:
        begin
          // left
          if SDL_ModState = 0 then
          begin
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 0;
            Dec(AktNuta[CP]);
            if AktNuta[CP] = -1 then
              AktNuta[CP] := Czesci[CP].Czesc[Czesci[CP].Akt].HighNut;
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;

            EditorLyric[CP].Selected := AktNuta[CP];
            editText := Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Tekst;
          end;
        end;
      SDLK_UP:
        begin
          if SDL_ModState = 0 then
          begin
            Refresh;
            Dec(Czesci[CP].Akt);
            AktNuta[CP] := 0;
            if Czesci[CP].Akt = -1 then
              Czesci[CP].Akt := Czesci[CP].High;

            SelectPrevNote;

            EditorLyric[CP].Selected := AktNuta[CP];
            editText := Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Tekst;
          end;

          // select singer 1 notes if possible
          if (SDL_ModState = KMOD_LSHIFT) and AktSong.isDuet then
          begin
            if (Length(Czesci[0].Czesc[Czesci[0].Akt].Nuta)>0) then
            begin
              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 0;
              CP := 0;
              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;

              EditorLyric[CP].Selected := AktNuta[CP];
              editText := Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Tekst;
            end;
          end;
        end;
      SDLK_DOWN:
        begin
          if SDL_ModState = 0 then
          begin
            Refresh;
            Inc(Czesci[CP].Akt);
            AktNuta[CP] := 0;
            //AktNuta[1] := 0;
            if Czesci[CP].Akt > Czesci[CP].High then
              Czesci[CP].Akt := 0;

            SelectNextNote;

            EditorLyric[CP].Selected := AktNuta[CP];
            editText := Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Tekst;
          end;

          // select singer 2 notes if possible
          if (SDL_ModState = KMOD_LSHIFT) and AktSong.isDuet then
          begin
            if (Length(Czesci[1].Czesc[Czesci[1].Akt].Nuta)>0) then
            begin
              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 0;
              CP := 1;
              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;

              EditorLyric[CP].Selected := AktNuta[CP];
              editText := Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Tekst;
            end;
          end;
        end;
    end;
  end;
end;

function TScreenEditSub.ParseInputEditBPM(PressedKey: Cardinal; ScanCode: byte; PressedDown: Boolean): Boolean;
var
  //SDL_ModState: Word;
  strBPM:       string;
  temp:         real;

begin
  // used when in Text Edit Mode
  Result := true;

  //SDL_ModState := SDL_GetModState and (KMOD_LSHIFT + KMOD_RSHIFT
  //  + KMOD_LCTRL + KMOD_RCTRL + KMOD_LALT  + KMOD_RALT {+ KMOD_CAPS});

  // check normal keys
  if (ScanCode in [48..57, 44]) then
  begin
    strBPM := Text[TextBPM].Text;
    Delete(strBPM, Length(strBPM), 1);
    Text[TextBPM].Text := strBPM + chr(ScanCode) + '|';
    Exit;
  end;

  If (PressedDown) Then
  begin // Key Down
    case PressedKey of
      SDLK_ESCAPE:
        begin
          Text[TextBPM].Text := FloatToStr(AktSong.BPM[0].BPM / 4);
          BPMEditMode := false;
        end;
      SDLK_F5, SDLK_RETURN:
        begin
          strBPM := Text[TextBPM].Text;
          Delete(strBPM, Length(strBPM), 1);
          Temp := StrToFloatDef(strBPM, 0);
          if (temp>0) then
            ChangeBPM(temp*4);

          BPMEditMode := false;
        end;
      
      SDLK_BACKSPACE:
        begin
          strBPM := Text[TextBPM].Text;
          if Length(strBPM)>1 then
          begin
            Delete(strBPM, Length(strBPM)-1, 2);
            Text[TextBPM].Text := strBPM + '|';
          end;
        end;
    end;
  end;
end;

procedure TScreenEditSub.DrawPitch(x, y, Width, Height: single; beat: integer);
var
  x1, y1, x2, y2: single;
  i: integer;
  ToneBoxWidth: real;
  ToneString: string;
  ToneStringWidth, ToneStringHeight: real;
  ToneStringMaxWidth: real;
  ToneStringCenterXOffset: real;

const
  PitchBarInnerHSpacing = 2;
  PitchBarInnerVSpacing = 1;

begin
  // calc tone pitch
  Sound[0].AnalizujBufor;

  // coordinates for black rect
  x1 := x;
  y1 := y;
  x2 := x + Width;
  y2 := y + Height;

  // init blend mode
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glEnable(GL_BLEND);

  // draw black background-rect
  glColor4f(0, 0, 0, 0.8);
  glBegin(GL_QUADS);
    glVertex2f(x1, y1);
    glVertex2f(x2, y1);
    glVertex2f(x2, y2);
    glVertex2f(x1, y2);
  glEnd();

  // coordinates for tone boxes
  ToneBoxWidth := Width / NumHalftones;
  y1 := y1 + PitchBarInnerVSpacing;
  y2 := y2 - PitchBarInnerVSpacing;

  glBegin(GL_QUADS);
    // draw tone boxes
    for i := 0 to NumHalftones-1 do
    begin
      x1 := x + i * ToneBoxWidth + PitchBarInnerHSpacing;
      x2 := x1 + ToneBoxWidth - 2*PitchBarInnerHSpacing;

      if ((Sound[0].SzczytJest) and
          (Sound[0].Ton = i)) then
      begin
        // highlight current tone-pitch
        glColor3f(1, i / (NumHalftones-1), 0);
        ActTonePitch := i;
      end
      else
      begin
        // grey other tone-pitches
        glColor3f(0.3, i / (NumHalftones-1) * 0.3, 0);
      end;

      glVertex2f(x1, y1);
      glVertex2f(x2, y1);
      glVertex2f(x2, y2);
      glVertex2f(x1, y2);
    end;
  glEnd();

  glDisable(GL_BLEND);

  ///
  // draw the name of the tone
  ///////

  ToneString := Sound[0].GetToneString + '(' + IntToStr(ActTonePitch) + ')';
  ToneStringHeight := 8;

  SetFontSize(ToneStringHeight);

  // center
  // Note: for centering let us assume that G#4 has the max. horizontal extent
  ToneStringWidth := glTextWidth(PChar(ToneString));
  ToneStringMaxWidth := glTextWidth('G#4 (222)');
  ToneStringCenterXOffset := (ToneStringMaxWidth-ToneStringWidth) / 2;

  // draw
  SetFontPos(x-ToneStringWidth-ToneStringCenterXOffset, y-ToneStringHeight/2);
  glColor3f(0, 0, 0);
  glPrint(PChar(ToneString));

  // rec pitches
  if not PitchRecOn then
    Exit;

  i := High(Pitches);
  beat := Floor(GetMidBeat(GetTimeFromBeat(beat) - Ini.LipSync*0.01 - (AktSong.Gap + 120 + Ini.Delay*10) / 1000));

  if (i = -1) or (Pitches[i].beat<beat) then
  begin
    SetLength(Pitches, i+2);
    Pitches[i+1].beat := beat;
    Pitches[i+1].pitch := ActTonePitch;
  end;
  
end;


procedure TScreenEditSub.StartVideo;
var
  R:  real;

begin
  // Play Sentences with Video
  MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[MidiLastNote].Ton + 60, 127);
  PlaySentenceMidi := false;
  PlayOneNoteMidi := false;
  Click := false;
  Music.Stop;

  if PlayVideo then
  begin
    CP := cpStart;
    Czesci[CP].Akt := lineStart;
    AktNuta[CP] := noteStart;
  end;

  R := GetTimeFromBeat(Czesci[CP].Czesc[Czesci[CP].Akt].StartNote);
  if R <= Music.Length then
  begin
    Music.MoveTo(R);
    PlayStopTime := Music.Length;
    PlaySentence := true;
    PlayOneNote := false;
    Music.Play;
    LastClick := Czesci[CP].Czesc[Czesci[CP].Akt].StartNote-1;
  end;

  noteStart := AktNuta[CP];
  lineStart := Czesci[CP].Akt;
  cpStart := CP;
  Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 0;
  AktNuta[CP] := 0;
  Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
  EditorLyric[CP].Selected := AktNuta[CP];
  LineChanged[0]:=false;
  LineChanged[1]:=false;
  PlayTime := 0;
  PlayVideo := true;
  StartVideoPreview;
end;

procedure TScreenEditSub.StartVideoPreview;
begin
  if (PlayTime<0.2) and PlayVideo then
  begin
    acClose;
    VidVis := none;
    StartTry := true;
  end else if StartTry then
  begin
  if (AktSong.Video <> '') and
    FileExists(AktSong.Path + AktSong.Video) then
    begin
      acOpenFile(PAnsiChar(AktSong.Path + AktSong.Video));

      acSkip2(AktSong.VideoGAP, Music.Position);
      Czas.Teraz := Music.Position;
      Czas.Razem := Music.Length;
      StartTry := false;
      try
        acGetFrame(Czas.Teraz);
        VidVis := windowed;

      except
        //If an Error occurs Reading Video: prevent Video from being Drawn again and Close Video
        Log.LogError('Error drawing Video, Video has been disabled for this Song/Session.');
        Log.LogError('Corrupted File: ' + AktSong.Video);
        AktSong.Video := ''; //dirt fix
        try
          acClose;
          VidVis := none;
        except
        end;
      end;
    end else
      VidVis := none;
  end;
end;

procedure TScreenEditSub.NewBeat;
begin
    // click
{    for Pet := 0 to Czesci[0].Czesc[Czesci[0].Akt].HighNut do
  if (Czesci[0].Czesc[Czesci[0].Akt].Nuta[Pet].Start = Czas.AktBeat) then begin
      // old}
//    Music.PlayClick;
end;

procedure TScreenEditSub.ChangeBPM(newBPM: real);
var
  P:    integer;
  C:    integer;
  N:    integer;
  f:    real;

begin                    
  f := newBPM/AktSong.BPM[0].BPM;    //z.B. neu/alt => 1/2 = 0.5 => *0.5
  AktSong.BPM[0].BPM := newBPM;

  for P := 0 to Length(Czesci) - 1 do
  begin
    for C := 0 to Czesci[P].High do
    begin
      Czesci[P].Czesc[C].Start :=    ceil(Czesci[P].Czesc[C].Start *f);
      Czesci[P].Czesc[C].StartNote := ceil(Czesci[P].Czesc[C].StartNote *f);
      if (Length(Czesci[P].Czesc[C].Nuta)>0) then
      begin
        for N := 0 to Czesci[P].Czesc[C].HighNut do
        begin
          Czesci[P].Czesc[C].Nuta[N].Start :=   ceil(Czesci[P].Czesc[C].Nuta[N].Start *f); //round up
          Czesci[P].Czesc[C].Nuta[N].Dlugosc := floor(Czesci[P].Czesc[C].Nuta[N].Dlugosc *f); //round down
          if (Czesci[P].Czesc[C].Nuta[N].Dlugosc=0) then
            Czesci[P].Czesc[C].Nuta[N].Dlugosc := 1;
        end; // N (notes)
        Czesci[P].Czesc[C].Koniec :=    Czesci[P].Czesc[C].Nuta[Czesci[P].Czesc[C].HighNut].Start +
          Czesci[P].Czesc[C].Nuta[Czesci[P].Czesc[C].HighNut].Dlugosc;
      end else
        Czesci[P].Czesc[C].Koniec := round(Czesci[P].Czesc[C].Koniec * f);
    end; // C (lines)
  end;
end;


procedure TScreenEditSub.CzesciDivide;
begin
  ChangeBPM(AktSong.BPM[0].BPM / 2);
end;

procedure TScreenEditSub.CzesciMultiply;
begin
  ChangeBPM(AktSong.BPM[0].BPM * 2);
end;

procedure TScreenEditSub.LyricsCapitalize;
var
  P:    integer;
  C:    integer;
  S:    string;
begin
  for P := 0 to Length(Czesci) - 1 do
  begin
    for C := 0 to Czesci[P].High do
    begin
      if (Length(Czesci[P].Czesc[C].Nuta)>0) then
      begin
        S := AnsiUpperCase(Copy(Czesci[P].Czesc[C].Nuta[0].Tekst, 1, 1));
        S := S + Copy(Czesci[P].Czesc[C].Nuta[0].Tekst, 2, Length(Czesci[P].Czesc[C].Nuta[0].Tekst)-1);
        Czesci[P].Czesc[C].Nuta[0].Tekst := S;
      end;
    end; // C
    EditorLyric[P].AddCzesc(P, Czesci[P].Akt);
  end;
end;

procedure TScreenEditSub.LyricsCorrectSpaces;
var
  P:    integer;
  C:    integer;
  N:    integer;
begin
  for P := 0 to Length(Czesci) - 1 do
  begin
    for C := 0 to Czesci[P].High do
    begin
      if(Length(Czesci[P].Czesc[C].Nuta)>0) then
      begin
        // correct starting spaces in the first word
        while Copy(Czesci[P].Czesc[C].Nuta[0].Tekst, 1, 1) = ' ' do
          Czesci[P].Czesc[C].Nuta[0].Tekst := Copy(Czesci[P].Czesc[C].Nuta[0].Tekst, 2, 100);

        // move spaces on the start to the end of the previous note
        {
        for N := 1 to Czesci[P].Czesc[C].HighNut do
        begin
          while (Copy(Czesci[P].Czesc[C].Nuta[N].Tekst, 1, 1) = ' ') do
          begin
            Czesci[P].Czesc[C].Nuta[N].Tekst := Copy(Czesci[P].Czesc[C].Nuta[N].Tekst, 2, 100);
            Czesci[P].Czesc[C].Nuta[N-1].Tekst := Czesci[P].Czesc[C].Nuta[N-1].Tekst + ' ';
          end;
        end; // N
        }

        // correct '-'  to '- '
        {for N := 0 to Czesci[P].Czesc[C].HighNut do
        begin
          if Czesci[P].Czesc[C].Nuta[N].Tekst = '-' then
            Czesci[P].Czesc[C].Nuta[N].Tekst := '- ';
        end; // N
        }
        // add space to the previous note when the current word is '- '
        {for N := 1 to Czesci[P].Czesc[C].HighNut do
        begin
          if Czesci[P].Czesc[C].Nuta[N].Tekst  = '- ' then
            Czesci[P].Czesc[C].Nuta[N-1].Tekst := Czesci[P].Czesc[C].Nuta[N-1].Tekst + ' ';
        end; // N
        }

        // correct too many spaces at the end of note
        for N := 0 to Czesci[P].Czesc[C].HighNut do
        begin
          while Copy(Czesci[P].Czesc[C].Nuta[N].Tekst, Length(Czesci[P].Czesc[C].Nuta[N].Tekst)-1, 2) = '  ' do
            Czesci[P].Czesc[C].Nuta[N].Tekst := Copy(Czesci[P].Czesc[C].Nuta[N].Tekst, 1, Length(Czesci[P].Czesc[C].Nuta[N].Tekst)-1);
        end; // N

        // and correct if there is no space at the end of sentence
        {N := Czesci[P].Czesc[C].HighNut;
        if Copy(Czesci[P].Czesc[C].Nuta[N].Tekst, Length(Czesci[P].Czesc[C].Nuta[N].Tekst), 1) <> ' ' then
          Czesci[P].Czesc[C].Nuta[N].Tekst := Czesci[P].Czesc[C].Nuta[N].Tekst + ' ';}
      end;
    end; // C
    EditorLyric[P].AddCzesc(P, Czesci[P].Akt);
  end;
end;

procedure TScreenEditSub.FixTimings;
var
  P:    integer;
  C:    integer;
  S:    integer;
  Min:  integer;
  Max:  integer;
  len:  integer;

  function GetMin(L: integer): integer;
  var
    len:  integer;
    min:  integer;
  begin
    Result := low(integer);
    if (Length(Czesci[0].Czesc[L].Nuta)>0) then
    begin
      len := Length(Czesci[0].Czesc[L].Nuta);
      Result := Czesci[0].Czesc[L].Nuta[len-1].Start + Czesci[0].Czesc[L].Nuta[len-1].Dlugosc;
    end;

    if not AktSong.isDuet then
      Exit;

    if (Length(Czesci[1].Czesc[L].Nuta)>0) then
    begin
      len := Length(Czesci[1].Czesc[L].Nuta);
      min := Czesci[1].Czesc[L].Nuta[len-1].Start + Czesci[1].Czesc[L].Nuta[len-1].Dlugosc;
      if (min>Result) then
        Result := min;
    end;
  end;

  function GetMax(L: integer): integer;
  var
    max:  integer;
  begin
    Result := high(integer);
    if (Length(Czesci[0].Czesc[L].Nuta)>0) then
    begin
      Result := Czesci[0].Czesc[L].Nuta[0].Start;
    end;

    if not AktSong.isDuet then
      Exit;

    if (Length(Czesci[1].Czesc[L].Nuta)>0) then
    begin
      max := Czesci[1].Czesc[L].Nuta[0].Start;
      if (max<Result) then
        Result := max;
    end;
  end;

begin
  for P := 0 to Length(Czesci) - 1 do
  begin
    for C := 1 to Length(Czesci[P].Czesc) - 1 do
    begin
      len := Length(Czesci[P].Czesc[C-1].Nuta);
      Min := Czesci[P].Czesc[C-1].Nuta[len-1].Start + Czesci[P].Czesc[C-1].Nuta[len-1].Dlugosc;
      Max := Czesci[P].Czesc[C].Nuta[0].Start;
      case (Max - Min) of
        0:    S := Max;
        1:    S := Max;
        2:    S := Max - 1;
        3:    S := Max - 2;
        else
          S := Min + 2;
      end; // case

      Czesci[P].Czesc[C].Start := S;
      Czesci[P].Czesc[C-1].Koniec := Min;
    end; // for
    C := Length(Czesci[P].Czesc) - 1;
    len := Length(Czesci[P].Czesc[C].Nuta);
    Max := Czesci[P].Czesc[C].Nuta[len-1].Start + Czesci[P].Czesc[C].Nuta[len-1].Dlugosc;
    Czesci[P].Czesc[C].Koniec := Max;
  end;
  {
  //second run for duet mode:
  if not AktSong.isDuet then
    Exit;
  
  for P := 0 to Length(Czesci) - 1 do
  begin
    Czesci[P].Czesc[0].Start := -100;
    for C := 1 to Czesci[P].High do
    begin
      if (Length(Czesci[P].Czesc[C-1].Nuta)=0) then
        Czesci[P].Czesc[C].Start := Czesci[(P+1) mod 2].Czesc[C].Start;
    end;
  end;    }
end;

procedure TScreenEditSub.DivideSentence;
var
  P:      integer;
  C:      integer;
  CStart: integer;
  CNew:   integer;
  CLen:   integer;
  N:      integer;
  NStart: integer;
  NHigh:  integer;
  NNewL:  integer;

  BStart: integer; //start beat

begin
  CStart := Czesci[CP].Akt;
  BStart := Czesci[CP].Czesc[CStart].Nuta[AktNuta[CP]].Start;

  CNew := CStart + 1;

  P := CP;
  {for P := 0 to Length(Czesci) - 1 do
  begin}
    // increase sentence length by 1
    CLen := Length(Czesci[P].Czesc);
    SetLength(Czesci[P].Czesc, CLen + 1);
    Inc(Czesci[P].Ilosc);
    Inc(Czesci[P].High);

    // move needed sentences to one forward. newly has the copy of divided sentence
    for C := CLen-1 downto CStart do
      CopyLine(P, C, P, C+1);
      //Czesci[P].Czesc[C+1] := Czesci[P].Czesc[C];

    // clear and set new sentence
    NStart := -1;
    if (Length(Czesci[P].Czesc[CStart].Nuta)>0) then
    begin
      for N := 0 to Length(Czesci[P].Czesc[CStart].Nuta) - 1 do
      begin
        if Czesci[P].Czesc[CStart].Nuta[N].Start>=BStart then
        begin
          NStart := N;
          break;
        end;
      end;

      if (NStart > -1) then
      begin
        Czesci[P].Czesc[CNew].Start := Czesci[P].Czesc[CStart].Nuta[NStart].Start;
        Czesci[P].Czesc[CNew].StartNote := Czesci[P].Czesc[CStart].Nuta[NStart].Start;
      end;
    end;

    Czesci[P].Czesc[CNew].Lyric := '';
    Czesci[P].Czesc[CNew].LyricWidth := 0;
    Czesci[P].Czesc[CNew].Koniec := 0;
    Czesci[P].Czesc[CNew].BaseNote := 0; // 0.5.0: we modify it later in this procedure
    Czesci[P].Czesc[CNew].IlNut := 0;
    Czesci[P].Czesc[CNew].HighNut := -1;
    SetLength(Czesci[P].Czesc[CNew].Nuta, 0);

    // move right notes to new sentences
    if (NStart > -1) then
    begin
      NHigh := Czesci[P].Czesc[CStart].HighNut;
      for N := NStart to NHigh do
      begin
        NNewL := Czesci[P].Czesc[CNew].IlNut;
        SetLength(Czesci[P].Czesc[CNew].Nuta, NNewL + 1);
        CopyNote(P, CStart, N, P, CNew, NNewL);
        //Czesci[P].Czesc[CNew].Nuta[NNewL] := Czesci[P].Czesc[CStart].Nuta[N];

        // increase sentence counters
        Inc(Czesci[P].Czesc[CNew].IlNut);
        Inc(Czesci[P].Czesc[CNew].HighNut);
        Czesci[P].Czesc[CNew].Koniec := Czesci[P].Czesc[CNew].Nuta[NNewL].Start +
          Czesci[P].Czesc[CNew].Nuta[NNewL].Dlugosc;
      end;

      // clear old notes and set sentence counters
      Czesci[P].Czesc[CStart].HighNut := NStart - 1;
      Czesci[P].Czesc[CStart].IlNut := Czesci[P].Czesc[CStart].HighNut + 1;
      if (NStart>0) then
        Czesci[P].Czesc[CStart].Koniec := Czesci[P].Czesc[CStart].Nuta[NStart-1].Start +
          Czesci[P].Czesc[CStart].Nuta[NStart-1].Dlugosc;

      SetLength(Czesci[P].Czesc[CStart].Nuta, Czesci[P].Czesc[CStart].IlNut);
    end;
    Czesci[P].Akt := Czesci[P].Akt + 1;
    AktNuta[P] := 0;
  //end;

  Refresh;
  Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
  EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
  EditorLyric[CP].Selected := AktNuta[CP];
end;

procedure TScreenEditSub.JoinSentence;
var
  P:      integer;
  C:      integer;
  N:      integer;
  NStart: integer;
  NDst:   integer;
begin
  P := CP;

  C := Czesci[CP].Akt;
  // set new sentence
  NStart := Czesci[P].Czesc[C].IlNut;
  Czesci[P].Czesc[C].IlNut := Czesci[P].Czesc[C].IlNut + Czesci[P].Czesc[C+1].IlNut;
  Czesci[P].Czesc[C].HighNut := Czesci[P].Czesc[C].HighNut + Czesci[P].Czesc[C+1].IlNut;
  SetLength(Czesci[P].Czesc[C].Nuta, Czesci[P].Czesc[C].IlNut);

  // move right notes to new sentences
  if (Length(Czesci[P].Czesc[C+1].Nuta)>0) then
  begin
    for N := 0 to Czesci[P].Czesc[C+1].HighNut do
    begin
      NDst := NStart + N;
      CopyNote(P, C+1, N, P, C, NDst);
    end;

    //add space before first note of 2. sentence
    if (Copy(Czesci[P].Czesc[C].Nuta[NStart].Tekst, 1, 1) <> ' ') then
      Czesci[P].Czesc[C].Nuta[NStart].Tekst := ' ' + Czesci[P].Czesc[C].Nuta[NStart].Tekst;

    // increase sentence counters
    NDst := Czesci[P].Czesc[C].HighNut;
    Czesci[P].Czesc[C].Koniec := Czesci[P].Czesc[C].Nuta[NDst].Start +
    Czesci[P].Czesc[C].Nuta[NDst].Dlugosc;
  end;
  // move needed sentences to one backward.
  for C := Czesci[P].Akt + 1 to Czesci[P].High - 1 do
    CopyLine(P, C+1, P, C);

  // decrease sentence length by 1
  SetLength(Czesci[P].Czesc, Length(Czesci[P].Czesc) - 1);
  Dec(Czesci[P].Ilosc);
  Dec(Czesci[P].High);

  Refresh;
  Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
  EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
  EditorLyric[CP].Selected := AktNuta[CP];
end;

procedure TScreenEditSub.DivideNote;
var
  C:    integer;
  N:    integer;
  NLen: integer;
begin
  C := Czesci[CP].Akt;

  NLen := Czesci[CP].Czesc[C].IlNut + 1;
  SetLength(Czesci[CP].Czesc[C].Nuta, NLen);
  Inc(Czesci[CP].Czesc[C].HighNut);
  Inc(Czesci[CP].Czesc[C].IlNut);

  // we copy all notes including selected one
  for N := Czesci[CP].Czesc[C].HighNut downto AktNuta[CP]+1 do
  begin
    Czesci[CP].Czesc[C].Nuta[N] := Czesci[CP].Czesc[C].Nuta[N-1];
  end;

  // me slightly modify new note
  Czesci[CP].Czesc[C].Nuta[AktNuta[CP]].Dlugosc := ceil(Czesci[CP].Czesc[C].Nuta[AktNuta[CP]+1].Dlugosc/2);

  Czesci[CP].Czesc[C].Nuta[AktNuta[CP]+1].Start := Czesci[CP].Czesc[C].Nuta[AktNuta[CP]+1].Start +
    Czesci[CP].Czesc[C].Nuta[AktNuta[CP]].Dlugosc;

  Czesci[CP].Czesc[C].Nuta[AktNuta[CP]+1].Dlugosc := Czesci[CP].Czesc[C].Nuta[AktNuta[CP]+1].Dlugosc -
    Czesci[CP].Czesc[C].Nuta[AktNuta[CP]].Dlugosc;

  if (Czesci[CP].Czesc[C].Nuta[AktNuta[CP]+1].Dlugosc>0) then
  begin
    Czesci[CP].Czesc[C].Nuta[AktNuta[CP]+1].Tekst := '~';
    Czesci[CP].Czesc[C].Nuta[AktNuta[CP]].Color := 2;
    Czesci[CP].Czesc[C].Nuta[AktNuta[CP]+1].Color := 0;
  end else
  begin
    Czesci[CP].Czesc[C].Nuta[AktNuta[CP]+1].Tekst := ' ';
    Czesci[CP].Czesc[C].Nuta[AktNuta[CP]].Color := 0;
    Czesci[CP].Czesc[C].Nuta[AktNuta[CP]+1].Color := 2;

    Inc(AktNuta[CP]);
  end;

  EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
  EditorLyric[CP].Selected := AktNuta[CP];
end;

procedure TScreenEditSub.DeleteNote;
var
  C:    integer;
  N:    integer;
  NLen: integer;
begin
  C := Czesci[CP].Akt;

  //Do Not delete Last Note
  if (Czesci[CP].High > 0) OR (Czesci[CP].Czesc[C].HighNut > 0) then
  begin

    // we copy all notes from the next to the selected one
    for N := AktNuta[CP]+1 to Czesci[CP].Czesc[C].HighNut do
    begin
      Czesci[CP].Czesc[C].Nuta[N-1] := Czesci[CP].Czesc[C].Nuta[N];
    end;

    NLen := Czesci[CP].Czesc[C].IlNut - 1;

    if (NLen > 0) then
    begin
      SetLength(Czesci[CP].Czesc[C].Nuta, NLen);
      Dec(Czesci[CP].Czesc[C].HighNut);
      Dec(Czesci[CP].Czesc[C].IlNut);

      // me slightly modify new note
      if AktNuta[CP] > Czesci[CP].Czesc[C].HighNut then
        Dec(AktNuta[CP]);

    end
    //Last Note of current Sentence Deleted - > Delete Sentence
    else
    begin
      DeleteSentence;
    end;
  end;
  
  Refresh;
  EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
  Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
  EditorLyric[CP].Selected := AktNuta[CP];
end;

procedure TScreenEditSub.DeleteSentence;
var
  Pv, Pt: integer;
  P:      integer;
  C:      integer;
  N:      integer;

begin
  Pv := CP;
  Pt := CP;

  {if AktSong.isDuet then
  begin
    if (Length(Czesci[(CP+1) mod 2].Czesc[Czesci[CP].Akt].Nuta)=0) then
    begin
      Pv := 0;
      Pt := 1;
    end;
  end;  }

  for P := Pv to Pt do
  begin
    C := Czesci[CP].Akt;
    {if (Pv <> Pt) or not AktSong.isDuet then
    begin}
      //Move all Sentences after the current to the Left
      for N := C+1 to Czesci[P].High do
        //Czesci[P].Czesc[N-1] := Czesci[P].Czesc[N];
        CopyLine(P, N, P, N-1);

      //Delete Last Sentence
      SetLength(Czesci[P].Czesc, Czesci[P].High);
      Czesci[P].High := High(Czesci[P].Czesc);
      Czesci[P].Ilosc := Length(Czesci[P].Czesc);

      AktNuta[P] := 0;
      if (C > 0) then
        Czesci[P].Akt := C - 1
      else
        Czesci[P].Akt := 0;
    {end else
    begin
      //delete all notes in that line
      SetLength(Czesci[P].Czesc[C].Nuta, 0);

      //switch to the other line
      CP := (CP+1) mod 2;
      AktNuta[CP] := 0;
      AktNuta[(CP+1) mod 2] := 0;
    end;}
  end;
  Refresh;
  //SelectPrevNote();
  //SelectNextNote();
  Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
  EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
end;

procedure TScreenEditSub.TransposeNote(Transpose: integer);
begin
  if (Length(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta)>0) then
  begin
    Inc(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Ton, Transpose);
  end;
end;

procedure TScreenEditSub.ChangeWholeTone(Tone: integer);
var
  C:  integer;
  N:  integer;
begin

    for C := 0 to Czesci[CP].High do
    begin
      if (Length(Czesci[CP].Czesc[C].Nuta)>0) then
      begin
        Czesci[CP].Czesc[C].BaseNote := Czesci[CP].Czesc[C].BaseNote + Tone;
        for N := 0 to Czesci[CP].Czesc[C].HighNut do
          Czesci[CP].Czesc[C].Nuta[N].Ton := Czesci[CP].Czesc[C].Nuta[N].Ton + Tone;
      end;
    end;
end;

procedure TScreenEditSub.ChangeWholeToneActLine(Tone: integer);
var
  C:  integer;
  N:  integer;

begin
  C := Czesci[CP].Akt;
  if (Length(Czesci[CP].Czesc[C].Nuta)>0) then
  begin
    Czesci[CP].Czesc[C].BaseNote := Czesci[CP].Czesc[C].BaseNote + Tone;
    for N := 0 to Czesci[CP].Czesc[C].HighNut do
      Czesci[CP].Czesc[C].Nuta[N].Ton := Czesci[CP].Czesc[C].Nuta[N].Ton + Tone;
  end;
end;

procedure TScreenEditSub.MoveAllToEnd(Move: integer);
var
  C:    integer;
  N:    integer;
  NStart: integer;
begin
  for C := Czesci[CP].Akt to Czesci[CP].High do
  begin
    NStart := 0;
    if C = Czesci[CP].Akt then NStart := AktNuta[CP];
    for N := NStart to Czesci[CP].Czesc[C].HighNut do
    begin
      Inc(Czesci[CP].Czesc[C].Nuta[N].Start, Move); // move note start

      if N = 0 then
      begin // fix beginning
        Inc(Czesci[CP].Czesc[C].Start, Move);
        Inc(Czesci[CP].Czesc[C].StartNote, Move);
      end;

      if N = Czesci[CP].Czesc[C].HighNut then // fix ending
        Inc(Czesci[CP].Czesc[C].Koniec, Move);

    end; // for
  end; // for
  EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
end;

procedure TScreenEditSub.MoveTextToRight;
var
  C:      integer;
  N:      integer;
  NHigh:  integer;
begin
  C := Czesci[CP].Akt;
  NHigh := Czesci[CP].Czesc[C].HighNut;

  // last word
  Czesci[CP].Czesc[C].Nuta[NHigh].Tekst := Czesci[CP].Czesc[C].Nuta[NHigh-1].Tekst +
    Czesci[CP].Czesc[C].Nuta[NHigh].Tekst;

  // other words
  for N := NHigh - 1 downto AktNuta[CP] + 1 do
  begin
    Czesci[CP].Czesc[C].Nuta[N].Tekst := Czesci[CP].Czesc[C].Nuta[N-1].Tekst;
  end; // for
  Czesci[CP].Czesc[C].Nuta[AktNuta[CP]].Tekst := '- ';
  EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
end;

procedure TScreenEditSub.MarkSrc;
begin
  CopySrcLine := Czesci[CP].Akt;
  CopySrcCP := CP;
end;

procedure TScreenEditSub.PasteText;
var
  C:    integer;
  N:    integer;
begin
  C := Czesci[CP].Akt;

  for N := 0 to Czesci[CopySrcCP].Czesc[CopySrcLine].HighNut do
    Czesci[CP].Czesc[C].Nuta[N].Tekst := Czesci[CopySrcCP].Czesc[CopySrcLine].Nuta[N].Tekst;

  Refresh;
  Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
  EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
end;

procedure TScreenEditSub.CopySentence(Src, Dst: integer);
var
  N:      integer;
  Time1:  integer;
  Time2:  integer;
  TD:  integer;
begin
  Time1 := Czesci[CopySrcCP].Czesc[Src].Nuta[0].Start;
  Time2 := Czesci[CP].Czesc[Dst].Nuta[0].Start;
  TD := Time2-Time1;

  SetLength(Czesci[CP].Czesc[Dst].Nuta, Czesci[CopySrcCP].Czesc[Src].IlNut);
  Czesci[CP].Czesc[Dst].IlNut := Czesci[CopySrcCP].Czesc[Src].IlNut;
  Czesci[CP].Czesc[Dst].HighNut := Czesci[CopySrcCP].Czesc[Src].HighNut;
  for N := 0 to Czesci[CopySrcCP].Czesc[Src].HighNut do
  begin
    Czesci[CP].Czesc[Dst].Nuta[N].Tekst := Czesci[CopySrcCP].Czesc[Src].Nuta[N].Tekst;
    Czesci[CP].Czesc[Dst].Nuta[N].Dlugosc := Czesci[CopySrcCP].Czesc[Src].Nuta[N].Dlugosc;
    Czesci[CP].Czesc[Dst].Nuta[N].Ton := Czesci[CopySrcCP].Czesc[Src].Nuta[N].Ton;
    Czesci[CP].Czesc[Dst].Nuta[N].FreeStyle := Czesci[CopySrcCP].Czesc[Src].Nuta[N].FreeStyle;
    Czesci[CP].Czesc[Dst].Nuta[N].Wartosc := Czesci[CopySrcCP].Czesc[Src].Nuta[N].Wartosc;
    Czesci[CP].Czesc[Dst].Nuta[N].Start := Czesci[CopySrcCP].Czesc[Src].Nuta[N].Start + TD;
  end;
  N := Czesci[CopySrcCP].Czesc[Src].HighNut;
  Czesci[CP].Czesc[Dst].Koniec := Czesci[CP].Czesc[Dst].Nuta[N].Start + Czesci[CP].Czesc[Dst].Nuta[N].Dlugosc;

  Refresh;
  Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
  EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
end;

procedure TScreenEditSub.CopySentences(Src, Dst, Num: integer);
var
  C:      integer;
begin
  // create place for new sentences
  SetLength(Czesci[0].Czesc, Czesci[0].Ilosc + Num - 1);

  // moves sentences next to the destination
  for C := Czesci[0].High downto Dst + 1 do begin
    Czesci[0].Czesc[C + Num - 1] := Czesci[0].Czesc[C];
  end;

  // prepares new sentences: sets sentence start and create first note
  for C := 1 to Num-1 do begin
    Czesci[0].Czesc[Dst + C].Start := Czesci[0].Czesc[Dst + C - 1].StartNote +
      (Czesci[0].Czesc[Src + C].StartNote - Czesci[0].Czesc[Src + C - 1].StartNote);
    SetLength(Czesci[0].Czesc[Dst + C].Nuta, 1);
    Czesci[0].Czesc[Dst + C].IlNut := 1;
    Czesci[0].Czesc[Dst + C].HighNut := 0;
    Czesci[0].Czesc[Dst + C].Nuta[0].Start := Czesci[0].Czesc[Dst + C].Start;
    Czesci[0].Czesc[Dst + C].Nuta[0].Dlugosc := 1;
    Czesci[0].Czesc[Dst + C].StartNote := Czesci[0].Czesc[Dst + C].Start;
    Czesci[0].Czesc[Dst + C].Koniec := Czesci[0].Czesc[Dst + C].Start + 1;
  end;

  // increase counters
  Czesci[0].Ilosc := Czesci[0].Ilosc + Num - 1;
  Czesci[0].High := Czesci[0].High + Num - 1;

  for C := 0 to Num-1 do
    CopySentence(Src + C, Dst + C);
end;


constructor TScreenEditSub.Create;
begin
  inherited Create;
  SetLength(Player, 1);

  //light blue:
  cRB := 0.9; cGB := 0.95; cBB := 1;

  //light red:
  cRR := 1; cGR := 0.8; cBR := 0.8;

  // Line
  //AddText(500, 573, 1, 7, 0, 0, 0, 'Line:');
  TextSentence := AddText(500, 573, 1, 7, 0, 0, 0, 'Line: 0/0');

  // Note
  //AddText(655, 573, 1, 7, 0, 0, 0, 'Note:');
  TextNote := AddText(655, 573, 1, 7, 0, 0, 0, 'Note: 0/0');

  AddText(10, 10, 0, 8, 0, 0, 0, 'Title:');
  AddText(10, 30, 0, 8, 0, 0, 0, 'Artist:');
  //AddText(10, 50, 0, 8, 0, 0, 0, 'Mp3:');
  AddText(10, 50, 0, 8, 0, 0, 0, 'BPM:');
  AddText(10, 70, 0, 8, 0, 0, 0, 'GAP:');

  TextTitle :=  AddText(80, 10, 0, 8, 0, 0, 0, 'a');
  TextArtist := AddText(80, 30, 0, 8, 0, 0, 0, 'b');
  //TextMp3 :=    AddText(80, 50, 0, 8, 0, 0, 0, 'c');
  TextBPM :=    AddText(80, 50, 0, 8, 0, 0, 0, 'd');
  TextGAP :=    AddText(80, 70, 0, 8, 0, 0, 0, 'e');

  // note info
  AddText(10, 90,  0, 8, 0, 0, 0, 'Start:');
    AddText(300, 90, 0, 8, 0, 0, 0, 'Duration:');

  AddText(10, 110, 0, 8, 0, 0, 0, 'Tone:');
  AddText(10, 130, 0, 8, 0, 0, 0, 'Text:');
    AddText(300, 130,  0, 8, 0, 0, 0, 'VideoGap:');

  TextNStart :=   AddText(80, 90,  0, 8, 0, 0, 0, 'a');
  TextNDlugosc := AddText(400, 90,  0, 8, 0, 0, 0, 'b');
  TextNTon :=     AddText(80, 110,  0, 8, 0, 0, 0, 'c');
  TextNText :=    AddText(80, 130,  0, 8, 0, 0, 0, 'd');
  TextVideoGap :=  AddText(400, 130,  0, 8, 0, 0, 0, 'e');

  // debug
  TextDebug :=  AddText(30, 575, 0, 9, 0, 0, 0, '');

  EditorLyric[0] := TLyric.Create;
  EditorLyric[1] := TLyric.Create;

  offset[0] := 155;
  offset[1] := 525;
end;

procedure TScreenEditSub.SelectNextNote();
begin
  Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;

  EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
  EditorLyric[CP].Selected := 0;
end;

procedure TScreenEditSub.SelectPrevNote();
begin
  Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;

  EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
  EditorLyric[CP].Selected := 0;
end;

procedure TScreenEditSub.MakeSingle;
begin
  SetLength(Czesci, 1);
  AktSong.isDuet := false;
  CP := 0;
  Refresh;
  AktNuta[CP] := 0;
  Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;

  EditorLyric[0].Y := offset[0]+300;
  EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
end;

procedure TScreenEditSub.MakeDuet;
var
  L, I:  integer;

begin
  SetLength(Czesci, 2);

  Czesci[1].Akt := Czesci[0].Akt;
  Czesci[1].High := Czesci[0].High;
  Czesci[1].Ilosc := Czesci[0].Ilosc;
  Czesci[1].Resolution := Czesci[0].Resolution;
  Czesci[1].NotesGAP := Czesci[0].NotesGAP;
  Czesci[1].Wartosc := 0;
  SetLength(Czesci[1].Czesc, Length(Czesci[0].Czesc));

  for L := 0 to Length(Czesci[0].Czesc) - 1 do
    CopyLine(0, L, 1, L);

  AktSong.isDuet := true;

  AktNuta[1] := 0;
  Czesci[1].Akt := 0;

  for I := 0 to Length(Czesci)-1 do
  begin
    EditorLyric[I].Clear;
    EditorLyric[I].X := 400;
    if not AktSong.isDuet and (I=0) then
      EditorLyric[I].Y := offset[I]+300
    else
      EditorLyric[I].Y := offset[I];

    EditorLyric[I].Align := 1;
    EditorLyric[I].Size := 13;
    EditorLyric[I].ColR := 0;
    EditorLyric[I].ColG := 0;
    EditorLyric[I].ColB := 0;
    EditorLyric[I].ColSR := Skin_FontHighlightR;
    EditorLyric[I].ColSG := Skin_FontHighlightG;
    EditorLyric[I].ColSB := Skin_FontHighlightB;
    EditorLyric[I].Style := 0;
    EditorLyric[I].AddCzesc(I, Czesci[I].Akt);
    EditorLyric[I].Selected := 0;
  end;

  //delete medley
  MedleyNotes.isStart := false;
  MedleyNotes.isEnd := false;
  AktSong.Medley.Source := msNone;
end;

function TScreenEditSub.DuetCopyLine: boolean;
var
  LSrc, LDst: integer;
  CSrc, CDst: integer;

  SrcStart:   integer;
  SrcEnd:     integer;

  DstStart:   integer;
  DstEnd:     integer;

  SrcNumN:    integer;
  DstNumN:    integer;

  I, C:       integer;

  CLen:       integer;
begin
  Result := false;

  CSrc := CP;
  CDst := (CP+1) mod 2;
  LSrc := Czesci[CSrc].Akt;
  LDst := -1;

  SrcStart := Czesci[CSrc].Czesc[LSrc].Nuta[0].Start;
  SrcNumN := Length(Czesci[CSrc].Czesc[LSrc].Nuta);
  SrcEnd := Czesci[CSrc].Czesc[LSrc].Nuta[SrcNumN-1].Start + Czesci[CSrc].Czesc[LSrc].Nuta[SrcNumN-1].Dlugosc;

  for I := 0 to Length(Czesci[CDst].Czesc)-1 do
  begin
    DstStart := Czesci[CDst].Czesc[I].Nuta[0].Start;
    DstNumN := Length(Czesci[CDst].Czesc[I].Nuta);
    DstEnd := Czesci[CDst].Czesc[I].Nuta[DstNumN-1].Start + Czesci[CDst].Czesc[I].Nuta[DstNumN-1].Dlugosc;
    if (DstStart<=SrcStart) and (SrcEnd<=DstEnd) then
    begin
      LDst := I;
      break;
    end;

    if (LDst = -1) and (I<Length(Czesci[CDst].Czesc)-1) then
    begin
      DstStart := DstEnd;
      DstEnd := Czesci[CDst].Czesc[I+1].Nuta[0].Start;
      if (DstStart<SrcStart) and (SrcEnd<DstEnd) then
      begin
        CLen := Length(Czesci[CDst].Czesc);
        SetLength(Czesci[CDst].Czesc, CLen + 1);
        Inc(Czesci[CDst].Ilosc);
        Inc(Czesci[CDst].High);

        for C := CLen-1 downto I do
          CopyLine(CDst, C, CDst, C+1);

        SetLength(Czesci[CDst].Czesc[I+1].Nuta, 0);
        LDst := I+1;
        break;
      end;
    end;
  end;

  if (LDst = -1) then
    Exit;

  CopyLine(CSrc, LSrc, CDst, LDst);

  Refresh;
  EditorLyric[CDst].AddCzesc(CDst, Czesci[CDst].Akt);
  EditorLyric[CDst].Selected := 0;
  AktNuta[CDst] := 0;
  Czesci[CSrc].Czesc[LSrc].Nuta[AktNuta[CSrc]].Color := 2;
  Result := true;
end;

procedure TScreenEditSub.CopyLine(Pf, Cf, Pt, Ct: integer);
var
  N:  integer;
begin
  Czesci[Pt].Czesc[Ct].IlNut := Czesci[Pf].Czesc[Cf].IlNut;
  Czesci[Pt].Czesc[Ct].HighNut := Czesci[Pf].Czesc[Cf].HighNut;
  Czesci[Pt].Czesc[Ct].Koniec := Czesci[Pf].Czesc[Cf].Koniec;
  Czesci[Pt].Czesc[Ct].Start := Czesci[Pf].Czesc[Cf].Start;
  Czesci[Pt].Czesc[Ct].BaseNote := Czesci[Pf].Czesc[Cf].BaseNote;
  Czesci[Pt].Czesc[Ct].StartNote := Czesci[Pf].Czesc[Cf].StartNote;

  SetLength(Czesci[Pt].Czesc[Ct].Nuta, Czesci[Pf].Czesc[Cf].IlNut);
  for N := 0 to Czesci[Pf].Czesc[Cf].HighNut do
    CopyNote(Pf, Cf, N, Pt, Ct, N);
end;

procedure TScreenEditSub.CopyNote(Pf, Cf, Nf, Pt, Ct, Nt: integer);
begin
  Czesci[Pt].Czesc[Ct].Nuta[Nt].Color := 0;
  Czesci[Pt].Czesc[Ct].Nuta[Nt].Start := Czesci[Pf].Czesc[Cf].Nuta[Nf].Start;
  Czesci[Pt].Czesc[Ct].Nuta[Nt].Dlugosc := Czesci[Pf].Czesc[Cf].Nuta[Nf].Dlugosc;
  Czesci[Pt].Czesc[Ct].Nuta[Nt].Ton := Czesci[Pf].Czesc[Cf].Nuta[Nf].Ton;
  Czesci[Pt].Czesc[Ct].Nuta[Nt].TonGamy := Czesci[Pf].Czesc[Cf].Nuta[Nf].TonGamy;
  Czesci[Pt].Czesc[Ct].Nuta[Nt].Tekst := Czesci[Pf].Czesc[Cf].Nuta[Nf].Tekst;
  Czesci[Pt].Czesc[Ct].Nuta[Nt].FreeStyle := Czesci[Pf].Czesc[Cf].Nuta[Nf].FreeStyle;
  Czesci[Pt].Czesc[Ct].Nuta[Nt].Wartosc := Czesci[Pf].Czesc[Cf].Nuta[Nf].Wartosc;
  Czesci[Pt].Czesc[Ct].Nuta[Nt].IsMedley := Czesci[Pf].Czesc[Cf].Nuta[Nf].IsMedley;
  Czesci[Pt].Czesc[Ct].Nuta[Nt].IsStartPreview := Czesci[Pf].Czesc[Cf].Nuta[Nf].IsStartPreview;
end;

procedure TScreenEditSub.DuetMoveLine;
begin
  if DuetCopyLine then
    DeleteSentence;
  {Czesci[CP].Czesc[Czesci[CP].Akt].Lyric := '';
  Czesci[CP].Czesc[Czesci[CP].Akt].LyricWidth := 0;
  Czesci[CP].Czesc[Czesci[CP].Akt].HighNut := -1;
  Czesci[CP].Czesc[Czesci[CP].Akt].IlNut := 0;
  Czesci[CP].Czesc[Czesci[CP].Akt].TotalNotes := 0;
  SetLength(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta, 0);

  EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
  EditorLyric[CP].Selected := -1;

  CP := (CP+1) mod 2;}
  //Refresh;
end;

procedure TScreenEditSub.Refresh;
var
  P:  integer;
  L:  integer;
  N:  integer;

begin
  FixTimings;
  LyricsCorrectSpaces;

  if MedleyNotes.isStart and
    ((High(Czesci[0].Czesc)<MedleyNotes.start.line) or
     (High(Czesci[0].Czesc[MedleyNotes.start.line].Nuta)<MedleyNotes.start.note)) then
    MedleyNotes.isStart := false;

  if MedleyNotes.isEnd and
    ((High(Czesci[0].Czesc)<MedleyNotes.end_.line) or
     (High(Czesci[0].Czesc[MedleyNotes.end_.line].Nuta)<MedleyNotes.end_.note)) then
    MedleyNotes.isEnd := false;

  for P := 0 to Length(Czesci) - 1 do
  begin
    Czesci[P].Ilosc := Length(Czesci[P].Czesc);
    Czesci[P].High := Czesci[P].Ilosc-1;
    Czesci[P].Wartosc := 0;

    for L := 0 to Czesci[P].High do
    begin
      with Czesci[P].Czesc[L] do
      begin
        IlNut := Length(Nuta);
        HighNut := IlNut-1;
        TotalNotes := 0;
        BaseNote := 120;

        if (Length(Nuta)>0) then
        begin
          StartNote := Nuta[0].Start;
          for N := 0 to Length(Czesci[P].Czesc[L].Nuta) - 1 do
          begin
            Nuta[N].Color := 0;
            if (MedleyNotes.isStart and (MedleyNotes.start.CP = P) and (MedleyNotes.start.line = L) and
              (MedleyNotes.start.note = N)) or
              (MedleyNotes.isEnd and (MedleyNotes.end_.CP = P) and (MedleyNotes.end_.line = L) and
              (MedleyNotes.end_.note = N)) then
              Nuta[N].IsMedley := true
            else
              Nuta[N].IsMedley := false;

            Nuta[N].IsStartPreview := false;

            Czesci[P].Wartosc := Czesci[P].Wartosc + Nuta[N].Dlugosc * Nuta[N].Wartosc;
            TotalNotes := TotalNotes + Nuta[N].Dlugosc * Nuta[N].Wartosc;

            if (Nuta[N].Ton < BaseNote) then
              BaseNote := Nuta[N].Ton;
          end;
        end else
          BaseNote := 0;
      end;
    end;
  end;

  //set Preview Start
  MedleyNotes.Preview := FindNote(round(GetMidBeat(AktSong.PreviewStart-AktSong.Gap/1000)));
  Czesci[MedleyNotes.Preview.CP].Czesc[MedleyNotes.Preview.line].Nuta[MedleyNotes.Preview.note].IsStartPreview := true;
  AktSong.PreviewStart :=
    GetTimeFromBeat(Czesci[MedleyNotes.Preview.CP].Czesc[MedleyNotes.Preview.line].Nuta[MedleyNotes.Preview.note].start);
end;

procedure TScreenEditSub.onShow;
var
  I:  integer;

begin
  Log.LogStatus('Initializing', 'TEditScreen.onShow');

  try
    ResetSingTemp;
    AktSong := CatSongs.Song[SongIndex];
    Error := not LoadSong(Path + FileName, SONG_LOAD_COMPLETE);
    if not Error and not AktSong.isDuet then
      FindRefrainStart(AktSong);
  except
    Error := True;
  end;

  if Error then
  begin
    //Error Loading Song -> Go back to Song Screen and Show some Error Message
    FadeTo(@ScreenSong);
    ScreenPopupError.ShowPopup (Language.Translate('ERROR_CORRUPT_SONG'));
    Exit;
  end
  else
  begin
    Music.CaptureStart;
    Refresh;
    MidiOut := TMidiOutput.Create(nil);
    MidiOut.Open;

    SetLength(Pitches, 0);
    PitchRecOn := false;

    //Set Volume
    MP3Volume := 50;
    Music.SetMusicVolume(MP3Volume);

    CP := 0;

    if not Help.SetHelpID(ID) then
      Log.LogError('No Entry for Help-ID ' + ID + ' (ScreenEditSub)');

    Text[TextTitle].Text :=   AktSong.Title;
    Text[TextArtist].Text :=  AktSong.Artist;
    //Text[TextMp3].Text :=     AktSong.Mp3;

    Czesci[0].Akt := 0;
    AktNuta[0] := 0;
    AktNuta[1] := 0;
    noteStart := 0; //when playing sentence
    lineStart := 0;
    cpStart := 0;

    if AktSong.isDuet then
    begin
      Czesci[1].Akt := 0;
      SelectNextNote;
    end else
      Czesci[0].Czesc[0].Nuta[0].Color := 2;

    if AktSong.Medley.Source <> msNone then
    begin
      MedleyNotes.isStart := true;
      MedleyNotes.isEnd := true;
      MedleyNotes.start := FindNote(AktSong.Medley.StartBeat);
      MedleyNotes.end_ := FindNote(AktSong.Medley.EndBeat);
      Czesci[0].Czesc[MedleyNotes.start.line].Nuta[MedleyNotes.start.note].IsMedley := true;
      Czesci[0].Czesc[MedleyNotes.end_.line].Nuta[MedleyNotes.end_.note].IsMedley := true;
    end;

    //set Preview Start
    MedleyNotes.Preview := FindNote(round(GetMidBeat(AktSong.PreviewStart-AktSong.Gap/1000)));
    Czesci[MedleyNotes.Preview.CP].Czesc[MedleyNotes.Preview.line].Nuta[MedleyNotes.Preview.note].IsStartPreview := true;
    AktSong.PreviewStart :=
      GetTimeFromBeat(Czesci[MedleyNotes.Preview.CP].Czesc[MedleyNotes.Preview.line].Nuta[MedleyNotes.Preview.note].start);

    Music.Open(Path + AktSong.Mp3);

    for I := 0 to Length(Czesci)-1 do
    begin
      EditorLyric[I].Clear;
      EditorLyric[I].X := 400;
      if not AktSong.isDuet and (I=0) then
        EditorLyric[I].Y := offset[I]+300
      else
        EditorLyric[I].Y := offset[I];

      EditorLyric[I].Align := 1;
      EditorLyric[I].Size := 13;
      EditorLyric[I].ColR := 0;
      EditorLyric[I].ColG := 0;
      EditorLyric[I].ColB := 0;
      EditorLyric[I].ColSR := Skin_FontHighlightR;
      EditorLyric[I].ColSG := Skin_FontHighlightG;
      EditorLyric[I].ColSB := Skin_FontHighlightB;
      EditorLyric[I].Style := 0;
      EditorLyric[I].AddCzesc(I, Czesci[I].Akt);
      EditorLyric[I].Selected := 0;
    end;

    NotesH := 7;
    NotesW := 4;

  end;

//  Interaction := 0;
  TextEditMode := false;
  BPMEditMode := false;

  //MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[MidiLastNote].Ton + 60, 127);
  MidiLastNote := 0;
  PlaySentenceMidi := false;
  PlayOneNoteMidi := false;
  Music.Stop;
  LineChanged[0]:=false;
  LineChanged[1]:=false;
  PlaySentence := false;
  PlayOneNote := false;

  StartTry := false;
  PlayTime := 0;
  PlayVideo := false;
end;

function TScreenEditSub.Draw: boolean;
var
  //Min:    integer;
  //Sec:    integer;
  //Tekst:  string;
  Pet:    integer;
  PlayClick:  boolean;
  line, note: integer;
  end_:   boolean;

  Window: TRectCoords;
  Blend:  real;

  beat:   integer;
  last:   integer;
begin
  DrawStatics;
  end_ := false;

  glClearColor(1,1,1,1);

  last := LastClick;

  PlayClick := false;
  if PlaySentenceMidi or PlaySentence then
  begin
    MidiPos := USTime.GetTime - MidiTime + MidiStart;
    PlayTime := PlayTime + TimeSkip;
    // click
    if PlaySentence then
    begin
      AktBeat := Floor(GetMidBeat(Music.Position - AktSong.GAP / 1000));
      if Music.Position>PlayStopTime then
        end_ := true
      else
        end_ := false;
    end else
    begin
      AktBeat := Floor(GetMidBeat(MidiPos - AktSong.GAP / 1000));
      if MidiPos>MidiStop then
        end_ := true
      else
        end_ := false;
    end;

    if AktBeat <> last then
    begin
      for beat := LastClick+1 to AktBeat do
      begin
        PlayClick := false;
        for line := 0 to Length(Czesci[CP].Czesc) - 1 do
        begin
          for note := 0 to Length(Czesci[CP].Czesc[line].Nuta) - 1 do
          begin
            //line change
            if (Czesci[CP].Czesc[line].Start = beat) and (line <> Czesci[CP].Akt) and
              not end_ and not PlayOneSentence then
            begin
              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 0;
              AktNuta[CP] := 0;
              Czesci[CP].Akt := line;
              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 1;
              EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
              EditorLyric[CP].Selected := AktNuta[CP];
              LineChanged[CP] := true;
            end;

            if (Czesci[CP].Czesc[line].Nuta[note].Start = beat) then
            begin
              if not PlayOneSentence or (line=Czesci[CP].Akt) then
              begin
                LastClick := beat;
                PlayClick := true;
              end;
            end;
          end;
        end;

        if AktSong.isDuet and not PlayOneSentence then
        begin
          for line := 0 to Length(Czesci[(CP+1) mod 2].Czesc) - 1 do
          begin
            for note := 0 to Length(Czesci[(CP+1) mod 2].Czesc[line].Nuta) - 1 do
            begin
              //line change
              if (Czesci[(CP+1) mod 2].Czesc[line].Start = beat) and (line <> Czesci[(CP+1) mod 2].Akt) and not end_ then
              begin
                if(Length(Czesci[(CP+1) mod 2].Czesc[Czesci[(CP+1) mod 2].Akt].Nuta)>0) then
                  Czesci[(CP+1) mod 2].Czesc[Czesci[(CP+1) mod 2].Akt].Nuta[AktNuta[(CP+1) mod 2]].Color := 0;
                AktNuta[(CP+1) mod 2] := 0;
                Czesci[(CP+1) mod 2].Akt := line;
                Czesci[(CP+1) mod 2].Czesc[Czesci[(CP+1) mod 2].Akt].Nuta[AktNuta[(CP+1) mod 2]].Color := 1;
                EditorLyric[(CP+1) mod 2].AddCzesc((CP+1) mod 2, Czesci[(CP+1) mod 2].Akt);
                EditorLyric[(CP+1) mod 2].Selected := AktNuta[(CP+1) mod 2];
                LineChanged[(CP+1) mod 2] := true;
              end;
            end;
          end;
        end;


        // midi music
        if PlaySentenceMidi then
        begin
          // stop the music
          if end_ then
          begin
            MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[MidiLastNote].Ton + 60, 127);
            PlaySentenceMidi := false;
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 0;
            if (Czesci[CP].Akt = lineStart) then
              AktNuta[CP] := noteStart;

            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
            EditorLyric[CP].Selected := AktNuta[CP];
          end;

          // click
          Text[TextDebug].Text := IntToStr(AktBeat);

          if PlayClick then
          begin
            for Pet := 0 to Czesci[CP].Czesc[Czesci[CP].Akt].HighNut do
              if (Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[Pet].Start = beat) then
            begin
              if Pet > 0 then
                MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[Pet-1].Ton + 60, 127);
              MidiOut.PutShort($91, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[Pet].Ton + 60, 127);
              MidiLastNote := Pet;
            end;
          end;
        end; // if PlaySentenceMidi

        // mp3 music
        if PlaySentence then
        begin
          // stop the music
          if end_ then
          begin
            Music.Stop;
            PlaySentence := false;
            PitchRecOn := false;
            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 0;
            if (Czesci[CP].Akt = lineStart) then
              AktNuta[CP] := noteStart;

            Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
            EditorLyric[CP].Selected := AktNuta[CP];
          end;

          if (Click) and (PlaySentence) then
          begin
            Text[TextDebug].Text := IntToStr(AktBeat);
            if PlayClick then
              Music.PlayClick;
          end; // click
        end;

        // move "cursor"
        if (PlaySentence or PlaySentenceMidi) then
        begin
          for line := 0 to Length(Czesci[CP].Czesc) - 1 do
          begin
            for note := 0 to Length(Czesci[CP].Czesc[line].Nuta) - 1 do
            begin
              //note change
              if (Czesci[CP].Czesc[line].Nuta[note].Start = beat) and
                (((note <> AktNuta[CP]) or LineChanged[CP]) and
                (not PlayOneSentence or (line = Czesci[CP].Akt))) then
              begin
                Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 0;
                if not LineChanged[CP] then
                begin
                  AktNuta[CP] := note;
                  Czesci[CP].Akt := line;
                end else
                  LineChanged[CP] := false;

                Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Color := 2;
                EditorLyric[CP].AddCzesc(CP, Czesci[CP].Akt);
                EditorLyric[CP].Selected := AktNuta[CP];
              end;
            end;
          end;

          if AktSong.isDuet and not PlayOneSentence then
          begin
            for line := 0 to Length(Czesci[(CP+1) mod 2].Czesc) - 1 do
            begin
              for note := 0 to Length(Czesci[(CP+1) mod 2].Czesc[line].Nuta) - 1 do
              begin
                //note change
                if (Czesci[(CP+1) mod 2].Czesc[line].Nuta[note].Start = beat) and
                  ((note <> AktNuta[(CP+1) mod 2]) or LineChanged[(CP+1) mod 2]) then
                begin
                  if(Length(Czesci[(CP+1) mod 2].Czesc[Czesci[(CP+1) mod 2].Akt].Nuta)>0) then
                    Czesci[(CP+1) mod 2].Czesc[Czesci[(CP+1) mod 2].Akt].Nuta[AktNuta[(CP+1) mod 2]].Color := 0;
                  if not LineChanged[(CP+1) mod 2] then
                  begin
                    AktNuta[(CP+1) mod 2] := note;
                    Czesci[(CP+1) mod 2].Akt := line;
                  end else
                    LineChanged[(CP+1) mod 2] := false;

                  Czesci[(CP+1) mod 2].Czesc[Czesci[(CP+1) mod 2].Akt].Nuta[AktNuta[(CP+1) mod 2]].Color := 2;
                  EditorLyric[(CP+1) mod 2].AddCzesc((CP+1) mod 2, Czesci[(CP+1) mod 2].Akt);
                  EditorLyric[(CP+1) mod 2].Selected := AktNuta[(CP+1) mod 2];
                end;
              end;
            end;
          end;
        end; //move "cursor"
      end; //for beat
    end; //AktBeat <> last
  end else
  begin
    LineChanged[0]:=false;
    LineChanged[1]:=false;
    PlayVideo := false;
    PlayOneSentence := false;
  end;

  // mp3 music
  if PlayOneNote then
  begin
    // stop the music
    if (Music.Position > PlayStopTime) then
    begin
      Music.Stop;
      PlayOneNote := false;
    end;

    // click
    if (Click) and (PlaySentence) then
    begin
      AktBeat := Floor(GetMidBeat(Music.Position - AktSong.GAP / 1000));
      Text[TextDebug].Text := IntToStr(AktBeat);
      if AktBeat <> LastClick then
      begin
        for beat := LastClick+1 to AktBeat do
        begin
          for Pet := 0 to Czesci[CP].Czesc[Czesci[CP].Akt].HighNut do
          begin
            if (Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[Pet].Start = beat) then
            begin
              Music.PlayClick;
              LastClick := beat;
            end;
          end;
        end; //for beat
      end;
    end; // click
  end; // if PlayOneNote

  // midi music
  if PlayOneNoteMidi then
  begin
    MidiPos := USTime.GetTime - MidiTime + MidiStart;
    // stop the music
    if (MidiPos > MidiStop) then
    begin
      MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[MidiLastNote].Ton + 60, 127);
      PlayOneNoteMidi := false;
    end;

    // click
    AktBeat := Floor(GetMidBeat(MidiPos - AktSong.GAP / 1000));
    Text[TextDebug].Text := IntToStr(AktBeat);

    if AktBeat <> LastClick then
    begin
      for beat := LastClick+1 to AktBeat do
      begin
        for Pet := 0 to Czesci[CP].Czesc[Czesci[CP].Akt].HighNut do
        begin
          if (Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[Pet].Start = beat) then
          begin
            LastClick := beat;
            if Pet > 0 then
              MidiOut.PutShort($81, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[Pet-1].Ton + 60, 127);
            MidiOut.PutShort($91, Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[Pet].Ton + 60, 127);
            MidiLastNote := Pet;
          end;
        end;
      end; //for beat
    end;
  end; // if PlayOneNoteMidi

  Text[TextSentence].Text := 'Line: ' + IntToStr(Czesci[CP].Akt + 1) + '/' + IntToStr(Czesci[CP].Ilosc);
  Text[TextNote].Text := 'Note: ' + IntToStr(AktNuta[CP] + 1) + '/' + IntToStr(Czesci[CP].Czesc[Czesci[CP].Akt].IlNut);

  // Song info
  if not BPMEditMode then
    Text[TextBPM].Text := FloatToStr(AktSong.BPM[0].BPM / 4);

  Text[TextGAP].Text := FloatToStr(AktSong.GAP);
  Text[TextVideoGap].Text := FloatToStr(AktSong.VideoGap);

  //Error reading Variables when no Song is loaded
  if not Error and (Length(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta)>AktNuta[CP]) then
  begin
    // Note info
    Text[TextNStart].Text :=    IntToStr(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Start);
    Text[TextNDlugosc].Text :=  IntToStr(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Dlugosc);
    Text[TextNTon].Text :=      IntToStr(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Ton) +
      ' ( ' + GetNoteName(Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Ton) + ' ) ' +
      IntToStr(Czesci[CP].Czesc[Czesci[CP].Akt].BaseNote);
    Text[TextNText].Text :=              Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Tekst;

    //F and G and Medley Mod:
    if Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].FreeStyle then
      Text[TextNTon].Text := Text[TextNTon].Text + ' *F*'
    else if Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].Wartosc = 2 then
      Text[TextNTon].Text := Text[TextNTon].Text + ' *G*';

    if MedleyNotes.isStart and (Czesci[CP].Akt = MedleyNotes.start.line)
      and (AktNuta[0] = MedleyNotes.start.note) then
      Text[TextNTon].Text := Text[TextNTon].Text + ' MedleyStart';
    if MedleyNotes.isEnd and (Czesci[CP].Akt = MedleyNotes.end_.line) and
      (AktNuta[0] = MedleyNotes.end_.note) then
      Text[TextNTon].Text := Text[TextNTon].Text + ' MedleyEnd';

    //preview mod
    if Czesci[CP].Czesc[Czesci[CP].Akt].Nuta[AktNuta[CP]].IsStartPreview then
      Text[TextNTon].Text := Text[TextNTon].Text + ' [PreviewStart]';
  end;

  // Text Edit Mode
  if TextEditMode then
    Text[TextNText].Text := Text[TextNText].Text + '|';

  // draw static menu
  inherited Draw;

  // draw notes
  if not AktSong.isDuet then
    SingDrawNoteLines(5, offset[0]+90, 795, 15, 1)
  else
  begin
    SingDrawNoteLines(5, offset[0]+45, 795, 15, 1);
    SingDrawNoteLines(5, offset[1]-140, 795, 15, 1);
  end;

  //Error Drawing when no Song is loaded
  if not Error then
  begin
    if not AktSong.isDuet then
    begin
      SingDrawBeatDelimeters(5, offset[0]+90, 795, 0);
      EditDrawCzesc(5, offset[0]+195, 795, 0, 15);
    end else
    begin
      SingDrawBeatDelimeters(5, offset[0]+45, 795, 0);
      EditDrawCzesc(5, offset[0]+150, 795, 0, 15);
      SingDrawBeatDelimeters(5, offset[1]-140, 795, 1);
      EditDrawCzesc(5, offset[1]-35, 795, 1, 15);
    end;
  end;

  // draw text
  if not AktSong.isDuet then
  begin
    EditorLyric[0].Draw;
    DrawInfoBar(0, 5, offset[0]+250, 790, 15);
  end else
  begin
    EditorLyric[0].Draw;
    DrawInfoBar(0, 5, offset[0]+185, 790, 15);

    EditorLyric[1].Draw;
    DrawInfoBar(1, 5, offset[1]-160, 790, 15);
  end;

  if (CP=1) then
  begin
    glEnable(GL_BLEND);
    glColor4f(0, 0, 0, 0.3);
    //notes
    glbegin(gl_quads);
      glVertex2f(5,   offset[0]+45);
      glVertex2f(5,   offset[0]+180);
      glVertex2f(795, offset[0]+180);
      glVertex2f(795, offset[0]+45);
    glEnd;
    //lyric
    glbegin(gl_quads);
      glVertex2f(5,   offset[0]+5);
      glVertex2f(5,   offset[0]+35);
      glVertex2f(795, offset[0]+35);
      glVertex2f(795, offset[0]+5);
    glEnd;
    glDisable(GL_BLEND);
  end else if AktSong.isDuet then
  begin
    glEnable(GL_BLEND);
    glColor4f(0, 0, 0, 0.3);
    //notes
    glbegin(gl_quads);
      glVertex2f(5,   offset[1]-140);
      glVertex2f(5,   offset[1]-5);
      glVertex2f(795, offset[1]-5);
      glVertex2f(795, offset[1]-140);
    glEnd;
    //lyric
    glbegin(gl_quads);
      glVertex2f(5,   offset[1]+35);
      glVertex2f(5,   offset[1]+5);
      glVertex2f(795, offset[1]+5);
      glVertex2f(795, offset[1]+35);
    glEnd;
    glDisable(GL_BLEND);
  end;

  if UVideo.VideoOpened and PlayVideo then
  begin
    Czas.Teraz := Czas.Teraz + TimeSkip;
    try
      acGetFrame(Czas.Teraz);

      if VidVis=windowed then
      begin
        Window.Left := 570;
        Window.Right := 790;
        Window.Upper := 10;
        Window.Lower := 145;
        Window.Reflection := false;
        Window.TargetAspect := acoCrop;
        Window.windowed := true;

        SetAspectCorrection(acoCrop);
        Blend := (PlayTime-0.2);
        if Blend<0 then
          Blend := 0
        else if Blend>1 then
          Blend := 1;

        acDrawGLi(ScreenAct, Window, Blend, true);
      end else if VidVis=full then
      begin
        acDrawGL(ScreenAct, true);
      end;

      if (Czas.Teraz>=Czas.Razem) then
      begin
        acClose;
        VidVis := none;
      end;
    except
      //If an Error occurs drawing: prevent Video from being Drawn again and Close Video
      log.LogError('Error drawing Video, Video has been disabled for this Song/Session.');
      Log.LogError('Corrupted File: ' + AktSong.Video);
      try
        acClose;
        VidVis := none;
      except

      end;
    end;
  end else
  begin
    DrawPitch(400, 75, 390, 15, AktBeat);
    StartVideoPreview;
  end;
end;

procedure TScreenEditSub.DrawStatics;
var
  x, y, w, h: Integer;

  procedure DrawBorder(x, y, w, h: real);
  begin
    glColor4f(0, 0, 0, 1);
    glLineWidth(2);
    glBegin(GL_LINE_LOOP);
      glVertex2f(x-1, y-1);
      glVertex2f(x+w+1, y-1);
      glVertex2f(x+w+1, y+h+1);
      glVertex2f(x-1, y+h+1);
    glEnd;
  end;
begin
  glDisable(GL_BLEND);

  //bg
  x := 0;
  y := 0;
  w := 800;
  h := 600;
  glColor4f(0.3, 0.5, 0.6, 1);
  glbegin(gl_quads);
   glVertex2f(x, y);
   glVertex2f(x, y+h);
   glVertex2f(x+w, y+h);
   glVertex2f(x+w, y);
  glEnd;

  // line bg
  if (CP=0) then
    glColor4f(cRB, cGB, cBB, 1)
  else
    glColor4f(cRR, cGR, cBR, 1);
  x := 650;
  y := 570;
  w := 145;
  h := 25;
  glbegin(gl_quads);
   glVertex2f(x, y);
   glVertex2f(x, y+h);
   glVertex2f(x+w, y+h);
   glVertex2f(x+w, y);
  glEnd;

  DrawBorder(x, y, w, h);



  // note bg
  if (CP=0) then
    glColor4f(cRB, cGB, cBB, 1)
  else
    glColor4f(cRR, cGR, cBR, 1);
  x := 495;
  y := 570;
  w := 145;
  h := 25;
  glbegin(gl_quads);
   glVertex2f(x, y);
   glVertex2f(x, y+h);
   glVertex2f(x+w, y+h);
   glVertex2f(x+w, y);
  glEnd;
  DrawBorder(x, y, w, h);

  // some borders:

  //info box
  x := 5;
  y := 5;
  w := 790;
  if AktSong.isDuet then
    h := 145
  else
    h := 150;

  glColor4f(0.95, 0.95, 0.95, 1);
  glbegin(gl_quads);
   glVertex2f(x, y);
   glVertex2f(x, y+h);
   glVertex2f(x+w, y+h);
   glVertex2f(x+w, y);
  glEnd;
  DrawBorder(x, y, w, h);

  //notes singer 1
  x := 5;
  if AktSong.isDuet then
    y := 200
  else
    y := 245;

  w := 790;
  h := 135;
  glColor4f(cRB, cGB, cBB, 1);
  glbegin(gl_quads);
   glVertex2f(x, y);
   glVertex2f(x, y+h);
   glVertex2f(x+w, y+h);
   glVertex2f(x+w, y);
  glEnd;
  DrawBorder(x, y, w, h);

  //notes singer 2
  if AktSong.isDuet then
  begin
    x := 5;
    y := 385;
    w := 790;
    h := 135;
    glColor4f(cRR, cGR, cBR, 1);
    glbegin(gl_quads);
      glVertex2f(x, y);
      glVertex2f(x, y+h);
      glVertex2f(x+w, y+h);
      glVertex2f(x+w, y);
    glEnd;
    DrawBorder(x, y, w, h);
  end;

  //lyric singer 1
  x := 5;
  if AktSong.isDuet then
    y := 160
  else
    y := 460;

  w := 790;
  h := 30;
  glColor4f(cRB, cGB, cBB, 1);
  glbegin(gl_quads);
   glVertex2f(x, y);
   glVertex2f(x, y+h);
   glVertex2f(x+w, y+h);
   glVertex2f(x+w, y);
  glEnd;
  DrawBorder(x, y, w, h);

  //lyric singer 2
  if AktSong.isDuet then
  begin
    x := 5;
    y := 530;
    w := 790;
    h := 30;
    glColor4f(cRR, cGR, cBR, 1);
      glbegin(gl_quads);
      glVertex2f(x, y);
      glVertex2f(x, y+h);
      glVertex2f(x+w, y+h);
      glVertex2f(x+w, y);
    glEnd;
    DrawBorder(x, y, w, h);
  end;

  glLineWidth(1);
end;

procedure TScreenEditSub.DrawInfoBar(P, x, y, w, h: integer);
var
  start, end_:  integer;
  start2:       integer;
  ww:           integer;

  pos:          real;
  br:           real;

  line:         integer;
  numLines:     integer;

  function FindStart(): integer;
  var
    I:      integer;
    start:  integer;
  begin
    start := High(integer);

    for I := 0 to Length(Czesci[0].Czesc) - 1 do
    begin
      if (Length(Czesci[0].Czesc[I].Nuta)>0) then
      begin
        if(start > Czesci[0].Czesc[I].Nuta[0].Start) then
          start := Czesci[0].Czesc[I].Nuta[0].Start;
      end;
    end;

    Result := start;

    if not AktSong.isDuet then
      Exit;

    for I := 0 to Length(Czesci[1].Czesc) - 1 do
    begin
      if (Length(Czesci[1].Czesc[I].Nuta)>0) then
      begin
        if(start > Czesci[1].Czesc[I].Nuta[0].Start) then
          start := Czesci[1].Czesc[I].Nuta[0].Start;
      end;
    end;

    Result := start;
  end;

  function FindEnd(): integer;
  var
    I:      integer;
    end_:   integer;
    h:      integer;
  begin
    end_ := Low(integer);

    for I := 0 to Length(Czesci[0].Czesc) - 1 do
    begin
      if (Length(Czesci[0].Czesc[I].Nuta)>0) then
      begin
        h := Length(Czesci[0].Czesc[I].Nuta)-1;
        if(end_ < Czesci[0].Czesc[I].Nuta[h].Start + Czesci[0].Czesc[I].Nuta[h].Dlugosc) then
          end_ := Czesci[0].Czesc[I].Nuta[h].Start + Czesci[0].Czesc[I].Nuta[h].Dlugosc;
      end;
    end;

    Result := end_;

    if not AktSong.isDuet then
      Exit;

    for I := 0 to Length(Czesci[1].Czesc) - 1 do
    begin
      if (Length(Czesci[1].Czesc[I].Nuta)>0) then
      begin
        h := Length(Czesci[1].Czesc[I].Nuta)-1;
        if(end_ < Czesci[1].Czesc[I].Nuta[h].Start + Czesci[1].Czesc[I].Nuta[h].Dlugosc) then
          end_ := Czesci[1].Czesc[I].Nuta[h].Start + Czesci[1].Czesc[I].Nuta[h].Dlugosc;
      end;
    end;

    Result := end_;
  end;
begin
  numLines := Length(Czesci[P].Czesc);

  if(numLines=0) then
    Exit;

  start2 := FindStart;
  end_ := FindEnd;
  ww := end_ - start2;

  glColor4f(0, 0, 0, 1);
  glDisable(GL_BLEND);
  glLineWidth(2);
  glBegin(GL_LINE_LOOP);
    glVertex2f(x-1, y-1);
    glVertex2f(x+w+1, y-1);
    glVertex2f(x+w+1, y+h+1);
    glVertex2f(x-1, y+h+1);
  glEnd;

  if (P=0) then
    glColor4f(cRB, cGB, cBB, 1)
  else
    glColor4f(cRR, cGR, cBR, 1);
  glbegin(gl_quads);
   glVertex2f(x, y);
   glVertex2f(x, y+h);
   glVertex2f(x+w, y+h);
   glVertex2f(x+w, y);
  glEnd;


  for line := 0 to numLines - 1 do
  begin
    if (line = Czesci[P].Akt) and not (PlaySentence or PlaySentenceMidi) then
      glColor4f(0.4, 0.4, 0, 1)
    else
      glColor4f(1, 0.6, 0, 1);

    if (Length(Czesci[P].Czesc[line].Nuta)>0) then
    begin
      start := Czesci[P].Czesc[line].Nuta[0].Start;
      end_ := Czesci[P].Czesc[line].Nuta[Czesci[P].Czesc[line].HighNut].Start+
        Czesci[P].Czesc[line].Nuta[Czesci[P].Czesc[line].HighNut].Dlugosc;

      pos := (start-start2)/ww*w;
      br := (end_-start)/ww*w;

      glbegin(gl_quads);
        glVertex2f(x+pos, y);
        glVertex2f(x+pos, y+h);
        glVertex2f(x+pos+br, y+h);
        glVertex2f(x+pos+br, y);
      glEnd;
    end;
  end;


  if(PlaySentence or PlaySentenceMidi) then
  begin
    glColor4f(1, 0, 0, 1);
    pos := (AktBeat-start2)/ww*w;
    br := 1;

    glbegin(gl_quads);
      glVertex2f(x+pos, y);
      glVertex2f(x+pos, y+h);
      glVertex2f(x+pos+br, y+h);
      glVertex2f(x+pos+br, y);
    glEnd;

    if (Length(Czesci[P].Czesc[Czesci[P].Akt].Nuta)>0) then
    begin
      start := Czesci[P].Czesc[Czesci[P].Akt].Nuta[0].Start;
      end_ := Czesci[P].Czesc[Czesci[P].Akt].Nuta[Czesci[P].Czesc[Czesci[P].Akt].HighNut].Start+
        Czesci[P].Czesc[Czesci[P].Akt].Nuta[Czesci[P].Czesc[Czesci[P].Akt].HighNut].Dlugosc;

      pos := (start-start2)/ww*w;
      br := (end_-start)/ww*w;

      glColor4f(0, 0, 0, 0.5);

      glEnable(GL_BLEND);
      glbegin(gl_quads);
        glVertex2f(x+pos, y);
        glVertex2f(x+pos, y+h);
        glVertex2f(x+pos+br, y+h);
        glVertex2f(x+pos+br, y);
      glEnd;
      glDisable(GL_BLEND);
    end;
  end else
  begin
    glColor4f(1, 0, 0, 1);
    if (Length(Czesci[P].Czesc[Czesci[P].Akt].Nuta)>0) then
    begin
      pos := (Czesci[P].Czesc[Czesci[P].Akt].Nuta[AktNuta[P]].Start-start2)/ww*w;
      br := Czesci[P].Czesc[Czesci[P].Akt].Nuta[AktNuta[P]].Dlugosc/ww*w;
      if (br<1) then
        br := 1;

      glbegin(gl_quads);
        glVertex2f(x+pos, y);
        glVertex2f(x+pos, y+h);
        glVertex2f(x+pos+br, y+h);
        glVertex2f(x+pos+br, y);
      glEnd;
    end;
  end;
end;

procedure TScreenEditSub.onHide;
begin
  MidiOut.Close;
  MidiOut.Free;
  Music.CaptureStop;
end;

function TScreenEditSub.GetNoteName(Note: Integer): String;
var N1, N2: Integer;
begin
  if (Note > 0) then
  begin
    N1 := Note mod 12;
    N2 := Note div 12;
  end
  else
  begin
    N1 := (Note + (-Trunc(Note/12)+1)*12) mod 12;
    N2 := -1;
  end;



  case N1 of
    0: Result := 'c';
    1: Result := 'c#';
    2: Result := 'd';
    3: Result := 'd#';
    4: Result := 'e';
    5: Result := 'f';
    6: Result := 'f#';
    7: Result := 'g';
    8: Result := 'g#';
    9: Result := 'a';
    10: Result := 'a#';
    11: Result := 'b';
  end;

  case N2 of
    0: Result := UpperCase(Result); //Normal Uppercase Note, 1: Normal lowercase Note
    2: Result := Result + '''';     //One Striped
    3: Result := Result + '''''';   //Two Striped
    4: Result := Result + ''''''''; //etc.
    5: Result := Result + '''''''''';
    6: Result := Result + '''''''''''';
    7: Result := Result + '''''''''''''';
  end;
end;

function TScreenEditSub.GetMedleyLength: real;
begin
  if MedleyNotes.isStart and MedleyNotes.isEnd then
  begin
    Result := GetTimeFromBeat(
      Czesci[0].Czesc[MedleyNotes.end_.line].Nuta[MedleyNotes.end_.note].Start +
      Czesci[0].Czesc[MedleyNotes.end_.line].Nuta[MedleyNotes.end_.note].Dlugosc) -
      GetTimeFromBeat(Czesci[0].Czesc[MedleyNotes.start.line].Nuta[MedleyNotes.start.note].Start);
  end else
    Result := 0;
end;

end.