Skip to content

Instantly share code, notes, and snippets.

@JMV38
Created September 15, 2013 17:46
Show Gist options
  • Save JMV38/6572916 to your computer and use it in GitHub Desktop.
Save JMV38/6572916 to your computer and use it in GitHub Desktop.
Codea Project Gist Created with AutoGist
AutoGist Tab Order Version: 2.2.8
------------------------------
This file should not be included in the Codea project.
#Img2str
#Icons
#Settings
#DropList
#Pinch
#Switch
#Slider
#Grid
#Main
#ImageCode
DropList = class()
function DropList:init(name, x, y, w, h, tab, callback, MaxList)
self.x = x
self.y = y
self.w = w
self.h = h
self.tab = tab
self.name = name
self.selected = false
self.selection = self.tab[1]
self.callback = callback
self.MaxList = MaxList or 10
self.dy = 0
end
function DropList:OpenDraw()
for k,v in pairs(self.tab) do
if k + self.dy <= self.MaxList and k + self.dy > 0 then
pushStyle()
if v == self.selection then
fill(0, 169, 255, 255)
else
fill(255, 255, 255, 255)
end
fontSize(18)
font("AmericanTypewriter")
textMode(CORNER)
text(v, self.x, self.y - self.h*(k + self.dy) - 5*(k + self.dy))
stroke(255, 255, 255, 255)
strokeWidth(1)
line(self.x, self.y - (k + self.dy)*self.h - 5*(k + self.dy) - 5,
self.x + self.w, self.y - (k + self.dy)*self.h - 5*(k + self.dy) - 5)
popStyle()
elseif k + self.dy == self.MaxList + 1 then
for i = 1,3 do
pushStyle()
fill(255, 255, 255, 255)
ellipse(self.x + i*self.w/3 - 10, self.y - self.h*(k + self.dy), 5)
popStyle()
end
end
end
pushStyle()
fill(255, 255, 255, 255)
fontSize(18)
textMode(CORNER)
text(self.name, self.x, self.y)
popStyle()
end
function DropList:ClosedDraw()
pushStyle()
fill(255, 255, 255, 255)
fontSize(18)
textMode(CORNER)
text(self.name, self.x, self.y)
popStyle()
end
function DropList:draw()
if self.selected then
self:OpenDraw()
elseif not self.selected then
self:ClosedDraw()
end
end
function DropList:touched(touch)
if not self.selected then
if touch.x > self.x and touch.x < self.x + self.w and touch.y > self.y and touch.y < self.y + self.h
and touch.state == BEGAN then
self.selected = true
end
end
if self.selected then
for k,v in pairs(self.tab) do
if k + self.dy <= self.MaxList and k + self.dy > 0 then
if touch.x > self.x and touch.x < self.x + self.w and
touch.y > self.y - self.h*(k + self.dy) - 5*(k + self.dy) and
touch.y < self.y - self.h*(k + self.dy) - 5*(k + self.dy) + self.h then
self.selection = v
end
end
end
if touch.x > self.x and touch.x < self.x + self.w and
touch.y > self.y and touch.y < self.y + self.h then
if touch.state == BEGAN then
if #self.tab > self.MaxList then
self.scroll = true
self.timer = ElapsedTime
end
end
end
if self.scroll and touch.y < self.y - self.h*self.MaxList then
if ElapsedTime - 1/5 > self.timer then
self.dy = math.min(math.max(self.dy - 1, -#self.tab + self.MaxList), 0)
self.timer = ElapsedTime
end
elseif self.scroll and touch.y > self.y then
if ElapsedTime - 1/5 > self.timer then
self.dy = math.min(math.max(self.dy + 1, -#self.tab + self.MaxList), 0)
self.timer = ElapsedTime
end
end
if touch.state == ENDED then
if touch.x > self.x and touch.x < self.x + self.w and
touch.y > self.y - self.h*math.min(#self.tab + 2, self.MaxList + 2) and touch.y < self.y then
self.callback()
sound(SOUND_BLIT, 21884)
end
end
end
if touch.state == ENDED then
self.scroll = false
self.selected = false
end
end
Grid = class()
function Grid:init(dimension)
ShowGrid = true
self.W = 749/(retina + 1)
self.H = 749/(retina + 1)
self.dimension = dimension
if self.dimension.x > self.dimension.y then
self.H = self.H*self.dimension.y/self.dimension.x
elseif self.dimension.y > self.dimension.x then
self.W = self.W*self.dimension.x/self.dimension.y
end
self.width = self.W/self.dimension.x
self.height = self.H/self.dimension.y
self.StartX = WIDTH/2 - 749/2 + 137
self.grids = image(self.W, self.H)
self.grid = image(self.W, self.H)
local floor = math.floor
local mod = math.fmod
local x,y
setContext(self.grids)
for i = 0,self.dimension.x do
pushStyle()
if mod(i,self.dimension.x) == 0 then
strokeWidth(3)
else
strokeWidth(1)
end
if mod(i,8) == 0 then
stroke(127, 0, 0, 255)
strokeWidth(3)
else
stroke(127, 127, 127, 255)
end
noSmooth()
x = floor(self.W * i / self.dimension.x)
line( x, 0, x, self.H)
popStyle()
end
for j = 0,self.dimension.y do
pushStyle()
if mod(j,self.dimension.y) == 0 then
strokeWidth(3)
else
strokeWidth(1)
end
if mod(j,8) == 0 then
stroke(127, 0, 0, 255)
strokeWidth(3)
else
stroke(127, 127, 127, 255)
end
noSmooth()
line(0, self.height*j, self.W, self.height*j)
popStyle()
end
setContext()
self.Image = image(self.dimension.x, self.dimension.y)
self.scl = 1
self.deltaX, self.deltaY = 0, 0
self.floodChecks = {}
self.posn = {}
self.p = pinch()
self.Move = {}
end
function Grid:draw()
self.p:processTouches()
self.scl = self.p.zoom
self.deltaX = math.max(math.min(self.deltaX, 0), self.W - self.scl*self.W)
self.deltaY = math.max(math.min(self.deltaY, 0), self.H - self.scl*self.H)
pushStyle() pushMatrix()
noSmooth()
translate(self.StartX + self.deltaX, self.deltaY)
scale(self.scl)
spriteMode(CORNER)
if Mode == "Rect" then
sprite(self.grid, 0, 0, self.W, self.H)
else
sprite(self.Image, 0, 0, self.W, self.H)
end
if ShowGrid then
sprite(self.grids, 0, 0)
end
popStyle() popMatrix()
fill(0, 0, 0, 255)
rect(-10, -10, self.StartX + 10, HEIGHT + 20)
if #self.floodChecks ~= 0 then
for k,v in pairs(self.floodChecks) do
table.remove(self.floodChecks, k)
if self.Filling == "Flood" then
self:Flood(v.x, v.y)
elseif self.Filling == "Crop" then
self:Crop(v.x, v.y, color(0, 0, 0, 0))
end
end
end
end
function Grid:touched(touch)
local tx = touch.x
local ty = touch.y
if #touches > 0 and touch.x > self.StartX then
if tool == "Pencil" or tool == "Eraser" then
if #touches == 1 then
if tx > self.StartX then
local tx = touches[1].x - self.StartX
local ty = touches[1].y
self.x = math.floor(1 + tx/self.width/self.scl - self.deltaX/self.width/self.scl)
self.y = math.floor(1 + ty/self.height/self.scl - self.deltaY/self.height/self.scl)
if touches[1].state == MOVING or touches[1].state == BEGAN then
if self.X ~= self.x or self.Y ~= self.y then
if tool == "Pencil" then
self:Pixelate(self.x, self.y)
elseif tool == "Eraser" then
self:Depixelate(self.x, self.y)
end
self.X = self.x
self.Y = self.y
end
end
end
self.PosnI = vec2(touches[1].x, touches[1].y)
self.dist = nil
end
elseif tool == "Fill" then
if touch.state == BEGAN then
local tx = touches[1].x - self.StartX
local ty = touches[1].y
self.x = math.floor(1 + tx/self.width/self.scl - self.deltaX/self.width/self.scl)
self.y = math.floor(1 + ty/self.height/self.scl - self.deltaY/self.height/self.scl)
if self.x > 0 and self.y > 0 and self.x <= self.Image.width and self.y <= self.Image.height then
self.r,self.g,self.b,self.a = self.Image:get(self.x, self.y)
if self.r ~= Red.value or self.b ~= Blue.value
or self.g ~= Green.value or self.a ~= Alpha.value then
self:Pixelate(self.x, self.y)
self:Flood(self.x, self.y)
end
end
end
elseif tool == "Picker" then
if touch.state == BEGAN then
local tx = touches[1].x - self.StartX
local ty = touches[1].y
self.x = math.floor(1 + tx/self.width/self.scl - self.deltaX/self.width/self.scl)
self.y = math.floor(1 + ty/self.height/self.scl - self.deltaY/self.height/self.scl)
if self.x > 0 and self.y > 0 then
self.r,self.g,self.b,self.a = self.Image:get(self.x, self.y)
Red.value = self.r
Green.value = self.g
Blue.value = self.b
Alpha.value = self.a
colour = color(self.r, self.g, self.b, self.a)
end
end
elseif tool == "Rect" or tool == "Circle" or tool == "Line" then
local tx = touches[1].x - self.StartX
local ty = touches[1].y
self.x = math.floor(1 + tx/self.width/self.scl - self.deltaX/self.width/self.scl)
self.y = math.floor(1 + ty/self.height/self.scl - self.deltaY/self.height/self.scl)
if self.R1 == nil then
self.R1 = vec2(self.x, self.y)
end
self.R2 = vec2(self.x, self.y)
elseif tool == "Crop Out" and #touches == 1 and touch.x > self.StartX then
local tx = touches[1].x - self.StartX
local ty = touches[1].y
self.x = math.floor(1 + tx/self.width/self.scl - self.deltaX/self.width/self.scl)
self.y = math.floor(1 + ty/self.height/self.scl - self.deltaY/self.height/self.scl)
if self.X ~= self.x or self.Y ~= self.y then
self:Depixelate(self.x, self.y)
end
if self.X == nil then
self.X = self.x
end
if self.Y == nil then
self.Y = self.y
end
if self.X ~= self.x - 1 and self.X ~= self.x + 1 and self.X ~= self.x then
if self.Y == self.y then
for i = math.min(self.x, self.X), math.max(self.x, self.X) do
self:Depixelate(i, self.y)
end
elseif self.Y == self.y - 1 then
for i = math.min(self.x, self.X), math.max(self.x, self.X) do
self:Depixelate(i, self.y)
end
elseif self.Y == self.y + 1 then
for i = math.min(self.x, self.X), math.max(self.x, self.X) do
self:Depixelate(i, self.y)
end
end
end
if self.Y ~= self.y and self.Y ~= self.y - 1 and self.Y ~= self.y + 1 then
if self.X == self.x then
for j = math.min(self.y, self.Y), math.max(self.y, self.Y) do
self:Depixelate(self.x, j)
end
elseif self.Y == self.y - 1 then
for j = math.min(self.y, self.Y), math.max(self.y, self.Y) do
self:Depixelate(self.x, j)
end
elseif self.Y == self.y + 1 then
for j = math.min(self.y, self.Y), math.max(self.y, self.Y) do
self:Depixelate(self.x, j)
end
end
end
self.X = self.x
self.Y = self.y
end
if touch.state == ENDED then
if tool == "Rect" then
if self.R1 then
for i = math.min(self.R1.x, self.R2.x), math.max(self.R1.x, self.R2.x) do
self:Pixelate(i, self.R1.y)
self:Pixelate(i, self.R2.y)
end
for j = math.min(self.R1.y, self.R2.y), math.max(self.R1.y, self.R2.y) do
self:Pixelate(self.R1.x, j)
self:Pixelate(self.R2.x, j)
end
end
elseif tool == "Circle" then
if self.R1 then
local x1 = self.R1.x
local x2 = self.R2.x
local y1 = self.R1.y
local y2 = self.R2.y
local radius = ((x2 - x1)^2 + (y2 - y1)^2)^(1/2)
if self.dimension.x > 150 or self.dimension.y > 150 then
self.Limit = 1080
self.Div = 3
else
self.Limit = 720
self.Div = 2
end
for i = 0,self.Limit do
local xp = math.floor(x1 + radius*math.cos(math.rad(i/self.Div)))
local yp = math.floor(y1 + radius*math.sin(math.rad(i/self.Div)))
self:Pixelate(xp, yp)
end
end
elseif tool == "Line" then
if self.R1 then
local x1 = self.R1.x
local x2 = self.R2.x
local y1 = self.R1.y
local y2 = self.R2.y
local distance = math.ceil(((x2 - x1)^2 + (y2 - y1)^2)^(1/2))
local theta = math.atan2((y2 - y1),(x2 - x1))
for i = 1,distance do
local xp = math.ceil(x1 + i*math.cos(theta))
local yp = math.ceil(y1 + i*math.sin(theta))
self:Pixelate(xp, yp)
end
end
elseif tool == "Crop" then
local tx = touches[1].x - self.StartX
local ty = touches[1].y
self.x = math.floor(1 + tx/self.width/self.scl - self.deltaX/self.width/self.scl)
self.y = math.floor(1 + ty/self.height/self.scl - self.deltaY/self.height/self.scl)
self:Crop(self.x, self.y, color(0, 0, 0, 0))
end
self.R1 = nil
self.X = nil
self.Y = nil
end
end
if #touches == 2 then
--translate
if touches[2].x > self.StartX and touches[1].x < self.StartX then
self.deltaX = math.max(math.min(self.deltaX + touches[2].deltaX, 0), self.W - self.scl*self.W)
self.deltaY = math.max(math.min(self.deltaY + touches[2].deltaY, 0), self.H - self.scl*self.H)
end
--zoom
self.p:touched(touch)
end
end
function Grid:Pixelate(a, b, Undoing)
if Mode == "Rect" then
if a >= 1 and b >= 1 and a <= self.Image.width and b <= self.Image.height then
self:Depixelate(a, b, Undoing)
setContext(self.grid)
pushStyle()
rectMode(CORNER)
fill(colour)
rect(a*self.width - self.width, b*self.height - self.height,
self.width, self.height)
popStyle()
setContext()
self.Image:set(a, b, colour)
end
else
if a >= 1 and b >= 1 and a <= self.Image.width and b <= self.Image.height then
if not Undoing then
local ri, gi, bi, ai = self.Image:get(a, b)
if ri ~= colour.r or gi ~=colour.g or bi~= colour.b or ai ~= colour.a then
local coli = color(ri, gi, bi, ai)
table.insert(self.Move, {x = a, y = b, c = coli})
end
end
self.Image:set(a, b, colour)
end
end
end
function Grid:Depixelate(a, b, Undoing)
if not Undoing then
if a >= 1 and b >= 1 and a <= self.Image.width and b <= self.Image.height then
local ri, gi, bi, ai = self.Image:get(a, b)
if ri ~= colour.r or gi ~=colour.g or bi~= colour.b or ai ~= colour.a or
tool == "Eraser" or tool == "Crop Out" then
local coli = color(ri, gi, bi, ai)
table.insert(self.Move, {x = a, y = b, c = coli})
end
end
end
if Mode == "Rect" then
if a >= 1 and b >= 1 and a <= self.Image.width and b <= self.Image.height then
setContext(self.grid)
pushStyle()
fill(BackGround)
rectMode(CORNER)
rect(a*self.width - self.width - .5, b*self.height - self.height - .5,
self.width + 1, self.height + 1)
popStyle()
setContext()
self.Image:set(a, b, color(0, 0, 0, 0))
end
else
if a >= 1 and b >= 1 and a <= self.Image.width and b <= self.Image.height then
self.Image:set(a, b, color(0, 0, 0, 0))
end
end
end
function Grid:Flood(a, b)
self.Filling = "Flood"
if a + 1 <= self.Image.width and b <= self.Image.height and a + 1 >= 1 and b >= 1 then
self.rc, self.gc, self.bc, self.ac = self.Image:get(a + 1, b)
if self.rc == self.r and self.gc == self.g and self.bc == self.b and self.ac == self.a then
self:Pixelate(a + 1, b)
table.insert(self.floodChecks, 1, vec2(a + 1, b))
end
end
if a - 1 <= self.Image.width and b <= self.Image.height and a - 1 >= 1 and b >= 1 then
self.rc, self.gc, self.bc, self.ac = self.Image:get(a - 1, b)
if self.rc == self.r and self.gc == self.g and self.bc == self.b and self.ac == self.a then
self:Pixelate(a - 1, b)
table.insert(self.floodChecks, 1, vec2(a - 1, b))
end
end
if a <= self.Image.width and b + 1 <= self.Image.height and a >= 1 and b + 1 >= 1 then
self.rc, self.gc, self.bc, self.ac = self.Image:get(a, b + 1)
if self.rc == self.r and self.gc == self.g and self.bc == self.b and self.ac == self.a then
self:Pixelate(a, b + 1)
table.insert(self.floodChecks, 1, vec2(a, b + 1))
end
end
if a <= self.Image.width and b - 1 <= self.Image.height and a >= 1 and b - 1 >= 1 then
self.rc, self.gc, self.bc, self.ac = self.Image:get(a, b - 1)
if self.rc == self.r and self.gc == self.g and self.bc == self.b and self.ac == self.a then
self:Pixelate(a, b - 1)
table.insert(self.floodChecks, 1, vec2(a, b - 1))
end
end
end
function Grid:Crop(a, b, col)
local c = col or color(0, 0, 0, 0)
self.Filling = "Crop"
if a + 1 <= self.Image.width and b <= self.Image.height and a + 1 >= 1 and b >= 1 then
self.rc, self.gc, self.bc, self.ac = self.Image:get(a + 1, b)
if self.rc ~= c.r or self.gc ~= c.g or self.bc ~= c.b or self.ac ~= c.a then
self:Depixelate(a + 1, b)
table.insert(self.floodChecks, 1, vec2(a + 1, b))
end
end
if a - 1 <= self.Image.width and b <= self.Image.height and a - 1 >= 1 and b >= 1 then
self.rc, self.gc, self.bc, self.ac = self.Image:get(a - 1, b)
if self.rc ~= c.r or self.gc ~= c.g or self.bc ~= c.b or self.ac ~= c.a then
self:Depixelate(a - 1, b)
table.insert(self.floodChecks, 1, vec2(a - 1, b))
end
end
if a <= self.Image.width and b + 1 <= self.Image.height and a >= 1 and b + 1 >= 1 then
self.rc, self.gc, self.bc, self.ac = self.Image:get(a, b + 1)
if self.rc ~= c.r or self.gc ~= c.g or self.bc ~= c.b or self.ac ~= c.a then
self:Depixelate(a, b + 1)
table.insert(self.floodChecks, 1, vec2(a, b + 1))
end
end
if a <= self.Image.width and b - 1 <= self.Image.height and a >= 1 and b - 1 >= 1 then
self.rc, self.gc, self.bc, self.ac = self.Image:get(a, b - 1)
if self.rc ~= c.r or self.gc ~= c.g or self.bc ~= c.b or self.ac ~= c.a then
self:Depixelate(a, b - 1)
table.insert(self.floodChecks, 1, vec2(a, b - 1))
end
end
end
icons = {}
icons['move'] = Decoder:Decode(32,32,
[[
>->->->-)))>-7
]]
,
[[
75kkjkukmjkskojk-@tjkI@0jk7@I77@R77@I78I@I7@@R7@@I7<kmjktkmjk)@77skmjq
7@771kkjq7@77@@77skkjqI@I78@@I7skljk)@I7-@I7ukmjqI@I7I@0jkkojkkj)77@0j
lkkjkkkjlkk)7I@@7@@@7I@0jjkj)7@@g7@@0jlkljqI@R7ukljnkljk)@I7skljjtkljq
I@@7skjjkkjjq7@@71kjjq7@R7skjjk)@R7A@@R7<kljkkmjkkljjukljjkmjjkljk7@0j
kI@tjk-@g7A7@R7AI@@5
]]
)
icons['rotate'] = Decoder:Decode(32,32,
[[
>->->->-)))>-7
]]
,
[[
7A:kjjl)@I7A@@-7?kq77=kqI7tkjjq7@R7@@1jokljnkojjkljjkljjkljokljnkkjkkk
jnkljj)@I7-@@7@@@7-@I70kljnkojnkljj)@I7g@R7g@I70kljj-@R71kljjR@I72kmjj
@@R73kmjj)@R75kmjq7@-76kj-78I@1jkI@tjs@
]]
)
icons['zoom'] = Decoder:Decode(32,32,
[[
>->->->-)))>-7
]]
,
[[
7Ekl)7@@Bjkkl)7@@I7skljjR@I7@@I7skljjR@I7@@I7skljjR@I7@@I7skljjR@I7@@I
7skljjR@I7@@I7skljjR@I7@@I7skljjR@I7@@I7skljjR@I7@@I7skljjR@I7@@3jjR@I
7@@3jjR@I7@@3jjR@I7@@I7=kljkkljj7@g7skljkkljj7@g7skljkkljj7@R7ukljkklj
j7@-7tkljkkljj7@@77@I7skljkkljj7@@7@@I7@@@7@@I7@@I76kljjkkjkkljkkljjsk
njkkljkkljjtkmjkkljkkljjg@g7@@I7@@I76kojkkljkkl)7@@Bjkkl)E
]]
)
icons['undo'] = Decoder:Decode(32,32,
[[
>->->->-)))>-7
]]
,
[[
7tg@77Bkljktknjkg@sjkR@ujk@@1jk)@3jqI@sjkkj-7tkq77@@5jq@@sjkkj-7tkq77g
@sjj@@sjokq772kq77g@sjj@@sjokq772kq77g@sjj@@sjokq772kq77g@sjj@@9jj@@9j
j@@9jj@@9jjI@8@75kjg787@4k#
]]
)
icons['color'] = Decoder:Decode(32,32,
[[
>->->->-)))>-)>-)>-))>->->-))>-7
]]
,
[[
71kq@7;kj@78@@RItkmjj-@@I4kkjj@@II6kljqI@II8@@I7tkmljtkkjq7@@R@@@I8@@@
7-@@RR@@I8@@@7R@7Rg@@I8@@77I@@Rskkljskkjkkkmq@@@I6kkjkkjmj)@@I6kkjjkjm
j7@@I5kkjjkjmj@@@I4kkjjkjmjI@@Iskq@77@7R3kjsjjkjmjI@unokkjjkjmjI@I-2kk
jjkjmj@@@-4kkjjkkmj7@@-3kkjkkkmj)@@-4kkjlkjmj)@@-4kjjmkkmq@@I-3kkjnkkm
q7@@-3kkjq7@@R-@I-2kkjq@@IRI@I-2kljqI@IR@@@-2kljj@@--1kkjj-@R-tkmjjtkj
@7;kq@3
]]
)
icons['fill'] = Decoder:Decode(32,32,
[[
>->->->-)))>->-))>-7
]]
,
[[
7hI@g7=kklnkqI75kklj-@@73kjljskkjj@@7I8@@@71kjljukkjj)@7I0kq7II@770kjl
q7@I7R@RI@@770kjlokkjnkkjkkmjj)@7IR@I7-@R7@@I71knjokkjkkkjkkljk)@@7R@@
7@@I78@@@7g@@7@@I76kljq7@@7@@I74kkjj)@@7I@@72kkjj@@@7@@I70kkjjR@@7@@I7
tkkjjg@@77@I7skkjjtkmjq7@I78@@I7ukljjg@I71kljjR@I73kljjI@@75kljj7@@787
@I7ukkjjukljq7@@7:kljnkkjkI@R7@@@7>kojktkljI
]]
)
icons['picker'] = Decoder:Decode(32,32,
[[
>->->->-)))>-7
]]
,
[[
76g@-7A@@-7AI@R7<kjjnkmjk@@@7-@R7:kljnkmjk)@R7-@R79kmjnkmjokmjj)@R7-@R
7I@sjj)@R7-@R77@tjj7@R7-@g7@@R72kmjnkmjmkljjI@R7-@@7-@I74kmjlkkjnkmjj-
@R77@@7-@R787@g7R@g787@R7R@@77@-787@I7I@@7I@-75kljlkljmkmjjR@I7I@I7g@R
73kljkkmjq7@I73kj)7g@I73kj77g@@74knjkkmjmkljj-@I7R@-77@R7<kq@7>knl
]]
)
icons['T'] = Decoder:Decode(32,32,
[[
>->->->-)))>-7
]]
,
[[
7uukkjl)@R7A@@g7?kkjjknjkR@@7R@I7<kkjnkmjk7@@7skmjjukkjqI@R787@@70kmjj
g@@72kmjjR@@74kmjj@@@76kljj7@@78@@I7ukkjjukmjq7@@7:kmjnkkjk7@-7R@@78I@
sjmkkjjskojq7@@74kojj)@@72kojj@@@70knjj-@@7tkojjg@@7-@g78I@@7I@g7:kq@7
=knjkskmjktkjo)
]]
)
icons['S'] = Decoder:Decode(32,32,
[[
>->->->-7))>-7
]]
,
[[
7fkktjmkktjmkktjmknjk)@I7R@g78I@I7R@I77@I78@@I7R@I7@@I787@I7R@I7I@I76k
ljmkljmkljj-@I7R@I7-@I74kljmkljokljjI@I7R@I7skljj@@I7R@I7tkljj7@I7R@I7
ukljj)@I7R@I70kljqI@I7R@I71kljq@@I7R@I72kljq7@I7R@I73kljokljmkljjR@I7-
@I7R@I75kljmkljmkljjg@I7I@I7R@I787@I7@@I7R@I78@@I77@I7R@I78I@g7R@I79kn
jmkktjmkktjmkktog
]]
)
icons['A'] = Decoder:Decode(32,32,
[[
>->->->-)))>->7))>-7
]]
,
[[
7fkktjmkktjmkkjk@@II77R@@7:kllkjmkkjk)@I77I@7R@@78I@I77I@7-@@78@@I7@I@
7-@@787@I7@I@7g@@76kljllkjokkjj-@I7II@7skkjjR@I7III7skkjjI@I7RI@7tkkjj
@@I7RI@7ukkjj7@I7-I@7ukkjj)@I7-I@70kkjqI@I7gI@70kkjq@@I7slkjj)@@7skljq
7I@71kkjokljq7II71kkjnkljq@I@72kkjmkljq@I@73kkjlkljqII@73kkjkkljqII@74
kkjjkljj)I@74knjj)I@75kmjolq775kklj-75kjlqItR
]]
)
icons['above'] = Decoder:Decode(32,32,
[[
>->->->->-))>-)))>-7
]]
,
[[
7Bkjjotlkjl)I-7A7ItjkRI@77Itjk7I@7RItjjtlkjq7IsjjgI@70lq773lkjlkkjq@Is
jj)I@7I@R7tlq77tlkjlknjj)Isjnlkjlkojj@I-7RI@7I@tjq@Ig7-I@7I@ujmlq77slk
jlkj)77Ig70lkjlkj@I@73lkjlkjI74lkjlkjR73lkjlkjg71lnkjsjj)I-@8@7ulljkkk
)7sljjmkk771kk78G
]]
)
icons['below'] = Decoder:Decode(32,32,
[[
>->->->-)))>->-))>-7
]]
,
[[
7uukkjl)@-7A7@tjkR@@77@tjk7@@7R@tjjtkkjq7@sjjg@@70kq773kkjmljjq@@sjj)@
@7RI777I77tkq77tkkjmljjlljjqI@sjnkkjqII771knjmkkjlljjq7I@7g@g7-@@7II77
2kq77skkjjI@sjj)@@7II77skojjI@@7II77R@gI-71kkjokolqI7ukkjlkq7I2jq@@ulj
sjoknlk@7-@I7@I:jokjjmljtjjRI4jjtlj77:lq77>lljktlkqA7
]]
)
icons['save'] = Decoder:Decode(32,32,
[[
>->->->->-))>-)))>-h71->->-8<jt)8?kYj
]]
,
[[
7`kljkI@77-@I7;kkjnkljk@@@7-@I7;kjjnkmjk7@@7-@I7RI8@@@II77@R7RI87@III7
7@I7-I@R5kkmkljkmjolkmq7Isklljmjljkmjolkmq7Iskkljkojq7I@RslknmkqI7tlkm
q7I@-R@tjqII@Rslknlkq7I@7ulkmq7I@-I@slkjqII@Rslknmkq@7ulkmq7I@-skq77sl
kmq7I@-slkmjlkkljolkmq7I@-slkmjlkjkkljmlkmq7I@-slkmjlkjqII@Rslknq7I@R7
I@7ulkmq7I@-slkmjlkjqIIIRgI@-slkmjlkjqIIRR-I1mjlkjj)IRRRI1mjlkjj7IRR5l
kjj@I9jjII8I0I
]]
)
icons['load'] = Decoder:Decode(32,32,
[[
>->->->-)))>-h71->->->-))>-8<jt)8?kYj
]]
,
[[
7;g@<jqI@<jqI@@I8I@@7ukklq7@1ljkkjqI@@IsklmmkmljkkjqI@@Iskknjmq7@7I7@@
7ukklq7@@-7Rtljkkjnmjjlkklq7@@-7R0kjjmmkjlkklq7@@--Rsjmmkjlkklq7@@-skj
mnjlmkjlkklq7@@-skkmnjkmkjlkklq7@@-skkmojjmkjlkklq7@@-skkljkjmq77I@@Is
kknq7@@I7@@Rg7I@@Iskknq7@@I7@@Rg7I@@Iskknq7@@I7@@77R-7I@IIg@@-gRsjjmmj
lkmlnkqIR1jmkmlmkj7I7@@Rg7-@RI5kkjkmmjokk)7-R77skjuj~
]]
)
icons['save'] = Decoder:Decode(32,32,
[[
>->->->-)))>->-))>-7
]]
,
[[
7Ekl)7@@Bjkkl)7@@Bjkkk@I7@sjkkkjj@@slkkq77@@@72kollkq77@@@72knlmkq77@@
@72kmlmkq@7@@@72kllmkqI7@@@72kkljR7@@@72kjlj-7@@@72ljg7@@@71ljsjkkkjj)
I8@7@@@77@uljtjkkkjjkj)I877@@@77@0jjljg7@@@77@0jjkjlj-7@@@72kkljR7@@@7
2kllmkqI7@@@72kmlmkq@7@@8IIR@sjkkk)II@sjkkk7I@@sjlkk7I7@sjmkktjnkksjok
kg7skk-E
]]
)
icons['floppy'] = Decoder:Decode(32,32,
[[
>->->->-)))>-7
]]
,
[[
7Ekl)7@@Bjkkl)7@@Bjkkl)7@@@72kjg7@@@72kjg7@@@72kjg7@@@72kjg7@@@72kjg7@
@@72kjg7@@@72kjg7@@@72kjg7@@@72kjg7@@@72kjg7@@@77@0jjkjg7@@@77@0jjkjg7
@@@77@0jjkjg7@@@77@0jjkjg7@@@72kjg7@@@72kjg7@@@72kjg7@@Bjkkl)7@@Bjlkku
jmkktjnkksjokkg7skk-E
]]
)
icons['load'] = Decoder:Decode(32,32,
[[
>->->->-)))>->-))>-7
]]
,
[[
7Ekl)7@@Bjkkl)7@@Bjkkk7I7@tjkkkjj@@slkkq77@@@72kq7II@g7@@@72kq7IR@-7@@
@72kq@IR@R7@@@72kqIIR@I7@@@70ljg@@7@@@70ljskjjkkkjj)I8@7@@@70ljujjkkjj
)I8I77@@77@uljtjkkkjjkqII87@77@@@77@uljg@@7@@@77@0jjkqIIR@I7@@@72kq@IR
@R7@@@72kq7IR@-7@@@72kq7II@g7@@:lkkq77@@:ljkq@7@@Bjlkkujmkktjnkksjokkg
7skk-E
]]
)
icons['mesh'] = Decoder:Decode(32,32,
[[
>->->->-)))>-88788788@>->-))>-7
]]
,
[[
7_@@I7<kqI7I@775kjI7I@@70kqIIg@@7I@I7g@tlj)@@7I@R77@5lq@@@7@@@I7@-RR@R
R@@RIg@@7@@@I@@7Rg@@I@@@RI@RI-@@77@@I@@@R-@@II@7Rg@III@@77@@II@@RR@7IR
@@Rskllkkjjjkklmkkmkkklnkkmq@@R77@@IR@@R@@7IskkmqI@@77@@I-@RIsklmokmjj
kklnkmlq@@IRR@-77@@Ig@IIukkmkklmkkkjjkklnkllj)@-RR@@77@@IR@8@R-@@77@@I
I@gR0kmmmkkjjkklkkklkkjmjkkmq@@IR-@I77@@I7@@II@7R@@@Rskmmmkkjkkmlmkjml
klmnkkljkkmlkkjkkllnkjmmkkmnkklkkkmkkkjkkklokjmnklmlkkllkkmjkkjkkklokj
mokkmlkjlnkmjlkllmkjmq7@@R7@@Ig@I7R@III@7Rsknlq7@@7g@@I@@7Rukllq7@77tk
mmqI@1jj)@@R0kkjjukj@O
]]
)
Decoder:Decode(32,32,
[[
>->->->-)))>-88788788@>->-))>-7
]]
,
[[
7_@@I7<kqI7I@775kjI7I@@70kqIIg@@7I@I7g@tlj)@@7I@R77@5lq@@@7@@@I7@-RR@R
R@@RIg@@7@@@I@@7Rg@@I@@@RI@RI-@@77@@I@@@R-@@II@7Rg@III@@77@@II@@RR@7IR
@@Rskllkkjjjkklmkkmkkklnkkmq@@R77@@IR@@R@@7IskkmqI@@77@@I-@RIsklmokmjj
kklnkmlq@@IRR@-77@@Ig@IIukkmkklmkkkjjkklnkllj)@-RR@@77@@IR@8@R-@@77@@I
I@gR0kmmmkkjjkklkkklkkjmjkkmq@@IR-@I77@@I7@@II@7R@@@Rskmmmkkjkkmlmkjml
klmnkkljkkmlkkjkkllnkjmmkkmnkklkkkmkkkjkkklokjmnklmlkkllkkmjkkjkkklokj
mokkmlkjlnkmjlkllmkjmq7@@R7@@Ig@I7R@III@7Rsknlq7@@7g@@I@@7Rukllq7@77tk
mmqI@1jj)@@R0kkjjukj@O
]]
)
Coder = class()
--This function encodes an image in two strings
--the first string makes a list of unique color settings (r,g,b,a), encoded as an 8 char hex string
--the second string runs through the image, through rows then columns, and every time the color changes,
--it records the color setting and number of cells that had that color
--NB it doesn't store the color setting as an 8 char hex string, imstead it stores the position of
--this color in the first string. This saves a lot of storage where colors are used over and over again
local
Codes="!#&~*+,)/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ-_`abcdefghijklmnopqrstuvwxyz"
-- 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
-- 1 2 3 4 5 6 7 8
function Coder:Encode(img)
local rows=img.height
local cols=img.width
local colors={} --hash table of unique colors, gives us their sequence in the unique list
local strCol="" --unique list of color settings, each one is 8 chars hex, for r,g,b,a
local str="" -- each item consists of a color setting (or rather, the position in strCol), and the number of
--cells to be colored
--for example, if the image starts with 56 cells with r=1,g=2,b=5,a=6, we will add the hex value of this,
--which is 001002005006, to strCol, and set colors[001002005006]=1, (1 because this is the first color)
--to str, we add the hex values of the string position, ie 1, and the number of cells, ie 56, so we will
--add 001038 to str
local colorCount=0
local prevColor=-1 --to tell us when the color changes
local count=0 --number of cells with current color
local r,g,b,a,x
local tbl={}
local tblCol={}
--loop through image
for i=1,cols do
for j=1,rows do
r,g,b,a=img:get(i,j)
x=r..","..g..","..b..","..a
if x==prevColor then
count=count+1
else --color has changed, store details of last color
if count>0 then --this will only be zero for the very first cell
y=colors[prevColor] --this looks up the position of the color, in the color list
if y==nil then --add the color to the list if not there
colorCount = colorCount + 1
y=colorCount
colors[prevColor]=colorCount
--if colorCount>1 then
--table.insert(tblCol,","..prevColor)
--else
table.insert(tblCol,prevColor)
--end
end
table.insert(tbl,y..","..count)
end
prevColor=x
count=1
end
if errMessage~=nil then break end
end
if errMessage~=nil then break end
end
--we've finished, but we may have some left over chars that need to be stored
if count>0 then
y=colors[prevColor]
if y==nil then
colorCount = colorCount + 1
colors[prevColor]=colorCount
y=colorCount
end
table.insert(tblCol,y)
table.insert(tbl,count)
end
str=table.concat(tbl,",")
strCol=table.concat(tblCol,",")
--create code for user
if errMessage~=nil then
print(errMessage)
return errMessage,errMessage
else
str1=Coder:Compress(strCol)
str2=Coder:Compress(str)
local final = Coder:PrintCode(cols,rows,str1,str2)
return final
end
end
function Coder:Compress(d)
local a={}
local m=0
local y,z
local x=string.gsub(d,"8","82")
x=string.gsub(x,"7","81")
x=string.gsub(x,"9","83")
x=string.gsub(x,",","7")
for i=1,#x,2 do
y=string.sub(x,i,i)*9
if i<#x then y=y+string.sub(x,i+1,i+1) else y=y+7 end
y=y+1
z=string.sub(Codes,y,y)
table.insert(a,z)
end
--return table.concat(a)
return self:concatN(table.concat(a),70)
end
local function splitString(s,n)
local out = {}
out[1] = ""
if #s <= n
then out[1] = s
else
for k = 1,math.ceil( #s/n ) do
out[k] = string.sub(s,(k-1)*n+1,k*n)
end
end
return out
end
function Coder:concatN(a,n)
local t = splitString(a,n)
local s = table.concat(t,'\n')
return s
end
function Coder:PrintCode(c,r,s1,s2)
local str =
"Decoder:Decode("..c..","..r
..',\n[[\n'..s1.. '\n]]\n,\n[[\n'..s2.. '\n]]\n)'
saveProjectTab("ImageCode",str)
return str
end
function Coder:saveToImageCode(s)
local str = "--image code\n img = " .. s
saveProjectTab("ImageCode",str)
return str
end
function Coder:saveToIconsCode(s)
local str = readProjectTab("Icons") .. "\n" .. s .. "\n"
saveProjectTab("Icons",str)
end
Decoder=class()
local
Codes="!#&~*+,)/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ-_`abcdefghijklmnopqrstuvwxyz"
-- 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
-- 1 2 3 4 5 6 7 8
function Decoder:Decode(cols,rows,cc,dd)
local img=image(cols,rows)
local n=0
local m=4
local arrCols={}
cc = string.gsub(cc,"\n","")
dd = string.gsub(dd,"\n","")
strCol=Decoder:Decompress(cc)
strDat=Decoder:Decompress(dd)
--unpack color descriptions
for q in string.gmatch(strCol,"[^,]+") do
m = m + 1
if m==5 then
m=1
n = n + 1
arrCols[n]={}
end
arrCols[n][m]=q
end
--if m~=4 then print("faulty column codes, m=",m) end
--unpack RLE content
local col=1
local row=0
local ind=1
n=0
for q in string.gmatch(strDat, "[^,]+") do
if ind==1 then
colIndex=tonumber(q)
else
colCount=tonumber(q)
n = n + 1
for u=1,colCount do
row = row + 1
if row>rows then col=col+1 row=1 end
img:set(col,row,
color(arrCols[colIndex][1],arrCols[colIndex][2],arrCols[colIndex][3],arrCols[colIndex][4]))
end
end
ind=3-ind
end
return img
end
function Decoder:Decompress(a)
local s={}
local d1,d2,y,x
for i=1,#a do
y=string.find(Codes,string.sub(a,i,i))-1
d2=y%9
d1=(y-d2)/9
d2=d2
table.insert(s,d1)
table.insert(s,d2)
end
if s[#s]==7 then table.remove(s,#s) end
str=table.concat(s)
x=string.gsub(str,"7",",")
x=string.gsub(x,"81","7")
x=string.gsub(x,"83","9")
x=string.gsub(x,"82","8")
return x
end
displayMode(FULLSCREEN_NO_BUTTONS)
--sprite()
function setup()
--MakeBackup("Pixel Art", true)
Settings()
Red = Slider(true, 25, 245, HEIGHT - 50, 0, 255, 255, "Red", Colour)
Green = Slider(true, 25, 245, HEIGHT - 100, 0, 255, 255, "Green", Colour)
Blue = Slider(true, 25, 245, HEIGHT - 150, 0, 255, 255, "Blue", Colour)
Alpha = Slider(true, 25, 245, HEIGHT - 200, 0, 255, 255, "Alpha", Colour)
Dimension = InitialDimension
colour = color(Red.value, Green.value, Blue.value, Alpha.value)
Reset = Switch(true, "Reset", 205, 45, reset, reset)
local h0 = 300
SaveIcon = Switch(true, "SaveIcon", 125, HEIGHT - h0, SaveIconFunction)
local dh = 75
Save = Switch(true, "Save", 125, HEIGHT - h0 -dh, Save)
BackGroundChange = Switch(true, "BackGround", 125, 150, BackGrounder)
Switches = {"Pencil", "Picker", "Eraser", "Fill", "Circle", "Rect", "Line", "Crop Out", "Crop"}
droplist = DropList("Tools", 25, HEIGHT - 400, 75, 30, Switches, ToolSelected)
droplist.selected = true
droplistSP = DropList("Sprite", 185, HEIGHT - 405, 65, 30, spriteList("Documents"), Load, 5)
SpritePacks = {"Documents", "Dropbox", "Cargo Bot", "Planet Cute", "Platformer Art",
"Small World", "Space Art", "SpaceCute", "Tyrian Remastered"}
droplistSpritePack = DropList("SP", 125, HEIGHT - 405, 60, 30, SpritePacks, SpriteLoad, 5)
tool = "Pencil"
Exit = Switch(true, "Exit", 205, 10, close)
ShowGrids = Switch(false, "Grid", 205, 80, ShowG, ShowG)
UndoSwitch = Switch(true, "Undo", 205, 115, Undo)
Slides = {Red, Green, Blue, Alpha, Reset, Save, SaveIcon,
ShowGrids, droplist,
droplistSP, droplistSpritePack,
Exit, BackGroundChange, UndoSwitch}
grid = Grid(Dimension)
floodChecks = {}
colours = {alpha = 0}
touches = {}
SpritePack = "Documents"
Name = "Name"
Loader = ""
PopUp = ""
end
function Undo()
local col = colour
for k,v in pairs(grid.Move) do
colour = v.c
grid:Pixelate(v.x, v.y, true)
end
grid.Move = {}
colour = col
end
function SpriteLoad()
SpritePack = droplistSpritePack.selection
droplistSP.tab = spriteList(SpritePack)
end
function BackGrounder()
if BackGround == color(0, 0, 0, 255) then
BackGround = color(255, 255, 255, 255)
elseif BackGround == color(255, 255, 255, 255) then
BackGround = color(0, 0, 0, 255)
end
end
function reset()
grid = Grid(Dimension)
end
function ShowG()
if ShowGrid then
ShowGrid = false
elseif not ShowGrid then
ShowGrid = true
end
end
function Save()
saveImage("Documents:"..Name, grid.Image)
local s = Coder:Encode(grid.Image)
Coder:saveToImageCode(s)
PopUp = "Image Saved"
local a = tween(.5, colours, {alpha = 255})
local b = tween(2, colours, {alpha = 0})
tween.sequence(a, b)
end
function SaveIconFunction()
local s = "icons['".. Name .."'] = " .. Coder:Encode(grid.Image)
Coder:saveToIconsCode(s)
PopUp = "Image Saved"
local a = tween(.5, colours, {alpha = 255})
local b = tween(2, colours, {alpha = 0})
tween.sequence(a, b)
end
function Load()
Loader = droplistSP.selection
LoadImg = readImage(SpritePack..":"..Loader)
if LoadImg then
cos = coroutine.create(Job)
grid = Grid(vec2(LoadImg.width, LoadImg.height))
local a = tween(.5, colours, {alpha = 255})
local b = tween(2, colours, {alpha = 0})
Dimension = grid.dimension
Name = droplistSP.selection
tween.sequence(a, b)
coroutine.resume(cos)
PopUp = "Please Wait, Loading"
else
local a = tween(.5, colours, {alpha = 255})
local b = tween(2, colours, {alpha = 0})
tween.sequence(a, b)
PopUp = "Image Does Not Exist"
end
end
function ReGrid()
grid = Grid(Dimension)
end
function ToolSelected()
tool = droplist.selection
colour = color(Red.value, Green.value, Blue.value, Alpha.value)
grid.Move = {}
end
function Colour()
colour = color(Red.value, Green.value, Blue.value, Alpha.value)
end
function Job()
Progress = 0
for i = 1, LoadImg.width do
for j = 1, LoadImg.height do
local rc, gc, bc, ac = LoadImg:get(i, j)
colour = color(rc, gc, bc, ac)
grid:Pixelate(i, j)
end
coroutine.yield()
end
local a = tween(.5, colours, {alpha = 255})
local b = tween(2, colours, {alpha = 0})
tween.sequence(a, b)
Colour()
PopUp = "Loaded"
Progress = -1
end
function draw()
background(BackGround)
grid:draw()
for k,v in pairs(Slides) do
v:draw()
end
pushStyle()
stroke(127, 127, 127, 255)
strokeWidth(3)
noFill()
rectMode(CORNER)
rect(125, HEIGHT - 335, 140, 30)
rect(25, HEIGHT - 255, 75, 30)
rect(160, HEIGHT - 255, 75, 30)
font("AmericanTypewriter")
fontSize(18)
textMode(CORNER)
fill(255, 255, 255, 255)
if isKeyboardShowing() then
if Typing == "Save" then
Name = keyboardBuffer()
elseif Typing == "DimensionX" then
Dimension = vec2(tonumber(keyboardBuffer()), Dimension.y)
TypingDimension = true
elseif Typing == "DimensionY" then
Dimension = vec2(Dimension.x, tonumber(keyboardBuffer()))
TypingDimension = true
end
end
text(Name, 130, HEIGHT - 332)
text(Dimension.x, 30, HEIGHT - 250)
text(Dimension.y, 165, HEIGHT - 250)
stroke(colour)
fill(colour)
ellipse(130, HEIGHT - 240, 30)
popStyle()
pushStyle()
spriteMode(CORNER)
rectMode(CORNER)
fill(BackGround)
rect(0, 0, math.min(150, grid.Image.width), math.min(150, grid.Image.height))
sprite(grid.Image, 0, 0, math.min(150, grid.Image.width), math.min(150, grid.Image.height))
noFill()
stroke(127, 127, 127, 255)
strokeWidth(2)
rect(0, 0, math.min(150, grid.Image.width) + 2, math.min(150, grid.Image.height) + 2)
fill(255, 255, 255, colours.alpha)
textMode(CENTER)
fontSize(60)
text(PopUp, WIDTH/2 + 137, HEIGHT/2)
popStyle()
pushStyle()
rectMode(CORNER)
fill(255, 255, 255, 100)
local wd = math.min(150, grid.Image.width)
local ht = math.min(150, grid.Image.height)
rect(-grid.deltaX/749/grid.scl*grid.Image.height, -grid.deltaY/768/grid.scl*grid.Image.height,
wd/grid.scl, ht/grid.scl)
popStyle()
if #grid.floodChecks ~= 0 then
pushStyle()
fill(255, 255, 255, 255)
font("AmericanTypewriter")
fontSize(30)
textMode(CENTER)
text("Please Wait, Filling", WIDTH/2 + 137, HEIGHT/2)
popStyle()
end
if not isKeyboardShowing() and TypingDimension then
TypingDimension = false
ReGrid()
end
if LoadImg and Progress ~= -1 then
coroutine.resume(cos)
end
end
function touched(touch)
if touch.state == BEGAN then
table.insert(touches, touch)
end
for k,v in pairs(touches) do
if v.id == touch.id then
table.remove(touches, k)
table.insert(touches, k, touch)
end
end
if not isKeyboardShowing() then
for k,v in pairs(Slides) do
v:touched(touch)
end
end
if #grid.floodChecks <= 0 then
if not isKeyboardShowing() then
grid:touched(touch)
end
if touch.state == BEGAN then
if touch.x > 125 and touch.x < 235 and touch.y > HEIGHT - 335 and touch.y < HEIGHT - 305 then
showKeyboard()
Typing = "Save"
end
if touch.x > 25 and touch.x < 100 and touch.y > HEIGHT - 255 and touch.y < HEIGHT - 225 then
showKeyboard()
Typing = "DimensionX"
end
if touch.x > 160 and touch.x < 235 and touch.y > HEIGHT - 255 and touch.y < HEIGHT - 225 then
showKeyboard()
Typing = "DimensionY"
end
end
end
droplist.selected = true
if #touches > 2 then
touches = {}
end
if touch.state == ENDED then
for k,v in pairs(touches) do
if v.id == touch.id then
table.remove(touches, k)
end
end
end
end
pinch = class()
function pinch:init()
self.tb = {}
self.zoom = 1
end
function pinch:touched(touch)
if touch.state == ENDED then
self.tb = {}
self.d1 = nil
self.d2 = nil
else
if touch.x < WIDTH/2 - 749/2 + 137 then
table.insert(self.tb,{x=touch.x, y=touch.y})
end
end
end
function pinch:processTouches()
if #self.tb == 2 then
local v1 = vec2(self.tb[1].x, self.tb[1].y)
local v2 = vec2(self.tb[2].x, self.tb[2].y)
self.d1 = v1:dist(v2)
end
if self.d2 ~= nil then
self.zoom = math.min(math.max(self.zoom*self.d1/self.d2 , 1), 10)
end
self.d2 = self.d1
self.tb = {}
end
function Settings()
--if non retina device then set retina to 1 else let it be
retina = 0
Mode = "Sprite" --"Rect" or "Sprite". Sprite is faster and Cleaner for Big Images,
--Rect is Sharper for smaller Images.
InitialDimension = vec2(32, 32) --WIDTH X HEIGHT
BackGround = color(0, 0, 0, 255) --Colour of the background.
end
Slider = class()
function Slider:init(int, x1, x2, y, min, max, init, name, callback, callPeriod)
self.int = int
self.x1 = x1
self.y = y
self.x2 = x2
self.min = min
self.max = max
self.value = init
self.posn = x1 + (x2 - x1)/(max - min)*self.value - (x2 - x1)/(max - min)*self.min
self.name = name
self.callback = callback
self.callPeriod = callPeriod
if self.callPeriod then
self.timer = os.date("*t")
self.call = false
end
end
function Slider:draw()
self.posn = self.x1 + (self.x2 - self.x1)/(self.max - self.min)*self.value -
(self.x2 - self.x1)/(self.max - self.min)*self.min
pushStyle()
font("AmericanTypewriter")
fontSize(18)
textMode(CORNER)
fill(255, 255, 255, 255)
text(self.name, self.x1, self.y + 8)
text(self.value, self.x2 - textSize(self.value), self.y + 8)
strokeWidth(4)
stroke(127, 127, 127, 255)
line(self.x1, self.y, self.x2, self.y)
strokeWidth(5)
stroke(235, 235, 235, 255)
line(self.x1, self.y, self.posn, self.y)
strokeWidth(3)
fill(0, 0, 0, 255)
ellipse(self.posn, self.y, 18)
popStyle()
if self.callPeriod then
self.currentTime = os.date("*t")
if self.callback and self.call and self.timer.sec < self.currentTime.sec - .1 then
self.callback()
self.call = false
end
end
return(self.value)
end
function Slider:touched(touch)
local tx = touch.x
local ty = touch.y
if tx > self.x1 - 10 and tx < self.x2 + 10 and ty > self.y - 10 and ty < self.y + 10
and touch.state == BEGAN then
self.touching = true
end
if self.touching then
self.posn = math.max(math.min(tx, self.x2), self.x1)
if self.int then
self.value = self.min + math.floor((self.posn - self.x1)/(self.x2 - self.x1)*(self.max - self.min))
else
self.value = self.min + (self.posn - self.x1)/(self.x2 - self.x1)*(self.max - self.min)
end
if self.callback and not self.callPeriod then
self.callback()
elseif self.callPeriod then
self.call = true
self.timer = os.date("*t")
end
end
if touch.state == ENDED then
self.touching = false
end
end
Switch = class(Control)
function Switch:init(press, name, x, y, callback1, callback2)
self.controlName = name or "Switch"
self.x = x
self.y = y
self.itemText = {}
self.callback1 = callback1
self.callback2 = callback2
self.press = press
self.FontSize = 19
end
function Switch:draw()
pushStyle()
font("AmericanTypewriter")
fontSize(self.FontSize)
textMode(CORNER)
if self.selected then
fill(0, 169, 255, 255)
else
fill(255, 255, 255, 255)
end
text(self.controlName, self.x, self.y)
popStyle()
end
function Switch:touched(touch)
if self.press and touch.state == BEGAN then
if touch.x > self.x and touch.x < self.x + textSize(self.controlName) and
touch.y > self.y and touch.y < self.y + 20 then
self.selected = true
end
elseif self.press and touch.state == ENDED then
self.selected = false
if touch.x > self.x and touch.x < self.x + textSize(self.controlName) and
touch.y > self.y and touch.y < self.y + 20 then
self.callback1()
end
end
if touch.state == BEGAN and not self.press then
if touch.x > self.x and touch.x < self.x + textSize(self.controlName) and
touch.y > self.y and touch.y < self.y + 20 then
if self.selected then
self.selected = false
if self.callback2 then
self.callback2()
end
elseif not self.selected then
self.selected = true
if self.callback1 then
self.callback1()
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment