Răsfoiți Sursa

Updates to keyboard/mouse; added BlahPressed/Blah/Released.

Mark Sibly 9 ani în urmă
părinte
comite
a6a18fb46d
2 a modificat fișierele cu 76 adăugiri și 43 ștergeri
  1. 40 23
      modules/mojo/input/keyboard.monkey2
  2. 36 20
      modules/mojo/input/mouse.monkey2

+ 40 - 23
modules/mojo/input/keyboard.monkey2

@@ -8,59 +8,76 @@ Class KeyboardDevice Extends InputDevice
 	#rem monkeydoc @hidden
 	#end
 	Method Reset()
-		Init()
 		For Local i:=0 Until _numKeys
-			_keyHit[i]=True
+			_pressed[i]=True
+			_released[i]=True
 		Next
 	End
 	
 	Method KeyDown:Bool( key:Key )
-		Init()
-		Return _keyMatrix[key]
+		DebugAssert( key>=0 And key<_numKeys,"Key code out of range" )
+		Return _matrix[key]
 	End
 	
-	Method KeyHit:Int( key:Key )
-		Init()
-		If _keyMatrix[key]
-			If _keyHit[key] Return False
-			_keyHit[key]=True
+	Method KeyPressed:Bool( key:Key )
+		DebugAssert( key>=0 And key<_numKeys,"Key code out of range" )
+		If _matrix[key]
+			If _pressed[key] Return False
+			_pressed[key]=True
 			Return True
 		Endif
-		_keyHit[key]=False
+		_pressed[key]=False
 		Return False
 	End
 	
+	Method KeyReleased:Bool( key:Key )
+		DebugAssert( key>=0 And key<_numKeys,"Key code out of range" )
+		If Not _matrix[key]
+			If _released[key] Return False
+			_released[key]=True
+			Return True
+		Endif
+		_released[key]=False
+		Return False
+	End
+	
+	Method KeyHit:Bool( key:Key )
+		Return KeyPressed( key )
+	End
+	
 	#rem monkeydoc @hidden
 	#end
 	Method KeyName:String( key:Key )
+		DebugAssert( key>0 And key<_numKeys,"Key code out of range" )
 		Local ikey:=Int( key )
 	
 		If ikey>=Int( Key.A ) And ikey<=Int( Key.Z ) Return String.FromChar( ikey-Int( Key.A )+65 )
-	
+
 		If ikey>=Int( Key.F1 ) And ikey<=Int( Key.F12 ) Return "F"+( ikey-Int( Key.F1 )+1 )
 		
 		Return "?"
 	End
 	
-	'Method GetButton:Bool( index:Int ) Override
-	'	Return KeyDown( Cast<Key>( index ) )
-	'End
+	'***** Internal *****
 	
+	Method Init()
+		If _init Return
+		_matrix=SDL_GetKeyboardState( Varptr _numKeys )
+		_pressed=New Bool[_numKeys]
+		_released=New Bool[_numKeys]
+		_init=True
+		Reset()
+	End
+
 	Private
 	
 	Field _init:Bool
 	Field _numKeys:Int
-	Field _keyMatrix:UByte Ptr
-	Field _keyHit:Bool[]
+	Field _matrix:UByte Ptr
+	Field _pressed:Bool[]
+	Field _released:Bool[]
 	
 	Method New()
 	End
 	
-	Method Init()
-		If _init Return
-		_keyMatrix=SDL_GetKeyboardState( Varptr _numKeys )
-		_keyHit=New Bool[_numKeys]
-		_init=True
-	End
-
 End

+ 36 - 20
modules/mojo/input/mouse.monkey2

@@ -17,8 +17,6 @@ Enum MouseButton
 	Left=1
 	Middle=2
 	Right=3
-	X1=4
-	X2=5
 End
 
 Class MouseDevice Extends InputDevice
@@ -26,9 +24,9 @@ Class MouseDevice Extends InputDevice
 	#rem monkeydoc @hidden
 	#end
 	Method Reset()
-		Init()
-		For Local i:=0 Until 6
-			_hits[i]=True
+		For Local i:=0 Until 4
+			_pressed[i]=True
+			_released[i]=True
 		Next
 	End
 	
@@ -41,26 +39,40 @@ Class MouseDevice Extends InputDevice
 	End
 
 	Property Location:Vec2i()
-		Init()
 		Return _location
 	End
 	
 	Method ButtonDown:Bool( button:MouseButton )
-		Init()
+		DebugAssert( button>=0 And button<4,"Mouse buttton out of range" )
 		Return _buttons[button]
 	End
 
-	Method ButtonHit:Bool( button:MouseButton )
-		Init()
+	Method ButtonPressed:Bool( button:MouseButton )
+		DebugAssert( button>=0 And button<4,"Mouse buttton out of range" )
 		If _buttons[button]
-			If _hits[button] Return False
-			_hits[button]=True
+			If _pressed[button] Return False
+			_pressed[button]=True
+			Return True
+		Endif
+		_pressed[button]=False
+		Return False
+	End
+	
+	Method ButtonReleased:Bool( button:MouseButton )
+		DebugAssert( button>=0 And button<4,"Mouse buttton out of range" )
+		If Not _buttons[button]
+			If _released[button] Return False
+			_released[button]=True
 			Return True
 		Endif
-		_hits[button]=False
+		_released[button]=False
 		Return False
 	End
 	
+	Method ButtonHit:Bool( button:MouseButton )
+		Return ButtonPressed( button )
+	End
+	
 	#rem monkeydoc @hidden
 	#end	
 	'Method GetButton:Bool( index:Int ) Override
@@ -73,12 +85,22 @@ Class MouseDevice Extends InputDevice
 	'	Return Location
 	'End
 	
+	'***** Internal *****
+
+	Method Init()
+		If _init Return
+		App.Idle+=Poll
+		_init=True
+		Reset()
+	End
+	
 	Private
 
 	Field _init:Bool	
 	Field _location:Vec2i
-	Field _buttons:=New Bool[6]
-	Field _hits:=New Bool[6]
+	Field _buttons:=New Bool[4]
+	Field _pressed:=New Bool[4]
+	Field _released:=New Bool[4]
 	
 	Method New()
 	End
@@ -92,10 +114,4 @@ Class MouseDevice Extends InputDevice
 		App.Idle+=Poll
 	End
 
-	Method Init()
-		If _init Return
-		App.Idle+=Poll
-		_init=True
-	End
-	
 End