Bladeren bron

Added mojo input files.

Mark Sibly 9 jaren geleden
bovenliggende
commit
19a406a8a3

+ 24 - 0
modules/mojo/input/device.monkey2

@@ -0,0 +1,24 @@
+
+Namespace mojo.input
+
+Class InputDevice
+
+	#rem monkeydoc @hidden
+	#end
+'	Method GetAxis:Float( index:Int ) Virtual
+'		Return 0
+'	End
+	
+	#rem monkeydoc @hidden
+	#end
+'	Method GetButton:Bool( index:Int ) Virtual
+'		Return 0
+'	End
+	
+	#rem monkeydoc @hidden
+	#end
+'	Method GetPointer:Vec2i( index:Int ) Virtual
+'		Return New Vec2i
+'	End
+
+End

+ 135 - 0
modules/mojo/input/joystick.monkey2

@@ -0,0 +1,135 @@
+
+Namespace mojo.input
+
+#rem monkeydoc
+
+| JoystickHat value	| 
+|:------------------|
+| Centered
+| Up
+| Right
+| Down
+| Left
+| RightUp
+| RightDown
+| LeftUp
+| LeftDown
+
+#end
+Enum JoystickHat	'SDL values...
+	Centered=0
+	Up=1
+	Right=2
+	Down=4
+	Left=8
+	RightUp=Right|Up
+	RightDown=Right|Down
+	LeftUp=Left|Up
+	LeftDown=Left|Down
+End
+
+Class JoystickDevice Extends InputDevice
+
+	Property Name:String()
+		Return _name
+	End
+	
+	Property GUID:String()
+		Return _guid
+	End
+
+	Property NumAxes:Int()
+		Return _numAxes
+	End
+	
+	Property NumBalls:Int()
+		Return _numBalls
+	End
+	
+	Property NumButtons:Int()
+		Return _numButtons
+	End
+	
+	Property NumHats:Int()
+		Return _numHats
+	End
+	
+	Method GetAxis:Float( axis:Int )
+		Return (Float(SDL_JoystickGetAxis( _joystick,axis ))+32768)/32767.5-1
+	End
+	
+	Method GetBall:Vec2i( ball:Int )
+		Local x:Int,y:Int
+		SDL_JoystickGetBall( _joystick,ball,Varptr x,Varptr y )
+		Return New Vec2i( x,y )
+	End
+
+	Method GetHat:JoystickHat( hat:Int )
+		Return Cast<JoystickHat>( SDL_JoystickGetHat( _joystick,hat ) )
+	End
+
+	Method ButtonDown:Bool( button:Int )
+		Return SDL_JoystickGetButton( _joystick,button )
+	End
+	
+	Method ButtonHit:Bool( button:Int )
+		If ButtonDown( button )
+			If _hits[button] Return False
+			_hits[button]=True
+			Return True
+		Endif
+		_hits[button]=False
+		Return False
+	End
+	
+	Function NumJoysticks:Int()
+		Return Min( SDL_NumJoysticks(),8 )
+	End
+
+	#rem  monkeydoc @hidden
+	#end	
+	Function UpdateJoysticks()
+		SDL_JoystickUpdate()
+	End
+	
+	Function Open:JoystickDevice( index:Int )
+		Assert( index>=0 And index<8 )
+		Local joystick:=_joysticks[index]
+		If Not joystick
+			Local sdlJoystick:=SDL_JoystickOpen( index )
+			If Not sdlJoystick Return Null
+			joystick=New JoystickDevice( sdlJoystick )
+			_joysticks[index]=joystick
+		Endif
+		Return joystick
+	End
+	
+	Private
+	
+	Global _joysticks:=New JoystickDevice[8]
+	
+	Field _joystick:SDL_Joystick Ptr
+	Field _name:String
+	Field _guid:String
+	Field _numAxes:Int
+	Field _numBalls:Int
+	Field _numButtons:Int
+	Field _numHats:Int
+	Field _hits:=New Bool[32]
+	
+	Method New( joystick:SDL_Joystick Ptr )
+		_joystick=joystick
+		_name=String.FromCString( SDL_JoystickName( _joystick ) )
+		_numAxes=SDL_JoystickNumAxes( _joystick )
+		_numBalls=SDL_JoystickNumBalls( _joystick )
+		_numButtons=SDL_JoystickNumButtons( _joystick )
+		_numHats=SDL_JoystickNumHats( _joystick )
+		
+		Local buf:=New Byte[64]
+		Local guid:=SDL_JoystickGetGUID( _joystick )
+		SDL_JoystickGetGUIDString( guid,Cast<libc.char_t Ptr>( buf.Data ),buf.Length )
+		buf[buf.Length-1]=0
+		_guid=String.FromCString( buf.Data )
+	End
+
+End

+ 64 - 0
modules/mojo/input/keyboard.monkey2

@@ -0,0 +1,64 @@
+
+Namespace mojo.input
+
+Global Keyboard:=New KeyboardDevice
+
+Class KeyboardDevice Extends InputDevice
+
+	Method Reset()
+		Init()
+		For Local i:=0 Until _numKeys
+			_keyHit[i]=True
+		Next
+	End
+	
+	Method KeyDown:Bool( key:Key )
+		Init()
+		Return _keyMatrix[key]
+	End
+	
+	Method KeyHit:Int( key:Key )
+		Init()
+		If _keyMatrix[key]
+			If _keyHit[key] Return False
+			_keyHit[key]=True
+			Return True
+		Endif
+		_keyHit[key]=False
+		Return False
+	End
+	
+	#rem monkeydoc @hidden
+	#end
+	Method KeyName:String( key:Key )
+		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
+	
+	Private
+	
+	Field _init:Bool
+	Field _numKeys:Int
+	Field _keyMatrix:UByte Ptr
+	Field _keyHit:Bool[]
+	
+	Method New()
+	End
+	
+	Method Init()
+		If _init Return
+		_keyMatrix=SDL_GetKeyboardState( Varptr _numKeys )
+		_keyHit=New Bool[_numKeys]
+		_init=True
+	End
+
+End

+ 170 - 0
modules/mojo/input/keycodes.monkey2

@@ -0,0 +1,170 @@
+
+Namespace mojo.input
+
+'These are actually SDL 'scan codes', ie: what's written on US keyboard keys...
+'
+#rem monkeydoc Key codes.
+
+| Key
+|:---
+| A
+| B
+| C
+| D
+| E
+| F
+| G
+| H
+| I
+| J 
+| K
+| L
+| M
+| N
+| O
+| P
+| Q
+| R
+| S
+| T
+| U
+| V
+| W
+| X
+| Y
+| Z
+| Key0
+| Key1
+| Key2
+| Key3
+| Key4
+| Key5
+| Key6
+| Key7
+| Key8
+| Key9
+| Enter
+| Escape
+| Backspace
+| Tab
+| Space
+| Minus
+| Equals
+| LeftBracket
+| RightBracket
+| Backslash
+| Semicolon
+| Apostrophe
+| Grave
+| Comma
+| Period
+| Slash
+| CapsLock
+| F1
+| F2
+| F3
+| F4
+| F5
+| F6
+| F7
+| F8
+| F9
+| F10
+| F11
+| F12
+| PrintScreem
+| ScrollLock
+| Pause
+| Insert
+| Home
+| PageUp
+| KeyDelete
+| KeyEnd
+| PageDown
+| Right
+| Left
+| Down
+| Up
+| LeftControl
+| LeftShift
+| LeftAlt
+| LeftGui
+| RightControl
+| RightShift
+| RightAlt
+| RightGui
+
+#end
+Enum Key
+
+	None=0
+
+	A=4,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
+	
+	Key1=30,Key2,Key3,Key4,Key5,Key6,Key7,Key8,Key9,Key0
+	
+	Enter=40,Escape,Backspace,Tab,Space
+	
+	Minus=45,Equals,LeftBracket,RightBracket,Blackslash
+	
+	Semicolon=51,Apostrophe,Grave,Comma,Period,Slash
+	
+	CapsLock=57
+	
+	F1=58,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12
+	
+	PrintScreen=70,ScrollLock,Pause,Insert
+	
+	Home=74,PageUp,KeyDelete,KeyEnd,PageDown,Right,Left,Down,Up
+
+	LeftControl=224,LeftShift,LeftAlt,LeftGui
+	
+	RightControl=228,RightShift,RightAlt,RightGui
+
+End
+
+#rem monkeydoc @hidden
+#end
+Enum ScanCode
+End
+
+#rem monkeydoc Modifier masks.
+
+| Modifier 		| Description 
+|:--------------|:-----------
+| LeftShift		| Left shift key.
+| RightShift	| Right shift key.
+| LeftControl	| Left control key.
+| RightControl	| Right control key.
+| LeftAlt		| Left alt key.
+| RightAlt		| Right alt key.
+| LeftGui		| Left gui key.
+| RightGui		| Right gui key.
+| NumLock		| Num lock key.
+| CapsLock		| Caps lock key.
+| Shift			| LeftShit | RightShift mask.
+| Control		| LeftControl | RightControl mask.
+| Alt			| LeftAlt | RightAlt mask.
+| Gui			| LeftGui | RightGui mask.
+
+#end
+Enum Modifier
+
+	None=			$0000
+	LeftShift=		$0001
+	RightShift=		$0002
+	LeftControl=	$0040
+	RightControl=	$0080
+	LeftAlt=		$0100
+	RightAlt=		$0200
+	LeftGui=		$0400
+	RightGui=		$0800
+	NumLock=		$1000
+	CapsLock=		$2000
+	
+	Shift=			LeftShift|RightShift
+	Control=		LeftControl|RightControl
+	Alt=			LeftAlt|RightAlt
+	Gui=			LeftGui|RightGui
+End
+

+ 99 - 0
modules/mojo/input/mouse.monkey2

@@ -0,0 +1,99 @@
+
+Namespace mojo.input
+
+Global Mouse:=New MouseDevice
+
+#rem monkeydoc Mouse buttons.
+
+| MouseButton	| Description
+|:--------------|------------
+| Left			| Left mouse button.
+| Middle		| Middle mouse button.
+| Right			| Right mouse button.
+
+#end
+Enum MouseButton
+	None=0
+	Left=1
+	Middle=2
+	Right=3
+	X1=4
+	X2=5
+End
+
+Class MouseDevice Extends InputDevice
+
+	Method Reset()
+		Init()
+		For Local i:=0 Until 6
+			_hits[i]=True
+		Next
+	End
+	
+	Property X:Int()
+		Return Location.x
+	End
+	
+	Property Y:Int()
+		Return Location.y
+	End
+
+	Property Location:Vec2i()
+		Init()
+		Return _location
+	End
+	
+	Method ButtonDown:Bool( button:MouseButton )
+		Init()
+		Return _buttons[button]
+	End
+
+	Method ButtonHit:Bool( button:MouseButton )
+		Init()
+		If _buttons[button]
+			If _hits[button] Return False
+			_hits[button]=True
+			Return True
+		Endif
+		_hits[button]=False
+		Return False
+	End
+	
+	#rem monkeydoc @hidden
+	#end	
+	'Method GetButton:Bool( index:Int ) Override
+	'	Return ButtonDown( Cast<MouseButton>( index ) )
+	'End
+	
+	#rem monkeydoc @hidden
+	#end	
+	'Method GetPointer:Vec2i( index:Int ) Override
+	'	Return Location
+	'End
+	
+	Private
+
+	Field _init:Bool	
+	Field _location:Vec2i
+	Field _buttons:=New Bool[6]
+	Field _hits:=New Bool[6]
+	
+	Method New()
+	End
+	
+	Method Poll()
+		Local mask:=SDL_GetMouseState( Varptr _location.x,Varptr _location.y )
+		If App.ActiveWindow _location=App.ActiveWindow.TransformPointFromView( _location,Null )
+		_buttons[MouseButton.Left]=mask & 1
+		_buttons[MouseButton.Middle]=mask & 2
+		_buttons[MouseButton.Right]=mask & 4
+		App.Idle+=Poll
+	End
+
+	Method Init()
+		If _init Return
+		App.Idle+=Poll
+		_init=True
+	End
+	
+End