0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-15 01:01:30 -05:00

♻️ Refactor drawing effects

This commit is contained in:
alonso.torres 2020-11-05 10:09:02 +01:00
parent 67ed1d89ac
commit 10a24d68c9
5 changed files with 187 additions and 164 deletions

View file

@ -57,13 +57,13 @@
(watch [_ state stream] (watch [_ state stream]
(rx/of (case type (rx/of (case type
:path :path
path/handle-drawing-path (path/handle-drawing-path)
:curve :curve
curve/handle-drawing-curve (curve/handle-drawing-curve)
;; default ;; default
box/handle-drawing-box))))) (box/handle-drawing-box))))))
;; Export ;; Export
(def close-drawing-path path/close-drawing-path) (def close-drawing-path path/close-drawing-path)

View file

@ -20,8 +20,7 @@
[app.main.streams :as ms] [app.main.streams :as ms]
[app.main.data.workspace.drawing.common :as common])) [app.main.data.workspace.drawing.common :as common]))
(def handle-drawing-box (defn resize-shape [{:keys [x y width height] :as shape} point lock? point-snap]
(letfn [(resize-shape [{:keys [x y width height] :as shape} point lock? point-snap]
(let [;; The new shape behaves like a resize on the bottom-right corner (let [;; The new shape behaves like a resize on the bottom-right corner
initial (gpt/point (+ x width) (+ y height)) initial (gpt/point (+ x width) (+ y height))
shapev (gpt/point width height) shapev (gpt/point width height)
@ -37,9 +36,10 @@
(assoc-in [:modifiers :resize-origin] (gpt/point x y)) (assoc-in [:modifiers :resize-origin] (gpt/point x y))
(assoc-in [:modifiers :resize-rotation] 0)))) (assoc-in [:modifiers :resize-rotation] 0))))
(update-drawing [state point lock? point-snap] (defn update-drawing [state point lock? point-snap]
(update-in state [:workspace-drawing :object] resize-shape point lock? point-snap))] (update-in state [:workspace-drawing :object] resize-shape point lock? point-snap))
(defn handle-drawing-box []
(ptk/reify ::handle-drawing-box (ptk/reify ::handle-drawing-box
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
@ -88,4 +88,4 @@
#(update-drawing % pt ctrl? point-snap))) #(update-drawing % pt ctrl? point-snap)))
(rx/take-until stoper)) (rx/take-until stoper))
(rx/of common/handle-finish-drawing))))))) (rx/of common/handle-finish-drawing))))))

View file

@ -18,24 +18,24 @@
(def simplify-tolerance 0.3) (def simplify-tolerance 0.3)
(def handle-drawing-curve (defn stoper-event? [{:keys [type shift] :as event}]
(letfn [(stoper-event? [{:keys [type shift] :as event}]
(ms/mouse-event? event) (= type :up)) (ms/mouse-event? event) (= type :up))
(initialize-drawing [state] (defn initialize-drawing [state]
(assoc-in state [:workspace-drawing :object :initialized?] true)) (assoc-in state [:workspace-drawing :object :initialized?] true))
(insert-point-segment [state point] (defn insert-point-segment [state point]
(update-in state [:workspace-drawing :object :segments] (fnil conj []) point)) (update-in state [:workspace-drawing :object :segments] (fnil conj []) point))
(finish-drawing-curve [state] (defn finish-drawing-curve [state]
(update-in (update-in
state [:workspace-drawing :object] state [:workspace-drawing :object]
(fn [shape] (fn [shape]
(-> shape (-> shape
(update :segments #(path/simplify % simplify-tolerance)) (update :segments #(path/simplify % simplify-tolerance))
(gsh/update-path-selrect)))))] (gsh/update-path-selrect)))))
(defn handle-drawing-curve []
(ptk/reify ::handle-drawing-curve (ptk/reify ::handle-drawing-curve
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
@ -48,4 +48,4 @@
(rx/map (fn [pt] #(insert-point-segment % pt))) (rx/map (fn [pt] #(insert-point-segment % pt)))
(rx/take-until stoper)) (rx/take-until stoper))
(rx/of finish-drawing-curve (rx/of finish-drawing-curve
common/handle-finish-drawing))))))) common/handle-finish-drawing))))))

View file

@ -17,8 +17,7 @@
[app.util.geom.path :as path] [app.util.geom.path :as path]
[app.main.data.workspace.drawing.common :as common])) [app.main.data.workspace.drawing.common :as common]))
(def handle-drawing-path (defn stoper-event? [{:keys [type shift] :as event}]
(letfn [(stoper-event? [{:keys [type shift] :as event}]
(or (= event ::end-path-drawing) (or (= event ::end-path-drawing)
(= event :interrupt) (= event :interrupt)
(and (ms/mouse-event? event) (and (ms/mouse-event? event)
@ -28,28 +27,47 @@
(= type :down) (= type :down)
(= 13 (:key event))))) (= 13 (:key event)))))
(initialize-drawing [state point] (defn init-path []
(-> state (fn [state]
(assoc-in [:workspace-drawing :object :segments] [point point]) (update-in state [:workspace-drawing :object]
(assoc-in [:workspace-drawing :object :initialized?] true))) assoc :content []
:initialized? true)))
(insert-point-segment [state point] (defn add-path-command [command]
(-> state (fn [state]
(update-in [:workspace-drawing :object :segments] (fnil conj []) point))) (update-in state [:workspace-drawing :object :content] conj command)))
(update-point-segment [state index point] #_(defn update-point-segment [state index point]
(let [segments (count (get-in state [:workspace-drawing :object :segments])) (let [segments (count (get-in state [:workspace-drawing :object :segments]))
exists? (< -1 index segments)] exists? (< -1 index segments)]
(cond-> state (cond-> state
exists? (assoc-in [:workspace-drawing :object :segments index] point)))) exists? (assoc-in [:workspace-drawing :object :segments index] point))))
(finish-drawing-path [state] (defn finish-drawing-path []
(fn [state]
(update-in (update-in
state [:workspace-drawing :object] state [:workspace-drawing :object]
(fn [shape] (-> shape (fn [shape] (-> shape
(update :segments #(vec (butlast %))) (update :segments #(vec (butlast %)))
(gsh/update-path-selrect)))))] (gsh/update-path-selrect))))))
(defn handle-drawing-path []
(ptk/reify ::handle-drawing-path
ptk/WatchEvent
(watch [_ state stream]
;; clicks stream<[MouseEvent, Position]>
clicks (->> stream
(rx/filter ms/mouse-click?)
(rx/with-latest vector ms/mouse-position))
)))
#_(def handle-drawing-path
(ptk/reify ::handle-drawing-path (ptk/reify ::handle-drawing-path
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
@ -107,7 +125,7 @@
point)] point)]
#(update-point-segment % index point))))) #(update-point-segment % index point)))))
(rx/of finish-drawing-path (rx/of finish-drawing-path
common/handle-finish-drawing)))))))) common/handle-finish-drawing)))))))
(defn close-drawing-path [] (defn close-drawing-path []
(ptk/reify ::close-drawing-path (ptk/reify ::close-drawing-path

View file

@ -26,6 +26,11 @@
[v] [v]
(instance? MouseEvent v)) (instance? MouseEvent v))
(defn mouse-down?
[v]
(and (mouse-event? v)
(= :down (:type v))))
(defn mouse-up? (defn mouse-up?
[v] [v]
(and (mouse-event? v) (and (mouse-event? v)