Here’s an example that uses the Craft touch routine and touch handler. There’s a box that you can rotate by sliding your finger around the screen. I added a button class and created a red and green button. Press each button to show a text message. The red button has a -2 priority and the green button has -1. Craft Touch has a 0 priority. The lower the priority number, the higher it’s touch priority. I overlapped the buttons to show how the priority works. Even though the green button is on top of the red button, if you touch the green button where it overlaps the red button, the red button takes priority since it has a lower priority number than the green button. And since both buttons have a lower priority value than Craft touch, the buttons have priority over the Craft Touch for rotating the box.
displayMode(FULLSCREEN)
function setup()
col={{name="red",c=color(255,0,0)},{name="green",c=color(0,255,0)}}
assert(craft, "Please include Craft as a dependency")
assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")
scene = craft.scene()
v=scene.camera:add(OrbitViewer, vec3(0,0,0), 100, 0, 200)
c=scene:entity()
c.model = craft.model.cube(vec3(25,10,10))
c.material = craft.material("Materials:Standard")
c.material.map = readImage("Blocks:Trunk White Top")
b1=btn1(300,100,200,150,-2,col[1].name,col[1].c)
b2=btn1(400,25,200,150,-1,col[2].name,col[2].c)
str="Press a button or rotate the box"
end
function draw()
update(DeltaTime)
scene:draw()
fill(255)
text("Drag your finger on the screen to rotate the box",WIDTH/2,HEIGHT-100)
text(str,WIDTH/2,HEIGHT-200)
b1:draw()
b2:draw()
end
function update(dt)
scene:update(dt)
end
btn1 = class()
function btn1:init(x,y,w,h,p,n,c)
self.x=x
self.y=y
self.w=w
self.h=h
self.name=n
self.col=c
touches.addHandler(self,p) -- register class with priority
end
function btn1:draw()
fill(self.col)
rect(self.x,self.y,self.w,self.h)
end
function btn1:touched(t)
if t.state == BEGAN then
if t.x>self.x and t.x<self.x+self.w and
t.y>self.y and t.y<self.y+self.h then
str=self.name.." button pressed"
return true
end
elseif t.state==ENDED then
str="Press a button or rotate the box"
end
end