Permalink
Browse files

refactor prototype

  • Loading branch information...
1 parent c2df590 commit ef03f2e71972a6205ce9e9c6406456556a32ebb4 @torii-y torii-y committed Aug 18, 2016
View
@@ -864,6 +864,7 @@ update removeToken action model =
let
object =
findObjectById (EditingFloor.present model.floor).objects id
+
model' =
{ model |
contextMenu = NoContextMenu
@@ -873,9 +874,17 @@ update removeToken action model =
Just e ->
let
(_, _, w, h) = rect e
+
(newId, seed) = IdGenerator.new model.seed
+
newPrototypes =
- Prototypes.register (newId, backgroundColorOf e, nameOf e, (w, h)) model.prototypes
+ Prototypes.register
+ { id = newId
+ , backgroundColor = backgroundColorOf e
+ , name = nameOf e
+ , size = (w, h)
+ }
+ model.prototypes
in
{ model' |
seed = seed
@@ -1310,7 +1319,12 @@ updateOnFinishStamp model =
candidatesWithNewIds' =
List.map
- (\(((_, color, name, (w, h)), (x, y)), newId) -> (newId, (x, y, w, h), color, name))
+ (\((prototype, (x, y)), newId) ->
+ let
+ (width, height) = prototype.size
+ in
+ (newId, (x, y, width, height), prototype.backgroundColor, prototype.name)
+ )
candidatesWithNewIds
(newFloor, cmd) =
@@ -1889,11 +1903,12 @@ stampCandidates model =
let
prototype =
selectedPrototype model.prototypes
- (prototypeId, color, name, deskSize) =
- prototype
+
(offsetX, offsetY) = model.offset
+
(x2, y2) =
model.pos
+
(x2', y2') =
screenToImageWithOffset model.scale (x2, y2) (offsetX, offsetY)
in
@@ -1906,11 +1921,12 @@ stampCandidates model =
stampCandidatesOnDragging model.gridSize prototype (x1', y1') (x2', y2')
_ ->
let
- (deskWidth, deskHeight) = deskSize
+ (deskWidth, deskHeight) = prototype.size
+
(left, top) =
fitPositionToGrid model.gridSize (x2' - deskWidth // 2, y2' - deskHeight // 2)
in
- [ ((prototypeId, color, name, (deskWidth, deskHeight)), (left, top))
+ [ (prototype, (left, top))
]
_ -> []
@@ -5,4 +5,8 @@ type alias Id =
type alias Prototype =
- (Id, String, String, (Int, Int)) -- id, color, name, size
+ { id : Id
+ , name : String
+ , backgroundColor : String
+ , size : (Int, Int)
+ }
@@ -122,7 +122,7 @@ generateAllCandidatePosition (deskWidth, deskHeight) (centerLeft, centerTop) (in
stampCandidatesOnDragging : Int -> Prototype -> (Int, Int) -> (Int, Int) -> List StampCandidate
stampCandidatesOnDragging gridSize prototype (x1, y1) (x2, y2) = -- imagePos
let
- (prototypeId, color, name, deskSize) = prototype
+ deskSize = prototype.size
flip (w, h) = (h, w)
horizontal = abs (x2 - x1) > abs (y2 - y1)
(deskWidth, deskHeight) = if horizontal then flip deskSize else deskSize
@@ -135,7 +135,8 @@ stampCandidatesOnDragging gridSize prototype (x1, y1) (x2, y2) = -- imagePos
(deskWidth, deskHeight)
(centerLeft, centerTop)
(indicesX, indicesY)
+
+ prototype' =
+ { prototype | size = (deskWidth, deskHeight) }
in
- List.map (\(left, top) ->
- ((prototypeId, color, name, (deskWidth, deskHeight)), (left, top))
- ) all
+ List.map ((,) prototype') all
@@ -268,7 +268,9 @@ decodeFloorInfo = Decode.map (\(lastFloor, lastFloorWithEdit) ->
decodePrototype : Decoder Prototype
decodePrototype =
decode
- (\id backgroundColor _ name width height _ _ -> (id, backgroundColor, name, (width, height)))
+ (\id backgroundColor _ name width height _ _ ->
+ { id = id, name = name, backgroundColor = backgroundColor, size = (width, height) }
+ )
|> required "id" Decode.string
|> required "backgroundColor" Decode.string
|> required "color" Decode.string
@@ -280,14 +282,17 @@ decodePrototype =
encodePrototype : Prototype -> Value
-encodePrototype (id, color, name, (width, height)) =
- object
- [ ("id", string id)
- , ("color", string color)
- , ("name", string name)
- , ("width", int width)
- , ("height", int height)
- ]
+encodePrototype { id, backgroundColor, name, size } =
+ let
+ (width, height) = size
+ in
+ object
+ [ ("id", string id)
+ , ("color", string backgroundColor)
+ , ("name", string name)
+ , ("width", int width)
+ , ("height", int height)
+ ]
serializePrototypes : List Prototype -> String
@@ -363,21 +363,24 @@ canvasImage floor =
temporaryStampView : Scale.Model -> Bool -> StampCandidate -> (String, Html msg)
-temporaryStampView scale selected ((prototypeId, color, name, (deskWidth, deskHeight)), (left, top)) =
- ( "temporary_" ++ toString left ++ "_" ++ toString top ++ "_" ++ toString deskWidth ++ "_" ++ toString deskHeight
- , ObjectView.viewDesk
- ObjectView.noEvents
- False
- (left, top, deskWidth, deskHeight)
- color
- name --name
- Object.defaultFontSize
- selected
- False -- alpha
- scale
- True -- disableTransition
- False -- personMatched
- )
+temporaryStampView scale selected (prototype, (left, top)) =
+ let
+ (deskWidth, deskHeight) = prototype.size
+ in
+ ( "temporary_" ++ toString left ++ "_" ++ toString top ++ "_" ++ toString deskWidth ++ "_" ++ toString deskHeight
+ , ObjectView.viewDesk
+ ObjectView.noEvents
+ False
+ (left, top, deskWidth, deskHeight)
+ prototype.backgroundColor
+ prototype.name --name
+ Object.defaultFontSize
+ selected
+ False -- alpha
+ scale
+ True -- disableTransition
+ False -- personMatched
+ )
temporaryPenView : Model -> (Int, Int) -> Html msg
View
@@ -240,14 +240,6 @@ prototypePreviewView prototypes stampMode =
width = 320 - (20 * 2) -- TODO
height = 238 -- TODO
- each index (prototype, selected) =
- let
- (_, _, _, (w, h)) = prototype
- left = width // 2 - w // 2
- top = height // 2 - h // 2
- in
- snd <| CanvasView.temporaryStampView Scale.init False (prototype, (left + index * width, top))
-
selectedIndex =
Maybe.withDefault 0 <|
List.head <|
@@ -260,13 +252,23 @@ prototypePreviewView prototypes stampMode =
inner =
div
[ style (S.prototypePreviewViewInner width selectedIndex) ]
- (List.indexedMap each prototypes)
+ (List.indexedMap (prototypePreviewViewEach (width, height)) prototypes)
in
div
[ style (S.prototypePreviewView stampMode) ]
( inner :: buttons )
+prototypePreviewViewEach : (Int, Int) -> Int -> (Prototype, Bool) -> Html Msg
+prototypePreviewViewEach (width, height) index (prototype, selected) =
+ let
+ (w, h) = prototype.size
+ left = width // 2 - w // 2
+ top = height // 2 - h // 2
+ in
+ snd <| CanvasView.temporaryStampView Scale.init False (prototype, (left + index * width, top))
+
+
prototypePreviewViewButtons : Int -> List (Prototype, Bool) -> List (Html Msg)
prototypePreviewViewButtons selectedIndex prototypes =
List.map (\isLeft ->
@@ -280,7 +282,7 @@ prototypePreviewViewButtons selectedIndex prototypes =
[ text label ]
)
( (if selectedIndex > 0 then [True] else []) ++
- (if selectedIndex < List.length prototypes - 1 then [False] else [])
+ (if selectedIndex < List.length prototypes - 1 then [ False ] else [])
)

0 comments on commit ef03f2e

Please sign in to comment.