Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 26 additions & 27 deletions src/dali/layout/connect.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
[dali.layout.utils :refer [bounds->anchor-point]]
[net.cgrand.enlive-html :as en]))

(defn- straight-anchors [element1 element2 bounds-fn]
(let [bounds1 (bounds-fn element1)
bounds2 (bounds-fn element2)]
(->>
(for [[anchor1 anchor2] [[:left :right]
[:right :left]
[:top :bottom]
[:bottom :top]]]
[[anchor1 anchor2]
(geom/distance-squared
(bounds->anchor-point anchor1 bounds1)
(bounds->anchor-point anchor2 bounds2))])
(sort-by second)
first ;;get the shortest distance
first))) ;;return the anchor pair
(defn- straight-anchors [bounds1 bounds2]
(->>
(for [[anchor1 anchor2] [[:left :right]
[:right :left]
[:top :bottom]
[:bottom :top]]]
[[anchor1 anchor2]
(geom/distance-squared
(bounds->anchor-point anchor1 bounds1)
(bounds->anchor-point anchor2 bounds2))])
(sort-by second)
first ;;get the shortest distance
first)) ;;return the anchor pair

(defn- corner-anchor [bounds intersection]
(->>
Expand All @@ -39,18 +37,16 @@
:dali/content points}
attrs)})

;;TODO refactor so that you pass bounds, not elements?
(defn- straight-connector [start-element end-element attrs bounds-fn]
(let [[a1 a2] (straight-anchors start-element end-element bounds-fn)
p1 (bounds->anchor-point a1 (bounds-fn start-element))
p2 (bounds->anchor-point a2 (bounds-fn end-element))]

(defn- straight-connector [bounds1 bounds2 attrs]
(let [[a1 a2] (straight-anchors bounds1 bounds2)
p1 (bounds->anchor-point a1 bounds1)
p2 (bounds->anchor-point a2 bounds2)]
(connector attrs [p1 p2])))

;;TODO refactor so that you pass bounds, not elements?
(defn- corner-connector [start-element end-element attrs connection-type bounds-fn]
(let [bounds1 (bounds-fn start-element)
bounds2 (bounds-fn end-element)
[cx1 cy1] (bounds->anchor-point :center bounds1)

(defn- corner-connector [bounds1 bounds2 attrs connection-type]
(let [[cx1 cy1] (bounds->anchor-point :center bounds1)
[cx2 cy2] (bounds->anchor-point :center bounds2)
intersection (if (= :|- connection-type)
[cx1 cy2]
Expand All @@ -72,6 +68,9 @@
start-element (first (en/select document start-selector))
end-element (first (en/select document end-selector))

bounds1 (bounds-fn start-element)
bounds2 (bounds-fn end-element)

check-element (fn [x selector]
(when-not x
(throw (utils/exception
Expand All @@ -83,5 +82,5 @@
(check-element start-element (-> tag :attrs :from))
(check-element end-element (-> tag :attrs :to))
(if (= :-- connection-type)
[(straight-connector start-element end-element attrs bounds-fn)]
[(corner-connector start-element end-element attrs connection-type bounds-fn)])))
[(straight-connector bounds1 bounds2 attrs)]
[(corner-connector bounds1 bounds2 attrs connection-type)])))