File: tile-with-clip.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 (70 lines) | stat: -rwxr-xr-x 2,055 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
-- Tile current selection with clipboard pattern.
-- Author: Andrew Trevorrow (andrew@trevorrow.com), Mar 2016.

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

-- assume one-state cell array (may change below)
local multistate = false

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

local function clip_rb(cells, right, bottom)
    -- set given cells except those outside given right and bottom edges
    local len = #cells
    local x = 1
    local y = 2
    if multistate then
        -- ignore padding int if present
        if len % 3 == 1 then len = len - 1 end
        while x <= len do
            if (cells[x] <= right) and (cells[y] <= bottom) then
                g.setcell(cells[x], cells[y], cells[x+2])
            end
            x = x + 3
            y = y + 3
        end
    else   
        while x <= len do
            if (cells[x] <= right) and (cells[y] <= bottom) then
                g.setcell(cells[x], cells[y], 1)
            end
            x = x + 2
            y = y + 2
        end
    end
end

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

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

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

local pwidth, pheight, p = g.getclip()

if #p & 1 == 1 then multistate = true end

g.clear(0)
if #p > 0 then
    -- tile selrect with p, clipping right & bottom edges if necessary
    local y = seltop
    while y <= selbottom do
        local bottom = y + pheight - 1
        local x = selleft
        while x <= selright do
            local right = x + pwidth - 1
            if (right <= selright) and (bottom <= selbottom) then
                g.putcells(p, x, y)
            else
                local tempcells = g.transform(p, x, y)
                clip_rb(tempcells, selright, selbottom)
            end
            x = x + pwidth
        end
        y = y + pheight
    end
end
if not g.visrect(selrect) then g.fitsel() end