-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.elm
89 lines (69 loc) · 2.28 KB
/
Main.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
module Main exposing (..)
import View exposing (..)
import Model exposing (..)
import String
import Array exposing (..)
import Keyboard exposing (KeyCode)
import Mouse exposing (..)
import Debug exposing (log)
import Html.App exposing (program)
import Util exposing (..)
main : Program Never
main =
program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
init : (Model,Cmd Msg )
init =
{ pieces = Array.fromList logoInit
, drag = Nothing
, select = 0 } ! []
update : Msg -> Model -> (Model,Cmd Msg)
update msg ({ pieces , drag } as model) =
updatehelp msg model ! []
updatehelp msg ({ pieces , drag } as model) =
case msg of
DragStart n xy ->
{model | drag = Just { id = n
, start = xy
, current = xy }
, select = n }
DragAt n xy ->
let drag' = (Maybe.map (\{id,start} -> { id = id , start = start , current = xy}) drag)
in {model | drag = drag' }
DragEnd n _ ->
{model | pieces = currentPieces drag model.pieces
, drag = Nothing }
Keydowns code ->
case code of
37 -> --left key
let pieces' = updateArray (\a -> { a | rotate = a.rotate - 5 }) model.select model.pieces
in {model | pieces = pieces'}
39 -> --right key
let pieces' = updateArray (\a -> { a | rotate = a.rotate + 5 }) model.select model.pieces
in {model | pieces = pieces'}
38 ->
{model | pieces = movedPieces (-) model.select model.pieces}
40 ->
{model | pieces = movedPieces (+) model.select model.pieces}
_ ->
model
movePixel = 10
movedPieces f id pieces =
let move ({position} as piece) = {piece | position = {position | y = f position.y movePixel } }
in updateArray move id pieces
updateArray : (a -> a) -> Int -> Array a -> Array a
updateArray f num array =
case Maybe.map (\a -> Array.set num (f a) array) (Array.get num array) of
Just a -> a
Nothing -> array
subscriptions : Model -> Sub Msg
subscriptions model =
case model.drag of
Nothing ->
Sub.batch [Keyboard.downs Keydowns ]
Just {id} ->
Sub.batch [ Mouse.moves (DragAt id), Mouse.ups (DragEnd id) ]