-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUI.elm
138 lines (123 loc) · 4.14 KB
/
UI.elm
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
module UI (mainView) where
import Core exposing(Model, GameState(..), Update(..))
import Html exposing(..)
import Html.Events exposing (..)
import List exposing(indexedMap, concat, reverse)
import Html.Attributes exposing (style)
import Signal exposing (Address)
import Style exposing(containerStyle, maskStyle, mainWrapperStyle,
wrapperStyle, gameInfoStyle, restartBtnStyle)
span = 100
space = 10
mask: Html
mask = (div [style maskStyle] [])
cell row col num =
let
top = row * (span + space)
left = col * (span + space)
symbol = toString num
(bgColor, color, numSymbol) = case num of
0 -> ("rgba(238, 228, 218, 0.35)", "black", "")
2 -> ("#eee4da", "black", symbol)
4 -> ("#ede0c8", "black", symbol)
8 -> ("#f2b179", "black", symbol)
16 -> ("#f59563", "black", symbol)
_ -> ("#f59563", "black", symbol)
in
div [style [
("position", "absolute")
,("top", (toString top) ++ "px")
,("left", (toString left) ++ "px")
,("width", (toString span) ++ "px")
,("height", (toString span) ++ "px")
,("background", bgColor)
,("color", color)
,("margin", "10px")
,("text-align", "center")
,("vertical-align", "middle")
,("line-height", "90px")
,("font-size", "40px")
,("font-weight", "bold")
]]
[text numSymbol]
mainViewBeforeStart: Address Update -> Model -> Html
mainViewBeforeStart address state =
div
[]
[
div [style gameInfoStyle]
[
p [] [text "Press Enter key to start"]
, button [
style restartBtnStyle
,(onClick address Start)]
[text "Start"]
]
,div [style containerStyle] (mask :: (drawCells state))
]
mainViewOnLoose: Address Update -> Model -> Html
mainViewOnLoose address state =
div
[]
[
div [style gameInfoStyle]
[
p [] [text "Looooooser, press Enter key to start again"]
,button [
style restartBtnStyle
,(onClick address Restart)]
[text "Restart"]
]
,div [style containerStyle] (mask :: (drawCells state))
]
mainViewWin: Address Update -> Model -> Html
mainViewWin address state =
div
[]
[
div [style gameInfoStyle]
[
p [] [text "Yooohoooo, YOU ROCK !!!"]
,button [
style restartBtnStyle
,(onClick address Restart)]
[text "Restart"]
]
,div [style containerStyle] (mask :: (drawCells state))
]
drawCells: Model -> List Html
drawCells state =
let
drawRow rowIdx row = indexedMap (cell rowIdx) row
in
concat (indexedMap drawRow state.cells)
mainViewOnAir: Address Update -> Model -> Html
mainViewOnAir address state =
div
[]
[
div [style gameInfoStyle]
[
p [] [text ("Score - " ++ (toString state.score))]
, button [
style restartBtnStyle
,(onClick address Restart)]
[text "Restart"]
]
, div [style containerStyle] (drawCells state)
]
mainView: Address Update -> GameState Model -> Html
mainView address model =
div [style mainWrapperStyle]
[
div
[style wrapperStyle]
[
h1 [] [text "2048 in Elm"],
case model of
BeforeStart state -> mainViewBeforeStart address state
OnAir state -> mainViewOnAir address state
EndLoose state -> mainViewOnLoose address state
EndWin state -> mainViewWin address state
]
]