Using the below code. It slows the project down once I have four buttons. Three runs smooth.
Is the pointinginrect slowing me down?
Button = class()
-- Fast Mesh Button Class courtesy of @Vega
-- 3 Aug 2012
--
-- Modified: - Call Back Functionality added
-- - pushStyle() & popStyle() added to draw()
-- - tapped status added
-- - pointInRect() function added
-- - changed vec2 location to x and y (to reduce typing!)
--
-- Version 1.3 (4 Aug 2012)
function Button:init(text,x,y,width,height)
self.state = "normal"
self.text = text
self.textColor = color(255,255,255,192)
self.x = x
self.y = y
self.width = width
self.height = height
self.visible = true
self.fontSize = 28
self.font = "ArialRoundedMTBold"
self.color1 = color(255, 255, 255, 96)
self.color2 = color(128,128,128,32)
self.presscolor1 = color(192, 224, 224, 128)
self.presscolor2 = color(96, 192, 224, 128)
self.verts = self:createVerts(self.width, self.height)
self.myMesh = mesh()
self.myMesh.vertices = triangulate(self.verts)
self.vertColor = {}
self:recolor()
self.action = nil
self.tapped = false
self.actionpress= false
self.istouched = false
end
function Button:setColors(c1,c2,p1,p2)
self.color1 = c1
self.color2 = c2
self.presscolor1 = p1
self.presscolor2 = p2
self:recolor()
end
function Button:textOptions(fn, sz, col)
self.font = fn
self.fontSize = sz
self.textColor = col
end
function Button:draw()
if self.visible == true then
pushStyle()
pushMatrix()
translate(self.x,self.y)
self.myMesh:draw()
fill(self.textColor)
fontSize(self.fontSize)
font(self.font)
text(self.text, self.width/2,self.height/2)
self:drawLines(self.verts)
popMatrix()
popStyle()
if self.istouched then
if self.actionpress then
if self.action then
self.action()
end
end
end
end
end
function Button:touched(touch)
self.tapped = false
if self.visible then
if pointInRect(touch.x, touch.y, self.x, self.y, self.width, self.height) then
if touch.state == BEGAN then
self.tapped = true
self.state = "pressing"
self:recolor()
self.istouched = true
elseif touch.state == ENDED then
if self.state == "pressing" then
self.state = "normal"
self.tapped = true
self:recolor()
end
if self.actionpress == false then
if self.action then
self.action()
actiontook = true
end
end
self.istouched = false
end
else
self.state = "normal"
self:recolor()
end
end
if touch.state == ENDED then
self.istouched = false
end
end
function Button:createVerts(w,h)
local r
local v = {}
if w > 100 or h > 100 then
if w>=h then r = math.round(h/100) else r = math.round(w/100) end
else
r = 1
end
v[1] = vec2(w,6*r)
v[2] = vec2(w-r,4*r)
v[3] = vec2(w-2*r,2*r)
v[4] = vec2(w-4*r,r)
v[5] = vec2(w-6*r,0)
v[6] = vec2(6*r,0)
v[7] = vec2(4*r,r)
v[8] = vec2(2*r,2*r)
v[9] = vec2(r,4*r)
v[10] = vec2(0,6*r)
v[11] = vec2(0,h-6*r)
v[12] = vec2(r,h-4*r)
v[13] = vec2(2*r,h-2*r)
v[14] = vec2(4*r,h-r)
v[15] = vec2(6*r,h)
v[16] = vec2(w-6*r,h)
v[17] = vec2(w-4*r,h-r)
v[18] = vec2(w-2*r,h-2*r)
v[19] = vec2(w-r,h-4*r)
v[20] = vec2(w,h-6*r)
return v
end
function Button:drawLines(v)
noSmooth()
strokeWidth(1)
stroke(0, 0, 0, 192)
for i=1, #v-1 do
line(v[i].x,v[i].y,v[i+1].x,v[i+1].y)
end
line(v[#v].x,v[#v].y,v[1].x,v[1].y)
end
function Button:recolor()
local lt, dk
if self.state == "normal" then
lt = self.color1
dk = self.color2
else
lt = self.presscolor1
dk = self.presscolor2
end
for i=1,3 * #self.verts - 6 do
if self.myMesh.vertices[i].y > self.height/2 then
self.vertColor[i] = lt
else
self.vertColor[i] = dk
end
end
self.myMesh.colors = self.vertColor
end
-- Math Utilities
function math.round(value)
-- math.round function courtesy of Vega.
return math.floor(value + 0.5)
end
function pointInRect(pointX, pointY, x, y, w, h)
-- Returns true if point (pointX, pointY) is within the rectangle
-- with lower left corner at (x, y) with a width of w and a
-- height of h.
--
-- Reefwing Software (www.reefwing.com.au)
-- Version 1.0
if pointX >= x and pointX <= x + w and pointY >= y and pointY <= y + h then
return true
else
return false
end
end