Browse Source

Touch events part 1...

Mark Sibly 8 years ago
parent
commit
4ed2168ca0

+ 89 - 0
bananas/touchtest/touchtest.monkey2

@@ -0,0 +1,89 @@
+
+Namespace myapp
+
+#Import "<std>"
+#Import "<mojo>"
+
+Using std..
+Using mojo..
+
+Class MyWindow Extends Window
+
+	Field gesturing:Bool
+	
+	Field g0:AffineMat3f
+	
+	Method New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=Null )
+
+		Super.New( title,width,height,flags )
+	End
+	
+	Method GestureMatrix:AffineMat3f( p0:Vec2f,p1:Vec2f )
+	
+		Local d:=p1-p0,r:=-ATan2( d.y,d.x ),s:=d.Length
+		
+		Return New AffineMat3f().Translate( p0 ).Rotate( r ).Scale( s,s )
+	End
+
+	Method OnRender( canvas:Canvas ) Override
+	
+		App.RequestRender()
+
+		Local gmatrix:=New AffineMat3f
+		
+		If Not gesturing And Touch.FingerDown( 0 ) And Touch.FingerDown( 1 )
+		
+			'begin gesturing
+			g0=-GestureMatrix( Touch.FingerLocation( 0 ),Touch.FingerLocation( 1 ) )
+			
+			gesturing=True
+		
+		Endif
+		
+		If gesturing And Touch.FingerDown( 0 ) And Touch.FingerDown( 1 )
+			
+			'gesturing...
+			gmatrix=GestureMatrix( Touch.FingerLocation( 0 ),Touch.FingerLocation( 1 ) ) * g0
+		
+		Else If gesturing
+		
+			'end gesturing
+			gesturing=False
+		
+		Endif
+		
+		canvas.PushMatrix()
+		
+		canvas.Matrix=gmatrix
+		
+		For Local i:=0 Until 8
+			For Local j:=0 Until 8
+				canvas.Color= (i~j)&1 ? Color.Yellow Else Color.Grey
+				canvas.DrawRect( New Recti( i*Width/8,j*Height/8,(i+1)*Width/8,(j+1)*Height/8 ) )
+			Next
+		Next
+		
+		canvas.PopMatrix()
+		
+		canvas.Color=Color.White
+		
+		canvas.Scale( 2,2 )
+		
+		For Local i:=0 Until 10
+		
+			canvas.DrawText( "Finger "+i+": down="+Int( Touch.FingerDown(i) )+", location="+Touch.FingerLocation(i)+", pressure="+Touch.FingerPressure(i),0,i*canvas.Font.Height )
+		
+		Next
+		
+	End
+	
+End
+
+Function Main()
+
+	New AppInstance
+	
+	New MyWindow
+	
+	App.Run()
+End

+ 55 - 2
modules/mojo/app/app.monkey2

@@ -101,6 +101,8 @@ Class AppInstance
 		
 		Mouse.Init()
 		
+		Touch.Init()
+		
 		Audio.Init()
 		
 		'Set GL attributes
@@ -569,6 +571,9 @@ Class AppInstance
 	Field _mouseLocation:Vec2i
 	Field _mouseWheel:Vec2i
 	Field _mouseClicks:Int=0
+	Field _finger:Int
+	Field _fingerPressure:Float
+	Field _fingerCoords:Vec2f
 	
 	Field _modalView:View
 	Field _modalStack:=New Stack<View>
@@ -589,9 +594,11 @@ Class AppInstance
 	
 	Method UpdateEvents()
 	
+		Keyboard.Update()
+		
 		Mouse.Update()
 		
-		Keyboard.Update()
+		Touch.Update()
 		
 		Local event:SDL_Event
 
@@ -656,6 +663,18 @@ Class AppInstance
 		
 	End
 	
+	Method SendTouchEvent( type:EventType )
+	
+		Local window:=_activeWindow
+		If Not window Return
+
+		Local p:=New Vec2i( _fingerCoords.x * window.Frame.Width,_fingerCoords.y * window.Frame.Height )
+
+		Local location:=window.TransformWindowPointToView( p )
+		
+		window.SendTouchEvent( New TouchEvent( type,_activeWindow,location,_finger,_fingerPressure ) )
+	End
+	
 	Method SendWindowEvent( type:EventType )
 	
 		Local event:=New WindowEvent( type,_window )
@@ -668,6 +687,10 @@ Class AppInstance
 		SdlEventFilter( event )
 	
 		Keyboard.SendEvent( event )
+		
+'		Mouse.SendEvent( event )
+		
+		Touch.SendEvent( event )
 	
 		Select event->type
 		
@@ -845,7 +868,37 @@ Class AppInstance
 				SendMouseEvent( EventType.MouseWheel,_hoverView )
 			
 			Endif
-
+			
+		Case SDL_FINGERDOWN
+		
+			Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
+			
+			_finger=tevent->fingerId
+			_fingerPressure=tevent->pressure
+			_fingerCoords=New Vec2f( tevent->x,tevent->y )
+			
+			SendTouchEvent( EventType.TouchDown )
+		
+		Case SDL_FINGERUP
+		
+			Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
+			
+			_finger=tevent->fingerId
+			_fingerPressure=tevent->pressure
+			_fingerCoords=New Vec2f( tevent->x,tevent->y )
+			
+			SendTouchEvent( EventType.TouchUp )
+		
+		Case SDL_FINGERMOTION
+		
+			Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
+			
+			_finger=tevent->fingerId
+			_fingerPressure=tevent->pressure
+			_fingerCoords=New Vec2f( tevent->x,tevent->y )
+			
+			SendTouchEvent( EventType.TouchMove )
+		
 		Case SDL_WINDOWEVENT
 		
 			Local wevent:=Cast<SDL_WindowEvent Ptr>( event )

+ 54 - 2
modules/mojo/app/event.monkey2

@@ -42,6 +42,10 @@ Enum EventType
 	MouseEnter,
 	MouseLeave,
 
+	TouchDown,
+	TouchUp,
+	TouchMove,
+	
 	WindowClose,
 	WindowMoved,
 	WindowResized,
@@ -193,10 +197,12 @@ Class MouseEvent Extends Event
 		Return _clicks
 	End
 	
+	#rem monkeydoc Transforms mouse event to different view.
+	#end
 	Method TransformToView:MouseEvent( view:View )
-		If view=View Return self
+		If view=_view Return self
 		Local location:=view.TransformPointFromView( _location,_view )
-		Return New MouseEvent( Type,View,location,_button,_wheel,_modifiers,_clicks )
+		Return New MouseEvent( _type,view,location,_button,_wheel,_modifiers,_clicks )
 	End
 	
 	Private
@@ -208,6 +214,52 @@ Class MouseEvent Extends Event
 	Field _clicks:Int
 End
 
+#rem monkeydoc The TouchEvent class.
+#end
+Class TouchEvent Extends Event
+
+	#rem monkeydoc Creates a new touch event.
+	#end
+	Method New( type:EventType,view:View,location:Vec2i,finger:int,pressure:Float )
+		Super.New( type,view )
+		_location=location
+		_finger=finger
+		_pressure=pressure
+	End
+	
+	#rem monkeydoc Finger location in view.
+	#end
+	Property Location:Vec2i()
+		Return _location
+	End
+	
+	#rem monkeydoc Finger index (0-9).
+	#end
+	Property Finger:Int()
+		Return _finger
+	End
+	
+	#rem monkeydoc Finger pressure (0-1).
+	#end
+	Property Pressure:Float()
+		Return _pressure
+	End
+	
+	#rem monkeydoc Transforms touch event to different view.
+	#end
+	Method TransformToView:TouchEvent( view:View )
+		If view=_view return Self
+		Local location:=view.TransformPointFromView( _location,_view )
+		Return New TouchEvent( _type,view,location,_finger,_pressure )
+	End
+	
+	Private
+	
+	Field _location:Vec2i
+	Field _finger:Int
+	field _pressure:Float
+End
+
 #rem monkeydoc The WindowEvent class.
 #end
 Class WindowEvent Extends Event

+ 16 - 0
modules/mojo/app/window.monkey2

@@ -222,6 +222,13 @@ Class Window Extends View
 	
 		Return _windowsByID[id]
 	End
+	
+	#rem monkeydoc @hidden
+	#end
+	Method SendTouchEvent( event:TouchEvent )
+	
+		OnTouchEvent( event )
+	End
 
 	#rem monkeydoc @hidden
 	#end
@@ -249,6 +256,15 @@ Class Window Extends View
 		_clearColor=App.Theme.GetColor( "windowClearColor" )
 	End
 	
+	#rem monkeydoc Touch event handler.
+	
+	Called when the user touches the window on a touch compatible device.
+	
+	#end
+	Method OnTouchEvent( event:TouchEvent ) Virtual
+	
+	End
+	
 	#rem monkeydoc Window event handler.
 	
 	Called when the window is sent a window event.

+ 139 - 0
modules/mojo/input/touch.monkey2

@@ -0,0 +1,139 @@
+
+Namespace mojo.input
+
+#rem monkeydoc Global instance of the TouchDevice class.
+#end
+Const Touch:=New TouchDevice
+
+#rem monkeydoc The TouchDevice class.
+
+To access the touch device, use the global [[Touch]] constant.
+
+The touch device should only used after a new [[app.AppInstance]] is created.
+
+#end
+Class TouchDevice Extends InputDevice
+
+	Method FingerDown:Bool( finger:Int )
+		DebugAssert( finger>=0 And finger<10,"Finger index out of range" )
+		Return _fingers[finger].down
+	End
+	
+	Method FingerPressed:Bool( finger:Int )
+		DebugAssert( finger>=0 And finger<10,"Finger index out of range" )
+		Return _fingers[finger].pressed
+	End
+	
+	Method FingerReleased:Bool( finger:Int )
+		DebugAssert( finger>=0 And finger<10,"Finger index out of range" )
+		Return _fingers[finger].released
+	End
+	
+	Method FingerPressure:Float( finger:Int )
+		DebugAssert( finger>=0 And finger<10,"Finger index out of range" )
+		Return _fingers[finger].pressure
+	End
+
+	Method FingerX:Int( finger:Int )
+		DebugAssert( finger>=0 And finger<10,"Finger index out of range" )
+		Return _fingers[finger].location.x
+	End
+	
+	Method FingerY:Int( finger:Int )
+		DebugAssert( finger>=0 And finger<10,"Finger index out of range" )
+		Return _fingers[finger].location.y
+	End
+	
+	Method FingerLocation:Vec2i( finger:Int )
+		DebugAssert( finger>=0 And finger<10,"Finger index out of range" )
+		Return _fingers[finger].location
+	End
+	
+	'***** INTERNAL *****
+	
+	#rem monkeydoc @hidden
+	#end
+	Method Init()
+	End
+	
+	#rem monkeydoc @hidden
+	#end
+	Method Update()
+		For Local i:=0 Until 10
+			_fingers[i].pressed=False
+		Next
+	End
+	
+	#rem monkeydoc @hidden
+	#end
+	Method EventLocation:Vec2i( tevent:SDL_TouchFingerEvent Ptr )
+	
+		Local window:=App.ActiveWindow
+	
+		Local p:=New Vec2i( tevent->x * window.Frame.Width,tevent->y * window.Frame.Height )
+
+		Return window.TransformPointFromView( p,Null )
+	End
+	
+	#rem monkeydoc @hidden
+	#end
+	Method SendEvent( event:SDL_Event Ptr )
+	
+		If Not App.ActiveWindow Return
+
+		Select event->type
+			
+		Case SDL_FINGERDOWN
+		
+			Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
+			
+			Local id:=tevent->fingerId
+			If id>=0 And id<10
+				_fingers[id].down=True
+				_fingers[id].pressed=True
+				_fingers[id].pressure=tevent->pressure
+				_fingers[id].location=EventLocation( tevent )
+			Endif
+		
+		Case SDL_FINGERUP
+		
+			Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
+			
+			Local id:=tevent->fingerId
+			If id>=0 And id<10
+				_fingers[id].down=False
+				_fingers[id].released=False
+				_fingers[id].pressure=0
+				_fingers[id].location=EventLocation( tevent )
+			Endif
+			
+		Case SDL_FINGERMOTION
+		
+			Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
+			
+			Local id:=tevent->fingerId
+			If id>=0 And id<10
+				_fingers[id].pressure=tevent->pressure
+				_fingers[id].location=EventLocation( tevent )
+			Endif
+		
+		End
+
+	End
+	
+	Private
+	
+	Struct FingerState
+		Field down:Bool
+		Field pressed:Bool
+		Field released:Bool
+		Field pressure:FLoat
+		Field location:Vec2i
+	End
+	
+	Field _fingers:=New FingerState[10]
+	
+	Method New()
+	End
+
+End

+ 1 - 0
modules/mojo/mojo.monkey2

@@ -39,6 +39,7 @@ Namespace mojo
 #Import "input/device"
 #Import "input/keyboard"
 #Import "input/mouse"
+#Import "input/touch"
 #Import "input/joystick"
 #Import "input/keycodes"