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
|
// This file is part of Golly.
// See docs/License.html for the copyright notice.
#include "bigint.h"
#include "lifealgo.h"
#include "viewport.h"
#include "utils.h" // for Warning, PollerReset, etc
#include "prefs.h" // for randomfill, etc
#include "status.h" // for Stringify, etc
#include "undo.h" // for currlayer->undoredo->...
#include "algos.h" // for *_ALGO, CreateNewUniverse
#include "layer.h" // for currlayer, MarkLayerDirty, etc
#include "view.h" // for OutsideLimits, etc
#include "control.h" // for generating, etc
#include "file.h" // for CreateUniverse
#include "select.h"
#include <stdlib.h> // for rand
#ifdef ANDROID_GUI
#include "jnicalls.h" // for BeginProgress, etc
#endif
#ifdef WEB_GUI
#include "webcalls.h" // for BeginProgress, etc
#endif
#ifdef IOS_GUI
#import "PatternViewController.h" // for BeginProgress, etc
#endif
// -----------------------------------------------------------------------------
Selection::Selection()
{
exists = false;
}
// -----------------------------------------------------------------------------
Selection::Selection(int t, int l, int b, int r)
{
// create rectangular selection if given edges are valid
exists = (t <= b && l <= r);
if (exists) {
seltop = t;
selleft = l;
selbottom = b;
selright = r;
}
}
// -----------------------------------------------------------------------------
Selection::~Selection()
{
// no need to do anything at the moment
}
// -----------------------------------------------------------------------------
bool Selection::operator==(const Selection& s) const
{
if (!exists && !s.exists) {
// neither selection exists
return true;
} else if (exists && s.exists) {
// check if edges match
return (seltop == s.seltop && selleft == s.selleft &&
selbottom == s.selbottom && selright == s.selright);
} else {
// one selection exists but not the other
return false;
}
}
// -----------------------------------------------------------------------------
bool Selection::operator!=(const Selection& s) const
{
return !(*this == s);
}
// -----------------------------------------------------------------------------
bool Selection::Exists()
{
return exists;
}
// -----------------------------------------------------------------------------
void Selection::Deselect()
{
exists = false;
}
// -----------------------------------------------------------------------------
bool Selection::TooBig()
{
return OutsideLimits(seltop, selleft, selbottom, selright);
}
// -----------------------------------------------------------------------------
void Selection::DisplaySize()
{
if (inscript) return;
bigint wd = selright; wd -= selleft; wd += bigint::one;
bigint ht = selbottom; ht -= seltop; ht += bigint::one;
std::string msg = "Selection width x height = ";
msg += Stringify(wd);
msg += " x ";
msg += Stringify(ht);
#ifdef WEB_GUI
msg += " (right-click on it to perform actions)";
#endif
SetMessage(msg.c_str());
}
// -----------------------------------------------------------------------------
void Selection::SetRect(int x, int y, int wd, int ht)
{
exists = (wd > 0 && ht > 0);
if (exists) {
seltop = y;
selleft = x;
// avoid int overflow
ht--;
wd--;
selbottom = y;
selbottom += ht;
selright = x;
selright += wd;
}
}
// -----------------------------------------------------------------------------
void Selection::GetRect(int* x, int* y, int* wd, int* ht)
{
*x = selleft.toint();
*y = seltop.toint();
*wd = selright.toint() - *x + 1;
*ht = selbottom.toint() - *y + 1;
}
// -----------------------------------------------------------------------------
void Selection::SetEdges(bigint& t, bigint& l, bigint& b, bigint& r)
{
exists = true;
seltop = t;
selleft = l;
selbottom = b;
selright = r;
CheckGridEdges();
}
// -----------------------------------------------------------------------------
void Selection::CheckGridEdges()
{
if (exists) {
// change selection edges if necessary to ensure they are inside a bounded grid
if (currlayer->algo->gridwd > 0) {
if (selleft > currlayer->algo->gridright || selright < currlayer->algo->gridleft) {
exists = false; // selection is outside grid
return;
}
if (selleft < currlayer->algo->gridleft) selleft = currlayer->algo->gridleft;
if (selright > currlayer->algo->gridright) selright = currlayer->algo->gridright;
}
if (currlayer->algo->gridht > 0) {
if (seltop > currlayer->algo->gridbottom || selbottom < currlayer->algo->gridtop) {
exists = false; // selection is outside grid
return;
}
if (seltop < currlayer->algo->gridtop) seltop = currlayer->algo->gridtop;
if (selbottom > currlayer->algo->gridbottom) selbottom = currlayer->algo->gridbottom;
}
}
}
// -----------------------------------------------------------------------------
bool Selection::Contains(bigint& t, bigint& l, bigint& b, bigint& r)
{
return ( seltop <= t && selleft <= l && selbottom >= b && selright >= r );
}
// -----------------------------------------------------------------------------
bool Selection::Outside(bigint& t, bigint& l, bigint& b, bigint& r)
{
return ( seltop > b || selleft > r || selbottom < t || selright < l );
}
// -----------------------------------------------------------------------------
bool Selection::ContainsCell(int x, int y)
{
return ( x >= selleft.toint() && x <= selright.toint() &&
y >= seltop.toint() && y <= selbottom.toint() );
}
// -----------------------------------------------------------------------------
bool Selection::SaveDifferences(lifealgo* oldalgo, lifealgo* newalgo,
int itop, int ileft, int ibottom, int iright)
{
int wd = iright - ileft + 1;
int ht = ibottom - itop + 1;
int cx, cy;
double maxcount = (double)wd * (double)ht;
int cntr = 0;
bool abort = false;
// compare patterns in given algos and call SaveCellChange for each different cell
BeginProgress("Saving cell changes");
for ( cy=itop; cy<=ibottom; cy++ ) {
for ( cx=ileft; cx<=iright; cx++ ) {
int oldstate = oldalgo->getcell(cx, cy);
int newstate = newalgo->getcell(cx, cy);
if ( oldstate != newstate ) {
// assume this is only called if allowundo
currlayer->undoredo->SaveCellChange(cx, cy, oldstate, newstate);
}
cntr++;
if ((cntr % 4096) == 0) {
abort = AbortProgress((double)cntr / maxcount, "");
if (abort) break;
}
}
if (abort) break;
}
EndProgress();
return !abort;
}
// -----------------------------------------------------------------------------
void Selection::Advance()
{
if (generating) return;
if (!exists) {
ErrorMessage(no_selection);
return;
}
if (currlayer->algo->isEmpty()) {
ErrorMessage(empty_selection);
return;
}
bigint top, left, bottom, right;
currlayer->algo->findedges(&top, &left, &bottom, &right);
// check if selection is completely outside pattern edges
if (Outside(top, left, bottom, right)) {
ErrorMessage(empty_selection);
return;
}
// save cell changes if undo/redo is enabled and script isn't constructing a pattern
bool savecells = allowundo && !currlayer->stayclean;
//!!! if (savecells && inscript) SavePendingChanges();
bool boundedgrid = currlayer->algo->unbounded && (currlayer->algo->gridwd > 0 || currlayer->algo->gridht > 0);
// check if selection encloses entire pattern;
// can't do this if qlife because it uses gen parity to decide which bits to draw;
// also avoid this if undo/redo is enabled (too messy to remember cell changes)
if ( currlayer->algtype != QLIFE_ALGO && !savecells && Contains(top, left, bottom, right) ) {
generating = true;
PollerReset();
// step by one gen without changing gen count
bigint savegen = currlayer->algo->getGeneration();
bigint saveinc = currlayer->algo->getIncrement();
currlayer->algo->setIncrement(1);
if (boundedgrid) currlayer->algo->CreateBorderCells();
currlayer->algo->step();
if (boundedgrid) currlayer->algo->DeleteBorderCells();
currlayer->algo->setIncrement(saveinc);
currlayer->algo->setGeneration(savegen);
generating = false;
// clear 1-cell thick strips just outside selection
ClearOutside();
MarkLayerDirty();
UpdateEverything();
return;
}
// find intersection of selection and pattern to minimize work
if (seltop > top) top = seltop;
if (selleft > left) left = selleft;
if (selbottom < bottom) bottom = selbottom;
if (selright < right) right = selright;
// check that intersection is within setcell/getcell limits
if ( OutsideLimits(top, left, bottom, right) ) {
ErrorMessage(selection_too_big);
return;
}
// create a temporary universe of same type as current universe
lifealgo* tempalgo = CreateNewUniverse(currlayer->algtype);
if (tempalgo->setrule(currlayer->algo->getrule()))
tempalgo->setrule(tempalgo->DefaultRule());
// copy live cells in selection to temporary universe
if ( !CopyRect(top.toint(), left.toint(), bottom.toint(), right.toint(),
currlayer->algo, tempalgo, false, "Saving selection") ) {
delete tempalgo;
return;
}
if ( tempalgo->isEmpty() ) {
ErrorMessage(empty_selection);
delete tempalgo;
return;
}
// advance temporary universe by one gen
generating = true;
PollerReset();
tempalgo->setIncrement(1);
if (boundedgrid) tempalgo->CreateBorderCells();
tempalgo->step();
if (boundedgrid) tempalgo->DeleteBorderCells();
generating = false;
if ( !tempalgo->isEmpty() ) {
// temporary pattern might have expanded
bigint temptop, templeft, tempbottom, tempright;
tempalgo->findedges(&temptop, &templeft, &tempbottom, &tempright);
if (temptop < top) top = temptop;
if (templeft < left) left = templeft;
if (tempbottom > bottom) bottom = tempbottom;
if (tempright > right) right = tempright;
// but ignore live cells created outside selection edges
if (top < seltop) top = seltop;
if (left < selleft) left = selleft;
if (bottom > selbottom) bottom = selbottom;
if (right > selright) right = selright;
}
if (savecells) {
// compare selection rect in currlayer->algo and tempalgo and call SaveCellChange
// for each cell that has a different state
if ( SaveDifferences(currlayer->algo, tempalgo,
top.toint(), left.toint(), bottom.toint(), right.toint()) ) {
if ( !currlayer->undoredo->RememberCellChanges("Advance Selection", currlayer->dirty) ) {
// pattern inside selection didn't change
delete tempalgo;
return;
}
} else {
currlayer->undoredo->ForgetCellChanges();
delete tempalgo;
return;
}
}
// copy all cells in new selection from tempalgo to currlayer->algo
CopyAllRect(top.toint(), left.toint(), bottom.toint(), right.toint(),
tempalgo, currlayer->algo, "Copying advanced selection");
delete tempalgo;
MarkLayerDirty();
UpdateEverything();
}
// -----------------------------------------------------------------------------
void Selection::AdvanceOutside()
{
if (generating) return;
if (!exists) {
ErrorMessage(no_selection);
return;
}
if (currlayer->algo->isEmpty()) {
ErrorMessage(empty_outside);
return;
}
bigint top, left, bottom, right;
currlayer->algo->findedges(&top, &left, &bottom, &right);
// check if selection encloses entire pattern
if (Contains(top, left, bottom, right)) {
ErrorMessage(empty_outside);
return;
}
// save cell changes if undo/redo is enabled and script isn't constructing a pattern
bool savecells = allowundo && !currlayer->stayclean;
//!!! if (savecells && inscript) SavePendingChanges();
bool boundedgrid = currlayer->algo->unbounded && (currlayer->algo->gridwd > 0 || currlayer->algo->gridht > 0);
// check if selection is completely outside pattern edges;
// can't do this if qlife because it uses gen parity to decide which bits to draw;
// also avoid this if undo/redo is enabled (too messy to remember cell changes)
if ( currlayer->algtype != QLIFE_ALGO && !savecells && Outside(top, left, bottom, right) ) {
generating = true;
PollerReset();
// step by one gen without changing gen count
bigint savegen = currlayer->algo->getGeneration();
bigint saveinc = currlayer->algo->getIncrement();
currlayer->algo->setIncrement(1);
if (boundedgrid) currlayer->algo->CreateBorderCells();
currlayer->algo->step();
if (boundedgrid) currlayer->algo->DeleteBorderCells();
currlayer->algo->setIncrement(saveinc);
currlayer->algo->setGeneration(savegen);
generating = false;
// clear selection in case pattern expanded into it
Clear();
MarkLayerDirty();
UpdateEverything();
return;
}
// check that pattern is within setcell/getcell limits
if ( OutsideLimits(top, left, bottom, right) ) {
ErrorMessage("Pattern is outside +/- 10^9 boundary.");
return;
}
lifealgo* oldalgo = NULL;
if (savecells) {
// copy current pattern to oldalgo, using same type and gen count
// so we can switch to oldalgo if user decides to abort below
oldalgo = CreateNewUniverse(currlayer->algtype);
if (oldalgo->setrule(currlayer->algo->getrule()))
oldalgo->setrule(oldalgo->DefaultRule());
oldalgo->setGeneration( currlayer->algo->getGeneration() );
if ( !CopyRect(top.toint(), left.toint(), bottom.toint(), right.toint(),
currlayer->algo, oldalgo, false, "Saving pattern") ) {
delete oldalgo;
return;
}
}
// create a new universe of same type
lifealgo* newalgo = CreateNewUniverse(currlayer->algtype);
if (newalgo->setrule(currlayer->algo->getrule()))
newalgo->setrule(newalgo->DefaultRule());
newalgo->setGeneration( currlayer->algo->getGeneration() );
// copy (and kill) live cells in selection to new universe
int iseltop = seltop.toint();
int iselleft = selleft.toint();
int iselbottom = selbottom.toint();
int iselright = selright.toint();
if ( !CopyRect(iseltop, iselleft, iselbottom, iselright,
currlayer->algo, newalgo, true, "Saving and erasing selection") ) {
// aborted, so best to restore selection
if ( !newalgo->isEmpty() ) {
newalgo->findedges(&top, &left, &bottom, &right);
CopyRect(top.toint(), left.toint(), bottom.toint(), right.toint(),
newalgo, currlayer->algo, false, "Restoring selection");
}
delete newalgo;
if (savecells) delete oldalgo;
UpdateEverything();
return;
}
// advance current universe by 1 generation
generating = true;
PollerReset();
currlayer->algo->setIncrement(1);
if (boundedgrid) currlayer->algo->CreateBorderCells();
currlayer->algo->step();
if (boundedgrid) currlayer->algo->DeleteBorderCells();
generating = false;
if ( !currlayer->algo->isEmpty() ) {
// find new edges and copy current pattern to new universe,
// except for any cells that were created in selection
// (newalgo contains the original selection)
bigint t, l, b, r;
currlayer->algo->findedges(&t, &l, &b, &r);
int itop = t.toint();
int ileft = l.toint();
int ibottom = b.toint();
int iright = r.toint();
int ht = ibottom - itop + 1;
int cx, cy;
// for showing accurate progress we need to add pattern height to pop count
// in case this is a huge pattern with many blank rows
double maxcount = currlayer->algo->getPopulation().todouble() + ht;
double accumcount = 0;
int currcount = 0;
int v = 0;
bool abort = false;
BeginProgress("Copying advanced pattern");
lifealgo* curralgo = currlayer->algo;
for ( cy=itop; cy<=ibottom; cy++ ) {
currcount++;
for ( cx=ileft; cx<=iright; cx++ ) {
int skip = curralgo->nextcell(cx, cy, v);
if (skip >= 0) {
// found next live cell in this row
cx += skip;
// only copy cell if outside selection
if ( cx < iselleft || cx > iselright ||
cy < iseltop || cy > iselbottom ) {
newalgo->setcell(cx, cy, v);
}
currcount++;
} else {
cx = iright; // done this row
}
if (currcount > 1024) {
accumcount += currcount;
currcount = 0;
abort = AbortProgress(accumcount / maxcount, "");
if (abort) break;
}
}
if (abort) break;
}
newalgo->endofpattern();
EndProgress();
if (abort && savecells) {
// revert back to pattern saved in oldalgo
delete newalgo;
delete currlayer->algo;
currlayer->algo = oldalgo;
SetGenIncrement();
UpdateEverything();
return;
}
}
// switch to new universe (best to do this even if aborted)
delete currlayer->algo;
currlayer->algo = newalgo;
SetGenIncrement();
if (savecells) {
// compare patterns in oldalgo and currlayer->algo and call SaveCellChange
// for each cell that has a different state; note that we need to compare
// the union of the original pattern's rect and the new pattern's rect
int otop = top.toint();
int oleft = left.toint();
int obottom = bottom.toint();
int oright = right.toint();
if (!currlayer->algo->isEmpty()) {
currlayer->algo->findedges(&top, &left, &bottom, &right);
int ntop = top.toint();
int nleft = left.toint();
int nbottom = bottom.toint();
int nright = right.toint();
if (ntop < otop) otop = ntop;
if (nleft < oleft) oleft = nleft;
if (nbottom > obottom) obottom = nbottom;
if (nright > oright) oright = nright;
}
if ( SaveDifferences(oldalgo, currlayer->algo, otop, oleft, obottom, oright) ) {
delete oldalgo;
if ( !currlayer->undoredo->RememberCellChanges("Advance Outside", currlayer->dirty) ) {
// pattern outside selection didn't change
UpdateEverything();
return;
}
} else {
// revert back to pattern saved in oldalgo
currlayer->undoredo->ForgetCellChanges();
delete currlayer->algo;
currlayer->algo = oldalgo;
SetGenIncrement();
UpdateEverything();
return;
}
}
MarkLayerDirty();
UpdateEverything();
}
// -----------------------------------------------------------------------------
void Selection::Modify(const int x, const int y,
bigint& anchorx, bigint& anchory,
bool* forceh, bool* forcev)
{
// assume tapped point is somewhere inside selection
pair<int,int> lt = currlayer->view->screenPosOf(selleft, seltop, currlayer->algo);
pair<int,int> rb = currlayer->view->screenPosOf(selright, selbottom, currlayer->algo);
if (currlayer->view->getmag() > 0) {
// move rb to pixel at bottom right corner of cell
rb.first += (1 << currlayer->view->getmag()) - 1;
rb.second += (1 << currlayer->view->getmag()) - 1;
if (currlayer->view->getmag() > 1) {
// allow for gap at scale 1:4 and above
rb.first--;
rb.second--;
}
}
double wd = rb.first - lt.first + 1;
double ht = rb.second - lt.second + 1;
double onefifthx = lt.first + wd / 5.0;
double fourfifthx = lt.first + wd * 4.0 / 5.0;
double onefifthy = lt.second + ht / 5.0;
double fourfifthy = lt.second + ht * 4.0 / 5.0;
if ( y < onefifthy && x < onefifthx ) {
// tap is near top left corner
anchory = selbottom;
anchorx = selright;
} else if ( y < onefifthy && x > fourfifthx ) {
// tap is near top right corner
anchory = selbottom;
anchorx = selleft;
} else if ( y > fourfifthy && x > fourfifthx ) {
// tap is near bottom right corner
anchory = seltop;
anchorx = selleft;
} else if ( y > fourfifthy && x < onefifthx ) {
// tap is near bottom left corner
anchory = seltop;
anchorx = selright;
} else if ( x < onefifthx ) {
// tap is near left edge
*forceh = true;
anchorx = selright;
} else if ( x > fourfifthx ) {
// tap is near right edge
*forceh = true;
anchorx = selleft;
} else if ( y < onefifthy ) {
// tap is near top edge
*forcev = true;
anchory = selbottom;
} else if ( y > fourfifthy ) {
// tap is near bottom edge
*forcev = true;
anchory = seltop;
} else {
// tap is in middle area, so move selection rather than resize
*forceh = true;
*forcev = true;
}
}
// -----------------------------------------------------------------------------
void Selection::Move(const bigint& xcells, const bigint& ycells)
{
if (exists) {
selleft += xcells;
selright += xcells;
seltop += ycells;
selbottom += ycells;
CheckGridEdges();
}
}
// -----------------------------------------------------------------------------
void Selection::SetLeftRight(const bigint& x, const bigint& anchorx)
{
if (x <= anchorx) {
selleft = x;
selright = anchorx;
} else {
selleft = anchorx;
selright = x;
}
exists = true;
}
// -----------------------------------------------------------------------------
void Selection::SetTopBottom(const bigint& y, const bigint& anchory)
{
if (y <= anchory) {
seltop = y;
selbottom = anchory;
} else {
seltop = anchory;
selbottom = y;
}
exists = true;
}
// -----------------------------------------------------------------------------
void Selection::Fit()
{
bigint newx = selright;
newx -= selleft;
newx += bigint::one;
newx.div2();
newx += selleft;
bigint newy = selbottom;
newy -= seltop;
newy += bigint::one;
newy.div2();
newy += seltop;
int mag = MAX_MAG;
while (true) {
currlayer->view->setpositionmag(newx, newy, mag);
if (currlayer->view->contains(selleft, seltop) &&
currlayer->view->contains(selright, selbottom))
break;
mag--;
}
}
// -----------------------------------------------------------------------------
void Selection::Shrink(bool fit)
{
if (!exists) return;
// check if there is no pattern
if (currlayer->algo->isEmpty()) {
ErrorMessage(empty_selection);
if (fit) FitSelection();
return;
}
// check if selection encloses entire pattern
bigint top, left, bottom, right;
currlayer->algo->findedges(&top, &left, &bottom, &right);
if (Contains(top, left, bottom, right)) {
// shrink edges
SaveCurrentSelection();
seltop = top;
selleft = left;
selbottom = bottom;
selright = right;
RememberNewSelection("Shrink Selection");
DisplaySelectionSize();
if (fit)
FitSelection(); // calls UpdateEverything
else
UpdatePatternAndStatus();
return;
}
// check if selection is completely outside pattern edges
if (Outside(top, left, bottom, right)) {
ErrorMessage(empty_selection);
if (fit) FitSelection();
return;
}
// find intersection of selection and pattern to minimize work
if (seltop > top) top = seltop;
if (selleft > left) left = selleft;
if (selbottom < bottom) bottom = selbottom;
if (selright < right) right = selright;
// check that selection is small enough to save
if ( OutsideLimits(top, left, bottom, right) ) {
ErrorMessage(selection_too_big);
if (fit) FitSelection();
return;
}
// the easy way to shrink selection is to create a new temporary universe,
// copy selection into new universe and then call findedges;
// if only 2 cell states then use qlife because its findedges call is faster
lifealgo* tempalgo = CreateNewUniverse(currlayer->algo->NumCellStates() > 2 ?
currlayer->algtype :
QLIFE_ALGO);
// make sure temporary universe has same # of cell states
if (currlayer->algo->NumCellStates() > 2)
if (tempalgo->setrule(currlayer->algo->getrule()))
tempalgo->setrule(tempalgo->DefaultRule());
// copy live cells in selection to temporary universe
if ( CopyRect(top.toint(), left.toint(), bottom.toint(), right.toint(),
currlayer->algo, tempalgo, false, "Copying selection") ) {
if ( tempalgo->isEmpty() ) {
ErrorMessage(empty_selection);
} else {
SaveCurrentSelection();
tempalgo->findedges(&seltop, &selleft, &selbottom, &selright);
RememberNewSelection("Shrink Selection");
DisplaySelectionSize();
if (!fit) UpdatePatternAndStatus();
}
}
delete tempalgo;
if (fit) FitSelection();
}
// -----------------------------------------------------------------------------
bool Selection::Visible(gRect* visrect)
{
if (!exists) return false;
pair<int,int> lt = currlayer->view->screenPosOf(selleft, seltop, currlayer->algo);
pair<int,int> rb = currlayer->view->screenPosOf(selright, selbottom, currlayer->algo);
if (lt.first > currlayer->view->getxmax() || rb.first < 0 ||
lt.second > currlayer->view->getymax() || rb.second < 0) {
// no part of selection is visible
return false;
}
// all or some of selection is visible in viewport;
// only set visible rectangle if requested
if (visrect) {
// first we must clip coords to viewport
if (lt.first < 0) lt.first = 0;
if (lt.second < 0) lt.second = 0;
if (rb.first > currlayer->view->getxmax()) rb.first = currlayer->view->getxmax();
if (rb.second > currlayer->view->getymax()) rb.second = currlayer->view->getymax();
if (currlayer->view->getmag() > 0) {
// move rb to pixel at bottom right corner of cell
rb.first += (1 << currlayer->view->getmag()) - 1;
rb.second += (1 << currlayer->view->getmag()) - 1;
if (currlayer->view->getmag() > 1) {
// avoid covering gaps at scale 1:4 and above
rb.first--;
rb.second--;
}
// clip to viewport again
if (rb.first > currlayer->view->getxmax()) rb.first = currlayer->view->getxmax();
if (rb.second > currlayer->view->getymax()) rb.second = currlayer->view->getymax();
}
visrect->x = lt.first;
visrect->y = lt.second;
visrect->width = rb.first - lt.first + 1;
visrect->height = rb.second - lt.second + 1;
}
return true;
}
// -----------------------------------------------------------------------------
void Selection::EmptyUniverse()
{
// save current step, scale, position and gen count
int savebase = currlayer->currbase;
int saveexpo = currlayer->currexpo;
int savemag = currlayer->view->getmag();
bigint savex = currlayer->view->x;
bigint savey = currlayer->view->y;
bigint savegen = currlayer->algo->getGeneration();
// kill all live cells by replacing the current universe with a
// new, empty universe which also uses the same rule
CreateUniverse();
// restore step, scale, position and gen count
currlayer->currbase = savebase;
SetStepExponent(saveexpo);
// SetStepExponent calls SetGenIncrement
currlayer->view->setpositionmag(savex, savey, savemag);
currlayer->algo->setGeneration(savegen);
UpdatePatternAndStatus();
}
// -----------------------------------------------------------------------------
void Selection::Clear()
{
if (!exists) return;
// no need to do anything if there is no pattern
if (currlayer->algo->isEmpty()) return;
// save cell changes if undo/redo is enabled and script isn't constructing a pattern
bool savecells = allowundo && !currlayer->stayclean;
//!!! if (savecells && inscript) SavePendingChanges();
bigint top, left, bottom, right;
currlayer->algo->findedges(&top, &left, &bottom, &right);
if ( !savecells && Contains(top, left, bottom, right) ) {
// selection encloses entire pattern so just create empty universe
EmptyUniverse();
MarkLayerDirty();
return;
}
// no need to do anything if selection is completely outside pattern edges
if (Outside(top, left, bottom, right)) {
return;
}
// find intersection of selection and pattern to minimize work
if (seltop > top) top = seltop;
if (selleft > left) left = selleft;
if (selbottom < bottom) bottom = selbottom;
if (selright < right) right = selright;
// can only use setcell in limited domain
if ( OutsideLimits(top, left, bottom, right) ) {
ErrorMessage(selection_too_big);
return;
}
// clear all live cells in selection
int itop = top.toint();
int ileft = left.toint();
int ibottom = bottom.toint();
int iright = right.toint();
int wd = iright - ileft + 1;
int ht = ibottom - itop + 1;
int cx, cy;
double maxcount = (double)wd * (double)ht;
int cntr = 0;
int v = 0;
bool abort = false;
bool selchanged = false;
BeginProgress("Clearing selection");
lifealgo* curralgo = currlayer->algo;
for ( cy=itop; cy<=ibottom; cy++ ) {
for ( cx=ileft; cx<=iright; cx++ ) {
int skip = curralgo->nextcell(cx, cy, v);
if (skip + cx > iright)
skip = -1; // pretend we found no more live cells
if (skip >= 0) {
// found next live cell
cx += skip;
curralgo->setcell(cx, cy, 0);
selchanged = true;
if (savecells) currlayer->undoredo->SaveCellChange(cx, cy, v, 0);
} else {
cx = iright + 1; // done this row
}
cntr++;
if ((cntr % 4096) == 0) {
double prog = ((cy - itop) * (double)(iright - ileft + 1) +
(cx - ileft)) / maxcount;
abort = AbortProgress(prog, "");
if (abort) break;
}
}
if (abort) break;
}
if (selchanged) curralgo->endofpattern();
EndProgress();
if (selchanged) {
if (savecells) currlayer->undoredo->RememberCellChanges("Clear", currlayer->dirty);
MarkLayerDirty();
UpdatePatternAndStatus();
}
}
// -----------------------------------------------------------------------------
bool Selection::SaveOutside(bigint& t, bigint& l, bigint& b, bigint& r)
{
if ( OutsideLimits(t, l, b, r) ) {
ErrorMessage(pattern_too_big);
return false;
}
int itop = t.toint();
int ileft = l.toint();
int ibottom = b.toint();
int iright = r.toint();
// save ALL cells if selection is completely outside pattern edges
bool saveall = Outside(t, l, b, r);
// integer selection edges must not be outside pattern edges
int stop = itop;
int sleft = ileft;
int sbottom = ibottom;
int sright = iright;
if (!saveall) {
if (seltop > t) stop = seltop.toint();
if (selleft > l) sleft = selleft.toint();
if (selbottom < b) sbottom = selbottom.toint();
if (selright < r) sright = selright.toint();
}
int wd = iright - ileft + 1;
int ht = ibottom - itop + 1;
int cx, cy;
int v = 0;
double maxcount = (double)wd * (double)ht;
int cntr = 0;
bool abort = false;
BeginProgress("Saving outside selection");
lifealgo* curralgo = currlayer->algo;
for ( cy=itop; cy<=ibottom; cy++ ) {
for ( cx=ileft; cx<=iright; cx++ ) {
int skip = curralgo->nextcell(cx, cy, v);
if (skip + cx > iright)
skip = -1; // pretend we found no more live cells
if (skip >= 0) {
// found next live cell
cx += skip;
if (saveall || cx < sleft || cx > sright || cy < stop || cy > sbottom) {
// cell is outside selection edges
currlayer->undoredo->SaveCellChange(cx, cy, v, 0);
}
} else {
cx = iright + 1; // done this row
}
cntr++;
if ((cntr % 4096) == 0) {
double prog = ((cy - itop) * (double)(iright - ileft + 1) +
(cx - ileft)) / maxcount;
abort = AbortProgress(prog, "");
if (abort) break;
}
}
if (abort) break;
}
EndProgress();
if (abort) currlayer->undoredo->ForgetCellChanges();
return !abort;
}
// -----------------------------------------------------------------------------
void Selection::ClearOutside()
{
if (!exists) return;
// no need to do anything if there is no pattern
if (currlayer->algo->isEmpty()) return;
// no need to do anything if selection encloses entire pattern
bigint top, left, bottom, right;
currlayer->algo->findedges(&top, &left, &bottom, &right);
if (Contains(top, left, bottom, right)) {
return;
}
// save cell changes if undo/redo is enabled and script isn't constructing a pattern
bool savecells = allowundo && !currlayer->stayclean;
//!!! if (savecells && inscript) SavePendingChanges();
if (savecells) {
// save live cells outside selection
if ( !SaveOutside(top, left, bottom, right) ) {
return;
}
} else {
// create empty universe if selection is completely outside pattern edges
if (Outside(top, left, bottom, right)) {
EmptyUniverse();
MarkLayerDirty();
return;
}
}
// find intersection of selection and pattern to minimize work
if (seltop > top) top = seltop;
if (selleft > left) left = selleft;
if (selbottom < bottom) bottom = selbottom;
if (selright < right) right = selright;
// check that selection is small enough to save
if ( OutsideLimits(top, left, bottom, right) ) {
ErrorMessage(selection_too_big);
return;
}
// create a new universe of same type
lifealgo* newalgo = CreateNewUniverse(currlayer->algtype);
if (newalgo->setrule(currlayer->algo->getrule()))
newalgo->setrule(newalgo->DefaultRule());
// set same gen count
newalgo->setGeneration( currlayer->algo->getGeneration() );
// copy live cells in selection to new universe
if ( CopyRect(top.toint(), left.toint(), bottom.toint(), right.toint(),
currlayer->algo, newalgo, false, "Saving selection") ) {
// delete old universe and point currlayer->algo at new universe
delete currlayer->algo;
currlayer->algo = newalgo;
SetGenIncrement();
if (savecells) currlayer->undoredo->RememberCellChanges("Clear Outside", currlayer->dirty);
MarkLayerDirty();
UpdatePatternAndStatus();
} else {
// CopyRect was aborted, so don't change current universe
delete newalgo;
if (savecells) currlayer->undoredo->ForgetCellChanges();
}
}
// -----------------------------------------------------------------------------
void Selection::AddEOL(char* &chptr)
{
// use LF on Mac
*chptr = '\n';
chptr += 1;
}
// -----------------------------------------------------------------------------
const int WRLE_NONE = -3;
const int WRLE_EOP = -2;
const int WRLE_NEWLINE = -1;
void Selection::AddRun(int state, // in: state of cell to write
int multistate, // true if #cell states > 2
unsigned int &run, // in and out
unsigned int &linelen, // ditto
char* &chptr) // ditto
{
// output of RLE pattern data is channelled thru here to make it easier to
// ensure all lines have <= maxrleline characters
const unsigned int maxrleline = 70;
unsigned int i, numlen;
char numstr[32];
if ( run > 1 ) {
sprintf(numstr, "%u", run);
numlen = (unsigned int)strlen(numstr);
} else {
numlen = 0; // no run count shown if 1
}
// keep linelen <= maxrleline
if ( linelen + numlen + 1 + multistate > maxrleline ) {
AddEOL(chptr);
linelen = 0;
}
i = 0;
while (i < numlen) {
*chptr = numstr[i];
chptr += 1;
i++;
}
if (multistate) {
if (state <= 0)
*chptr = ".$!"[-state];
else {
if (state > 24) {
int hi = (state - 25) / 24;
*chptr = hi + 'p';
chptr += 1;
linelen += 1;
state -= (hi + 1) * 24;
}
*chptr = 'A' + state - 1;
}
} else
*chptr = "!$bo"[state+2];
chptr += 1;
linelen += numlen + 1;
run = 0; // reset run count
}
// -----------------------------------------------------------------------------
void Selection::CopyToClipboard(bool cut)
{
// can only use getcell/setcell in limited domain
if (TooBig()) {
ErrorMessage(selection_too_big);
return;
}
int itop = seltop.toint();
int ileft = selleft.toint();
int ibottom = selbottom.toint();
int iright = selright.toint();
unsigned int wd = iright - ileft + 1;
unsigned int ht = ibottom - itop + 1;
// convert cells in selection to RLE data in textptr
char* textptr;
char* etextptr;
int cursize = 4096;
textptr = (char*)malloc(cursize);
if (textptr == NULL) {
ErrorMessage("Not enough memory for clipboard data!");
return;
}
etextptr = textptr + cursize;
// add RLE header line
sprintf(textptr, "x = %u, y = %u, rule = %s", wd, ht, currlayer->algo->getrule());
char* chptr = textptr;
chptr += strlen(textptr);
AddEOL(chptr);
// save start of data in case livecount is zero
int datastart = int(chptr - textptr);
// add RLE pattern data
unsigned int livecount = 0;
unsigned int linelen = 0;
unsigned int brun = 0;
unsigned int orun = 0;
unsigned int dollrun = 0;
int laststate;
int cx, cy;
int v = 0;
// save cell changes if undo/redo is enabled and script isn't constructing a pattern
bool savecells = allowundo && !currlayer->stayclean;
//!!! if (savecells && inscript) SavePendingChanges();
double maxcount = (double)wd * (double)ht;
int cntr = 0;
bool abort = false;
if (cut)
BeginProgress("Cutting selection");
else
BeginProgress("Copying selection");
lifealgo* curralgo = currlayer->algo;
int multistate = curralgo->NumCellStates() > 2;
for ( cy=itop; cy<=ibottom; cy++ ) {
laststate = WRLE_NONE;
for ( cx=ileft; cx<=iright; cx++ ) {
int skip = curralgo->nextcell(cx, cy, v);
if (skip + cx > iright)
skip = -1; // pretend we found no more live cells
if (skip > 0) {
// have exactly "skip" empty cells here
if (laststate == 0) {
brun += skip;
} else {
if (orun > 0) {
// output current run of live cells
AddRun(laststate, multistate, orun, linelen, chptr);
}
laststate = 0;
brun = skip;
}
}
if (skip >= 0) {
// found next live cell
cx += skip;
livecount++;
if (cut) {
curralgo->setcell(cx, cy, 0);
if (savecells) currlayer->undoredo->SaveCellChange(cx, cy, v, 0);
}
if (laststate == v) {
orun++;
} else {
if (dollrun > 0)
// output current run of $ chars
AddRun(WRLE_NEWLINE, multistate, dollrun, linelen, chptr);
if (brun > 0)
// output current run of dead cells
AddRun(0, multistate, brun, linelen, chptr);
if (orun > 0)
// output current run of other live cells
AddRun(laststate, multistate, orun, linelen, chptr);
laststate = v;
orun = 1;
}
} else {
cx = iright + 1; // done this row
}
cntr++;
if ((cntr % 4096) == 0) {
double prog = ((cy - itop) * (double)(iright - ileft + 1) +
(cx - ileft)) / maxcount;
abort = AbortProgress(prog, "");
if (abort) break;
}
if (chptr + 60 >= etextptr) {
// nearly out of space; try to increase allocation
char* ntxtptr = (char*) realloc(textptr, 2*cursize);
if (ntxtptr == 0) {
ErrorMessage("No more memory for clipboard data!");
// don't return here -- best to set abort flag and break so that
// partially cut/copied portion gets saved to clipboard
abort = true;
break;
}
chptr = ntxtptr + (chptr - textptr);
cursize *= 2;
etextptr = ntxtptr + cursize;
textptr = ntxtptr;
}
}
if (abort) break;
// end of current row
if (laststate == 0)
// forget dead cells at end of row
brun = 0;
else if (laststate >= 0)
// output current run of live cells
AddRun(laststate, multistate, orun, linelen, chptr);
dollrun++;
}
if (livecount == 0) {
// no live cells in selection so simplify RLE data to "!"
chptr = textptr + datastart;
*chptr = '!';
chptr++;
} else {
// terminate RLE data
dollrun = 1;
AddRun(WRLE_EOP, multistate, dollrun, linelen, chptr);
if (cut) currlayer->algo->endofpattern();
}
AddEOL(chptr);
*chptr = 0;
EndProgress();
if (cut && livecount > 0) {
if (savecells) currlayer->undoredo->RememberCellChanges("Cut", currlayer->dirty);
// update currlayer->dirty AFTER RememberCellChanges
MarkLayerDirty();
UpdatePatternAndStatus();
}
CopyTextToClipboard(textptr);
free(textptr);
}
// -----------------------------------------------------------------------------
bool Selection::CanPaste(const bigint& wd, const bigint& ht, bigint& top, bigint& left)
{
bigint selht = selbottom; selht -= seltop; selht += 1;
bigint selwd = selright; selwd -= selleft; selwd += 1;
if ( ht > selht || wd > selwd ) return false;
// set paste rectangle's top left cell coord
top = seltop;
left = selleft;
return true;
}
// -----------------------------------------------------------------------------
void Selection::RandomFill()
{
if (!exists) return;
// can only use getcell/setcell in limited domain
if (TooBig()) {
ErrorMessage(selection_too_big);
return;
}
// save cell changes if undo/redo is enabled and script isn't constructing a pattern
bool savecells = allowundo && !currlayer->stayclean;
//!!! if (savecells && inscript) SavePendingChanges();
// no need to kill cells if selection is empty
bool killcells = !currlayer->algo->isEmpty();
if ( killcells ) {
// find pattern edges and compare with selection edges
bigint top, left, bottom, right;
currlayer->algo->findedges(&top, &left, &bottom, &right);
if (Contains(top, left, bottom, right)) {
// selection encloses entire pattern so create empty universe
if (savecells) {
// don't kill pattern otherwise we can't use SaveCellChange below
} else {
EmptyUniverse();
killcells = false;
}
} else if (Outside(top, left, bottom, right)) {
// selection is completely outside pattern edges
killcells = false;
}
}
int itop = seltop.toint();
int ileft = selleft.toint();
int ibottom = selbottom.toint();
int iright = selright.toint();
int wd = iright - ileft + 1;
int ht = ibottom - itop + 1;
double maxcount = (double)wd * (double)ht;
int cntr = 0;
bool abort = false;
BeginProgress("Randomly filling selection");
int cx, cy;
lifealgo* curralgo = currlayer->algo;
int livestates = curralgo->NumRandomizedCellStates() - 1; // don't count dead state
for ( cy=itop; cy<=ibottom; cy++ ) {
for ( cx=ileft; cx<=iright; cx++ ) {
// randomfill is from 1..100
if (savecells) {
// remember cell change only if state changes
int oldstate = curralgo->getcell(cx, cy);
if ((rand() % 100) < randomfill) {
int newstate = livestates < 2 ? 1 : 1 + (rand() % livestates);
if (oldstate != newstate) {
curralgo->setcell(cx, cy, newstate);
currlayer->undoredo->SaveCellChange(cx, cy, oldstate, newstate);
}
} else if (killcells && oldstate > 0) {
curralgo->setcell(cx, cy, 0);
currlayer->undoredo->SaveCellChange(cx, cy, oldstate, 0);
}
} else {
if ((rand() % 100) < randomfill) {
if (livestates < 2) {
curralgo->setcell(cx, cy, 1);
} else {
curralgo->setcell(cx, cy, 1 + (rand() % livestates));
}
} else if (killcells) {
curralgo->setcell(cx, cy, 0);
}
}
cntr++;
if ((cntr % 4096) == 0) {
abort = AbortProgress((double)cntr / maxcount, "");
if (abort) break;
}
}
if (abort) break;
}
currlayer->algo->endofpattern();
EndProgress();
if (savecells) currlayer->undoredo->RememberCellChanges("Random Fill", currlayer->dirty);
// update currlayer->dirty AFTER RememberCellChanges
MarkLayerDirty();
UpdatePatternAndStatus();
}
// -----------------------------------------------------------------------------
bool Selection::FlipRect(bool topbottom, lifealgo* srcalgo, lifealgo* destalgo, bool erasesrc,
int itop, int ileft, int ibottom, int iright)
{
int wd = iright - ileft + 1;
int ht = ibottom - itop + 1;
double maxcount = (double)wd * (double)ht;
int cntr = 0;
bool abort = false;
int v = 0;
int cx, cy, newx, newy, newxinc, newyinc;
if (topbottom) {
BeginProgress("Flipping top-bottom");
newy = ibottom;
newyinc = -1;
newxinc = 1;
} else {
BeginProgress("Flipping left-right");
newy = itop;
newyinc = 1;
newxinc = -1;
}
for ( cy=itop; cy<=ibottom; cy++ ) {
newx = topbottom ? ileft : iright;
for ( cx=ileft; cx<=iright; cx++ ) {
int skip = srcalgo->nextcell(cx, cy, v);
if (skip + cx > iright)
skip = -1; // pretend we found no more live cells
if (skip >= 0) {
// found next live cell
cx += skip;
if (erasesrc) srcalgo->setcell(cx, cy, 0);
newx += newxinc * skip;
destalgo->setcell(newx, newy, v);
} else {
cx = iright + 1; // done this row
}
cntr++;
if ((cntr % 4096) == 0) {
double prog = ((cy - itop) * (double)(iright - ileft + 1) +
(cx - ileft)) / maxcount;
abort = AbortProgress(prog, "");
if (abort) break;
}
newx += newxinc;
}
if (abort) break;
newy += newyinc;
}
if (erasesrc) srcalgo->endofpattern();
destalgo->endofpattern();
EndProgress();
return !abort;
}
// -----------------------------------------------------------------------------
bool Selection::Flip(bool topbottom, bool inundoredo)
{
if (!exists) return false;
if (topbottom) {
if (seltop == selbottom) return true;
} else {
if (selleft == selright) return true;
}
if (currlayer->algo->isEmpty()) return true;
bigint top, left, bottom, right;
currlayer->algo->findedges(&top, &left, &bottom, &right);
bigint stop = seltop;
bigint sleft = selleft;
bigint sbottom = selbottom;
bigint sright = selright;
bool simpleflip;
if (Contains(top, left, bottom, right)) {
// selection encloses entire pattern so we may only need to flip a smaller rectangle
if (topbottom) {
bigint tdiff = top; tdiff -= stop;
bigint bdiff = sbottom; bdiff -= bottom;
bigint mindiff = tdiff;
if (bdiff < mindiff) mindiff = bdiff;
stop += mindiff;
sbottom -= mindiff;
sleft = left;
sright = right;
} else {
bigint ldiff = left; ldiff -= sleft;
bigint rdiff = sright; rdiff -= right;
bigint mindiff = ldiff;
if (rdiff < mindiff) mindiff = rdiff;
sleft += mindiff;
sright -= mindiff;
stop = top;
sbottom = bottom;
}
simpleflip = true;
} else {
// selection encloses part of pattern so we can clip some selection edges
// if they are outside the pattern edges
if (topbottom) {
if (sleft < left) sleft = left;
if (sright > right) sright = right;
} else {
if (stop < top) stop = top;
if (sbottom > bottom) sbottom = bottom;
}
simpleflip = false;
}
// can only use getcell/setcell in limited domain
if ( OutsideLimits(stop, sbottom, sleft, sright) ) {
ErrorMessage(selection_too_big);
return false;
}
int itop = stop.toint();
int ileft = sleft.toint();
int ibottom = sbottom.toint();
int iright = sright.toint();
if (simpleflip) {
// selection encloses all of pattern so we can flip into new universe
// (must be same type) without killing live cells in selection
lifealgo* newalgo = CreateNewUniverse(currlayer->algtype);
if (newalgo->setrule(currlayer->algo->getrule()))
newalgo->setrule(newalgo->DefaultRule());
newalgo->setGeneration( currlayer->algo->getGeneration() );
if ( FlipRect(topbottom, currlayer->algo, newalgo, false, itop, ileft, ibottom, iright) ) {
// switch to newalgo
delete currlayer->algo;
currlayer->algo = newalgo;
SetGenIncrement();
} else {
// user aborted flip
delete newalgo;
return false;
}
} else {
// flip into temporary universe and kill all live cells in selection;
// if only 2 cell states then use qlife because its setcell/getcell calls are faster
lifealgo* tempalgo = CreateNewUniverse(currlayer->algo->NumCellStates() > 2 ?
currlayer->algtype :
QLIFE_ALGO);
// make sure temporary universe has same # of cell states
if (currlayer->algo->NumCellStates() > 2)
if (tempalgo->setrule(currlayer->algo->getrule()))
tempalgo->setrule(tempalgo->DefaultRule());
if ( FlipRect(topbottom, currlayer->algo, tempalgo, true, itop, ileft, ibottom, iright) ) {
// find pattern edges in temporary universe (could be much smaller)
// and copy temporary pattern into current universe
tempalgo->findedges(&top, &left, &bottom, &right);
CopyRect(top.toint(), left.toint(), bottom.toint(), right.toint(),
tempalgo, currlayer->algo, false, "Adding flipped selection");
delete tempalgo;
} else {
// user aborted flip so flip tempalgo pattern back into current universe
FlipRect(topbottom, tempalgo, currlayer->algo, false, itop, ileft, ibottom, iright);
delete tempalgo;
return false;
}
}
// flips are always reversible so no need to use SaveCellChange and RememberCellChanges
if (allowundo && !currlayer->stayclean && !inundoredo) {
//!!! if (inscript) SavePendingChanges();
currlayer->undoredo->RememberFlip(topbottom, currlayer->dirty);
}
// update currlayer->dirty AFTER RememberFlip
if (!inundoredo) MarkLayerDirty();
UpdatePatternAndStatus();
return true;
}
// -----------------------------------------------------------------------------
const char* rotate_clockwise = "Rotating selection +90 degrees";
const char* rotate_anticlockwise = "Rotating selection -90 degrees";
bool Selection::RotateRect(bool clockwise,
lifealgo* srcalgo, lifealgo* destalgo, bool erasesrc,
int itop, int ileft, int ibottom, int iright,
int ntop, int nleft, int nbottom, int nright)
{
int wd = iright - ileft + 1;
int ht = ibottom - itop + 1;
double maxcount = (double)wd * (double)ht;
int cntr = 0;
bool abort = false;
int cx, cy, newx, newy, newxinc, newyinc, v=0;
if (clockwise) {
BeginProgress(rotate_clockwise);
newx = nright;
newyinc = 1;
newxinc = -1;
} else {
BeginProgress(rotate_anticlockwise);
newx = nleft;
newyinc = -1;
newxinc = 1;
}
for ( cy=itop; cy<=ibottom; cy++ ) {
newy = clockwise ? ntop : nbottom;
for ( cx=ileft; cx<=iright; cx++ ) {
int skip = srcalgo->nextcell(cx, cy, v);
if (skip + cx > iright)
skip = -1; // pretend we found no more live cells
if (skip >= 0) {
// found next live cell
cx += skip;
if (erasesrc) srcalgo->setcell(cx, cy, 0);
newy += newyinc * skip;
destalgo->setcell(newx, newy, v);
} else {
cx = iright + 1; // done this row
}
cntr++;
if ((cntr % 4096) == 0) {
double prog = ((cy - itop) * (double)(iright - ileft + 1) +
(cx - ileft)) / maxcount;
abort = AbortProgress(prog, "");
if (abort) break;
}
newy += newyinc;
}
if (abort) break;
newx += newxinc;
}
if (erasesrc) srcalgo->endofpattern();
destalgo->endofpattern();
EndProgress();
return !abort;
}
// -----------------------------------------------------------------------------
bool Selection::RotatePattern(bool clockwise,
bigint& newtop, bigint& newbottom,
bigint& newleft, bigint& newright,
bool inundoredo)
{
// create new universe of same type as current universe
lifealgo* newalgo = CreateNewUniverse(currlayer->algtype);
if (newalgo->setrule(currlayer->algo->getrule()))
newalgo->setrule(newalgo->DefaultRule());
// set same gen count
newalgo->setGeneration( currlayer->algo->getGeneration() );
// copy all live cells to new universe, rotating the coords by +/- 90 degrees
int itop = seltop.toint();
int ileft = selleft.toint();
int ibottom = selbottom.toint();
int iright = selright.toint();
int wd = iright - ileft + 1;
int ht = ibottom - itop + 1;
double maxcount = (double)wd * (double)ht;
int cntr = 0;
bool abort = false;
int cx, cy, newx, newy, newxinc, newyinc, firstnewy, v=0;
if (clockwise) {
BeginProgress(rotate_clockwise);
firstnewy = newtop.toint();
newx = newright.toint();
newyinc = 1;
newxinc = -1;
} else {
BeginProgress(rotate_anticlockwise);
firstnewy = newbottom.toint();
newx = newleft.toint();
newyinc = -1;
newxinc = 1;
}
lifealgo* curralgo = currlayer->algo;
for ( cy=itop; cy<=ibottom; cy++ ) {
newy = firstnewy;
for ( cx=ileft; cx<=iright; cx++ ) {
int skip = curralgo->nextcell(cx, cy, v);
if (skip + cx > iright)
skip = -1; // pretend we found no more live cells
if (skip >= 0) {
// found next live cell
cx += skip;
newy += newyinc * skip;
newalgo->setcell(newx, newy, v);
} else {
cx = iright + 1; // done this row
}
cntr++;
if ((cntr % 4096) == 0) {
double prog = ((cy - itop) * (double)(iright - ileft + 1) +
(cx - ileft)) / maxcount;
abort = AbortProgress(prog, "");
if (abort) break;
}
newy += newyinc;
}
if (abort) break;
newx += newxinc;
}
newalgo->endofpattern();
EndProgress();
if (abort) {
delete newalgo;
} else {
// rotate the selection edges
seltop = newtop;
selbottom = newbottom;
selleft = newleft;
selright = newright;
// switch to new universe and display results
delete currlayer->algo;
currlayer->algo = newalgo;
SetGenIncrement();
DisplaySelectionSize();
// rotating entire pattern is easily reversible so no need to use
// SaveCellChange and RememberCellChanges in this case
if (allowundo && !currlayer->stayclean && !inundoredo) {
//!!! if (inscript) SavePendingChanges();
currlayer->undoredo->RememberRotation(clockwise, currlayer->dirty);
}
// update currlayer->dirty AFTER RememberRotation
if (!inundoredo) MarkLayerDirty();
UpdatePatternAndStatus();
}
return !abort;
}
// -----------------------------------------------------------------------------
bool Selection::Rotate(bool clockwise, bool inundoredo)
{
if (!exists) return false;
// determine rotated selection edges
bigint halfht = selbottom; halfht -= seltop; halfht.div2();
bigint halfwd = selright; halfwd -= selleft; halfwd.div2();
bigint midy = seltop; midy += halfht;
bigint midx = selleft; midx += halfwd;
bigint newtop = midy; newtop += selleft; newtop -= midx;
bigint newbottom = midy; newbottom += selright; newbottom -= midx;
bigint newleft = midx; newleft += seltop; newleft -= midy;
bigint newright = midx; newright += selbottom; newright -= midy;
if (!inundoredo) {
// check if rotated selection edges are outside bounded grid
if ((currlayer->algo->gridwd > 0 &&
(newleft < currlayer->algo->gridleft || newright > currlayer->algo->gridright)) ||
(currlayer->algo->gridht > 0 &&
(newtop < currlayer->algo->gridtop || newbottom > currlayer->algo->gridbottom))) {
ErrorMessage("New selection would be outside grid boundary.");
return false;
}
}
// if there is no pattern then just rotate the selection edges
if (currlayer->algo->isEmpty()) {
SaveCurrentSelection();
seltop = newtop;
selbottom = newbottom;
selleft = newleft;
selright = newright;
RememberNewSelection("Rotation");
DisplaySelectionSize();
UpdatePatternAndStatus();
return true;
}
// if the current selection and the rotated selection are both outside the
// pattern edges (ie. both are empty) then just rotate the selection edges
bigint top, left, bottom, right;
currlayer->algo->findedges(&top, &left, &bottom, &right);
if ( (seltop > bottom || selbottom < top || selleft > right || selright < left) &&
(newtop > bottom || newbottom < top || newleft > right || newright < left) ) {
SaveCurrentSelection();
seltop = newtop;
selbottom = newbottom;
selleft = newleft;
selright = newright;
RememberNewSelection("Rotation");
DisplaySelectionSize();
UpdatePatternAndStatus();
return true;
}
// can only use nextcell/getcell/setcell in limited domain
if (TooBig()) {
ErrorMessage(selection_too_big);
return false;
}
// make sure rotated selection edges are also within limits
if ( OutsideLimits(newtop, newbottom, newleft, newright) ) {
ErrorMessage("New selection would be outside +/- 10^9 boundary.");
return false;
}
// use faster method if selection encloses entire pattern
if (Contains(top, left, bottom, right)) {
return RotatePattern(clockwise, newtop, newbottom, newleft, newright, inundoredo);
}
int itop = seltop.toint();
int ileft = selleft.toint();
int ibottom = selbottom.toint();
int iright = selright.toint();
int ntop = newtop.toint();
int nleft = newleft.toint();
int nbottom = newbottom.toint();
int nright = newright.toint();
// save cell changes if undo/redo is enabled and script isn't constructing a pattern
// and we're not undoing/redoing an earlier rotation
bool savecells = allowundo && !currlayer->stayclean && !inundoredo;
//!!! if (savecells && inscript) SavePendingChanges();
lifealgo* oldalgo = NULL;
int otop = itop;
int oleft = ileft;
int obottom = ibottom;
int oright = iright;
if (savecells) {
// copy current pattern to oldalgo using union of old and new selection rects
if (otop > ntop) otop = ntop;
if (oleft > nleft) oleft = nleft;
if (obottom < nbottom) obottom = nbottom;
if (oright < nright) oright = nright;
oldalgo = CreateNewUniverse(currlayer->algo->NumCellStates() > 2 ? currlayer->algtype : QLIFE_ALGO);
// make sure universe has same # of cell states
if (currlayer->algo->NumCellStates() > 2)
if (oldalgo->setrule(currlayer->algo->getrule()))
oldalgo->setrule(oldalgo->DefaultRule());
if ( !CopyRect(otop, oleft, obottom, oright, currlayer->algo, oldalgo,
false, "Saving part of pattern") ) {
delete oldalgo;
return false;
}
}
// create temporary universe; doesn't need to match current universe so
// if only 2 cell states then use qlife because its setcell/getcell calls are faster
lifealgo* tempalgo = CreateNewUniverse(currlayer->algo->NumCellStates() > 2 ?
currlayer->algtype :
QLIFE_ALGO);
// make sure temporary universe has same # of cell states
if (currlayer->algo->NumCellStates() > 2)
if (tempalgo->setrule(currlayer->algo->getrule()))
tempalgo->setrule(tempalgo->DefaultRule());
// copy (and kill) live cells in selection to temporary universe,
// rotating the new coords by +/- 90 degrees
if ( !RotateRect(clockwise, currlayer->algo, tempalgo, true,
itop, ileft, ibottom, iright,
ntop, nleft, nbottom, nright) ) {
// user aborted rotation
if (savecells) {
// use oldalgo to restore erased selection
CopyRect(itop, ileft, ibottom, iright, oldalgo, currlayer->algo, false, "Restoring selection");
delete oldalgo;
} else {
// restore erased selection by rotating tempalgo in opposite direction
// back into the current universe
RotateRect(!clockwise, tempalgo, currlayer->algo, false,
ntop, nleft, nbottom, nright,
itop, ileft, ibottom, iright);
}
delete tempalgo;
UpdatePatternAndStatus();
return false;
}
// copy rotated selection from temporary universe to current universe;
// check if new selection rect is outside modified pattern edges
currlayer->algo->findedges(&top, &left, &bottom, &right);
if ( newtop > bottom || newbottom < top || newleft > right || newright < left ) {
// safe to use fast nextcell calls
CopyRect(ntop, nleft, nbottom, nright, tempalgo, currlayer->algo, false, "Adding rotated selection");
} else {
// have to use slow getcell calls
CopyAllRect(ntop, nleft, nbottom, nright, tempalgo, currlayer->algo, "Pasting rotated selection");
}
// don't need temporary universe any more
delete tempalgo;
// rotate the selection edges
seltop = newtop;
selbottom = newbottom;
selleft = newleft;
selright = newright;
if (savecells) {
// compare patterns in oldalgo and currlayer->algo and call SaveCellChange
// for each cell that has a different state
if ( SaveDifferences(oldalgo, currlayer->algo, otop, oleft, obottom, oright) ) {
Selection oldsel(itop, ileft, ibottom, iright);
Selection newsel(ntop, nleft, nbottom, nright);
currlayer->undoredo->RememberRotation(clockwise, oldsel, newsel, currlayer->dirty);
} else {
currlayer->undoredo->ForgetCellChanges();
Warning("You can't undo this change!");
}
delete oldalgo;
}
// display results
DisplaySelectionSize();
if (!inundoredo) MarkLayerDirty();
UpdatePatternAndStatus();
return true;
}
|