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
|
-- LifeViewer for Golly (work in progress)
-- Author: Chris Rowett (crowett@gmail.com), September 2016.
--
-- LifeViewer is a scriptable pattern viewer featuring:
-- - Rotation and smooth non-integer zoom.
-- - Color themes with cell history and longevity.
-- - Automatic hex grid display.
-- - Pseudo 3D layers and stars.
--
-- This is the Lua version of the Javascript/HTML5 LifeViewer
-- used on the conwaylife.com forums and the LifeWiki.
--
-- Press H after running lifeviewer.lua to display the list of
-- supported keyboard shortcuts and script commands.
--
-- To see some example LifeViewer script commands in action, open
-- Patterns/Life/Rakes/c2-Cordership-rake.rle, then run lifeviewer.lua.
-- build number
local buildnumber = 30
local g = golly()
local ov = g.overlay
local gp = require "gplus"
local op = require "oplus"
local a = require "gplus.strict"
local viewwd, viewht = g.getview(g.getlayer())
-- update flags
local update = {
docells = false,
docamera = false,
dorefresh = false,
dostatus = false
}
-- textalert
local textalert = {
appear = 0,
hold = 0,
disappear = 0,
color = "32 255 255 255",
fontsize = 30,
message = "",
starttime = 0
}
-- timing
local timing = {
updatecells = 0,
drawcells = 0,
drawmenu = 0,
texttime = 0,
show = false,
extended = false,
genstarttime = 0,
framestarttime = 0,
frameendtime = 0
}
-- whether generating life
local generating = false
local autostart = false
local loopgen = -1
local stopgen = -1
local currentgen = 0
-- view constants
local viewconstants = {
minzoom = 0.0625, -- 1/16
maxzoom = 32,
minangle = 0,
maxangle = 360,
viewwidth = 2048,
viewheight = 2048,
mindepth = 0,
maxdepth = 1,
minlayers = 1,
maxlayers = 10,
zoomborder = 0.9, -- proportion of view to fill with fit zoom
glidesteps = 40, -- steps to glide camera
minstop = 1,
minloop = 1,
mintheme = 0,
maxtheme = #op.themes,
minpan = -4096,
maxpan = 4096,
mingps = 1,
maxgps = 60,
minstep = 1,
maxstep = 50,
decaysteps = 64, -- number of steps to decay after life finished
mingridmajor = 0,
maxgridmajor = 16,
minviewerwidth = 480,
maxviewerwidth = 4096,
minviewerheight = 480,
maxviewerheight = 4096
}
viewconstants.mininvzoom = -1 / viewconstants.minzoom
-- playback speed
local defgps = 60
local defstep = 1
local gps = defgps
local step = defstep
-- smooth camera movement
local startx
local starty
local startangle
local startzoom
local endx
local endy
local endangle
local endzoom
local camsteps = -1
-- camera defaults
local defcamx = viewconstants.viewwidth / 2
local defcamy = viewconstants.viewheight / 2
local defcamangle = 0
local defcamlayers = 1
local defcamlayerdepth = 0.05
local deflinearzoom = 0.4
-- camera
local camx = defcamx
local camy = defcamy
local camangle = defcamangle
local camlayers = defcamlayers
local camlayerdepth = defcamlayerdepth
local autofit = false
local historyfit = false
local decay = 0
-- tracking
local tracking = {
east = 0,
south = 0,
west = 0,
north = 0,
period = 0,
defined = false
}
local initialrect -- initial bounding box for pattern at gen 0
local initialzoom = 1 -- initial fit zoom value at gen 0
local histleftx -- used for autofit history mode
local histrightx
local histbottomy
local histtopy
-- origin
local originx = 0
local originy = 0
local originz = 1
-- zoom is held as a value 0..1 for smooth linear zooms
local linearzoom
-- theme
local themeon = true
local theme = 1
-- mouse drag
local lastmousex = 0
local lastmousey = 0
local mousedrag = false
local clickx = 0
local clicky = 0
-- whether hex display is on
local hexon = false
-- grid
local grid = false
local gridmajoron = true
local gridmajor = 10
-- stars
local stars = false
-- reset
local hardreset = false
-- menu button images
local buttons = {
["play"] = "button0",
["pause"] = "button1",
["reset"] = "button2",
["menu"] = "button3",
["stepback"] = "button4",
["stepforward"] = "button5",
["autofit"] = "button6",
["fit"] = "button7",
["squaregrid"] = "button8",
["help"] = "button9",
["shrink"] = "button10",
["fps"] = "button11",
["hexgrid"] = "button12"
}
-- script tokens
local tokens = {}
local curtok = 1
local arguments
local rawarguments -- the script text before conversion
local commands = {
scriptstartword = "[[",
scriptendword = "]]",
angleword = "ANGLE",
autofitword = "AUTOFIT",
autostartword = "AUTOSTART",
depthword = "DEPTH",
extendedtimingword = "EXTENDEDTIMING",
gpsword = "GPS",
gridword = "GRID",
gridmajorword = "GRIDMAJOR",
hardresetword = "HARDRESET",
heightword = "HEIGHT",
hexdisplayword = "HEXDISPLAY",
historyfitword = "HISTORYFIT",
layersword = "LAYERS",
loopword = "LOOP",
pauseword = "PAUSE",
showtimingword = "SHOWTIMING",
squaredisplayword = "SQUAREDISPLAY",
starsword = "STARS",
stepword = "STEP",
stopword = "STOP",
tword = "T",
themeword = "THEME",
trackword = "TRACK",
trackboxword = "TRACKBOX",
trackloopword = "TRACKLOOP",
widthword = "WIDTH",
xword = "X",
yword = "Y",
zoomword = "ZOOM",
zword = "Z"
}
-- keyword decoding
-- each argument has a type followed by constraint values
-- "r" - float range
-- "R" - int range
-- "l" - float lower bound
-- "L" - int lower bound
local keywords = {
[commands.angleword] = { "r", viewconstants.minangle, viewconstants.maxangle, "" },
[commands.autofitword] = { "" },
[commands.autostartword] = { "" },
[commands.depthword] = { "r", viewconstants.mindepth, viewconstants.maxdepth, "" },
[commands.extendedtimingword] = { "" },
[commands.gpsword] = { "r", viewconstants.mingps, viewconstants.maxgps, "" },
[commands.gridword] = { "" },
[commands.gridmajorword] = { "R", viewconstants.mingridmajor, viewconstants.maxgridmajor, "" },
[commands.hardresetword] = { "" },
[commands.heightword] = { "R", viewconstants.minviewerheight, viewconstants.maxviewerheight, "" },
[commands.hexdisplayword] = { "" },
[commands.historyfitword] = { "" },
[commands.layersword] = { "R", viewconstants.minlayers, viewconstants.maxlayers, "" },
[commands.loopword] = { "L", viewconstants.minloop, "" },
[commands.pauseword] = { "l", 0.0, "" },
[commands.showtimingword] = { "" },
[commands.squaredisplayword] = { "" },
[commands.starsword] = { "" },
[commands.stepword] = { "r", viewconstants.minstep, viewconstants.maxstep, "" },
[commands.stopword] = { "L", viewconstants.minstop, "" },
[commands.tword] = { "L", 0, "" },
[commands.themeword] = { "R", viewconstants.mintheme, viewconstants.maxtheme, "" },
[commands.trackword] = { "r", -1, 1, "r", -1, 1, "" },
[commands.trackboxword] = { "r", -1, 1, "r", -1, 1, "r", -1, 1, "r", -1, 1, "" },
[commands.trackloopword] = { "L", 1, "r", -1, 1, "r", -1, 1, "" },
[commands.widthword] = { "R", viewconstants.minviewerwidth, viewconstants.maxviewerwidth, "" },
[commands.xword] = { "r", viewconstants.minpan, viewconstants.maxpan, "" },
[commands.yword] = { "r", viewconstants.minpan, viewconstants.maxpan, "" },
[commands.zoomword] = { "r", viewconstants.mininvzoom, viewconstants.maxzoom, "" },
[commands.zword] = { "r", viewconstants.mininvzoom, viewconstants.maxzoom, "" }
}
-- pre-rendered text clips
local clips = {
timinglongfg = "tlongfg",
timinglongshadow = "tlongshadow",
shortht = 0,
longht = 0
}
--------------------------------------------------------------------------------
local function maketext(s, name)
name = name or "temp"
-- convert string to text in current font
local w, h, descent = gp.split(ov("text "..name.." "..s))
return tonumber(w), tonumber(h), tonumber(descent)
end
--------------------------------------------------------------------------------
local function pastetext(x, y, name)
name = name or "temp"
ov("paste "..x.." "..y.." "..name)
end
--------------------------------------------------------------------------------
local function makeclips()
local oldalign = ov("textoption align left")
local oldtextbg = ov("textoption background 0 0 0 0")
local label = "fps"
local longlabel = "updatecells\ndrawcells\nmenu"
ov(op.black)
local w, h = maketext(longlabel, clips.timinglongshadow)
clips.longht = h
ov(op.white)
w, h = maketext(label)
clips.shortht = h
maketext(longlabel, clips.timinglongfg)
-- restore settings
ov("textoption background "..oldtextbg)
ov("textoption align "..oldalign)
end
--------------------------------------------------------------------------------
local function outputtiming(xoff, yoff, labelclip)
-- draw labels
ov("paste "..(viewwd - 140 + xoff).." "..(20 + yoff + clips.shortht).." "..labelclip)
-- draw values
local output = string.format("%.1fms", timing.updatecells)
output = output.."\n"..string.format("%.1fms", timing.drawcells)
output = output.."\n"..string.format("%.1fms", timing.drawmenu)
ov("textoption align right")
local w = maketext(output)
pastetext(viewwd - w - 20 + xoff, 20 + yoff + clips.shortht)
end
--------------------------------------------------------------------------------
local function drawmenu()
local y = 10
local i = 0
ov("blend 1")
while i < 13 do
-- TBD ov("paste "..(i * 50).." "..y.. "button"..i)
i = i + 1
end
ov("blend 0")
end
--------------------------------------------------------------------------------
local function drawtiming()
local start = g.millisecs()
local oldblend = ov("blend 1")
local oldalign = ov("textoption align left")
local oldtextbg = ov("textoption background 0 0 0 0")
-- draw translucent rectangle
local height = clips.shortht
if timing.extended then
height = height + clips.longht
end
ov("rgba 0 0 0 128")
ov("fill "..(viewwd - 140).." 20 122 "..height)
-- compute load
local frametime = timing.frameendtime - timing.framestarttime
local loadamount = frametime / 16
if loadamount > 1 then
loadamount = 1
end
if frametime < 16.666 then
frametime = 16.666
end
-- draw load background
if (loadamount < 0.5) then
-- fade from green to yellow
ov("rgba "..math.floor(255 * loadamount * 2).." 255 0 144")
else
-- fade from yellow to red
ov("rgba 255 "..math.floor(255 * (1 - (loadamount - 0.5) * 2)).." 0 144")
end
ov("fill "..(viewwd - 140).." 20 "..math.floor(122 * loadamount).." "..clips.shortht)
-- draw fps and load
ov(op.black)
maketext(math.floor(1000 / frametime).."fps")
pastetext(viewwd - 138, 22)
local w = maketext(math.floor(100 * loadamount).."%")
pastetext(viewwd - 18 - w, 22)
ov(op.white)
maketext(math.floor(1000 / frametime).."fps")
pastetext(viewwd - 140, 20)
maketext(math.floor(100 * loadamount).."%")
pastetext(viewwd - w - 20, 20)
-- draw extended timing if enabled
local shadowclip, fgclip
if timing.extended then
shadowclip = clips.timinglongshadow
fgclip = clips.timinglongfg
ov(op.black)
outputtiming(2, 2, shadowclip)
ov(op.white)
outputtiming(0, 0, fgclip)
end
-- restore settings
ov("textoption background "..oldtextbg)
ov("textoption align "..oldalign)
ov("blend "..oldblend)
timing.texttime = g.millisecs() - start
end
--------------------------------------------------------------------------------
local function alert(message, appear, hold, disappear)
textalert.message = message
textalert.appear = appear
textalert.hold = hold
textalert.disappear = disappear
end
--------------------------------------------------------------------------------
local function updatealert()
if textalert.message ~= "" then
end
end
--------------------------------------------------------------------------------
local function realtolinear(zoom)
return math.log(zoom / viewconstants.minzoom) / math.log(viewconstants.maxzoom / viewconstants.minzoom)
end
--------------------------------------------------------------------------------
local function lineartoreal(zoom)
return viewconstants.minzoom * math.pow(viewconstants.maxzoom / viewconstants.minzoom, zoom)
end
--------------------------------------------------------------------------------
local function updatestatus()
-- convert zoom to actual value
local zoom = lineartoreal(linearzoom) * originz
if zoom < 0.99999 then
zoom = -(1 / zoom)
end
-- convert x and y to display coordinates
local x = camx - viewconstants.viewwidth / 2 + originx
local y = camy - viewconstants.viewheight / 2 + originy
if hexon then
x = x + camy / 2
end
-- convert theme to status
local themestatus = "off"
if themeon then
themestatus = theme
end
-- convert autofit to status
local autofitstatus = "off"
if autofit then
autofitstatus = "on"
end
-- convert hex mode to status
local hexstatus = "square"
if hexon then
hexstatus = "hex"
end
-- convert grid to status
local gridstatus = "off"
if grid then
gridstatus = "on"
end
-- convert angle to status
local displayangle = camangle
if hexon then
displayangle = 0
end
-- update status bar
local status = "Hit escape to close."
status = status.." Zoom "..string.format("%.1f", zoom).."x Angle "..displayangle
status = status.." X "..string.format("%.1f", x).." Y "..string.format("%.1f", y)
status = status.." Layers "..camlayers.." Depth "..string.format("%.2f",camlayerdepth)
status = status.." GPS "..gps.." Step "..step.." Theme "..themestatus
status = status.." Autofit "..autofitstatus.." Mode "..hexstatus.." Grid "..gridstatus
if stopgen ~= -1 then
status = status.." Stop "..stopgen
end
if loopgen ~= -1 then
status = status.." Loop "..loopgen
end
g.show(status)
end
--------------------------------------------------------------------------------
local function refresh()
local start = g.millisecs()
local newx, newy, newz
-- add the fractional part to the current generation
if tracking.defined then
local fractionalgen = (g.millisecs() - timing.genstarttime) / 1000 * gps
if fractionalgen < 0 then
fractionalgen = 0
else
if fractionalgen > 1 then
fractionalgen = 1
end
end
local gen = currentgen + fractionalgen
-- compute the new origin
originx = gen * (tracking.east + tracking.west) / 2
originy = gen * (tracking.south + tracking.north) / 2
-- compute the trackbox
local leftx = initialrect[1]
local bottomy = initialrect[2]
local width = initialrect[3]
local height = initialrect[4]
local rightx = leftx + width
local topy = bottomy + height
leftx = leftx + gen * tracking.west
rightx = rightx + gen * tracking.east
bottomy = bottomy + gen * tracking.north
topy = topy + gen * tracking.south
width = rightx - leftx + 1
height = topy - bottomy + 1
-- check for hex
if hexon then
leftx = leftx - bottomy / 2
rightx = rightx - topy / 2
width = rightx - leftx + 1
end
-- get the smallest zoom needed
local zoom = viewwd / width
if zoom > viewht / height then
zoom = viewht / height
end
-- leave a border around the pattern
zoom = zoom * viewconstants.zoomborder
-- apply the origin to the camera
originz = zoom / initialzoom
newx = camx + originx
newy = camy + originy
newz = lineartoreal(linearzoom) * originz
if newz > viewconstants.maxzoom then
newz = viewconstants.maxzoom
else
if newz < viewconstants.minzoom then
newz = viewconstants.minzoom
end
end
-- set the new camera settings
ov("camera xy "..newx.." "..newy)
ov("camera zoom "..newz)
-- update status because camera changed
update.dostatus = true
else
originx = 0
originy = 0
originz = 1
end
-- draw the cells
ov("drawcells")
timing.drawcells = g.millisecs() - start
-- draw the menu
local t1 = g.millisecs()
drawmenu()
timing.drawmenu = g.millisecs() - t1
-- end of frame time
timing.frameendtime = g.millisecs()
-- draw timing if on
if timing.show then
drawtiming()
end
-- update the overlay
ov("update")
end
--------------------------------------------------------------------------------
local function setgridlines()
if grid then
ov("celloption grid 1")
else
ov("celloption grid 0")
end
if gridmajoron then
ov("celloption gridmajor "..gridmajor)
else
ov("celloption gridmajor 0")
end
end
--------------------------------------------------------------------------------
local function setstars()
if stars then
ov("celloption stars 1")
else
ov("celloption stars 0")
end
end
--------------------------------------------------------------------------------
local function sethex()
if hexon then
ov("celloption hex 1")
else
ov("celloption hex 0")
end
ov("camera xy "..camx.." "..camy)
end
--------------------------------------------------------------------------------
local function checkhex()
hexon = op.hexrule()
sethex()
end
--------------------------------------------------------------------------------
local function showhelp()
-- open help window
g.open(g.getdir("app").."Help/lifeviewer.html")
end
--------------------------------------------------------------------------------
local function getmouseposition()
local x = viewwd / 2
local y = viewht / 2
local mousepos = ov("xy")
if mousepos ~= "" then
x, y = gp.split(mousepos)
x = tonumber(x)
y = tonumber(y)
end
return x, y
end
--------------------------------------------------------------------------------
local function createoverlay()
-- create overlay over entire viewport
ov("create "..viewwd.." "..viewht)
end
--------------------------------------------------------------------------------
local function updatecells()
local start = g.millisecs()
ov("updatecells")
timing.updatecells = g.millisecs() - start
end
--------------------------------------------------------------------------------
local function updatecamera()
-- convert linear zoom to real zoom
local camzoom = lineartoreal(linearzoom)
ov("camera zoom "..camzoom)
ov("camera angle "..camangle)
ov("camera xy "..camx.." "..camy)
ov("celloption layers "..camlayers)
ov("celloption depth "..camlayerdepth)
-- update status
update.dostatus = true
end
--------------------------------------------------------------------------------
local function resetcamera()
-- restore default camera settings
camx = defcamx
camy = defcamy
camangle = defcamangle
camlayers = defcamlayers
camlayerdepth = defcamlayerdepth
linearzoom = deflinearzoom
update.docamera = true
end
--------------------------------------------------------------------------------
local function setdefaultcamera()
defcamx = camx
defcamy = camy
defcamangle = camangle
deflinearzoom = linearzoom
defcamlayers = camlayers
defcamlayerdepth = camlayerdepth
end
--------------------------------------------------------------------------------
local function togglehex()
hexon = not hexon
sethex()
if hexon then
camx = camx - camy / 2
defcamx = defcamx - camy / 2
else
camx = camx + camy / 2
defcamx = defcamx + camy / 2
end
update.docamera = true
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function togglegrid()
grid = not grid
setgridlines()
update.dostatus = true
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function togglegridmajor()
gridmajoron = not gridmajoron
setgridlines()
update.dostatus = true
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function togglestars()
stars = not stars
setstars()
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function bezierx(t, x0, x1, x2, x3)
local cX = 3 * (x1 - x0)
local bX = 3 * (x2 - x1) - cX
local aX = x3 - x0 - cX - bX
-- compute x position
local x = (aX * math.pow(t, 3)) + (bX * math.pow(t, 2)) + (cX * t) + x0
return x
end
--------------------------------------------------------------------------------
local function glidecamera()
local linearcomplete = camsteps / viewconstants.glidesteps
local beziercomplete = bezierx(linearcomplete, 0, 0, 1, 1)
camx = startx + beziercomplete * (endx - startx)
camy = starty + beziercomplete * (endy - starty)
local camzoom = startzoom + beziercomplete * (endzoom - startzoom)
linearzoom = realtolinear(camzoom)
camsteps = camsteps + 1
if camsteps > viewconstants.glidesteps then
camsteps = -1
camx = endx
camy = endy
linearzoom = realtolinear(endzoom)
end
update.docamera = true
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function createcellview()
ov("cellview "..math.floor(-viewconstants.viewwidth / 2).." "..math.floor(-viewconstants.viewheight / 2).. " "..viewconstants.viewwidth.." "..viewconstants.viewheight)
end
--------------------------------------------------------------------------------
local function fitzoom(immediate)
local rect = g.getrect()
if #rect > 0 then
local leftx = rect[1]
local bottomy = rect[2]
local width = rect[3]
local height = rect[4]
local rightx = leftx + width
local topy = bottomy + height
-- check for historyfit
if historyfit then
-- update history bounding box
if histleftx > leftx then
histleftx = leftx
end
if histrightx < rightx then
histrightx = rightx
end
if histbottomy > bottomy then
histbottomy = bottomy
end
if histtopy < topy then
histtopy = topy
end
leftx = histleftx
rightx = histrightx
bottomy = histbottomy
topy = histtopy
width = rightx - leftx + 1
height = topy - bottomy + 1
end
-- check for hex
if hexon then
leftx = leftx - bottomy / 2
rightx = rightx - topy / 2
width = rightx - leftx + 1
end
-- get the smallest zoom needed
local zoom = viewwd / width
if zoom > viewht / height then
zoom = viewht / height
end
-- leave a border around the pattern
zoom = zoom * viewconstants.zoomborder
-- apply origin
if tracking.defined then
zoom = zoom / originz
end
-- ensure zoom in range
if zoom < viewconstants.minzoom then
zoom = viewconstants.minzoom
else
if zoom > viewconstants.maxzoom then
zoom = viewconstants.maxzoom
end
end
-- get new pan position
local newx = viewconstants.viewwidth / 2 + leftx + width / 2 + originy
local newy = viewconstants.viewheight / 2 + bottomy + height / 2 + originx
if hexon then
newx = newx - newy / 2
end
-- check whether to fit immediately
if immediate then
camx = newx
camy = newy
linearzoom = realtolinear(zoom)
update.docamera = true
else
-- setup start point
startx = camx
starty = camy
startangle = camangle
startzoom = lineartoreal(linearzoom)
-- setup destination point
endx = newx
endy = newy
endangle = camangle
endzoom = zoom
-- trigger start
camsteps = 0
end
end
end
--------------------------------------------------------------------------------
local function advance(singlestep)
-- check if all cells are dead
if g.empty() then
generating = false
if decay > 0 then
update.docells = true
update.dorefresh = true
decay = decay - 1
end
return
end
-- check for single step
local remaining = step
if singlestep then
remaining = 1
end
-- check if enough time has elapsed to advance
local deltatime = (g.millisecs() - timing.genstarttime) / 1000
if deltatime > 1 / gps then
while remaining > 0 do
g.run(1)
updatecells()
if g.empty() then
refresh()
g.note("Life ended at generation "..tonumber(g.getgen()))
remaining = 0
decay = viewconstants.decaysteps
else
remaining = remaining - 1
end
end
timing.genstarttime = g.millisecs()
end
currentgen = tonumber(g.getgen())
if autofit then
fitzoom(true)
end
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function rotate(amount)
camangle = camangle + amount
if camangle >= viewconstants.maxangle then
camangle = camangle - viewconstants.maxangle
else
if camangle < viewconstants.minangle then
camangle = camangle + viewconstants.maxangle
end
end
update.docamera = true
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function adjustzoom(amount, x, y)
local newx = 0
local newy = 0
local newzoom
-- get the current zoom as actual zoom
local currentzoom = lineartoreal(linearzoom)
-- compute new zoom and ensure it is in range
linearzoom = linearzoom + amount
if linearzoom < 0 then
linearzoom = 0
else
if linearzoom > 1 then
linearzoom = 1
end
end
local sinangle = math.sin(-camangle / 180 * math.pi)
local cosangle = math.cos(-camangle / 180 * math.pi)
local dx = 0
local dy = 0
-- convert new zoom to actual zoom
newzoom = lineartoreal(linearzoom)
-- compute as an offset from the centre
x = x - viewwd / 2
y = y - viewht / 2
-- compute position based on new zoom
newx = x * (newzoom / currentzoom)
newy = y * (newzoom / currentzoom)
dx = (x - newx) / newzoom
dy = -(y - newy) / newzoom
-- apply pan
camx = camx - dx * cosangle + dy * -sinangle
camy = camy - dx * sinangle + dy * cosangle
end
--------------------------------------------------------------------------------
local function zoominto(event)
local _, x, y = gp.split(event)
adjustzoom(0.05, x, y)
update.docamera = true
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function zoomoutfrom(event)
local _, x, y = gp.split(event)
adjustzoom(-0.05, x, y)
update.docamera = true
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function zoomout()
local x, y = getmouseposition()
adjustzoom(-0.01, x, y)
update.docamera = true
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function zoomin()
local x, y = getmouseposition()
adjustzoom(0.01, x, y)
update.docamera = true
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function resetangle()
camangle = 0
update.docamera = true
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function setzoom(zoom)
startx = camx
starty = camy
startzoom = lineartoreal(linearzoom)
startangle = camangle
endx = camx
endy = camy
endzoom = zoom
endangle = camangle
camsteps = 0
if tracking.defined then
startzoom = startzoom / originz
endzoom = endzoom / originz
end
end
--------------------------------------------------------------------------------
local function integerzoom()
local camzoom = lineartoreal(linearzoom)
if camzoom >= 1 then
camzoom = math.floor(camzoom + 0.5)
else
camzoom = 1 / math.floor(1 / camzoom + 0.5)
end
setzoom(camzoom)
end
--------------------------------------------------------------------------------
local function halvezoom()
local camzoom = lineartoreal(linearzoom)
camzoom = camzoom / 2
if camzoom < viewconstants.minzoom then
camzoom = viewconstants.minzoom
end
setzoom(camzoom)
end
--------------------------------------------------------------------------------
local function doublezoom()
local camzoom = lineartoreal(linearzoom)
camzoom = camzoom * 2
if camzoom > viewconstants.maxzoom then
camzoom = viewconstants.maxzoom
end
setzoom(camzoom)
end
--------------------------------------------------------------------------------
local function settheme()
local index = -1
if themeon then
index = theme
end
ov(op.themes[index])
update.docells = true
update.dostatus = true
end
--------------------------------------------------------------------------------
local function cycletheme()
if themeon then
theme = theme + 1
if theme > viewconstants.maxtheme then
theme = viewconstants.mintheme
end
else
themeon = true
end
settheme()
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function toggletheme()
themeon = not themeon
settheme()
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function decreaselayerdepth()
if camlayerdepth > viewconstants.mindepth then
camlayerdepth = camlayerdepth - 0.01
if camlayerdepth < viewconstants.mindepth then
camlayerdepth = viewconstants.mindepth
end
update.docamera = true
update.dorefresh = true
end
end
--------------------------------------------------------------------------------
local function increaselayerdepth()
if camlayerdepth < viewconstants.maxdepth then
camlayerdepth = camlayerdepth + 0.01
if camlayerdepth > viewconstants.maxdepth then
camlayerdepth = viewconstants.maxdepth
end
update.docamera = true
update.dorefresh = true
end
end
--------------------------------------------------------------------------------
local function decreaselayers()
if camlayers > viewconstants.minlayers then
camlayers = camlayers - 1
update.docamera = true
update.dorefresh = true
end
end
--------------------------------------------------------------------------------
local function increaselayers()
if camlayers < viewconstants.maxlayers then
camlayers = camlayers + 1
update.docamera = true
update.dorefresh = true
end
end
--------------------------------------------------------------------------------
local function panview(dx, dy)
local camzoom = lineartoreal(linearzoom)
dx = dx / camzoom
dy = dy / camzoom
local angle = camangle
if hexon then
angle = 0
end
local sinangle = math.sin(-angle / 180 * math.pi)
local cosangle = math.cos(-angle / 180 * math.pi)
camx = camx + (dx * cosangle + dy * -sinangle)
camy = camy + (dx * sinangle + dy * cosangle)
update.docamera = true
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function doclick(event)
local _, x, y, button, mode = gp.split(event)
-- start of drag
lastmousex = tonumber(x)
lastmousey = tonumber(y)
mousedrag = true
end
--------------------------------------------------------------------------------
local function stopdrag()
mousedrag = false
end
--------------------------------------------------------------------------------
local function checkdrag()
local x, y
-- check if a drag is in progress
if mousedrag then
-- get the mouse position
local mousepos = ov("xy")
if #mousepos > 0 then
x, y = gp.split(mousepos)
panview(lastmousex - tonumber(x), lastmousey - tonumber(y))
lastmousex = x
lastmousey = y
end
end
end
--------------------------------------------------------------------------------
local function resethistory()
histleftx = initialrect[1]
histbottomy = initialrect[2]
histrightx = histleftx + initialrect[3] - 1
histtopy = histbottomy + initialrect[4] - 1
end
--------------------------------------------------------------------------------
local function reset(looping)
-- reset the camera if at generation 0
local gen = tonumber(g.getgen())
if gen == 0 or looping or hardreset then
resetcamera()
if looping then
generating = true
else
generating = autostart
end
else
generating = false
end
-- reset historyfit
resethistory()
-- reset origin
originx = 0
originy = 0
originz = 1
-- reset golly
g.reset()
g.update()
currentgen = 0
-- if using theme then need to recreate the cell view to clear history
if theme ~= -1 then
createcellview()
setstars()
settheme()
updatecamera()
sethex()
setgridlines()
end
-- update cell view from reset universe
update.docells = true
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function toggleautofit()
autofit = not autofit
update.dostatus = true
end
--------------------------------------------------------------------------------
local function togglehistoryfit()
historyfit = not historyfit
update.dostatus = true
end
--------------------------------------------------------------------------------
local function readtokens()
local comments = g.getinfo()
local inscript = false
-- build token list
for token in comments:gsub("\n", " "):gmatch("([^ ]*)") do
if token ~= "" then
if token == commands.scriptendword then
inscript = false
end
if inscript then
table.insert(tokens, token)
end
if token == commands.scriptstartword then
inscript = true
end
end
end
end
--------------------------------------------------------------------------------
local function numberorfraction(argument)
local result = nil
-- search for a fraction
local index = argument:find("/")
if index == nil then
-- simple number
result = tonumber(argument)
else
-- possible fraction
local left = argument:sub(1, index - 1)
local right = argument:sub(index + 1)
if tonumber(left) ~= nil and tonumber(right) ~= nil then
result = left / right
end
end
return result
end
--------------------------------------------------------------------------------
local function validatekeyword(token)
local invalid = ""
local keyword = keywords[token]
local validation
local index = 1
local lower, upper
local argvalue
local argnum = 1
local whicharg
local arglist = ""
if keyword == nil then
invalid = "unknown script command"
else
-- check what sort of validation is required for this keyword
arguments = {}
rawarguments = {}
validation = keyword[index]
while validation ~= "" do
-- need an argument
whicharg = arglist
if curtok >= #tokens then
invalid = whicharg.."{{}}\nArgument "..argnum.." is missing"
else
-- get the argument
curtok = curtok + 1
argvalue = tokens[curtok]
whicharg = whicharg.." {{"..argvalue.."}}\nArgument "..argnum
-- check which sort of validation to perform
if validation == "l" then
-- float lower bound
argvalue = numberorfraction(argvalue)
if argvalue == nil then
invalid = whicharg.." must be a number"
else
index = index + 1
lower = keyword[index]
if argvalue < lower then
invalid = whicharg.." must be at least "..lower
end
end
elseif validation == "L" then
-- integer lower bound
argvalue = tonumber(argvalue)
if argvalue == nil then
invalid = whicharg.." must be an integer"
else
if argvalue ~= math.floor(argvalue) then
invalid = whicharg.." must be an integer"
else
index = index + 1
lower = keyword[index]
if argvalue < lower then
invalid = whicharg.." must be at least "..lower
end
end
end
elseif validation == "r" then
-- float range
argvalue = numberorfraction(argvalue)
if argvalue == nil then
invalid = whicharg.." must be a number"
else
index = index + 1
lower = keyword[index]
index = index + 1
upper = keyword[index]
if argvalue < lower or argvalue > upper then
invalid = whicharg.." must be between "..lower.." and "..upper
end
end
elseif validation == "R" then
-- integer range
argvalue = tonumber(argvalue)
if argvalue == nil then
invalid = whicharg.." must be an integer"
else
if argvalue ~= math.floor(argvalue) then
invalid = whicharg.." must be an integer"
else
index = index + 1
lower = keyword[index]
index = index + 1
upper = keyword[index]
if argvalue < lower or argvalue > upper then
invalid = whicharg.." must be between "..lower.." and "..upper
end
end
end
end
end
-- check if validation failed
if invalid ~= "" then
-- exit loop
validation = ""
else
-- save the argument
arguments[argnum] = argvalue
rawarguments[argnum] = tokens[curtok]
argnum = argnum + 1
arglist = arglist..tokens[curtok].." "
-- get next argument to validation
index = index + 1
validation = keyword[index]
end
end
end
return invalid
end
--------------------------------------------------------------------------------
local function checkscript()
local invalid = ""
local token
-- read tokens
readtokens()
-- process any script comments
if #tokens > 0 then
curtok = 1
while curtok <= #tokens do
-- get the next token
token = tokens[curtok]
-- check if it is a valid keyword
invalid = validatekeyword(token)
-- process command if valid
if invalid == "" then
if token == commands.angleword then
camangle = arguments[1]
elseif token == commands.autofitword then
autofit = true
elseif token == commands.autostartword then
autostart = true
generating = true
elseif token == commands.depthword then
camlayerdepth = arguments[1]
elseif token == commands.extendedtimingword then
timing.extended = true
elseif token == commands.gpsword then
gps = arguments[1]
elseif token == commands.gridword then
grid = true
elseif token == gridmajor then
gridmajor = arguments[1]
elseif token == commands.hardresetword then
hardreset = true
elseif token == commands.heightword then
-- ignored
elseif token == commands.hexdisplayword then
hexon = true
elseif token == commands.layersword then
camlayers = arguments[1]
elseif token == commands.historyfitword then
historyfit = true
elseif token == commands.loopword then
loopgen = arguments[1]
elseif token == commands.showtimingword then
timing.show = true
elseif token == commands.squaredisplayword then
hexon = false
elseif token == commands.starsword then
stars = true
elseif token == commands.stepword then
step = arguments[1]
elseif token == commands.stopword then
stopgen = arguments[1]
elseif token == commands.themeword then
theme = arguments[1]
elseif token == commands.trackword then
tracking.east = arguments[1]
tracking.south = arguments[2]
tracking.west = tracking.east
tracking.north = tracking.south
tracking.defined = true
elseif token == commands.trackboxword then
tracking.east = arguments[1]
tracking.south = arguments[2]
tracking.west = arguments[3]
tracking.north = arguments[4]
if tracking.west > tracking.east then
invalid = "[["..rawarguments[1].."]] "..rawarguments[2].." [["
invalid = invalid..rawarguments[3].."]] "..rawarguments[4]
invalid = invalid.."\nW is greater than E"
else
if tracking.north > tracking.south then
invalid = rawarguments[1].." [["..rawarguments[2].."]] "
invalid = invalid..rawarguments[3].." [["..rawarguments[4].."]]"
invalid = invalid.."\nN is greater than S"
else
tracking.defined = true
end
end
elseif token == commands.trackloopword then
tracking.period = arguments[1]
tracking.east = arguments[2]
tracking.south = arguments[3]
tracking.west = tracking.east
tracking.north = tracking.south
tracking.defined = true
elseif token == commands.widthword then
-- ignored
elseif token == commands.xword then
camx = arguments[1]
elseif token == commands.yword then
camy = arguments[1]
elseif token == commands.zoomword or token == commands.zword then
if arguments[1] < 0 then
arguments[1] = -1 / arguments[1]
end
linearzoom = realtolinear(arguments[1])
end
end
-- abort script processing on error
if invalid ~= "" then
curtok = #tokens
end
curtok = curtok + 1
end
-- check for trackloop
if tracking.defined and tracking.period > 0 then
if gridmajor > 0 then
loopgen = tracking.period * gridmajor
else
loopgen = tracking.period
end
end
setstars()
sethex()
if hexon then
camx = camx - camy / 2
end
setgridlines()
update.docamera = true
update.dorefresh = true
if invalid ~= "" then
g.note(token.." "..invalid)
end
end
end
--------------------------------------------------------------------------------
local function decreasespeed()
if step > viewconstants.minstep then
step = step - 1
else
if gps > viewconstants.mingps then
gps = gps - 1
end
end
update.dostatus = true
end
--------------------------------------------------------------------------------
local function increasespeed()
if gps < viewconstants.maxgps then
gps = gps + 1
else
if step < viewconstants.maxstep then
step = step + 1
end
end
update.dostatus = true
end
--------------------------------------------------------------------------------
local function setmaxspeed()
if gps < viewconstants.maxgps then
gps = viewconstants.maxgps
else
if step < viewconstants.maxstep then
step = viewconstants.maxstep
end
end
update.dostatus = true
end
--------------------------------------------------------------------------------
local function setminspeed()
if step > viewconstants.minstep then
step = viewconstants.minstep
else
if gps > viewconstants.mingps then
gps = viewconstants.mingps
end
end
update.dostatus = true
end
--------------------------------------------------------------------------------
local function increasestep()
if step < viewconstants.maxstep then
step = step + 1
update.dostatus = true
end
end
--------------------------------------------------------------------------------
local function decreasestep()
if step > viewconstants.minstep then
step = step - 1
update.dostatus = true
end
end
--------------------------------------------------------------------------------
local function setmaxstep()
step = viewconstants.maxstep
update.dostatus = true
end
--------------------------------------------------------------------------------
local function setminstep()
step = viewconstants.minstep
update.dostatus = true
end
--------------------------------------------------------------------------------
local function resetspeed()
gps = defgps
step = defstep
update.dostatus = true
end
--------------------------------------------------------------------------------
local function togglehardreset()
hardreset = not hardreset
end
--------------------------------------------------------------------------------
local function toggletiming()
timing.show = not timing.show
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function toggleextendedtiming()
timing.extended = not timing.extended
update.dorefresh = true
end
--------------------------------------------------------------------------------
local function cbauto()
toggleautofit()
end
--------------------------------------------------------------------------------
local function cbfit()
if not autofit then
fitzoom(false)
end
end
--------------------------------------------------------------------------------
local function cbgrid()
togglegrid()
end
--------------------------------------------------------------------------------
local function cbhelp()
showhelp()
end
--------------------------------------------------------------------------------
local function cbreset()
reset(false)
end
--------------------------------------------------------------------------------
local function cbplay()
generating = not generating
end
--------------------------------------------------------------------------------
local function cbfps()
toggletiming()
end
--------------------------------------------------------------------------------
local function createmenu()
ov("blend 0")
ov("create 1 1 menuclip")
ov("target menuclip")
-- load button images to get width and height
local w, h = gp.split(ov("load 1 0 oplus/images/lifeviewer.png"))
ov("resize "..w.." "..h.." menuclip")
-- load buttons over the rectangle
ov("load 0 0 oplus/images/lifeviewer.png")
-- replace transparent background with semi-opaque
ov("rgba 0 0 0 128")
ov("replace *# *# *# 0")
-- create clips from the images
ov("rgba 32 255 255 255")
local x = 0
while x < 13 do
-- draw box around button
ov("line "..(40 * x).." 0 "..(40 * x + 39).." 0")
ov("line "..(40 * x).." 39 "..(40 * x + 39).." 39")
ov("line "..(40 * x).." 0 "..(40 * x).." 39")
ov("line "..(40 * x + 39).." 0 "..(40 * x + 39).." 39")
-- create clip from button
ov("copy "..(40 * x).." 0 40 40 button"..x)
x = x + 1
end
-- delete menuclip
ov("target")
ov("delete menuclip")
end
--------------------------------------------------------------------------------
local function setupoverlay()
-- create overlay and cell view
createoverlay()
createcellview()
-- create menu
createmenu()
-- make prerendered text clips
makeclips()
end
--------------------------------------------------------------------------------
function main()
-- check if there is a pattern
if g.empty() then
g.exit("There is no pattern.")
end
-- reset pattern if required
currentgen = tonumber(g.getgen())
if currentgen ~= 0 then
g.reset()
end
-- setup overlay
setupoverlay()
-- check for hex rules
checkhex()
-- reset the camera to default position
fitzoom(true)
-- get the initial bounding box and zoom
initialrect = g.getrect()
initialzoom = lineartoreal(linearzoom)
-- reset history box
resethistory()
-- use Golly's colors if multi-state pattern
if g.numstates() > 2 then
themeon = false
end
-- check for script commands
checkscript()
-- save camera settings to use when reset
setdefaultcamera()
-- apply any script settings
settheme()
updatecamera()
updatecells()
refresh()
updatestatus()
-- start timing
timing.genstarttime = g.millisecs()
-- loop until quit
while true do
-- clear update flags
update.dorefresh = false
update.dostatus = false
update.docamera = false
update.docells = false
-- check if size of layer has changed
local newwd, newht = g.getview(g.getlayer())
if newwd ~= viewwd or newht ~= viewht then
viewwd = newwd
viewht = newht
-- resize overlay
ov("resize "..viewwd.." "..viewht)
update.dorefresh = true
end
-- check for user input
local event = op.process(g.getevent())
if event == "key return none" then
generating = not generating
if generating then
timing.genstarttime = g.millisecs()
end
elseif event == "key space none" then
advance(true)
generating = false
elseif event == "key tab none" then
advance(false)
generating = false
elseif event == "key r none" then
reset(false)
elseif event == "key r shift" then
togglehardreset()
elseif event == "key - none" then
decreasespeed()
elseif event == "key = none" then
increasespeed()
elseif event == "key _ none" then
setminspeed()
elseif event == "key + none" then
setmaxspeed()
elseif event == "key d none" then
decreasestep()
elseif event == "key e none" then
increasestep()
elseif event == "key d shift" then
setminstep()
elseif event == "key e shift" then
setmaxstep()
elseif event == "key 0 none" then
resetspeed()
elseif event == "key f none" then
if not autofit then
fitzoom(false)
end
elseif event == "key f shift" then
toggleautofit()
elseif event == "key h shift" then
togglehistoryfit()
elseif event == "key [ none" then
zoomout()
elseif event == "key ] none" then
zoomin()
elseif event == "key { none" then
halvezoom()
elseif event == "key } none" then
doublezoom()
elseif event == "key , none" then
rotate(-1)
elseif event == "key < none" then
rotate(-90)
elseif event == "key . none" then
rotate(1)
elseif event == "key > none" then
rotate(90)
elseif event == "key 5 none" then
resetangle()
elseif event == "key 1 none" then
setzoom(1)
elseif event == "key ! none" then
integerzoom()
elseif event == "key 2 none" then
setzoom(2)
elseif event == "key \" none" then
setzoom(1/2)
elseif event == "key 4 none" then
setzoom(4)
elseif event == "key $ none" then
setzoom(1/4)
elseif event == "key 8 none" then
setzoom(8)
elseif event == "key * none" then
setzoom(1/8)
elseif event == "key 6 none" then
setzoom(16)
elseif event == "key ^ none" then
setzoom(1/16)
elseif event == "key 3 none" then
setzoom(32)
elseif event == "key c none" then
cycletheme()
elseif event == "key c shift" then
toggletheme()
elseif event == "key q none" then
increaselayers()
elseif event == "key a none" then
decreaselayers()
elseif event == "key p none" then
increaselayerdepth()
elseif event == "key l none" then
decreaselayerdepth()
elseif event == "key s none" then
togglestars()
elseif event == "key left none" then
panview(-1, 0)
elseif event == "key right none" then
panview(1, 0)
elseif event == "key up none" then
panview(0, -1)
elseif event == "key down none" then
panview(0, 1)
elseif event == "key left shift" then
panview(-1, -1)
elseif event == "key right shift" then
panview(1, 1)
elseif event == "key up shift" then
panview(1, -1)
elseif event == "key down shift" then
panview(-1, 1)
elseif event == "key h none" then
showhelp()
elseif event == "key / none" then
togglehex()
elseif event == "key x none" then
togglegrid()
elseif event == "key x shift" then
togglegridmajor()
elseif event == "key t none" then
toggletiming()
elseif event == "key t shift" then
toggleextendedtiming()
elseif event:find("^ozoomin") then
zoominto(event)
elseif event:find("^ozoomout") then
zoomoutfrom(event)
elseif event:find("^oclick") then
doclick(event)
elseif event:find("^mup") then
stopdrag()
else
g.doevent(event)
checkdrag()
end
-- start frame timer
timing.framestarttime = g.millisecs()
-- check if playing
if generating then
advance(false)
-- check for stop or loop
if stopgen == currentgen then
generating = false
g.note("STOP reached, Play to continue")
end
if loopgen == currentgen then
reset(true)
end
else
-- check if life ended but cells still decaying
if decay > 0 then
advance(false)
end
end
-- check if camera still gliding to target
if camsteps ~= -1 then
glidecamera()
end
-- perform required updates
if update.docamera then
updatecamera()
end
if update.docells then
updatecells()
end
if update.dorefresh then
refresh()
end
if update.dostatus then
updatestatus()
end
end
end
--------------------------------------------------------------------------------
local status, err = xpcall(main, gp.trace)
if err then g.continue(err) end
-- the following code is always executed
g.check(false)
ov("delete")
|