blob: 9511ff5d937277921eb172a5617157e0f77a629b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
module Chelleport where
import Chelleport.AppShell (Action (AppAction, SysQuit), DrawContext, setupAppShell)
import Chelleport.Draw (renderText)
import qualified Data.Text as Text
import qualified SDL
newtype State = State
{ stateCount :: Int
}
newtype AppAction = ActionUpdateCount Int
open :: IO ()
open = setupAppShell initialState update eventToAction render
initialState :: State
initialState = State {stateCount = 0}
render :: State -> DrawContext -> IO ()
render state ctx = do
renderText ctx $ Text.pack $ "Hello" ++ show (stateCount state)
update :: State -> DrawContext -> AppAction -> IO State
update state _ctx (ActionUpdateCount count) = do
-- SDL.warpMouse SDL.WarpGlobal $ SDL.P $ SDL.V2 (unsafeCoerce $ 10 * stateCount state) 100
pure state {stateCount = count}
eventToAction :: State -> SDL.Event -> Maybe (Action AppAction)
eventToAction state event =
case SDL.eventPayload event of
SDL.KeyboardEvent ev
| isKeyPress ev SDL.KeycodeQ -> Just SysQuit
| isKeyPress ev SDL.KeycodeEscape -> Just SysQuit
| isKeyPress ev SDL.KeycodeJ -> Just $ AppAction $ ActionUpdateCount (stateCount state - 1)
| isKeyPress ev SDL.KeycodeK -> Just $ AppAction $ ActionUpdateCount (stateCount state + 1)
_ -> Nothing
isKeyPress :: SDL.KeyboardEventData -> SDL.Keycode -> Bool
isKeyPress keyboardEvent keyCode =
SDL.keyboardEventKeyMotion keyboardEvent == SDL.Pressed
&& SDL.keysymKeycode (SDL.keyboardEventKeysym keyboardEvent) == keyCode
|