on d9d1dfd help sign in
elk/core/Form.elm
114 lines 3.2 kB history raw
2module Collage where
4data Line
5 = Start (Float,Float)
6 | Arc (Float,Float) Float (Float,Float) Path
7 | Line (Float,Float) Path
8 | Bezier (Float,Float) (Float,Float) (Float,Float) Path
9 | Quadratic (Float,Float) (Float,Float) Path
11path ps = case ps of
12 [] -> Start (0,0)
13 hd:tl -> foldl Line (Start hd) tl
15segment p1 p2 = Line p1 (Start p2)
17start = Start
18arcTo = Arc
19lineTo = Line
20bezierTo = Bezier
21quadraticTo = Quadratic
23data Shape = Shape Line
25polygon ps = Shape (case ps of
26 [] -> Start (0,0)
27 hd:tl -> Line hd $ foldl Line (Start hd) tl)
29rect w h = polygon [ (0-w/2,0-h/2), (0-w/2,h/2), (w/2,h/2), (w/2,0-h/2) ]
30oval w h =
31 let n = 50
32 f i = ( w/2 * cos (2*pi/n * i), h/2 * sin (2*pi/n * i))
33 in Shape $ map f [0..n-1]
34circle r = oval (2*r) (2*r)
35ngon n r =
36 let m = toFloat n
37 f i = ( r * cos (2*pi/m * i), r * sin (2*pi/m * i))
38 in Shape $ map f [0..n-1]
40data Transform
41 = Rotate Float
42 | Move Float Float
43 | Scale Float Float
44 | Transform Float Float Float Float Float Float
46data Form = Form [Transform] BasicForm
49data LineCap = Butt | Round | Square
50data LineJoin = Round | Bevel | Miter
52data FillStyle
53 = NoFill
54 | Solid Color
55 | Texture String
56 | LinearGradient [(Float,Color)] (Float,Float) (Float,Float)
57 | RadialGradient [(Float,Color)] (Float,Float) Float (Float,Float) Float
59data LineStyle = NoLine | Line [Int] Int Float LineCap LineJoin Float FillStyle
61data BasicForm
62 = FLine LineStyle FillStyle Line
63 | FShape LineStyle FillStyle Shape
64 | FImage Int Int String
65 | FElement Element
66 | FGroup [Form]
68setFillStyle fs form =
69 case form of
70 FLine ls _ ln -> FLine ls fs ln
71 FShape ls _ shp -> FLine ls fs shp
72 _ -> form
74noFill = setFillStyle NoFill
75fillColor clr = setFillStyle (Solid clr)
76texture src = setFillStyle (Texture src)
77linearGradient stops start end = setFillStyle (LinearGradient stops start end)
78radialGradient stops innerP innerR outerP outerR =
79 setFillStyle (RadialGradient stops innerP innerR outerP outerR)
81solid clr ln = Form [] (FLine ln)
82dotted clr ln = Form [] (FLine ln)
83dashed clr ln = Form [] (FLine ln)
84customLine pattern clr ln = Form [] (FLine (Custom pattern) clr ln)
87filled clr shp = Form [] (FShape shp)
88outlined clr shp = Form [] (FShape shp)
89customOutline pattern clr (Shape ps pos) =
90 Form [] (FShape (CustomOutline pattern) clr (Shape ps pos))
91textured src (Shape ps pos) = Form [] (FShape (Textured src) black (Shape ps pos))
93fromLine = FLine
94fromShape = FShape
95fromElement = FElement
97sprite w h pos src = Form 0 1 pos (FImage w h src)
98toForm pos e = Form 0 1 pos (FElement e)
100addTransform t (Form ts form) = Form (t:ts) form
102rotateRad rad = addTransform $ Rotate rad
103rotateDeg deg = addTransform $ Rotate (pi * deg / 180)
104rotate rot = addTransform $ Rotate (2 * pi * rot)
106scale s = addTransform (Scale s s)
107scaleX s = addTransform (Scale s 1)
108scaleY s = addTransform (Scale 1 s)
110move x y = addTransform (Move x y)
111moveX x = addTransform (Move x 0)
112moveY y = addTransform (Move 0 y)
114transform a b c d e f = addTransform (Transform a b c d e f)