[Monkey] Gestenerkennung auf dem Android und IPhone

Übersicht Andere Programmiersprachen Codearchiv & Module

Neue Antwort erstellen

Midimaster

Betreff: Gestenerkennung auf dem Android und IPhone

BeitragDi, Okt 02, 2012 12:32
Antworten mit Zitat
Benutzer-Profile anzeigen
Hier ist ein erster selbstgebastelter Versuch eine Gestenerkennung in Monkey zu schreiben. Der Code verwendet keine nativen Java-Funktionen, sondern nur den Touch() Befehl aus Monkey. Auf meinem Android läuft es, es dürfte aber auch am Iphone laufen.

Zum Implementieren wird nur die Klasse Gesture benötigt. Dem Code hier habe ich auch gleich ein lauffähiges Beispiel hinzugefügt.

Zur Zeit können folgende Gesten erkannt werden:

Zoomen, Rotieren, Bewegen und Klicken.

Version=0.81 Ich werde es wahrscheinlich noch weiter ausbauen....

BlitzMax: [AUSKLAPPEN]
Strict
Import mojo

Class GestureTest Extends App

Field MyScale#=1, MyRotation%=0, MyBoxColor%, MyX%, MyY%
Field Gestures:Gestures


Method OnCreate%()
Gestures=New Gestures
SetUpdateRate 30
Return 0
End



Method OnUpdate%()

If KeyHit(KEY_ESCAPE) Then Error ""

' check gestures:
Gestures.Update()

'set final values, if a gestures was finished:
' Zoom:
MyScale=MyScale*Gestures.LastZoom()
' rotation:
MyRotation=MyRotation + Gestures.LastAngle()
' Movement:
MyX=MyX + Gestures.LastX()
MyY=MyY + Gestures.LastY()
' TouchHit():
If Gestures.ItWasAHit()
MyBoxColor=Int(Rnd(5))
End

Return 0
End





Method OnRender%()
Cls

' change temp.values of running gesture:
'zoom:
Local ActScale#=MyScale*Gestures.TempZoom()
'rotation:
Local ActRotation%=MyRotation+Gestures.TempAngle()
Local ActTranslationX%=MyX+Gestures.TempX()
Local ActTranslationY%=MyY+Gestures.TempY()

Translate 120,160
Translate ActTranslationX,ActTranslationY
Scale ActScale,ActScale
Rotate ActRotation

DrawRect -50,-40,100,80
' Color indicates a TouchHit()
SetColorIndex MyBoxColor
DrawRect -50,-40,100,20
Return 0
End

End



Function Main%()
New GestureTest
Return 0
End



Function SetColorIndex:Void(No%)
Select No
Case 0
SetColor 255,0,0
Case 1
SetColor 255,255,0
Case 2
SetColor 0,255,0
Case 3
SetColor 255,0,255
Case 4
SetColor 0,0,255
End Select
End


Class Gestures
'
' can detect zoom and rotaion gestures on Android and can differ these from TouchHit(). During one
' gesture only 'Zoom' OR 'Rotation' OR 'TouchHit' can happen.
'
' During a gesture the current values are offered in TempZoom#() TempAngle#() TempX#() TempY#()
' At the end of the gesture call LastZoom#() LastAngle#() ItWasAHit%() LastX#() LastY#()
' to get the final values
'
' TempZoom#() LastZoom#() return values form 0.01 to 1 and 1 to 100 as a scaling factor
' TempAngle#() LastAngle#() return values from -360° to +360° in degrees
' TempX#() LastX#() ... return values in from -1024 to 1024 pixels
' ItWasAHit%() returns FALSE if a Zoom or Rotation was detected and TRUE if it was a simple TouchHit()
'
Field NewHit%=False
Field StartZoom#, LastZoomValue#=1,TempZoomValue#=1
Field StartAngle#,LastAngleValue#=0, TempAngleValue#=0
Field StartX%, StartY%, LastXValue%, LastYValue%, TempXValue%, TempYValue%

Field GestureTyp%
Const G_ONE%=1, G_HIT%=2, G_BOTH%=4, G_MOVE%=8, G_ZOOM%=16, G_ROTATE%=32

Method Update:Void()

'check current values of zoom And rotation
Local NowZoom#= Pow(TouchX(0)-TouchX(1),2) + Pow(TouchY(0)-TouchY(1),2)
Local NowAngle% = (360+ ATan2( TouchX(0)-TouchX(1), TouchY(0)-TouchY(1) ) ) Mod 360
Local NowX%=TouchX(0)
Local NowY%=TouchY(0)
TempZoomValue=1
TempAngleValue=0
TempXValue=0
TempYValue=0

'check if both fingers are down:
If TouchDown(0) And TouchDown(1)

If GestureTyp<G_BOTH
GestureTyp=G_BOTH
' start up: store values for zoom and rotation
StartZoom=NowZoom
StartAngle = NowAngle
EndIf


Local locZoom#=NowZoom/StartZoom
' store values difference since start
Select GestureTyp
Case G_ROTATE
TempAngleValue = NowAngle-StartAngle
Case G_ZOOM
TempZoomValue=locZoom
Case G_BOTH
If ((Abs(NowAngle-StartAngle)>15) )
GestureTyp=G_ROTATE
TempAngleValue = NowAngle-StartAngle
ElseIf (locZoom>1.2) Or (locZoom<0.8)
TempZoomValue=locZoom
GestureTyp=G_ZOOM
EndIf
End Select

' no, only one finger is down:
ElseIf TouchDown(0)
' long enough?
If GestureTyp=0
' start up: store values for movement
GestureTyp=G_ONE
StartX=NowX
StartY=NowY
EndIf


Select GestureTyp
' store values difference since start
Case G_MOVE
TempXValue=NowX-StartX
TempYValue=NowY -StartY
Case G_ONE
If Pow(NowX-StartX,2)+Pow(NowY-StartY,2)>20
Print "move"
GestureTyp=G_MOVE
TempXValue=NowX-StartX
TempYValue=NowY-StartY
EndIf
End Select



' no, fingers released:
Else
If GestureTyp>0

' now make the new values become the final
Select GestureTyp
Case G_ROTATE
LastAngleValue= NowAngle-StartAngle
Case G_ZOOM
LastZoomValue= NowZoom/StartZoom
Case G_MOVE
Print "was move"
LastXValue=NowX-StartX
LastYValue=NowY-StartY
Default
Print "hit"
NewHit=True
End Select
GestureTyp=0
EndIf
EndIf
End



Method LastZoom#()
Local loc#=LastZoomValue
' is it a final value?
'no:
If loc=1 Then Return 1
'yes:
'reset last values for next gesture:
LastZoomValue=1
'TempZoomValue=1
Return loc
End Method



Method LastAngle#()
Local loc#= LastAngleValue
' is it a final value?
'no:
If loc=0 Then Return 0
'yes:
'reset last values for next gesture:
LastAngleValue=0
'TempAngleValue=0
Return loc
End Method



Method LastX#()
Local loc#=LastXValue
' is it a final value?
'no:
If loc=1 Then Return 0
'yes:
'reset last values for next gesture:
LastXValue=0
'TempZoomValue=1
Return loc
End





Method LastY#()
Local loc#=LastYValue
' is it a final value?
'no:
If loc=1 Then Return 0
'yes:
'reset last values for next gesture:
LastYValue=0
'TempZoomValue=1
Return loc
End



Method ItWasAHit%()
Local loc%= NewHit
'reset temp value for next gesture:
NewHit=False
Return loc
End



Method TempZoom#()
' return value, but do not reset
Return TempZoomValue
End



Method TempAngle#()
' return value, but do not reset
Return TempAngleValue
End

Method TempX#()
' return value, but do not reset
Return TempXValue
End

Method TempY#()
' return value, but do not reset
Return TempYValue
End
End
Gewinner des BCC #53 mit "Gitarrist vs Fussballer" http://www.midimaster.de/downl...ssball.exe

Neue Antwort erstellen


Übersicht Andere Programmiersprachen Codearchiv & Module

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group