1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718
|
/**
* @file
* @brief Menus and associated malarkey.
**/
#include "AppHdr.h"
#include "menu.h"
#include <cctype>
#include <functional>
#include "cio.h"
#include "colour.h"
#include "command.h"
#include "coord.h"
#include "env.h"
#include "tile-env.h"
#include "hints.h"
#include "invent.h"
#include "libutil.h"
#include "macro.h"
#include "message.h"
#ifdef USE_TILE
#include "mon-util.h"
#endif
#include "options.h"
#include "player.h"
#include "player-save-info.h"
#include "state.h"
#include "stringutil.h"
#ifdef USE_TILE
#include "terrain.h"
#endif
#ifdef USE_TILE_LOCAL
#include "tilebuf.h"
#endif
#ifdef USE_TILE
#include "tiledoll.h"
#include "tile-flags.h"
#include "tile-player-flag-cut.h"
#include "rltiles/tiledef-dngn.h"
#include "rltiles/tiledef-icons.h"
#include "rltiles/tiledef-main.h"
#include "rltiles/tiledef-player.h"
#endif
#ifdef USE_TILE_LOCAL
#include "tilefont.h"
#endif
#ifdef USE_TILE
#include "tilepick.h"
#include "tilepick-p.h"
#endif
#ifdef USE_TILE_LOCAL
#include "tilereg-crt.h"
#endif
#ifdef USE_TILE
#include "travel.h"
#endif
#include "ui.h"
#include "unicode.h"
#include "unwind.h"
#ifdef USE_TILE_LOCAL
#include "windowmanager.h"
#endif
// Menu and subclasses: one of the main higher-level UI elements in dcss. The
// inventory screen is a prototypical use of this style of menu.
//
// Class hierarchy for Menu (many of these also subclass MenuEntry):
//
// Menu (menu.cc/h): possible to use directly, but rare, e.g. y/n prompts
// | \-ToggleableMenu (menu.cc/h): ability menu, various deck menus
// | \-SpellMenu (spl-cast.cc): spell casting / spell view menu
// |
// |\--PromptMenu (prompt.h): menus that can show in both the message pane
// prompt channel, and as a regular menu
// |
// |\--InvMenu (invent.cc/h): most inventory/inv selection menus aside from
// | | what the subclasses do. (Including take off
// | | jewellery and remove armour.)
// | |\--AcquireMenu (acquire.cc): acquirement
// | |\--UseItemMenu (item-use.cc): scrolls, potions, put on jewellery, wear
// | | armour, wield, scroll item selection
// | | (e.g. brand, enchant)
// | |\--KnownMenu (known-items.cc): known/unknown items
// | |\--DescMenu (directn.cc): xv and ctrl-x menus, help lookup
// | \---ShopMenu (shopping.cc): buying in shops
// |
// |\--ShoppingListMenu (shopping.cc): shopping lists (`$`)
// |\--StashSearchMenu (stash.cc): stash searches (ctrl-f)
// |\--ActionSelectMenu (quiver.cc): quiver action selection
// |\--SpellLibraryMenu (spl-book.cc): memorize menu
// |\--GameMenu (main.cc): game menu
// |\--MacroEditMenu (macro.cc): macro editing overall menu
// |\--MappingEditMenu (macro.cc): single macro mapping editing
// |\--LookupHelpMenu (lookup-help.cc): help lookup, e.g. `?/`
// |\--DescMenu (lookup-help.cc): submenu for choosing between search results
// |\--MutationMenu (mutation.cc): view info about mutations (`A`)
// \---StackFiveMenu (decks.cc): menu for the stack five ability
//
// Unfortunately, PrecisionMenu is something entirely different (used for the
// skill menu), as is OuterMenu (local builds main menu, character/background
// selection, hi scores). PrecisionMenu is deprecated.
using namespace ui;
class UIMenu : public Widget
{
friend class UIMenuPopup;
public:
UIMenu(Menu *menu) : m_menu(menu), m_num_columns(1)
#ifdef USE_TILE_LOCAL
, m_font_entry(tiles.get_crt_font()), m_text_buf(m_font_entry)
#endif
{
#ifdef USE_TILE_LOCAL
// this seems ... non-ideal?? (pattern occurs in a few other places,
// precision menu, playerdoll, ...)
const ImageManager *image = tiles.get_image_manager();
for (int i = 0; i < TEX_MAX; i++)
m_tile_buf[i].set_tex(&image->get_texture(static_cast<TextureID>(i)));
#else
expand_h = true;
#endif
};
~UIMenu() {};
virtual void _render() override;
virtual SizeReq _get_preferred_size(Direction dim, int prosp_width) override;
virtual void _allocate_region() override;
int get_num_columns() const { return m_num_columns; };
void set_num_columns(int n) {
m_num_columns = n;
_invalidate_sizereq();
_queue_allocation();
};
#ifdef USE_TILE_LOCAL
virtual bool on_event(const Event& event) override;
#endif
void set_min_col_width(int w) { m_min_col_width = w; } // XX min height?
int get_min_col_width() { return m_min_col_width; }
void set_initial_scroll(int i) { m_force_scroll = i; }
int get_scroll_context() { return m_scroll_context + m_scroll_context / 2; }
void update_item(int index);
void update_items();
void set_hovered_entry(int i);
void is_visible_item_range(int *vis_min, int *vis_max);
void get_item_region(int index, int *y1, int *y2);
void get_item_gridpos(int index, int *row, int *col);
#ifndef USE_TILE_LOCAL
void set_showable_height(int h)
{
m_shown_height = h;
_invalidate_sizereq();
}
#endif
protected:
Menu *m_menu;
int m_height; // set by do_layout()
int m_hover_idx = -1;
int m_real_hover_idx = -1;
int m_min_col_width = -1;
int m_force_scroll = -1; // in rows, no pixels
bool m_initial_hover_snap = false;
int m_scroll_context = 0;
int m_num_columns = 1;
int m_nat_column_width; // set by do_layout()
void do_layout(int mw, int num_columns, bool just_checking=false);
struct MenuItemInfo {
int x, y, row, column;
formatted_string text;
#ifdef USE_TILE_LOCAL
vector<tile_def> tiles;
#endif
bool heading;
};
vector<MenuItemInfo> item_info;
#ifdef USE_TILE_LOCAL
int get_max_viewport_height();
vector<int> row_heights;
bool m_mouse_pressed = false;
int m_mouse_x = -1, m_mouse_y = -1;
void update_hovered_entry(bool force=false);
void mark_buffers_dirty();
void pack_buffers();
bool m_buffers_dirty = false;
bool m_draw_tiles;
FontWrapper *m_font_entry;
ShapeBuffer m_shape_buf;
LineBuffer m_line_buf, m_div_line_buf;
FontBuffer m_text_buf;
FixedVector<TileBuffer, TEX_MAX> m_tile_buf;
public:
size_t shown_items() { return item_info.size(); }
static constexpr int item_pad = 2;
static constexpr int pad_right = 10;
#else
static constexpr int pad_right = 2; // no vertical padding for console
int m_shown_height {0};
#endif
};
void UIMenu::update_items()
{
_invalidate_sizereq();
item_info.resize(m_menu->items.size());
do_layout(m_region.width, m_num_columns, true);
for (unsigned int i = 0; i < m_menu->items.size(); ++i)
update_item(i);
#ifdef USE_TILE_LOCAL
// update m_draw_tiles
m_draw_tiles = false;
for (const auto& entry : item_info)
if (!entry.heading && !entry.tiles.empty())
{
m_draw_tiles = Options.tile_menu_icons;
break;
}
#endif
}
void UIMenu::is_visible_item_range(int *vis_min, int *vis_max)
{
const int viewport_height = m_menu->m_ui.scroller->get_region().height;
const int scroll = m_menu->m_ui.scroller->get_scroll();
int v_min = 0, v_max = item_info.size(), i;
#ifdef USE_TILE_LOCAL
for (i = 0; i < (int)item_info.size(); ++i)
{
if (row_heights[item_info[i].row + 1] > scroll)
{
v_min = i;
break;
}
}
for (; i < (int)item_info.size(); ++i)
{
if (row_heights[item_info[i].row] >= scroll + viewport_height)
{
v_max = i;
break;
}
}
#else
for (i = 0; i < (int)item_info.size(); ++i)
{
if (item_info[i].row >= scroll)
{
v_min = i;
break;
}
}
for (; i < (int)item_info.size(); ++i)
{
if (item_info[i].row >= scroll + viewport_height)
{
v_max = i;
break;
}
}
#endif
v_max = min(v_max, (int)m_menu->items.size()); // ??
if (vis_min)
*vis_min = v_min;
if (vis_max)
*vis_max = v_max;
}
void UIMenu::get_item_gridpos(int index, int *row, int *col)
{
ASSERT_RANGE(index, 0, (int)m_menu->items.size());
if (item_info.empty())
{
// no layout yet
if (row)
*row = index;
if (col)
*col = 0;
return;
}
ASSERT_RANGE(index, 0, (int)item_info.size());
// XX why not just expose MenuItemInfo
if (row)
*row = item_info[index].row;
if (col)
*col = item_info[index].column;
}
void UIMenu::get_item_region(int index, int *y1, int *y2)
{
ASSERT_RANGE(index, 0, (int)m_menu->items.size());
int row = -1;
// use just the index in an uninitialized menu
if (item_info.size() != m_menu->items.size())
row = index;
else
row = item_info[index].row;
#ifdef USE_TILE_LOCAL
if (static_cast<size_t>(row + 1) >= row_heights.size())
{
// call before UIMenu has been laid out
if (y1)
*y1 = -1;
if (y2)
*y2 = -1;
return;
}
if (y1)
*y1 = row_heights[row];
if (y2)
*y2 = row_heights[row+1];
#else
if (y1)
*y1 = row;
if (y2)
*y2 = row+1;
#endif
}
void UIMenu::update_item(int index)
{
_invalidate_sizereq();
_queue_allocation();
ASSERT(index < static_cast<int>(m_menu->items.size()));
const MenuEntry *me = m_menu->items[index];
int colour = m_menu->item_colour(me);
string text = me->get_text();
item_info.resize(m_menu->items.size());
auto& entry = item_info[index];
entry.text.clear();
entry.text.textcolour(colour);
entry.text += formatted_string::parse_string(text);
entry.heading = me->level == MEL_TITLE || me->level == MEL_SUBTITLE;
#ifdef USE_TILE_LOCAL
entry.tiles.clear();
me->get_tiles(entry.tiles);
#endif
}
#ifdef USE_TILE_LOCAL
static bool _has_hotkey_prefix(const string &s)
{
// Don't read out of bounds!
if (s.size() < 5)
return false;
// [enne] - Ugh, hack. Maybe MenuEntry could specify the
// presence and length of this substring?
bool let = (s[1] >= 'a' && s[1] <= 'z' || s[1] >= 'A' && s[1] <= 'Z');
bool plus = (s[3] == '-' || s[3] == '+' || s[3] == '#');
return let && plus && s[0] == ' ' && s[2] == ' ' && s[4] == ' ';
}
#endif
void UIMenu::do_layout(int mw, int num_columns, bool just_checking)
{
#ifdef USE_TILE_LOCAL
const int min_column_width = m_min_col_width > 0 ? m_min_col_width : 400;
const int max_column_width = mw / num_columns;
const int text_height = m_font_entry->char_height();
int column = -1; // an initial increment makes this 0
int column_width = 0;
int row_height = 0;
int height = 0;
int row_count = 0;
// if the row heights are completely uninitialized, we should put something
// in there
just_checking = just_checking && !row_heights.empty();
if (!just_checking)
{
row_heights.clear();
row_heights.reserve(m_menu->items.size()+1);
}
for (size_t i = 0; i < m_menu->items.size(); ++i)
{
auto& entry = item_info[i];
column = entry.heading ? 0 : (column+1) % num_columns;
if (column == 0)
{
row_height += row_height == 0 ? 0 : 2*item_pad;
m_scroll_context = max(m_scroll_context, row_height);
height += row_height;
if (!just_checking)
row_heights.push_back(height);
row_count++;
row_height = 0;
}
const int text_width = m_font_entry->string_width(entry.text);
if (!just_checking)
entry.y = height;
entry.row = row_count - 1;
entry.column = column;
if (entry.heading)
{
entry.x = 0;
// extra space here is used for divider line and padding; note that
// we only want top padding if we're not the first item, since the
// popup and the more already have padding.
row_height = text_height + (i == 0 ? 5 : 10);
// wrap titles to two lines if they don't fit
if (m_draw_tiles && text_width > mw)
{
formatted_string split = m_font_entry->split(entry.text, mw, UINT_MAX);
row_height = max(row_height, (int)m_font_entry->string_height(split));
}
column = num_columns-1;
}
else
{
const int text_indent = m_draw_tiles ? 38 : 0;
entry.x = text_indent;
int text_sx = text_indent;
int item_height = max(text_height, !entry.tiles.empty() ? 32 : 0);
// Split menu entries that don't fit into a single line into two lines.
if (!m_menu->is_set(MF_NO_WRAP_ROWS))
{
if ((text_width > max_column_width-entry.x-pad_right))
{
formatted_string text;
// TODO: refactor to use _get_text_preface
if (_has_hotkey_prefix(entry.text.tostring()))
{
formatted_string header = entry.text.chop(5);
text_sx += m_font_entry->string_width(header);
text = entry.text;
// remove hotkeys. As Enne said above, this is a monstrosity.
for (int k = 0; k < 5; k++)
text.del_char();
}
else
text += entry.text;
int w = max_column_width - text_sx - pad_right;
formatted_string split = m_font_entry->split(text, w, UINT_MAX);
int string_height = m_font_entry->string_height(split);
string_height = min(string_height, text_height*2);
item_height = max(item_height, string_height);
}
}
column_width = max(column_width, text_sx + text_width + pad_right);
row_height = max(row_height, item_height);
}
}
row_height += row_height == 0 ? 0 : 2*item_pad;
m_scroll_context = max(m_scroll_context, row_height);
height += row_height;
if (!just_checking)
row_heights.push_back(height);
column_width += 2*item_pad;
m_height = height;
m_nat_column_width = max(min_column_width, min(column_width, max_column_width));
#else
UNUSED(just_checking);
// TODO: this code is not dissimilar to the tiles code, could they be
// further unified?
const int min_column_width = m_min_col_width > 0 ? m_min_col_width : 10;
const int max_column_width = mw / num_columns;
int row = -1;
int column = -1; // an initial increment makes these 0
int column_width = 0;
for (size_t i = 0; i < m_menu->items.size(); ++i)
{
auto& entry = item_info[i];
// headings occupy the entire row
column = entry.heading ? 0 : (column+1) % num_columns;
if (column == 0)
row++;
const int text_width = static_cast<int>(entry.text.width());
entry.x = 0;
entry.y = row;
entry.row = row;
entry.column = column;
if (entry.heading)
column = num_columns-1; // headings occupy the entire row, skip the rest
else
{
// TODO(?): tiles will wrap menu entries that don't fit in a single
// row, should that be implemented for console?
column_width = max(column_width, text_width + pad_right);
}
}
m_height = row + 1;
// should this update the region??
m_nat_column_width = max(min_column_width, min(column_width, max_column_width));
#endif
}
#ifdef USE_TILE_LOCAL
int UIMenu::get_max_viewport_height()
{
// Limit page size to ensure <= 52 items visible
int max_viewport_height = INT_MAX;
size_t a = 0, b = 0, num_items = 0;
while (b < item_info.size())
{
if (num_items < 52)
num_items += !item_info[b++].heading;
else if (num_items == 52)
{
int item_h = row_heights[item_info[b].row] - row_heights[item_info[b-1].row];
int delta = item_h + item_info[b-1].y - item_info[a].y;
max_viewport_height = min(max_viewport_height, delta);
do
{
num_items -= !item_info[a++].heading;
}
while (item_info[a].column != 0);
}
}
return max_viewport_height;
}
#endif
void UIMenu::_render()
{
#ifdef USE_TILE_LOCAL
if (m_buffers_dirty)
{
pack_buffers();
m_buffers_dirty = false;
}
GLW_3VF t = {(float)m_region.x, (float)m_region.y, 0}, s = {1, 1, 1};
glmanager->set_transform(t, s);
m_shape_buf.draw();
m_div_line_buf.draw();
for (int i = 0; i < TEX_MAX; i++)
m_tile_buf[i].draw();
m_text_buf.draw();
m_line_buf.draw();
glmanager->reset_transform();
#else
int vis_min, vis_max;
is_visible_item_range(&vis_min, &vis_max);
const int scroll = m_menu->m_ui.scroller->get_scroll();
for (int i = vis_min; i < vis_max; i++)
{
const MenuEntry *me = m_menu->items[i];
int y = item_info[i].y - item_info[vis_min].y + 1;
int x = item_info[i].x + item_info[i].column * m_nat_column_width;
cgotoxy(m_region.x + x + 1, m_region.y + scroll + y);
const int col = m_menu->item_colour(me);
textcolour(col);
// TODO: is this highlighting good enough for accessibility purposes?
if (m_hover_idx == i)
textbackground(default_hover_colour());
// XX these will format the hover differently.
// TODO: multiline entries
if (m_menu->get_flags() & MF_ALLOW_FORMATTING)
{
formatted_string s = formatted_string::parse_string(
me->get_text(), col);
// headings always occupy full width
if (item_info[i].heading)
s.chop(m_region.width).display();
else
s.chop(m_nat_column_width).display();
}
else
{
string text = me->get_text();
// headings always occupy full width
if (item_info[i].heading)
text = chop_string(text, m_region.width);
else
text = chop_string(text, m_nat_column_width);
cprintf("%s", text.c_str());
}
textbackground(BLACK);
}
#endif
}
SizeReq UIMenu::_get_preferred_size(Direction dim, int prosp_width)
{
#ifdef USE_TILE_LOCAL
if (!dim)
{
do_layout(INT_MAX, m_num_columns, true);
const int em = tiles.get_crt_font()->char_width();
int max_menu_width = min(93*em, m_nat_column_width * m_num_columns);
return {0, max_menu_width};
}
else
{
do_layout(prosp_width, m_num_columns, true);
return {0, m_height};
}
#else
UNUSED(prosp_width);
if (!dim)
return {0, 80};
else
return {1, max({1, (int)m_menu->items.size(), m_shown_height})};
#endif
}
class UIMenuScroller : public Scroller
{
public:
UIMenuScroller() : Scroller() {};
virtual ~UIMenuScroller() {};
virtual void _allocate_region() override {
m_child->set_allocation_needed();
Scroller::_allocate_region();
};
};
class UIMenuMore : public Text
{
public:
UIMenuMore() : using_template(false)
{}
virtual ~UIMenuMore() {}
void set_text_immediately(const formatted_string &fs)
{
m_text.clear();
m_text += fs;
_expose();
m_wrapped_size = Size(-1);
// XX this doesn't handle height changes. I think it's partly a
// sequencing issue: previous code will correctly invalidate on a
// height change, but m_region isn't updated until later, so you get
// the wrong wrapping behavior + a lack of reflowing until the next
// update.
wrap_text_to_size(m_region.width, m_region.height);
}
void set_more_template(const string &scroll, const string &noscroll)
{
// a string that can have `XXX` in it somewhere, to be replaced by a
// scroll position
// TODO: might be simpler just to call get_keyhelp directly?
const bool diff = !using_template || text_for_scrollable != scroll
|| text_for_unscrollable != noscroll;
if (diff)
{
text_for_scrollable = scroll;
text_for_unscrollable = noscroll;
_invalidate_sizereq();
_queue_allocation();
}
if (!using_template)
{
using_template = true;
set_from_template(true, 0);
}
}
void set_from_template(bool scrollable, int scroll_percent)
{
if (!using_template)
return;
string more_template = scrollable
? text_for_scrollable
: text_for_unscrollable;
const string perc = scroll_percent <= 0 ? "top"
: scroll_percent >= 100 ? "bot"
: make_stringf("%2d%%", scroll_percent);
more_template = replace_all(more_template, "XXX", perc);
set_text_immediately(formatted_string::parse_string(more_template));
}
#ifdef USE_TILE_WEB
void webtiles_write_more()
{
// assumes an open object
if (using_template)
{
tiles.json_write_string("more", text_for_scrollable);
tiles.json_write_string("alt_more", text_for_unscrollable);
}
else
{
const string shown_more = get_text().to_colour_string(LIGHTGRAY);
tiles.json_write_string("more", shown_more);
tiles.json_write_string("alt_more", shown_more);
}
}
#endif
bool using_template;
private:
string text_for_scrollable;
string text_for_unscrollable;
};
class UIMenuPopup : public ui::Popup
{
public:
UIMenuPopup(shared_ptr<Widget> child, Menu *menu) : ui::Popup(child), m_menu(menu) {};
virtual ~UIMenuPopup() {};
int _calc_columns(int mw) const;
virtual void _allocate_region() override;
private:
Menu *m_menu;
};
/// What is the max number of columns that will fit into mw, given the current
/// items?
int UIMenuPopup::_calc_columns(int mw) const
{
int min_column_width = m_menu->m_ui.menu->get_min_col_width();
if (min_column_width <= 0)
#ifdef USE_TILE_LOCAL
min_column_width = m_menu->m_ui.menu->m_font_entry->char_width() * 10;
#else
min_column_width = 10;
#endif
#ifdef USE_TILE_LOCAL
// TODO: some code overlap with do_layout
int max_entry_text = min_column_width;
for (size_t i = 0; i < m_menu->items.size(); ++i)
{
auto& entry = m_menu->m_ui.menu->item_info[i];
const int text_width = entry.x
+ m_menu->m_ui.menu->m_font_entry->string_width(entry.text)
+ m_menu->m_ui.menu->pad_right;
max_entry_text = max(max_entry_text, text_width);
}
#else
int max_entry_text = min_column_width;
for (size_t i = 0; i < m_menu->items.size(); ++i)
{
max_entry_text = max(max_entry_text,
static_cast<int>(m_menu->items[i]->get_text().size())
+ m_menu->m_ui.menu->pad_right);
}
#endif
// the 6 here is somewhat heuristic, but menus with more columns than
// around this, in my testing, become harder to parse
return max(1, min(6, mw / max_entry_text));
}
void UIMenuPopup::_allocate_region()
{
Popup::_allocate_region();
int max_height = m_menu->m_ui.popup->get_max_child_size().height;
max_height -= m_menu->m_ui.title->get_region().height;
max_height -= m_menu->m_ui.title->get_margin().bottom;
max_height -= m_menu->m_ui.more->get_region().height;
int viewport_height = m_menu->m_ui.scroller->get_region().height;
int menu_w = m_menu->m_ui.menu->get_region().width;
m_menu->m_ui.menu->do_layout(menu_w, 1);
// see if we should try a two-column layout
int m_height = m_menu->m_ui.menu->m_height;
int more_height = m_menu->m_ui.more->get_region().height;
int num_cols = m_menu->m_ui.menu->get_num_columns();
if (m_menu->is_set(MF_GRID_LAYOUT))
{
const int max_columns = _calc_columns(menu_w);
if (num_cols != max_columns)
{
// TODO: it might be good to horizontally shrink grid layout
// menus a bit if the calculated column count would add too much
// whitespace. But I'm not quite sure how to do this...
m_menu->m_ui.menu->set_num_columns(max_columns);
ui::restart_layout(); // NORETURN
}
}
else if (m_menu->is_set(MF_USE_TWO_COLUMNS))
{
// XX should this be smarter about width for console?
if ((num_cols == 1 && m_height+more_height > max_height)
|| (num_cols == 2 && m_height+more_height <= max_height))
{
m_menu->m_ui.menu->set_num_columns(3 - num_cols);
ui::restart_layout(); // NORETURN
}
}
m_menu->m_ui.menu->do_layout(menu_w, num_cols);
if (m_menu->m_keyhelp_more)
{
// note: m_menu->m_ui.menu->get_region().height seems to be inaccurate
// on console (ok on tiles) -- is this a problem?
const int menu_height = m_menu->m_ui.menu->m_height;
const int scroll_percent = (menu_height - viewport_height == 0)
? 0
: m_menu->m_ui.scroller->get_scroll() * 100
/ (menu_height - viewport_height);
m_menu->m_ui.more->set_from_template(menu_height > max_height, scroll_percent);
// XX what if this changes the # of lines
}
// is the more invisible but has some text? This can happen either on
// initial layout, or if the keyhelp changes depending on whether there's
// a scrollbar.
const bool more_visible = m_menu->m_ui.more->is_visible();
const bool more_has_text = !m_menu->m_ui.more->get_text().ops.empty();
if (more_visible != more_has_text)
{
// TODO: for some reason if this is initially set to false, on the
// first render, toggling it to true doesn't work. (Workaround: always
// start true.) I think it might be an issue with the more changing
// height not working correctly?
m_menu->m_ui.more->set_visible(!more_visible);
_invalidate_sizereq();
m_menu->m_ui.more->_queue_allocation();
ui::restart_layout();
}
// adjust maximum height
#ifdef USE_TILE_LOCAL
const int max_viewport_height = m_menu->m_ui.menu->get_max_viewport_height();
#else
const int max_viewport_height = 52;
#endif
m_menu->m_ui.scroller->max_size().height = max_viewport_height;
if (max_viewport_height < viewport_height)
{
m_menu->m_ui.scroller->_invalidate_sizereq();
m_menu->m_ui.scroller->_queue_allocation();
ui::restart_layout();
}
}
void UIMenu::_allocate_region()
{
// Do some initial setup that requires higher-level calls but can't happen
// until the menu entry heights are known
if (m_force_scroll >= 0)
{
m_menu->set_scroll(m_force_scroll);
m_force_scroll = -1;
}
if (!m_initial_hover_snap)
{
// TODO: because it is initial this does the minimum snap to ensure
// the entry is visible, which can leave it the last on the page. This
// is exactly what we want for things like pgdn, but it may not be
// what a caller wants for an initial snap -- maybe do some extra
// scrolling for this case? (A caller can manually set the scroll, but
// this is tricky for some use cases because they won't know the menu
// height.)
if (m_menu->last_hovered >= 0)
m_menu->snap_in_page(m_menu->last_hovered);
m_initial_hover_snap = true;
}
do_layout(m_region.width, m_num_columns);
#ifdef USE_TILE_LOCAL
if (!(m_menu->flags & MF_ARROWS_SELECT) || m_menu->last_hovered < 0)
update_hovered_entry();
else
m_hover_idx = m_menu->last_hovered;
mark_buffers_dirty();
#endif
}
void UIMenu::set_hovered_entry(int i)
{
m_hover_idx = i;
#ifdef USE_TILE_LOCAL
mark_buffers_dirty();
#endif
_expose();
}
#ifdef USE_TILE_LOCAL
void UIMenu::update_hovered_entry(bool force)
{
const int x = m_mouse_x - m_region.x,
y = m_mouse_y - m_region.y;
int vis_min, vis_max;
is_visible_item_range(&vis_min, &vis_max);
for (int i = vis_min; i < vis_max; ++i)
{
const auto& entry = item_info[i];
if (entry.heading)
continue;
const auto me = m_menu->items[i];
if (me->hotkeys_count() == 0 && !force)
continue;
const int w = m_region.width / m_num_columns;
const int entry_x = entry.column * w;
const int entry_h = row_heights[entry.row+1] - row_heights[entry.row];
if (x >= entry_x && x < entry_x+w && y >= entry.y && y < entry.y+entry_h)
{
wm->set_mouse_cursor(MOUSE_CURSOR_POINTER);
if (force && m_menu->last_hovered != i)
m_menu->set_hovered(i, force); // give menu a chance to change state
else if (me->hotkeys_count())
m_hover_idx = i;
m_real_hover_idx = i;
return;
}
}
wm->set_mouse_cursor(MOUSE_CURSOR_ARROW);
if (!(m_menu->flags & MF_ARROWS_SELECT))
{
if (force)
m_menu->set_hovered(-1, force);
else
m_hover_idx = -1;
m_real_hover_idx = -1;
}
}
bool UIMenu::on_event(const Event& ev)
{
if (Widget::on_event(ev))
return true;
if (ev.type() != Event::Type::MouseMove
&& ev.type() != Event::Type::MouseDown
&& ev.type() != Event::Type::MouseUp
&& ev.type() != Event::Type::MouseEnter
&& ev.type() != Event::Type::MouseLeave)
{
return false;
}
auto event = static_cast<const MouseEvent&>(ev);
m_mouse_x = event.x();
m_mouse_y = event.y();
if (event.type() == Event::Type::MouseEnter)
{
do_layout(m_region.width, m_num_columns);
if (!(m_menu->flags & MF_ARROWS_SELECT) || m_menu->last_hovered < 0)
update_hovered_entry(true);
mark_buffers_dirty();
_expose();
return false;
}
if (event.type() == Event::Type::MouseLeave)
{
wm->set_mouse_cursor(MOUSE_CURSOR_ARROW);
m_mouse_x = -1;
m_mouse_y = -1;
m_mouse_pressed = false;
if (!(m_menu->is_set(MF_ARROWS_SELECT)))
m_hover_idx = -1;
m_real_hover_idx = -1;
do_layout(m_region.width, m_num_columns);
mark_buffers_dirty();
_expose();
return false;
}
if (event.type() == Event::Type::MouseMove)
{
do_layout(m_region.width, m_num_columns);
update_hovered_entry(true);
mark_buffers_dirty();
_expose();
return true;
}
int key = -1;
if (event.type() == Event::Type::MouseDown
&& (event.button() == MouseEvent::Button::Left
|| event.button() == MouseEvent::Button::Right))
{
m_mouse_pressed = true;
do_layout(m_region.width, m_num_columns);
update_hovered_entry(true);
mark_buffers_dirty();
_expose();
}
else if (event.type() == Event::Type::MouseUp
&& (event.button() == MouseEvent::Button::Left
|| event.button() == MouseEvent::Button::Right)
&& m_mouse_pressed)
{
// use the "real" hover idx, a menu item that the mouse is currently
// positioned over.
int entry = m_real_hover_idx;
if (entry != -1 && m_menu->items[entry]->hotkeys_count() > 0)
key = event.button() == MouseEvent::Button::Left ? CK_MOUSE_B1 : CK_MOUSE_B2;
m_mouse_pressed = false;
_queue_allocation();
}
if (key != -1)
{
wm_keyboard_event wm_ev = {0};
wm_ev.keysym.sym = key;
KeyEvent key_ev(Event::Type::KeyDown, wm_ev);
m_menu->m_ui.popup->on_event(key_ev);
}
return true;
}
void UIMenu::mark_buffers_dirty()
{
m_buffers_dirty = true;
}
void UIMenu::pack_buffers()
{
m_shape_buf.clear();
m_div_line_buf.clear();
for (int i = 0; i < TEX_MAX; i++)
m_tile_buf[i].clear();
m_text_buf.clear();
m_line_buf.clear();
const VColour selected_colour(50, 50, 10, 255);
const VColour header_div_colour(64, 64, 64, 200);
if (!item_info.size())
return;
const int col_width = m_region.width / m_num_columns;
int vis_min, vis_max;
is_visible_item_range(&vis_min, &vis_max);
for (int i = vis_min; i < vis_max; ++i)
{
const auto& entry = item_info[i];
const auto me = m_menu->items[i];
const int entry_x = entry.column * col_width;
const int entry_ex = entry_x + col_width;
const int entry_h = row_heights[entry.row+1] - row_heights[entry.row];
if (entry.heading)
{
formatted_string split = m_font_entry->split(entry.text, m_region.width, entry_h);
// see corresponding section in do_layout()
int line_y = entry.y + (i == 0 ? 0 : 5) + item_pad;
if (i < (int)item_info.size()-1 && !item_info[i+1].heading)
{
m_div_line_buf.add_square(entry.x, line_y,
entry.x+m_num_columns*col_width, line_y, header_div_colour);
}
m_text_buf.add(split, entry.x, line_y+3);
}
else
{
const int ty = entry.y + max(entry_h-32, 0)/2;
for (const tile_def &tile : entry.tiles)
{
// NOTE: This is not perfect. Tiles will be drawn
// sorted by texture first, e.g. you can never draw
// a dungeon tile over a monster tile.
const auto tex = get_tile_texture(tile.tile);
m_tile_buf[tex].add(tile.tile, entry_x + item_pad, ty, 0, 0, false, tile.ymax, 1, 1);
}
const int text_indent = m_draw_tiles ? 38 : 0;
int text_sx = entry_x + text_indent + item_pad;
int text_sy = entry.y + (entry_h - m_font_entry->char_height())/2;
// Split off and render any hotkey prefix first
formatted_string text;
if (_has_hotkey_prefix(entry.text.tostring()))
{
formatted_string header = entry.text.chop(5);
m_text_buf.add(header, text_sx, text_sy);
text_sx += m_font_entry->string_width(header);
text = entry.text;
// remove hotkeys. As Enne said above, this is a monstrosity.
for (int k = 0; k < 5; k++)
text.del_char();
}
else
text += entry.text;
// Line wrap and render the remaining text
int w = entry_ex-text_sx - pad_right;
int h = m_font_entry->char_height();
h *= m_menu->is_set(MF_NO_WRAP_ROWS) ? 1 : 2;
formatted_string split = m_font_entry->split(text, w, h);
int string_height = m_font_entry->string_height(split);
text_sy = entry.y + (entry_h - string_height)/2;
m_text_buf.add(split, text_sx, text_sy);
}
if (!m_menu->is_set(MF_NOSELECT))
{
bool hovered = i == m_hover_idx
&& !entry.heading
&& me->hotkeys_count() > 0;
if (me->selected() && !m_menu->is_set(MF_QUIET_SELECT))
{
// draw a highlighted background on selected entries (usually only
// multiselect menus), overriding hover background
m_shape_buf.add(entry_x, entry.y,
entry_ex, entry.y+entry_h, selected_colour);
}
else if (hovered)
{
// draw the regular hover background
const VColour hover_bg = m_mouse_pressed ?
VColour(0, 0, 0, 255) : VColour(255, 255, 255, 25);
m_shape_buf.add(entry_x, entry.y,
entry_ex, entry.y+entry_h, hover_bg);
}
// draw a line around any hovered entry
if (hovered)
{
const VColour mouse_colour = m_mouse_pressed ?
VColour(34, 34, 34, 255) : VColour(255, 255, 255, 51);
m_line_buf.add_square(entry_x + 1, entry.y + 1,
entry_x+col_width, entry.y+entry_h, mouse_colour);
}
}
}
}
#endif
Menu::Menu(int _flags, const string& tagname, KeymapContext kmc)
: f_selitem(nullptr), f_keyfilter(nullptr),
action_cycle(CYCLE_NONE), menu_action(ACT_EXECUTE), title(nullptr),
title2(nullptr), flags(_flags), tag(tagname),
cur_page(1), items(), sel(),
select_filter(), highlighter(new MenuHighlighter), num(-1), lastch(0),
alive(false), more_needs_init(true), remap_numpad(true),
last_hovered(-1), m_kmc(kmc), m_filter(nullptr)
{
m_ui.menu = make_shared<UIMenu>(this);
m_ui.scroller = make_shared<UIMenuScroller>();
m_ui.title = make_shared<Text>();
m_ui.more = make_shared<UIMenuMore>();
m_ui.more->set_visible(false);
m_ui.vbox = make_shared<Box>(Widget::VERT);
m_ui.vbox->set_cross_alignment(Widget::STRETCH);
m_ui.vbox->add_child(m_ui.title);
#ifdef USE_TILE_LOCAL
m_ui.vbox->add_child(m_ui.scroller);
#else
auto scroller_wrap = make_shared<Box>(Widget::VERT, Box::Expand::EXPAND_V);
scroller_wrap->set_cross_alignment(Widget::STRETCH);
scroller_wrap->add_child(m_ui.scroller);
m_ui.vbox->add_child(scroller_wrap);
#endif
m_ui.vbox->add_child(m_ui.more);
m_ui.scroller->set_child(m_ui.menu);
set_flags(flags);
// just do minimal initialization for now, full default more initialization
// happens on show
set_more("");
more_needs_init = true; // reset
}
void Menu::check_add_formatted_line(int firstcol, int nextcol,
string &line, bool check_eol)
{
if (line.empty())
return;
if (check_eol && line.find("\n") == string::npos)
return;
vector<string> lines = split_string("\n", line, false, true);
int size = lines.size();
// If we have stuff after EOL, leave that in the line variable and
// don't add an entry for it, unless the caller told us not to
// check EOL sanity.
if (check_eol && !ends_with(line, "\n"))
line = lines[--size];
else
line.clear();
for (int i = 0, col = firstcol; i < size; ++i, col = nextcol)
{
string &s(lines[i]);
trim_string_right(s);
MenuEntry *me = new MenuEntry(s);
me->colour = col;
if (!title)
set_title(me);
else
add_entry(me);
}
line.clear();
}
Menu::~Menu()
{
deleteAll(items);
delete title;
if (title2)
delete title2;
delete highlighter;
}
void Menu::clear()
{
deleteAll(items);
m_ui.menu->_queue_allocation();
}
void Menu::set_flags(int new_flags)
{
flags = new_flags;
// disable arrow controls depending on options, or for any noselect menu
if (!Options.menu_arrow_control || !!(flags & MF_NOSELECT))
flags = flags & (~(MF_ARROWS_SELECT | MF_INIT_HOVER));
#ifdef DEBUG
int sel_flag = flags & (MF_NOSELECT | MF_SINGLESELECT | MF_MULTISELECT);
ASSERT(sel_flag == MF_NOSELECT || sel_flag == MF_SINGLESELECT || sel_flag == MF_MULTISELECT);
#endif
}
bool Menu::minus_is_command() const
{
// TODO: remove?
return is_set(MF_MULTISELECT) || !is_set(MF_SPECIAL_MINUS);
}
void Menu::set_more(const formatted_string &fs)
{
m_keyhelp_more = false;
more_needs_init = false;
more = fs;
update_more();
}
void Menu::set_more(const string s)
{
set_more(formatted_string::parse_string(s));
}
formatted_string pad_more_with(formatted_string s,
const formatted_string &pad, int min_width)
{
if (min_width <= 0)
return s;
// Needs to be done as a parsed formatted_string so that columns are
// counted correctly.
const auto lines = split_string("\n", s.tostring(), false, true);
const int last_len = static_cast<int>(lines.back().size());
const int pad_size = static_cast<int>(pad.tostring().size());
if (last_len < (min_width - pad_size))
{
s += string(min_width - (last_len + pad_size), ' ');
s += pad;
}
return s;
}
// assumes contiguous lettering
string hyphenated_hotkey_letters(int how_many, char first)
{
how_many = min(how_many, 52);
string s;
if (how_many > 1)
{
// crawl uses A-Z second, but it is first in ascii
int last = static_cast<int>(first) + how_many - 1;
if (last > 'z')
last = 'A' + last - ('z' + 1);
s = make_stringf("[<w>%c</w>-<w>%c</w>]", first, static_cast<char>(last));
}
else
s = make_stringf("[<w>%c</w>]", first);
return s;
}
string pad_more_with(const string &s, const string &pad, int min_width)
{
// TODO is there a way of compensating for the tile width on the left margin
// of certain menus while adding a padded element?
return pad_more_with(
formatted_string::parse_string(s),
formatted_string::parse_string(pad),
min_width).to_colour_string(LIGHTGRAY);
}
static string _keyhelp_format_key(const string &s)
{
return make_stringf("[<w>%s</w>]", s.c_str());
}
string menu_keyhelp_select_keys()
{
const string up = command_to_string(CMD_MENU_UP);
const string down = command_to_string(CMD_MENU_DOWN);
return make_stringf("[<w>%s</w>|<w>%s</w>]", up.c_str(), down.c_str());
}
static string _command_to_string(command_type cmd)
{
return replace_all(command_to_string(cmd), "<", "<<");
}
// standardized formatting for this
string pad_more_with_esc(const string &s)
{
return pad_more_with(s, menu_keyhelp_cmd(CMD_MENU_EXIT) + " exit");
}
string menu_keyhelp_cmd(command_type cmd)
{
if (cmd == CMD_MENU_PAGE_UP || cmd == CMD_MENU_PAGE_DOWN)
{
// always show < and > as secondary keys, if they are defined
const string primary = _command_to_string(cmd);
const string secondary_key = cmd == CMD_MENU_PAGE_UP ? "<<" : ">";
string secondary;
if (primary != secondary_key && key_to_command(secondary_key[0], KMC_MENU) == cmd)
secondary = make_stringf("|<w>%s</w>", secondary_key.c_str());
return make_stringf("[<w>%s</w>%s]", primary.c_str(), secondary.c_str());
}
else if (cmd == CMD_MENU_TOGGLE_SELECTED)
{
// TODO: fix once space is not hardcoded
return make_stringf("[<w>%s</w>|<w>Space</w>]", _command_to_string(cmd).c_str());
}
else
return _keyhelp_format_key(_command_to_string(cmd));
}
string Menu::get_keyhelp(bool scrollable) const
{
// TODO: derive this from cmd key bindings
// multiselect always shows a keyhelp, for singleselect it is blank unless
// it can be scrolled
if (!scrollable && !is_set(MF_MULTISELECT))
return "";
string navigation = "<lightgrey>";
if (is_set(MF_ARROWS_SELECT))
navigation += menu_keyhelp_select_keys() + " select ";
if (scrollable)
{
navigation +=
menu_keyhelp_cmd(CMD_MENU_PAGE_DOWN) + " page down "
+ menu_keyhelp_cmd(CMD_MENU_PAGE_UP) + " page up ";
}
if (!is_set(MF_MULTISELECT))
navigation += menu_keyhelp_cmd(CMD_MENU_EXIT) + " exit";
navigation += "</lightgrey>";
if (is_set(MF_MULTISELECT))
{
navigation = pad_more_with_esc(navigation);
// XX this may not work perfectly with the way InvMenu handles
// selection
const auto chosen_count = selected_entries().size();
navigation +=
"\n<lightgrey>"
"Letters toggle ";
if (is_set(MF_ARROWS_SELECT))
{
navigation += menu_keyhelp_cmd(CMD_MENU_TOGGLE_SELECTED)
+ " toggle selected ";
}
if (chosen_count)
{
navigation += menu_keyhelp_cmd(CMD_MENU_ACCEPT_SELECTION)
+ make_stringf(
" accept (%zu chosen)"
"</lightgrey>",
chosen_count);
}
}
// XX this is present on non-scrolling multiselect keyhelps mostly for
// aesthetic reasons, but maybe it could change with hover?
// the `XXX` is replaced in the ui code with a position in the scroll.
return pad_more_with(navigation, "<lightgrey>[<w>XXX</w>]</lightgrey>");
}
// use the class's built in keyhelp string
void Menu::set_more()
{
m_keyhelp_more = true;
more_needs_init = false;
update_more();
}
void Menu::set_min_col_width(int w)
{
#ifdef USE_TILE_LOCAL
// w is in chars
m_ui.menu->set_min_col_width(w * tiles.get_crt_font()->char_width());
#else
// n.b. this currently has no effect in webtiles unless the more is visible
m_ui.menu->set_min_col_width(w);
#endif
}
void Menu::set_highlighter(MenuHighlighter *mh)
{
if (highlighter != mh)
delete highlighter;
highlighter = mh;
}
void Menu::set_title(MenuEntry *e, bool first, bool indent)
{
if (first)
{
if (title != e)
delete title;
title = e;
title->level = MEL_TITLE;
}
else
{
title2 = e;
title2->level = MEL_TITLE;
}
m_indent_title = indent;
update_title();
}
void Menu::set_title(const string &t, bool first, bool indent)
{
set_title(new MenuEntry(t, MEL_TITLE), first, indent);
}
void Menu::add_entry(MenuEntry *entry)
{
entry->tag = tag;
items.push_back(entry);
}
void Menu::reset()
{
m_ui.scroller->set_scroll(0);
}
vector<MenuEntry *> Menu::show(bool reuse_selections)
{
cursor_control cs(false);
// no one has yet manually called set_more, so use a keyhelp more. We do it
// here and this way instead of in the constructor so that a derived class
// get_keyhelp is used if relevant -- this sets the height correctly.
if (more_needs_init)
set_more();
if (reuse_selections)
get_selected(&sel);
else
deselect_all(false);
if (is_set(MF_START_AT_END))
{
m_ui.scroller->set_scroll(INT_MAX);
if (is_set(MF_INIT_HOVER))
{
// MF_START_AT_END overrides a manually set initial hover
set_hovered(static_cast<int>(items.size()) - 1);
if (items[last_hovered]->level != MEL_ITEM)
cycle_hover(true);
}
}
else if (is_set(MF_INIT_HOVER)
&& (last_hovered < 0 || items[last_hovered]->level != MEL_ITEM)) // XX check hotkeys?
{
cycle_hover();
}
do_menu();
return sel;
}
void Menu::do_menu()
{
bool done = false;
m_ui.popup = make_shared<UIMenuPopup>(m_ui.vbox, this);
m_ui.popup->on_keydown_event([this, &done](const KeyEvent& ev) {
// uses numpad number keys for navigation
int key = remap_numpad ? numpad_to_regular(ev.key(), true) : ev.key();
if (m_filter)
{
if (ev.key() == '?' && _title_prompt_help_tag.size())
{
// TODO: only useful for non-general prompts, is there another
// help key that would be better?
show_specific_help(_title_prompt_help_tag);
return true;
}
key = m_filter->putkey(ev.key());
if (key == CK_ESCAPE)
m_filter->set_text("");
if (key != -1)
{
lastch = key;
delete m_filter;
m_filter = nullptr;
}
update_title();
return true;
}
done = !process_key(key);
return true;
});
update_menu();
ui::push_layout(m_ui.popup, m_kmc);
#ifdef USE_TILE_WEB
tiles.push_menu(this);
_webtiles_title_changed = false;
m_ui.popup->on_layout_pop([](){ tiles.pop_menu(); });
#endif
alive = true;
if (on_show)
done = !on_show();
while (alive && !done && !crawl_state.seen_hups)
{
#ifdef USE_TILE_WEB
if (_webtiles_title_changed)
{
webtiles_update_title();
_webtiles_title_changed = false;
}
#endif
ui::pump_events();
}
alive = false;
ui::pop_layout();
}
bool Menu::is_set(int flag) const
{
return (flags & flag) == flag;
}
int Menu::pre_process(int k)
{
return k;
}
bool Menu::filter_with_regex(const char *re)
{
text_pattern tpat(re, true);
for (unsigned int i = 0; i < items.size(); ++i)
{
if (items[i]->level == MEL_ITEM
&& tpat.matches(items[i]->get_text()))
{
select_index(i);
if (flags & MF_SINGLESELECT)
{
// Return the first item found.
// currently, no menu uses this?
get_selected(&sel);
return false;
}
}
}
get_selected(&sel);
return true;
}
bool Menu::title_prompt(char linebuf[], int bufsz, const char* prompt, string help_tag)
{
bool validline;
ASSERT(!m_filter);
// XX show cursor in console
#ifdef USE_TILE_WEB
tiles.json_open_object();
tiles.json_write_string("msg", "title_prompt");
tiles.json_write_string("prompt", prompt);
tiles.json_close_object();
tiles.finish_message();
#endif
m_filter = new resumable_line_reader(linebuf, bufsz);
m_filter->set_prompt(prompt);
// ugly to use a member variable for this, maybe generalize to a feature
// of line_reader?
_title_prompt_help_tag = help_tag;
update_title();
do
{
ui::pump_events();
}
while (m_filter && !crawl_state.seen_hups);
validline = linebuf[0];
return validline;
}
bool Menu::cycle_mode(bool forward)
{
switch (action_cycle)
{
case CYCLE_NONE:
return false;
case CYCLE_TOGGLE:
// direction doesn't matter for this case
ASSERT(menu_action != ACT_MISC);
if (menu_action == ACT_EXECUTE)
menu_action = ACT_EXAMINE;
else
menu_action = ACT_EXECUTE;
break;
case CYCLE_CYCLE:
if (forward)
menu_action = (action)((menu_action + 1) % ACT_NUM);
else
menu_action = (action)((menu_action + ACT_NUM - 1) % ACT_NUM);
break;
}
sel.clear();
update_title();
update_more();
return true;
}
/// Given some (possibly empty) selection, run any associated on_select or
/// on_single_selection triggers, and return whether the menu loop should
/// continue running. These triggers only happen for singleselect menus.
bool Menu::process_selection()
{
// update selection
get_selected(&sel);
bool ret = sel.empty();
// if it's non-empty, and we are in a singleselect menu, try to run any
// associated on_select functions
if (!!(flags & MF_SINGLESELECT) && !ret)
{
// singleselect: try running any on_select calls
MenuEntry *item = sel[0];
ret = false;
if (item->on_select)
ret = item->on_select(*item);
else if (on_single_selection) // currently, no menus use both
ret = on_single_selection(*item);
// the UI for singleselect menus behaves oddly if anything is
// selected when it runs, because select acts as a toggle. So
// if the selection has been processed and the menu is
// continuing, clear the selection.
// TODO: this is a fairly clumsy api for menus that are
// trying to *do* something.
if (ret)
deselect_all();
}
return ret;
}
bool Menu::process_command(command_type cmd)
{
bool ret = true;
#ifdef USE_TILE_WEB
const int old_vis_first = get_first_visible();
#endif
// note -- MF_USE_TWO_COLUMNS doesn't guarantee two cols!
// currently guaranteed to be false except on local tiles
const bool multicol = m_ui.menu->get_num_columns() > 1;
const int old_hover = last_hovered;
switch (cmd)
{
case CMD_MENU_UP:
if (is_set(MF_ARROWS_SELECT))
{
cycle_hover(true, false, true);
if (last_hovered >= 0 && old_hover == last_hovered)
line_up();
}
else
line_up();
break;
case CMD_MENU_DOWN:
if (is_set(MF_ARROWS_SELECT))
{
cycle_hover(false, false, true);
if (last_hovered >= 0 && old_hover == last_hovered)
line_down();
}
else
line_down();
break;
case CMD_MENU_RIGHT:
if (multicol && is_set(MF_ARROWS_SELECT))
cycle_hover(false, true, false);
else
cycle_mode(true);
break;
case CMD_MENU_LEFT:
if (multicol && is_set(MF_ARROWS_SELECT))
cycle_hover(true, true, false);
else
cycle_mode(false);
break;
case CMD_MENU_LINE_UP:
line_up();
break;
case CMD_MENU_LINE_DOWN:
line_down();
break;
case CMD_MENU_PAGE_UP:
page_up();
break;
case CMD_MENU_PAGE_DOWN:
if (!page_down() && is_set(MF_WRAP))
m_ui.scroller->set_scroll(0);
break;
case CMD_MENU_SCROLL_TO_TOP:
m_ui.scroller->set_scroll(0);
if (is_set(MF_ARROWS_SELECT) && items.size())
{
set_hovered(0);
if (items[last_hovered]->level != MEL_ITEM)
cycle_hover();
}
break;
case CMD_MENU_SCROLL_TO_END:
// setting this to INT_MAX when the last item is already visible does
// unnecessary scrolling to force the last item to be exactly at the
// end of the menu. (This has a weird interaction with page down.)
// TODO: possibly should be fixed in ui.cc? But I don't understand that
// code well enough
if (items.size())
{
if (!in_page(static_cast<int>(items.size()) - 1, true))
m_ui.scroller->set_scroll(INT_MAX);
if (is_set(MF_ARROWS_SELECT))
{
set_hovered(static_cast<int>(items.size()) - 1);
if (items[last_hovered]->level != MEL_ITEM)
cycle_hover(true);
}
}
break;
case CMD_MENU_SEARCH:
if ((flags & MF_ALLOW_FILTER))
{
char linebuf[80] = "";
const bool validline = title_prompt(linebuf, sizeof linebuf,
"Select what (regex)?");
// XX what is the use case for this exiting, should this set lastch
ret = (validline && linebuf[0]) ? filter_with_regex(linebuf) : true;
}
break;
case CMD_MENU_CYCLE_MODE:
cycle_mode(true);
break;
case CMD_MENU_CYCLE_MODE_REVERSE:
cycle_mode(false);
break;
case CMD_MENU_CYCLE_HEADERS:
cycle_headers(true);
break;
case CMD_MENU_HELP:
if (!help_key().empty())
show_specific_help(help_key());
break;
case CMD_MENU_ACCEPT_SELECTION:
ASSERT(is_set(MF_MULTISELECT));
get_selected(&sel);
ret = sel.empty(); // only exit if there is a selection, otherwise noop
break;
case CMD_MENU_SELECT:
// select + accept. Note that this will work in multiselect mode to do
// a quick accept if something is hovered, but is currently preempted by
// CMD_MENU_ACCEPT_SELECTION above.
if (is_set(MF_NOSELECT))
break; // or crash?
// try selecting a hovered item
if (last_hovered >= 0)
select_item_index(last_hovered, MENU_SELECT_ALL);
ret = process_selection();
break;
case CMD_MENU_EXIT:
sel.clear();
lastch = CK_ESCAPE; // XX is this correct?
ret = is_set(MF_UNCANCEL) && !crawl_state.seen_hups;
break;
case CMD_MENU_TOGGLE_SELECTED:
// toggle the currently hovered item, if any. Noop if no hover.
ASSERT(is_set(MF_MULTISELECT));
if (last_hovered >= 0)
{
select_item_index(last_hovered, MENU_SELECT_INVERT);
get_selected(&sel);
}
break;
case CMD_MENU_SELECT_ALL: // Select all or apply filter if there is one.
ASSERT(is_set(MF_MULTISELECT));
select_index(-1, MENU_SELECT_ALL);
break;
case CMD_MENU_INVERT_SELECTION:
ASSERT(is_set(MF_MULTISELECT));
select_index(-1, MENU_SELECT_INVERT);
get_selected(&sel);
break;
case CMD_MENU_CLEAR_SELECTION:
ASSERT(is_set(MF_MULTISELECT));
select_index(-1, MENU_SELECT_CLEAR); // XX is there a singleselect menu where this should work?
break;
case CMD_MENU_EXAMINE:
if (last_hovered >= 0)
ret = examine_index(last_hovered);
break;
default:
break;
}
if (ret)
{
// is this overkill to always do?
update_title();
update_more();
}
if (cmd != CMD_NO_CMD)
num = -1;
#ifdef USE_TILE_WEB
if (old_vis_first != get_first_visible())
webtiles_update_scroll_pos();
#endif
return ret;
}
bool Menu::skip_process_command(int keyin)
{
// TODO: autodetect if there is a menu item that uses a key that would
// otherwise be bound?
if (keyin == '-' && !minus_is_command())
return true;
return false;
}
static bool _cmd_converts_to_examine(command_type c)
{
switch (c)
{
case CMD_MENU_SELECT:
case CMD_MENU_ACCEPT_SELECTION:
case CMD_MENU_TOGGLE_SELECTED:
return true;
default:
return false;
}
}
command_type Menu::get_command(int keyin)
{
if (skip_process_command(keyin))
return CMD_NO_CMD;
// this is a `CASE_ESCAPE` case that the command mapping doesn't handle
// -- is it needed?
if (keyin == -1)
return CMD_MENU_EXIT;
// mouse clicks from UIMenu with a menu item selected
if (keyin == CK_MOUSE_B1)
{
command_type cmd = is_set(MF_MULTISELECT)
? CMD_MENU_TOGGLE_SELECTED : CMD_MENU_SELECT;
if (menu_action == ACT_EXAMINE && _cmd_converts_to_examine(cmd))
cmd = CMD_MENU_EXAMINE;
return cmd;
}
else if (keyin == CK_MOUSE_B2)
return CMD_MENU_EXAMINE;
// for multiselect menus, we first check in the multiselect-specific keymap,
// and if this doesn't find anything, the general menu keymap.
if (is_set(MF_MULTISELECT))
{
// relies on subclasses to actually implement an examine behavior
command_type cmd = key_to_command(keyin, KMC_MENU_MULTISELECT);
if (menu_action == ACT_EXAMINE && _cmd_converts_to_examine(cmd))
cmd = CMD_MENU_EXAMINE;
if (cmd != CMD_NO_CMD)
return cmd;
}
// n.b. this explicitly ignores m_kmc, which is intended only for keymap
// purposes
command_type cmd = key_to_command(keyin, KMC_MENU);
// relies on subclasses to actually implement an examine behavior
if (menu_action == ACT_EXAMINE && _cmd_converts_to_examine(cmd))
cmd = CMD_MENU_EXAMINE;
return cmd;
}
bool Menu::process_key(int keyin)
{
// overall sequence given keyin:
// 1. apply any keyfilter (currently: entirely unused!)
// 2. apply any pre_process transformation
// 3. try converting to a command (get_command, often overridden)
// a. if the key is skipped (usually by a subclass), return CMD_NO_CMD
// b. return the results of the key_to_command mapping
// 4. if this got a command, run the command
// 5. otherwise proceed to manual key handling on keyin
//
// n.b. superclasses can override process_key to preempt these steps as
// well. Various special cases, e.g. digits for quantity menus
//
// webtiles complication alert: most of the menu navigation key handling
// has a separate client-side implementation (for UI responsivenss), and
// this function may not be called for arbitrary client-side keys!
if (!is_set(MF_SHOW_EMPTY) && items.empty())
{
lastch = keyin;
return false;
}
if (f_keyfilter)
keyin = f_keyfilter(keyin);
keyin = pre_process(keyin);
#ifdef USE_TILE_WEB
const int old_vis_first = get_first_visible();
#endif
if (keyin == ' '
&& !!(flags & MF_MULTISELECT) && !!(flags & MF_ARROWS_SELECT))
{
// XX allow customizing this mapping
keyin = '.';
}
command_type cmd = CMD_NO_CMD;
if (is_set(MF_SELECT_QTY) && !is_set(MF_NOSELECT) && isadigit(keyin))
{
// override cmd bindings for quantity digits
if (num > 999)
num = -1;
num = (num == -1) ? keyin - '0' :
num * 10 + keyin - '0';
}
else
cmd = get_command(keyin);
if (cmd != CMD_NO_CMD)
{
lastch = keyin; // TODO: remove lastch
return process_command(cmd);
}
switch (keyin)
{
case CK_NO_KEY:
case CK_REDRAW:
return true;
case 0:
return true;
case CK_MOUSE_CLICK:
// click event from ui.cc
break;
default:
// Even if we do return early, lastch needs to be set first,
// as it's sometimes checked when leaving a menu.
lastch = keyin; // TODO: remove lastch?
const int primary_index = hotkey_to_index(keyin, true);
const int key_index = hotkey_to_index(keyin, false);
// If no selection at all is allowed, exit now.
if (!(flags & (MF_SINGLESELECT | MF_MULTISELECT)))
return false;
if (is_set(MF_SECONDARY_SCROLL) && primary_index < 0 && key_index >= 0)
{
auto snap_range = hotkey_range(keyin);
snap_in_page(snap_range.second);
set_hovered(snap_range.first);
#ifdef USE_TILE_WEB
webtiles_update_scroll_pos(true);
#endif
return true;
}
if (menu_action == ACT_EXAMINE && key_index >= 0)
return examine_by_key(keyin);
// Update either single or multiselect; noop if keyin isn't a hotkey.
// Updates hover.
select_items(keyin, num);
get_selected(&sel);
// we have received what should be a menu item hotkey. Activate that
// menu item.
if (is_set(MF_SINGLESELECT) && key_index >= 0)
{
#ifdef USE_TILE_WEB
webtiles_update_scroll_pos(true);
#endif
return process_selection();
}
if (is_set(MF_ANYPRINTABLE)
&& (!isadigit(keyin) || !is_set(MF_SELECT_QTY)))
{
// TODO: should this behavior be made to coexist with multiselect?
return false;
}
update_title();
update_more();
break;
}
// reset number state if anything other than setting a digit happened
if (!isadigit(keyin))
num = -1;
#ifdef USE_TILE_WEB
// XX is handling this in process_command enough?
if (old_vis_first != get_first_visible())
webtiles_update_scroll_pos();
#endif
return true;
}
string Menu::get_select_count_string(int) const
{
string ret;
if (f_selitem)
ret = f_selitem(&sel);
// count is shown in footer now
return ret + string(max(12 - (int)ret.size(), 0), ' ');
}
vector<MenuEntry*> Menu::selected_entries() const
{
vector<MenuEntry*> selection;
get_selected(&selection);
return selection;
}
void Menu::get_selected(vector<MenuEntry*> *selected) const
{
selected->clear();
for (MenuEntry *item : items)
if (item->selected())
selected->push_back(item);
}
void Menu::deselect_all(bool update_view)
{
for (int i = 0, count = items.size(); i < count; ++i)
{
if (items[i]->level == MEL_ITEM && items[i]->selected())
{
items[i]->select(0);
if (update_view)
{
m_ui.menu->update_item(i);
#ifdef USE_TILE_WEB
webtiles_update_item(i);
#endif
}
}
}
sel.clear();
}
int Menu::get_first_visible(bool skip_init_headers, int col) const
{
int y = m_ui.scroller->get_scroll();
for (int i = 0; i < static_cast<int>(items.size()); i++)
{
int item_y1;
m_ui.menu->get_item_region(i, &item_y1, nullptr);
if (item_y1 >= y)
{
if (skip_init_headers
&& (items[i]->level == MEL_TITLE
|| items[i]->level == MEL_SUBTITLE))
{
// when using this to determine e.g. scroll position, it is
// useful to ignore visible headers
continue;
}
if (col >= 0)
{
int cur_col;
m_ui.menu->get_item_gridpos(i, nullptr, &cur_col);
if (cur_col != col)
continue;
}
return i;
}
}
// returns 0 on empty menu -- callers should guard for this if relevant
// (XX -1 might be better? but callers currently assume non-negative...)
return items.size();
}
bool Menu::is_hotkey(int i, int key)
{
return items[i]->is_hotkey(key);
}
/// find the first item (if any) that has hotkey `key`.
int Menu::hotkey_to_index(int key, bool primary_only)
{
const int final = items.size();
// Process all items, in case user hits hotkey for an
// item not on the current page.
// Depending on flags, we have one of two behaviors:
//
// If MF_SELECT_BY_CATEGORY is set (used for single-page inventory screen),
// we first check for the first matching hotkey, starting from the top of
// the current menu subsection the cursor is in. If no match is found, we
// next check from the start of the menu to the current position. (This
// means that in cases where a menu has multiple entries with the same
// letter, we will select one within the current subsection, if one exists,
// and then check elsewhere if not.)
//
// If it is not, we simply select the nearest entry with a matching hotkey.
if (is_set(MF_SELECT_BY_CATEGORY))
{
// First, determine the top of our current section.
int top = 0;
for (int i = last_hovered; i >= 0; --i)
{
if (items[i]->level != MEL_ITEM)
{
top = i;
break;
}
}
for (int i = top; i < final; ++i)
{
if (is_hotkey(i, key)
&& (!primary_only || items[i]->hotkeys[0] == key))
{
return i;
}
}
for (int i = 0; i < top; ++i)
{
if (is_hotkey(i, key)
&& (!primary_only || items[i]->hotkeys[0] == key))
{
return i;
}
}
}
else
{
int nearest_index = -1;
int nearest_dist = INT_MAX;
for (int i = 0; i < (int)items.size(); ++i)
{
if (is_hotkey(i, key)
&& (!primary_only || items[i]->hotkeys[0] == key))
{
int dist = abs(i - last_hovered);
if (dist < nearest_dist)
{
nearest_dist = dist;
nearest_index = i;
}
}
}
return nearest_index;
}
return -1;
}
pair<int,int> Menu::hotkey_range(int key)
{
int first = -1;
int last = -1;
for (int i = 0; i < static_cast<int>(items.size()); ++i)
if (is_hotkey(i, key))
{
if (first < 0)
first = i;
last = i;
}
return make_pair(first, last);
}
void Menu::select_items(int key, int qty)
{
const int index = hotkey_to_index(key, !is_set(MF_SINGLESELECT));
if (index >= 0)
{
select_index(index, qty);
// XX should this update hover generally? This approach is tailored
// towards avoiding a weirdness with just the memorize menu...
if (is_set(MF_MULTISELECT))
set_hovered(index);
return;
}
// no primary hotkeys found, check secondary hotkeys for multiselect
if (is_set(MF_MULTISELECT))
{
auto snap_range = hotkey_range(key);
if (snap_range.first >= 0)
{
for (int i = snap_range.first; i <= snap_range.second; ++i)
if (is_hotkey(i, key))
select_index(i, qty);
// try to ensure as much of the selection as possible is in
// view by snapping twice
snap_in_page(snap_range.second);
set_hovered(snap_range.first);
#ifdef USE_TILE_WEB
webtiles_update_scroll_pos(true);
#endif
}
}
}
bool Menu::examine_index(int i)
{
ASSERT(i >= 0 && i < static_cast<int>(items.size()));
if (on_examine)
return on_examine(*items[i]);
return true;
}
bool Menu::examine_by_key(int keyin)
{
const int index = hotkey_to_index(keyin, !is_set(MF_SINGLESELECT));
if (index >= 0)
{
set_hovered(index);
#ifdef USE_TILE_WEB
webtiles_update_scroll_pos(true);
#endif
return examine_index(index);
}
return true;
}
bool MenuEntry::selected() const
{
return selected_qty > 0 && (quantity || on_select);
}
// -1: Invert (MENU_SELECT_INVERT; used only in multiselect)
// -2: Select all (MENU_SELECT_ALL)
// a menu can be selected either if it defines a quantity, or an on_select
// function.
// TODO: fix this mess
void MenuEntry::select(int qty)
{
const int real_max = quantity == 0 && on_select ? 1 : quantity;
if (qty == MENU_SELECT_ALL)
selected_qty = real_max;
else if (qty == MENU_SELECT_INVERT)
selected_qty = selected() ? 0 : real_max;
else
selected_qty = min(qty, real_max);
}
string MenuEntry::_get_text_preface() const
{
if (level == MEL_ITEM && hotkeys_count())
return make_stringf(" %s - ", keycode_to_name(hotkeys[0]).c_str());
else if (level == MEL_ITEM && indent_no_hotkeys)
return " ";
else
return "";
}
string MenuEntry::get_text() const
{
return _get_text_preface() + text;
}
void MenuEntry::wrap_text(int width)
{
// Warning: console menus cannot handle multiline regular entries, use for
// the title only (TODO)
const int indent
#ifdef USE_TILE_LOCAL
= 0; // tiles does line-wrapping inside the text
#else
= static_cast<int>(_get_text_preface().size());
width -= indent;
#endif
if (width <= 0)
return;
linebreak_string(text, width, true, indent);
}
MonsterMenuEntry::MonsterMenuEntry(const string &str, const monster_info* mon,
int hotkey) :
MenuEntry(str, MEL_ITEM, 1, hotkey)
{
data = (void*)mon;
quantity = 1;
}
FeatureMenuEntry::FeatureMenuEntry(const string &str, const coord_def p,
int hotkey) :
MenuEntry(str, MEL_ITEM, 1, hotkey)
{
if (in_bounds(p))
feat = env.grid(p);
else
feat = DNGN_UNSEEN;
pos = p;
quantity = 1;
}
FeatureMenuEntry::FeatureMenuEntry(const string &str,
const dungeon_feature_type f,
int hotkey) :
MenuEntry(str, MEL_ITEM, 1, hotkey)
{
pos.reset();
feat = f;
quantity = 1;
}
#ifdef USE_TILE
bool MenuEntry::get_tiles(vector<tile_def>& tileset) const
{
if (!Options.tile_menu_icons || tiles.empty())
return false;
tileset.insert(end(tileset), begin(tiles), end(tiles));
return true;
}
#else
bool MenuEntry::get_tiles(vector<tile_def>& /*tileset*/) const { return false; }
#endif
void MenuEntry::add_tile(tile_def tile)
{
#ifdef USE_TILE
tiles.push_back(tile);
#else
UNUSED(tile);
#endif
}
#ifdef USE_TILE
PlayerMenuEntry::PlayerMenuEntry(const string &str) :
MenuEntry(str, MEL_ITEM, 1)
{
quantity = 1;
}
bool MonsterMenuEntry::get_tiles(vector<tile_def>& tileset) const
{
if (!Options.tile_menu_icons)
return false;
monster_info* m = (monster_info*)(data);
if (!m)
return false;
MenuEntry::get_tiles(tileset);
const bool fake = m->props.exists(FAKE_MON_KEY);
const coord_def c = m->pos;
tileidx_t ch = TILE_FLOOR_NORMAL;
if (!fake)
ch = tileidx_feature(c);
tileset.emplace_back(ch);
if (m->attitude == ATT_FRIENDLY)
tileset.emplace_back(TILE_HALO_FRIENDLY);
else if (m->attitude == ATT_GOOD_NEUTRAL)
tileset.emplace_back(TILE_HALO_GD_NEUTRAL);
else if (m->neutral())
tileset.emplace_back(TILE_HALO_NEUTRAL);
else if (Options.tile_show_threat_levels.find("unusual") != string::npos
&& m->has_unusual_items())
tileset.emplace_back(TILE_THREAT_UNUSUAL);
else if (m->type == MONS_PLAYER_GHOST)
switch (m->threat)
{
case MTHRT_TRIVIAL:
tileset.emplace_back(TILE_THREAT_GHOST_TRIVIAL);
break;
case MTHRT_EASY:
tileset.emplace_back(TILE_THREAT_GHOST_EASY);
break;
case MTHRT_TOUGH:
tileset.emplace_back(TILE_THREAT_GHOST_TOUGH);
break;
case MTHRT_NASTY:
tileset.emplace_back(TILE_THREAT_GHOST_NASTY);
break;
default:
break;
}
else
switch (m->threat)
{
case MTHRT_TRIVIAL:
if (Options.tile_show_threat_levels.find("trivial") != string::npos)
tileset.emplace_back(TILE_THREAT_TRIVIAL);
break;
case MTHRT_EASY:
if (Options.tile_show_threat_levels.find("easy") != string::npos)
tileset.emplace_back(TILE_THREAT_EASY);
break;
case MTHRT_TOUGH:
if (Options.tile_show_threat_levels.find("tough") != string::npos)
tileset.emplace_back(TILE_THREAT_TOUGH);
break;
case MTHRT_NASTY:
if (Options.tile_show_threat_levels.find("nasty") != string::npos)
tileset.emplace_back(TILE_THREAT_NASTY);
break;
default:
break;
}
if (m->type == MONS_DANCING_WEAPON)
{
// other animated objects use regular monster tiles (TODO: animate
// armour's seems broken in this menu)
item_def item;
if (!fake && m->inv[MSLOT_WEAPON])
item = *m->inv[MSLOT_WEAPON];
if (fake || !item.defined())
{
item.base_type = OBJ_WEAPONS;
item.sub_type = WPN_LONG_SWORD;
item.quantity = 1;
}
tileset.emplace_back(tileidx_item(item));
tileset.emplace_back(TILEI_ANIMATED_WEAPON);
}
else if (mons_is_draconian(m->type))
{
tileset.emplace_back(tileidx_draco_base(*m));
const tileidx_t job = tileidx_draco_job(*m);
if (job)
tileset.emplace_back(job);
}
else
{
tileidx_t idx = tileidx_monster(*m) & TILE_FLAG_MASK;
tileset.emplace_back(idx);
}
// A fake monster might not have its ghost member set up properly.
if (!fake && !m->airborne())
{
if (ch == TILE_DNGN_LAVA)
tileset.emplace_back(TILEI_MASK_LAVA);
else if (ch == TILE_DNGN_SHALLOW_WATER)
tileset.emplace_back(TILEI_MASK_SHALLOW_WATER);
else if (ch == TILE_DNGN_DEEP_WATER)
tileset.emplace_back(TILEI_MASK_DEEP_WATER);
else if (ch == TILE_DNGN_SHALLOW_WATER_MURKY)
tileset.emplace_back(TILEI_MASK_SHALLOW_WATER_MURKY);
else if (ch == TILE_DNGN_DEEP_WATER_MURKY)
tileset.emplace_back(TILEI_MASK_DEEP_WATER_MURKY);
}
string damage_desc;
mon_dam_level_type damage_level = m->dam;
switch (damage_level)
{
case MDAM_DEAD:
case MDAM_ALMOST_DEAD:
tileset.emplace_back(TILEI_MDAM_ALMOST_DEAD);
break;
case MDAM_SEVERELY_DAMAGED:
tileset.emplace_back(TILEI_MDAM_SEVERELY_DAMAGED);
break;
case MDAM_HEAVILY_DAMAGED:
tileset.emplace_back(TILEI_MDAM_HEAVILY_DAMAGED);
break;
case MDAM_MODERATELY_DAMAGED:
tileset.emplace_back(TILEI_MDAM_MODERATELY_DAMAGED);
break;
case MDAM_LIGHTLY_DAMAGED:
tileset.emplace_back(TILEI_MDAM_LIGHTLY_DAMAGED);
break;
case MDAM_OKAY:
default:
// no flag for okay.
break;
}
if (m->attitude == ATT_FRIENDLY)
tileset.emplace_back(TILEI_FRIENDLY);
else if (m->attitude == ATT_GOOD_NEUTRAL)
tileset.emplace_back(TILEI_GOOD_NEUTRAL);
else if (m->neutral())
tileset.emplace_back(TILEI_NEUTRAL);
else if (m->is(MB_PARALYSED))
tileset.emplace_back(TILEI_PARALYSED);
else if (m->is(MB_FLEEING))
tileset.emplace_back(TILEI_FLEEING);
else if (m->is(MB_STABBABLE))
tileset.emplace_back(TILEI_STAB_BRAND);
else if (m->is(MB_DISTRACTED) || m->is(MB_UNAWARE) || m->is(MB_WANDERING)
|| m->is(MB_CANT_SEE_YOU))
{
tileset.emplace_back(TILEI_UNAWARE);
}
return true;
}
bool FeatureMenuEntry::get_tiles(vector<tile_def>& tileset) const
{
if (!Options.tile_menu_icons)
return false;
if (feat == DNGN_UNSEEN)
return false;
MenuEntry::get_tiles(tileset);
tileidx_t tile = tileidx_feature(pos);
tileset.emplace_back(tile);
if (in_bounds(pos) && is_unknown_stair(pos))
tileset.emplace_back(TILEI_NEW_STAIR);
if (in_bounds(pos) && is_unknown_transporter(pos))
tileset.emplace_back(TILEI_NEW_TRANSPORTER);
return true;
}
bool PlayerMenuEntry::get_tiles(vector<tile_def>& tileset) const
{
if (!Options.tile_menu_icons)
return false;
MenuEntry::get_tiles(tileset);
const player_save_info &player = *static_cast<player_save_info*>(data);
pack_tilep_set(tileset, player.doll);
return true;
}
#endif
bool Menu::is_selectable(int item) const
{
if (select_filter.empty())
return true;
string text = items[item]->get_filter_text();
for (const text_pattern &pat : select_filter)
if (pat.matches(text))
return true;
return false;
}
void Menu::select_item_index(int idx, int qty)
{
items[idx]->select(qty);
m_ui.menu->update_item(idx);
#ifdef USE_TILE_WEB
webtiles_update_item(idx);
#endif
}
// index = -1, do special action depending on qty
// qty = 0 (MENU_SELECT_CLEAR): clear selection
// qty = -1 (MENU_SELECT_INVERT): invert selection
// qty = -2 (MENU_SELECT_ALL): select all or apply filter
// TODO: refactor in a better way
void Menu::select_index(int index, int qty)
{
int first_vis = get_first_visible();
int si = index == -1 ? first_vis : index;
if (index == -1)
{
if (flags & MF_MULTISELECT)
{
for (int i = 0, count = items.size(); i < count; ++i)
{
if (items[i]->level != MEL_ITEM
|| items[i]->hotkeys.empty())
{
continue;
}
if (is_hotkey(i, items[i]->hotkeys[0])
&& (qty != MENU_SELECT_ALL || is_selectable(i)))
{
select_item_index(i, qty);
}
}
}
}
else if (items[si]->level == MEL_SUBTITLE && (flags & MF_MULTISELECT))
{
for (int i = si + 1, count = items.size(); i < count; ++i)
{
if (items[i]->level != MEL_ITEM
|| items[i]->hotkeys.empty())
{
continue;
}
if (is_hotkey(i, items[i]->hotkeys[0]))
select_item_index(i, qty);
}
}
else if (items[si]->level == MEL_ITEM
&& (flags & (MF_SINGLESELECT | MF_MULTISELECT)))
{
select_item_index(si, qty);
}
}
size_t Menu::item_count(bool include_headers) const
{
size_t count = items.size();
if (!include_headers)
{
for (const auto &item : items)
if (item->level != MEL_ITEM)
{
ASSERT(count > 0);
count--;
}
}
return count;
}
int Menu::get_entry_index(const MenuEntry *e) const
{
int index = 0;
for (const auto &item : items)
{
if (item == e)
return index;
if (item->quantity != 0)
index++;
}
return -1;
}
void Menu::update_menu(bool update_entries)
{
m_ui.menu->update_items();
update_title();
// sanitize hover in case items have changed. The first check here handles
// a mouse hover case that set_hovered will not.
if (!is_set(MF_ARROWS_SELECT) && last_hovered >= static_cast<int>(items.size()))
last_hovered = -1;
if (last_hovered >= 0)
set_hovered(last_hovered);
if (!alive)
return;
#ifdef USE_TILE_WEB
if (update_entries)
{
tiles.json_open_object();
tiles.json_write_string("msg", "update_menu");
tiles.json_write_int("total_items", items.size());
tiles.json_write_int("last_hovered", last_hovered);
tiles.json_close_object();
tiles.finish_message();
if (items.size() > 0)
webtiles_update_items(0, items.size() - 1);
}
#else
UNUSED(update_entries);
#endif
}
void Menu::update_more()
{
if (crawl_state.doing_prev_cmd_again)
return;
// used as a hacky way of enforcing a min width for tiles when a more is
// visible. results in consistent popup widths. Only has an effect if the
// min width is explicitly set.
const int width =
#ifdef USE_TILE_LOCAL
0;
#else
m_ui.menu->get_min_col_width();
#endif
if (m_keyhelp_more)
{
// this case does not use `this->more` at all
m_ui.more->set_more_template(
pad_more_with(get_keyhelp(true), "", width),
pad_more_with(get_keyhelp(false), "", width));
m_ui.more->_expose();
// visibility is handled in lower-level UI code, but for some reason
// only a toggle from visible to invisible works on initial render
m_ui.more->set_visible(true);
}
else
{
formatted_string shown_more = more.ops.empty()
? more
: pad_more_with(more, formatted_string(""), width);
m_ui.more->set_text(shown_more);
m_ui.more->using_template = false;
m_ui.more->set_visible(!shown_more.ops.empty());
}
#ifdef USE_TILE_WEB
if (!alive)
return;
tiles.json_open_object();
tiles.json_write_string("msg", "update_menu");
m_ui.more->webtiles_write_more();
tiles.json_close_object();
tiles.finish_message();
#endif
}
int Menu::item_colour(const MenuEntry *entry) const
{
int icol = -1;
if (highlighter)
icol = highlighter->entry_colour(entry);
return icol == -1 ? entry->colour : icol;
}
formatted_string Menu::calc_title() { return formatted_string(); }
MenuEntry *Menu::get_cur_title() const
{
const bool first = (action_cycle == CYCLE_NONE
|| menu_action == ACT_EXECUTE);
// if title2 is set, use it as an alt title; otherwise don't change
// titles
return first ? title
: title2 ? title2 : title;
}
void Menu::update_title()
{
if (!title || crawl_state.doing_prev_cmd_again)
return;
formatted_string fs;
if (m_filter)
{
fs = formatted_string::parse_string(
m_filter->get_prompt().c_str())
// apply formatting only to the prompt
+ " " + m_filter->get_text();
}
else
fs = calc_title();
if (fs.empty())
{
const auto *t = get_cur_title();
ASSERT(t);
auto col = item_colour(t);
string text = t->get_text();
fs.textcolour(col);
if (flags & MF_ALLOW_FORMATTING)
fs += formatted_string::parse_string(text);
else
fs.cprintf("%s", text.c_str());
}
if (!is_set(MF_QUIET_SELECT) && is_set(MF_MULTISELECT))
fs.cprintf("%s", get_select_count_string(sel.size()).c_str());
if (m_indent_title)
{
formatted_string indented(" ");
indented += fs;
fs = indented;
}
#ifdef USE_TILE_LOCAL
const bool tile_indent = m_indent_title && Options.tile_menu_icons;
m_ui.title->set_margin_for_sdl(0, UIMenu::item_pad+UIMenu::pad_right, 10,
UIMenu::item_pad + (tile_indent ? 38 : 0));
m_ui.more->set_margin_for_sdl(10, UIMenu::item_pad+UIMenu::pad_right, 0,
tile_indent ? UIMenu::item_pad + 38 : 0);
#endif
m_ui.title->set_text(fs);
#ifdef USE_TILE_WEB
webtiles_set_title(fs);
#endif
}
void Menu::set_hovered(int index, bool force)
{
if (!force && !is_set(MF_ARROWS_SELECT))
{
snap_in_page(index);
return;
}
// intentionally goes to -1 on size 0
last_hovered = min(index, static_cast<int>(items.size()) - 1);
m_ui.menu->set_hovered_entry(last_hovered);
if (last_hovered >= 0)
snap_in_page(last_hovered);
}
bool Menu::in_page(int index, bool strict) const
{
int y1, y2;
m_ui.menu->get_item_region(index, &y1, &y2);
int vph = m_ui.scroller->get_region().height;
int vpy = m_ui.scroller->get_scroll();
const bool upper_in = (vpy <= y1 && y1 <= vpy+vph);
const bool lower_in = (vpy <= y2 && y2 <= vpy+vph);
return strict ? (lower_in && upper_in) : (lower_in || upper_in);
}
bool Menu::set_scroll(int index)
{
// ui not yet set up. Setting this value now will force a
// `set_scroll` call with the same index on first render.
if (!ui_is_initialized())
{
m_ui.menu->set_initial_scroll(index);
return false;
}
// TODO: code duplication, maybe could be refactored into lower-level code
const int vph = m_ui.scroller->get_region().height;
ASSERT(vph > 0);
if (index < 0 || index >= static_cast<int>(items.size()))
return false;
int y1, y2;
m_ui.menu->get_item_region(index, &y1, &y2);
// special case: the immediately preceding items are a header of some kind.
// Most important visually when the first item in a menu is a header
const int any_preceding_headers = get_header_block(index).first;
if (any_preceding_headers != index)
m_ui.menu->get_item_region(any_preceding_headers, &y1, nullptr);
const int vpy = m_ui.scroller->get_scroll();
m_ui.scroller->set_scroll(y1
#ifdef USE_TILE_LOCAL
- UI_SCROLLER_SHADE_SIZE / 2
#endif
);
#ifdef USE_TILE_WEB
// XX this doesn't force update the server, should it ever?
webtiles_update_scroll_pos();
#endif
return vpy != y1;
}
bool Menu::ui_is_initialized() const
{
// is this really the best way to do this?? I arrived at this by
// generalizing some code that used this specific check, but maybe it
// should be done in some more general fashion...
return m_ui.scroller && m_ui.scroller->get_region().height > 0;
}
bool Menu::item_visible(int index)
{
// ui not yet set up. Use `set_scroll` for this case, or a hover will
// be automatically snapped.
if (!ui_is_initialized())
return false;
// TODO: code duplication, maybe could be refactored into lower-level code
const int vph = m_ui.scroller->get_region().height;
ASSERT(vph > 0);
if (index < 0 || index >= static_cast<int>(items.size()))
return false;
int y1, y2;
m_ui.menu->get_item_region(index, &y1, &y2);
// special case: the immediately preceding items are a header of some kind.
// Most important visually when the first item in a menu is a header
const int any_preceding_headers = get_header_block(index).first;
if (any_preceding_headers != index)
m_ui.menu->get_item_region(any_preceding_headers, &y1, nullptr);
const int vpy = m_ui.scroller->get_scroll();
// full visibility -- should this be partial visibility?
return y1 >= vpy && y2 < vpy + vph;
}
/// Ensure that the item at index is visible in the scroller. This happens
/// relative to the current scroll.
bool Menu::snap_in_page(int index)
{
// ui not yet set up. Use `set_scroll` for this case, or a hover will
// be automatically snapped.
if (!ui_is_initialized())
return false;
// TODO: code duplication, maybe could be refactored into lower-level code
const int vph = m_ui.scroller->get_region().height;
ASSERT(vph > 0);
if (index < 0 || index >= static_cast<int>(items.size()))
return false;
int y1, y2;
m_ui.menu->get_item_region(index, &y1, &y2);
// special case: the immediately preceding items are a header of some kind.
// Most important visually when the first item in a menu is a header
const int any_preceding_headers = get_header_block(index).first;
if (any_preceding_headers != index)
m_ui.menu->get_item_region(any_preceding_headers, &y1, nullptr);
const int vpy = m_ui.scroller->get_scroll();
#ifdef USE_TILE_LOCAL
// on local tiles, when scrolling longer menus, the scroller will apply a
// shade to the edge of the scroller. Compensate for this for non-end menu
// items.
const int shade = (index > 0 || index < static_cast<int>(items.size()) - 1)
? UI_SCROLLER_SHADE_SIZE / 2 : 0;
#else
const int shade = 0;
#endif
// the = for these is to apply the local tiles shade adjustment if necessary
if (y1 <= vpy)
{
// scroll up
m_ui.scroller->set_scroll(y1 - shade);
}
else if (y2 >= vpy + vph)
{
// scroll down
m_ui.scroller->set_scroll(y2 - vph + shade);
}
else
return false; // already in page
return true;
}
bool Menu::page_down()
{
int new_hover = -1;
if (is_set(MF_ARROWS_SELECT) && last_hovered < 0 && items.size() > 0)
last_hovered = 0;
// preserve relative position
int col = 0;
if (last_hovered >= 0 && m_ui.menu)
m_ui.menu->get_item_gridpos(last_hovered, nullptr, &col);
if (last_hovered >= 0)
{
new_hover = in_page(last_hovered)
? max(0, last_hovered - get_first_visible(true, col))
: 0;
}
int dy = m_ui.scroller->get_region().height - m_ui.menu->get_scroll_context();
int y = m_ui.scroller->get_scroll();
bool at_bottom = y+dy >= m_ui.menu->get_region().height;
// don't scroll further if the last item is already visible
// (TODO: I don't understand why this check is necessary, but without it,
// you sometimes unpredictably end up with the last element on its own page)
if (items.size() && !in_page(static_cast<int>(items.size()) - 1, true))
m_ui.scroller->set_scroll(y+dy);
if (new_hover >= 0)
{
// if pgdn wouldn't change the hover, move it to the last element
if (is_set(MF_ARROWS_SELECT) && get_first_visible(true, col) + new_hover == last_hovered)
set_hovered(items.size() - 1);
else
set_hovered(get_first_visible(true, col) + new_hover);
if (items[last_hovered]->level != MEL_ITEM)
cycle_hover(true); // reverse so we don't overshoot
}
#ifndef USE_TILE_LOCAL
if (!at_bottom)
m_ui.menu->set_showable_height(y+dy+dy);
#endif
return !at_bottom;
}
bool Menu::page_up()
{
int new_hover = -1;
if (is_set(MF_ARROWS_SELECT) && last_hovered < 0 && items.size() > 0)
last_hovered = 0;
if (last_hovered >= 0)
{
new_hover = in_page(last_hovered)
? max(0, last_hovered - get_first_visible(true))
: 0;
}
int dy = m_ui.scroller->get_region().height - m_ui.menu->get_scroll_context();
int y = m_ui.scroller->get_scroll();
m_ui.scroller->set_scroll(y-dy);
if (new_hover >= 0)
{
// if pgup wouldn't change the hover, select the first element
if (is_set(MF_ARROWS_SELECT) && get_first_visible(true) + new_hover == last_hovered)
new_hover = 0;
set_hovered(get_first_visible(true) + new_hover);
if (items[last_hovered]->level != MEL_ITEM)
cycle_hover(); // forward so we don't overshoot
}
#ifndef USE_TILE_LOCAL
m_ui.menu->set_showable_height(y);
#endif
return y > 0;
}
bool Menu::line_down()
{
// check if we are already at the end.
if (items.empty() || in_page(static_cast<int>(items.size()) - 1, true))
return false;
int index = get_first_visible();
int first_vis_y;
m_ui.menu->get_item_region(index, &first_vis_y, nullptr);
index++;
while (index < (int)items.size())
{
int y;
m_ui.menu->get_item_region(index, &y, nullptr);
index++;
if (y == first_vis_y)
continue;
m_ui.scroller->set_scroll(y);
return true;
}
return false;
}
void Menu::cycle_hover(bool reverse, bool preserve_row, bool preserve_col)
{
if (!is_set(MF_ARROWS_SELECT))
return;
int items_tried = 0;
const int max_items = is_set(MF_WRAP) ? items.size()
: reverse
? last_hovered
: items.size() - max(last_hovered, 0);
int new_hover = last_hovered;
if (reverse && last_hovered < 0)
new_hover = 0;
int row = 0;
int col = 0;
if (last_hovered >= 0 && m_ui.menu)
m_ui.menu->get_item_gridpos(last_hovered, &row, &col);
bool found = false;
while (items_tried < max_items)
{
new_hover = new_hover + (reverse ? -1 : 1);
items_tried++;
// try to find a non-heading to hover over
const int sz = static_cast<int>(items.size());
if (is_set(MF_WRAP) && sz > 0)
new_hover = (new_hover + sz) % sz;
new_hover = max(0, min(new_hover, sz - 1));
int cur_row, cur_col;
if (m_ui.menu && (preserve_row || preserve_col))
{
m_ui.menu->get_item_gridpos(new_hover, &cur_row, &cur_col);
if (preserve_row && cur_row != row || preserve_col && cur_col != col)
continue;
}
if (items[new_hover]->level == MEL_ITEM)
{
found = true;
break;
}
}
if (!found)
return;
set_hovered(new_hover);
#ifdef USE_TILE_WEB
webtiles_update_scroll_pos();
#endif
}
// XXX: doesn't do exactly what we want
bool Menu::line_up()
{
int index = get_first_visible();
if (index > 0)
{
int y;
m_ui.menu->get_item_region(index-1, &y, nullptr);
m_ui.scroller->set_scroll(y);
#ifndef USE_TILE_LOCAL
int dy = m_ui.scroller->get_region().height;
m_ui.menu->set_showable_height(y+dy);
#endif
return true;
}
return false;
}
/// Return a range for index that includes at least one non-header if possible,
/// and any sequence of adjacent headers. Used for display logic to try to
/// show headers before any selected item. Doesn't check for valid indices
pair<int, int> Menu::get_header_block(int index) const
{
int first = index;
int last = index;
while (first >= 1 && items[first - 1]->level != MEL_ITEM)
first--;
// if index is a header, scan forward to look for a non-headed
while (last + 1 < static_cast<int>(items.size()) && items[last]->level != MEL_ITEM)
last++;
return make_pair(first, last);
}
int Menu::next_block_from(int index, bool forward, bool wrap) const
{
const auto cur_block = get_header_block(index);
int next = forward ? cur_block.second + 1 : cur_block.first - 1;
if (wrap)
next = next % items.size();
else
next = max(min(next, static_cast<int>(items.size()) - 1), 0);
return get_header_block(next).first;
}
bool Menu::cycle_headers(bool forward)
{
if (items.size() == 0)
return false;
// XX this doesn't work quite right if called before the menu is displayed
int start = is_set(MF_ARROWS_SELECT) ? max(last_hovered, 0)
: get_first_visible();
start = get_header_block(start).first;
int cur = next_block_from(start, forward, true);
while (cur != start)
{
if (items[cur]->level == MEL_SUBTITLE || items[cur]->level == MEL_TITLE)
{
if (!item_visible(cur) || !is_set(MF_ARROWS_SELECT))
set_scroll(cur);
if (is_set(MF_ARROWS_SELECT))
{
set_hovered(cur);
cycle_hover(); // cycle to get a valid hover
}
#ifdef USE_TILE_WEB
// cycle_headers doesn't currently have a client-side
// implementation, so force-send the server-side scroll
webtiles_update_scroll_pos(true);
#endif
return true;
}
cur = next_block_from(cur, forward, true);
}
return false;
}
#ifdef USE_TILE_WEB
void Menu::webtiles_write_menu(bool replace) const
{
if (crawl_state.doing_prev_cmd_again)
return;
tiles.json_open_object();
tiles.json_write_string("msg", "menu");
tiles.json_write_bool("ui-centred", !crawl_state.need_save);
tiles.json_write_string("tag", tag);
tiles.json_write_int("flags", flags);
tiles.json_write_int("last_hovered", last_hovered);
if (replace)
tiles.json_write_int("replace", 1);
webtiles_write_title();
m_ui.more->webtiles_write_more();
int count = items.size();
int start = 0;
int end = start + count;
tiles.json_write_int("total_items", count);
tiles.json_write_int("chunk_start", start);
int first_entry = get_first_visible();
if (first_entry != 0 && !is_set(MF_START_AT_END))
tiles.json_write_int("jump_to", first_entry);
tiles.json_open_array("items");
for (int i = start; i < end; ++i)
webtiles_write_item(items[i]);
tiles.json_close_array();
tiles.json_close_object();
}
void Menu::webtiles_scroll(int first, int hover)
{
// catch and ignore stale scroll events
if (first >= static_cast<int>(items.size()))
return;
int item_y;
m_ui.menu->get_item_region(first, &item_y, nullptr);
if (m_ui.scroller->get_scroll() != item_y)
{
m_ui.scroller->set_scroll(item_y);
// The `set_hovered` call here will trigger a snap, which is a way
// for the server to get out of sync with the client. We therefore
// only call it if the hover has actually changed on the client side.
// if (last_hovered != hover)
set_hovered(hover);
webtiles_update_scroll_pos();
ui::force_render();
}
}
void Menu::webtiles_handle_item_request(int start, int end)
{
start = min(max(0, start), (int)items.size()-1);
if (end < start)
end = start;
if (end >= (int)items.size())
end = (int)items.size() - 1;
tiles.json_open_object();
tiles.json_write_string("msg", "update_menu_items");
tiles.json_write_int("chunk_start", start);
tiles.json_open_array("items");
for (int i = start; i <= end; ++i)
webtiles_write_item(items[i]);
tiles.json_close_array();
tiles.json_close_object();
tiles.finish_message();
}
void Menu::webtiles_set_title(const formatted_string title_)
{
if (title_.to_colour_string() != _webtiles_title.to_colour_string())
{
_webtiles_title_changed = true;
_webtiles_title = title_;
}
}
void Menu::webtiles_update_items(int start, int end) const
{
ASSERT_RANGE(start, 0, (int) items.size());
ASSERT_RANGE(end, start, (int) items.size());
tiles.json_open_object();
tiles.json_write_string("msg", "update_menu_items");
tiles.json_write_int("chunk_start", start);
tiles.json_open_array("items");
for (int i = start; i <= end; ++i)
webtiles_write_item(items[i]);
tiles.json_close_array();
tiles.json_close_object();
tiles.finish_message();
}
void Menu::webtiles_update_item(int index) const
{
webtiles_update_items(index, index);
}
void Menu::webtiles_update_title() const
{
tiles.json_open_object();
tiles.json_write_string("msg", "update_menu");
webtiles_write_title();
tiles.json_close_object();
tiles.finish_message();
}
void Menu::webtiles_update_scroll_pos(bool force) const
{
tiles.json_open_object();
tiles.json_write_string("msg", "menu_scroll");
tiles.json_write_int("first", get_first_visible());
tiles.json_write_int("last_hovered", last_hovered);
tiles.json_write_bool("force", force);
tiles.json_close_object();
tiles.finish_message();
}
void Menu::webtiles_write_title() const
{
// the title object only exists for backwards compatibility
tiles.json_open_object("title");
tiles.json_write_string("text", _webtiles_title.to_colour_string(LIGHTGRAY));
tiles.json_close_object("title");
}
void Menu::webtiles_write_tiles(const MenuEntry& me) const
{
vector<tile_def> t;
if (me.get_tiles(t) && !t.empty())
{
tiles.json_open_array("tiles");
for (const tile_def &tile : t)
{
tiles.json_open_object();
tiles.json_write_int("t", tile.tile);
tiles.json_write_int("tex", get_tile_texture(tile.tile));
if (tile.ymax != TILE_Y)
tiles.json_write_int("ymax", tile.ymax);
tiles.json_close_object();
}
tiles.json_close_array();
}
}
void Menu::webtiles_write_item(const MenuEntry* me) const
{
tiles.json_open_object();
if (me)
tiles.json_write_string("text", me->get_text());
else
{
tiles.json_write_string("text", "");
tiles.json_close_object();
return;
}
if (me->quantity)
tiles.json_write_int("q", me->quantity);
int col = item_colour(me);
if (col != MENU_ITEM_STOCK_COLOUR)
tiles.json_write_int("colour", col);
if (!me->hotkeys.empty())
{
tiles.json_open_array("hotkeys");
for (int hotkey : me->hotkeys)
tiles.json_write_int(hotkey);
tiles.json_close_array();
}
if (me->level != MEL_NONE)
tiles.json_write_int("level", me->level);
webtiles_write_tiles(*me);
tiles.json_close_object();
}
#endif // USE_TILE_WEB
/////////////////////////////////////////////////////////////////
// Menu colouring
//
int menu_colour(const string &text, const string &prefix, const string &tag, bool strict)
{
const string tmp_text = prefix + text;
for (const colour_mapping &cm : Options.menu_colour_mappings)
{
const bool match_any = !strict &&
(cm.tag.empty() || cm.tag == "item" || cm.tag == "any");
if ((match_any
|| cm.tag == tag || cm.tag == "inventory" && tag == "pickup")
&& cm.pattern.matches(tmp_text))
{
return cm.colour;
}
}
return -1;
}
int MenuHighlighter::entry_colour(const MenuEntry *entry) const
{
return entry->colour != MENU_ITEM_STOCK_COLOUR ? entry->colour
: entry->highlight_colour();
}
///////////////////////////////////////////////////////////////////////
// column_composer
column_composer::column_composer(int cols, ...)
: columns()
{
ASSERT(cols > 0);
va_list args;
va_start(args, cols);
columns.emplace_back(1);
int lastcol = 1;
for (int i = 1; i < cols; ++i)
{
int nextcol = va_arg(args, int);
ASSERT(nextcol > lastcol);
lastcol = nextcol;
columns.emplace_back(nextcol);
}
va_end(args);
}
column_composer::column_composer(int cols, vector<int> widths)
: columns()
{
ASSERT(cols > 0 && (int)widths.size() >= cols);
columns.emplace_back(1);
int lastcol = 1;
for (int i = 0; i < cols; ++i)
{
int nextcol = widths[i];
ASSERT(nextcol > lastcol);
lastcol = nextcol;
columns.emplace_back(nextcol);
}
}
void column_composer::clear()
{
flines.clear();
}
void column_composer::add_formatted(int ncol,
const string &s,
bool add_separator,
int margin,
bool centered,
colour_t colour)
{
ASSERT_RANGE(ncol, 0, (int) columns.size());
column &col = columns[ncol];
vector<string> segs = split_string("\n", s, false, true);
vector<formatted_string> newlines;
// Add a blank line if necessary. Blank lines will not
// be added at page boundaries.
if (add_separator && col.lines && !segs.empty())
newlines.emplace_back(formatted_string::parse_string("", colour));
for (const string &seg : segs)
newlines.push_back(formatted_string::parse_string(seg, colour));
strip_blank_lines(newlines);
if (centered && ncol < (int)columns.size() - 1)
{
for (size_t i = 0; i < newlines.size(); ++i)
{
const int delta = columns[ncol+1].margin - col.margin - newlines[i].width();
const int pad_left = max(0, delta / 2 - 1);
const int pad_right = delta - pad_left;
formatted_string new_str = formatted_string::parse_string(string(pad_left, ' '));
new_str += newlines[i];
new_str.cprintf("%-*s", pad_right, "");
newlines[i] = new_str;
}
}
compose_formatted_column(newlines,
col.lines,
margin == -1 ? col.margin : margin);
col.lines += newlines.size();
strip_blank_lines(flines);
}
vector<formatted_string> column_composer::formatted_lines() const
{
return flines;
}
void column_composer::strip_blank_lines(vector<formatted_string> &fs) const
{
for (int i = fs.size() - 1; i >= 0; --i)
{
if (fs[i].width() == 0)
fs.erase(fs.begin() + i);
else
break;
}
}
void column_composer::compose_formatted_column(
const vector<formatted_string> &lines,
int startline,
int margin)
{
if (flines.size() < startline + lines.size())
flines.resize(startline + lines.size());
for (unsigned i = 0, size = lines.size(); i < size; ++i)
{
int f = i + startline;
if (margin > 1)
{
int xdelta = margin - flines[f].width() - 1;
if (xdelta > 0)
flines[f].cprintf("%-*s", xdelta, "");
}
flines[f] += lines[i];
}
}
int linebreak_string(string& s, int maxcol, bool indent, int force_indent)
{
// [ds] Don't loop forever if the user is playing silly games with
// their term size.
if (maxcol < 1)
return 0;
int breakcount = 0;
string res;
while (!s.empty())
{
res += wordwrap_line(s, maxcol, true, indent, force_indent);
if (!s.empty())
{
res += "\n";
breakcount++;
}
}
s = res;
return breakcount;
}
string get_linebreak_string(const string& s, int maxcol)
{
string r = s;
linebreak_string(r, maxcol);
return r;
}
void ToggleableMenu::add_toggle_from_command(command_type cmd)
{
// use this to align toggle with a command, e.g. CMD_MENU_CYCLE_MODE
// XX do away with ToggleableMenu and just use Menu code for this?
const vector<int> keys = command_to_keys(cmd);
for (auto k : keys)
add_toggle_key(k);
}
// TODO: do away with this somehow? Maybe this whole subclass isn't really
// needed?
int ToggleableMenu::pre_process(int key)
{
if (find(toggle_keys.begin(), toggle_keys.end(), key) != toggle_keys.end())
{
// Toggle all menu entries
for (MenuEntry *item : items)
if (auto p = dynamic_cast<ToggleableMenuEntry*>(item))
p->toggle();
// Toggle title
if (auto pt = dynamic_cast<ToggleableMenuEntry*>(title))
pt->toggle();
update_menu();
#ifdef USE_TILE_WEB
webtiles_update_items(0, items.size() - 1);
#endif
if (flags & MF_TOGGLE_ACTION)
{
if (menu_action == ACT_EXECUTE)
menu_action = ACT_EXAMINE;
else
menu_action = ACT_EXECUTE;
}
// Don't further process the key
return 0;
}
return key;
}
|