Skip to content

Instantly share code, notes, and snippets.

@Dwiniflin Dwiniflin/Sprite Creator
Last active Aug 29, 2015

Embed
What would you like to do?
--# Main
-- Main
-- Sprite Creator
--[[
Check these before running
------------------------------
First line of setup() -- gridSize = pixel dimensions for image
Second line of setup() -- spriteScale = how big the image scales when saved to Dropbox
Third line of setup() -- backupMax = number of undos (100 for 16 pixels, 10 for 32 pixels)
Fourth line of draw() -- sprite("Dropbox:Grid") -- you can use the app to draw a transparency background and save it to Dropbox
First line of upload() -- imageUpload = readImage("Dropbox:name of file you want to upload")
Make sure to slide down the output box so you can see more of your toolbox!
------------------------------
]]
supportedOrientations(LANDSCAPE_RIGHT)
displayMode(STANDARD)
-- Use this function to perform your initial setup
function setup()
gridSize = 16
spriteScale = 16
backupMax = 100
-- 16px:100 undos, 32px:10 undos, 64px:1 undo (maybe)
gridWidth = WIDTH/gridSize
gridHeight = HEIGHT/gridSize
spriteDraw = {}
-- create a 2D table to hold sprite pixel data
for i = 1, gridSize do
spriteDraw[i] = {}
for j = 1, gridSize do
spriteDraw[i][j] = color(0,0,0,0) -- set each pixel color to nil
end
end
spriteImage = image(gridSize * spriteScale, gridSize * spriteScale)
setUpParameters()
touches = {}
backups = {}
spriteImage = image(gridSize * spriteScale, gridSize * spriteScale)
setContext(spriteImage)
noSmooth()
for i = 1, gridSize do
for j = 1, gridSize do
fill(spriteDraw[i][j])
rect((i - 1) * spriteScale, (j - 1) * spriteScale, spriteScale, spriteScale)
end
end
setContext()
backups[1] = spriteImage
-- backupMax = 100
backupId = 1
end
function draw()
fps = 1/DeltaTime
background(40, 40, 50)
sprite("Dropbox:Grid", WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
for i = 1, gridSize do
for j = 1, gridSize do
pushStyle()
fill(spriteDraw[i][j]) -- fill color for each pixel
rect((i - 1) * gridWidth, (j - 1) * gridHeight, gridWidth + 1.3, gridHeight + 1.3)
pushStyle()
end
end
pushStyle()
stroke(75, 75, 75, 255)
strokeWidth(3)
if lineStart then line(lineStart.x*gridWidth-gridWidth/2, lineStart.y*gridHeight-gridHeight/2,
lineEnd.x*gridWidth-gridWidth/2, lineEnd.y*gridHeight-gridHeight/2) end
if rectStart then
r1 = vec2(rectStart.x*gridWidth-gridWidth/2, rectStart.y*gridHeight-gridHeight/2)
r2 = vec2(rectStart.x*gridWidth-gridWidth/2, rectEnd.y*gridHeight-gridHeight/2)
r3 = vec2(rectEnd.x*gridWidth-gridWidth/2, rectEnd.y*gridHeight-gridHeight/2)
r4 = vec2(rectEnd.x*gridWidth-gridWidth/2, rectStart.y*gridHeight-gridHeight/2)
line(r1.x,r1.y,r2.x,r2.y); line(r2.x,r2.y,r3.x,r3.y)
line(r3.x,r3.y,r4.x,r4.y); line(r4.x,r4.y,r1.x,r1.y)
end
popStyle()
if Grid == true then drawGrid() end
end
function touched(touch)
if FreeDraw then freeDraw(touch) end
if Line then lineDraw(touch) end
if Rectangle then rectDraw(touch) end
if PaintBucket then paintBucket(touch) end
if ColorFill then colorFill(touch) end
if Eraser then eraser(touch) end
if EyeDropper then eyeDropper(touch) end
end
--# Parameters
-- Parameters
function setUpParameters()
parameter.text("SpriteName")
parameter.color("Color", color(0,0,255,255))
parameter.boolean("FreeDraw", true, function() if FreeDraw == true then
Line,Rectangle,PaintBucket,ColorFill,Eraser,EyeDropper = false,false,false,false,false,false end end)
parameter.boolean("Line", false, function() if Line == true then
FreeDraw,Rectangle,PaintBucket,ColorFill,Eraser,EyeDropper = false,false,false,false,false,false end end)
parameter.boolean("Rectangle", false, function() if Rectangle == true then
FreeDraw,Line,PaintBucket,ColorFill,Eraser,EyeDropper = false,false,false,false,false,false end end)
-- parameter.boolean("PaintBucket", false, function() if PaintBucket == true then
-- FreeDraw,Line,Rectangle,ColorFill,Eraser,EyeDropper = false,false,false,false,false,false end end)
PaintBucket = false
parameter.boolean("ColorFill", false, function() if ColorFill == true then
FreeDraw,Line,Rectangle,PaintBucket,Eraser,EyeDropper = false,false,false,false,false,false end end)
parameter.boolean("Eraser", false, function() if Eraser == true then
FreeDraw,Line,Rectangle,PaintBucket,ColorFill,EyeDropper = false,false,false,false,false,false end end)
parameter.boolean("EyeDropper", false, function() if EyeDropper == true then
FreeDraw,Line,Rectangle,PaintBucket,ColorFill,Eraser = false,false,false,false,false,false end end)
parameter.boolean("Grid", false)
parameter.action("Undo", undo)
parameter.action("Redo", redo)
parameter.action("Save", save)
parameter.action("Upload", upload)
parameter.action("Invert",invert)
parameter.watch("fps")
end
--# Actions
-- Actions
function freeDraw(touch)
for i = 1, gridSize do
for j = 1, gridSize do
if touch.x > (i - 1) * gridWidth and touch.x < i * gridWidth and
touch.y > (j - 1) * gridHeight and touch.y < j * gridHeight then
spriteDraw[i][j] = Color
end
end
end
if touch.state == ENDED then backup() end
end
function paintBucket(touch)
for i = 1, gridSize do
for j = 1, gridSize do
if touch.x > (i - 1) * gridWidth and touch.x < i * gridWidth and
touch.y > (j - 1) * gridHeight and touch.y < j * gridHeight then
local clr = spriteDraw[i][j]
paintFill(i,j,clr)
end
end
end
if touch.state == ENDED then backup() end
end
function paintFill(i,j,clr)
if spriteDraw[i][j] == clr then
spriteDraw[i][j] = Color
if i < gridSize then paintFill(i + 1, j, clr) end
if j < gridSize then paintFill(i, j + 1, clr) end
if i > 1 then paintFill(i - 1, j, clr) end
if j > 1 then paintFill(i, j - 1, clr) end
end
end
function colorFill(touch)
for i = 1, gridSize do
for j = 1, gridSize do
if touch.x > (i - 1) * gridWidth and touch.x < i * gridWidth and
touch.y > (j - 1) * gridHeight and touch.y < j * gridHeight then
if touch.state == BEGAN then clr = spriteDraw[i][j] end
end
if spriteDraw[i][j] == clr then spriteDraw[i][j] = Color end
end
end
if touch.state == ENDED then backup() end
end
function eraser(touch)
for i = 1, gridSize do
for j = 1, gridSize do
if touch.x > (i - 1) * gridWidth and touch.x < i * gridWidth and
touch.y > (j - 1) * gridHeight and touch.y < j * gridHeight then
spriteDraw[i][j] = color(0,0,0,0)
end
end
end
if touch.state == ENDED then backup() end
end
function eyeDropper(touch)
for i = 1, gridSize do
for j = 1, gridSize do
if touch.x > (i - 1) * gridWidth and touch.x < i * gridWidth and
touch.y > (j - 1) * gridHeight and touch.y < j * gridHeight then
Color = spriteDraw[i][j]
end
end
end
end
function drawGrid()
for i = 0, gridSize do
stroke(127, 127, 127, 255)
strokeWidth(2)
line(i * WIDTH/gridSize, 0, i * WIDTH/gridSize, HEIGHT)
line(0, i * HEIGHT/gridSize, WIDTH, i * HEIGHT/gridSize)
end
end
function invert()
local spriteImage = image(gridSize * spriteScale, gridSize * spriteScale)
setContext(spriteImage)
noSmooth()
for i = 1, gridSize do
for j = 1, gridSize do
fill(spriteDraw[i][j])
rect((i - 1) * spriteScale, (j - 1) * spriteScale, spriteScale, spriteScale)
end
end
setContext()
for i = 1, gridSize do
for j = 1, gridSize do
r,g,b,a = spriteImage:get(i * spriteScale - spriteScale/2,
j * spriteScale - spriteScale/2)
spriteDraw[i][j] = color(255-r,255-g,255-b,a)
end
end
-- backup()
end
--# Line
-- Line
function lineDraw(touch)
for i = 1, gridSize do
for j = 1, gridSize do
if touch.x > (i - 1) * gridWidth and touch.x < i * gridWidth and
touch.y > (j - 1) * gridHeight and touch.y < j * gridHeight then
if touch.state == BEGAN then
lineStart = vec2(i,j)
lineEnd = vec2(i,j)
end
if touch.state == MOVING then lineEnd = vec2(i,j) end
if touch.state == ENDED then
local slope = (lineEnd.y - lineStart.y)/(lineEnd.x - lineStart.x)
if slope > 1 or slope < -1 then
if lineStart.y < lineEnd.y then lineUp(1/slope) end
if lineStart.y > lineEnd.y then lineDown(1/slope) end
else
if lineStart.x > lineEnd.x then lineLeft(slope) end
if lineStart.x < lineEnd.x then lineRight(slope) end
end
if lineStart == lineEnd then spriteDraw[i][j] = Color end
lineStart = vec2()
lineEnd = vec2()
end
end
end
end
if touch.state == ENDED then backup() end
end
function lineUp(slope)
for j = lineStart.y, lineEnd.y do
local y = slope * (j - lineStart.y) + lineStart.x - 1/2 -- y = m(x-x1)+y1
for i = 1, gridSize do
if y < i + 0.1 and y > (i - 1.1) then spriteDraw[i][j] = Color end
-- without the added 0.1 some boxes get skipped
end
end
end
function lineDown(slope)
for j = lineEnd.y, lineStart.y do
local y = slope * (j - lineEnd.y) + lineEnd.x - 1/2
for i = 1, gridSize do
if y < i + 0.1 and y > (i - 1.1) then spriteDraw[i][j] = Color end
end
end
end
function lineLeft(slope)
for i = lineEnd.x, lineStart.x do
local y = slope * (i - lineEnd.x) + lineEnd.y - 1/2
for j = 1, gridSize do
if y < j + 0.1 and y > (j - 1.1) then spriteDraw[i][j] = Color end
end
end
end
function lineRight(slope)
for i = lineStart.x, lineEnd.x do
local y = slope * (i - lineStart.x) + lineStart.y - 1/2
for j = 1, gridSize do
if y < j + 0.1 and y > (j - 1.1) then
spriteDraw[i][j] = Color
end
end
end
end
--# Rectangle
-- Rectangle
function rectDraw(touch)
for i = 1, gridSize do
for j = 1, gridSize do
if touch.x > (i - 1) * gridWidth and touch.x < i * gridWidth and
touch.y > (j - 1) * gridHeight and touch.y < j * gridHeight then
if touch.state == BEGAN then
rectStart = vec2(i,j)
rectEnd = vec2(i,j)
end
if touch.state == MOVING then rectEnd = vec2(i,j) end
if touch.state == ENDED then
local x,y
if rectStart.x > rectEnd.x then x = -1 else x = 1 end
if rectStart.y > rectEnd.y then y = -1 else y = 1 end
rectFill(x,y)
rectStart = vec2()
rectEnd = vec2()
end
end
end
end
if touch.state == ENDED then backup() end
end
function rectFill(x,y)
for i = rectStart.x, rectEnd.x, x do
for j = 1, gridSize do
spriteDraw[i][rectStart.y] = Color
spriteDraw[i][rectEnd.y] = Color
end
end
for i = 1, gridSize do
for j = rectStart.y, rectEnd.y, y do
spriteDraw[rectStart.x][j] = Color
spriteDraw[rectEnd.x][j] = Color
end
end
end
--# Undo
-- Undo
function backup()
spriteImage = image(gridSize * spriteScale, gridSize * spriteScale)
setContext(spriteImage)
noSmooth()
for i = 1, gridSize do
for j = 1, gridSize do
fill(spriteDraw[i][j])
rect((i - 1) * spriteScale, (j - 1) * spriteScale, spriteScale, spriteScale)
end
end
setContext()
if #backups > backupMax then
for i = 1, backupMax do
backups[i] = backups[i+1]
end
end
backups[backupMax+1] = nil
backups[backupId+1] = spriteImage
print(#backups,backupId)
backupId = backupId + 1
if backupId < #backups then
for i = backupId+1, #backups do
backups[i] = nil
end
end
if backupId > backupMax then backupId = backupMax end
end
function undo()
if backupId > 1 then
local imageUpload = backups[backupId-1]
for i = 1, gridSize do
for j = 1, gridSize do
r,g,b,a = imageUpload:get(i * spriteScale - spriteScale/2,
j * spriteScale - spriteScale/2)
spriteDraw[i][j] = color(r,g,b,a)
end
end
backupId = backupId - 1
end
print(#backups,backupId)
end
function redo()
if backupId < #backups then
local imageUpload = backups[backupId+1]
for i = 1, gridSize do
for j = 1, gridSize do
r,g,b,a = imageUpload:get(i * spriteScale - spriteScale/2,
j * spriteScale - spriteScale/2)
spriteDraw[i][j] = color(r,g,b,a)
end
end
backupId = backupId + 1
end
print(#backups,backupId)
end
--# Save
-- Save
function save()
spriteImage = image(gridSize * spriteScale, gridSize * spriteScale)
setContext(spriteImage)
noSmooth()
for i = 1, gridSize do
for j = 1, gridSize do
fill(spriteDraw[i][j])
rect((i - 1) * spriteScale, (j - 1) * spriteScale, spriteScale, spriteScale)
end
end
setContext()
saveImage(tostring("Dropbox:"..SpriteName),spriteImage)
end
--# Upload
-- Upload
function upload()
imageUpload = readImage("Dropbox:")
for i = 1, gridSize do
for j = 1, gridSize do
r,g,b,a = imageUpload:get(i * spriteScale - spriteScale/2,
j * spriteScale - spriteScale/2)
spriteDraw[i][j] = color(r,g,b,a)
end
end
backup()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.