Improved opposite handler management

This commit is contained in:
alonso.torres 2020-11-26 10:50:24 +01:00
parent 89f0f24707
commit f8cf7103ca
3 changed files with 44 additions and 34 deletions

View file

@ -62,9 +62,6 @@
(s/def ::content (s/def ::content
(s/coll-of ::content-entry :kind vector?)) (s/coll-of ::content-entry :kind vector?))
(s/def ::path-shape
(s/keys :req-un [::content]))
;; CONSTANTS ;; CONSTANTS
(defonce enter-keycode 13) (defonce enter-keycode 13)
@ -234,7 +231,6 @@
last-point (get-in state [:workspace-local :edit-path id :last-point]) last-point (get-in state [:workspace-local :edit-path id :last-point])
position (cond-> (gpt/point x y) position (cond-> (gpt/point x y)
fix-angle? (position-fixed-angle last-point)) fix-angle? (position-fixed-angle last-point))
shape (get-in state (get-path state)) shape (get-in state (get-path state))
{:keys [last-point prev-handler]} (get-in state [:workspace-local :edit-path id]) {:keys [last-point prev-handler]} (get-in state [:workspace-local :edit-path id])
command (next-node shape position last-point prev-handler)] command (next-node shape position last-point prev-handler)]
@ -278,21 +274,22 @@
(= command :line-to) (= command :line-to)
(update-in (get-path state :content index) make-curve)))))) (update-in (get-path state :content index) make-curve))))))
(defn drag-handler [{:keys [x y]}] (defn drag-handler [{:keys [x y alt? shift?]}]
(ptk/reify ::drag-handler (ptk/reify ::drag-handler
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
(let [id (get-path-id state) (let [id (get-path-id state)
handler-position (gpt/point x y)
shape (get-in state (get-path state)) shape (get-in state (get-path state))
content (:content shape) content (:content shape)
index (dec (count content)) index (dec (count content))
node-position (ugp/command->point (nth content index)) node-position (ugp/command->point (nth content index))
handler-position (cond-> (gpt/point x y)
shift? (position-fixed-angle node-position))
{dx :x dy :y} (gpt/subtract handler-position node-position) {dx :x dy :y} (gpt/subtract handler-position node-position)
match-opposite? true match-opposite? (not alt?)
modifiers (move-handler-modifiers content (inc index) :c1 match-opposite? dx dy)] modifiers (move-handler-modifiers content (inc index) :c1 match-opposite? dx dy)]
(-> state (-> state
(assoc-in [:workspace-local :edit-path id :content-modifiers] modifiers) (update-in [:workspace-local :edit-path id :content-modifiers] merge modifiers)
(assoc-in [:workspace-local :edit-path id :prev-handler] handler-position) (assoc-in [:workspace-local :edit-path id :prev-handler] handler-position)
(assoc-in [:workspace-local :edit-path id :drag-handler] handler-position)))))) (assoc-in [:workspace-local :edit-path id :drag-handler] handler-position))))))
@ -331,16 +328,11 @@
(->> stream (rx/filter #(or (end-path-event? %) (->> stream (rx/filter #(or (end-path-event? %)
(ms/mouse-up? %)))) (ms/mouse-up? %))))
position-stream
(->> ms/mouse-position
(rx/take-until stop-stream)
(rx/throttle 50))
drag-events-stream drag-events-stream
(->> position-stream (->> (position-stream)
(rx/take-until stop-stream)
(rx/map #(drag-handler %)))] (rx/map #(drag-handler %)))]
(rx/concat (rx/concat
(rx/of (add-node position)) (rx/of (add-node position))
(drag-stream (drag-stream
@ -436,7 +428,7 @@
[stream down-event zoom] [stream down-event zoom]
(let [mouse-up (->> stream (rx/filter #(or (end-path-event? %) (let [mouse-up (->> stream (rx/filter #(or (end-path-event? %)
(ms/mouse-up? %)))) (ms/mouse-up? %))))
drag-events (->> ms/mouse-position drag-events (->> (position-stream)
(rx/take-until mouse-up) (rx/take-until mouse-up)
(rx/map #(drag-handler %)))] (rx/map #(drag-handler %)))]
@ -567,6 +559,14 @@
(defn save-path-content [] (defn save-path-content []
(ptk/reify ::save-path-content (ptk/reify ::save-path-content
ptk/UpdateEvent
(update [_ state]
(let [content (get-in state (get-path state :content))
content (if (= (-> content last :command) :move-to)
(into [] (take (dec (count content)) content))
content)]
(assoc-in state (get-path state :content) content)))
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
(let [id (get-in state [:workspace-local :edition]) (let [id (get-in state [:workspace-local :edition])
@ -653,25 +653,39 @@
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
(let [id (get-in state [:workspace-local :edition]) (let [id (get-in state [:workspace-local :edition])
[cx cy] (if (= prefix :c1) [:c1x :c1y] [:c2x :c2y]) cx (ud/prefix-keyword prefix :x)
cy (ud/prefix-keyword prefix :y)
start-point @ms/mouse-position start-point @ms/mouse-position
start-delta-x (get-in state [:workspace-local :edit-path id :content-modifiers index cx] 0) modifiers (get-in state [:workspace-local :edit-path id :content-modifiers])
start-delta-y (get-in state [:workspace-local :edit-path id :content-modifiers index cy] 0)] start-delta-x (get-in modifiers [index cx] 0)
start-delta-y (get-in modifiers [index cy] 0)
content (get-in state (get-path state :content))
opposite-index (ugp/opposite-index content index prefix)
opposite-prefix (if (= prefix :c1) :c2 :c1)
opposite-handler (-> content (get opposite-index) (ugp/get-handler opposite-prefix))
point (-> content (get (if (= prefix :c1) (dec index) index)) (ugp/command->point))
handler (-> content (get index) (ugp/get-handler prefix))
current-distance (gpt/distance (ugp/opposite-handler point handler) opposite-handler)
match-opposite? (mth/almost-zero? current-distance)]
(drag-stream (drag-stream
(rx/concat (rx/concat
(->> ms/mouse-position (->> (position-stream)
(rx/take-until (->> stream (rx/filter ms/mouse-up?))) (rx/take-until (->> stream (rx/filter ms/mouse-up?)))
(rx/with-latest vector ms/mouse-position-alt)
(rx/map (rx/map
(fn [[pos alt?]] (fn [{:keys [x y alt? shift?]}]
(modify-handler (let [pos (cond-> (gpt/point x y)
id shift? (position-fixed-angle point))]
index (modify-handler
prefix id
(+ start-delta-x (- (:x pos) (:x start-point))) index
(+ start-delta-y (- (:y pos) (:y start-point))) prefix
(not alt?))))) (+ start-delta-x (- (:x pos) (:x start-point)))
(+ start-delta-y (- (:y pos) (:y start-point)))
(and (not alt?) match-opposite?))))))
(rx/concat (rx/of (apply-content-modifiers))))))))) (rx/concat (rx/of (apply-content-modifiers)))))))))
(defn start-draw-mode [] (defn start-draw-mode []

View file

@ -333,10 +333,6 @@
(when drag-handler (when drag-handler
[:g.drag-handler {:pointer-events "none"} [:g.drag-handler {:pointer-events "none"}
(when (not= :move-to (:command last-command))
[:& path-handler {:point last-p
:handler (ugp/opposite-handler last-p drag-handler)
:zoom zoom}])
[:& path-handler {:point last-p [:& path-handler {:point last-p
:handler drag-handler :handler drag-handler
:zoom zoom}]])])) :zoom zoom}]])]))

View file

@ -368,7 +368,7 @@
calculate-vector calculate-vector
(fn [point next prev] (fn [point next prev]
(let [base-vector (if (or (nil? next) (nil? prev)) (let [base-vector (if (or (nil? next) (nil? prev) (= next prev))
(-> (gpt/to-vec point (or next prev)) (-> (gpt/to-vec point (or next prev))
(gpt/normal-left)) (gpt/normal-left))
(gpt/to-vec next prev))] (gpt/to-vec next prev))]