Move frames with guides move the guides

This commit is contained in:
alonso.torres 2022-01-24 11:46:04 +01:00
parent 3f89baa1fe
commit 62f7323acf
5 changed files with 122 additions and 105 deletions

View file

@ -6,7 +6,10 @@
(ns app.main.data.workspace.guides (ns app.main.data.workspace.guides
(:require (:require
[app.common.geom.point :as gpt]
[app.common.geom.shapes :as gsh]
[app.common.spec :as us] [app.common.spec :as us]
[app.common.types.page-options :as tpo]
[app.main.data.workspace.changes :as dwc] [app.main.data.workspace.changes :as dwc]
[app.main.data.workspace.state-helpers :as wsh] [app.main.data.workspace.state-helpers :as wsh]
[beicon.core :as rx] [beicon.core :as rx]
@ -20,7 +23,7 @@
(merge guide)))) (merge guide))))
(defn update-guides [guide] (defn update-guides [guide]
;; TODO CHECK SPEC (us/verify ::tpo/guide guide)
(ptk/reify ::update-guides (ptk/reify ::update-guides
ptk/WatchEvent ptk/WatchEvent
(watch [it state _] (watch [it state _]
@ -44,7 +47,7 @@
:origin it})))))) :origin it}))))))
(defn remove-guide [guide] (defn remove-guide [guide]
;; TODO CHECK SPEC (us/verify ::tpo/guide guide)
(ptk/reify ::remove-guide (ptk/reify ::remove-guide
ptk/WatchEvent ptk/WatchEvent
(watch [it state _] (watch [it state _]
@ -75,26 +78,24 @@
(watch [_ state _] (watch [_ state _]
(let [objects (wsh/lookup-page-objects state) (let [objects (wsh/lookup-page-objects state)
frame-ids (->> ids (filter #(= :frame (get-in objects [% :type]))) (into #{})) frame-ids? (->> ids (filter #(= :frame (get-in objects [% :type]))) (into #{}))
object-modifiers (get state :workspace-modifiers) object-modifiers (get state :workspace-modifiers)
moved-guide?
(fn [guide]
(let [frame-id (:frame-id guide)]
(and (contains? frame-ids frame-id)
(some? (get-in object-modifiers [frame-id :modifiers :displacement])))))
build-move-event build-move-event
(fn [guide] (fn [guide]
(let [disp (get-in object-modifiers [(:frame-id guide) :modifiers :displacement]) (let [frame (get objects (:frame-id guide))
guide (if (= :x (:axis guide)) frame' (-> (merge frame (get object-modifiers (:frame-id guide)))
(update guide :position + (:e disp)) (gsh/transform-shape))
(update guide :position + (:f disp)))]
moved (gpt/to-vec (gpt/point (:x frame) (:y frame))
(gpt/point (:x frame') (:y frame')))
guide (update guide :position + (get moved (:axis guide)))]
(update-guides guide)))] (update-guides guide)))]
(->> (wsh/lookup-page-options state) (->> (wsh/lookup-page-options state)
:guides :guides
(vals) (vals)
(filter moved-guide?) (filter (comp frame-ids? :frame-id))
(map build-move-event) (map build-move-event)
(rx/from)))))) (rx/from))))))

View file

@ -171,6 +171,7 @@
ignore-tree (get-ignore-tree object-modifiers objects ids)] ignore-tree (get-ignore-tree object-modifiers objects ids)]
(rx/of (dwu/start-undo-transaction) (rx/of (dwu/start-undo-transaction)
(dwg/move-frame-guides ids-with-children)
(dch/update-shapes (dch/update-shapes
ids-with-children ids-with-children
(fn [shape] (fn [shape]
@ -189,7 +190,6 @@
:rotation :rotation
:flip-x :flip-x
:flip-y]}) :flip-y]})
(dwg/move-frame-guides ids-with-children)
(clear-local-transform) (clear-local-transform)
(dwu/commit-undo-transaction)))))) (dwu/commit-undo-transaction))))))

View file

@ -158,7 +158,7 @@
(mf/defc export-guides (mf/defc export-guides
[{:keys [guides]}] [{:keys [guides]}]
[:> "penpot:guides" #js {} [:> "penpot:guides" #js {}
(for [{:keys [id position frame-id axis]} (vals guides)] (for [{:keys [position frame-id axis]} (vals guides)]
[:> "penpot:guide" #js {:position position [:> "penpot:guide" #js {:position position
:frame-id frame-id :frame-id frame-id
:axis (d/name axis)}])]) :axis (d/name axis)}])])

View file

@ -370,5 +370,6 @@
{:zoom zoom {:zoom zoom
:vbox vbox :vbox vbox
:hover-frame frame-parent :hover-frame frame-parent
:modifiers modifiers
:disabled-guides? disabled-guides?}]])]]])) :disabled-guides? disabled-guides?}]])]]]))

View file

@ -7,6 +7,8 @@
(ns app.main.ui.workspace.viewport.guides (ns app.main.ui.workspace.viewport.guides
(:require (:require
[app.common.colors :as colors] [app.common.colors :as colors]
[app.common.geom.point :as gpt]
[app.common.geom.shapes :as gsh]
[app.common.math :as mth] [app.common.math :as mth]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
[app.main.data.workspace :as dw] [app.main.data.workspace :as dw]
@ -137,11 +139,7 @@
{:x (+ (:x vbox) rules-pos) {:x (+ (:x vbox) rules-pos)
:y (- pos (/ guide-active-area 2)) :y (- pos (/ guide-active-area 2))
:width (:width vbox) :width (:width vbox)
:height guide-active-area} :height guide-active-area})))
)))
(defn guide-line-axis (defn guide-line-axis
([pos vbox axis] ([pos vbox axis]
@ -232,9 +230,19 @@
:width (:width vbox) :width (:width vbox)
:height (/ 24 zoom)})) :height (/ 24 zoom)}))
(defn is-guide-inside-frame?
[guide frame]
(if (= :x (:axis guide))
(and (>= (:position guide) (:x frame) )
(<= (:position guide) (+ (:x frame) (:width frame)) ))
(and (>= (:position guide) (:y frame) )
(<= (:position guide) (+ (:y frame) (:height frame)) ))))
(mf/defc guide (mf/defc guide
{::mf/wrap [mf/memo]} {::mf/wrap [mf/memo]}
[{:keys [guide hover? on-guide-change get-hover-frame vbox zoom hover-frame disabled-guides?]}] [{:keys [guide hover? on-guide-change get-hover-frame vbox zoom hover-frame disabled-guides? frame-modifier]}]
(let [axis (:axis guide) (let [axis (:axis guide)
@ -254,11 +262,17 @@
state state
frame]} (use-guide handle-change-position get-hover-frame zoom guide) frame]} (use-guide handle-change-position get-hover-frame zoom guide)
frame (or frame hover-frame) base-frame (or frame hover-frame)
pos (or (:new-position @state) (:position guide)) frame (gsh/transform-shape (merge base-frame frame-modifier))
move-vec (gpt/to-vec (gpt/point (:x base-frame) (:y base-frame))
(gpt/point (:x frame) (:y frame)))
pos (+ (or (:new-position @state) (:position guide)) (get move-vec axis))
guide-width (/ guide-width zoom) guide-width (/ guide-width zoom)
guide-pill-corner-radius (/ guide-pill-corner-radius zoom)] guide-pill-corner-radius (/ guide-pill-corner-radius zoom)]
(when (or (nil? frame) (is-guide-inside-frame? (assoc guide :position pos) frame))
[:g.guide-area {:data-guide-frame-id (when (some? frame) (str (:id frame)))} [:g.guide-area {:data-guide-frame-id (when (some? frame) (str (:id frame)))}
(when-not disabled-guides? (when-not disabled-guides?
(let [{:keys [x y width height]} (guide-area-axis pos vbox zoom frame axis)] (let [{:keys [x y width height]} (guide-area-axis pos vbox zoom frame axis)]
@ -343,7 +357,7 @@
:style {:font-size (/ 13 zoom) :style {:font-size (/ 13 zoom)
:font-family "sourcesanspro" :font-family "sourcesanspro"
:fill colors/black}} :fill colors/black}}
(str (mth/round pos))]]))])) (str (mth/round pos))]]))])))
(mf/defc new-guide-area (mf/defc new-guide-area
[{:keys [vbox zoom axis get-hover-frame disabled-guides?]}] [{:keys [vbox zoom axis get-hover-frame disabled-guides?]}]
@ -395,7 +409,7 @@
(mf/defc viewport-guides (mf/defc viewport-guides
{::mf/wrap [mf/memo]} {::mf/wrap [mf/memo]}
[{:keys [zoom vbox hover-frame disabled-guides?]}] [{:keys [zoom vbox hover-frame disabled-guides? modifiers]}]
(let [page (mf/deref refs/workspace-page) (let [page (mf/deref refs/workspace-page)
@ -445,6 +459,7 @@
:guide current :guide current
:vbox vbox :vbox vbox
:zoom zoom :zoom zoom
:frame-modifier (get modifiers (:frame-id current))
:get-hover-frame get-hover-frame :get-hover-frame get-hover-frame
:on-guide-change on-guide-change :on-guide-change on-guide-change
:disabled-guides? disabled-guides?}])])) :disabled-guides? disabled-guides?}])]))