Skip to content

Commit

Permalink
Nodes now no longer have weird default record, and refactored positio…
Browse files Browse the repository at this point in the history
…ns and diminsions
  • Loading branch information
td-ideabox committed Jul 18, 2018
1 parent 32236d7 commit 4d47bee
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 80 deletions.
10 changes: 8 additions & 2 deletions src/components/elm/src/Lib/ExportDot.elm
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,17 @@ nodesToDot nodes =
nodeStyling : Node -> NodeStyling
nodeStyling node =
let
width =
Tuple.first node.diminsions |> toString

height =
Tuple.second node.diminsions |> toString

styleStr =
[ ( "label", "\"" ++ node.label ++ "\"" )
, ( "shape", "box" )
, ( "width", toString node.width )
, ( "height", toString node.height )
, ( "width", width )
, ( "height", height )
]
|> List.map (\it -> Tuple.first it ++ "=" ++ Tuple.second it)
|> String.join " "
Expand Down
12 changes: 12 additions & 0 deletions src/components/elm/src/Lib/Geometry.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ module Geometry exposing (..)
import Tuple exposing (first, second)


type alias XCoord =
Float


type alias YCoord =
Float


type alias Pos =
( XCoord, YCoord )


lineMidPoint : ( Float, Float ) -> ( Float, Float ) -> ( Float, Float )
lineMidPoint src dest =
let
Expand Down
40 changes: 23 additions & 17 deletions src/components/elm/src/State.elm
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,10 @@ insertNode model =
newY =
(toFloat (model.windowSize.height) + yRand) / 2

newNode =
{ initialNode
| x = newX
, y = newY
, idx = i
}
node =
newNode i "#000" ( newX, newY ) Nothing False ( 1.0, 1.0 ) ""
in
incrementIdx { model | nodes = Dict.insert i newNode model.nodes }
incrementIdx { model | nodes = Dict.insert i node model.nodes }


moveViewBox : ViewBox -> Float -> ViewBox
Expand Down Expand Up @@ -353,7 +349,7 @@ executeEditNodeCommand elementIdx model =
Just n ->
Ok
{ model
| viewBox = focusViewBox ( n.x, n.y ) model.windowSize model.viewBox
| viewBox = focusViewBox n.position model.windowSize model.viewBox
, currentCommand = ""
, mode = (EditNode n)
}
Expand Down Expand Up @@ -384,7 +380,7 @@ executeEditEdgeCommand elementIdx model =
Just n2 ->
let
midPoint =
lineMidPoint ( n1.x, n1.y ) ( n2.x, n2.y )
lineMidPoint n1.position n2.position
in
Ok
{ model
Expand Down Expand Up @@ -557,7 +553,7 @@ calcForcesOnNode node nodes edges =
Just anchor ->
let
p1 =
( node.x, node.y )
node.position

p2 =
anchor
Expand Down Expand Up @@ -585,10 +581,10 @@ calcForcesOnNode node nodes edges =
(\n ->
let
p1 =
( node.x, node.y )
node.position

p2 =
( n.x, n.y )
n.position

dist =
distance p1 p2
Expand Down Expand Up @@ -618,7 +614,7 @@ calcForcesOnNode node nodes edges =
p1 =
case src of
Just s ->
( s.x, s.y )
s.position

Nothing ->
Debug.crash (e.src ++ " is not in the node map!")
Expand All @@ -629,7 +625,7 @@ calcForcesOnNode node nodes edges =
p2 =
case dest of
Just d ->
( d.x, d.y )
d.position

Nothing ->
Debug.crash (e.dest ++ " is not in the node map")
Expand Down Expand Up @@ -661,10 +657,14 @@ calcNodeRepulseRadius : Node -> Float
calcNodeRepulseRadius node =
let
halfWidth =
inchesToPixels node.width |> toFloat
Tuple.first node.diminsions
|> inchesToPixels
|> toFloat

halfHeight =
inchesToPixels node.width |> toFloat
Tuple.second node.diminsions
|> inchesToPixels
|> toFloat

hypot =
(halfWidth ^ 2) + (halfHeight ^ 2) |> sqrt
Expand All @@ -689,6 +689,12 @@ applyPhysics dt nodes edges node =

vy =
second finalForce

x =
Tuple.first node.position

y =
Tuple.second node.position
in
--- Calculate force to apply based on distance and direction
{ node | x = node.x + vx * dt, y = node.y + vy * dt }
{ node | position = ( x + vx * dt, y + vy * dt ) }
75 changes: 47 additions & 28 deletions src/components/elm/src/Types.elm
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,30 @@ type Mode
---- MODEL ----


type alias Coord =
type alias XCoord =
Float


type alias YCoord =
Float


type alias Position =
( XCoord, YCoord )


type alias Width =
Inches


type alias Height =
Inches


type alias Diminsions =
( Width, Height )


type alias ViewBox =
{ x : Float
, y : Float
Expand All @@ -90,6 +110,14 @@ initialViewBox =
}


type alias NodeIdx =
String


type alias NodeColor =
String


type alias NodeLabel =
String

Expand All @@ -106,39 +134,30 @@ type alias NumPixels =
Int


type alias AnchorCoord =
Maybe Position


type alias Node =
{ idx : String
, color : String
, x : Float
, anchorCoord : Maybe ( Float, Float )
, y : Float
{ idx : NodeIdx
, color : NodeColor
, position : Position
, anchorCoord : AnchorCoord
, ignoreForces : Bool
, width : Inches
, height : Inches
, roundX : Int
, roundY : Int
, diminsions : Diminsions
, label : NodeLabel
}



-- We're initializing data with valid but incorrect data. Is
-- there a better pattern?


initialNode : Node
initialNode =
{ idx = "unassigned"
, x = 0
, y = 0
, anchorCoord = Nothing
, ignoreForces = False
, label = ""
, color = "#f00"
, width = 1.25
, height = 1.0
, roundX = 15
, roundY = 15
newNode : NodeIdx -> NodeColor -> Position -> AnchorCoord -> Bool -> Diminsions -> NodeLabel -> Node
newNode nodeIdx nodeColor position anchorCoord ignoreForces diminsions nodeLabel =
{ idx = nodeIdx
, color = nodeColor
, position = position
, anchorCoord = anchorCoord
, ignoreForces = ignoreForces
, diminsions = diminsions
, label = nodeLabel
}


Expand Down
63 changes: 30 additions & 33 deletions src/components/elm/src/ViewSvg.elm
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,20 @@ calcConnectPoints : Node -> List ( Float, Float )
calcConnectPoints node =
let
x =
node.x
Tuple.first node.position

y =
node.y
Tuple.second node.position

width =
inchesToPixels node.width |> toFloat
Tuple.first node.diminsions
|> inchesToPixels
|> toFloat

height =
inchesToPixels node.height |> toFloat
Tuple.second node.diminsions
|> inchesToPixels
|> toFloat

halfWidth =
width / 2.0
Expand Down Expand Up @@ -249,61 +253,54 @@ nodeToSvg : Node -> Svg msg
nodeToSvg node =
let
xPos =
toString node.x
Tuple.first node.position
|> toString

yPos =
toString node.y
Tuple.second node.position
|> toString

nWidth =
inchesToPixels node.width |> toString
nWidthInches =
Tuple.first node.diminsions

nHeight =
inchesToPixels node.height |> toString
nWidthPixels =
inchesToPixels nWidthInches

roundX =
toString node.roundX
nHeightInches =
Tuple.second node.diminsions

roundY =
toString node.roundY
nHeightPixels =
inchesToPixels nHeightInches

idx =
node.idx

idxX =
toString (node.x + 10)
toString (Tuple.first node.position + 10)

idxY =
toString (node.y + 15)

debugCoordX =
toString (node.x + 10)

debugCoordY =
toString (node.y + 60)

debugCoord =
toString (Basics.round node.x) ++ ", " ++ toString (round node.y)
toString (Tuple.second node.position + 15)

labelX =
inchesToPixels node.width
|> toFloat
toFloat nWidthPixels
|> (*) 0.25
|> (+) node.x
|> (+) (Tuple.first node.position)
|> toString

labelY =
inchesToPixels node.height
|> toFloat
toFloat nHeightPixels
|> (*) 0.5
|> (+) node.y
|> (+) (Tuple.second node.position)
|> toString
in
g []
[ rect
[ x xPos
, y yPos
, width nWidth
, height nHeight
, toString nWidthPixels
|> width
, toString nHeightPixels
|> height
, fill "transparent"
, stroke "black"
]
Expand Down

0 comments on commit 4d47bee

Please sign in to comment.