Source file b_layout.ml
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
open Tsdl
open B_utils
module Avar = B_avar
module Box = B_box
module Chain = B_chain
module Draw = B_draw
module Label = B_label
module Mouse = B_mouse
module Selection = B_selection
module Slider = B_slider
module Style = B_style
module Sync = B_sync
module Theme = B_theme
module Time = B_time
module Trigger = B_trigger
module Tvar = B_tvar
module Var = B_var
module Widget = B_widget
type background =
| Style of Style.t
| Box of Box.t
let color_bg color =
Style (Style.(of_bg (color_bg color)))
let opaque_bg color = color_bg Draw.(opaque color)
let theme_bg = opaque_bg Draw.bg_color
let style_bg s =
Style s
let box_bg b =
Box b
type adjust =
| Fit
| Width
| Height
| Nothing
type transform = {
angle : float Avar.t;
center : (Sdl.point option) Avar.t;
flip : Sdl.flip Avar.t;
alpha : float Avar.t
}
type geometry = {
x : int Avar.t;
y : int Avar.t;
w : int Avar.t;
h : int Avar.t;
voffset : (int Avar.t) Var.t;
transform: transform;
}
type current_geom = {
x : int;
y : int;
w : int;
h : int;
voffset : int
}
let to_draw_geom (g : current_geom) =
{ Draw.x = g.x; Draw.y = g.y; Draw.w = g.w; Draw.h = g.h;
Draw.voffset = g.voffset }
type room_content =
| Rooms of room list
| Resident of Widget.t
and room = {
id : int;
name : string option;
lock : Mutex.t;
mutable thread_id : int;
adjust : adjust;
mutable resize : ((int * int) -> unit);
mutable show : bool;
mutable hidden : bool;
mutable geometry : geometry;
mutable current_geom : current_geom;
mutable clip : bool;
mutable background : background option;
mutable shadow : Style.shadow option;
mask : Sdl.surface option;
mutable content : room_content;
mutable layer : Draw.layer;
mutable canvas : Draw.canvas option;
mutable house: room option;
mutable mouse_focus : bool;
mutable keyboard_focus : bool option;
mutable draggable : bool;
mutable removed : bool;
mutable disabled : bool;
}
type t = room
exception Fatal_error of (t * string)
exception Found of t
let not_specified = Sdl.Window.pos_undefined
let no_clip = ref false
let draw_boxes = Widget.draw_boxes
let equal r1 r2 = r1.id = r2.id
let (==) = equal
let sprint_id r =
Printf.sprintf "#%u%s" r.id (match r.name with
| None -> ""
| Some s -> Printf.sprintf " (%s)" s)
module Hash = struct
type t = room
let equal = equal
let hash room = room.id
end
module WHash = Weak.Make(Hash)
let rooms_wtable = WHash.create 50
let cemetery = ref []
let send_to_cemetery room =
cemetery := room.id :: !cemetery
let rec remove_wtable room =
if WHash.mem rooms_wtable room
then begin
printd debug_memory "Removing room %s from Wtable" (sprint_id room);
WHash.remove rooms_wtable room;
if WHash.mem rooms_wtable room
then begin
printd debug_error
"Several instances of room %s are registered in the weak hash table."
(sprint_id room);
remove_wtable room;
end;
send_to_cemetery room;
end
let clear_wtable () = WHash.clear rooms_wtable
let delete_background room =
printd debug_memory "Delete background for room %s" (sprint_id room);
do_option room.background
(fun b ->
let () =
room.background <-
if !debug then Some (opaque_bg Draw.red) else None in
match b with
| Style s -> Style.unload s
| Box b -> Box.unload b)
let unload_background room =
do_option room.background (function
| Box b -> Box.unload b
| Style s -> Style.unload s)
let rec iter f room =
f room;
match room.content with
| Resident _ -> ()
| Rooms list -> List.iter (iter f) list
let rec iter_widgets f room =
match room.content with
| Resident w -> f w
| Rooms list -> List.iter (iter_widgets f) list
let map_widgets f room =
let list = ref [] in
iter_widgets (fun w -> list := (f w) :: !list) room;
!list
let iter_rooms f house =
match house.content with
| Resident _ -> printd (debug_error + debug_board)
"Layout %s has no rooms: cannot iter." (sprint_id house)
| Rooms list -> List.iter f list
let get_rooms layout =
match layout.content with
| Resident _ ->
printd debug_error
"[Layout.get_rooms] This layout %s is a leaf, not a node: it does not \
contain a list of rooms" (sprint_id layout);
raise Not_found
| Rooms list -> list
let neighbours room =
match room.house with
| None ->
printd debug_error
"Cannot get siblings of room %s because it does not belong to any \
house." (sprint_id room);
[]
| Some house -> get_rooms house
let siblings = neighbours
let rec belongs_to ~parent room =
match room.house with
| None -> false
| Some h -> equal h parent || belongs_to ~parent h
let widget layout =
match layout.content with
| Rooms _ ->
printd debug_error
"This room %s is a node, not a leaf: \
it does not contain a resident widget" (sprint_id layout);
raise Not_found
| Resident w -> w
let get_resident = widget
let rec first_show_widget layout =
if layout.show
then match layout.content with
| Resident w ->
(printd debug_board "first_show_widget selects %u" (Widget.id w); w)
| Rooms rooms ->
let rec loop = function
| [] -> raise Not_found
| r::rest -> try first_show_widget r with Not_found -> loop rest in
loop rooms
else raise Not_found
let unload_widget_textures room =
unload_background room;
iter_widgets Widget.unload_texture room
let unload_texture room =
match room.content with
| Resident w -> Widget.unload_texture w
| _ -> ()
let iter_unload_textures room =
let f r =
unload_texture r;
unload_background r in
iter f room
let delete_backgrounds room =
iter delete_background room
let delete_textures room =
iter_unload_textures room;
delete_backgrounds room
let finalize room =
printd debug_memory "Finalize room %s" (sprint_id room);
delete_background room;
unload_texture room
let get_texts room =
map_widgets Widget.get_text room
|> List.filter (fun s -> s <> "")
let get_text room =
get_resident room
|> Widget.get_text
let set_text room text =
Widget.set_text (get_resident room) text
let keyboard_focus_before_tab : t option ref = ref None
let fresh_id = fresh_int ()
(** make geometry *)
let geometry ?(x=0) ?(y=0) ?(w=0) ?(h=0) ?(voffset=0) ?transform () : geometry =
{ x = Avar.var x;
y = Avar.var y;
w = Avar.var w;
h = Avar.var h;
voffset = Var.create (Avar.var voffset);
transform = default transform
{ angle = Avar.var 0.;
center = Avar.var None;
flip = Avar.var Sdl.Flip.none;
alpha = Avar.var 1.}
}
(** list of all integer dynamical variables *)
let get_int_avars room =
let g = room.geometry in [g.x; g.y; g.w; g.h; Var.get g.voffset]
let current_geom ?(x=0) ?(y=0) ?(w=0) ?(h=0) ?(voffset=0) () : current_geom =
{ x; y; w; h; voffset}
let to_current_geom (g : geometry) : current_geom =
{ x = Avar.get g.x;
y = Avar.get g.y;
w = Avar.get g.w;
h = Avar.get g.h;
voffset = Avar.get (Var.get g.voffset) }
let get_layer l =
l.layer
let base_layer = function
| [] -> Draw.get_current_layer ()
| room::rooms ->
List.fold_left Chain.min (get_layer room) (List.map get_layer rooms)
let create_unsafe
?name
?(set_house = true) ?(adjust = Fit)
?(resize = fun _ -> ())
?layer
?mask ?background ?shadow ?house ?keyboard_focus ?(mouse_focus=false)
?(show = true) ?(clip = false) ?(draggable = false) ?canvas
geometry content =
let id = fresh_id () in
let layer = match layer with
| Some layer -> layer
| None -> match content with
| Rooms rooms -> base_layer rooms
| Resident _ -> Draw.get_current_layer () in
let room =
{
id;
name;
lock = Mutex.create ();
thread_id = Thread.(id (self ()));
show;
hidden = true;
adjust;
resize;
geometry;
current_geom = to_current_geom geometry;
clip;
mask;
background;
shadow;
content;
layer;
house;
keyboard_focus;
mouse_focus;
canvas;
draggable;
removed = false;
disabled = false
} in
if !debug
then if WHash.mem rooms_wtable room
then (printd debug_error "A room with same id was already in the table !";
remove_wtable room);
WHash.add rooms_wtable room;
let () = match content with
| Resident w -> w.Widget.room_id <- Some id
| Rooms list -> if set_house
then List.iter (fun r -> r.house <- Some room) list in
Gc.finalise finalize room;
printd debug_board "Layout %s created." (sprint_id room);
room
let create = create_unsafe ~set_house:true
let dummy_room = create ~name:"dummy" (geometry ()) (Rooms [])
let of_id_unsafe id =
match WHash.find_opt rooms_wtable {dummy_room with id} with
| None ->
printd debug_warning "Cannot find room with id=%d" id;
None
| o -> o
let is_detached room =
room.house = None && room.canvas = None
let is_removed room =
room.removed
let remove_one room =
printd debug_board "Removing layout %s from focus" (sprint_id room);
room.removed <- true
let remove ?(children = false) room =
if children
then iter remove_one room
else remove_one room;
Trigger.push_remove_focus (room.id)
let of_id_opt ?not_found id : room option =
match WHash.find_opt rooms_wtable {dummy_room with id} with
| None ->
printd debug_error "Cannot find room with id=%d" id;
do_option not_found run;
None
| Some r as o ->
if is_detached r
then (printd debug_error "Trying to access the detached room #%d" id; None)
else o
let containing_widget w =
check_option w.Widget.room_id of_id_opt
let of_wid wid =
let w = Widget.of_id wid in
containing_widget w
let check_cemetery () =
let check id = match of_id_unsafe id with
| Some r ->
printd debug_memory
"Dead room %s seems to be living. Beware of zombies." (sprint_id r);
false
| None ->
printd debug_memory
"Dead room #%u was correctly burried by the GC. RIP." id;
true
in
let rec loop list newlist empty =
match list with
| [] -> empty, newlist
| id::rest ->
if check id
then loop rest newlist empty
else loop rest (id :: newlist) false in
let empty, newlist = loop !cemetery [] true in
cemetery := newlist;
empty
let lock l =
if Mutex.try_lock l.lock
then begin
let id = Thread.(id (self ())) in
printd debug_thread "Locking room %s for thread #%i." (sprint_id l) id;
l.thread_id <- id
end
else
if Thread.(id (self ())) <> l.thread_id
then begin
let id = Thread.(id (self ())) in
printd debug_thread "Waiting for thread #%i to remove lock for room %s"
id (sprint_id l);
Mutex.lock l.lock;
l.thread_id <- id
end
else begin
printd (debug_thread + debug_error + debug_user)
"!! Layout %s was locked, but by the same thread: we \
continue, but this should be corrected."
(sprint_id l)
end
let unlock l =
printd debug_thread "Unlocking layout %s" (sprint_id l);
if Mutex.try_lock l.lock
then printd debug_thread " (but layout %s was already unlocked)." (sprint_id l);
Mutex.unlock l.lock
let renderer t = match t.canvas with
| Some c -> c.Draw.renderer
| _ -> failwith "Cannot get renderer because no canvas was defined"
let window_opt t =
map_option t.canvas (fun c -> c.Draw.window)
let window t =
match window_opt t with
| Some w -> w
| _ -> begin
printd debug_error
"Cannot get window for layout %s because no canvas was defined"
(sprint_id t);
raise Not_found
end
let rec top_house layout =
match layout.house with
| None -> layout
| Some r -> top_house r
let guess_top () =
try WHash.iter (fun r ->
if not (is_detached r) then raise (Found r)) rooms_wtable;
None with
| Found r -> Some (top_house r)
let is_top layout =
layout.house = None && layout.canvas <> None
let get_house layout =
layout.house
let get_content layout =
layout.content
let get_canvas l =
match l.canvas with
| Some c -> c
| None ->
raise (Fatal_error
(l, Printf.sprintf "The room #%d is not associated with any canvas"
l.id))
let same_layer l1 l2 =
Chain.(get_layer l1 == get_layer l2)
let same_stack l1 l2 =
Chain.(same_stack (get_layer l1) (get_layer l2))
let get_background l =
l.background
let compute_background room =
do_option room.background (
fun bg ->
let g = room.current_geom in
Sdl.log "COMPUTE BG w=%u h=%u" g.w g.h;
let box = match bg with
| Style style ->
let b = Box.(create ~width:g.w ~height:g.h ~style ()) in
room.background <- (Some (Box b));
b
| Box b -> Box.unload b; b in
ignore (Box.display (get_canvas room) (get_layer room) box
(Draw.scale_geom (to_draw_geom g))))
let set_background l b =
unload_background l;
l.background <- b
let set_shadow l s =
l.shadow <- s
(** get size of layout *)
let get_size l =
l.current_geom.w, l.current_geom.h
let get_physical_size l =
get_size l |> Draw.scale_size
(** get width of layout *)
let width l =
l.current_geom.w
(** get height *)
let height l =
l.current_geom.h
let resize room =
do_option (get_house room) (fun house ->
room.resize (get_size house))
let disable_resize room =
printd debug_board "Disabling resize fn of %s." (sprint_id room);
room.resize <- (fun _ -> ())
let on_resize room f =
let r = room.resize in
room.resize <- (fun house_size -> r house_size; f ())
let fix_content house =
iter_rooms disable_resize house
let resize_content house =
let s = get_size house in
match house.content with
| Rooms list -> List.iter (fun room -> room.resize s) list
| Resident w -> Widget.resize w s
let adjust_window_size l =
if not (is_top l)
then printd debug_error
"[adjust_window_size] should only be called on a top house, what %s is not."
(sprint_id l)
else if l.canvas <> None
then let w,h = get_physical_size l in
let win = window l in
if (w,h) <> Draw.get_window_size win
then Draw.set_window_size win ~w ~h
else printd debug_graphics
"Window for layout %s already has the required size."
(sprint_id l)
let get_voffset l =
Avar.get (Var.get l.geometry.voffset)
let xpos l =
l.current_geom.x
let ypos l =
l.current_geom.y
let x_origin l = match l.house with
| None -> 0
| Some h -> xpos h
let y_origin l = match l.house with
| None -> 0
| Some h -> ypos h
let pos_from house room =
xpos room - xpos house, ypos room - ypos house
(** get current x value. *)
let getx l =
Avar.get l.geometry.x
let get_oldx l =
Avar.old l.geometry.x
(** get current y value *)
let gety l =
Avar.get l.geometry.y
let get_oldy l =
Avar.old l.geometry.y
let update_current_geom l =
match l.house with
| None ->
printd (debug_error + debug_board)
"Cannot [update_current_geom] because layout %s has no house." (sprint_id l)
| Some house ->
printd debug_custom "[update_current_geom] for room %s" (sprint_id l);
let rec loop x0 y0 v0 r =
let x, y, voffset = x0 + getx r, y0 + gety r + v0, get_voffset r in
r.current_geom <- { r.current_geom with x; y; voffset};
match r.content with
| Resident _ -> ()
| Rooms list -> List.iter (loop x y voffset) list in
let x0, y0, v0 = xpos house, ypos house, get_voffset house in
loop x0 y0 v0 l
let setx ?(keep_resize = false) l x =
let x0 = getx l in
l.current_geom <- { l.current_geom with x = l.current_geom.x + x - x0 };
Avar.set l.geometry.x x;
if not keep_resize then disable_resize l
let sety ?(keep_resize = false) l y =
let y0 = get_oldy l in
l.current_geom <- { l.current_geom with y = l.current_geom.y + y - y0 };
Avar.set l.geometry.y y;
if not keep_resize then disable_resize l
let set_voffset l vo =
Avar.set (Var.get l.geometry.voffset) vo;
l.current_geom <- { l.current_geom with voffset = vo }
let shift_voffset l dv =
Var.update l.geometry.voffset (fun av ->
if Avar.finished av
then begin
let vo = Avar.get av + dv in
Avar.set av vo;
l.current_geom <- { l.current_geom with voffset = vo };
av
end
else Avar.apply (fun y -> y + dv) av)
let set_size ?(keep_resize = false) ?(check_window = true)
?(update_bg = false) ?w ?h l =
if w = None && h = None then ()
else let () = match w,h with
| Some w, Some h ->
l.current_geom <- { l.current_geom with w; h };
Avar.set l.geometry.h h;
Avar.set l.geometry.w w
| Some w, None ->
l.current_geom <- { l.current_geom with w };
Avar.set l.geometry.w w
| None, Some h ->
l.current_geom <- { l.current_geom with h };
Avar.set l.geometry.h h
| None, None -> failwith "set_size: case already treated" in
if update_bg && l.canvas <> None then compute_background l;
if not keep_resize then disable_resize l;
if check_window && is_top l then adjust_window_size l;
resize_content l
module Resize = struct
let keep_resize = true
let check_window = false
type strategy =
| Disable
| Linear
| Default
let set_height ?update_bg l h =
set_size ~keep_resize ~check_window ?update_bg ~h l
let set_width ?update_bg l w =
set_size ~keep_resize ~check_window ?update_bg ~w l
let set_size = set_size ~keep_resize ~check_window
let setx l x = setx ~keep_resize l x
let sety l y = sety ~keep_resize l y
type position =
| Pos of (int * int)
| Left_of of room
| Right_of of room
| Below of room
| Above of room
| Top_margin
| Bottom_margin
| Left_margin
| Right_margin
let set_pos_fn ?(sep = Theme.room_margin) room pos =
let top_m = if pos = Top_margin then gety room else 0 in
let bottom_m = if pos = Bottom_margin
then match get_house room with
| Some r -> height r - gety room - height room
| None -> Theme.room_margin
else 0 in
let left_m = if pos = Left_margin then getx room else 0 in
let right_m = if pos = Right_margin
then match get_house room with
| Some r -> width r - getx room - width room
| None -> Theme.room_margin
else 0 in
(fun (w, h) ->
match pos with
| Pos (x, y) -> setx room x; sety room y
| Left_of r -> setx room (getx r - sep - width room)
| Right_of r -> setx room (getx r + sep + width r)
| Below r -> sety room (gety r + height r + sep)
| Above r -> sety room (gety r - height room - sep)
| Top_margin -> sety room top_m
| Bottom_margin -> sety room (h - bottom_m - height room)
| Left_margin -> setx room left_m
| Right_margin -> setx room (w - right_m - width room)
)
let ( >> ) f1 f2 = fun p -> (f1 p; f2 p)
let only_set_pos ?(sep = Theme.room_margin) room pos =
let rsz = set_pos_fn ~sep room pos in
room.resize <- rsz
let and_set_pos ?(sep = Theme.room_margin) room pos =
let rsz = set_pos_fn ~sep room pos in
room.resize <- (room.resize >> rsz)
end
let set_height ?keep_resize ?check_window ?update_bg l h =
set_size ?keep_resize ?check_window ?update_bg ~h l
let set_width ?keep_resize ?check_window ?update_bg l w =
set_size ?keep_resize ?check_window ?update_bg ~w l
let reset_pos l =
let w,h = get_size l in
let g = geometry ~w ~h () in
l.geometry <- g;
l.current_geom <- to_current_geom g
let get_window_pos layout =
let f x = if x = not_specified then None else Some x in
let x,y = match layout.canvas with
| None -> xpos layout, ypos layout
| Some _ -> Draw.get_window_position (window layout) in
f x, f y
let set_window_pos layout (x,y) =
match layout.canvas with
| None -> let g = layout.current_geom in
layout.current_geom <- { g with x; y }
| Some _ -> Draw.set_window_position (window layout) x y
let get_transform l =
let t = l.geometry.transform in
let angle = Avar.get t.angle in
let center = Avar.get t.center in
let flip = Avar.get t.flip in
let alpha = Avar.get t.alpha in
Draw.make_transform ~angle ?center ~flip ~alpha ()
let get_alpha l =
Avar.get l.geometry.transform.alpha
let draggable l =
l.draggable
let set_draggable l =
l.draggable <- true
let set_clip l =
l.clip <- true
let unset_clip l =
l.clip <- false
let set_show l b =
l.show <- b
let rec_set_show b l =
let rec loop b l =
l.show <- b;
match l.content with
| Resident _ -> ()
| Rooms list -> List.iter (loop b) list in
loop b l
let is_shown l = l.show
let show_window t =
set_show (top_house t) true;
do_option (window_opt t) Sdl.show_window
let hide_window t =
set_show (top_house t) false;
do_option (window_opt t) Sdl.hide_window
(** return absolute (x,y) position *)
let compute_pos room =
let rec loop x0 y0 r =
let x,y = x0 + (Avar.get r.geometry.x),
y0 + (Avar.get r.geometry.y) + (Avar.get (Var.get r.geometry.voffset)) in
match r.house with
| None -> x,y
| Some h -> loop x y h in
loop 0 0 room
let house_pos room =
match room.house with
| None -> 0,0
| Some h -> compute_pos h;;
let is_leaf room =
match room.content with
| Resident _
| Rooms [] -> true
| _ -> false
let rec find_resident test room =
match room.content with
| Resident w -> if test w then Some room else None
| Rooms list -> let rec loop = function
| [] -> None
| r :: rest -> let f = find_resident test r in
if f = None then loop rest else f in
loop list
let search room scan =
if scan room then Some room
else let f r = if scan r then raise (Found r) in
try
iter f (top_house room);
raise Not_found
with
| Found r -> printd debug_warning "Search OK"; Some r
| Not_found -> printd debug_error "Search produced no result!"; None
| e -> raise e
let find_room_old house id =
printd debug_warning "Search room #%d in %d..." id (house.id);
let scan r = r.id = id in
search house scan
let next ?(circular = false) ?(only_visible = true) room =
match room.house with
| None -> None
| Some h ->
let rooms = get_rooms h in
let equal r1 r2 = (r1.show || not only_visible) && r1 == r2 in
match try list_next equal room rooms with Not_found -> None
with
| None -> if circular
then if only_visible then List.find_opt is_shown rooms else list_hd_opt rooms
else None
| n -> n
let prev ?(circular = false) ?(only_visible = true) room =
match room.house with
| None -> None
| Some h ->
let rooms = get_rooms h in
match try list_prev_check (equal room) ~check:(fun r -> r.show || not only_visible)
rooms with Not_found -> None
with
| None ->
if circular
then if only_visible
then List.find_opt is_shown (List.rev rooms)
else list_last_opt rooms
else None
| n -> n
let rec first_room r =
printd debug_board "Descending to room %s" (sprint_id r);
match r.content with
| Resident _ -> r
| Rooms [] -> r
| Rooms (a::_) -> first_room a
let rec next_up r =
check_option r.house (fun h ->
match next h with
| None -> next_up h
| o -> o)
let next_leaf ~top room =
if not (top_house room == top)
then begin
printd (debug_board + debug_warning + debug_custom)
"Room %s does not belong the the top house %s. We select the first room \
of the top house." (sprint_id room) (sprint_id top);
first_room top
end
else match room.content with
| Rooms []
| Resident _ -> begin
match next room with
| None ->
let h = match next_up room with
| Some r -> r
| None -> printd debug_board
"No next widget was found; we start again from top";
top in
first_room h
| Some n ->
(match n.content with
| Resident _ -> n
| Rooms _ -> first_room n)
end
| Rooms _ -> first_room room
let next_keyboard ~top room =
let rec loop r visited =
if List.mem r.id visited
then (printd debug_custom "Room %s already visited" (sprint_id room);
None)
else
let n = next_leaf ~top r in
if equal room n
then (printd (debug_board + debug_custom) "No keyboard_focus found"; None)
else if n.keyboard_focus <> None && n.show && not n.disabled
then (printd (debug_board + debug_custom) "Found %s" (sprint_id n); Some n)
else loop n (r.id :: visited) in
loop room []
let remove_canvas room =
iter (fun r -> r.canvas <- None) room
let free l =
printd debug_memory "Freeing Layout %s" (sprint_id l);
unload_background l;
begin match l.content with
| Resident _ -> ()
| Rooms list ->
list_iter list (fun r ->
do_option r.house (fun h ->
if equal h l
then printd debug_warning "Room %s is now orphan" (sprint_id r)))
end
let kill_all_NO room =
match room.house with
| Some h -> printd debug_error "Cannot kill layout #%u because it still \
belongs to a house #%u" room.id h.id;
| None ->
Sync.push (fun () ->
delete_backgrounds room;
let rec loop r =
remove_wtable r;
match r.content with
| Resident w -> Widget.free w
| Rooms list -> List.iter loop list
in
loop room)
let kill_rooms_NO house =
match house.content with
| Resident _ -> printd debug_error "House #%u does not have rooms to kill..." house.id
| Rooms list ->
Sync.push (fun () ->
let rec loop r =
delete_background r;
remove_wtable r;
match r.content with
| Resident w -> Widget.free w
| Rooms list -> List.iter loop list
in
List.iter loop list)
let global_translate room dx dy =
do_option room.house (fun h ->
printd debug_warning
"You are translating the current_geom of room #%u which already has a \
house #%u. This is likely to have no effect, as the current_geom is \
automatically updated at each display" room.id h.id);
iter (fun r ->
r.current_geom <- { r.current_geom with x = r.current_geom.x + dx;
y = r.current_geom.y + dy }) room
let rec fit_content ?(sep = Theme.room_margin/2) l =
if l.adjust = Nothing || l.clip then ()
else let w,h = match l.content with
| Resident widget -> Widget.default_size widget
| Rooms list ->
let x0 = l.current_geom.x in
let y0 = l.current_geom.y in
( List.fold_left (fun m r ->
if same_layer r l
then imax m (r.current_geom.x - x0 + r.current_geom.w)
else m)
0 list,
List.fold_left (fun m r ->
if same_layer r l
then imax m (r.current_geom.y - y0 + r.current_geom.h)
else m)
0 list )
in
let g' = match l.adjust with
| Fit -> { l.current_geom with w = w+sep; h = h+sep };
| Width -> { l.current_geom with w = w+sep };
| Height -> { l.current_geom with h = h+sep };
| Nothing -> failwith "already treated case !" in
let oldg = l.current_geom in
if g' <> oldg then begin
printd debug_graphics "ADJUST %s to New SIZE %d,%d" (sprint_id l) w h;
set_size l ~w:g'.w ~h:g'.h;
do_option l.house fit_content
end
let rec get_widgets layout =
match layout.content with
| Rooms h -> List.flatten (List.map get_widgets h)
| Resident w -> [w]
let has_resident layout =
match layout.content with
| Resident _ -> true
| Rooms _ -> false
let accept_focus r =
r.show && not r.disabled && not r.removed && not r.hidden
let has_keyboard_focus r =
r.keyboard_focus = Some true && not r.disabled
let set_keyboard_focus r =
if r.disabled
then printd debug_error "Trying to set keyboard_focus to a disabled room %s" (sprint_id r)
else match r.keyboard_focus with
| Some b ->
if not b then begin
printd debug_board "Setting keyboard_focus to room %s" (sprint_id r);
r.keyboard_focus <- Some true;
match r.content with
| Rooms _ -> ()
| Resident w -> Widget.set_keyboard_focus w
end
| None -> printd debug_board
"Cannot set keyboard_focus to room %s because if was not created \
with keyboard_focus capability." (sprint_id r)
let rec remove_keyboard_focus r =
do_option r.keyboard_focus (fun b -> if b then r.keyboard_focus <- Some false);
Trigger.push_remove_focus r.id;
match r.content with
| Rooms list -> List.iter remove_keyboard_focus list
| Resident w -> Widget.remove_keyboard_focus w
let claim_focus r =
if has_resident r then Trigger.push_mouse_focus r.id
else printd (debug_error + debug_board)
"Cannot claim focus on room %s without resident." (sprint_id r)
let claim_keyboard_focus r =
if has_resident r then Trigger.push_keyboard_focus r.id
else printd (debug_error + debug_board)
"Cannot claim keyboard_focus on room %s without resident." (sprint_id r)
let disable t =
iter (fun r -> r.disabled <- true) t;
remove_keyboard_focus t;
Trigger.push_remove_focus t.id
let enable t =
iter (fun r -> r.disabled <- false) t
let set_enabled t = function
| true -> enable t
| false -> disable t
let v_center layout y0 h =
match layout.content with
| Resident _ -> ()
| Rooms rs ->
list_iter rs
(fun r -> let h0 = height r in
let y = Draw.center y0 h h0 in
sety r y)
(** vertical align *)
let v_align ~align layout y0 h =
match layout.content with
| Resident _ -> ()
| Rooms rs ->
list_iter rs
(fun r -> let h0 = height r in
let y = Draw.align align y0 h h0 in
sety r y)
(** create a room (=layout) with a unique resident (=widget), in the current
layer (unless specified). No margin possible. *)
let resident_with_layer ?layer ?name ?(x = 0) ?(y = 0) ?w ?h ?background
?draggable ?canvas ?keyboard_focus widget =
let (w',h') = Widget.default_size widget in
let w = default w w' in
let h = default h h' in
let keyboard_focus = match keyboard_focus with
| Some true -> Some false
| Some false -> None
| None -> Widget.guess_unset_keyboard_focus widget in
let geometry = geometry ~x ~y ~w ~h () in
create ?name ?background ?keyboard_focus ?draggable ?canvas ?layer
geometry (Resident widget)
let resident ?name ?(x = 0) ?(y = 0) ?w ?h ?background ?draggable
?canvas ?keyboard_focus widget =
resident_with_layer ?name ~x ~y ?w ?h ?background ?draggable
?canvas ?keyboard_focus widget
let of_widget = resident
let change_resident ?w ?h room widget =
match room.content with
| Resident resid ->
printd debug_board "Replacing room %s's widget by widget #w%d"
(sprint_id room) (Widget.id widget);
let (w',h') = Widget.default_size widget in
let w = default w w' in
let h = default h h' in
room.content <- Resident widget;
widget.Widget.room_id <- Some room.id;
room.keyboard_focus <- Widget.guess_unset_keyboard_focus widget;
resid.Widget.room_id <- None;
set_size room ~w ~h
| _ -> printd debug_event "[change_resident]: but target room has no resident!"
let empty ?name ?background ~w ~h () =
let geometry = geometry ~w ~h () in
create ?name ?background geometry (Rooms [])
let scale_resize ?(scale_width=true) ?(scale_height=true)
?(scale_x=true) ?(scale_y=true) (w,h) r =
let x = xpos r in
let y = ypos r in
let rw,rh = get_size r in
let resize (hw,hh) = let open Resize in
let scalex z = z * hw / w in
let scaley z = z * hh / h in
if scale_x then setx r (scalex x);
if scale_y then sety r (scaley y);
if scale_height then set_voffset r (scaley (get_voffset r));
if scale_height && scale_width then set_size r ~w:(scalex rw) ~h:(scaley rh)
else if scale_height then set_height r (scaley rh)
else if scale_width then set_width r (scalex rw) in
r.resize <- resize
let scale_resize_list ?scale_width ?scale_height ?scale_x ?scale_y (w,h) rooms =
List.iter (scale_resize ?scale_width ?scale_height ?scale_x ?scale_y (w,h))
rooms
let scale_resize ?scale_width ?scale_height ?scale_x ?scale_y room =
match room.house with
| None -> printd debug_error
"Cannot compute the resize function of room %s since it does not \
belong to a house" (sprint_id room)
| Some h ->
let s = get_size h in
scale_resize ?scale_width ?scale_height ?scale_x ?scale_y s room
let auto_scale house =
let (w,h) = get_size house in
match house.content with
| Rooms rooms -> scale_resize_list (w,h) rooms
| Resident _ -> printd debug_warning "TODO: auto_scale resident"
let clip_interval ?xmin ?xmax x =
match xmin, xmax with
| None, None -> x
| Some xmin, None -> imax x xmin
| None, Some xmax -> imin x xmax
| Some xmin, Some xmax -> imin xmax (imax xmin x)
let clip_size ?min_width ?min_height ?max_width ?max_height (w, h) =
let ww = clip_interval ?xmin:min_width ?xmax:max_width w in
let hh = clip_interval ?xmin:min_height ?xmax:max_height h in
(ww, hh)
let resize_follow_house ?min_width ?min_height ?max_width ?max_height room =
let rsz = match min_width, min_height, max_width, max_height with
| None, None, None, None ->
fun (w, h) -> Resize.set_size room ~w ~h
| _ -> let open Resize in fun (w, h) ->
let w, h = clip_size ?min_width ?min_height ?max_width ?max_height (w, h) in
set_size room ~w ~h
in
room.resize <- rsz
let resize_follow_width ?min_width ?max_width room =
let rsz = match min_width, max_width with
| None, None -> fun (w, _) -> Resize.set_width room w
| _ -> let open Resize in fun (w, _) ->
set_width room (clip_interval ?xmin:min_width ?xmax:max_width w) in
room.resize <- rsz
let resize_keep_margins ?margin ?(min_width=Theme.room_margin)
?(min_height = Theme.room_margin) ?max_width ?max_height room =
let top_margin, bottom_margin, left_margin, right_margin =
match margin with
| Some x -> x, x, x, x
| None -> let bm, rm = match get_house room with
| Some house -> height house - height room - gety room,
width house - width room - getx room
| None -> gety room, getx room in
gety room, bm, getx room, rm in
room.resize <- let open Resize in
(fun (w, h) ->
let w, h = clip_size ~min_width ~min_height ?max_width ?max_height
(w - left_margin - right_margin, h - top_margin - bottom_margin) in
set_size room ~w ~h;
setx room left_margin;
sety room top_margin)
let flat_settle_rooms ~sep ~hmargin ~vmargin rooms =
let rec loop list x y =
match list with
| [] -> if x = hmargin then (2*hmargin, 2*vmargin)
else (x - sep + hmargin, y)
| r::rest ->
setx r x;
sety r vmargin;
let wr = width r in
loop rest (if wr = 0 then x else x + sep + wr)
(max y ((height r) + 2*vmargin))
in loop rooms hmargin vmargin
module Detect = struct
let constant_list = function
| [] -> true
| x::rest -> List.for_all (fun (i : int) -> i = x) rest
let almost_equal x y = abs (x-y) <= 1
let almost_constant = function
| [] -> true
| x::rest ->
let rec loop m mm = function
| [] -> almost_equal m mm
| i::rest -> almost_equal m mm && almost_equal m i && almost_equal m i &&
loop (imin m i) (imax mm i) rest in
loop x x rest
let test_almost_constant () =
assert (almost_constant [5;5;5;5;5]);
assert (almost_constant [4;3;4;3;4;4;3;3;4]);
assert (not (almost_constant [4;3;4;3;4;4;3;3;5]))
let detect_h_alignment gs =
let tops = List.map (fun g -> g.y) gs in
let bottoms = List.map (fun g -> g.y + g.h) gs in
let mids = List.map (fun g -> g.y + g.h/2) gs in
if constant_list tops then Some Draw.Min
else if constant_list bottoms then Some Draw.Max
else if constant_list mids then Some Draw.Center
else if almost_constant tops then Some Draw.Min
else if almost_constant bottoms then Some Draw.Max
else if almost_constant mids then Some Draw.Center
else None
let detect_h_margin w = function
| [] -> 0
| g1::_ as l ->
let g2 = list_last l in
let m1 = g1.x in
let m2 = w - g2.x - g2.w in
if almost_equal m1 m2 then imin m1 m2
else (printd debug_warning "left and right margins are not equal, taking min.";
imin m1 m2)
let detect_sep = function
| [] -> 0
| g::rest -> let rec loop seps g = function
| [] -> seps
| g2::rest -> loop ((g2.x - g.x - g.w) :: seps) g2 rest in
let seps = loop [] g rest in
if not (almost_constant seps)
then printd debug_error
"Separation spaces are not constant in this layout; using the min.";
default (list_min Int.compare seps) 0
let detect_flat_margins room =
match room.content with
| Resident _ ->
printd (debug_error + debug_board)
"[detect_flat_margins] on %s expects rooms, not a resident"
(sprint_id room);
None
| Rooms [] -> Some (Draw.Min, 0, 0, 0)
| Rooms rooms ->
let gs = List.map (fun g -> g.current_geom) rooms in
match detect_h_alignment gs with
| None ->
printd (debug_error + debug_board)
"[detect_flat_margins] argument %s does not seem to be a flat (not aligned)"
(sprint_id room); None
| Some align ->
let w = width room in
let hmargin = detect_h_margin w gs in
let vmargin = default (list_min Int.compare (List.map (fun g -> g.y) gs)) 0 in
let sep = detect_sep gs in
Some (align, sep, hmargin, vmargin)
end
let compute_linear_scale n scale initial_len ~len ~margin ~sep =
if n <> 0
then let havail_old = imax 1 (initial_len - 2*margin - (n-1)*sep) in
let havail_new = imax 1 (len - 2*margin - (n-1)*sep) in
let s = float havail_new /. float havail_old in
scale := Some s;
s
else 1.
let resize_flat_room ~resize_height initial_flat_size scale
~hmargin ~vmargin ~sep ~align room =
let init_w, init_h = get_size room in
let scalex, scaley = scale in
room.resize <- (fun (w, h) ->
let open Resize in
if room.show then begin
let scale, margin, len = scalex, hmargin, w in
let n = List.length (neighbours room) in
let s = match !scale with
| Some x -> x
| None -> compute_linear_scale n scale ~len (fst initial_flat_size)
~margin ~sep in
let ww = (imax 1 (round (s *. float init_w))) in
let hh = if resize_height
then begin
let scale, margin, len = scaley, vmargin, h in
let s = match !scale with
| Some x -> x
| None -> compute_linear_scale 1 scale ~len (snd initial_flat_size)
~margin ~sep in
Some (imax 1 (round (s *. float init_h)))
end else None in
set_size room ~w:ww ?h:hh;
if next room = None then (scalex := None; scaley:=None);
let () = match prev room with
| None -> setx room hmargin
| Some p -> let wp = width p in
setx room (if wp = 0 then getx p else getx p + wp + sep) in
let y = Draw.align align vmargin (h - 2*vmargin) (height room) in
sety room y
end)
let resize_tower_room ~resize_width initial_tower_size scale
~hmargin ~vmargin ~sep ~align room =
let init_w, init_h = get_size room in
let scalex, scaley = scale in
printd debug_custom "[resize_tower_room] %s init_w=%i" (sprint_id room) init_w;
room.resize <- (fun (w,h) ->
let open Resize in
if room.show then begin
printd debug_custom "resize function for %s w=%i" (sprint_id room) w;
let scale, margin, len = scaley, vmargin, h in
let n = List.length (neighbours room) in
let s = match !scale with
| Some x -> x
| None -> compute_linear_scale n scale ~len (snd initial_tower_size)
~margin ~sep in
let hh = (imax 1 (round (s *. float init_h))) in
let ww = if resize_width then begin
let scale, margin, len = scalex, hmargin, w in
let s = match !scale with
| Some x -> x
| None -> compute_linear_scale 1 scale ~len (fst initial_tower_size)
~margin ~sep in
printd debug_custom "set_width %i" (imax 1 (round (s *. float init_w)));
Some (imax 1 (round (s *. float init_w)))
end else None in
set_size room ?w:ww ~h:hh;
if next room = None then (scalex := None; scaley := None);
let () = match prev room with
| None -> sety room vmargin
| Some p -> printd debug_custom "previous room %s, show=%b, y=%i"
(sprint_id p) (p.show) (gety p);
let hp = height p in
sety room (if hp = 0 then gety p else gety p + height p + sep) in
let x = Draw.align align hmargin (w - 2*hmargin) (width room) in
setx room x
end)
let resize_flat ?(resize_height = true) ~hmargin ~vmargin ~sep ~align house =
let size = get_size house in
let scale = ref None, ref None in
iter_rooms (resize_flat_room ~resize_height size scale ~hmargin ~vmargin ~sep ~align)
house
let resize_tower ?(resize_width = true) ~hmargin ~vmargin ~sep ~align house =
let size = get_size house in
let scale = ref None, ref None in
iter_rooms (resize_tower_room ~resize_width size scale ~hmargin ~vmargin ~sep ~align)
house
let resize_fix_x room =
let f = room.resize in
room.resize <- (fun size ->
let open Resize in
let x,w = getx room, width room in
f size;
setx room x;
set_width room w
)
let resize_fix_y room =
let f = room.resize in
room.resize <- (fun size ->
let open Resize in
let y,h = gety room, height room in
f size;
sety room y;
set_height room h
)
let maximize l =
setx l 0;
sety l 0;
let w,h = get_size (top_house l) in
l.current_geom <- { l.current_geom with h; w };
Avar.set l.geometry.h h;
Avar.set l.geometry.w w;
scale_resize_list (w,h) [l];
resize_content l
let check_layers room =
let rec loop house r =
match r.content with
| Resident _ ->
if Chain.(get_layer house >. get_layer r)
then printd debug_error
"The house #%d contains a room #%d with deeper layer! (%d>%d)"
house.id r.id (Chain.depth (get_layer house))
(Chain.depth (get_layer r));
| Rooms h -> List.iter (loop r) h
in
loop room room
(** Set the canvas of the layout. Warning! we assume that if a room has a
canvas, all smaller rooms already have the same canvas... *)
let set_canvas canvas room =
room.canvas <- Some canvas;
if !debug then check_layers room
(** Set the canvas for layout and all children *)
let global_set_canvas room canvas =
iter (fun r -> r.canvas <- Some canvas) room
let check_layer_error room house =
if not (Chain.same_stack room.layer house.layer)
then printd debug_error
"The replacement room %s belongs to a separate set of layers disjoint \
from the house %s (or one of them has empty layer). Beware that it \
will probably never be displayed"
(sprint_id room) (sprint_id house)
let move_to_stack ~dst room =
if not (same_stack room dst)
then begin
let dst_layer = get_layer dst in
printd debug_board "Moving layer of %s into that of %s (stack:%u)"
(sprint_id room) (sprint_id dst) (Chain.get_stack_id dst_layer);
room.layer <- Chain.copy_into ~dst:dst_layer (get_layer room);
Chain.replace room.layer (Draw.new_layer ())
end
let move_all_to_stack ~dst room =
iter (move_to_stack ~dst) room
let unify_layer_stack room =
move_all_to_stack ~dst:room room
let set_new_stack ?(force = false) win =
if force || is_top win then begin
printd debug_board "Creating a new stack for window layout %s."
(sprint_id win);
win.layer <- Chain.copy_into ~dst:None (get_layer win);
Chain.replace win.layer (Draw.new_layer ())
end else printd (debug_board + debug_error)
"Creating a new stack is only allowed for top layouts (windows), not for %s"
(sprint_id win)
let move_to_new_stack room =
set_new_stack room;
unify_layer_stack room
let create_win_house windows =
let rec loop layer_ids = function
| [] -> ()
| win::rest ->
let id = Chain.get_stack_id (get_layer win) in
let id =
if List.mem id layer_ids then
begin
set_new_stack ~force:true win;
Chain.get_stack_id (get_layer win)
end else id in
unify_layer_stack win;
loop (id::layer_ids) rest in
loop [] windows;
let layer = None in
create_unsafe ~set_house:false ~name:"windows_house" ~layer
(geometry ()) (Rooms windows)
let detach_rooms layout =
match layout.content with
| Resident _ ->
printd debug_error "No rooms to detach from layout %s" (sprint_id layout)
| Rooms rooms ->
list_iter rooms (fun r ->
if r.house <> None then
(r.house <- None;
r.canvas <- None;
printd debug_warning "Room %s was detached from House %s..."
(sprint_id r) (sprint_id layout)))
let detach room =
match room.house with
| None -> printd debug_error "Cannot detach because room %s has no house"
(sprint_id room)
| Some h ->
room.house <- None;
room.canvas <- None;
remove ~children:true room;
let rooms = List.filter (fun r -> not (r == room)) (get_rooms h) in
h.content <- Rooms rooms;
printd debug_warning "Room %s was detached from House %s."
(sprint_id room) (sprint_id h)
let attach ~dst room =
room.house <- Some dst;
move_to_stack ~dst room;
do_option dst.canvas (global_set_canvas room)
let ok_to_add_room ?(already = false) ?(loop_error = true) ~dst room =
if equal room dst
then begin
printd debug_error "Cannot add room %s to itself!" (sprint_id room);
if loop_error then invalid_arg "[Layout.ok_to_add_room] room %s"
(sprint_id room)
else false
end else match room.house with
| Some h when equal h dst ->
printd ((if already then debug_warning else debug_error) + debug_board)
"Room %s already belongs to %s." (sprint_id room) (sprint_id dst);
already
| Some h ->
printd (debug_error + debug_board)
"Room %s should not be added to %s because it belongs to another house \
(%s). We do it anyway." (sprint_id room) (sprint_id dst) (sprint_id h);
true
| None -> true
let set_rooms layout rooms =
match layout.content with
| Resident _ ->
printd debug_error
"Cannot transform a leaf (Resident #%u) to a node (Rooms) because the \
resident widget would be lost" layout.id
| Rooms old_rooms ->
list_iter rooms (fun r ->
if mem equal r old_rooms
then printd (debug_board + debug_warning)
"Trying to insert a room (%s) that is already there (%s). We leave \
it there, no problem."
(sprint_id r) (sprint_id layout)
else if ok_to_add_room ~dst:layout r
then attach ~dst:layout r);
list_iter old_rooms (fun r ->
if not (mem equal r rooms) then begin
detach r;
iter_unload_textures r
end);
layout.content <- Rooms rooms
let set_rooms layout ?(sync=true) rooms =
(if sync then Sync.push else run) (fun () -> set_rooms layout rooms)
let replace_rooms_NO layout rooms =
kill_rooms_NO layout;
set_rooms layout rooms
let copy ~src ~dst =
let dx = dst.current_geom.x - src.current_geom.x in
let dy = dst.current_geom.y - src.current_geom.y in
global_translate src dx dy;
dst.geometry <- { dst.geometry with w = src.geometry.w; h = src.geometry.h };
let w,h = get_size src in
dst.current_geom <- { dst.current_geom with w; h };
dst.clip <- src.clip;
dst.background <- src.background;
begin match src.content with
| Resident r as c -> r.Widget.room_id <- Some dst.id; dst.content <- c
| Rooms rooms -> set_rooms dst rooms
end;
dst.keyboard_focus <- src.keyboard_focus;
dst.draggable <- src.draggable
let add_room ?valign ?halign ~dst room =
if ok_to_add_room ~dst room then begin
check_layer_error room dst;
let rooms = get_rooms dst in
let wmax = (getx room) + (width room) in
if wmax > width dst
then (printd debug_error "The attached Room #%u is too wide" room.id;
);
let hmax = (gety room) + (height room) in
if hmax > height dst
then (printd debug_error "The attached Room #%u is too tall" room.id;
);
let x = default_lazy (map_option halign (fun a ->
Draw.align a 0 (width dst) (width room)))
(lazy (getx room)) in
let y = default_lazy (map_option valign (fun a ->
Draw.align a 0 (height dst) (height room)))
(lazy (gety room)) in
setx room x;
sety room y;
attach ~dst room;
dst.content <- Rooms (List.rev (room :: (List.rev rooms)))
end
let set_layer ?(debug = !debug) room layer =
room.layer <- layer;
if debug then check_layers room
let rec_set_layer room layer =
iter (fun r -> set_layer ~debug:false r layer) room
let flat ?name ?sep ?(adjust=Fit) ?hmargin ?vmargin ?margins ?align
?background ?shadow ?canvas ?resize ?clip rooms =
let sep, hmargin, vmargin = match margins with
| Some m ->
if sep <> None || hmargin <> None || vmargin <> None
then printd debug_user "Call to [flat]: the [margins] parameter is set and \
overrides [sep,hmargin,vmargin].";
m,m,m
| None -> default sep (Theme.room_margin / 2),
default hmargin (Theme.room_margin),
default vmargin (Theme.room_margin) in
let w,h = flat_settle_rooms ~sep ~hmargin ~vmargin rooms in
let layout = create ?name ?background ?shadow ?clip
(geometry ~w ~h ()) ~adjust (Rooms rooms) ?canvas in
do_option align (fun align -> v_align ~align layout vmargin (h-2*vmargin));
let resize = default resize
(if clip = Some true then Resize.Disable else Resize.Default) in
let () = if resize = Resize.Disable then List.iter disable_resize rooms
else let resize_height = not (resize = Resize.Linear) in
resize_flat ~resize_height ~hmargin ~vmargin ~sep ~align:(default align Draw.Min)
layout in
layout
let hbox = flat
let flat_of_w ?name ?(sep = Theme.room_margin) ?h ?align ?background ?widget_bg
?canvas ?resize ?clip widgets =
let rooms =
List.map (fun wg ->
let name = map_option name (fun s -> "Resident of [" ^ s ^ "]") in
resident ?name ?h ~x:0 ~y:sep ?background:widget_bg ?canvas wg) widgets
in
flat ?name ~margins:sep ?align ?background ?canvas ?resize ?clip rooms
let h_center layout x0 w =
match layout.content with
| Resident _ -> ()
| Rooms rs ->
list_iter rs
(fun r -> let w0 = width r in
let x = Draw.center x0 w w0 in
setx r x)
let h_align ~align layout x0 w =
match layout.content with
| Resident _ -> ()
| Rooms rs ->
list_iter rs
(fun r -> let w0 = width r in
let x = Draw.align align x0 w w0 in
setx r x)
let tower ?name ?sep ?margins ?hmargin ?vmargin ?align
?(adjust = Fit) ?background ?shadow ?canvas ?resize
?clip rooms =
let sep, hmargin, vmargin = match margins with
| Some m ->
if sep <> None || hmargin <> None || vmargin <> None
then printd debug_user "Call to [tower]: the [margins] parameter is set \
and overrides [sep,hmargin,vmargin].";
m,m,m
| None -> default sep (Theme.room_margin / 2),
default hmargin (Theme.room_margin),
default vmargin (Theme.room_margin) in
let rec loop list x y =
match list with
| [] -> if y = vmargin then (2*hmargin, 2*vmargin)
else (x, y - sep + vmargin)
| r::rest ->
setx r hmargin;
sety r y;
let hr = height r in
loop rest (max x ((width r) + 2*hmargin))
(if hr = 0 then y else y + sep + hr) in
let w,h = loop rooms hmargin vmargin in
let layout = create ~adjust ?name ?background ?shadow ?clip
(geometry ~w ~h ()) (Rooms rooms) ?canvas in
do_option align (fun align -> h_align ~align layout hmargin (w-2*hmargin));
let resize = default resize
(if clip = Some true then Resize.Disable else Resize.Default) in
let () = if resize = Resize.Disable then List.iter disable_resize rooms
else let resize_width = not (resize = Resize.Linear) in
resize_tower ~resize_width ~hmargin ~vmargin ~sep ~align:(default align Draw.Min)
layout in
printd debug_custom "end of tower for %s" (sprint_id layout);
layout
let tower_of_w ?name ?(sep = Theme.room_margin) ?w ?align ?background ?widget_bg
?canvas ?resize ?clip widgets =
let rooms =
List.map (fun wg ->
let name = map_option name (fun s -> "Resident of [" ^ s ^ "]") in
resident ?name ?w ~x:sep ~y:0 ?background:widget_bg ?canvas wg) widgets
in
tower ?name ~margins:sep ?align ?background ?canvas ?resize ?clip rooms
let bounding_geometry = function
| [] -> printd debug_warning "Trying to find bounding_geometry of empty list";
0,0,0,0
| rooms ->
let rec loop xmin ymin xmax ymax = function
| [] -> (xmin, ymin, xmax - xmin + 1, ymax - ymin + 1)
| room :: rest ->
let x,y = getx room, gety room in
loop
(imin xmin x)
(imin ymin y)
(imax xmax (width room + x - 1))
(imax ymax (height room + y - 1))
rest in
loop max_int max_int 0 0 rooms
module Grid = struct
let detect_rows ?(overlap = 1) house =
let vranges =
match house.content with
| Resident _ -> [(0, height house)]
| Rooms rooms -> List.map (fun r ->
let y = gety r in
(y, y + height r - overlap)) rooms in
Selection.of_list vranges
let detect_columns ?(overlap = 1) house =
let hranges =
match house.content with
| Resident _ -> [(0, width house)]
| Rooms rooms -> List.map (fun r ->
let x = getx r in
(x, x + width r - overlap)) rooms in
Selection.of_list hranges
end
let cover ?name room =
create ?name ~clip:true room.geometry (Rooms [room])
let superpose ?w ?h ?name ?background ?canvas ?(center=false)
?(scale_content=true) rooms =
let x,y,bw,bh = bounding_geometry rooms in
List.iter (fun r ->
setx r (getx r - x);
sety r (gety r - y)) rooms;
let w = default w bw in
let h = default h bh in
if center then List.iter (fun r ->
setx r (Draw.center (getx r) w (width r));
sety r (Draw.center (gety r) h (height r))) rooms;
let geometry = geometry ~x ~y ~w ~h () in
if scale_content then scale_resize_list (w,h) rooms;
create ?name ?background ?canvas geometry (Rooms rooms)
(** save the layout_id in the user event *)
let save_to_event_OLD event room =
Sdl.Event.(set event Trigger.room_id room.id)
(** ask all the subwidgets to update themselves. *)
let rec ask_update room =
match room.content with
| Resident w -> Widget.update w
| Rooms list -> List.iter ask_update list
(** animations: *)
let animate_x room x =
let g = room.geometry in
Avar.stop g.x;
room.geometry <- { g with x }
let animate_y room y =
let g = room.geometry in
Avar.stop g.y;
room.geometry <- { g with y }
let animate_w room w =
let g = room.geometry in
Avar.stop g.w;
room.geometry <- { g with w }
let animate_h room h =
let g = room.geometry in
Avar.stop g.h;
room.geometry <- { g with h }
let animate_voffset room voffset =
let g = room.geometry in
let avar = Var.get g.voffset in
let is_current = Avar.started avar && not (Avar.finished avar) in
Avar.stop avar;
Var.set g.voffset voffset;
if is_current then ignore (get_voffset room)
let animate_alpha room alpha =
let g = room.geometry in
Avar.stop g.transform.alpha;
room.geometry <- { g with transform = { g.transform with alpha }};
ask_update room
let animate_angle room angle =
let g = room.geometry in
Avar.stop g.transform.angle;
room.geometry <- { g with transform = { g.transform with angle }}
let stop_pos room =
printd debug_graphics "Stop position animation for layout %s." (sprint_id room);
let g = room.geometry in
Avar.stop g.x;
Avar.stop g.y
(** get desired room (relative) geometry after applying animation *)
let geom r =
let g = r.geometry in
to_current_geom g
(** some predefined animations: *)
let default_duration = 300
let show ?(duration=default_duration) ?from room =
if room.show && Avar.finished (Var.get room.geometry.voffset)
then if !debug then begin assert (not room.removed);
printd (debug_board + debug_warning)
"Room %s is already shown, we don't run the show animation"
(sprint_id room)
end else ()
else begin
let clip = ref false in
let init () =
clip := room.clip;
set_clip room
in
let h = height room in
if not room.show && (get_voffset room <> -h)
then (printd debug_warning
"Using a 'show' animation on a room that was not previously in a \
'hidden' state. Forcing voffset to %d." (-h);
set_voffset room (-h));
let h, duration = match from with
| None ->
let current_vo = get_voffset room in
let d' = if h = 0 then 0 else abs ((current_vo * duration) / h) in
current_vo, d'
| Some Avar.Bottom -> h, duration
| Some Avar.Top -> -h, duration
| Some _ -> printd debug_board "Layout.show direction not implemented";
h, duration in
let ending () =
printd debug_board "End of show for %s" (sprint_id room);
room.clip <- !clip in
let voffset = Avar.show ~init ~ending ~duration h 0 in
animate_voffset room voffset;
rec_set_show true room;
room.removed <- false
end
let hide ?(duration=default_duration) ?(towards = Avar.Bottom) room =
if (not room.show) then ()
else begin
let clip = ref false in
let init () =
clip := room.clip;
set_clip room
in
let h = height room in
let current_vo = get_voffset room in
let d' = abs ((h + current_vo)*duration) / (abs h+1) in
let vo = match towards with
| Avar.Bottom -> h
| Avar.Top -> -h
| _ -> printd debug_board "Layout.show direction not implemented"; h in
let ending _ =
printd debug_board "End of hide";
room.clip <- !clip;
rec_set_show false room;
remove room in
let voffset = Avar.show ~init ~ending ~duration:d' current_vo vo in
animate_voffset room voffset
end
(** scrolling to a particular vertical position y, for a prescribed duration. *)
let scroll_to ?(duration=1000) y room =
match room.house with
| None -> printd debug_error "Cannot scroll the top layout (need a house)"
| Some house ->
let current_vo = get_voffset house in
let y' = max 0 (min y (height room - height house)) in
let duration = duration * (abs(current_vo + y') + 1) / (abs (current_vo + y) + 1) in
printd debug_graphics "Scroll room#%u in house #%u, from %d to %d, duration=%d"
room.id house.id current_vo (-y') duration;
let voffset = Avar.fromto ~duration current_vo (-y') in
animate_voffset house voffset
(** relative scrolling *)
let scroll_delay = ref 0.5
let scroll_old2 ?(duration=default_duration) dy room =
do_option room.house (fun house ->
let current_vo = get_voffset house in
let avar = Var.get house.geometry.voffset in
let jump_vo = Avar.final_value avar in
let final_vo = - (max 0 (min (dy - jump_vo) (height room - height house))) in
let duration = duration * (abs(final_vo - current_vo) + 1) / (abs (jump_vo - dy - current_vo) + 1) in
let dt = Avar.progress avar in
printd debug_graphics "Scroll room #%u: dy=%d, current_vo=%d, jump_vo=%d, final_vo=%d, duration=%d, progress=%f"
room.id dy current_vo jump_vo final_vo duration dt;
let voffset = if current_vo = jump_vo || duration = 0
then (scroll_delay := 0.5;
Avar.fromto ~duration current_vo final_vo)
else
let dv = final_vo - jump_vo in
let h = if dt < !scroll_delay
then scroll_delay := max 0.3 !scroll_delay /. 1.5
else if dt > 1.5 *. !scroll_delay
then scroll_delay := min 0.5 (1.5 *. !scroll_delay);
!scroll_delay in
let slope = (float (jump_vo - current_vo)) *. (1. -. h) /.
(h *. (float (final_vo - jump_vo))) in
let g1 = Avar.affine (float current_vo) (float jump_vo) in
let g2 u = Avar.initial_slope ~slope:(max 1. (abs_float slope)) u
|> Avar.affine (float jump_vo) (float final_vo) in
let update _ u = Avar.concat ~weight:h g1 g2 u
|> round in
printd debug_graphics "Scroll: dv=%d, h=%f slope=%f" dv h slope;
Avar.create ~duration ~update current_vo in
animate_voffset house voffset)
let last_time = ref (Time.now ());;
let scroll ?(duration=default_duration) dy room =
do_option room.house (fun house ->
let current_vo = get_voffset house in
let avar = Var.get house.geometry.voffset in
let jump_vo = Avar.final_value avar in
let final_vo = - (max 0 (min (dy - jump_vo) (height room - height house))) in
let elapsed = Time.(now () - !last_time) in
let duration =
if (elapsed = 0)
|| (elapsed > 300)
|| current_vo = final_vo
then duration
else let speed = (float (-dy)) /. (float elapsed) in
let final = current_vo + (round (speed *. (float duration))) in
if final = current_vo then duration
else abs (duration * (final_vo - current_vo) / (final - current_vo)) in
printd debug_graphics
"Scroll room #%u: current:%d, final_vo=%d, duration=%d"
room.id current_vo final_vo duration;
let voffset = Avar.fromto ~duration current_vo final_vo in
last_time := Time.now ();
animate_voffset house voffset)
let scroll_old ?(duration=300) dy room =
do_option room.house (fun house ->
Avar.finish (Var.get house.geometry.voffset);
let previous_vo = get_voffset house in
printd debug_graphics "Scroll room #%u, dy=%d, previous_vo=%d" room.id dy previous_vo;
scroll_to ~duration (dy - previous_vo) room)
let rec find_clip_house room =
match room.house with
| None -> None
| Some h ->
if h.clip then Some room
else find_clip_house h
(** add fade_in transform to the existing animation of the room *)
let fade_in ?duration ?(from_alpha = 0.) ?(to_alpha = 1.) room =
let alpha = Avar.fade_in ?duration ~from_alpha ~to_alpha () in
animate_alpha room alpha
(** add fade_out transform to the existing animation of the room *)
let fade_out ?duration ?from_alpha ?(to_alpha = 0.) ?(hide=false) room =
let from_alpha = default_lazy from_alpha (lazy (get_alpha room)) in
let ending _ =
printd debug_board "End of complete fade_out => hiding room";
remove room;
rec_set_show false room in
let ending = if hide then Some ending else None in
let alpha = Avar.fade_out ?duration ?ending ~from_alpha ~to_alpha () in
animate_alpha room alpha
let rotate ?duration ?(from_angle = 0.) ~angle room =
let angle = Avar.fromto_float ?duration from_angle (from_angle +. angle) in
animate_angle room angle
let zoom_x ?duration ~from_factor ~to_factor room =
let w0 = round (float (width room) *. from_factor) in
let w1 = round (float (width room) *. to_factor) in
let w = Avar.fromto ?duration w0 w1 in
printd debug_graphics "ZOOM width from %d to %d" w0 w1;
animate_w room w
let zoom_y ?duration ~from_factor ~to_factor room =
let h0 = round (float (height room) *. from_factor) in
let h1 = round (float (height room) *. to_factor) in
let h = Avar.fromto ?duration h0 h1 in
printd debug_graphics "ZOOM height from %d to %d" h0 h1;
animate_h room h
let zoom ?duration ~from_factor ~to_factor room =
zoom_x ?duration ~from_factor ~to_factor room;
zoom_y ?duration ~from_factor ~to_factor room
(** oscillate (for fun) *)
let oscillate ?(duration = 10000) ?(frequency=5.) amplitude room =
let x = Avar.oscillate ~duration ~frequency amplitude (getx room) in
animate_x room x
(** add a slide_in animation to the room *)
let slide_in ?duration ?from ?dst room =
let dst = default dst room in
let x,y = Avar.slide_in ?from ?duration ~size:(get_size dst)
~pos:(getx room, gety room) () in
animate_x room x;
animate_y room y
(** translation animation *)
let slide_to ?(duration=default_duration) room (x0,y0) =
let x1 = getx room in
let y1 = gety room in
let x = Avar.fromto ~duration x1 x0 in
let y = Avar.fromto ~duration y1 y0 in
animate_x room x;
animate_y room y
(** follow mouse animation. *)
let mouse_motion_x ?dx ?modifier room =
let x0 = ref 0 in
let init () =
x0 := default_lazy dx
(lazy (fst (Mouse.window_pos (window room)) - xpos room)) in
let update _ _ =
let x = fst (Mouse.window_pos (window room)) - (x_origin room) - !x0 in
match modifier with
| None -> x
| Some f -> x + f x in
Avar.create ~duration:(-1) ~update ~init 0
let mouse_motion_y ?dy ?modifier room =
let y0 = ref 0 in
let init () =
y0 := default_lazy dy
(lazy (snd (Mouse.window_pos (window room)) - ypos room)) in
let update _ _ =
let y = snd (Mouse.window_pos (window room)) - (y_origin room) - !y0 in
match modifier with
| None -> y
| Some f -> y + f y in
Avar.create ~duration:(-1) ~update ~init 0
let follow_mouse ?dx ?dy ?modifierx ?modifiery room =
let x = mouse_motion_x ?dx ?modifier:modifierx room in
let y = mouse_motion_y ?dy ?modifier:modifiery room in
animate_x room x;
animate_y room y
let make_clip
?w ?(scrollbar = true) ?(scrollbar_inside = false)
?(scrollbar_width = 10) ?on_scroll ~h room =
let name = (default room.name "") ^ ":clip" in
if w <> None
then printd debug_error "Horizontal scrolling is not implemented yet";
let w = default w (width room) in
let x0 = getx room in
let y0 = gety room in
sety room 0;
let active_bg = Widget.empty ~w:(width room) ~h:(height room) () in
let layer = get_layer room in
let hopt = match room.house with
| Some h ->
printd (debug_user + debug_warning + debug_board)
"Applying [make_clip] to a room %s that already belongs to a house \
%s. We try to relocate the container in that house and hope for the \
best." (sprint_id room) (sprint_id h);
detach room; Some h
| None -> None in
let container = tower ~margins:0 ~clip:true
[superpose [resident_with_layer ~layer active_bg; room]] in
set_size container ~w ~h;
do_option hopt (fun dst -> attach ~dst container);
let result =
if scrollbar
then begin
let bar = resident_with_layer ~layer
~background:(color_bg Draw.(lighter scrollbar_color))
(Widget.empty ~w:10 ~h:10 ()) in
let old_vo = ref (get_voffset container) in
let var = Tvar.create container.geometry.voffset
~t_from:(fun vo ->
let vo = Avar.get vo in
if vo <> !old_vo then do_option on_scroll (apply vo);
old_vo := vo;
let dh = height room - height bar in
if dh <= 0 then 0
else dh + vo)
~t_to:(fun v ->
let dh = height room - height bar in
let v = imin v dh |> imax 0 in
let vo = height bar - height room + v in
if vo <> !old_vo then do_option on_scroll (apply vo);
old_vo := vo;
Avar.var vo) in
let wsli = Widget.slider ~kind:Slider.Vertical ~length:h
~thickness:scrollbar_width ~tick_size:(h * h / (height room))
~var (imax 0 (height room - h)) in
change_resident bar wsli;
if h >= (height room) then hide ~duration:0 bar;
let r = if scrollbar_inside
then (setx bar (w - width bar);
set_layer bar (Chain.insert_after
(Chain.last (get_layer container))
(Draw.new_layer ()));
superpose ~name [container; bar])
else flat ~name ~margins:0 [container; bar] in
disable_resize bar;
container.resize <- (fun (w,h) ->
let open Resize in
set_height bar h;
if scrollbar_inside then set_size container ~w ~h
else begin
set_size container ~w:(w - width bar) ~h;
setx bar (w - width bar)
end;
let dh = height room - height bar in
let sli = Widget.get_slider wsli in
if dh >= 1 then Slider.set_max sli dh
else set_voffset container 0;
let h = height bar in
if height room <> 0
then Slider.set_tick_size sli
(imax (Slider.min_tick_size sli) (h * h / (height room)));
let v = Slider.update_value sli; Slider.value sli in
if v < 0 then Slider.set sli 0;
if dh <= 0
then (if is_shown bar then hide ~duration:0 bar)
else (if not (is_shown bar) then show ~duration:0 bar));
r
end
else container in
sety result y0;
setx result x0;
let shadow = room.shadow in
result.shadow <- shadow;
room.shadow <- None;
result
let relayout createfn ?(duration=200) layout =
let rooms = get_rooms layout in
let old_pos = List.map (fun r -> getx r, gety r) rooms in
let tmp = createfn rooms in
tmp.content <- Rooms [];
List.iter (fun r -> r.house <- Some layout) rooms;
if duration <> 0 then
List.iter2 (fun (oldx,oldy) room ->
let newx = getx room in
if oldx <> newx
then animate_x room (Avar.fromto ~duration oldx newx);
let newy = gety room in
if oldy <> newy
then animate_y room (Avar.fromto ~duration oldy newy))
old_pos rooms
let reflat ?align
?(hmargin = Theme.room_margin) ?(vmargin = Theme.room_margin) ?margins =
relayout (fun rooms -> flat ~name:"reflat_tmp" ~hmargin ~vmargin ?margins ?align rooms)
let retower ?align
?(hmargin = Theme.room_margin) ?(vmargin = Theme.room_margin) ?margins =
relayout (fun rooms -> tower ~name:"retower_tmp" ~hmargin ~vmargin ?margins ?align rooms)
let expand_width house =
let w = width house in
iter_rooms (fun room ->
let x = getx room in
if w-x < 1 then printd debug_warning
"Cannot expand_width because house x position is larger than width.";
set_width room (w-x)) house
let replace_room_with_layer ?(keep_layer=false) ~by room =
match room.house with
| None ->
printd (debug_error + debug_user)
"Cannot use \"replace_room\" because room %s does not belong to a house."
(sprint_id room);
false
| Some h when ok_to_add_room ~dst:h by ->
printd (debug_warning + debug_board) "Replacing room %s by room %s inside %s."
(sprint_id room) (sprint_id by) (sprint_id h);
check_layer_error by h;
if same_stack room by && not (same_layer room by) then begin
printd debug_warning "The replacement room does not belong to the same \
layer as the original room.";
if not keep_layer then begin
printd debug_warning "[replace_room]: we fix the layer. Use the \
[keep_layer] option to override.";
rec_set_layer by (room.layer)
end
end;
let rooms = get_rooms h in
detach room;
attach ~dst:h by;
h.content <- Rooms (list_replace (equal room) rooms by);
let x = getx by + xpos h in
let y = gety by + ypos h in
by.current_geom <- { by.current_geom with x; y};
true
| _ -> printd (debug_board + debug_error) "Cannot replace room %s by %s"
(sprint_id room) (sprint_id by);
false
let replace_room ~by room =
replace_room_with_layer ~keep_layer:false ~by room
let relocate ~dst ?(scroll=true) ?(auto_scale=false) room =
let x0,y0 = compute_pos dst in
let x1,y1 = compute_pos room in
if room.house <> None then detach room;
let room2 = if not scroll then room
else begin
let y2 = y1-y0 + height room in
printd debug_board "Relocate room : y2=%i y1=%i y0=%i room=%i \
dst=%i" y2 y1 y0 (height room) (height dst);
make_clip ~h:(height dst - y1 + y0) ~scrollbar_inside:true
~scrollbar_width:4 room
end in
add_room ~dst room2;
setx room2 (x1-x0);
sety room2 (y1-y0);
if auto_scale then scale_resize room2;
room2
(** display section *)
let debug_box ~color room x y =
let w,h = Draw.scale_size (get_size room) in
let x,y = Draw.scale_pos (x,y) in
let bg = if room.mouse_focus then Some (Draw.lighter color) else None in
Draw.rect_to_layer ?bg ~color (get_canvas room) (get_layer room)
(x,y) w h
let scale_rect rect =
Sdl.Rect.(create
~x:(Theme.scale_int (x rect))
~y:(Theme.scale_int (y rect))
~h:(Theme.scale_int (h rect))
~w:(Theme.scale_int (w rect)))
let scale_clip clip =
map_option clip scale_rect
let show_keyboard_focus room _transform rect =
let layer = get_layer room in
let canvas = get_canvas room in
let blits = Draw.box_shadow ~offset:(0,0) ~size:(Theme.scale_int 3) ~fill:false
canvas layer (scale_rect rect) in
List.iter Draw.blit_to_layer blits
let display ?pos0 room =
let x0,y0 = match pos0 with
| None -> house_pos room
| Some p -> p in
let rec display_loop x0 y0 h0 clip0 tr0 r =
if not r.show then ()
else begin
let g = geom r in
let x = x0 + g.x
and y = y0 + g.y + h0
and voffset = g.voffset in
r.current_geom <- { g with x; y };
let rect = Sdl.Rect.create ~x ~y ~w:g.w ~h:g.h in
let clip = if not r.clip || !no_clip then clip0
else Draw.intersect_rect clip0 (Some rect) in
let sclip = scale_clip clip in
match clip with
| Some clip_rect when not (Sdl.has_intersection clip_rect rect) ->
(r.hidden <- true;
printd debug_warning "Room #%u is hidden (y=%d)" r.id y)
| _ -> begin
r.hidden <- false;
let transform =
let tr = get_transform r in
let open Draw in
compose_transform tr0 tr in
let bg = map_option r.background (fun bg ->
let box = match bg with
| Style style ->
let b = Box.(create ~width:g.w ~height:g.h ~style ()) in
r.background <- (Some (Box b));
b
| Box b -> b in
let blits = Box.display (get_canvas r) (get_layer r) box
Draw.(scale_geom {x; y; w = g.w; h = g.h;
voffset = - voffset}) in
blits) in
if !debug && r.keyboard_focus = Some true
then show_keyboard_focus r transform rect;
begin match r.content with
| Rooms h ->
do_option bg
(List.iter
(fun blit ->
let open Draw in
let t = compose_transform transform blit.transform in
blit_to_layer { blit with clip = sclip; transform = t }));
if !draw_boxes then begin
let rect = debug_box ~color:(0,0,255,200) r x y in
let open Draw in
let t = compose_transform transform rect.transform in
blit_to_layer { rect with clip = sclip; transform = t }
end;
List.iter (display_loop x y voffset clip transform) h
| Resident w ->
let blits = Widget.display (get_canvas r) (get_layer r) w
Draw.({x; y; w = g.w; h = g.h; voffset}) in
let blits = match bg with
| None -> blits
| Some b -> List.rev_append b blits in
let blits = if !draw_boxes
then
let color = (255,0,0,200) in
let rect = debug_box ~color r x y in
rect :: blits
else blits in
List.iter
(fun blit ->
let open Draw in
let t = compose_transform transform blit.transform in
let clip = sclip in
blit_to_layer { blit with clip; transform = t }) blits
end;
if !draw_boxes
then let label = Label.create ~size:7 ~fg:(Draw.(transp blue))
(sprint_id r) in
let geom = Draw.scale_geom {Draw.x; y; w=g.w+1; h=g.h+1; voffset} in
let blits = Label.display (get_canvas r) (get_layer r) label geom in
List.iter Draw.blit_to_layer blits;
List.iter Draw.unload_blit blits
end
end in
display_loop x0 y0 0 None (Draw.make_transform ()) room
let get_focus room =
room.mouse_focus
let set_focus room =
room.mouse_focus <- true
let unset_focus room =
room.mouse_focus <- false
let set_cursor roomo =
let cursor = match roomo with
| None -> go (Draw.create_system_cursor Sdl.System_cursor.arrow)
| Some room -> match room.content with
| Rooms _ -> go (Draw.create_system_cursor Sdl.System_cursor.arrow)
| Resident w -> Widget.get_cursor w in
Sdl.set_cursor (Some cursor)
let update_old room =
let rec update_loop x0 y0 h0 clip0 room =
if not room.show then ()
else begin
let g = geom room in
let x = x0 + g.x
and y = y0 + g.y + h0 in
let clip = if g.voffset = 0 || !no_clip then clip0
else Draw.intersect_rect clip0 (Some (Sdl.Rect.create ~x ~y ~w:g.w ~h:g.h)) in
room.current_geom <- current_geom ~x ~y ~w:g.w ~h:g.h ();
match room.content with
| Rooms h -> List.iter (update_loop x y g.voffset clip) h
| Resident w -> if not (Widget.is_fresh w) then begin
let blits = Widget.display (get_canvas room) (get_layer room) w
{Draw.x = x; y; w = g.w; h = g.h; voffset = g.voffset} in
List.iter (fun blit -> Draw.(blit_to_layer { blit with clip })) blits
end
end in
let x0,y0 = house_pos room in
update_loop x0 y0 0 None room
(** check is the room has some non-fresh components. *)
let rec is_fresh room =
match room.content with
| Rooms list -> let rec loop = function
| [] -> true
| r::h -> if not (is_fresh r) then false
else loop h in
loop list
| Resident w -> Widget.is_fresh w
let room_has_anim room =
Avar.has_anim room.geometry.transform.alpha ||
Avar.has_anim room.geometry.transform.center ||
Avar.has_anim room.geometry.transform.flip ||
Avar.has_anim room.geometry.transform.angle ||
List.fold_left (fun b v -> b || (Avar.has_anim v))
false (get_int_avars room)
let rec has_anim room =
if !debug && (not room.show) && (room.hidden) && (room_has_anim room)
then printd debug_error "Room %s has unfinished animation but it is not shown."
(sprint_id room);
room.show && (not room.hidden) &&
(room_has_anim room ||
match room.content with
| Rooms list -> List.fold_left (fun b r -> b || (has_anim r)) false list
| Resident _ -> false)
let flip ?(clear=false) ?(present=true) layout =
printd debug_graphics "flip layout %s" (sprint_id layout);
if clear then Draw.clear_canvas (get_canvas layout);
printd debug_graphics "Render layers";
Var.protect_do Draw.current_layer (fun () ->
Draw.render_all_layers (get_layer layout));
if present then begin
printd debug_graphics "Present";
Draw.(sdl_flip (renderer layout))
end
let render layout =
if Draw.window_is_shown (window layout) then display layout
else printd debug_board "Window (layout #%u) is hidden" layout.id
let resize_from_window ?(flip=true) layout =
let top = top_house layout in
if not (equal layout top)
then printd debug_error
"The layout for resizing window should be the top layout";
let w,h = Draw.get_window_size (window top)
|> Draw.unscale_pos in
let w', h' = get_size top in
if (w',h') <> (w,h)
then begin
printd debug_graphics "Resize window layout %s: (%d,%d) --> (%d,%d)"
(sprint_id layout) w' h' w h;
set_size ~keep_resize:true ~check_window:false top ~w ~h;
Draw.update_background (get_canvas top);
if flip then Draw.sdl_flip (renderer top)
end
(** initialize SDL if necessary and create a window of the size of the layout *)
let make_window ?window layout =
printd debug_graphics "Make window";
let top = top_house layout in
if not (equal layout top)
then printd debug_error
" The layout for creating a window should be the top layout";
Draw.video_init ();
let w,h = get_physical_size top in
let wmax, hmax = 4096, 4096 in
if wmax < w || hmax < h
then printd debug_error
" The layout has size (%u,%u), which exceeds the max size (%u,%u)."
w h wmax hmax;
let w = min w wmax in
let h = min h hmax in
let x,y = get_window_pos layout in
let canvas = Draw.init ?window ?name:layout.name ?x ?y ~w ~h () in
global_set_canvas top canvas
let adjust_window ?(display=false) layout =
Sync.push (fun () ->
let top = top_house layout in
if not (equal layout top)
then printd debug_error
"The layout for resizing window should be the top layout";
let w,h = get_physical_size top in
let win = window top in
printd debug_graphics "SDL set window size %d x %d" w h;
Draw.set_window_size win ~w ~h;
if display && Draw.window_is_shown (window top) then begin
render top;
flip top
end)
let push_close r =
let id = Sdl.get_window_id (window r) in
Trigger.push_close id
let destroy_window r =
match window_opt r with
| Some w ->
let window_id = Sdl.get_window_id w in
Trigger.push_destroy_window (r.id) ~window_id
| None ->
printd (debug_board + debug_error + debug_user)
"Cannot destroy window for layout %s because it is not associated with any \
SDL window." (sprint_id r)
let add_window r =
match window_opt r with
| Some w ->
printd (debug_error + debug_user + debug_board)
"Cannot create a new window with Room %s because it already belongs to an \
SDL window (id=%u)" (sprint_id r) (Sdl.get_window_id w)
| None -> Trigger.push_add_window (r.id)
let inside_geom geometry (x,y) =
x <= geometry.x + geometry.w && x >= geometry.x &&
y <= geometry.y + geometry.h && y >= geometry.y
let inside room (x,y) =
match room.mask with
| None -> inside_geom room.current_geom (x,y)
| Some mask ->
let x0,y0 = room.current_geom.x, room.current_geom.y in
let _,_,_,a = Draw.get_pixel_color mask ~x:(x-x0) ~y:(y-y0) in
a <> 0
(** get the smallest room (with Resident) containing point (x,y), or None *)
let rec over_focus x y t =
let g = to_current_geom t.geometry
in
if t.show && (inside_geom g (x,y))
then match t.content with
| Resident _ -> Some t
| Rooms h ->
list_check (fun r -> over_focus (x - g.x) (y - g.y) r) h
else None
let rec focus_list_old x y t =
let g = to_current_geom t.geometry in
if t.show && (inside_geom g (x,y)) then match t.content with
| Resident _ -> [ t ]
| Rooms h -> List.flatten (List.map (fun r -> focus_list_old (x - g.x) (y - g.y) r) h)
else []
let rec focus_list x y t =
if t.show && not (t.disabled) && (inside t (x,y)) then match t.content with
| Resident _ -> [ t ]
| Rooms list -> List.flatten (List.rev_map (focus_list x y) list)
else []
let top_focus x y t =
let flist = focus_list x y t in
printd debug_graphics "Number of layers detected under mouse: %u (%s)"
(List.length flist)
(String.concat " " (List.map (fun r -> "#" ^ (string_of_int r.id)) flist));
let compare r1 r2 = Chain.compare (get_layer r1) (get_layer r2) in
list_max compare flist
(** get the smallest room (with Rooms or Resident) containing (x,y), or None *)
let rec hover x y t =
let g = to_current_geom t.geometry in
if t.show && (inside_geom g (x,y)) then match t.content with
| Resident _ -> Some t
| Rooms h -> (match
list_check (fun r -> hover (x - g.x) (y - g.y) r) h
with
| None -> Some t
| o -> o)
else None