mirror of
https://github.com/penpot/penpot.git
synced 2025-03-14 16:51:18 -05:00
♻️ Refactor drawing effects
This commit is contained in:
parent
67ed1d89ac
commit
10a24d68c9
5 changed files with 187 additions and 164 deletions
|
@ -57,13 +57,13 @@
|
|||
(watch [_ state stream]
|
||||
(rx/of (case type
|
||||
:path
|
||||
path/handle-drawing-path
|
||||
(path/handle-drawing-path)
|
||||
|
||||
:curve
|
||||
curve/handle-drawing-curve
|
||||
(curve/handle-drawing-curve)
|
||||
|
||||
;; default
|
||||
box/handle-drawing-box)))))
|
||||
(box/handle-drawing-box))))))
|
||||
|
||||
;; Export
|
||||
(def close-drawing-path path/close-drawing-path)
|
||||
|
|
|
@ -20,8 +20,7 @@
|
|||
[app.main.streams :as ms]
|
||||
[app.main.data.workspace.drawing.common :as common]))
|
||||
|
||||
(def handle-drawing-box
|
||||
(letfn [(resize-shape [{:keys [x y width height] :as shape} point lock? point-snap]
|
||||
(defn 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
|
||||
initial (gpt/point (+ x width) (+ y height))
|
||||
shapev (gpt/point width height)
|
||||
|
@ -37,9 +36,10 @@
|
|||
(assoc-in [:modifiers :resize-origin] (gpt/point x y))
|
||||
(assoc-in [:modifiers :resize-rotation] 0))))
|
||||
|
||||
(update-drawing [state point lock? point-snap]
|
||||
(update-in state [:workspace-drawing :object] resize-shape point lock? point-snap))]
|
||||
(defn update-drawing [state 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/WatchEvent
|
||||
(watch [_ state stream]
|
||||
|
@ -88,4 +88,4 @@
|
|||
#(update-drawing % pt ctrl? point-snap)))
|
||||
|
||||
(rx/take-until stoper))
|
||||
(rx/of common/handle-finish-drawing)))))))
|
||||
(rx/of common/handle-finish-drawing))))))
|
||||
|
|
|
@ -18,24 +18,24 @@
|
|||
|
||||
(def simplify-tolerance 0.3)
|
||||
|
||||
(def handle-drawing-curve
|
||||
(letfn [(stoper-event? [{:keys [type shift] :as event}]
|
||||
(defn stoper-event? [{:keys [type shift] :as event}]
|
||||
(ms/mouse-event? event) (= type :up))
|
||||
|
||||
(initialize-drawing [state]
|
||||
(defn initialize-drawing [state]
|
||||
(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))
|
||||
|
||||
(finish-drawing-curve [state]
|
||||
(defn finish-drawing-curve [state]
|
||||
(update-in
|
||||
state [:workspace-drawing :object]
|
||||
(fn [shape]
|
||||
(-> shape
|
||||
(update :segments #(path/simplify % simplify-tolerance))
|
||||
(gsh/update-path-selrect)))))]
|
||||
(gsh/update-path-selrect)))))
|
||||
|
||||
(defn handle-drawing-curve []
|
||||
(ptk/reify ::handle-drawing-curve
|
||||
ptk/WatchEvent
|
||||
(watch [_ state stream]
|
||||
|
@ -48,4 +48,4 @@
|
|||
(rx/map (fn [pt] #(insert-point-segment % pt)))
|
||||
(rx/take-until stoper))
|
||||
(rx/of finish-drawing-curve
|
||||
common/handle-finish-drawing)))))))
|
||||
common/handle-finish-drawing))))))
|
||||
|
|
|
@ -17,8 +17,7 @@
|
|||
[app.util.geom.path :as path]
|
||||
[app.main.data.workspace.drawing.common :as common]))
|
||||
|
||||
(def handle-drawing-path
|
||||
(letfn [(stoper-event? [{:keys [type shift] :as event}]
|
||||
(defn stoper-event? [{:keys [type shift] :as event}]
|
||||
(or (= event ::end-path-drawing)
|
||||
(= event :interrupt)
|
||||
(and (ms/mouse-event? event)
|
||||
|
@ -28,28 +27,47 @@
|
|||
(= type :down)
|
||||
(= 13 (:key event)))))
|
||||
|
||||
(initialize-drawing [state point]
|
||||
(-> state
|
||||
(assoc-in [:workspace-drawing :object :segments] [point point])
|
||||
(assoc-in [:workspace-drawing :object :initialized?] true)))
|
||||
(defn init-path []
|
||||
(fn [state]
|
||||
(update-in state [:workspace-drawing :object]
|
||||
assoc :content []
|
||||
:initialized? true)))
|
||||
|
||||
(insert-point-segment [state point]
|
||||
(-> state
|
||||
(update-in [:workspace-drawing :object :segments] (fnil conj []) point)))
|
||||
(defn add-path-command [command]
|
||||
(fn [state]
|
||||
(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]))
|
||||
exists? (< -1 index segments)]
|
||||
(cond-> state
|
||||
exists? (assoc-in [:workspace-drawing :object :segments index] point))))
|
||||
|
||||
(finish-drawing-path [state]
|
||||
(defn finish-drawing-path []
|
||||
(fn [state]
|
||||
(update-in
|
||||
state [:workspace-drawing :object]
|
||||
(fn [shape] (-> shape
|
||||
(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/WatchEvent
|
||||
(watch [_ state stream]
|
||||
|
@ -107,7 +125,7 @@
|
|||
point)]
|
||||
#(update-point-segment % index point)))))
|
||||
(rx/of finish-drawing-path
|
||||
common/handle-finish-drawing))))))))
|
||||
common/handle-finish-drawing)))))))
|
||||
|
||||
(defn close-drawing-path []
|
||||
(ptk/reify ::close-drawing-path
|
||||
|
|
|
@ -26,6 +26,11 @@
|
|||
[v]
|
||||
(instance? MouseEvent v))
|
||||
|
||||
(defn mouse-down?
|
||||
[v]
|
||||
(and (mouse-event? v)
|
||||
(= :down (:type v))))
|
||||
|
||||
(defn mouse-up?
|
||||
[v]
|
||||
(and (mouse-event? v)
|
||||
|
|
Loading…
Add table
Reference in a new issue