Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Nick = Int
- data Pos = Pos Double Double
- data Sz = Sz Double Double
- data Box = Box Pos Sz
- data Doodle =
- Doodle { titled :: Text
- , sized :: Sz
- , widgets :: Map Nick Widget
- , displayed :: Nick
- , kbFocus :: KbFocus
- }
- data KbFocus = KbFocus [Nick] Int -- which has focus
- data Render c => Widget b c =
- Widget { box :: Box
- , thing :: c
- , handleKey :: Key -> c -> (b, c)
- , handleMouse :: Mouse -> c -> (b, c)
- }
- kbFocused :: DoodleIO -> Nick
- kbFocused io = do
- d <- readIORef io
- return (case kbFocus d of
- KbFocus ns i -> ns !! i)
- runDoodle :: Doodle -> IO ()
- runDoodle doodle = do
- let doodleIO = newIORef doodle
- in bananaSDL (titled doodle) $ \ah -> do
- sdl <- fromAddHandler ah
- keys <- filter isKey sdl
- mice <- filter isMouse sdl
- doodleB <- doodleIO
- -- a behavior meant to be the widget to recieve key events
- kbw <- kbFocused <@> doodleB
- (h1, lens1) <- lookupKeyHandler kbw <$> doodleB
- applyHandler lens1 h1 doodleB
- -- the handler gives the new value
- -- the doodleB is updated by the lens with the new value
- -- figure out who is hit
- hby <- whoHitBy <@> mice
- (h2, lens2) <- lookupMouseHandler doodleB
- -- lookup hit widgets handler and state
- -- apply the handler to the state with the event
- -- return the events raised and the new state
- applyHandler lens2 h2 doodleB
- reactimate' $ draw <$> changes doodleB
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement