• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Snippet Swipe gesture

muruba

New member
CX Code Contributor
3rd Party Module Dev
Tutorial Author
Patreon Silver
Joined
Jul 5, 2017
Messages
230
Hi all,

Please see the simple swipe example:

https://gist.github.com/IvelleGames/a4038810ea53c67132088d291d10a227

Code:
Import mojo

Global SWIPE_QUICK_MS:Int = 500
Global TOUCH_RADIUS_THRESHOLD:Int = 15

Class Point
  Public
    Field x:Float
    Field y:Float
    Method New(xx:Float, yy:Float)
      Self.x = xx
      Self.y = yy
    End Method
    Method ToString:String()
      Return "Point [" + x + ", " + y + "]"
    End Method
    Method Close:Bool(p2:Point, radius:Int)
      Return CirclesColliding (x, y, radius, p2.x, p2.y, radius)
    End Method
End Class

Function CirclesColliding:Bool(x1:Int, y1:Int, radius1:Int, x2:Int, y2:Int, radius2:Int)

    'compare the distance To combined radii
    Local dx:Int = x2 - x1
    Local dy:Int = y2 - y1
    Local radii:Int = radius1 + radius2
    If (( dx * dx )  + ( dy * dy )) < (radii * radii)
        Return True
    Endif
End Function


Class SwipeEvent

  Public
 
    Field touched:Bool = False
   
    Field touchedStartPoint:Point = New Point()
    Field touchedStartTime:Int = 0
   
    Field touchedEndPoint:Point = New Point()
    Field touchedEndTime:Int = 0
   
    Field swipeFinished:Bool = False
    Field swipeType:String = ""
    Field swipeRelativeDistanceH:Float
    Field swipeRelativeDistanceV:Float
   
    Field touchFinished:Bool = False
   
   Public
    Method OnUpdate:Void()
     
      swipeFinished = False
      touchFinished = False
          
      If touched = True And False = TouchDown(0)
     
        touchedEndPoint = New Point( TouchX(), TouchY() )
        touchedEndTime = Millisecs()
       
'        Print "touched up: " +  touchedEndPoint.ToString()
'        Print ("touch up: "  + touchedEndPoint.ToString() + ", took: " + (touchedEndTime - touchedStartTime) + " ms" )
       
        If touchedEndPoint.Close(touchedStartPoint, TOUCH_RADIUS_THRESHOLD)
       
          Self.touchFinished= True
         
        Else
       
          Self.swipeFinished= True
         
          If touchedEndTime - touchedStartTime  < SWIPE_QUICK_MS
            swipeType = "quick"
          Else
            swipeType = "slow"
          Endif
         
          Self.swipeRelativeDistanceH = Abs ( (touchedStartPoint.x - touchedEndPoint.x) / DeviceWidth())
          Self.swipeRelativeDistanceV = Abs ( (touchedStartPoint.y - touchedEndPoint.y) / DeviceHeight())
         
        endif
       
        touched = False
       
               
      elseIf touched = False And TouchDown(0)
       
        touched = True
        touchedStartTime = Millisecs()
        touchedStartPoint = New Point( TouchX(), TouchY() )
'       Print ("touch down: " + touchedStartPoint.ToString() )
       
       Elseif TouchDown(0) = false
        'Print "looks like nothing touched"
        touched = False
       
      Endif
     
    End Method
End

Class Game Extends App

  Field swipeEvent:SwipeEvent

    Method OnCreate ()
        SetUpdateRate 60
        Self.swipeEvent =  New SwipeEvent()
    End
   
    Method OnUpdate ()

    swipeEvent.OnUpdate()
   
    If swipeEvent.touchFinished
      Print "touched: " + swipeEvent.touchedEndPoint
    ElseIf swipeEvent.swipeFinished
      Print "Swiped From: " + swipeEvent.touchedStartPoint.ToString() + " to: "  + swipeEvent.touchedEndPoint.ToString()
      Print "More swipe info: " + swipeEvent.swipeType + " [" +  swipeEvent.swipeRelativeDistanceH + ", " + swipeEvent.swipeRelativeDistanceV + "]"
    Endif
   

    End
   
    Method OnRender ()
   
    Cls 255,255,255
        DrawText "touched: " + Int(swipeEvent.touched), 10,10
  End
   
End

Function Main ()
    New Game
End
 
Back
Top Bottom