@UberGoober I don’t know what to tell you, I don’t have any trouble with the touch areas. Did you try increasing the value in self.dist. Anyways, here’s another version. This one allows you to create or destroy a button on the fly. Put a name in the name box and slide one of the parameters.
viewer.mode=STANDARD
function setup()
parameter.text("name",name)
parameter.boolean("create",false,create)
parameter.boolean("destroy",false,destroy)
boxTab={}
rectMode(CENTER)
table.insert(boxTab,box(200,200,100,200,"box1",1))
table.insert(boxTab,box(200,400,200,100,"box2",2))
table.insert(boxTab,box(500,500,200,100,"box3",3))
end
function draw()
background(0)
for a,b in pairs(boxTab) do
b:draw()
end
end
function touched(t)
for a,b in pairs(boxTab) do
b:touched(t)
end
end
function create()
if name~="" then
table.insert(boxTab,box(WIDTH/2,HEIGHT/2,200,100,name,3))
end
create=false
end
function destroy()
if name~="" then
for a,b in pairs(boxTab) do
if b.name==name then
table.remove(boxTab,a)
end
end
end
destroy=false
end
box=class()
function box:init(x,y,w,h,n,v)
self.x=x
self.y=y
self.w=w
self.h=h
self.sel=0
self.lock=false
self.dir=""
self.name=n
self.dist=20
end
function box:draw()
fill(188, 176, 175)
rect(self.x,self.y,self.w,self.h)
fill(255)
if self.lock then
fill(255,0,0)
end
text(self.name,self.x,self.y)
end
function box:touched(t)
if t.state==BEGAN then
if t.x>self.x-self.w/2 and t.x<self.x+self.w/2 and
t.y>self.y-self.h/2 and t.y<self.y+self.h/2 and self.lock then
print(self.name.." selected")
end
if math.abs(t.x-self.x)<self.dist and math.abs(t.y-self.y)<self.dist then
self.sel=1
if self.lock then print(self.name.." selected") end
if t.tapCount==5 then self.lock= not self.lock end
end
if math.abs(t.x-(self.x+self.w/2))<self.dist and
math.abs(t.y-(self.y+self.h/2))<self.dist then self.dir="ur" end
if math.abs(t.x-(self.x+self.w/2))<self.dist and
math.abs(t.y-(self.y-self.h/2))<self.dist then self.dir="lr" end
if math.abs(t.x-(self.x-self.w/2))<self.dist and
math.abs(t.y-(self.y-self.h/2))<self.dist then self.dir="ll" end
if math.abs(t.x-(self.x-self.w/2))<self.dist and
math.abs(t.y-(self.y+self.h/2))<self.dist then self.dir="ul" end
end
if t.state==CHANGED and not self.lock then
if self.sel==1 then
self.x,self.y=t.x,t.y
elseif self.dir=="ur" then
self.w,self.h=self.w+t.deltaX*2,self.h+t.deltaY*2
elseif self.dir=="ul" then
self.w,self.h=self.w-t.deltaX*2,self.h+t.deltaY*2
elseif self.dir=="lr" then
self.w,self.h=self.w+t.deltaX*2,self.h-t.deltaY*2
elseif self.dir=="ll" then
self.w,self.h=self.w-t.deltaX*2,self.h-t.deltaY*2
end
if self.w<50 then self.w=50 end
if self.h<50 then self.h=50 end
end
if t.state==ENDED then
self.sel,self.dir=0,""
end
end