Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sprocketc committed Dec 2, 2024
1 parent 9cb5059 commit 971342b
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 70 deletions.
6 changes: 3 additions & 3 deletions src/renderer/dialog/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

(rf/reg-event-fx
::close
(fn [{:keys [db]} [_ dispatch]]
(fn [{:keys [db]} [_ event]]
(cond-> {:db (update db :dialogs pop)}
dispatch
(assoc :dispatch dispatch))))
event
(assoc :dispatch event))))
6 changes: 3 additions & 3 deletions src/renderer/element/impl/container/canvas.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
[renderer.tool.hierarchy :as tool.hierarchy]
[renderer.tool.subs :as-alias tool.s]
[renderer.utils.keyboard :as keyb]
[renderer.utils.overlay :as overlay]
[renderer.utils.pointer :as pointer]))
[renderer.utils.pointer :as pointer]
[renderer.utils.svg :as svg]))

(derive :canvas ::hierarchy/element)

Expand Down Expand Up @@ -83,7 +83,7 @@
(when snap?
[:<>
(when snapped-el
[overlay/bounding-box (:bounds snapped-el) true])
[svg/bounding-box (:bounds snapped-el) true])
(when nearest-neighbor
[snap.v/canvas-label nearest-neighbor])])

Expand Down
8 changes: 4 additions & 4 deletions src/renderer/element/impl/custom/blob.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
[renderer.ui :as ui]
[renderer.utils.element :as element]
[renderer.utils.length :as length]
[renderer.utils.overlay :as overlay]
[renderer.utils.pointer :as pointer]))
[renderer.utils.pointer :as pointer]
[renderer.utils.svg :as svg]))

(derive :blob ::hierarchy/renderable)

Expand Down Expand Up @@ -158,7 +158,7 @@
[x1 y1] (cond->> [x y] (not (element/svg? el)) (mat/add offset))
[x2 y2] (mat/add [x1 y1] size)]
[:<>
[overlay/line [x1 y1] [x2 y2]]
[svg/line [x1 y1] [x2 y2]]
[tool.v/square-handle
{:type :handle
:cursor "move"
Expand All @@ -167,4 +167,4 @@
:x x2
:y y2
:id :size}]
[overlay/times [x1 y1]]]))
[svg/times [x1 y1]]]))
16 changes: 8 additions & 8 deletions src/renderer/element/impl/custom/measure.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[renderer.element.hierarchy :as hierarchy]
[renderer.utils.length :as length]
[renderer.utils.math :as math]
[renderer.utils.overlay :as overlay]))
[renderer.utils.svg :as svg]))

(derive :measure ::hierarchy/element)

Expand All @@ -19,19 +19,19 @@
straight? (< angle 180)
straight-angle (if straight? angle (- angle 360))]
[:g {:key id}
[overlay/cross [x1 y1]]
[overlay/cross [x2 y2]]
[svg/cross [x1 y1]]
[svg/cross [x2 y2]]

[overlay/arc [x1 y1] 20 (if straight? 0 angle) (abs straight-angle)]
[svg/arc [x1 y1] 20 (if straight? 0 angle) (abs straight-angle)]

[overlay/line [x1 y1] [x2 y2] false]
[overlay/line [x1 y1] [(+ x1 (/ 30 zoom)) y1]]
[svg/line [x1 y1] [x2 y2] false]
[svg/line [x1 y1] [(+ x1 (/ 30 zoom)) y1]]

[overlay/label
[svg/label
(str (.toFixed straight-angle 2) "°")
[(+ x1 (/ 40 zoom)) y1]
"start"]

[overlay/label
[svg/label
(-> hypotenuse js/parseFloat (.toFixed 2) str)
[(/ (+ x1 x2) 2) (/ (+ y1 y2) 2)]]]))
20 changes: 10 additions & 10 deletions src/renderer/element/impl/shape/circle.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[renderer.utils.bounds :as bounds]
[renderer.utils.element :as element]
[renderer.utils.length :as length]
[renderer.utils.overlay :as overlay]))
[renderer.utils.svg :as svg]))

(derive :circle ::hierarchy/shape)

Expand Down Expand Up @@ -74,14 +74,14 @@
[cx cy] (bounds/center bounds)
r (/ (first (bounds/->dimensions bounds)) 2)]
[:g
[overlay/line [cx cy] [(+ cx r) cy]]
[overlay/label (str (.toFixed r 2)) [(+ cx (/ r 2)) cy]]
[overlay/times [cx cy]]
[svg/line [cx cy] [(+ cx r) cy]]
[svg/label (str (.toFixed r 2)) [(+ cx (/ r 2)) cy]]
[svg/times [cx cy]]
[tool.v/square-handle {:x (+ cx r)
:y cy
:id :r
:type :handle
:action :edit
:cursor "move"
:element (:id el)}
:y cy
:id :r
:type :handle
:action :edit
:cursor "move"
:element (:id el)}
[:title {:key "r-title"} "r"]]]))
12 changes: 6 additions & 6 deletions src/renderer/element/impl/shape/ellipse.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[renderer.utils.bounds :as bounds]
[renderer.utils.element :as element]
[renderer.utils.length :as length]
[renderer.utils.overlay :as overlay]))
[renderer.utils.svg :as svg]))

(derive :ellipse ::hierarchy/shape)

Expand Down Expand Up @@ -67,11 +67,11 @@
[cx cy] (bounds/center bounds)
[rx ry] (mat/div (bounds/->dimensions bounds) 2)]
[:g ::edit-handles
[overlay/times [cx cy]]
[overlay/line [cx cy] [(+ cx rx) cy]]
[overlay/label (str (.toFixed rx 2)) [(+ cx (/ rx 2)) cy]]
[overlay/line [cx cy] [cx (- cy ry)]]
[overlay/label (str (.toFixed ry 2)) [cx (- cy (/ ry 2))]]
[svg/times [cx cy]]
[svg/line [cx cy] [(+ cx rx) cy]]
[svg/label (str (.toFixed rx 2)) [(+ cx (/ rx 2)) cy]]
[svg/line [cx cy] [cx (- cy ry)]]
[svg/label (str (.toFixed ry 2)) [cx (- cy (/ ry 2))]]
(map (fn [handle]
^{:key (:id handle)}
[tool.v/square-handle
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/snap/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[renderer.snap.events :as-alias snap.e]
[renderer.snap.subs :as-alias snap.s]
[renderer.ui :as ui]
[renderer.utils.overlay :as overlay]))
[renderer.utils.svg :as svg]))

(defn options-dropdown
[]
Expand Down Expand Up @@ -59,6 +59,6 @@
label (str/join " to " (remove nil? [base-label point-label]))
point (:point nearest-neighbor)]
[:<>
[overlay/times point]
[svg/times point]
(when (not-empty label)
[overlay/label label (mat/add point margin) "start"])]))
[svg/label label (mat/add point margin) "start"])]))
12 changes: 8 additions & 4 deletions src/renderer/tool/impl/base/edit.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
[renderer.snap.handlers :as snap.h]
[renderer.tool.handlers :as h]
[renderer.tool.hierarchy :as hierarchy]
[renderer.utils.overlay :as overlay]
[renderer.utils.pointer :as pointer]))
[renderer.utils.element :as element]
[renderer.utils.pointer :as pointer]
[renderer.utils.svg :as svg]))

(derive :edit ::hierarchy/tool)

Expand Down Expand Up @@ -97,5 +98,8 @@
^{:key (str (:id el) "-edit-points")}
[:g
[element.hierarchy/render-edit el]
^{:key (str (:id el) "-centroid")}
[overlay/centroid el]])]))
(when-let [pos (element.hierarchy/centroid el)]
(let [offset (element/offset el)
pos (mat/add offset pos)]
^{:key (str (:id el) "-centroid")}
[svg/dot pos [:title "Centroid"]]))])]))
16 changes: 8 additions & 8 deletions src/renderer/tool/impl/base/transform.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
[renderer.utils.bounds :as bounds :refer [Bounds]]
[renderer.utils.element :as element]
[renderer.utils.math :refer [Vec2]]
[renderer.utils.overlay :as overlay]
[renderer.utils.pointer :as pointer]))
[renderer.utils.pointer :as pointer]
[renderer.utils.svg :as svg]))

(def ScaleHandle [:enum
:middle-right
Expand Down Expand Up @@ -152,7 +152,7 @@

(defn select-rect
[db intersecting?]
(cond-> (overlay/select-box db)
(cond-> (svg/select-box db)
(not intersecting?)
(assoc-in [:attrs :fill] "transparent")))

Expand Down Expand Up @@ -379,7 +379,7 @@
y (+ y2 (/ (+ (/ theme.db/handle-size 2) 15) zoom))
[width height] (bounds/->dimensions bounds)
text (str (.toFixed width 2) " x " (.toFixed height 2))]
[overlay/label text [x y]]))
[svg/label text [x y]]))

(m/=> area-label [:-> number? Bounds any?])
(defn area-label
Expand All @@ -390,7 +390,7 @@
x (+ x1 (/ (- x2 x1) 2))
y (+ y1 (/ (- -15 (/ theme.db/handle-size 2)) zoom))
text (str (.toFixed area 2) " px²")]
[overlay/label text [x y]])))
[svg/label text [x y]])))

(defmethod hierarchy/render :transform
[]
Expand All @@ -404,12 +404,12 @@
(for [el selected-elements]
(when (:bounds el)
^{:key (str (:id el) "-bounds")}
[overlay/bounding-box (:bounds el) false]))
[svg/bounding-box (:bounds el) false]))

(for [el hovered-ids]
(when (:bounds el)
^{:key (str (:id el) "-bounds")}
[overlay/bounding-box (:bounds el) true]))
[svg/bounding-box (:bounds el) true]))

(when (and (pos? elements-area) (= state :scale) (seq bounds))
[area-label elements-area bounds])
Expand All @@ -423,4 +423,4 @@
nil)])

(when pivot-point
[overlay/times pivot-point])]))
[svg/times pivot-point])]))
6 changes: 3 additions & 3 deletions src/renderer/tool/impl/base/zoom.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
[renderer.snap.handlers :as snap.h]
[renderer.tool.handlers :as h]
[renderer.tool.hierarchy :as hierarchy]
[renderer.utils.overlay :as overlay]
[renderer.utils.pointer :as pointer]))
[renderer.utils.pointer :as pointer]
[renderer.utils.svg :as svg]))

(derive :zoom ::hierarchy/tool)

Expand Down Expand Up @@ -42,7 +42,7 @@

(defmethod hierarchy/on-drag :zoom
[db _e]
(h/set-temp db (overlay/select-box db)))
(h/set-temp db (svg/select-box db)))

(defmethod hierarchy/on-drag-end :zoom
[db e]
Expand Down
19 changes: 1 addition & 18 deletions src/renderer/utils/overlay.cljs → src/renderer/utils/svg.cljs
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
(ns renderer.utils.overlay
(ns renderer.utils.svg
"Render functions for canvas overlay objects."
(:require
[clojure.core.matrix :as mat]
[malli.core :as m]
[re-frame.core :as rf]
[renderer.app.db :refer [App]]
[renderer.app.subs :as-alias app.s]
[renderer.document.subs :as-alias document.s]
[renderer.element.db :refer [Element]]
[renderer.element.hierarchy :as element.hierarchy]
[renderer.frame.subs :as-alias frame.s]
[renderer.snap.subs :as-alias snap.s]
[renderer.theme.db :as theme.db]
[renderer.tool.subs :as-alias tool.s]
[renderer.utils.bounds :as bounds :refer [Bounds]]
[renderer.utils.element :as element]
[renderer.utils.hiccup :refer [Hiccup]]
[renderer.utils.math :as math :refer [Vec2]]))

Expand Down Expand Up @@ -176,12 +168,3 @@
:stroke theme.db/accent
:stroke-opacity ".5"
:stroke-width (/ 1 zoom)}}))

(m/=> centroid [:-> Element any?])
(defn centroid
[el]
(when-let [pos (element.hierarchy/centroid el)]
(let [offset (element/offset el)
pos (mat/add offset pos)]
[dot pos
[:title "Centroid"]])))

0 comments on commit 971342b

Please sign in to comment.