File: tile.lua

package info (click to toggle)
golly 3.3-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 20,176 kB
  • sloc: cpp: 72,638; ansic: 25,919; python: 7,921; sh: 4,245; objc: 3,721; java: 2,781; xml: 1,362; makefile: 530; javascript: 279; perl: 69
file content (176 lines) | stat: -rwxr-xr-x 4,666 bytes parent folder | download | duplicates (3)
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
-- Tile current selection with pattern inside selection.
-- Author: Andrew Trevorrow (andrew@trevorrow.com), Mar 2016.

local g = golly()
local gp = require "gplus"

local selrect = g.getselrect()
if #selrect == 0 then g.exit("There is no selection.") end

local selpatt = g.getcells(selrect)
if #selpatt == 0 then g.exit("No pattern in selection.") end

-- determine if selpatt is one-state or multi-state
local inc = 2
if (#selpatt & 1) == 1 then inc = 3 end

--------------------------------------------------------------------------------

local function clip_left(cells, left)
    local len = #cells
    local x = 1
    if inc == 3 then
        -- ignore padding int if present
        if len % 3 == 1 then len = len - 1 end
        while x <= len do
            if cells[x] >= left then
                g.setcell(cells[x], cells[x+1], cells[x+2])
            end
            x = x + 3
        end
    else
        while x <= len do
            if cells[x] >= left then
                g.setcell(cells[x], cells[x+1], 1)
            end
            x = x + 2
        end
    end
end

--------------------------------------------------------------------------------

local function clip_right(cells, right)
    local len = #cells
    local x = 1
    if inc == 3 then
        -- ignore padding int if present
        if len % 3 == 1 then len = len - 1 end
        while x <= len do
            if cells[x] <= right then
                g.setcell(cells[x], cells[x+1], cells[x+2])
            end
            x = x + 3
        end
    else   
        while x <= len do
            if cells[x] <= right then
                g.setcell(cells[x], cells[x+1], 1)
            end
            x = x + 2
        end
    end
end

--------------------------------------------------------------------------------

local function clip_top(cells, top)
    local len = #cells
    local y = 2
    if inc == 3 then
        -- ignore padding int if present
        if len % 3 == 1 then len = len - 1 end
        while y <= len do
            if cells[y] >= top then
                g.setcell(cells[y-1], cells[y], cells[y+1])
            end
            y = y + 3
        end
    else   
        while y <= len do
            if cells[y] >= top then
                g.setcell(cells[y-1], cells[y], 1)
            end
            y = y + 2
        end
    end
end

--------------------------------------------------------------------------------

local function clip_bottom(cells, bottom)
    local len = #cells
    local y = 2
    if inc == 3 then
        -- ignore padding int if present
        if len % 3 == 1 then len = len - 1 end
        while y <= len do
            if cells[y] <= bottom then
                g.setcell(cells[y-1], cells[y], cells[y+1])
            end
            y = y + 3
        end
    else   
        while y <= len do
            if cells[y] <= bottom then
                g.setcell(cells[y-1], cells[y], 1)
            end
            y = y + 2
        end
    end
end

--------------------------------------------------------------------------------

-- get selection edges
local selleft, seltop, selright, selbottom = gp.getedges(selrect)

-- find selpatt's minimal bounding box
local bbox = gp.getminbox(selpatt)
local i

-- first tile selpatt horizontally, clipping where necessary
local left = bbox.x
local right = left + bbox.wd - 1
i = 0
while left > selleft do
    left = left - bbox.wd
    i = i + 1
    if left >= selleft then
        g.putcells(selpatt, -bbox.wd * i, 0)
    else
        local tempcells = g.transform(selpatt, -bbox.wd * i, 0)
        clip_left(tempcells, selleft)
    end
end
i = 0
while right < selright do
    right = right + bbox.wd
    i = i + 1
    if right <= selright then
        g.putcells(selpatt, bbox.wd * i, 0)
    else
        local tempcells = g.transform(selpatt, bbox.wd * i, 0)
        clip_right(tempcells, selright)
    end
end

-- get new selection pattern and tile vertically, clipping where necessary
selpatt = g.getcells(selrect)
bbox = gp.getminbox(selpatt)
local top = bbox.y
local bottom = top + bbox.ht - 1
i = 0
while top > seltop do
    top = top - bbox.ht
    i = i + 1
    if top >= seltop then
        g.putcells(selpatt, 0, -bbox.ht * i)
    else
        local tempcells = g.transform(selpatt, 0, -bbox.ht * i)
        clip_top(tempcells, seltop)
    end
end
i = 0
while bottom < selbottom do
    bottom = bottom + bbox.ht
    i = i + 1
    if bottom <= selbottom then
        g.putcells(selpatt, 0, bbox.ht * i)
    else
        local tempcells = g.transform(selpatt, 0, bbox.ht * i)
        clip_bottom(tempcells, selbottom)
    end
end

if not g.visrect(selrect) then g.fitsel() end