-
Notifications
You must be signed in to change notification settings - Fork 0
/
dice.elm
85 lines (67 loc) · 1.73 KB
/
dice.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
import Html exposing (..)
import Html.Events exposing (..)
import Html.Attributes exposing (..)
import Random
import String exposing (concat)
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
--MODEL
type alias Model =
{ dieFace1 : Int
, dieFace2 : Int
}
--INIT
init: (Model, Cmd Msg)
init =
(Model 1 1, Cmd.none)
--UPDATE
type Msg = Roll | NewFace1 Int | NewFace2 Int
type alias Time =
Float
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Roll ->
(model, Cmd.batch [ran NewFace1, ran NewFace2])
NewFace1 newFace ->
({ model | dieFace1 = newFace}, Cmd.none)
NewFace2 newFace ->
({ model | dieFace2 = newFace }, Cmd.none)
--UTILITIES
ran msg =
Random.generate msg (Random.int 1 6)
dieImage : Int -> String
dieImage dieFace =
concat ["./image-dieface/", (toString dieFace), ".svg"]
--VIEW
view : Model -> Html Msg
view model =
div [style [("text-align" , "center"), ("margin-top", "30px")]]
[ img [src (dieImage model.dieFace1), style [("display" , "inline-block" )]] []
, img [src (dieImage model.dieFace2), style [("display" , "inline-block" )]] []
, button [ onClick Roll, style buttonStyle ] [ text "Roll"]
]
buttonStyle : List (String, String)
buttonStyle =
[("background-color", "#e4685d")
,("border-radius","9px")
,("border","1px solid #ffffff")
,("display" , "block")
,("margin", "0 auto")
,("cursor","pointer")
,("color","#ffffff")
,("font-family","Arial")
,("font-size","15px")
,("padding","12px 15px")
,("text-decoration","none")
,("text-shadow","0px 1px 0px #b23e35")
]
--subscriptions
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none