Skip to content

Instantly share code, notes, and snippets.

@JMV38

JMV38/1aTabOrder Secret

Created August 24, 2013 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JMV38/2559b70abc9ed33796c7 to your computer and use it in GitHub Desktop.
Save JMV38/2559b70abc9ed33796c7 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.
#HandTriangle
#HandMesh
#Mesh
#Main
HandMesh = class()
function HandMesh:init(x)
self.triangles = {}
self.vert2tri = {}
self.x0,self.y0 = WIDTH/2,HEIGHT/2
self.ready = true
local a = 300
local imgSpot = image(20,20)
setContext(imgSpot)
background(255, 0, 0, 255)
setContext()
local imgSquare = image(10,10)
setContext(imgSquare)
background(255, 0, 0, 255)
setContext()
self.imgSpot = imgSpot
self.imgSquare = imgSquare
HandTriangle(self,a,-a,-a,-a,-a,a)
HandTriangle(self,a,-a,a,a,-a,a)
self.mode = "move"
self.retraitMax = 5
self.retrait = self.retraitMax
self:meshInit(x)
end
local sign = function(a)
local out
if a<0 then out=-1
elseif a==0 then out=0
else out =1 end
return out
end
function HandMesh:save()
local ms = self.ms
local vertices = {}
table.insert( vertices, "vertices = {")
local colors = {}
table.insert( colors, "colors = {")
for i=1,#self.ms.vertices do
table.insert( vertices, "vec3" .. tostring( ms:vertex(i) ).."," )
table.insert( colors, "color" .. tostring( ms:color(i) ).."," )
end
table.insert( vertices, " }")
table.insert( colors, " }")
local str = {}
table.insert( str, table.concat(vertices,"\r") )
table.insert( str, table.concat(colors,"\r") )
table.insert( str, [[
ms0 = mesh()
ms0.vertices = vertices
ms0.colors = colors
]]
)
saveProjectTab("Mesh",table.concat(str,"\r"))
print("mesh saved!")
end
function HandMesh:meshInit()
local ms = mesh()
local vertices = {}
local colors = {}
local i = 0
local k = 5
for _,t in pairs(self.triangles) do
t:meshIndex(i)
self.vert2tri[i]=t
vertices[i+1] = vec3(0,0,0)
vertices[i+2] = vec3(0,0,0)
vertices[i+3] = vec3(0,0,0)
colors[i+1] = t.col
colors[i+2] = t.col
colors[i+3] = t.col
i = i + 3
end
ms.vertices = vertices
ms.colors = colors
self.ms = ms
self:refresh()
end
function HandMesh:refresh()
for _,t in pairs(self.triangles) do
local i = t.index
self:setTrianglePosition(i)
self:setcolor(i,t.col)
end
end
function HandMesh:toggleRetrait()
if self.retrait == self.retraitMax then self.retrait = 0
else self.retrait = self.retraitMax end
self:refresh()
end
function HandMesh:setTrianglePosition(i)
local ms = self.ms
local k = self.retrait
local t = self.vert2tri[i]
local dv = vec3(t.x1-t.x123, t.y1-t.y123,0):normalize()*k
ms:vertex(i+1, vec3(t.x1,t.y1,0) - dv)
local dv = vec3(t.x2-t.x123, t.y2-t.y123,0):normalize()*k
ms:vertex(i+2, vec3(t.x2,t.y2,0) - dv)
local dv = vec3(t.x3-t.x123, t.y3-t.y123,0):normalize()*k
ms:vertex(i+3, vec3(t.x3,t.y3,0) - dv)
end
function HandMesh:setcolor(i,c)
local ms = self.ms
ms:color(i+1,c)
ms:color(i+2,c)
ms:color(i+3,c)
end
function HandMesh:split()
local copy = {}
for _,tri in pairs(self.triangles) do
table.insert(copy,tri)
end
for _,tri in pairs(copy) do
tri:split()
end
self:meshInit(x)
end
function HandMesh:draw()
translate(self.x0,self.y0)
self.ms:draw()
for _,tri in pairs(self.triangles) do
tri:draw()
end
end
function HandMesh:touched(touch)
local rtouch = {}
rtouch.state = touch.state
rtouch.x = touch.x - self.x0
rtouch.y = touch.y - self.y0
local intercepted = false
for _,tri in pairs(self.triangles) do
-- if not intercepted then intercepted = tri:touched(rtouch) end
tri:touched(rtouch)
end
end
HandTriangle = class()
function HandTriangle:init(parent,x1,y1,x2,y2,x3,y3)
self.parent = parent
self.list = parent.triangles
self.imgSpot = parent.imgSpot
self.imgSquare = parent.imgSquare
self.list[self]=self
self:reset(x1,y1,x2,y2,x3,y3)
self.p1Touched = false
self.p2Touched = false
self.p3Touched = false
self.p123Touched = false
self.r = 5
self.c = 8
self.col = color(200)
self.ready = true
end
function HandTriangle:reset(x1,y1,x2,y2,x3,y3)
self.x1,self.y1,self.x2,self.y2,self.x3,self.y3 = x1,y1,x2,y2,x3,y3
self.x123 = (x1+x2+x3)/3
self.y123 = (y1+y2+y3)/3
local ms = self.parent.ms
local i = self.index
if i then
self.parent:setTrianglePosition(i)
end
end
function HandTriangle:meshIndex(i)
self.index = i
end
function HandTriangle:delete()
self.col = color(0,0,0,0)
self.parent:setcolor(self.index,self.col)
end
function HandTriangle:setColor()
self.col = currentColor
self.parent:setcolor(self.index,self.col)
end
function HandTriangle:split()
if not self.ready then return end
local x1,y1,x2,y2,x3,y3 = self.x1,self.y1,self.x2,self.y2,self.x3,self.y3
local x12 = (x1+x2)/2
local y12 = (y1+y2)/2
local x23 = (x3+x2)/2
local y23 = (y3+y2)/2
local x31 = (x1+x3)/2
local y31 = (y1+y3)/2
self:reset(x23,y23,x12,y12,x31,y31)
local a = HandTriangle(self.parent,x2,y2,x12,y12,x23,y23)
a.col = self.col
local a = HandTriangle(self.parent,x1,y1,x12,y12,x31,y31)
a.col = self.col
local a = HandTriangle(self.parent,x3,y3,x31,y31,x23,y23)
a.col = self.col
self.ready = false
tween.delay(0.2,function() self.ready=true end)
end
function HandTriangle:splitAndUpdate()
self:split()
self.parent:meshInit()
end
function HandTriangle:draw()
pushStyle()
resetStyle()
stroke(255, 0, 0, 255)
fill(255, 0, 0, 255)
strokeWidth(3)
popStyle()
end
function HandTriangle:touched(touch)
local abs = math.abs
local x1,y1,x2,y2,x3,y3 = self.x1,self.y1,self.x2,self.y2,self.x3,self.y3
local x123,y123 = self.x123,self.y123
local x,y,state= touch.x,touch.y,touch.state
local intercept = false
local r = 20
if state == BEGAN then
if self.parent.mode == "move" then
if abs(x1-x)<r and abs(y1-y)<r then self.p1Touched = true
elseif abs(x2-x)<r and abs(y2-y)<r then self.p2Touched = true
elseif abs(x3-x)<r and abs(y3-y)<r then self.p3Touched = true
end
else
if abs(x123-x)<r and abs(y123-y)<r then
self.p123Touched = true
if self.parent.mode == "delete" then self:delete() end
if self.parent.mode == "color" then self:setColor() end
if self.parent.mode == "split" then self:splitAndUpdate() end
end
end
elseif state == MOVING then
if self.parent.mode == "move" then
if self.p1Touched then x1,y1 = x,y end
if self.p2Touched then x2,y2 = x,y end
if self.p3Touched then x3,y3 = x,y end
self:reset(x1,y1,x2,y2,x3,y3)
else
if abs(x123-x)<r and abs(y123-y)<r then
self.p123Touched = true
if self.parent.mode == "delete" then self:delete() end
if self.parent.mode == "color" then self:setColor() end
if self.parent.mode == "split" then self:splitAndUpdate() end
end
end
else -- ENDED or CANCELLED
self.p1Touched = false
self.p2Touched = false
self.p3Touched = false
end
return intercept
end
-- 2d mesh drawer
-- PAS FINI FONCTIONNE PAS- JETABLE
-- Use this function to perform your initial setup
function setup()
ms = HandMesh()
print(ms.mode)
show = true
parameter.action("show new mesh / saved mesh",function() show = not show end)
parameter.action("save mesh",function() ms:save() end)
parameter.action("split triangles",function() ms:split() end)
parameter.action("move / color / delete / split",
function()
if ms.mode=="move" then ms.mode="color"
elseif ms.mode=="color" then ms.mode="delete"
elseif ms.mode=="delete" then ms.mode="split"
elseif ms.mode=="split" then ms.mode="move"
end
print(ms.mode)
end)
parameter.action("show/hide triangles",function() ms:toggleRetrait() end)
parameter.color("currentColor",128)
if FPS then fps = FPS() end
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
if show then
ms:draw()
else
translate(ms.x0,ms.y0)
ms0:draw()
end
if fps then resetMatrix() fps:draw() end
end
function touched(touch)
if show then ms:touched(touch) end
end
vertices = {
vec3(295.527863, -297.763947, 0.000000),
vec3(-296.464478, -296.464478, 0.000000),
vec3(-297.763947, 295.527863, 0.000000),
vec3(297.763947, -295.527863, 0.000000),
vec3(296.464478, 296.464478, 0.000000),
vec3(-295.527863, 297.763947, 0.000000),
}
colors = {
color(200, 200, 200, 255),
color(200, 200, 200, 255),
color(200, 200, 200, 255),
color(200, 200, 200, 255),
color(200, 200, 200, 255),
color(200, 200, 200, 255),
}
ms0 = mesh()
ms0.vertices = vertices
ms0.colors = colors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment