Craft touch handler

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

Sorry to dig this up from the past but it’s a current problem for me.

If I make the boxes’ priorities positive numbers it doesn’t seem to work at all.

@UberGoober That probably makes them a lower priority. Looks like the lower the number, the higher the priority. So a negative number make a higher priority.

@dave1707 according to the comments in the Touches project, I think, the priority number only affects the order in which handlers are called, it doesn’t prevent lower handlers from being called.

Are you saying that’s wrong—that the first handler to receive a touch blocks all others from receiving it?

Because what’s happening is that the squares aren’t responding at all.

@UberGoober In my example, if I make both boxes a priority 0, rotation works, but the boxes don’t. If I make one of the boxes -1, it works, but the other one doesn’t. If they’re both -1, they both work. If I make one of the boxes a lower number it has priority over the other one. So I would say the touch with the lowest number has priority over the other ones.

Ok so snooping around a bit it looks like OrbitViewer’s touchHandler is priority 0, and it captures touches, meaning it tells the touches class not to let anybody else react to a touch once it’s passed to the OrbitViewer. So I think that is why no positive numbers ever detect touches here.