Skip to content

Instantly share code, notes, and snippets.

@drZool
Created February 7, 2022 12:09
Show Gist options
  • Select an option

  • Save drZool/02351cbdde73988288b4064be6b40b2a to your computer and use it in GitHub Desktop.

Select an option

Save drZool/02351cbdde73988288b4064be6b40b2a to your computer and use it in GitHub Desktop.
Aseprite plugin to make a grid layer
-- Makes a grid layer
if app.activeSprite == nil then
app.alert "There is no document open"
else
local settings = {
xSize = 16,
ySize = 16,
nthLine = 10,
opacity = 255,
color = Color{r=128, g=128, b=128},
color2 = Color{r=0, g=0, b=0},
lineSize = 1,
lineSize2 = 2,
secondGrid = true,
height = 0,
width = 0,
}
local function createLayer(sprite, name, opacity)
local layer = sprite:newLayer()
layer.name = name
layer.isEditable = false
layer.isContinuous = true
layer.opacity = opacity
sprite:newCel(layer, 1)
return layer
end
local function drawGridWithLines(gridData)
----------------
-- First grid --
----------------
local x = 0
local y = 0
local brush = Brush(gridData.lineSize)
local interval = 0
while y < gridData.height do
if not gridData.secondGrid or not(interval % gridData.nthLine == 0) then
app.useTool{
tool = "line",
color = gridData.color,
points = { Point(0, y), Point(gridData.width, y)},
brush = brush,
}
end
interval = interval + 1
y = y + gridData.ySize
end
interval = 0
while x < gridData.width do
if not gridData.secondGrid or not(interval % gridData.nthLine == 0) then
app.useTool{
tool = "line",
color = gridData.color,
points = { Point(x, 0), Point(x, gridData.height)},
brush = brush,
}
end
interval = interval + 1
x = x + gridData.xSize
end
-----------------
-- Second grid --
-----------------
if(not gridData.secondGrid) then
return
end
x = 0
y = 0
local brush2 = Brush(gridData.lineSize2)
while y < gridData.height do
app.useTool{
tool = "line",
color = gridData.color2,
points = { Point(0, y), Point(gridData.width, y)},
brush = brush2,
}
y = y + gridData.ySize * gridData.nthLine
end
while x < gridData.width do
app.useTool{
tool = "line",
color = gridData.color2,
points = { Point(x, 0), Point(x, gridData.height)},
brush = brush2,
}
x = x + gridData.xSize * gridData.nthLine
end
end
local maxGrid = app.activeSprite.width
if maxGrid > app.activeSprite.height then
maxGrid = app.activeSprite.height
end
local dlg = Dialog("Grid")
dlg :separator{ text="Size:" }
:slider {id="xSize", label="X:", min=2, max=maxGrid, value=settings.xSize}
:slider {id="ySize", label="Y:", min=2, max=maxGrid, value=settings.ySize}
:separator{ text="First grid:" }
:color {id="color", label="Color 1:", color = settings.color}
:slider {id="lineSize", label="Line size:", min=1, max=10, value=settings.lineSize}
:separator{ text="Second grid:" }
:check { id="secondGrid", label="Enabled", selected=true, onclick=function()
dlg:modify{id="nthLine", visible=dlg.data.secondGrid, enabled=dlg.data.secondGrid}
dlg:modify{id="color2", visible=dlg.data.secondGrid, enabled=dlg.data.secondGrid}
dlg:modify{id="lineSize2", visible=dlg.data.secondGrid, enabled=dlg.data.secondGrid}
end
}
:number {id="nthLine", label="Every nth line:", decimals=0, text=""..settings.nthLine}
:color {id="color2", label="Color:", color = settings.color2}
:slider {id="lineSize2", label="Line size:", min=1, max=10, value=settings.lineSize2}
:separator{ text="Layer:" }
:slider {id="opacity", label="Opacity:", min=0, max=255, value=settings.opacity}
:separator()
:button {id="ok", text="Add Grid",onclick=function()
local data = dlg.data
app.transaction(function()
local prevFrame = app.activeFrame
app.activeFrame = 1
local layer = createLayer(app.activeSprite, "Grid "..data.xSize.."x"..data.ySize, data.opacity)
data.width = app.activeSprite.width
data.height = app.activeSprite.height
drawGridWithLines(data)
app.activeFrame = prevFrame
end)
dlg:close()
app.refresh()
end
}
:show{wait=false}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment