Last active
October 11, 2017 09:29
-
-
Save IvelleGames/a4038810ea53c67132088d291d10a227 to your computer and use it in GitHub Desktop.
Cerberus-X Swipe example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment