#15 Basic input handling
This commit is contained in:
		
							parent
							
								
									9e5f22458c
								
							
						
					
					
						commit
						cdd0c3989c
					
				
					 4 changed files with 70 additions and 7 deletions
				
			
		|  | @ -7,6 +7,8 @@ module RPGEngine | |||
| 
 | ||||
| import Game | ||||
| import Render | ||||
| import Input | ||||
| 
 | ||||
| import Graphics.Gloss ( | ||||
|     Color(..) | ||||
|     , white | ||||
|  | @ -23,10 +25,6 @@ winDimensions = (1280, 720) | |||
| winOffsets :: (Int, Int) | ||||
| winOffsets = (0, 0) | ||||
| 
 | ||||
| -- Game background color | ||||
| bgColor :: Color | ||||
| bgColor = white | ||||
| 
 | ||||
| ---------------------------------------------------------------------- | ||||
| 
 | ||||
| -- This is the gameloop. | ||||
|  | @ -34,6 +32,6 @@ bgColor = white | |||
| playRPGEngine :: String -> Int -> IO() | ||||
| playRPGEngine title fps = do  | ||||
|     play window bgColor fps initGame render handleInputs step | ||||
|     where window           = initWindow title winDimensions winOffsets | ||||
|           step         _ g = g -- TODO Do something with step? | ||||
|           handleInputs _ g = g -- TODO Implement inputHanlders | ||||
|     where window       = initWindow title winDimensions winOffsets | ||||
|           step _ g     = g -- TODO Do something with step? | ||||
|           handleInputs = handleAllInput | ||||
|  |  | |||
							
								
								
									
										23
									
								
								lib/control/Input.hs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								lib/control/Input.hs
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,23 @@ | |||
| module Input | ||||
| ( | ||||
| -- Handle all input for RPG-Engine | ||||
| handleAllInput | ||||
| ) where | ||||
| 
 | ||||
| import Game | ||||
| import State | ||||
| import InputHandling | ||||
| 
 | ||||
| import Graphics.Gloss.Interface.IO.Game | ||||
| 
 | ||||
| ---------------------------------------------------------------------- | ||||
| 
 | ||||
| handleAllInput :: InputHandler Game | ||||
| handleAllInput = composeInputHandlers [ | ||||
|     handleSpecialKey KeySpace setNextState | ||||
|     ] | ||||
| 
 | ||||
| -- Go to the next stage of the Game | ||||
| setNextState :: Game -> Game | ||||
| setNextState game = game{ state = newState } | ||||
|     where newState = nextState $ state game | ||||
							
								
								
									
										41
									
								
								lib/control/InputHandling.hs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								lib/control/InputHandling.hs
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,41 @@ | |||
| -- Allows to create a massive inputHandler that can handle anything | ||||
| -- after you specify what you want it to do. | ||||
| 
 | ||||
| module InputHandling | ||||
| ( InputHandler(..), | ||||
| -- Compose multiple InputHandlers into one InputHandler that handles | ||||
| -- all of them. | ||||
| composeInputHandlers, | ||||
| 
 | ||||
| handle,  | ||||
| handleSpecialKey | ||||
| ) where | ||||
| 
 | ||||
| import Graphics.Gloss.Interface.IO.Game | ||||
| 
 | ||||
| ----------------------------- Constants ------------------------------ | ||||
| 
 | ||||
| type InputHandler a = Event -> (a -> a) | ||||
| 
 | ||||
| ---------------------------------------------------------------------- | ||||
| 
 | ||||
| composeInputHandlers :: [InputHandler a] -> InputHandler a | ||||
| composeInputHandlers []       ev a = a | ||||
| composeInputHandlers (ih:ihs) ev a = composeInputHandlers ihs ev (ih ev a) | ||||
| 
 | ||||
| handle :: Event -> (a -> a) -> Event -> (a -> a) | ||||
| handle (EventKey key _ _ _) = handleKey key | ||||
| -- handle (EventMotion _)      = undefined | ||||
| -- handle (EventResize _)      = undefined | ||||
| handle _                    = (\_ -> (\_ -> id)) | ||||
| 
 | ||||
| handleKey :: Key -> (a -> a) -> Event -> (a -> a) | ||||
| handleKey (SpecialKey  key) = handleSpecialKey key | ||||
| handleKey (Char        _  ) = (\_ -> (\_ -> id)) | ||||
| handleKey (MouseButton _  ) = (\_ -> (\_ -> id)) | ||||
| 
 | ||||
| handleSpecialKey :: SpecialKey -> (a -> a) -> Event -> (a -> a) | ||||
| handleSpecialKey sk1 f (EventKey (SpecialKey sk2) Down _ _) | ||||
|     | sk1 == sk2 = f | ||||
|     | otherwise  = id | ||||
| handleSpecialKey _   _ _ = id | ||||
		Reference in a new issue