🐛 Fix problem with gradient handlers

This commit is contained in:
alonso.torres 2022-02-09 13:04:16 +01:00
parent 85cab5031d
commit ee5b341d0e

View file

@ -35,7 +35,7 @@
(l/derived (l/in [:workspace-local :editing-stop]) st/state)) (l/derived (l/in [:workspace-local :editing-stop]) st/state))
(def current-gradient-ref (def current-gradient-ref
(l/derived (l/in [:workspace-local :current-gradient]) st/state)) (l/derived (l/in [:workspace-local :current-gradient]) st/state =))
(mf/defc shadow [{:keys [id x y width height offset]}] (mf/defc shadow [{:keys [id x y width height offset]}]
[:filter {:id id [:filter {:id id
@ -158,17 +158,19 @@
(rx/filter ms/pointer-event?) (rx/filter ms/pointer-event?)
(rx/filter #(= :viewport (:source %))) (rx/filter #(= :viewport (:source %)))
(rx/map :pt) (rx/map :pt)
(rx/subs #(case @moving-point (rx/subs
:from-p (when on-change-start (on-change-start %)) (fn [pt]
:to-p (when on-change-finish (on-change-finish %)) (case @moving-point
:width-p (when on-change-width :from-p (when on-change-start (on-change-start pt))
(let [width-v (gpt/unit (gpt/to-vec from-p width-p)) :to-p (when on-change-finish (on-change-finish pt))
distance (gpt/point-line-distance % from-p to-p) :width-p (when on-change-width
new-width-p (gpt/add (let [width-v (gpt/unit (gpt/to-vec from-p width-p))
from-p distance (gpt/point-line-distance pt from-p to-p)
(gpt/multiply width-v (gpt/point distance)))] new-width-p (gpt/add
(on-change-width new-width-p))) from-p
nil)))] (gpt/multiply width-v (gpt/point distance)))]
(on-change-width new-width-p)))
nil))))]
(fn [] (rx/dispose! subs))))) (fn [] (rx/dispose! subs)))))
[:g.gradient-handlers [:g.gradient-handlers
[:defs [:defs
@ -229,9 +231,15 @@
(mf/defc gradient-handlers (mf/defc gradient-handlers
{::mf/wrap [mf/memo]}
[{:keys [id zoom]}] [{:keys [id zoom]}]
(let [shape (mf/deref (refs/object-by-id id)) (let [current-change (mf/use-state {})
shape-ref (mf/use-memo (mf/deps id) #(refs/object-by-id id))
shape (mf/deref shape-ref)
gradient (mf/deref current-gradient-ref) gradient (mf/deref current-gradient-ref)
gradient (merge gradient @current-change)
editing-spot (mf/deref editing-spot-ref) editing-spot (mf/deref editing-spot-ref)
transform (gsh/transform-matrix shape) transform (gsh/transform-matrix shape)
@ -261,31 +269,43 @@
width-p (gpt/add from-p width-v) width-p (gpt/add from-p width-v)
change! (fn [changes] change!
(st/emit! (dc/update-gradient changes))) (mf/use-callback
(fn [changes]
(swap! current-change merge changes)
(st/emit! (dc/update-gradient changes))))
on-change-start (fn [point] on-change-start
(let [point (gpt/transform point transform-inverse) (mf/use-callback
start-x (/ (- (:x point) x) width) (mf/deps transform-inverse width height)
start-y (/ (- (:y point) y) height) (fn [point]
start-x (mth/precision start-x 2) (let [point (gpt/transform point transform-inverse)
start-y (mth/precision start-y 2)] start-x (/ (- (:x point) x) width)
(change! {:start-x start-x :start-y start-y}))) start-y (/ (- (:y point) y) height)
start-x (mth/precision start-x 2)
start-y (mth/precision start-y 2)]
(change! {:start-x start-x :start-y start-y}))))
on-change-finish (fn [point] on-change-finish
(let [point (gpt/transform point transform-inverse) (mf/use-callback
end-x (/ (- (:x point) x) width) (mf/deps transform-inverse width height)
end-y (/ (- (:y point) y) height) (fn [point]
end-x (mth/precision end-x 2) (let [point (gpt/transform point transform-inverse)
end-y (mth/precision end-y 2)] end-x (/ (- (:x point) x) width)
(change! {:end-x end-x :end-y end-y}))) end-y (/ (- (:y point) y) height)
end-x (mth/precision end-x 2)
end-y (mth/precision end-y 2)]
(change! {:end-x end-x :end-y end-y}))))
on-change-width (fn [point] on-change-width
(let [scale-factor-y (/ gradient-length (/ height 2)) (mf/use-callback
norm-dist (/ (gpt/distance point from-p) (mf/deps gradient-length width height)
(* (/ width 2) scale-factor-y))] (fn [point]
(when (and norm-dist (mth/finite? norm-dist)) (let [scale-factor-y (/ gradient-length (/ height 2))
(change! {:width norm-dist}))))] norm-dist (/ (gpt/distance point from-p)
(* (/ width 2) scale-factor-y))]
(when (and norm-dist (mth/finite? norm-dist))
(change! {:width norm-dist})))))]
(when (and gradient (when (and gradient
(= id (:shape-id gradient)) (= id (:shape-id gradient))