Click and drag library
Posted: Sun Aug 24, 2014 12:45 pm
Here is the library I have been using to get clicks and drags
Code: Select all
/*
higher order functions for touch interface
TAPPOLL() is the polling routine to detect the touches and follow the time. Must
be called before the other functions can work
TAPX() and TAPY() return the coordinates of the click, double click, or start of drag
TOUCHSTART() = 1 while a touch is going on and click has not been decided - first 100ms
CLICK() = 1 if the most recent touch activity was a click, else 0
DOUBLECLICK() = 1 if the most recent touch was a double click, else 0
DRAGGING() = 1 if the ongoing touching of the screen is dragging
DRAGGED() = 1 if the most recent completed touch was dragging
DRAGENDX() and DRAGENDY() are the end of the last drag
DRAGSTARTT() and DRAGT() are the start and ongoing/end times of the drag
taps.clicktime is the duration in ms of touch after which it is a drag and not a click
x and y are determined by the first touch. click, double click, and dragged persist until polled or the next touch starts a new event.
*/
/*
'uncomment this section to test taps
tp
graphics
graphics clear 0,0,0
draw color 1,1,.8
poll:
tappoll
if click then draw text "click" at tapx, tapy
if doubleclick then
draw text "doubleclick" at tapx, tapy
pause 1
graphics clear 0,0,0
end if
if dragging then draw circle dragendx, dragendy size 3
if dragged then
draw circle dragendx, dragendy size 8
draw circle tapx, tapy size 8
draw text (dragt - dragstartt)/1000 at 1,1
end if
goto poll
*/
goto tapinit
def tp 'setup
clicktime = 100
doubleclicktime = 300
t0 = timer()
touch = 0
click = 0
nclick = 0
doubleclick = 0
dragging = 0
dragged = 0
tclick = 0
tdrag = 0
end def
def tappoll
'returns nothing
'if there is a touch
' if it is new
' clear click, double click, and dragged flags (but not dragging)
' set touch flag
' log its position and time
' else continued touch
' log drag position
' if not already dragging
' if click time expired
' set dragging flag
' (else nothing. continued touch and not new dragging)
'else no touch
' if dragging
' set dragged flag
' clear dragging flag
' clear touch
' log end drag time
' else not dragging
' if touch was set
' clear touch
' if click time exceeded
' set dragged flag
' else it's a click
' if no prior click, or prior click but too long ago for a double click
' click count = 1
' set click,tclick
' else it's a double click
' reset click count,click
' set doubleclick
get touch 0 as x,y
if x<> -1 and y <> -1 then 'there is a touch
if tp.touch = 0 then 'new touch
tp.click = 0
tp.doubleclick = 0
tp.dragged = 0
'tp.dragging already = 0
tp.touch = 1
tp.t0 = timer()
tp.x = x
tp.y = y
else 'continued touch
tp.dragx = x
tp.dragy = y
tp.tdrag = timer()
if tp.dragging = 0 and timer() - tp.t0 > tp.clicktime then tp.dragging = 1
end if
else 'no touch
if tp.dragging = 1 then
tp.dragging = 0
tp.dragged = 1
tp.touch = 0
tp.tdrag = timer()
else
if tp.touch = 1 then
tp.touch = 0
if timer() - tp.t0 > tp.clicktime then
tp.dragged = 1
tp.tdrag = timer()
else 'it is the end of a click
if tp.nclick = 0 or timer() - tp.tclick > tp.doubleclicktime then
tp.nclick = 1
tp.click = 1
tp.tclick = tp.t0
else
tp.doubleclick = 1
tp.click = 0
tp.nclick = 0
end if
end if
end if
end if
end if
end def
def tapx
tapx = tp.x
end def
def tapy
tapy = tp.y
end def
def touchstart
if tp.touch = 0 then return 0
if tp.click + tp.doubleclick + tp.dragged + tp.dragging then return 0
return 1
end def
def click
click = tp.click
tp.click = 0
end def
def doubleclick
doubleclick = tp.doubleclick
tp.doubleclick = 0
end def
def dragging
dragging = tp.dragging
end def
def dragged
dragged = tp.dragged
tp.dragged = 0
end def
def dragendx
dragendx = tp.dragx
end def
def dragendy
dragendy = tp.dragy
end def
def dragstartt
dragstartt = tp.t0
end def
def dragt
dragt = tp.tdrag
end def
tapinit:
tp