<commit_before>module FPPrac.Events
  ( module Graphics.Gloss.Interface.Game
  , Input (..)
  , eventHandler
  )
where

import Data.Maybe (fromMaybe)
import FPPrac.Graphics
import Graphics.Gloss.Interface.Game

data EventState a = EventState { picture   :: Picture
                               , userState :: a
                               }

data Input = NoInput
           | KeyIn 				Char
					 | MouseDown    (Float,Float)
					 | MouseUp      (Float,Float)
					 | MouseDragged (Float,Float)
	deriving (Eq,Show)

eventToInput (EventKey (Char x) 								Down _ _) = KeyIn x
eventToInput (EventKey (MouseButton LeftButton) Down _ p) = MouseDown p
eventToInput (EventKey (MouseButton LeftButton) Up   _ p) = MouseUp p
eventToInput (EventMotion p)															= MouseDragged p
eventToInput _                                            = NoInput

eventHandler ::
  String
  -> (a -> Input -> (a, Maybe Picture))
  -> a
  -> IO ()
eventHandler name handler initState = gameInWindow
  name
  (800,600)
  (20,20)
  white
  50
  (EventState Blank initState)
  picture
  (\e s -> let (s',g) = handler (userState s) (eventToInput e)
           in  EventState (fromMaybe (picture s) g) s')
  (\t s -> let (s',g) = handler (userState s) NoInput
           in  EventState (fromMaybe (picture s) g) s')
  <commit_msg>Add space, enter, and tab to inputevents<commit_after>module FPPrac.Events
  ( module Graphics.Gloss.Interface.Game
  , Input (..)
  , eventHandler
  )
where

import Data.Maybe (fromMaybe)
import FPPrac.Graphics
import Graphics.Gloss.Interface.Game

data EventState a = EventState { picture   :: Picture
                               , userState :: a
                               }

data Input = NoInput
           | KeyIn 				Char
					 | MouseDown    (Float,Float)
					 | MouseUp      (Float,Float)
					 | MouseDragged (Float,Float)
	deriving (Eq,Show)

eventToInput (EventKey (Char x) 								Down _ _) = KeyIn x
eventToInput (EventKey (SpecialKey  KeySpace)   Down _ p) = KeyIn ' '
eventToInput (EventKey (SpecialKey  KeyTab)     Down _ p) = KeyIn '\t'
eventToInput (EventKey (SpecialKey  KeyEnter)   Down _ p) = KeyIn '\n'
eventToInput (EventKey (MouseButton LeftButton) Down _ p) = MouseDown p
eventToInput (EventKey (MouseButton LeftButton) Up   _ p) = MouseUp p
eventToInput (EventMotion p)															= MouseDragged p
eventToInput e                                            = NoInput

eventHandler ::
  String
  -> (a -> Input -> (a, Maybe Picture))
  -> a
  -> IO ()
eventHandler name handler initState = gameInWindow
  name
  (800,600)
  (20,20)
  white
  50
  (EventState Blank initState)
  picture
  (\e s -> let (s',g) = handler (userState s) (eventToInput e)
           in  EventState (fromMaybe (picture s) g) s')
  (\t s -> let (s',g) = handler (userState s) NoInput
           in  EventState (fromMaybe (picture s) g) s')
  
{'fraction_non_alphanumeric': 0.09947643979057591, 'fraction_numerical': 0.008376963350785341, 'mean_word_length': 2.253121452894438, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '769510', 'n_tokens_mistral': 982, 'n_tokens_neox': 887, 'n_words': 268}