0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-09 08:38:15 -05:00

Adjustments to measurement guides

This commit is contained in:
alonso.torres 2020-10-29 11:53:28 +01:00 committed by Hirunatan
parent 8afe037def
commit 03c8b42296
2 changed files with 83 additions and 41 deletions

View file

@ -542,6 +542,14 @@
(< ry1 sy2) (< ry1 sy2)
(> ry2 sy1)))) (> ry2 sy1))))
(defn fully-contained?
"Checks if one rect is fully inside the other"
[rect other]
(and (<= (:x1 rect) (:x1 other))
(>= (:x2 rect) (:x2 other))
(<= (:y1 rect) (:y1 other))
(>= (:y2 rect) (:y2 other))))
(defn has-point? (defn has-point?
[shape position] [shape position]
(let [{:keys [x y]} position (let [{:keys [x y]} position
@ -956,3 +964,4 @@
:width width :height height :width width :height height
:x1 x :y1 y :x1 x :y1 y
:x2 (+ x width) :y2 (+ y height)}))) :x2 (+ x width) :y2 (+ y height)})))

View file

@ -177,45 +177,42 @@
:font-size (/ font-size zoom)}} :font-size (/ font-size zoom)}}
size-label]])) size-label]]))
(mf/defc distance-display-pill [{:keys [x y zoom distance frame]}]
(mf/defc distance-display [{:keys [type from to zoom]}] (let [distance-pill-width (/ distance-pill-width zoom)
(let [h-lines (let [y (+ (:y from) (/ (:height from) 2))]
(->> (calculate-distance-lines (:x1 from) (:x2 from) (:x1 to) (:x2 to))
(map (fn [[start end]] [start y end y]))))
v-lines (let [x (+ (:x from) (/ (:width from) 2))]
(->> (calculate-distance-lines (:y1 from) (:y2 from) (:y1 to) (:y2 to))
(map (fn [[start end]] [x start x end]))))
lines (d/concat [] v-lines h-lines)
distance-pill-width (/ distance-pill-width zoom)
distance-pill-height (/ distance-pill-height zoom) distance-pill-height (/ distance-pill-height zoom)
distance-line-stroke (/ distance-line-stroke zoom) distance-line-stroke (/ distance-line-stroke zoom)
font-size (/ font-size zoom) font-size (/ font-size zoom)
text-padding (/ 3 zoom) text-padding (/ 3 zoom)
distance-border-radius (/ distance-border-radius zoom)] distance-border-radius (/ distance-border-radius zoom)
(for [[x1 y1 x2 y2] lines] {frame-width :width frame-height :height} frame
(let [center-x (+ x1 (/ (- x2 x1) 2))
center-y (+ y1 (/ (- y2 y1) 2))] rect-x (- x (/ distance-pill-width 2))
[:g.distance-line {:key (str "line-%s-%s-%s-%s" x1 y1 x2 y2)} rect-y (- y (/ distance-pill-height 2))
[:line {:x1 x1
:y1 y1 text-x x
:x2 x2 text-y (+ y text-padding)
:y2 y2
:style {:stroke distance-color offset-x (cond (< rect-x 0) (- rect-x)
:stroke-width distance-line-stroke}}] (> (+ rect-x distance-pill-width) frame-width) (- frame-width (+ rect-x distance-pill-width))
[:rect {:x (- center-x (/ distance-pill-width 2)) :else 0)
:y (- center-y (/ distance-pill-height 2))
offset-y (cond (< rect-y 0) (- rect-y)
(> (+ rect-y distance-pill-height) frame-height) (- frame-height (+ rect-y distance-pill-height))
:else 0)
]
[:g.distance-pill
[:rect {:x (+ rect-x offset-x)
:y (+ rect-y offset-y)
:rx distance-border-radius :rx distance-border-radius
:ry distance-border-radius :ry distance-border-radius
:width distance-pill-width :width distance-pill-width
:height distance-pill-height :height distance-pill-height
:style {:fill distance-color}}] :style {:fill distance-color}}]
[:text {:x center-x [:text {:x (+ text-x offset-x)
:y (+ center-y text-padding) :y (+ text-y offset-y)
:rx distance-border-radius :rx distance-border-radius
:ry distance-border-radius :ry distance-border-radius
:text-anchor "middle" :text-anchor "middle"
@ -223,8 +220,44 @@
:height distance-pill-height :height distance-pill-height
:style {:fill distance-text-color :style {:fill distance-text-color
:font-size font-size}} :font-size font-size}}
(str (mth/round distance]])
(gpt/distance (gpt/point x1 y1) (gpt/point x2 y2))) "px")]])))) )
(mf/defc distance-display [{:keys [type from to zoom frame]}]
(let [fixed-x (if (gsh/fully-contained? from to)
(+ (:x to) (/ (:width to) 2))
(+ (:x from) (/ (:width from) 2)))
fixed-y (if (gsh/fully-contained? from to)
(+ (:y to) (/ (:height to) 2))
(+ (:y from) (/ (:height from) 2)))
v-lines (->> (calculate-distance-lines (:y1 from) (:y2 from) (:y1 to) (:y2 to))
(map (fn [[start end]] [fixed-x start fixed-x end])))
h-lines (->> (calculate-distance-lines (:x1 from) (:x2 from) (:x1 to) (:x2 to))
(map (fn [[start end]] [start fixed-y end fixed-y])))
lines (d/concat [] v-lines h-lines)]
(for [[x1 y1 x2 y2] lines]
(let [center-x (+ x1 (/ (- x2 x1) 2))
center-y (+ y1 (/ (- y2 y1) 2))
distance (gpt/distance (gpt/point x1 y1) (gpt/point x2 y2))]
[:g.distance-line {:key (str "line-%s-%s-%s-%s" x1 y1 x2 y2)}
[:line
{:x1 x1
:y1 y1
:x2 x2
:y2 y2
:style {:stroke distance-color
:stroke-width distance-line-stroke}}]
[:& distance-display-pill
{:x center-x
:y center-y
:zoom zoom
:distance (str (mth/round distance) "px")
:frame frame}]]))))
(mf/defc selection-feedback [{:keys [frame]}] (mf/defc selection-feedback [{:keys [frame]}]
(let [zoom (mf/deref selected-zoom) (let [zoom (mf/deref selected-zoom)
@ -246,11 +279,11 @@
(if (and (not-empty selected-shapes) (not hover-shape)) (if (and (not-empty selected-shapes) (not hover-shape))
[:g.hover-shapes [:g.hover-shapes
[:& distance-display {:from (frame->selrect frame) :to selrect :zoom zoom}]] [:& distance-display {:from (frame->selrect frame) :to selrect :zoom zoom :frame frame}]]
(let [hover-selrect (-> hover-shape (gsh/translate-to-frame frame) :selrect)] (let [hover-selrect (-> hover-shape (gsh/translate-to-frame frame) :selrect)]
[:g.hover-shapes [:g.hover-shapes
[:& selection-rect {:type :hover :selrect hover-selrect :zoom zoom}] [:& selection-rect {:type :hover :selrect hover-selrect :zoom zoom}]
[:& size-display {:selrect hover-selrect :zoom zoom}] [:& size-display {:selrect hover-selrect :zoom zoom}]
[:& distance-display {:from hover-selrect :to selrect :zoom zoom}]]))]))) [:& distance-display {:from hover-selrect :to selrect :zoom zoom :frame frame}]]))])))