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
|
--[[
If the current rule uses a hexagonal neighborhood then create
an overlay that shows the current pattern in a hexagonal grid.
Much thanks to http://www.redblobgames.com/grids/hexagons/.
Author: Andrew Trevorrow (andrew@trevorrow.com), August 2016.
--]]
local g = golly()
-- require "gplus.strict"
local gp = require "gplus"
local op = require "oplus"
local int = gp.int
local round = gp.round
local split = gp.split
local draw_line = op.draw_line
local ov = g.overlay
local ovt = g.ovtable
-- minor optimizations
local abs = math.abs
-- the following are set in create_overlay()
local state_rgba = {} -- table of RGBA strings for each state
local show_grid_rgba -- RGBA string for showing grid lines
local hide_grid_rgba -- RGBA string for hiding grid lines
local grid_rgba -- set to one of the above
-- this array remembers which hexagons are alive
local livehexagons = {}
-- get pixel dimensions of current layer (used to create/resize overlay)
local viewwd, viewht = g.getview(g.getlayer())
-- keep track of the layer's middle pixel for drawing the central hexagon
local midx, midy = int(viewwd/2), int(viewht/2)
-- keep track of the position of the current layer (the central cell)
local xpos, ypos = gp.getposint()
-- keep track of the magnification of the current layer
local currmag = g.getmag()
local edgelen -- current length of a hexagon edge (see set_hex_size)
local minedge = 1 -- minimum edgelen
local maxedge = 22 -- maximum edgelen
local mingrid = 3 -- minimum edgelen at which to draw hexagonal grid
-- this array is used to set edgelen when currmag is 1 to 5 (scale = 1:2 to 1:32)
local hexsize = { 2, 3, 6, 11, maxedge }
-- the following depend on edgelen and are set in create_hextile()
local hextilewd -- width of tile used to draw hex grid
local hextileht -- height of tile used to draw hex grid
local hexwd -- width of 1 hexagon
local hexht -- height of 1 hexagon
local xrad -- x distances to each vertex
local yrad -- y distances to each vertex
local hextox -- used to find central x coord of a hexagon
local hextoy -- used to find central y coord of a hexagon
-- get width and height of the universe (in cells, bounded if either > 0)
local gridwd = g.getwidth()
local gridht = g.getheight()
-- these controls are created in create_overlay
local ssbutton -- Start/Stop button
local sbutton -- Step button
local s1button -- +1 button
local rbutton -- Reset button
local fbutton -- Fit button
local hbutton -- ? button
local ebutton -- X button
local zslider -- slider for zooming in/out
local toolbarht -- height of tool bar
local generating = false
--------------------------------------------------------------------------------
local function check_rule()
-- check if the current rule uses a hexagonal neighborhood
if not op.hexrule() then
if g.getalgo() == "RuleLoader" then
g.warn(
[[If the current rule does not use
a hexagonal neighborhood then the
results will look a bit strange!]])
-- let user continue
else
g.exit("The current rule does not use a hexagonal neighborhood.")
end
end
end
--------------------------------------------------------------------------------
local function get_vertex(xc, yc, i)
return int(xc + xrad[i] + 0.5), int(yc + yrad[i] + 0.5)
end
--------------------------------------------------------------------------------
local function create_hextile()
--[[
initialize values that depend on the current edgelen and
create a clip for tiling the background in draw_hex_grid()
that contains one flat-topped hexagon (minus bottom edge)
with a horizontal edge sticking out from the east vertex:
___
/4 5\
{3 0}---
\2 1/
--]]
-- these arrays are used in get_vertex
xrad = {}
yrad = {}
for i = 0, 5 do
local angle_rad = math.pi / 180 * 60 * i
xrad[i] = edgelen * math.cos(angle_rad)
yrad[i] = edgelen * math.sin(angle_rad)
end
local x0, y0 = get_vertex(midx, midy, 0) -- E
local x1, y1 = get_vertex(midx, midy, 1) -- SE
local x2, y2 = get_vertex(midx, midy, 2) -- SW
local x3, y3 = get_vertex(midx, midy, 3) -- W
local x4, y4 = get_vertex(midx, midy, 4) -- NW
local x5, y5 = get_vertex(midx, midy, 5) -- NE
hexwd = x0 - x3
hexht = y1 - y5
-- these values are used to find the x,y coords of a hexagon's center
hextox = edgelen * 3/2
hextoy = hexht
-- create the clip
hextilewd = hexwd + edgelen
hextileht = hexht
ov("rgba "..state_rgba[0])
ovt{"fill", x3, y4, hextilewd, hextileht}
ov("rgba "..grid_rgba)
draw_line(x0, y0, x1, y1)
-- skip bottom edge
-- draw_line(x1, y1, x2, y2)
draw_line(x2, y2, x3, y3)
draw_line(x3, y3, x4, y4)
draw_line(x4, y4, x5, y5)
draw_line(x5, y5, x0, y0)
-- draw horizontal edge from right-most vertex
draw_line(x0, y0, x0 + edgelen, y0)
ov("copy "..x3.." "..y4.." "..hextilewd.." "..hextileht.." hextile")
end
--------------------------------------------------------------------------------
local function cube_round(x, y, z)
local rx = round(x)
local ry = round(y)
local rz = round(z)
local x_diff = abs(rx - x)
local y_diff = abs(ry - y)
local z_diff = abs(rz - z)
if x_diff > y_diff and x_diff > z_diff then
rx = -ry-rz
elseif y_diff > z_diff then
ry = -rx-rz
else
rz = -rx-ry
end
return rx, rz
end
--------------------------------------------------------------------------------
local function pixel_to_hex(x, y)
local q = (x - midx) / hextox
local r = (y - midy) / hextoy - q / 2
return cube_round(q, -q-r, r)
end
--------------------------------------------------------------------------------
local function get_visible_rect()
-- get axial coords of corner hexagons
local qmin, _ = pixel_to_hex(0, 0)
local qmax, _ = pixel_to_hex(viewwd-1, viewht-1)
local _, rmin = pixel_to_hex(viewwd-1, 0)
local _, rmax = pixel_to_hex(0, viewht-1)
-- convert axial coords to cell coords
local minx = qmin + rmin + xpos
local maxx = qmax + rmax + xpos
local miny = rmin + ypos
local maxy = rmax + ypos
-- adjust limits if current layer's grid is bounded
if gridwd > 0 then
local bminx = -int(gridwd / 2)
local bmaxx = bminx + gridwd - 1
if minx < bminx then minx = bminx end
if maxx > bmaxx then maxx = bmaxx end
end
if gridht > 0 then
local bminy = -int(gridht / 2)
local bmaxy = bminy + gridht - 1
if miny < bminy then miny = bminy end
if maxy > bmaxy then maxy = bmaxy end
end
return { minx, miny, maxx-minx+1, maxy-miny+1 }
end
--------------------------------------------------------------------------------
local function cell_to_hexagon(x, y)
--[[
convert cell position x,y to axial coords q,r so that the pattern
is rotated clockwise by 45 degrees; ie. the square cell at x,y = -1,-1
will be in the hexagonal cell at q,r = 0,-1
_________________ _____
| | | | / \
|-1,-1| 0,-1|+1,-1| -----{ 0,-1 }-----
|_____|_____|_____| /-1,0 \_____/ +1,-1\
| | | | \ / \ /
|-1,0 | 0,0 |+1,0 | ---> -----{ 0,0 }----{
|_____|_____|_____| /-1,+1 \_____/ +1,0 \
| | | | \ / \ /
|-1,+1| 0,+1|+1,+1| -----{ 0,+1 }-----
|_____|_____|_____| \_____/
--]]
local r = y - ypos
local q = x - xpos - r
-- return central pixel position of corresponding hexagon in overlay
x = int(hextox * q + midx + 0.5)
y = int(hextoy * (r + q/2) + midy + 0.5)
return x, y
end
--------------------------------------------------------------------------------
local function fill_hexagon(xc, yc, state)
ov("rgba "..state_rgba[state])
if edgelen < mingrid then
if edgelen == 1 then
ovt{"set", xc, yc}
else
local x = int(xc - edgelen/2 + 0.5)
local y = int(yc - edgelen/2 + 0.5)
ovt{"fill", x, y, edgelen, edgelen}
end
else
-- adjust xc and/or yc if they are outside overlay but hexagon is partially visible
local check_pixel = false
if xc < 0 then
if xc + edgelen >= 0 then
xc = 0
check_pixel = true
else
return
end
elseif xc >= viewwd then
if xc - edgelen < viewwd then
xc = viewwd-1
check_pixel = true
else
return
end
end
if yc < toolbarht then
if yc + hexht/2 >= toolbarht then
yc = toolbarht
check_pixel = true
else
return
end
elseif yc >= viewht then
if yc - hexht/2 < viewht then
yc = viewht-1
check_pixel = true
else
return
end
end
if check_pixel then
-- avoid filling grid line
if ov("get "..xc.." "..yc) == grid_rgba then return end
end
ov("flood "..xc.." "..yc)
end
end
--------------------------------------------------------------------------------
local function draw_live_cells()
livehexagons = {}
if g.empty() then return end
-- get live cells that will be visible in current hex grid
local cells = g.getcells( get_visible_rect() )
if #cells == 0 then return end
local len = #cells
local inc = 2
if len & 1 == 1 then
-- multi-state cell array so set inc to 3 and ignore any padding int
inc = 3
if len % 3 == 1 then len = len - 1 end
end
for i = 1, len, inc do
local x = cells[i]
local y = cells[i+1]
local state = 1
if inc == 3 then state = cells[i+2] end
-- calculate pixel at center of hexagon and fill it with current state
local xc, yc = cell_to_hexagon(x, y)
fill_hexagon(xc, yc, state)
livehexagons[#livehexagons + 1] = {xc, yc}
end
end
--------------------------------------------------------------------------------
local function kill_live_cells()
for _, hexcell in ipairs(livehexagons) do
local xc, yc = table.unpack(hexcell)
fill_hexagon(xc, yc, 0)
end
livehexagons = nil
end
--------------------------------------------------------------------------------
local function draw_hex_grid()
-- draw middle hexagon
local x = int(midx - edgelen + 0.5)
local y = int(midy - hextileht/2 + 0.5)
local xy = {"paste"}
local xyi = 2
xy[xyi] = x
xy[xyi + 1] = y
xyi = xyi + 2
-- draw middle row of hexagons
while x > 0 do
x = x - hextilewd
xy[xyi] = x
xy[xyi + 1] = y
xyi = xyi + 2
end
x = int(midx - edgelen + 0.5) + hextilewd
while x < viewwd do
xy[xyi] = x
xy[xyi + 1] = y
xyi = xyi + 2
x = x + hextilewd
end
xy[xyi] = "hextile"
ovt(xy)
-- copy middle row and paste above and below until overlay is tiled
ov("copy 0 "..y.." "..viewwd.." "..hextileht.." row")
xy = {"paste"}
xyi = 2
while y > 0 do
y = y - hextileht
xy[xyi] = 0
xy[xyi + 1] = y
xyi = xyi + 2
end
y = int(midy - hextileht/2 + 0.5) + hextileht
while y < viewht do
xy[xyi] = 0
xy[xyi + 1] = y
xyi = xyi + 2
y = y + hextileht
end
xy[xyi] = "row"
ovt(xy)
end
--------------------------------------------------------------------------------
local function draw_boundary_line(x1, y1, x2, y2)
x1, y1 = cell_to_hexagon(x1, y1)
x2, y2 = cell_to_hexagon(x2, y2)
ovt{"line", x1, y1, x2, y2}
end
--------------------------------------------------------------------------------
local function draw_outer_hexagon(xc, yc, toprow, bottomrow, leftcol, rightcol)
local x0, y0 = get_vertex(xc, yc, 0)
local x1, y1 = get_vertex(xc, yc, 1)
local x2, y2 = get_vertex(xc, yc, 2)
local x3, y3 = get_vertex(xc, yc, 3)
local x4, y4 = get_vertex(xc, yc, 4)
local x5, y5 = get_vertex(xc, yc, 5)
if toprow then
-- draw top edge and NE edge
draw_line(x4, y4, x5, y5)
draw_line(x5, y5, x0, y0)
end
if bottomrow then
-- draw bottom edge and SW edge
draw_line(x1, y1, x2, y2)
draw_line(x2, y2, x3, y3)
end
if leftcol then
-- draw NW edge
draw_line(x3, y3, x4, y4)
if not toprow then
-- draw top edge
draw_line(x4, y4, x5, y5)
end
end
if rightcol then
-- draw SE edge
draw_line(x0, y0, x1, y1)
if not bottomrow then
-- draw bottom edge
draw_line(x1, y1, x2, y2)
end
end
end
--------------------------------------------------------------------------------
local function draw_inner_hexagon(xc, yc, toprow, leftcol)
local x0, y0 = get_vertex(xc, yc, 0)
local x1, y1 = get_vertex(xc, yc, 1)
local x2, y2 = get_vertex(xc, yc, 2)
local x3, y3 = get_vertex(xc, yc, 3)
local x4, y4 = get_vertex(xc, yc, 4)
local x5, y5 = get_vertex(xc, yc, 5)
draw_line(x0, y0, x1, y1)
draw_line(x1, y1, x2, y2)
draw_line(x2, y2, x3, y3)
if leftcol then
-- need to draw NW edge
draw_line(x3, y3, x4, y4)
end
if toprow then
-- need to draw top edge and NE edge
draw_line(x4, y4, x5, y5)
draw_line(x5, y5, x0, y0)
elseif leftcol then
-- need to draw top edge
draw_line(x4, y4, x5, y5)
end
end
--------------------------------------------------------------------------------
local function outside_grid(q, r)
if gridwd == 0 and gridht == 0 then
-- grid is unbounded
return false
end
-- convert axial coords to cell coords
local x = q + r + xpos
local y = r + ypos
if gridwd > 0 then
local bminx = -int(gridwd / 2)
local bmaxx = bminx + gridwd - 1
if x < bminx or x > bmaxx then
return true
end
end
if gridht > 0 then
local bminy = -int(gridht / 2)
local bmaxy = bminy + gridht - 1
if y < bminy or y > bmaxy then
return true
end
end
return false
end
--------------------------------------------------------------------------------
local function border_visible()
-- return true if any corner hexagon is outside the bounded grid
if outside_grid( pixel_to_hex(0,0) ) then return true end
if outside_grid( pixel_to_hex(0,viewht-1) ) then return true end
if outside_grid( pixel_to_hex(viewwd-1,0) ) then return true end
if outside_grid( pixel_to_hex(viewwd-1,viewht-1) ) then return true end
return false
end
--------------------------------------------------------------------------------
local function pixel_in_overlay(x, y)
return x >= 0 and x < viewwd and y >= 0 and y < viewht
end
--------------------------------------------------------------------------------
local function draw_bounded_grid()
local minx, miny, wd, ht = table.unpack( get_visible_rect() )
local maxx = minx+wd-1
local maxy = miny+ht-1
if edgelen < mingrid then
-- fill bounded diamond or strip with state 0 color
ov("rgba "..state_rgba[0])
local offset
if edgelen == 2 and (gridht == 0 or gridwd == 0) then
-- offset needs to be larger so strip is wider
offset = 0.5
else
offset = (edgelen - 1) / 3
end
draw_boundary_line(minx-offset, miny-offset, maxx+offset, miny-offset)
draw_boundary_line(maxx+offset, miny-offset, maxx+offset, maxy+offset)
draw_boundary_line(maxx+offset, maxy+offset, minx-offset, maxy+offset)
draw_boundary_line(minx-offset, maxy+offset, minx-offset, miny-offset)
if edgelen > 1 or (edgelen == 1 and wd > 1 and ht > 1) then
local xc, yc = cell_to_hexagon(minx + int(wd/2), miny + int(ht/2))
ov("flood "..xc.." "..yc)
end
else
-- draw the outer edges of the outer hexagons
ov("rgba "..grid_rgba)
for y = miny, maxy do
for x = minx, maxx do
if x == minx or x == maxx or y == miny or y == maxy then
local xc, yc = cell_to_hexagon(x, y)
draw_outer_hexagon(xc, yc, y == miny, y == maxy, x == minx, x == maxx)
end
end
end
-- flood the interior with state 0 color
ov("rgba "..state_rgba[0])
local xc, yc = cell_to_hexagon(minx + int(wd/2), miny + int(ht/2))
if pixel_in_overlay(xc, yc) then
ov("flood "..xc.." "..yc)
else
-- central hexagon is not visible so search border hexagons
for y = miny, maxy do
for x = minx, maxx do
if x == minx or x == maxx or y == miny or y == maxy then
local xc, yc = cell_to_hexagon(x, y)
if pixel_in_overlay(xc, yc) then
ov("flood "..xc.." "..yc)
goto done
end
end
end
end
::done::
end
-- draw the inner hexagon edges
ov("rgba "..grid_rgba)
for y = miny, maxy do
for x = minx, maxx do
local xc, yc = cell_to_hexagon(x, y)
draw_inner_hexagon(xc, yc, y == miny, x == minx)
end
end
end
end
--------------------------------------------------------------------------------
local function draw_tool_bar()
ov(op.white)
ovt{"fill", 0, 0, viewwd, toolbarht}
-- must draw line at bottom edge of tool bar in case a cell color is white
ov("rgba "..show_grid_rgba)
draw_line(0, toolbarht-1, viewwd-1, toolbarht-1)
local gap = 10
local x = gap
local y = int((toolbarht - ssbutton.ht) / 2)
if y < 10 then return end
if x >= viewwd then return end
ssbutton.show(x, y)
x = x + ssbutton.wd + gap
if x >= viewwd then return end
sbutton.show(x, y)
x = x + sbutton.wd + gap
if x >= viewwd then return end
s1button.show(x, y)
x = x + s1button.wd + gap
if x >= viewwd then return end
rbutton.show(x, y)
x = x + rbutton.wd + gap
if x >= viewwd then return end
zslider.show(x, y, edgelen)
x = x + zslider.wd + gap
if x >= viewwd then return end
fbutton.show(x, y)
x = viewwd - gap - ebutton.wd
if x >= viewwd then return end
ebutton.show(x, y)
x = x - gap - hbutton.wd
if x >= viewwd then return end
hbutton.show(x, y)
end
--------------------------------------------------------------------------------
local function refresh()
create_hextile()
if (gridwd > 0 or gridht > 0) and border_visible() then
-- fill overlay with border color
local br, bg, bb = g.getcolor("border")
ovt{"rgba", br, bg, bb, 255}
ovt{"fill"}
draw_bounded_grid()
else
-- fill overlay with state 0 color
ov("rgba "..state_rgba[0])
ovt{"fill"}
if edgelen >= mingrid then
draw_hex_grid()
end
end
draw_live_cells()
-- if selection exists then draw translucent selection color over selected cells???!!!
draw_tool_bar()
ov("update")
end
--------------------------------------------------------------------------------
local function set_hex_size()
-- get current layer's magnification
currmag = g.getmag()
-- set edgelen so hexagons are roughly the same size as cells
if currmag <= 0 then
edgelen = 1
else
-- currmag is 1 to 5 (scale is 2^mag)
edgelen = hexsize[currmag]
end
end
--------------------------------------------------------------------------------
local function sync_layer_scale()
-- edgelen has been changed so set layer magnification accordingly
for i = #hexsize, 1, -1 do
if edgelen >= hexsize[i] then
currmag = i
g.setmag(currmag)
return
end
end
currmag = 0
g.setmag(currmag)
end
--------------------------------------------------------------------------------
local function fit_pattern()
g.fit()
-- update currmag and edgelen
set_hex_size()
if not g.empty() then
-- may need to reduce edgelen further to see live cells at left/right edges
local x, y, w, h = table.unpack(g.getrect())
while edgelen > minedge do
-- need to update values that depend on edgelen
create_hextile()
-- also need to update xpos and ypos
xpos, ypos = gp.getposint()
local x1, y1 = cell_to_hexagon(x, y+h-1)
local x2, y2 = cell_to_hexagon(x+w-1, y)
if pixel_in_overlay(x1, y1) and pixel_in_overlay(x2, y2) then
break
end
edgelen = edgelen - 1
sync_layer_scale()
end
end
refresh()
-- update the status bar to show scale
g.update()
end
--------------------------------------------------------------------------------
local function zoom_in()
if g.getoption("showoverlay") == 0 then
-- zoom in to the current layer's central pixel
-- (can't use midx and midy because they have been offset by toolbarht/2)
g.doevent("zoomin "..int(viewwd/2).." "..int(viewht/2))
elseif edgelen < maxedge then
-- draw bigger hexagons
edgelen = edgelen + 1
refresh()
sync_layer_scale()
end
end
--------------------------------------------------------------------------------
local function zoom_out()
if g.getoption("showoverlay") == 0 then
-- zoom out from the current layer's central pixel
-- (can't use midx and midy because they have been offset by toolbarht/2)
g.doevent("zoomout "..int(viewwd/2).." "..int(viewht/2))
elseif edgelen > minedge then
-- draw smaller hexagons
edgelen = edgelen - 1
refresh()
sync_layer_scale()
end
end
--------------------------------------------------------------------------------
local function zoom_hexagons(event)
local direction, x, y = split(event)
x = tonumber(x)
y = tonumber(y)
if y < toolbarht then return end
if direction == "ozoomin" then
-- try to zoom in to hexagon at x,y???!!!
zoom_in()
else
-- try to zoom out from hexagon at x,y???!!!
zoom_out()
end
end
--------------------------------------------------------------------------------
local function click_in_layer(event)
-- process a mouse click in current layer like "click 100 20 left none"
local _, x, y, button, mods = split(event)
local state = g.getcell(x, y)
if button ~= "left" then return end
if mods ~= "none" and mods ~= "shift" then return end
if g.getcursor() == "Draw" then
local drawstate = g.getoption("drawingstate")
if state == 0 and drawstate == 0 then
-- no change
else
if state == drawstate then
-- kill cell
g.setcell(x, y, 0)
else
g.setcell(x, y, drawstate)
end
g.update()
refresh() -- updates overlay (currently not shown)
end
elseif g.getcursor() == "Pick" then
g.setoption("drawingstate", state)
g.update() -- updates edit bar
elseif g.getcursor() == "Select" then
-- don't allow selections???!!!
-- g.doevent(event)
else
-- g.getcursor() == "Move" or "Zoom In" or "Zoom Out"
g.doevent(event)
end
end
--------------------------------------------------------------------------------
local function edit_hexagons(x, y)
-- get axial coords of hexagon containing clicked pixel
local q, r = pixel_to_hex(x, y)
-- check if hexagon is outside bounded grid
if outside_grid(q, r) then return end
-- update corresponding cell in current layer
local xcell = q + r + xpos
local ycell = r + ypos
local state = g.getcell(xcell, ycell)
local drawstate = g.getoption("drawingstate")
if state == drawstate then
-- kill cell
g.setcell(xcell, ycell, 0)
else
g.setcell(xcell, ycell, drawstate)
end
-- get central pixel of hexagon
local xc = int(hextox * q + midx + 0.5)
local yc = int(hextoy * (r + q/2) + midy + 0.5)
-- update hexagon
if state == drawstate then
-- erase hexagon
fill_hexagon(xc, yc, 0)
else
fill_hexagon(xc, yc, drawstate)
if drawstate > 0 then
livehexagons[#livehexagons + 1] = {xc, yc}
end
end
-- display overlay and show new population count in status bar
g.update()
end
--------------------------------------------------------------------------------
local function get_state(color)
-- search state_rgba array for color and return matching state
for s, c in pairs(state_rgba) do
if c == color then
return s
end
end
return -1 -- no match
end
--------------------------------------------------------------------------------
local function set_position(x, y)
-- avoid error message from gp.setposint if grid is bounded
if gridwd > 0 then
local bminx = -int(gridwd / 2)
local bmaxx = bminx + gridwd - 1
if x < bminx or x > bmaxx then return end
end
if gridht > 0 then
local bminy = -int(gridht / 2)
local bmaxy = bminy + gridht - 1
if y < bminy or y > bmaxy then return end
end
gp.setposint(x, y)
end
--------------------------------------------------------------------------------
local function check_position()
-- refresh overlay if current layer's position has changed
local x0, y0 = gp.getposint()
if x0 ~= xpos or y0 ~= ypos then
xpos = x0
ypos = y0
refresh()
end
end
--------------------------------------------------------------------------------
local function check_position_and_scale()
-- refresh overlay if current layer's position or scale has changed
local call_refresh = false
local x0, y0 = gp.getposint()
if x0 ~= xpos or y0 ~= ypos then
xpos = x0
ypos = y0
call_refresh = true
end
local newmag = g.getmag()
if newmag ~= currmag then
set_hex_size()
call_refresh = true
end
if call_refresh then refresh() end
end
--------------------------------------------------------------------------------
local function move_hexagons(x, y)
-- get axial coords of hexagon containing clicked pixel
local q, r = pixel_to_hex(x, y)
-- check if hexagon is outside bounded grid
if outside_grid(q, r) then return end
-- drag clicked hexagon to new position
local prevq, prevr = q, r
while true do
local event = g.getevent()
if event:find("^mup") then break end
local xy = ov("xy")
if #xy > 0 then
x, y = split(xy)
q, r = pixel_to_hex(tonumber(x), tonumber(y))
if q ~= prevq or r ~= prevr then
if not outside_grid(q, r) then
local deltaq = q - prevq
local deltar = r - prevr
--[[
^
| move up/down if deltaq == 0
v
}____{ _
/ \ /|
}-----{ q,r-1 }-----{ |/_ move NE/SW if deltaq == -deltar
/ \ / \
{ q-1,r }----{ q+1,r-1 }
\ / \ /
}-----{ q,r }-----{
/ \ / \
{ q-1,r+1 }----{ q+1,r }
\ / \ / _
}-----{ q,r+1 }-----{ |\ move NW/SE if deltar == 0
\ / _\|
}----{
--]]
if deltar ~= 0 and deltaq == 0 then
-- move current layer diagonally to move hexagons up/down
set_position(xpos - deltar, ypos - deltar)
check_position()
elseif deltaq ~= 0 and deltar == 0 then
-- move current layer left/right to move hexagons NW/SE
set_position(xpos - deltaq, ypos)
check_position()
elseif deltaq == -deltar then
-- move current layer up/down to move hexagons NE/SW
set_position(xpos, ypos + deltaq)
check_position()
else
-- move hexagons NE/SW until prevr = r
deltaq = prevr - r
ypos = ypos + deltaq
set_position(xpos, ypos)
prevq = prevq + deltaq
-- now move hexagons NW/SE until prevq = q
deltaq = q - prevq
set_position(xpos - deltaq, ypos)
check_position()
end
end
prevq, prevr = q, r
end
end
end
end
--------------------------------------------------------------------------------
local function pan(event)
-- user pressed an arrow key
if g.getoption("showoverlay") == 0 then
-- just pan the current layer (assuming the user hasn't changed
-- the default actions for the arrow keys)
g.doevent(event)
else
local _, key, mods = split(event)
local amount = 1
if edgelen < mingrid then
amount = 10 -- pan further at this scale
end
if mods == "none" then
-- to pan overlay orthogonally we need to pan layer diagonally
if key == "left" then
set_position(xpos-amount, ypos+amount)
elseif key == "right" then
set_position(xpos+amount, ypos-amount)
elseif key == "up" then
set_position(xpos-amount, ypos-amount)
elseif key == "down" then
set_position(xpos+amount, ypos+amount)
end
elseif mods == "shift" then
-- to pan overlay diagonally we need to pan layer orthogonally
if key == "left" then
set_position(xpos-amount, ypos)
elseif key == "right" then
set_position(xpos+amount, ypos)
elseif key == "up" then
set_position(xpos, ypos-amount)
elseif key == "down" then
set_position(xpos, ypos+amount)
end
end
-- main loop will call check_position_and_scale()
end
end
--------------------------------------------------------------------------------
local function click_in_overlay(event)
-- process a mouse click in overlay like "oclick 100 20 left none"
local _, x, y, button, mods = split(event)
x = tonumber(x)
y = tonumber(y)
if y < toolbarht then return end
if button ~= "left" then return end
if mods ~= "none" and mods ~= "shift" then return end
if g.getcursor() == "Draw" then
edit_hexagons(x, y)
elseif g.getcursor() == "Pick" then
local state = get_state( ov("get "..x.." "..y) )
if state >= 0 then
g.setoption("drawingstate", state)
g.update() -- updates edit bar
end
elseif g.getcursor() == "Move" then
move_hexagons(x, y)
elseif g.getcursor() == "Select" then
-- don't allow selections???!!!
elseif g.getcursor() == "Zoom In" then
-- zoom in to clicked hexagon
zoom_hexagons("ozoomin "..x.." "..y)
elseif g.getcursor() == "Zoom Out" then
-- zoom out from clicked hexagon
zoom_hexagons("ozoomout "..x.." "..y)
end
end
--------------------------------------------------------------------------------
local function check_grid()
if g.getoption("showgrid") == 1 then
if grid_rgba ~= show_grid_rgba then
grid_rgba = show_grid_rgba
refresh()
g.update()
end
else
if grid_rgba ~= hide_grid_rgba then
grid_rgba = hide_grid_rgba
refresh()
g.update()
end
end
end
--------------------------------------------------------------------------------
local function toggle_overlay()
if g.getoption("showoverlay") == 1 then
g.setoption("showoverlay", 0)
g.update()
else
g.setoption("showoverlay", 1)
-- force check_position_and_scale to call refresh
currmag = 666
end
end
--------------------------------------------------------------------------------
local function toggle_grid()
g.setoption("showgrid", 1 - g.getoption("showgrid"))
-- main loop will call check_grid() to do the update
end
--------------------------------------------------------------------------------
local function check_layer_size()
local newwd, newht = g.getview(g.getlayer())
if newwd ~= viewwd or newht ~= viewht then
viewwd, viewht = newwd, newht
if viewwd < 1 then viewwd = 1 end
if viewht < 1 then viewht = 1 end
midx, midy = int(viewwd/2), int(viewht/2 + toolbarht/2)
ov("resize "..viewwd.." "..viewht)
-- hide tool bar controls so show calls will use correct background
ssbutton.hide()
sbutton.hide()
s1button.hide()
rbutton.hide()
fbutton.hide()
hbutton.hide()
ebutton.hide()
zslider.hide()
refresh()
end
end
--------------------------------------------------------------------------------
local arrow_cursor = false
local function check_cursor()
local xy = ov("xy")
if #xy > 0 then
local x, y = split(xy)
x = tonumber(x)
y = tonumber(y)
if y < toolbarht then
-- mouse is inside tool bar
if not arrow_cursor then
ov("cursor arrow")
arrow_cursor = true
end
else
-- mouse is outside tool bar
if arrow_cursor then
ov("cursor current")
arrow_cursor = false
end
end
end
end
--------------------------------------------------------------------------------
local function update_start_button()
-- change label in ssbutton without changing the button's width
if generating then
ssbutton.setlabel("Stop", false)
else
ssbutton.setlabel("Start", false)
end
draw_tool_bar()
ov("update")
end
--------------------------------------------------------------------------------
local function advance(by1)
if g.empty() then
g.note("There are no live cells.")
generating = false
update_start_button()
return
end
if by1 then
g.run(1)
else
g.step()
end
-- erase live hexagons and draw new ones
kill_live_cells()
draw_live_cells()
draw_tool_bar()
g.update()
end
--------------------------------------------------------------------------------
local function step()
advance(false)
generating = false
update_start_button()
end
--------------------------------------------------------------------------------
local function step1()
advance(true)
generating = false
update_start_button()
end
--------------------------------------------------------------------------------
local function start_stop()
generating = not generating
update_start_button()
end
--------------------------------------------------------------------------------
local function reset()
g.reset()
-- erase live hexagons and draw new ones
kill_live_cells()
draw_live_cells()
generating = false
update_start_button()
draw_tool_bar()
g.update()
end
--------------------------------------------------------------------------------
local function zoom_slider(newval)
-- called if zslider position has changed
edgelen = newval
refresh()
sync_layer_scale()
end
--------------------------------------------------------------------------------
local function show_help()
if g.getoption("showoverlay") == 0 then
g.setoption("showoverlay", 1)
g.update()
end
local helptext =
[[
Features:
- Allows toggling between hex layer and current layer.
- Synchronizes hex layer's scale and position with current layer.
- Allows simple editing in hex layer or current layer.
- Allows pattern generation in hex layer or current layer.
- Hex layer can be zoomed in/out, but only from middle hexagon.
- Hex layer can be panned via arrow keys or via hand cursor.
- Hex layer has a tool bar with buttons and a slider for zooming.
Current limitations:
- Doesn't allow editing multiple cells/hexagons via click and drag.
- Doesn't show any selection, nor allow selection to be changed.
- Doesn't support zooming in/out from a particular hexagon.
- Hex layer can't be zoomed out beyond 1:1 scale.
Special keys and their actions:
enter/return - start/stop generating
tab - advance by step size
space - advance by 1
arrows - pan up/down/left/right
shift-arrows - pan NE/SE/NW/SW
f - fit pattern
h - display this help
l - show/hide grid lines
o - show/hide overlay
r - reset to starting pattern
[ - zoom out
] - zoom in
(click or hit any key to close help)]]
ov("font 11 mono-bold")
ov(op.black)
local w, h = split(ov("text temp "..helptext))
w = tonumber(w) + 20
h = tonumber(h) + 20
local x = int((viewwd - w) / 2)
local y = int((viewht - h) / 2)
ov(op.gray)
ovt{"fill", x, y, w, h}
ov("rgba 255 253 217 255") -- pale yellow (matches Help window)
ovt{"fill", (x+2), (y+2), (w-4), (h-4)}
local oldblend = ov("blend 1")
ovt{"paste", (x+10), (y+10), "temp"}
ov("blend "..oldblend)
ov("update")
while true do
local event = g.getevent()
if event:find("^key") or event:find("^oclick") then
break
end
end
refresh()
end
--------------------------------------------------------------------------------
local function exit_script()
g.show("") -- clear message
g.exit() -- don't beep
end
--------------------------------------------------------------------------------
local function create_overlay()
-- overlay covers entire viewport
ov("create "..viewwd.." "..viewht)
ov("cursor current") -- use current Golly cursor
-- use smaller buttons in our tool bar
op.buttonht = 20 -- height of buttons (also used for check boxes and sliders)
op.sliderwd = 12 -- width of slider button (best if even)
op.textgap = 8 -- gap between edge of button and its label
op.textfont = "font 10 default-bold" -- font for labels
op.textshadowx = 2
op.textshadowy = 2
if g.os() == "Mac" then
op.yoffset = -1
end
if g.os() == "Linux" then
op.textfont = "font 10 default"
end
-- create tool bar buttons
ssbutton = op.button("Start", start_stop)
sbutton = op.button("Step", step)
s1button = op.button("+1", step1)
rbutton = op.button("Reset", reset)
fbutton = op.button("Fit", fit_pattern)
hbutton = op.button("?", show_help)
ebutton = op.button("X", exit_script)
-- create a slider for zooming in/out
zslider = op.slider("", op.black, (maxedge-minedge+1)*3, minedge, maxedge, zoom_slider)
toolbarht = 20 + ssbutton.ht
midy = int(viewht/2 + toolbarht/2)
local currcolors = g.getcolors()
for i = 0, g.numstates()-1 do
state_rgba[i] = currcolors[2+i*4].." "..currcolors[3+i*4].." "..currcolors[4+i*4].." 255"
end
-- set grid colors based on current background color
local gridr, gridg, gridb = currcolors[2], currcolors[3], currcolors[4]
local diff = 48
if (gridr + gridg + gridb) / 3 > 127 then
-- light background so make grid lines slightly darker
if gridr > diff then gridr = gridr - diff else gridr = 0 end
if gridg > diff then gridg = gridg - diff else gridg = 0 end
if gridb > diff then gridb = gridb - diff else gridb = 0 end
else
-- dark background so make grid lines slightly lighter
if gridr + diff < 256 then gridr = gridr + diff else gridr = 255 end
if gridg + diff < 256 then gridg = gridg + diff else gridg = 255 end
if gridb + diff < 256 then gridb = gridb + diff else gridb = 255 end
end
show_grid_rgba = gridr.." "..gridg.." "..gridb.." 255"
if currcolors[2] > 0 then
-- hide_grid_rgba is the state 0 color with a slightly different red value
-- (this allows fill_hexagon to draw a hexagon with a flood command)
hide_grid_rgba = (currcolors[2]-1).." "..currcolors[3].." "..currcolors[4].." 255"
else
hide_grid_rgba = (currcolors[2]+1).." "..currcolors[3].." "..currcolors[4].." 255"
end
if g.getoption("showgrid") == 1 then
grid_rgba = show_grid_rgba
else
grid_rgba = hide_grid_rgba
end
-- initialize edgelen
set_hex_size()
end
--------------------------------------------------------------------------------
local function do_key(event)
local key = event:sub(5)
if key == "return none" then
start_stop()
elseif key == "space none" then
step1()
elseif key == "tab none" then
step()
elseif key:find("^up ") or key:find("^down ") or
key:find("^left ") or key:find("^right ") then
pan(event)
elseif key == "f none" then
fit_pattern()
elseif key == "h none" then
show_help()
elseif key == "l none" then
toggle_grid()
elseif key == "o none" then
toggle_overlay()
elseif key == "r none" then
reset()
elseif key == "] none" then
zoom_in()
elseif key == "[ none" then
zoom_out()
else
-- might be a user keyboard shortcut, so do it
g.doevent(event)
end
end
--------------------------------------------------------------------------------
function main()
check_rule()
create_overlay()
refresh()
g.show("Hit escape key to abort script...")
while true do
-- if size of layer has changed then resize the overlay
check_layer_size()
-- update cursor if mouse moves in/out of tool bar
check_cursor()
-- check for user input
local event = op.process( g.getevent() )
-- event is empty if op.process handled the given event
if #event > 0 then
if event:find("^key") then
do_key(event)
elseif event:find("^click") then
click_in_layer(event)
elseif event:find("^oclick") then
click_in_overlay(event)
elseif event:find("^ozoomin") or event:find("^ozoomout") then
zoom_hexagons(event)
else
-- eg. zoomin/zoomout event in current layer
g.doevent(event)
end
end
if g.getoption("showoverlay") == 1 then
-- check if current layer's position or scale has changed
check_position_and_scale()
end
-- check if user toggled grid lines (possibly via View menu)
check_grid()
if generating then advance(false) end
end
end
--------------------------------------------------------------------------------
local oldstack = g.setoption("stacklayers", 0)
local oldbuttons = g.setoption("showbuttons", 0) -- disable translucent buttons
local status, err = xpcall(main, gp.trace)
if err then g.continue(err) end
-- the following code is always executed
ov("delete")
g.setoption("stacklayers", oldstack)
g.setoption("showbuttons", oldbuttons)
|