Touch example

Here is a small piece of code illustrating some touch commands. It provides examples of activating events when touch events are complete as well as tracking a touch, storing touches in tables and some rudimentary gesture recognition. These can be turned on and off via parameters

-- Touchtracker
-- by West
-- Use this function to perform your initial setup
function setup()
    touches={}
    tsup={} --tsup contains the supplementary info about the start position of the touch
    points={}
    pulse={}
    parameter.boolean("includeTarget",true)
    parameter.boolean("includeTrail",false)
    parameter.boolean("includeStart",true)
    parameter.boolean("includeDuration",false)
    parameter.boolean("includeHistory",true)
    parameter.boolean("joinHistory",false)
    parameter.boolean("includePulse",true)
    parameter.boolean("includeClassification",true)
end

-- This function gets called once every frame
function draw()
    -- This sets a background color 
    background(106, 164, 152, 255)
    noStroke()
    --draw any active touch pulses
    for i,p in pairs(pulse) do
        local pulsesize=500 --the maximum radius of the touch circle pulse
        local fade=100-(p.r/pulsesize)*100 --calculate the
        fill(255,255,255,fade)
        ellipse(p.x,p.y,p.r)
        p.rate = p.rate + 1
        p.r = p.r + p.rate
        if p.r>pulsesize then
            table.remove(pulse,i)
        end
    end
    
    fill(255)
    --draw a circle at each of the touched points
    if includeHistory==true then
        for i,pt in pairs(points) do
            ellipse(pt.x,pt.y,10)
            if joinHistory==true then
                --draw lines between all historic touch points
                strokeWidth(2)
                if i>1 then
                    stroke(255)
                    line(pt.x,pt.y,prevx,prevy)
                end
                prevx=pt.x
                prevy=pt.y
            end
        end
    end
    --draw start and stop circles and connect them with lines for all active touches
    strokeWidth(2)
    for i,t in pairs(touches) do
        fill(255)
        stroke(255)
        if includeStart==true then
            line(tsup[i].tstartx,tsup[i].tstarty,t.x,t.y)
            ellipse(tsup[i].tstartx,tsup[i].tstarty,10)
        end
        fill(255)
        if includeDuration==true then
            local formattime=math.floor(10*(ElapsedTime-tsup[i].starttime))/10
            text(formattime,t.x,t.y+50)
        end
        ellipse(t.x,t.y,10)
        if includeTarget==true then
            local endpt=math.max(1200-800*(ElapsedTime-tsup[i].starttime),50+10*math.sin(5*ElapsedTime))
            local spin=ElapsedTime*40
            pushMatrix()
            translate(t.x,t.y)
            rotate(spin)
            translate(-endpt,0)
            sprite("Cargo Bot:How Arrow",0,0)
            translate(endpt,0)
            rotate(90)
            translate(-endpt,0)
            sprite("Cargo Bot:How Arrow",0,0)
            translate(endpt,0)
            rotate(90)
            translate(-endpt,0)
            sprite("Cargo Bot:How Arrow",0,0)
            translate(endpt,0)
            rotate(90)
            translate(-endpt,0)
            sprite("Cargo Bot:How Arrow",0,0)
            translate(endpt,0)
            popMatrix()
        end
        --draw path of touch
        if includeTrail==true then
            for j,p in pairs(tsup[i].path) do
                fill(255)
                ellipse(p.x,p.y,5)
            end
        end
    end
end

function touched(touch)
    if touch.state==MOVING then
        --record path
       if tsup[touch.id]~=nil then
           table.insert(tsup[touch.id].path,vec2(touch.x,touch.y))
       end
    end
    if touch.state==ENDED or touch.state==CANCELLED then
        processTouch(touch)
        touches[touch.id] = nil
        tsup[touch.id]=nil
    else
        touches[touch.id] = touch
        --if there is no supplementary info associated with the current touch then add it
        if tsup[touch.id]==nil then 
            tsup[touch.id]={tstartx=touch.x,tstarty=touch.y,starttime=ElapsedTime,path={}} 
        end
    end
end

function processTouch(touch)
    if includeHistory==true then
        table.insert(points,vec2(touch.x,touch.y)) --add a point to the points table
    end
    if includePulse==true then
        table.insert(pulse,{x=touch.x,y=touch.y,r=8,rate=1}) --add a new pulse
    end
    if includeClassification==true then
        if ElapsedTime-tsup[touch.id].starttime<0.2 then
            --very short event
            if tsup[touch.id]==nil then
                print("tap")
            elseif vec2(touch.x,touch.y):dist(vec2(tsup[touch.id].tstartx,tsup[touch.id].tstarty))<10 then
                print("tap")
            else
                print("dash")
            end
        elseif ElapsedTime-tsup[touch.id].starttime<1 then  
            --a slightly longer gesture
                if touch.x>tsup[touch.id].tstartx+25 and math.abs(touch.y-tsup[touch.id].tstarty)<50 then
                    print("swipe right")
                elseif touch.x<tsup[touch.id].tstartx-25 and math.abs(touch.y-tsup[touch.id].tstarty)<50 then
                    print("swipe left")
                elseif touch.y>tsup[touch.id].tstarty+25 and math.abs(touch.x-tsup[touch.id].tstartx)<50 then
                    print("swipe up")
                elseif touch.y<tsup[touch.id].tstarty-25 and math.abs(touch.x-tsup[touch.id].tstartx)<50 then
                    print("swipe down")
                end
        end
    end
end

@West Thats a good touch example. I LOVE the sci-fi arrows that fly in.

@West very polished, thanks!

Thanks