Click and drag library

Post Reply
sneepy
Posts: 8
Joined: Sun Jul 06, 2014 4:00 am
My devices: Ipad

Click and drag library

Post by sneepy »

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

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Click and drag library

Post by Mr. Kibernetik »

Very interesting, thanks!

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Click and drag library

Post by Henko »

Thanks, i know this is not easy stuff (used it in the PERT planning program).
What kind of programs will you make?

sneepy
Posts: 8
Joined: Sun Jul 06, 2014 4:00 am
My devices: Ipad

Re: Click and drag library

Post by sneepy »

I've been working on a program to help me run an elementary school chess tournament. Hope to complete it before the May 2015 competition. A couple more utilities to follow when I get them beautified for public display.

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Click and drag library

Post by rbytes »

It seems as if some change in smartBASIC has made this code unusable. I haven't spent a lot of time with it, but I get a syntax error on the line "click=0" in the code for DEF tp. if I comment that out, then another line has a syntax error, and so on.

MR. K., can you see if it still runs for you?
The only thing that gets me down is gravity...

Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Re: Click and drag library

Post by Operator »

Guess it runs now...
(renamed some variables: adding1:
click -> click1)
Don't ask me why, it just works...,please check.
[sB 5.6 / iOS 6.1 / iPhone 4]

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
click1 = 0
nclick = 0
doubleclick1 = 0
dragging1 = 0
dragged1 = 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.click1 = 0
    tp.doubleclick1 = 0
    tp.dragged1 = 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.dragging1 = 0 AND TIMER() - tp.t0 > tp.clicktime THEN tp.dragging1 = 1
  END IF
ELSE 'no touch
  IF tp.dragging = 1 THEN
    tp.dragging1 = 0
    tp.dragged1 = 1
    tp.TOUCH = 0
    tp.tdrag = TIMER()
  ELSE
    IF tp.TOUCH = 1 THEN
      tp.TOUCH = 0
      IF TIMER() - tp.t0 > tp.clicktime THEN
        tp.dragged1 = 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.click1 = 1
          tp.tclick = tp.t0
        ELSE
          tp.doubleclick1 = 1
          tp.click1 = 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.click1
  tp.click1 = 0
END DEF

DEF doubleclick
  doubleclick = tp.doubleclick1
  tp.doubleclick1 = 0
END DEF

DEF dragging
  dragging = tp.dragging1
END DEF

DEF dragged
  dragged = tp.dragged1
  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
Attachments
image.jpg
image.jpg (61.42 KiB) Viewed 4570 times

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Click and drag library

Post by rbytes »

Yes, it works now. Iwill play with it a bit. There is a variable being printed in the top left corner, but it overwrites itself without erasing and becomes illegible. I think that is the drag length. Also a doubleclick prints Click and then Doubleclick right on top of it. That should be fixed. These routines could be quit useful.
The only thing that gets me down is gravity...

matt7
Posts: 115
Joined: Sun Jul 12, 2015 5:00 pm
My devices: iPhone
Location: USA

Re: Click and drag library

Post by matt7 »

This is really cool! I have been thinking about how to go about detecting these sorts of touches.

A correction though: I believe when updating the variable names some were missed:

Code: Select all

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 dragged
  dragged = tp.dragged1
  tp.dragged = 0
END DEF
Should be:

Code: Select all

DEF touchstart
  IF tp.TOUCH = 0 THEN RETURN 0
  IF tp.click1 + tp.doubleclick1 + tp.dragged1 + tp.dragging1 THEN RETURN 0
  RETURN 1
END DEF

' ...

DEF dragged
  dragged = tp.dragged1
  tp.dragged1 = 0
END DEF
Not sure what effect this had on touchstart, but the effect this had on dragged was that it would constantly return 1 every time it was called after a drag event, rather than resetting tp.dragged1 to zero after the first time detecting it.

Also, there was one other one I found in DEF tappoll.

Here is everything fixed, with some tweaks to displaying the drag time and showing X's for clicks and doubleclicks:

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.
*/

'b'
'uncomment this section to test taps
tp
GRAPHICS
GRAPHICS CLEAR 0,0,0
REFRESH OFF
DRAW COLOR 1,1,.8
FILL COLOR 0,0,0
poll:
tappoll
IF click THEN
  DRAW TEXT "click" AT tapx+5, tapy
  DRAW LINE tp.x-4,tp.y-4 TO tp.x+4,tp.y+4
  DRAW LINE tp.x-4,tp.y+4 TO tp.x+4,tp.y-4
END IF
IF doubleclick THEN
  GRAPHICS CLEAR 0,0,0
  DRAW TEXT "doubleclick" AT tapx+5, tapy
  DRAW LINE tp.x-4,tp.y-4 TO tp.x+4,tp.y+4
  DRAW LINE tp.x-4,tp.y+4 TO tp.x+4,tp.y-4
END IF
IF dragging THEN
  DRAW CIRCLE dragendx, dragendy SIZE 3
  FILL RECT 0,0 TO 120,20
  DRAW TEXT (dragt - dragstartt)/1000 AT 1,1
END IF
IF dragged THEN
  DRAW CIRCLE dragendx, dragendy SIZE 8
  DRAW CIRCLE tapx, tapy SIZE 8
  FILL RECT 0,0 TO 120,20
  DRAW TEXT (dragt - dragstartt)/1000 AT 1,1
END IF
REFRESH
GOTO poll

''
GOTO tapinit

DEF tp 'setup
clicktime = 100
doubleclicktime = 300
t0 = TIMER()
TOUCH = 0
click1 = 0
nclick = 0
doubleclick1 = 0
dragging1 = 0
dragged1 = 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.click1 = 0
    tp.doubleclick1 = 0
    tp.dragged1 = 0
    'tp.dragging1 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.dragging1 = 0 AND TIMER() - tp.t0 > tp.clicktime THEN tp.dragging1 = 1
  END IF
ELSE 'no touch
  IF tp.dragging1 = 1 THEN
    tp.dragging1 = 0
    tp.dragged1 = 1
    tp.TOUCH = 0
    tp.tdrag = TIMER()
  ELSE
    IF tp.TOUCH = 1 THEN
      tp.TOUCH = 0
      IF TIMER() - tp.t0 > tp.clicktime THEN
        tp.dragged1 = 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.click1 = 1
          tp.tclick = tp.t0
        ELSE
          tp.doubleclick1 = 1
          tp.click1 = 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.click1 + tp.doubleclick1 + tp.dragged1 + tp.dragging1 THEN RETURN 0
  RETURN 1
END DEF

DEF click
  click = tp.click1
  tp.click1 = 0
END DEF

DEF doubleclick
  doubleclick = tp.doubleclick1
  tp.doubleclick1 = 0
END DEF

DEF dragging
  dragging = tp.dragging1
END DEF

DEF dragged
  dragged = tp.dragged1
  tp.dragged1 = 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

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Click and drag library

Post by rbytes »

Thanks, matt7, for fixing the bugs. I will try to use some of these touches in my future programs.
The only thing that gets me down is gravity...

Post Reply