瀏覽代碼

Fixed KeyPressed/KeyReleased so they work on timers and added App.ResetPolledInput().

Mark Sibly 8 年之前
父節點
當前提交
11d773644e
共有 3 個文件被更改,包括 68 次插入28 次删除
  1. 10 0
      modules/mojo/app/app.monkey2
  2. 36 17
      modules/mojo/input/keyboard.monkey2
  3. 22 11
      modules/mojo/input/mouse.monkey2

+ 10 - 0
modules/mojo/app/app.monkey2

@@ -514,6 +514,16 @@ Class AppInstance
 		
 	End
 	
+	#rem monkeydoc Resets polled mouse and keyboard devices.
+	
+	Clears the keyboard character queue and clears the pressed and released states for all keys and mouse buttons.
+	
+	#end
+	Method ResetPolledInput()
+		Keyboard.Reset()
+		Mouse.Reset()
+	End
+	
 	#rem monkeydoc @hidden
 	#end
 	Function EmscriptenMainLoop()

+ 36 - 17
modules/mojo/input/keyboard.monkey2

@@ -103,16 +103,28 @@ Class KeyboardDevice Extends InputDevice
 
 	If `key` is a raw key, the state of the key as it is physically positioned on US keyboards is returned.
 	
+	if `repeating` is true, then key repeats are included.
+	
 	@param key Key to check.
 	
 	#end
 	Method KeyPressed:Bool( key:Key,repeating:Bool=False )
-	
+		
 		Local scode:=ScanCode( key )
 		
-		If repeating Return _keys[scode].rpressed=_frame
+		If repeating 
+			Local pressed:=_keys[scode].rpressed<>0
+			
+			_keys[scode].rpressed=0
+			
+			Return pressed
+		Endif
+
+		Local pressed:=_keys[scode].pressed<>0
+		
+		_keys[scode].pressed=0
 		
-		Return _keys[scode].pressed=_frame
+		Return pressed
 	End
 
 	#rem monkeydoc Checks if a key was released.
@@ -128,7 +140,11 @@ Class KeyboardDevice Extends InputDevice
 	
 		Local scode:=ScanCode( key )
 		
-		Return _keys[scode].released=_frame
+		Local released:=_keys[scode].released<>0
+		
+		_keys[scode].released=0
+		
+		Return released
 	End
 	
 	#rem monkeydoc @hidden
@@ -154,16 +170,19 @@ Class KeyboardDevice Extends InputDevice
 	End
 	
 	#rem monkeydoc Flushes the character queue.
+	
+	Removes all queued characters in the character queue.
+	
+	Note that [[app.App.ResetPolledInput]] also flushes the character queue.
+	
 	#end
 	Method FlushChars()
 		_charPut=0
 		_charGet=0
 	End
 	
-	'***** Internal *****
+	Internal
 	
-	#rem monkeydoc @hidden
-	#end
 	Method ScanCode:Int( key:Key )
 		If key & Key.Raw 
 			key&=~Key.Raw
@@ -174,23 +193,17 @@ Class KeyboardDevice Extends InputDevice
 		Return _key2scan[ key ]
 	End
 	
-	#rem monkeydoc @hidden
-	#end
 	Method KeyCodeToKey:Key( keyCode:Int )
 		If (keyCode & $40000000) keyCode=(keyCode & ~$40000000)+$80
 		If keyCode<=0 Or keyCode>=Int( Key.Max ) Return Null
 		Return Cast<Key>( keyCode )
 	End
 	
-	#rem monkeydoc @hidden
-	#end
 	Method ScanCodeToRawKey:Key( scanCode:Int )
 		If scanCode<=0 Or scanCode>=512 Return null
 		Return _scan2raw[ scanCode ]
 	End
 	
-	#rem monkeydoc @hidden
-	#end
 	Method Init()
 		Local p:=bbKeyInfos
 		
@@ -218,15 +231,11 @@ Class KeyboardDevice Extends InputDevice
 
 	End
 	
-	#rem monkeydoc @hidden
-	#end
 	Method Update()
 		_frame+=1
 		FlushChars()
 	End
 	
-	#rem monkeydoc @hidden
-	#end
 	Method SendEvent( event:SDL_Event Ptr )
 	
 		Select event->type
@@ -296,6 +305,16 @@ Class KeyboardDevice Extends InputDevice
 		End
 
 	End
+	
+	Method Reset()
+		_charPut=0
+		_charGet=0
+		For Local i:=0 Until 512
+			_keys[i].pressed=0
+			_keys[i].rpressed=0
+			_keys[i].released=0
+		Next
+	End
 
 	Private
 	

+ 22 - 11
modules/mojo/input/mouse.monkey2

@@ -153,7 +153,12 @@ Class MouseDevice Extends InputDevice
 	#end
 	Method ButtonPressed:Bool( button:MouseButton )
 		DebugAssert( button>=0 And button<4,"Mouse button out of range" )
-		Return _pressed[button]
+		
+		Local pressed:=_pressed[button]
+		
+		_pressed[button]=False
+		
+		Return pressed
 	End
 	
 	#rem monkeydoc Checks if a mouse button was released.
@@ -165,7 +170,12 @@ Class MouseDevice Extends InputDevice
 	#end
 	Method ButtonReleased:Bool( button:MouseButton )
 		DebugAssert( button>=0 And button<4,"Mouse button out of range" )
-		Return _released[button]
+		
+		Local released:=_released[button]
+		
+		_released[button]=False
+		
+		Return released
 	End
 	
 	#rem monkeydoc @hidden
@@ -174,10 +184,8 @@ Class MouseDevice Extends InputDevice
 		Return ButtonPressed( button )
 	End
 
-	'***** Internal *****
+	Internal
 
-	#rem monkeydoc @hidden
-	#end
 	Method Init()
 	
 		_cursors=New SDL_Cursor Ptr[ MouseCursor.Max ]
@@ -192,8 +200,6 @@ Class MouseDevice Extends InputDevice
 		SDL_SetCursor( _cursors[_cursor] )
 	End
 	
-	#rem monkeydoc @hidden
-	#end
 	Method Update()
 	
 		Local mask:=SDL_GetMouseState( Varptr _location.x,Varptr _location.y )
@@ -206,8 +212,6 @@ Class MouseDevice Extends InputDevice
 		_wheel=Null
 	End
 	
-	#rem monkeydoc @hidden
-	#end
 	Method SendEvent( event:SDL_Event Ptr )
 	
 		Select event->type
@@ -224,6 +228,13 @@ Class MouseDevice Extends InputDevice
 		End
 	End
 	
+	Method Reset()
+		For Local i:=0 Until 4
+			_pressed[i]=False
+			_released[i]=false
+		next
+	End
+	
 	Private
 
 	Field _init:Bool	
@@ -239,8 +250,8 @@ Class MouseDevice Extends InputDevice
 	End
 	
 	Method UpdateButton( button:MouseButton,down:Bool )
-		_pressed[button]=False
-		_released[button]=False
+		'_pressed[button]=False
+		'_released[button]=False
 		If down=_down[button] Return
 		If down _pressed[button]=True Else _released[button]=True
 		_down[button]=down