| 
									
										
										
										
											2009-05-08 14:00:34 -04:00
										 |  |  | USING: arrays accessors continuations kernel math system | 
					
						
							| 
									
										
										
										
											2008-12-17 20:52:47 -05:00
										 |  |  | sequences namespaces init vocabs vocabs.loader combinators ;
 | 
					
						
							| 
									
										
										
										
											2008-07-19 14:04:07 -04:00
										 |  |  | IN: game-input | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-26 00:25:46 -04:00
										 |  |  | SYMBOLS: game-input-backend game-input-opened ;
 | 
					
						
							| 
									
										
										
										
											2008-07-19 14:04:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-08 14:00:34 -04:00
										 |  |  | game-input-opened [ 0 ] initialize
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-26 00:25:46 -04:00
										 |  |  | HOOK: (open-game-input)  game-input-backend ( -- )
 | 
					
						
							|  |  |  | HOOK: (close-game-input) game-input-backend ( -- )
 | 
					
						
							| 
									
										
										
										
											2008-07-29 22:53:00 -04:00
										 |  |  | HOOK: (reset-game-input) game-input-backend ( -- )
 | 
					
						
							| 
									
										
										
										
											2008-07-26 00:25:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-08 14:00:34 -04:00
										 |  |  | HOOK: get-controllers game-input-backend ( -- sequence )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: product-string game-input-backend ( controller -- string )
 | 
					
						
							|  |  |  | HOOK: product-id game-input-backend ( controller -- id )
 | 
					
						
							|  |  |  | HOOK: instance-id game-input-backend ( controller -- id )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: read-controller game-input-backend ( controller -- controller-state )
 | 
					
						
							|  |  |  | HOOK: calibrate-controller game-input-backend ( controller -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: read-keyboard game-input-backend ( -- keyboard-state )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: read-mouse game-input-backend ( -- mouse-state )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: reset-mouse game-input-backend ( -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-26 00:25:46 -04:00
										 |  |  | : game-input-opened? ( -- ? )
 | 
					
						
							| 
									
										
										
										
											2009-05-08 14:00:34 -04:00
										 |  |  |     game-input-opened get zero? not ;
 | 
					
						
							| 
									
										
										
										
											2008-07-26 00:25:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-29 01:42:28 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-29 22:53:00 -04:00
										 |  |  | M: f (reset-game-input) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-29 01:42:28 -04:00
										 |  |  | : reset-game-input ( -- )
 | 
					
						
							| 
									
										
										
										
											2008-07-29 22:53:00 -04:00
										 |  |  |     (reset-game-input) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-29 01:42:28 -04:00
										 |  |  | [ reset-game-input ] "game-input" add-init-hook | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-08 14:00:34 -04:00
										 |  |  | ERROR: game-input-not-open ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-26 00:25:46 -04:00
										 |  |  | : open-game-input ( -- )
 | 
					
						
							|  |  |  |     game-input-opened? [ | 
					
						
							|  |  |  |         (open-game-input)  | 
					
						
							| 
									
										
										
										
											2009-05-08 14:00:34 -04:00
										 |  |  |     ] unless
 | 
					
						
							| 
									
										
										
										
											2009-08-13 20:21:44 -04:00
										 |  |  |     game-input-opened [ 1 + ] change-global
 | 
					
						
							| 
									
										
										
										
											2009-05-08 14:00:34 -04:00
										 |  |  |     reset-mouse ;
 | 
					
						
							| 
									
										
										
										
											2008-07-26 00:25:46 -04:00
										 |  |  | : close-game-input ( -- )
 | 
					
						
							| 
									
										
										
										
											2009-05-08 14:00:34 -04:00
										 |  |  |     game-input-opened [ | 
					
						
							|  |  |  |         dup zero? [ game-input-not-open ] when
 | 
					
						
							| 
									
										
										
										
											2009-08-13 20:21:44 -04:00
										 |  |  |         1 -
 | 
					
						
							| 
									
										
										
										
											2009-05-08 14:00:34 -04:00
										 |  |  |     ] change-global
 | 
					
						
							| 
									
										
										
										
											2008-07-26 00:25:46 -04:00
										 |  |  |     game-input-opened? [ | 
					
						
							|  |  |  |         (close-game-input)  | 
					
						
							| 
									
										
										
										
											2008-07-29 01:42:28 -04:00
										 |  |  |         reset-game-input | 
					
						
							| 
									
										
										
										
											2009-05-08 18:22:04 -04:00
										 |  |  |     ] unless ;
 | 
					
						
							| 
									
										
										
										
											2008-07-19 14:04:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : with-game-input ( quot -- )
 | 
					
						
							| 
									
										
										
										
											2009-03-24 04:58:11 -04:00
										 |  |  |     open-game-input [ close-game-input ] [ ] cleanup ; inline
 | 
					
						
							| 
									
										
										
										
											2008-07-19 14:04:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | TUPLE: controller handle ;
 | 
					
						
							|  |  |  | TUPLE: controller-state x y z rx ry rz slider pov buttons ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: controller-state clone | 
					
						
							|  |  |  |     call-next-method dup buttons>> clone >>buttons ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYMBOLS:
 | 
					
						
							|  |  |  |     pov-neutral | 
					
						
							| 
									
										
										
										
											2008-07-19 18:17:12 -04:00
										 |  |  |     pov-up pov-up-right pov-right pov-down-right | 
					
						
							|  |  |  |     pov-down pov-down-left pov-left pov-up-left ;
 | 
					
						
							| 
									
										
										
										
											2008-07-19 14:04:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-20 21:04:47 -04:00
										 |  |  | : find-controller-products ( product-id -- sequence )
 | 
					
						
							|  |  |  |     get-controllers [ product-id = ] with filter ;
 | 
					
						
							|  |  |  | : find-controller-instance ( product-id instance-id -- controller/f )
 | 
					
						
							|  |  |  |     get-controllers [ | 
					
						
							| 
									
										
										
										
											2008-12-17 20:52:47 -05:00
										 |  |  |         tuck | 
					
						
							| 
									
										
										
										
											2008-07-20 21:04:47 -04:00
										 |  |  |         [ product-id  = ] | 
					
						
							| 
									
										
										
										
											2008-12-17 20:52:47 -05:00
										 |  |  |         [ instance-id = ] 2bi* and
 | 
					
						
							|  |  |  |     ] with with find nip ;
 | 
					
						
							| 
									
										
										
										
											2008-07-19 14:04:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | TUPLE: keyboard-state keys ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: keyboard-state clone | 
					
						
							|  |  |  |     call-next-method dup keys>> clone >>keys ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-05 10:45:43 -04:00
										 |  |  | TUPLE: mouse-state dx dy scroll-dx scroll-dy buttons ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: mouse-state clone | 
					
						
							|  |  |  |     call-next-method dup buttons>> clone >>buttons ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-16 02:33:51 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     { [ os windows? ] [ "game-input.dinput" require ] } | 
					
						
							|  |  |  |     { [ os macosx? ] [ "game-input.iokit" require ] } | 
					
						
							|  |  |  |     { [ t ] [ ] } | 
					
						
							|  |  |  | } cond
 |