-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.hs
More file actions
71 lines (60 loc) · 1.69 KB
/
Model.hs
File metadata and controls
71 lines (60 loc) · 1.69 KB
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
61
62
63
64
65
66
67
68
69
70
71
module Model where
import Data.Matrix ( Matrix, matrix )
import Graphics.Gloss (Color)
import Data.Bifunctor (bimap)
import Control.Monad (join)
import Graphics.Gloss.Data.Color
data RenderOptions = RenderOptions
{ pixelSize :: Int
, offset :: (Float,Float)
, canvasSize :: (Int,Int)
} deriving (Show)
data InsertMode = InsertMode
{ moveDirection :: (Int, Int)
, isPainting :: Bool
} deriving (Show)
newtype ExMode = ExMode String
deriving (Show)
data InputState =
InputInsert InsertMode
| InputEx ExMode
deriving (Show)
data AppState = AppState
{ painting :: Matrix Color
, renderOptions :: RenderOptions
, cursor :: (Int,Int)
, inputState :: InputState
} deriving (Show)
-- blackbird operator, compose with 2 args = \f g x y -> f(g x y)
(...) :: (b -> c) -> (a1 -> a2 -> b) -> a1 -> a2 -> c
(...) = (.) . (.)
mapTuple :: (a->b) -> (a,a) -> (b,b)
mapTuple = join bimap
applyTuple :: (t1 -> t2 -> b) -> (t1, t1) -> (t2, t2) -> (b, b)
applyTuple f (x1,x2) (y1,y2) = (f x1 y1, f x2 y2)
mapI :: (a -> Int -> b) -> [a] -> [b]
mapI f l = zipWith f l [0..]
defaultInput :: InputState
defaultInput = InputInsert InsertMode
{ moveDirection = (0,0)
, isPainting = False
}
paintingWidth = 50;
paintingHeight = 50;
initialState :: AppState
initialState = AppState
{ painting = matrix m n (const $ dim white)
, cursor = mapTuple (`div` 2) (m,n)
, inputState = defaultInput
, renderOptions = RenderOptions
{ pixelSize = pxSize
, offset = baseOffset
, canvasSize = mapTuple (+100) paintingSize
}
}
where
m = paintingWidth
n = paintingHeight
pxSize = 10
paintingSize = (m*pxSize,n*pxSize)
baseOffset = mapTuple ((* 0.5) . fromIntegral) paintingSize