-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.elm
330 lines (282 loc) · 9.45 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
module Main exposing (..)
import Html.App
import Html
import Html.Attributes
import Html.Events
import Json.Decode
import Mouse
import ImageSearch
borderColor =
"tan"
type alias Model =
{ canvas : Size
, borderSize : Int
, frame : Frame
, dragState :
Maybe
{ startPosition : Mouse.Position
, path : FramePath
}
, imageSearch : ImageSearch.State
}
type alias Size =
{ width : Int, height : Int }
type alias Position =
{ x : Int, y : Int }
type alias Image =
{ url : String
, size : Size
, offset : Position
}
type Frame
= SingleImage Image
| HorizontalSplit
{ top : Frame
, topHeight : Int
, bottom : Frame
}
type alias FramePath =
List Int
initialModel : Model
initialModel =
{ canvas = { width = 250, height = 250 }
, borderSize = 5
, frame =
HorizontalSplit
{ top =
SingleImage
{ url = "https://i.imgur.com/gt5lnkS.jpg"
, size = { width = 960, height = 637 }
, offset = { x = 0, y = 0 }
}
, topHeight = 80
, bottom =
HorizontalSplit
{ top =
SingleImage
{ url = "http://imgur.com/4mYf6Jh.jpg"
, size = { width = 960, height = 618 }
, offset = { x = 0, y = 0 }
}
, topHeight = 100
, bottom =
SingleImage
{ url = "http://imgur.com/Pz945w3.jpg"
, size = { width = 960, height = 720 }
, offset = { x = 0, y = 0 }
}
}
}
, dragState = Nothing
, imageSearch = ImageSearch.init
}
type Msg
= DragStart FramePath Mouse.Position
| DragMove Mouse.Position
| DragEnd Mouse.Position
| ImageSearchMsg ImageSearch.Msg
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case Debug.log "msg" msg of
DragStart path position ->
( { model
| dragState =
Just
{ path = List.reverse path
, startPosition = position
}
}
, Cmd.none
)
DragMove currentPosition ->
case model.dragState of
Just { startPosition, path } ->
( { model
| frame =
applyDrag path
{ x = startPosition.x - currentPosition.x
, y = startPosition.y - currentPosition.y
}
model.frame
, dragState =
Just
{ path = path
, startPosition = currentPosition
}
}
, Cmd.none
)
Nothing ->
( model, Cmd.none )
DragEnd endPosition ->
( { model | dragState = Nothing }
, Cmd.none
)
ImageSearchMsg childMsg ->
let
( newChildState, childCmd, selectedImage ) =
ImageSearch.update childMsg model.imageSearch
newModel =
case selectedImage of
Just newImage ->
{ model
| frame = replaceImage [ 0 ] newImage.url model.frame
}
Nothing ->
model
in
( { newModel | imageSearch = newChildState }
, Cmd.map ImageSearchMsg childCmd
)
replaceImage : FramePath -> String -> Frame -> Frame
replaceImage path newUrl frame =
case frame of
SingleImage currentImage ->
SingleImage { currentImage | url = newUrl }
HorizontalSplit current ->
-- TODO follow the correct path
HorizontalSplit
{ current | top = replaceImage [] newUrl current.top }
applyDrag : FramePath -> Position -> Frame -> Frame
applyDrag path change frame =
case frame of
HorizontalSplit { top, topHeight, bottom } ->
case path of
0 :: rest ->
HorizontalSplit
{ top = applyDrag rest change top
, topHeight = topHeight
, bottom = bottom
}
1 :: rest ->
HorizontalSplit
{ top = top
, topHeight = topHeight
, bottom = applyDrag rest change bottom
}
_ ->
HorizontalSplit
{ top = top
, bottom = bottom
, topHeight = topHeight - change.y
}
SingleImage image ->
SingleImage
{ image
| offset =
{ x = image.offset.x + change.x
, y = image.offset.y + change.y
}
}
viewCanvas : Int -> Size -> Frame -> Html.Html Msg
viewCanvas borderSize size rootFrame =
Html.div
[ Html.Attributes.style
[ ( "width", toString size.width ++ "px" )
, ( "height", toString size.height ++ "px" )
, ( "border", "2px solid black" )
]
]
[ Html.div
[ Html.Attributes.style
[ ( "border", toString borderSize ++ "px solid " ++ borderColor )
]
]
[ viewFrame []
borderSize
{ width = size.width - 2 * borderSize
, height = size.height - 2 * borderSize
}
rootFrame
]
]
viewFrame : FramePath -> Int -> Size -> Frame -> Html.Html Msg
viewFrame path borderSize size frame =
case frame of
SingleImage image ->
let
imageRatio =
toFloat image.size.width / toFloat image.size.height
frameRatio =
toFloat size.width / toFloat size.height
in
Html.div
[ Html.Attributes.style
[ ( "height", toString size.height ++ "px" )
, ( "width", toString size.width ++ "px" )
, ( "background-image", "url(" ++ image.url ++ ")" )
, ( "background-size"
, if imageRatio > frameRatio then
"auto " ++ toString size.height ++ "px"
else
toString size.width ++ "px auto"
)
, ( "background-position"
, toString -image.offset.x ++ "px " ++ toString -image.offset.y ++ "px"
)
]
, Html.Events.on "mousedown"
(Json.Decode.map (DragStart path) Mouse.position)
]
[]
HorizontalSplit { top, topHeight, bottom } ->
Html.div []
[ viewFrame (0 :: path)
borderSize
{ width = size.width
, height = topHeight
}
top
, Html.div
[ Html.Attributes.style
[ ( "width", toString size.width ++ "px" )
, ( "height", toString borderSize ++ "px" )
, ( "background-color", borderColor )
, ( "cursor", "ns-resize" )
]
, Html.Events.on "mousedown"
(Json.Decode.map (DragStart path) Mouse.position)
]
[]
, viewFrame (1 :: path)
borderSize
{ width = size.width
, height = size.height - topHeight - borderSize
}
bottom
]
view : Model -> Html.Html Msg
view model =
Html.div
[ Html.Attributes.style [ ( "padding", "8px" ) ]
]
[ viewCanvas model.borderSize model.canvas model.frame
, Html.div
[ Html.Attributes.style
[ ( "position", "absolute" )
, ( "right", "0px" )
, ( "top", "0px" )
]
]
[ Html.App.map ImageSearchMsg
(ImageSearch.view model.imageSearch)
]
, Html.hr [] []
, Html.text <| toString model
]
subscriptions model =
case model.dragState of
Just _ ->
Sub.batch
[ Mouse.moves DragMove
, Mouse.ups DragEnd
]
_ ->
Sub.none
main =
Html.App.program
{ init = ( initialModel, Cmd.none )
, subscriptions = subscriptions
, update = update
, view = view
}