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
open Tsdl
open B_utils
module Widget = B_widget
module Avar = B_avar
module Chain = B_chain
module Theme = B_theme
module Time = B_time
module Var = B_var
module Tvar = B_tvar
module Trigger = B_trigger
module Sync = B_sync
module Draw = B_draw
module Mouse = B_mouse
module Style = B_style
module Box = B_box
module Slider = B_slider
type background =
| Solid of Draw.color
| Box of Box.t;;
let color_bg color =
Solid color
let bg_color = color_bg Draw.(opaque bg_color)
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 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; (** = parent: this is the "room" that contains this room in his "Rooms" *)
mutable mouse_focus : bool;
mutable keyboard_focus : bool option;
mutable draggable : bool;
};;
type t = room;;
exception Fatal_error of (t * string)
let not_specified = -66666;;
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;
remove_wtable room;
send_to_cemetery room;
end;;
let clear_wtable () = WHash.clear rooms_wtable;;
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) };;
(** create a new room *)
let create
?name
?(set_house = true) ?(adjust = Fit)
?(layer = Draw.get_current_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 room =
{
id;
name;
lock = Mutex.create ();
thread_id = Thread.(id (self ()));
show;
hidden = true;
adjust;
geometry;
current_geom = to_current_geom geometry;
clip;
mask;
background;
shadow;
content;
layer;
house;
keyboard_focus;
mouse_focus;
canvas;
draggable
} 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
printd debug_board "Layout %s created." (sprint_id room);
room;;
let dummy_room = create (geometry ()) (Rooms []);;
let of_id id : room =
try WHash.find rooms_wtable {dummy_room with id} with
| Not_found -> (printd debug_warning "Cannot find room with id=%d" id;
raise Not_found);;
let of_id_opt id : room option =
try Some (WHash.find rooms_wtable {dummy_room with id}) with
| Not_found -> (printd debug_warning "Cannot find room with id=%d" id;
None);;
let of_wid wid =
let w = Widget.of_id wid in
let id = Widget.get_room_id w in
of_id id;;
let get_rooms layout =
match layout.content with
| Resident _ ->
printd debug_error
"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 has_resident layout =
match layout.content with
| Resident _ -> true
| Rooms _ -> false;;
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 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 check_cemetery () =
let check id = try
let r = of_id id in
printd debug_memory "Dead room %s seems to be living. Beware of zombies." (sprint_id r);
false
with
| Not_found -> 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 =
printd debug_thread "Locking layout %s" (sprint_id l);
if Mutex.try_lock l.lock
then l.thread_id <- Thread.(id (self ()))
else
if Thread.(id (self ())) <> l.thread_id
then begin
Mutex.lock l.lock;
l.thread_id <- Thread.(id (self ()))
end
else printd debug_thread "Layout #%u was locked, but by the same thread: we continue." l.id;;
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;;
(** get the renderer of the layout *)
let renderer t = match t.canvas with
| Some c -> c.Draw.renderer
| _ -> failwith "Cannot get renderer because no canvas was defined";;
(** get the window of the layout *)
let window t = match t.canvas with
| Some c -> c.Draw.window
| _ -> begin
printd debug_error "Cannot get window for layout %s \
because no canvas was defined" (sprint_id t);
raise Not_found
end;;
(** return the top-level layout *)
let rec top_house layout =
match layout.house with
| None -> layout
| Some r -> top_house r;;
let get_content layout =
layout.content;;
(** is the layout representing a Sdl.Window ? it must be a member of the
top_house *)
let is_window layout =
match layout.house with
| None -> false
| Some r -> r.house = None;;
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));;
(** get current layer of layout *)
let get_layer l =
l.layer;;
(** test if layouts share the same layer (= same depth) *)
let same_layer l1 l2 =
Chain.(get_layer l1 == get_layer l2);;
(** get the layout background *)
let get_background l =
l.background;;
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 (Solid Draw.(opaque red)) else None in
match b with
| Solid _ -> ()
| Box b -> Box.unload b;
);;
let unload_background room =
do_option room.background (function
| Box b -> Box.unload b
| Solid _ -> ());;
let compute_background ?(mustlock = true) 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
| Solid c ->
let b = Box.(create ~width:g.w ~height:g.h
~background:(Style.Solid c) ()) in
if mustlock then lock room;
room.background <- (Some (Box b));
if mustlock then unlock room;
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 =
lock l;
unload_background l;
l.background <- b;
unlock l;;
let set_shadow l s =
lock l;
l.shadow <- s;
unlock l;;
(** 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;;
(** change width of layout, without adjusting parent house *)
let set_width ?(update_bg = false) l w =
if l.current_geom.w <> w then begin
lock l;
l.current_geom <- { l.current_geom with w };
Avar.set l.geometry.w w;
if update_bg && l.canvas <> None then compute_background ~mustlock:false l;
unlock l
end;;
(** get height *)
let height l =
l.current_geom.h;;
(** change height of layout, without adjusting parent house *)
let set_height l h =
lock l;
l.current_geom <- { l.current_geom with h };
Avar.set l.geometry.h h;
unlock l;;
(** get voffset *)
let get_voffset l =
Avar.get (Var.get l.geometry.voffset);;
(** get current absolute x position (relative to the top-left corner of the
window) *)
let xpos l =
l.current_geom.x;;
(** get current absolute y position *)
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;;
(** change x of layout, without adjusting parent house *)
let setx l x =
lock l;
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;
unlock l;;
(** change y of layout, without adjusting parent house *)
let sety l y =
lock l;
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;
unlock l;;
let set_voffset l vo =
lock l;
Avar.set (Var.get l.geometry.voffset) vo;
l.current_geom <- { l.current_geom with voffset = vo };
unlock l;;
let shift_voffset_generic vset l dv =
let av = Var.get l.geometry.voffset in
if Avar.finished av
then set_voffset l (Avar.get av + dv)
else let av_new = Avar.apply (fun y -> y + dv) av in
vset l.geometry.voffset av_new;;
let shift_voffset = shift_voffset_generic Var.set;;
let shift_voffset_unsafe = shift_voffset_generic Var.unsafe_set;;
let maximize l =
lock l;
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;
unlock l;
setx l 0;
sety l 0;;
let reset_pos l =
lock l;
let w,h = get_size l in
let g = geometry ~w ~h () in
l.geometry <- g;
l.current_geom <- to_current_geom g;
unlock l;;
let get_window_pos layout =
let f x = if x = not_specified then None else Some x in
f layout.current_geom.x, f layout.current_geom.y;;
let set_window_pos layout (x,y)=
let g = layout.current_geom in
layout.current_geom <- { g with x; y };;
let get_transform l =
let angle = Avar.get l.geometry.transform.angle in
let center = Avar.get l.geometry.transform.center in
let flip = Avar.get l.geometry.transform.flip in
let alpha = Avar.get l.geometry.transform.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 rec_set_show b l =
l.show <- b;
match l.content with
| Resident _ -> ()
| Rooms list -> List.iter (rec_set_show b) list;;
(** 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;;
(** get absolute position of the parent house *)
let house_pos room =
match room.house with
| None -> 0,0
| Some h -> compute_pos h;;
let rec iter f room =
f room;
match room.content with
| Resident _ -> ()
| Rooms list -> List.iter (iter f) list;;
(** iter through widgets *)
let rec iter_widgets f room =
match room.content with
| Resident w -> f w
| Rooms list -> List.iter (iter_widgets f) 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 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;;
exception Found of t;;
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;;
(** find room by id in the connected component of house *)
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) room =
match room.house with
| None ->
None
| Some h ->
let rooms = get_rooms h in
let first = List.hd rooms in
let rec loop list ok = match list with
| [] -> if ok then if circular then Some first else None
else failwith "Room should be in the list!"
| a::rest -> if ok then Some a else loop rest (equal a room) in
loop rooms false;;
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 ->
default_option (next h) (next_up h));;
let next_leaf room =
match room.content with
| Rooms []
| Resident _ -> begin
match next room with
| None ->
let h = default
(next_up room)
(printd debug_board "No next widget was found; \
we start again from top";
top_house room) in
first_room h
| Some n -> (match n.content with
| Resident _ -> n
| Rooms _ -> first_room n)
end
| Rooms _ -> first_room room;;
(** find the next visible room with a widget that can have keyboard_focus *)
let next_keyboard_old room =
let room = first_room room in
let visited = ref [] in
let rec loop r =
if List.mem r.id !visited
then (printd debug_error "Room %s already visited (this should not happen). Quitting search." (sprint_id r); assert(false))
else begin
visited := r.id :: !visited;
printd debug_board "Searching next room %s" (sprint_id r);
let n = next_leaf r in
if equal room n then (printd debug_board "Nothing found"; None)
else if n.keyboard_focus <> None && n.show
then (printd debug_board "Found %s" (sprint_id n); Some n)
else loop n
end in
loop room;;
let next_keyboard room =
let rec loop r visited =
if List.mem r.id visited then None
else
let n = next_leaf 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
then (printd (debug_board+debug_custom) "Found %s" (sprint_id n); Some n)
else loop n (r.id :: visited) in
loop room [];;
let unload_widget_textures room =
unload_background room;
iter_widgets Widget.unload_texture room;;
let unload_textures room =
let f r =
unload_background r;
match r.content with
| Resident w -> Widget.unload_texture w
| _ -> () in
iter f room;;
let delete_backgrounds room =
iter delete_background room;;
let delete_textures room =
unload_textures room;
delete_backgrounds room;;
let remove_canvas room =
delete_textures 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 (fun r ->
do_option r.house (fun h ->
if equal h l
then printd debug_warning "Room %s is now orphan" (sprint_id r))) list
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_width l g'.w;
set_height l g'.h;
do_option l.house fit_content
end;;
(** return the list of widgets used inside the layout *)
let rec get_widgets layout =
match layout.content with
| Rooms h -> List.flatten (List.map get_widgets h)
| Resident w -> [w];;
let has_keyboard_focus r =
r.keyboard_focus = Some true;;
(** set keyboard_focus if possible *)
let set_keyboard_focus r =
do_option r.keyboard_focus (fun b ->
if not b then begin
printd debug_board "Setting layout keyboard_focus";
r.keyboard_focus <- Some true;
match r.content with
| Rooms _ -> ()
| Resident w -> Widget.set_keyboard_focus w
end
);;
let rec remove_keyboard_focus r =
do_option r.keyboard_focus (fun b -> if b then r.keyboard_focus <- Some false);
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 "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 "Cannot claim keyboard_focus on room %s without \
resident." (sprint_id r)
let v_center layout y0 h =
match layout.content with
| Resident _ -> ()
| Rooms rs -> List.iter
(fun r -> let h0 = height r in
let y = Draw.center y0 h h0 in
sety r y)
rs;;
(** vertical align *)
let v_align ~align layout y0 h =
match layout.content with
| Resident _ -> ()
| Rooms rs -> List.iter
(fun r -> let h0 = height r in
let y = Draw.align align y0 h h0 in
sety r y)
rs;;
(** create a room (=layout) with a unique resident (=widget), no margin possible *)
let resident ?name ?(x = 0) ?(y = 0) ?w ?h ?background ?draggable ?canvas ?layer
?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 geometry (Resident widget) ?layer ?canvas;;
let of_widget = resident;;
let empty ?name ?background ~w ~h () =
let geometry = geometry ~w ~h () in
create ?name ?background geometry (Rooms []);;
(** create a horizontal layout (a "flat") from a list of widgets *)
let flat_of_w ?name ?(sep = Theme.room_margin) ?h ?align ?background ?widget_bg
?canvas widgets =
let rec loop list rooms (x,y) =
match list with
| [] -> rooms, (x,y)
| wg::rest ->
let name = map_option name (fun s -> "Resident of [" ^ s ^ "]") in
let room = resident ?name ?h ~x ~y:sep ?background:widget_bg ?canvas wg in
let w,h = get_size room in
loop rest (room::rooms) (x+sep+w, max y (h+2*sep)) in
let rooms, (w,h) = loop widgets [] (sep,sep) in
let geometry = geometry ~w ~h () in
let layout = create ?name ?background geometry (Rooms (List.rev rooms)) ?canvas in
List.iter (fun room -> room.house <- Some layout) rooms;
do_option align (fun align -> v_align ~align layout sep (h-2*sep));
layout;;
(** check if a sublayer is deeper (= below = Chain.<) than the main layer, which
(in principle) should not happen *)
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 =
lock room;
room.canvas <- Some canvas;
if !debug then check_layers room;
unlock room;;
(** Set the canvas for layout and all children *)
let global_set_canvas ?(mustlock=true) room canvas =
if mustlock then lock room;
iter (fun r -> r.canvas <- Some canvas) room;
if mustlock then unlock room;;
let check_layer_error room house =
if not (Chain.same_component room.layer house.layer)
then printd debug_error
"The replacement room %s belongs to a separate set of layers disjoint \
from the house %s. Beware that it will probably never be displayed"
(sprint_id room) (sprint_id house);;
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 (fun r ->
if r.house <> None then
(r.house <- None;
printd debug_warning "Room %s was detached from House %s" (sprint_id r) (sprint_id layout))) rooms;;
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;
printd debug_warning "Room %s was detached from House %s but we didn't check wether the House is not using the Room anymore..." (sprint_id room) (sprint_id h));;
let set_rooms layout ?(sync=true) 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
| _ ->
(if sync then Sync.push else run)
(fun () ->
List.iter (fun r ->
r.house <- Some layout;
check_layer_error r layout;
do_option layout.canvas (global_set_canvas ~mustlock:false r)) rooms;
layout.content <- Rooms rooms;
);;
let replace_rooms_NO layout rooms =
kill_rooms_NO layout;
set_rooms layout rooms;;
let copy ~src ~dst =
lock src;
lock 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;
unlock src;
unlock dst;;
let add_room ?valign ?halign ~dst room =
check_layer_error room dst;
lock dst;
lock room;
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 (map_option halign (fun a ->
Draw.align a 0 (width dst) (width room))) (getx room) in
let y = default (map_option valign (fun a ->
Draw.align a 0 (height dst) (height room))) (gety room) in
setx room x;
sety room y;
room.house <- Some dst;
do_option dst.canvas (global_set_canvas ~mustlock:false room);
let rooms = get_rooms dst in
dst.content <- Rooms (List.rev (room :: (List.rev rooms)));
unlock dst;
unlock room;;
let set_layer ?(debug = !debug) room layer =
lock room;
room.layer <- layer;
unlock room;
if debug then check_layers room;;
let global_set_layer room layer =
iter (fun r -> set_layer ~debug:false r layer) room;;
(** construct a horizontal house from a list of rooms *)
let flat ?name ?(sep = Theme.room_margin / 2) ?(adjust=Fit)
?(hmargin = Theme.room_margin) ?(vmargin = Theme.room_margin)
?margins ?align ?background ?shadow ?canvas rooms =
let sep, hmargin, vmargin = match margins with
| Some m -> m,m,m
| None -> sep, hmargin, vmargin in
let rec loop list x y =
match list with
| [] -> (x - sep + hmargin, y)
| r::rest ->
setx r x;
sety r vmargin;
loop rest (x + sep + (width r)) (max y ((height r) + 2*vmargin)) in
let w,h = loop rooms hmargin vmargin in
let layout = create ?name ?background ?shadow (geometry ~w ~h ()) ~adjust (Rooms rooms) ?canvas in
do_option align (fun align -> v_align ~align layout vmargin (h-2*vmargin));
layout;;
let hbox = flat;;
let h_center layout x0 w =
match layout.content with
| Resident _ -> ()
| Rooms rs -> List.iter
(fun r -> let w0 = width r in
let x = Draw.center x0 w w0 in
setx r x)
rs;;
let h_align ~align layout x0 w =
match layout.content with
| Resident _ -> ()
| Rooms rs -> List.iter
(fun r -> let w0 = width r in
let x = Draw.align align x0 w w0 in
setx r x)
rs;;
(** create a vertical layout ("tower") from a list of widgets *)
let tower_of_w ?name ?(sep = Theme.room_margin) ?align
?background ?widget_bg
?canvas widgets =
let rec loop list rooms (x,y) =
match list with
| [] -> rooms, (x,y)
| wg::rest ->
let room = resident ~x:sep ~y ?background:widget_bg ?canvas wg in
let w,h = get_size room in
loop rest (room::rooms) (max x (w+2*sep), y+sep+h) in
let rooms, (w,h) = loop widgets [] (sep,sep) in
let layout = create (geometry ~w ~h ()) ?name ?background (Rooms (List.rev rooms)) ?canvas in
do_option align (fun align -> h_align ~align layout sep (w-2*sep));
layout;;
(** create a tower from a list of rooms *)
let tower ?name ?(sep = Theme.room_margin/2) ?margins
?(hmargin = Theme.room_margin) ?(vmargin = Theme.room_margin)
?align ?(adjust = Fit) ?background ?shadow ?canvas rooms =
let sep, hmargin, vmargin = match margins with
| Some m -> m,m,m
| None -> sep, hmargin, vmargin in
let rec loop list x y =
match list with
| [] -> (x, y - sep + vmargin)
| r::rest ->
setx r hmargin;
sety r y;
loop rest (max x ((width r) + 2*hmargin)) (y + sep + (height r)) in
let w,h = loop rooms hmargin vmargin in
let layout = create ~adjust ?name ?background ?shadow
(geometry ~w ~h ()) (Rooms rooms) ?canvas in
do_option align (fun align -> h_align ~align layout hmargin (w-2*hmargin));
layout;;
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, ymax-ymin)
| room :: rest ->
let x,y = getx room, gety room in
loop
(imin xmin x)
(imin ymin y)
(imax xmax (width room + x))
(imax ymax (height room + y))
rest in
loop max_int max_int 0 0 rooms;;
let superpose ?w ?h ?name ?background ?canvas 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 geometry = geometry ~x ~y ~w:(default w bw) ~h:(default h bh) () in
let layer = match rooms with
| [] -> Draw.get_current_layer ()
| r::_ -> get_layer r in
create ?name ~layer ?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 =
lock room;
let g = room.geometry in
Avar.stop g.x;
room.geometry <- { g with x };
unlock room;;
let animate_y room y =
lock room;
let g = room.geometry in
Avar.stop g.y;
room.geometry <- { g with y };
unlock room;;
let animate_w room w =
lock room;
let g = room.geometry in
Avar.stop g.w;
room.geometry <- { g with w };
unlock room;;
let animate_h room h =
lock room;
let g = room.geometry in
Avar.stop g.h;
room.geometry <- { g with h };
unlock room;;
let animate_voffset room voffset =
lock room;
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);
unlock room;;
let animate_alpha room alpha =
lock room;
let g = room.geometry in
Avar.stop g.transform.alpha;
room.geometry <- { g with transform = { g.transform with alpha }};
unlock room;
ask_update room;;
let animate_angle room angle =
lock room;
let g = room.geometry in
Avar.stop g.transform.angle;
room.geometry <- { g with transform = { g.transform with angle }};
unlock room;;
let stop_pos room =
printd debug_graphics "Stop position animation for layout %s." (sprint_id room);
lock room;
let g = room.geometry in
Avar.stop g.x;
Avar.stop g.y;
unlock room;;
(** 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 printd (debug_board + debug_warning)
"Room %s is already shown, we don't run the show animation"
(sprint_id room)
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' = 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;
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 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 from_alpha (get_alpha room) in
let ending _ =
printd debug_board "End of complete fade_out => hiding 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 ?from ~dst room =
let x,y = Avar.slide_in ?from ~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 dx (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 dy (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;;
(** clip a room inside a smaller container and make it scrollable, and
optionally add a scrollbar widget *)
let make_clip ?w ?(scrollbar = true) ?(scrollbar_inside = false)
?(scrollbar_width = 10)
~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 y0 = gety room in
sety room 0;
let active_bg = Widget.empty ~w:(width room) ~h:(height room) () in
let container = tower ~sep:0 ~hmargin:0 ~vmargin:0
[superpose [room; resident active_bg ]] in
set_height container h;
set_width container w;
set_clip container;
let result =
if scrollbar && h < (height room)
then let var = Tvar.create container.geometry.voffset
~t_from:(fun a -> height room - h + Avar.get a)
~t_to:(fun v -> Avar.var (h - (height room) + v)) in
let bar = resident ~background:(Solid Draw.(lighter scrollbar_color))
(Widget.slider ~kind:Slider.Vertical ~length:h ~thickness:scrollbar_width
~tick_size:(h * h / (height room))
~var (height room - h)) in
if scrollbar_inside
then (setx bar (width container - width bar);
set_layer bar (Chain.insert_after (Chain.last (get_layer container)) (Draw.new_layer ()));
superpose ~name [container; bar])
else flat ~name ~sep:0 ~hmargin:0 ~vmargin:0 [container; bar]
else container in
sety result y0;
let x0 = getx room in
setx room 0;
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 () = ignore (createfn rooms) in
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 ~hmargin ~vmargin ?margins ?align rooms);;
let retower ?align
?(hmargin = Theme.room_margin) ?(vmargin = Theme.room_margin) ?margins =
relayout (fun rooms -> tower ~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 ?house ~by room =
match room.house, house with
| None, None -> printd debug_error
"Cannot use \"replace_room\" because room %s \
does not belong to a house." (sprint_id room)
| _, Some h
| Some h, None -> begin
printd debug_warning "Replacing room %s by room %s"
(sprint_id room) (sprint_id by);
lock h;
lock by;
let rooms_before_rev, rooms_after =
let rec loop rb = function
| [] ->
failwith "Room not found in the house, this should not happen!"
| r :: rest -> if equal r room
then rb, rest
else loop (r::rb) rest in
loop [] (get_rooms h) in
by.house <- Some h;
iter (fun r -> r.canvas <- room.canvas) by;
h.content <-
Rooms (List.rev_append (by::rooms_before_rev) rooms_after);
unlock by;
unlock h
end;;
let relocate ~dst ?(scroll=true) room =
let house = room.house in
let x0,y0 = compute_pos dst in
let x1,y1 = compute_pos room in
do_option house
(fun h ->
let rooms = List.filter (fun r -> not (r == room)) (get_rooms h) in
h.content <- Rooms rooms
);
let room = if not scroll then room
else 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);
if y2 <= height dst then room
else begin
make_clip ~h:(height dst - y1 + y0) ~scrollbar_inside:true
~scrollbar_width:4 room
end in
add_room ~dst room;
setx room (x1-x0);
sety room (y1-y0);
room;;
(** 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
let rect = Draw.rect_to_layer ?bg ~color (get_canvas room) (get_layer room) (x,y) w h in
Draw.forget_texture rect.Draw.texture;
rect;;
let scale_clip clip =
map_option clip (fun c ->
Sdl.Rect.(create
~x:(Theme.scale_int (x c))
~y:(Theme.scale_int (y c))
~h:(Theme.scale_int (h c))
~w:(Theme.scale_int (w c))));;
(** Display a room: *)
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
| Solid c ->
let b = Box.(create ~width:g.w ~height:g.h
~background:(Style.Solid c)
?shadow:r.shadow ()) in
lock r;
r.background <- (Some (Box b));
unlock r;
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
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
let clip = sclip in
blit_to_layer { blit with clip; 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
let clip = sclip in
blit_to_layer { rect with clip; 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 = B_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
List.iter
Draw.blit_to_layer
(B_label.display (get_canvas r) (get_layer r) label geom)
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_fn 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 ?(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 = Sdl.get_window_size (window top)
|> Draw.unscale_pos in
let w' = width top
and h' = height top in
if (w',h') <> (w,h)
then begin
printd debug_graphics "Resize (%d,%d) --> (%d,%d)" w' h' w h;
set_width top w;
set_height top 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";
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;;
(** adjust the window size to the top layout *)
let adjust_window ?(display=true) 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;
Sdl.set_window_size win ~w ~h;
resize ~flip:display top;
if display && Draw.window_is_shown (window top) then begin
render top;
flip top
end);;
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 && (inside t (x,y)) then match t.content with
| Resident _ -> [ t ]
| Rooms h -> List.flatten (List.map (fun r -> focus_list x y r) h)
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;;