Portability | GHC only |
---|---|
Stability | experimental |
Maintainer | Michael Sloan <mgsloan@gmail.com> |
The Gtk Toy Framework is a wrapper over Gtk for conveniently creating applications which draw things and use the mouse or keyboard.
It handles the minutiae of setting up the Gtk window and canvas, and processes mouse and keyboard inputs into more palatable data structures.
- type KeyInfo = (Bool, Int, [Modifier])
- type KeyTable = Map String KeyInfo
- type MouseEvent = Maybe (Bool, Int)
- type KeyEvent = (Bool, Either String Char)
- data InputState = InputState {}
- class Interactive a where
- tick :: InputState -> a -> IO (a, Bool)
- mouse :: MouseEvent -> InputState -> a -> IO a
- keyboard :: KeyEvent -> InputState -> a -> IO a
- class Interactive a => GtkInteractive a where
- display :: DrawWindow -> InputState -> a -> IO a
- runToy :: GtkInteractive a => a -> IO ()
- quitToy :: IO ()
- keyInfo :: String -> InputState -> Maybe KeyInfo
- keyHeld :: String -> InputState -> Bool
- mouseHeld :: Int -> InputState -> Bool
- simpleTick :: (a -> a) -> InputState -> a -> IO (a, Bool)
- simpleDisplay :: (DrawWindow -> a -> a) -> DrawWindow -> InputState -> a -> IO a
- simpleMouse :: (MouseEvent -> (Double, Double) -> a -> a) -> MouseEvent -> InputState -> a -> IO a
- simpleKeyboard :: (KeyEvent -> a -> a) -> KeyEvent -> InputState -> a -> IO a
- quitKeyboard :: KeyEvent -> InputState -> a -> IO a
Documentation
type KeyInfo = (Bool, Int, [Modifier])Source
Information about the most recent key-state transition. The tuple contains whether the button was pressed, at what time in msec, and with which GTK modifiers.
type KeyTable = Map String KeyInfoSource
A map of GTK keynames to last-received event regarding each respective key. This can be interpreted as the current keyboard state - a key is down if it was last seen being pressed.
type MouseEvent = Maybe (Bool, Int)Source
A MouseEvent
is Nothing
if it's a mouse motion event, and otherwise
provides mouse press information.
type KeyEvent = (Bool, Either String Char)Source
A KeyEvent
tuple specifies whether the key was pressed or not, and
which key was pressed. Right Char
is yielded for keys which would
normally correspond to character insertions, while Left String
provides
GTK-convention names for the rest.
data InputState Source
class Interactive a whereSource
A class for things which change within an interactive context. The default method implementations do nothing.
tick :: InputState -> a -> IO (a, Bool)Source
tick
is (ideally) called every 30ms. The bool result indicates if the
graphics need to be refreshed.
mouse :: MouseEvent -> InputState -> a -> IO aSource
mouse
is called when the mouse moves or presses occur.
keyboard :: KeyEvent -> InputState -> a -> IO aSource
keyboard
is called on key-presses.
class Interactive a => GtkInteractive a whereSource
display :: DrawWindow -> InputState -> a -> IO aSource
display
is called when the rendering needs to be refreshed.
runToy :: GtkInteractive a => a -> IO ()Source
Main program entrypoint. This is how you turn an instance of Interactive into an application.
InputState Accessors
keyInfo :: String -> InputState -> Maybe KeyInfoSource
the information for the most recent key event of the named key.
keyHeld :: String -> InputState -> BoolSource
Gets whether the named key is held down.
mouseHeld :: Int -> InputState -> BoolSource
Whether the indicated mouse button is considered pressed in the InputState.
Utilities
Functions to allow for writing simpler, pure implementations of the different members of Interactive.
simpleTick :: (a -> a) -> InputState -> a -> IO (a, Bool)Source
Converts a pure state transform to a function for Interactive tick
.
simpleDisplay :: (DrawWindow -> a -> a) -> DrawWindow -> InputState -> a -> IO aSource
Converts a diagram projection to a function for Interactive display
.
simpleMouse :: (MouseEvent -> (Double, Double) -> a -> a) -> MouseEvent -> InputState -> a -> IO aSource
Converts a function which responds to mouse-presses, and transforms state
accordingly to a function for Interactive mouse
.
simpleKeyboard :: (KeyEvent -> a -> a) -> KeyEvent -> InputState -> a -> IO aSource
Converts a function which responds to key-presses, and transforms state
accordingly to a function for Interactive keyboard
.
quitKeyboard :: KeyEvent -> InputState -> a -> IO aSource
A definition for the keyboard handler that just calls quitToy when Escape is pressed.