-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRender.hs
60 lines (53 loc) · 1.84 KB
/
Render.hs
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{-# LANGUAGE NamedFieldPuns #-}
module Render (renderFn, paintPixels, pointToPixel) where
import Model
import Graphics.Gloss.Data.Color
import Data.Matrix
import Graphics.Gloss.Data.Point (Path)
import Graphics.Gloss.Data.Picture
renderFn :: AppState -> Picture
renderFn AppState
{ renderOptions = renderOpts,
painting=px,
cursor=cursorPos,
inputState
} =
-- order gives z axis
pictures (pixelsPicture ++ [cursorPicture, exModePrompt])
where
pixelsPicture = mapI (\color listIndex ->
let (i,j) = quotRem listIndex (ncols px) in
drawPixel color renderOpts (i,j)
)(toList px)
cursorPicture = lineLoop $ getRectPath renderOpts $
-- 1 because matrix is 1 based
mapTuple (subtract 1) cursorPos
exModePrompt =
case inputState of
InputEx (ExMode msg) ->
uncurry Translate (mapTuple (* (-1)) $ offset renderOpts)
$ Scale 0.1 0.1
$ Text (":" ++ msg)
_ -> Blank
pixelToPoint :: RenderOptions -> (Int,Int) -> (Float,Float)
pixelToPoint RenderOptions { pixelSize, offset } =
-- matrix is 1 based index, remove 1
applyTuple subtract offset . mapTuple (fromIntegral.(* pixelSize).subtract 1)
pointToPixel :: RenderOptions -> (Float,Float) -> (Int,Int)
pointToPixel RenderOptions { pixelSize=size, offset } =
mapTuple (ceiling.(/ intSize).(+1)) . applyTuple (+) offset
where
intSize = fromIntegral size
getRectPath :: RenderOptions -> (Int,Int) -> Path
getRectPath renderOpts (i,j) =
map (\(a,b) -> pixelToPoint renderOpts (a+i,b+j)
)
rectangle
where
rectangle = [(0,0),(0,1),(1,1),(1,0)]
drawPixel :: Color -> RenderOptions -> (Int,Int) -> Picture
drawPixel c =
Color c ... (Polygon ... getRectPath)
paintPixels :: Color -> Matrix Color -> [(Int,Int)] -> Matrix Color
paintPixels =
foldr . setElem