Improved modifiers lens

This commit is contained in:
alonso.torres 2023-01-03 16:58:10 +01:00
parent d5ab0eea1a
commit 98698cf2db
5 changed files with 41 additions and 37 deletions

View file

@ -100,15 +100,13 @@
(and (= :frame (:type shape)) (= :flex (:layout shape))))) (and (= :frame (:type shape)) (= :flex (:layout shape)))))
(defn layout-child? [objects shape] (defn layout-child? [objects shape]
(let [parent-id (:parent-id shape) (let [frame-id (:frame-id shape)
parent (get objects parent-id)] frame (get objects frame-id)]
(layout? parent))) (layout? frame)))
(defn layout-child-id? [objects id] (defn layout-child-id? [objects id]
(let [shape (get objects id) (let [shape (get objects id)]
parent-id (:parent-id shape) (layout-child? objects shape)))
parent (get objects parent-id)]
(layout? parent)))
(defn inside-layout? (defn inside-layout?
"Check if the shape is inside a layout" "Check if the shape is inside a layout"

View file

@ -37,19 +37,21 @@
(assoc :x (- (:x point) (* sx (- dy dx))))))) (assoc :x (- (:x point) (* sx (- dy dx)))))))
(defn resize-shape [{:keys [x y width height] :as shape} initial point lock?] (defn resize-shape [{:keys [x y width height] :as shape} initial point lock?]
(let [draw-rect (gsh/make-rect initial (cond-> point lock? (adjust-ratio initial))) (if (and (some? x) (some? y) (some? width) (some? height))
shape-rect (gsh/make-rect x y width height) (let [draw-rect (gsh/make-rect initial (cond-> point lock? (adjust-ratio initial)))
shape-rect (gsh/make-rect x y width height)
scalev (gpt/point (/ (:width draw-rect) (:width shape-rect)) scalev (gpt/point (/ (:width draw-rect) (:width shape-rect))
(/ (:height draw-rect) (:height shape-rect))) (/ (:height draw-rect) (:height shape-rect)))
movev (gpt/to-vec (gpt/point shape-rect) (gpt/point draw-rect))] movev (gpt/to-vec (gpt/point shape-rect) (gpt/point draw-rect))]
(-> shape (-> shape
(assoc :click-draw? false) (assoc :click-draw? false)
(gsh/transform-shape (-> (ctm/empty) (gsh/transform-shape (-> (ctm/empty)
(ctm/resize scalev (gpt/point x y)) (ctm/resize scalev (gpt/point x y))
(ctm/move movev)))))) (ctm/move movev)))))
shape))
(defn update-drawing [state initial point lock?] (defn update-drawing [state initial point lock?]
(update-in state [:workspace-drawing :object] resize-shape initial point lock?)) (update-in state [:workspace-drawing :object] resize-shape initial point lock?))

View file

@ -219,6 +219,7 @@
(rx/concat (rx/concat
(->> ms/mouse-position (->> ms/mouse-position
(rx/filter some?)
(rx/with-latest-from ms/mouse-position-shift ms/mouse-position-alt) (rx/with-latest-from ms/mouse-position-shift ms/mouse-position-alt)
(rx/map normalize-proportion-lock) (rx/map normalize-proportion-lock)
(rx/switch-map (fn [[point _ _ :as current]] (rx/switch-map (fn [[point _ _ :as current]]

View file

@ -324,11 +324,7 @@
(l/derived :workspace-editor-state st/state)) (l/derived :workspace-editor-state st/state))
(def workspace-modifiers (def workspace-modifiers
(l/derived :workspace-modifiers st/state)) (l/derived :workspace-modifiers st/state =))
(defn workspace-modifiers-by-id
[ids]
(l/derived #(select-keys % ids) workspace-modifiers))
(def workspace-modifiers-with-objects (def workspace-modifiers-with-objects
(l/derived (l/derived
@ -340,20 +336,29 @@
(and (= (:modifiers a) (:modifiers b)) (and (= (:modifiers a) (:modifiers b))
(identical? (:objects a) (:objects b)))))) (identical? (:objects a) (:objects b))))))
(defn workspace-modifiers-by-frame-id (def workspace-frame-modifiers
[frame-id]
(l/derived (l/derived
(fn [{:keys [modifiers objects]}] (fn [{:keys [modifiers objects]}]
(let [keys (->> modifiers (->> modifiers
(keys) (reduce
(filter (fn [id] (fn [result [id modifiers]]
(let [shape (get objects id)] (let [shape (get objects id)
(or (= frame-id id) frame-id (:frame-id shape)]
(and (= frame-id (:frame-id shape)) (cond
(not (= :frame (:type shape)))))))))] (cph/frame-shape? shape)
(select-keys modifiers keys))) (assoc-in result [id id] modifiers)
workspace-modifiers-with-objects
=)) (some? frame-id)
(assoc-in result [frame-id id] modifiers)
:else
result)))
{})))
workspace-modifiers-with-objects))
(defn workspace-modifiers-by-frame-id
[frame-id]
(l/derived #(get % frame-id) workspace-frame-modifiers =))
(defn select-bool-children [id] (defn select-bool-children [id]
(l/derived (partial wsh/select-bool-children id) st/state =)) (l/derived (partial wsh/select-bool-children id) st/state =))

View file

@ -110,9 +110,7 @@
:circle [:> circle-wrapper opts] :circle [:> circle-wrapper opts]
:svg-raw [:> svg-raw-wrapper opts] :svg-raw [:> svg-raw-wrapper opts]
:bool [:> bool-wrapper opts] :bool [:> bool-wrapper opts]
:frame [:> nested-frame-wrapper opts]
;; Only used when drawing a new frame.
:frame [:> nested-frame-wrapper opts]
nil)]))) nil)])))