Advertisement
Guest User

Untitled

a guest
Jul 10th, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. type Nick = Int
  2. data Pos = Pos Double Double
  3. data Sz = Sz Double Double
  4. data Box = Box Pos Sz
  5.  
  6. data Doodle =
  7. Doodle { titled :: Text
  8. , sized :: Sz
  9. , widgets :: Map Nick Widget
  10. , displayed :: Nick
  11. , kbFocus :: KbFocus
  12. }
  13.  
  14. data KbFocus = KbFocus [Nick] Int -- which has focus
  15.  
  16. data Render c => Widget b c =
  17. Widget { box :: Box
  18. , thing :: c
  19. , handleKey :: Key -> c -> (b, c)
  20. , handleMouse :: Mouse -> c -> (b, c)
  21. }
  22.  
  23. kbFocused :: DoodleIO -> Nick
  24. kbFocused io = do
  25.  
  26. d <- readIORef io
  27. return (case kbFocus d of
  28. KbFocus ns i -> ns !! i)
  29.  
  30. runDoodle :: Doodle -> IO ()
  31. runDoodle doodle = do
  32. let doodleIO = newIORef doodle
  33. in bananaSDL (titled doodle) $ \ah -> do
  34. sdl <- fromAddHandler ah
  35. keys <- filter isKey sdl
  36. mice <- filter isMouse sdl
  37.  
  38. doodleB <- doodleIO
  39.  
  40. -- a behavior meant to be the widget to recieve key events
  41. kbw <- kbFocused <@> doodleB
  42.  
  43. (h1, lens1) <- lookupKeyHandler kbw <$> doodleB
  44. applyHandler lens1 h1 doodleB
  45. -- the handler gives the new value
  46. -- the doodleB is updated by the lens with the new value
  47.  
  48. -- figure out who is hit
  49. hby <- whoHitBy <@> mice
  50. (h2, lens2) <- lookupMouseHandler doodleB
  51.  
  52. -- lookup hit widgets handler and state
  53. -- apply the handler to the state with the event
  54. -- return the events raised and the new state
  55. applyHandler lens2 h2 doodleB
  56.  
  57. reactimate' $ draw <$> changes doodleB
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement