You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.0 KiB

  1. module UI (drawUI) where
  2. import State
  3. import Data.IORef
  4. import Board
  5. import Raylib.Core
  6. import Raylib.Types
  7. import Raylib.Core.Shapes
  8. import Raylib.Util.Math
  9. drawUI :: IORef AppState -> IO ()
  10. drawUI r = do
  11. state <- readIORef r
  12. winW <- getRenderWidth
  13. winH <- getRenderHeight
  14. let act = state'action state
  15. brd = state'board state
  16. pth = state'path state
  17. (Point off) = state'offset state
  18. zoom = state'zoom state
  19. (Board ti bg pths) = brd
  20. drawBG off zoom winW winH bg
  21. mapM_ (drawPath off zoom) pths
  22. case act of
  23. ActionDraw p -> drawPath off zoom p
  24. _ -> return ()
  25. drawInterface act pth ti winW winH
  26. drawPath :: Vector2 -> Float -> Path -> IO ()
  27. drawPath offset zoom path =
  28. let points = path'points path
  29. segments = zip points (init points)
  30. col = unBoardColor $ path'color path
  31. thick = (path'thickness path) * zoom
  32. in
  33. mapM_ (\(s, e) ->
  34. let s' = (offset |+| unPoint s) |* zoom
  35. e' = (offset |+| unPoint e) |* zoom
  36. in
  37. drawLineEx s' e' thick col
  38. ) segments
  39. drawBG :: Vector2 -> Float -> Int -> Int -> BackgroundPattern -> IO ()
  40. drawBG _ _ _ _ (BGPatternPlain (BoardColor col)) = clearBackground col
  41. drawBG off zoom maxx' maxy' (BGPatternGrid fc' dc' hs' vs' th) =
  42. let fc = unBoardColor fc'
  43. dc = unBoardColor dc'
  44. hs = fromIntegral hs' * zoom
  45. vs = fromIntegral vs' * zoom
  46. offx = fromIntegral ((truncate $ vector2'x off) `rem` hs') * zoom
  47. offy = fromIntegral ((truncate $ vector2'y off) `rem` vs') * zoom
  48. maxx = fromIntegral maxx'
  49. maxy = fromIntegral maxy'
  50. in do
  51. clearBackground fc
  52. mapM_ (\x -> drawLineEx (Vector2 x 0) (Vector2 x maxy) th dc)
  53. [offx, offx+hs .. maxx]
  54. mapM_ (\y -> drawLineEx (Vector2 0 y) (Vector2 maxx y) th dc)
  55. [offy, offy+vs .. maxy]
  56. drawBG _ _ _ _ _ = undefined
  57. drawInterface :: Action -> String -> String -> Int -> Int -> IO ()
  58. drawInterface act pth ti winW winH = do
  59. return ()