Snaps depending on zoom level

This commit is contained in:
alonso.torres 2021-01-27 17:12:37 +01:00
parent 44eb961c27
commit 1273336622
3 changed files with 27 additions and 23 deletions

View file

@ -57,10 +57,10 @@
stoper (rx/filter stoper? stream) stoper (rx/filter stoper? stream)
initial @ms/mouse-position initial @ms/mouse-position
page-id (:current-page-id state) page-id (:current-page-id state)
objects (dwc/lookup-page-objects state page-id) objects (dwc/lookup-page-objects state page-id)
layout (get state :workspace-layout) layout (get state :workspace-layout)
zoom (get-in state [:workspace-local :zoom] 1)
frames (cp/select-frames objects) frames (cp/select-frames objects)
fid (or (->> frames fid (or (->> frames
@ -80,7 +80,7 @@
(rx/of #(assoc-in state [:workspace-drawing :object] shape)) (rx/of #(assoc-in state [:workspace-drawing :object] shape))
;; Initial SNAP ;; Initial SNAP
(->> (snap/closest-snap-point page-id [shape] layout initial) (->> (snap/closest-snap-point page-id [shape] layout zoom initial)
(rx/map move-drawing)) (rx/map move-drawing))
(->> ms/mouse-position (->> ms/mouse-position
@ -88,7 +88,7 @@
(rx/with-latest vector ms/mouse-position-ctrl) (rx/with-latest vector ms/mouse-position-ctrl)
(rx/switch-map (rx/switch-map
(fn [[point :as current]] (fn [[point :as current]]
(->> (snap/closest-snap-point page-id [shape] layout point) (->> (snap/closest-snap-point page-id [shape] layout zoom point)
(rx/map #(conj current %))))) (rx/map #(conj current %)))))
(rx/map (rx/map
(fn [[_ ctrl? point]] (fn [[_ ctrl? point]]

View file

@ -140,6 +140,7 @@
stoper (rx/filter ms/mouse-up? stream) stoper (rx/filter ms/mouse-up? stream)
layout (:workspace-layout state) layout (:workspace-layout state)
page-id (:current-page-id state) page-id (:current-page-id state)
zoom (get-in state [:workspace-local :zoom] 1)
objects (dwc/lookup-page-objects state page-id) objects (dwc/lookup-page-objects state page-id)
resizing-shapes (map #(get objects %) ids) resizing-shapes (map #(get objects %) ids)
text-shapes-ids (->> resizing-shapes text-shapes-ids (->> resizing-shapes
@ -151,7 +152,7 @@
(rx/with-latest vector ms/mouse-position-shift) (rx/with-latest vector ms/mouse-position-shift)
(rx/map normalize-proportion-lock) (rx/map normalize-proportion-lock)
(rx/switch-map (fn [[point :as current]] (rx/switch-map (fn [[point :as current]]
(->> (snap/closest-snap-point page-id resizing-shapes layout point) (->> (snap/closest-snap-point page-id resizing-shapes layout zoom point)
(rx/map #(conj current %))))) (rx/map #(conj current %)))))
(rx/mapcat (partial resize shape initial-position resizing-shapes)) (rx/mapcat (partial resize shape initial-position resizing-shapes))
(rx/take-until stoper)) (rx/take-until stoper))
@ -284,6 +285,7 @@
shapes (mapv #(get objects %) ids) shapes (mapv #(get objects %) ids)
stopper (rx/filter ms/mouse-up? stream) stopper (rx/filter ms/mouse-up? stream)
layout (get state :workspace-layout) layout (get state :workspace-layout)
zoom (get-in state [:workspace-local :zoom] 1)
position (->> ms/mouse-position position (->> ms/mouse-position
@ -291,7 +293,7 @@
(rx/map #(gpt/to-vec from-position %))) (rx/map #(gpt/to-vec from-position %)))
snap-delta (->> position snap-delta (->> position
(rx/switch-map #(snap/closest-snap-move page-id shapes objects layout %)))] (rx/switch-map #(snap/closest-snap-move page-id shapes objects layout zoom %)))]
(rx/concat (rx/concat
(->> snap-delta (->> snap-delta
(rx/with-latest vector position) (rx/with-latest vector position)

View file

@ -69,8 +69,9 @@
(rx/map flatten-to-points)))) (rx/map flatten-to-points))))
(defn- search-snap (defn- search-snap
[page-id frame-id points coord filter-shapes] [page-id frame-id points coord filter-shapes zoom]
(let [ranges (->> points (let [snap-accuracy (/ snap-accuracy zoom)
ranges (->> points
(map coord) (map coord)
(mapv #(vector (- % snap-accuracy) (mapv #(vector (- % snap-accuracy)
(+ % snap-accuracy))))] (+ % snap-accuracy))))]
@ -90,9 +91,9 @@
(gpt/to-vec from to)))) (gpt/to-vec from to))))
(defn- closest-snap (defn- closest-snap
[page-id frame-id points filter-shapes] [page-id frame-id points filter-shapes zoom]
(let [snap-x (search-snap page-id frame-id points :x filter-shapes) (let [snap-x (search-snap page-id frame-id points :x filter-shapes zoom)
snap-y (search-snap page-id frame-id points :y filter-shapes)] snap-y (search-snap page-id frame-id points :y filter-shapes zoom)]
;; snap-x is the second parameter because is the "source" to combine ;; snap-x is the second parameter because is the "source" to combine
(rx/combine-latest snap->vector snap-y snap-x))) (rx/combine-latest snap->vector snap-y snap-x)))
@ -114,8 +115,9 @@
(and (>= s1c1 s2c1) (<= s1c1 s2c2)) (and (>= s1c1 s2c1) (<= s1c1 s2c2))
(and (>= s1c2 s2c1) (<= s1c2 s2c2))))) (and (>= s1c2 s2c1) (<= s1c2 s2c2)))))
(defn calculate-snap [coord selrect shapes-lt shapes-gt] (defn calculate-snap [coord selrect shapes-lt shapes-gt zoom]
(let [dist-lt (fn [other] (sr-distance coord (:selrect other) selrect)) (let [snap-distance-accuracy (/ snap-distance-accuracy zoom)
dist-lt (fn [other] (sr-distance coord (:selrect other) selrect))
dist-gt (fn [other] (sr-distance coord selrect (:selrect other))) dist-gt (fn [other] (sr-distance coord selrect (:selrect other)))
;; Calculates the snap distance when in the middle of two shapes ;; Calculates the snap distance when in the middle of two shapes
@ -142,7 +144,7 @@
(fn [acc val] (fn [acc val]
;; Using a number is faster than accesing the variable. ;; Using a number is faster than accesing the variable.
;; Keep up to date with `snap-distance-accuracy` ;; Keep up to date with `snap-distance-accuracy`
(if (and (<= val 10) (>= val (- 10))) (if (and (<= val snap-distance-accuracy) (>= val (- snap-distance-accuracy)))
(min acc val) (min acc val)
acc)) acc))
@ -171,11 +173,11 @@
(if (mth/finite? min-snap) [0 min-snap] nil))) (if (mth/finite? min-snap) [0 min-snap] nil)))
(defn search-snap-distance [selrect coord shapes-lt shapes-gt] (defn search-snap-distance [selrect coord shapes-lt shapes-gt zoom]
(->> shapes-lt (->> shapes-lt
(rx/combine-latest vector shapes-gt) (rx/combine-latest vector shapes-gt)
(rx/map (fn [[shapes-lt shapes-gt]] (rx/map (fn [[shapes-lt shapes-gt]]
(calculate-snap coord selrect shapes-lt shapes-gt))))) (calculate-snap coord selrect shapes-lt shapes-gt zoom)))))
(defn select-shapes-area (defn select-shapes-area
[page-id shapes objects area-selrect] [page-id shapes objects area-selrect]
@ -187,7 +189,7 @@
(rx/map (fn [ids] (map #(get objects %) ids))))) (rx/map (fn [ids] (map #(get objects %) ids)))))
(defn closest-distance-snap (defn closest-distance-snap
[page-id shapes objects movev] [page-id shapes objects zoom movev]
(let [frame-id (snap-frame-id shapes) (let [frame-id (snap-frame-id shapes)
frame (get objects frame-id) frame (get objects frame-id)
selrect (->> shapes (map #(gsh/move % movev)) gsh/selection-rect)] selrect (->> shapes (map #(gsh/move % movev)) gsh/selection-rect)]
@ -197,12 +199,12 @@
(let [areas (->> (gsh/selrect->areas (or (:selrect frame) (let [areas (->> (gsh/selrect->areas (or (:selrect frame)
(gsh/rect->selrect @refs/vbox)) selrect) (gsh/rect->selrect @refs/vbox)) selrect)
(d/mapm #(select-shapes-area page-id shapes objects %2))) (d/mapm #(select-shapes-area page-id shapes objects %2)))
snap-x (search-snap-distance selrect :x (:left areas) (:right areas)) snap-x (search-snap-distance selrect :x (:left areas) (:right areas) zoom)
snap-y (search-snap-distance selrect :y (:top areas) (:bottom areas))] snap-y (search-snap-distance selrect :y (:top areas) (:bottom areas) zoom)]
(rx/combine-latest snap->vector snap-y snap-x))))))) (rx/combine-latest snap->vector snap-y snap-x)))))))
(defn closest-snap-point (defn closest-snap-point
[page-id shapes layout point] [page-id shapes layout zoom point]
(let [frame-id (snap-frame-id shapes) (let [frame-id (snap-frame-id shapes)
filter-shapes (into #{} (map :id shapes)) filter-shapes (into #{} (map :id shapes))
filter-shapes (fn [id] (if (= id :layout) filter-shapes (fn [id] (if (= id :layout)
@ -210,12 +212,12 @@
(not (contains? layout :snap-grid))) (not (contains? layout :snap-grid)))
(or (filter-shapes id) (or (filter-shapes id)
(not (contains? layout :dynamic-alignment)))))] (not (contains? layout :dynamic-alignment)))))]
(->> (closest-snap page-id frame-id [point] filter-shapes) (->> (closest-snap page-id frame-id [point] filter-shapes zoom)
(rx/map #(or % (gpt/point 0 0))) (rx/map #(or % (gpt/point 0 0)))
(rx/map #(gpt/add point %))))) (rx/map #(gpt/add point %)))))
(defn closest-snap-move (defn closest-snap-move
[page-id shapes objects layout movev] [page-id shapes objects layout zoom movev]
(let [frame-id (snap-frame-id shapes) (let [frame-id (snap-frame-id shapes)
filter-shapes (into #{} (map :id shapes)) filter-shapes (into #{} (map :id shapes))
filter-shapes (fn [id] (if (= id :layout) filter-shapes (fn [id] (if (= id :layout)
@ -232,9 +234,9 @@
;; Move the points in the translation vector ;; Move the points in the translation vector
(map #(gpt/add % movev)))] (map #(gpt/add % movev)))]
(->> (rx/merge (closest-snap page-id frame-id shapes-points filter-shapes) (->> (rx/merge (closest-snap page-id frame-id shapes-points filter-shapes zoom)
(when (contains? layout :dynamic-alignment) (when (contains? layout :dynamic-alignment)
(closest-distance-snap page-id shapes objects movev))) (closest-distance-snap page-id shapes objects zoom movev)))
(rx/reduce gpt/min) (rx/reduce gpt/min)
(rx/map #(or % (gpt/point 0 0)))))) (rx/map #(or % (gpt/point 0 0))))))