wip del gueno

This commit is contained in:
Andrés Moya
2022-11-24 16:22:31 +01:00
parent a4e36390e2
commit 8ccd9bedfa
2 changed files with 264 additions and 144 deletions

View File

@@ -15,6 +15,7 @@
[app.common.pages.helpers :as cph]
[app.common.spec :as us]
[app.common.types.modifiers :as ctm]
[app.common.types.component :as ctk]
[app.common.types.container :as ctn]
[app.common.types.shape :as cts]
[app.common.types.shape.layout :as ctl]
@@ -28,6 +29,106 @@
[cljs.spec.alpha :as s]
[potok.core :as ptk]))
;; -- copies --------------------------------------------------------
;; TBD...
(defn- get-copies
"If one or more of the shapes belongs to a component's main instance, find all copies of
the component in the same page.
Return a map {<main-root-id> [<main-root> [<copy-root> <copy-root>...]] ...}"
[shapes objects modif-tree]
(letfn [(get-copies-one [shape]
(let [root-shape (ctn/get-root-shape objects shape)]
(when (:main-instance? root-shape)
(let [children (->> root-shape
:shapes
(map #(get objects %))
(map #(gsh/transform-shape % (get-in modif-tree [(:id %) :modifiers]))))
root-shape (gsh/update-group-selrect root-shape children)]
[(:id root-shape) [root-shape (ctn/get-instances objects root-shape)]]))))]
(into {} (map get-copies-one shapes))))
(defn- reposition-shape
[shape origin-root dest-root]
(let [shape-pos (fn [shape]
(gpt/point (get-in shape [:selrect :x])
(get-in shape [:selrect :y])))
origin-root-pos (shape-pos origin-root)
dest-root-pos (shape-pos dest-root)
delta (gpt/subtract dest-root-pos origin-root-pos)]
(gsh/move shape delta)))
(defn- sync-shape
[main-shape copy-shape main-root copy-root]
(if (ctk/touched-group? copy-shape :geometry-group)
{}
(let [main-shape (reposition-shape main-shape main-root copy-root)
translation (gpt/subtract (gsh/orig-pos main-shape)
(gsh/orig-pos copy-shape))
center (gsh/orig-pos copy-shape)
mult-w (/ (gsh/width main-shape) (gsh/width copy-shape))
mult-h (/ (gsh/height main-shape) (gsh/height copy-shape))
resize (gpt/point mult-w mult-h)]
(-> (ctm/empty)
(ctm/move translation)
(ctm/resize resize center)))))
(defn- process-text-modifiers
"For texts we only use the displacement because resize
needs to recalculate the text layout"
[shape modif-tree]
modif-tree)
;; (cond-> modifiers
;; (= :text (:type shape))
;; (select-keys [:displacement :rotation])))
(defn- add-copies-modifiers
"Add modifiers to all necessary shapes inside the copies"
[copies objects modif-tree]
(letfn [(add-copy-modifiers-one [modif-tree copy-shape copy-root main-root main-shapes main-shapes-modif]
(let [main-shape-modif (d/seek #(ctk/is-main-of? % copy-shape) main-shapes-modif)
modifiers (sync-shape main-shape-modif copy-shape main-root copy-root)
;; %%(cond-> (sync-shape main-shape-modif copy-shape copy-root main-root)
;; %% (some? (:rotation (get-in modifiers [(:id main-shape-modif) :modifiers])))
;; %% (assoc :rotation (:rotation (get-in modifiers [(:id main-shape-modif) :modifiers])))
;; %% )
]
(if (seq modifiers)
(assoc-in modif-tree [(:id copy-shape) :modifiers] modifiers)
modif-tree)))
(add-copy-modifiers [modif-tree copy-root main-root main-shapes main-shapes-modif]
(let [copy-shapes (into [copy-root] (cph/get-children objects (:id copy-root)))]
(reduce #(add-copy-modifiers-one %1 %2 copy-root main-root main-shapes main-shapes-modif)
modif-tree
copy-shapes)))
(add-copies-modifiers-one [modif-tree [main-root copy-roots]]
(let [main-shapes (into [main-root] (cph/get-children objects (:id main-root)))
main-shapes-modif (map (fn [shape]
(let [; shape (cond-> shape
; (some? (:transform-inverse shape))
; (gsh/apply-transform (:transform-inverse shape)))
]
(->> (get-in modif-tree [(:id shape) :modifiers])
(process-text-modifiers shape)
(gsh/transform-shape shape))))
main-shapes)]
(reduce #(add-copy-modifiers %1 %2 main-root main-shapes main-shapes-modif)
modif-tree
copy-roots)))]
(reduce add-copies-modifiers-one
modif-tree
(vals copies))))
;; -- temporary modifiers -------------------------------------------
;; During an interactive transformation of shapes (e.g. when resizing or rotating
@@ -236,12 +337,25 @@
(wsh/lookup-page-objects state)
snap-pixel?
(and (not ignore-snap-pixel) (contains? (:workspace-layout state) :snap-pixel-grid))]
(and (not ignore-snap-pixel) (contains? (:workspace-layout state) :snap-pixel-grid))
(as-> objects $
(apply-text-modifiers $ (get state :workspace-text-modifier))
;;(apply-path-modifiers $ (get-in state [:workspace-local :edit-path]))
(gsh/set-objects-modifiers modif-tree $ ignore-constraints snap-pixel?)))))
modif-tree
(as-> objects $
(apply-text-modifiers $ (get state :workspace-text-modifier))
;;(apply-path-modifiers $ (get-in state [:workspace-local :edit-path]))
(gsh/set-objects-modifiers modif-tree $ ignore-constraints snap-pixel?))
shapes
(->> (keys modif-tree)
(map (d/getf objects)))
copies
(get-copies shapes objects modif-tree)
;; TODO: mark new modifiers to be ignored in apply-modifiers
modif-tree (add-copies-modifiers copies objects modif-tree)]
modif-tree)))
(defn set-modifiers
([modif-tree]

View File

@@ -38,110 +38,111 @@
[cljs.spec.alpha :as s]
[potok.core :as ptk]))
;; --- copies ---------------------------
(defn- get-copies
"If one or more of the shapes belongs to a component's main instance, find all copies of
the component in the same page.
Return a map {<main-root-id> [<main-root> [<copy-root> <copy-root>...]] ...}"
[shapes objects modifiers]
(letfn [(get-copies-one [shape]
(let [root-shape (ctn/get-root-shape objects shape)]
(when (:main-instance? root-shape)
(let [children (->> root-shape
:shapes
(map #(get objects %))
;; %% (map #(gsh/apply-modifiers % (get-in modifiers [(:id %) :modifiers])))
)
root-shape (gsh/update-group-selrect root-shape children)]
[(:id root-shape) [root-shape (ctn/get-instances objects root-shape)]]))))]
(into {} (map get-copies-one shapes))))
(defn- reposition-shape
[shape origin-root dest-root]
(let [shape-pos (fn [shape]
(gpt/point (get-in shape [:selrect :x])
(get-in shape [:selrect :y])))
origin-root-pos (shape-pos origin-root)
dest-root-pos (shape-pos dest-root)
delta (gpt/subtract dest-root-pos origin-root-pos)]
(gsh/move shape delta)))
(defn- sync-shape
[main-shape copy-shape copy-root main-root]
;; (js/console.log "+++")
;; (js/console.log "main-shape" (clj->js main-shape))
;; (js/console.log "copy-shape" (clj->js copy-shape))
(if (ctk/touched-group? copy-shape :geometry-group)
{}
(let [main-shape (reposition-shape main-shape main-root copy-root)
translation (gpt/subtract (gsh/orig-pos main-shape)
(gsh/orig-pos copy-shape))
center (gsh/orig-pos copy-shape)
mult-w (/ (gsh/width main-shape) (gsh/width copy-shape))
mult-h (/ (gsh/height main-shape) (gsh/height copy-shape))
resize (gpt/point mult-w mult-h)]
(cond-> {}
(not (gpt/almost-zero? translation))
(assoc :displacement (gmt/translate-matrix translation))
(not (gpt/close? resize (gpt/point 1 1)))
(assoc :resize-vector resize
:resize-origin center)))))
(defn- process-text-modifiers
"For texts we only use the displacement because resize
needs to recalculate the text layout"
[shape modifiers]
modifiers)
;; (cond-> modifiers
;; (= :text (:type shape))
;; (select-keys [:displacement :rotation])))
(defn- add-copies-modifiers
"Add modifiers to all necessary shapes inside the copies"
[copies objects modifiers]
(letfn [(add-copy-modifiers-one [modifiers copy-shape copy-root main-root main-shapes main-shapes-modif]
(let [main-shape-modif (d/seek #(ctk/is-main-of? % copy-shape) main-shapes-modif)
modifier (cond-> (sync-shape main-shape-modif copy-shape copy-root main-root)
(some? (:rotation (get-in modifiers [(:id main-shape-modif) :modifiers])))
(assoc :rotation (:rotation (get-in modifiers [(:id main-shape-modif) :modifiers])))
)]
(if (seq modifier)
(assoc-in modifiers [(:id copy-shape) :modifiers] modifier)
modifiers)))
(add-copy-modifiers [modifiers copy-root main-root main-shapes main-shapes-modif]
(let [copy-shapes (into [copy-root] (cph/get-children objects (:id copy-root)))]
(reduce #(add-copy-modifiers-one %1 %2 copy-root main-root main-shapes main-shapes-modif)
modifiers
copy-shapes)))
(add-copies-modifiers-one [modifiers [main-root copy-roots]]
(let [main-shapes (into [main-root] (cph/get-children objects (:id main-root)))
main-shapes-modif (map (fn [shape]
(let [; shape (cond-> shape
; (some? (:transform-inverse shape))
; (gsh/apply-transform (:transform-inverse shape)))
]
(->> (get-in modifiers [(:id shape) :modifiers])
(process-text-modifiers shape)
;; %% (gsh/apply-modifiers shape)
)))
main-shapes)]
(reduce #(add-copy-modifiers %1 %2 main-root main-shapes main-shapes-modif)
modifiers
copy-roots)))]
(reduce add-copies-modifiers-one
modifiers
(vals copies))))
;; $$ ESTE ES EL BUENO que estoy moviendo a data/worskpace/modifiers
;; ;; --- copies ---------------------------
;;
;; (defn- get-copies
;; "If one or more of the shapes belongs to a component's main instance, find all copies of
;; the component in the same page.
;;
;; Return a map {<main-root-id> [<main-root> [<copy-root> <copy-root>...]] ...}"
;; [shapes objects modifiers]
;; (letfn [(get-copies-one [shape]
;; (let [root-shape (ctn/get-root-shape objects shape)]
;; (when (:main-instance? root-shape)
;; (let [children (->> root-shape
;; :shapes
;; (map #(get objects %))
;; ;; %% (map #(gsh/apply-modifiers % (get-in modifiers [(:id %) :modifiers])))
;; )
;; root-shape (gsh/update-group-selrect root-shape children)]
;; [(:id root-shape) [root-shape (ctn/get-instances objects root-shape)]]))))]
;;
;; (into {} (map get-copies-one shapes))))
;;
;; (defn- reposition-shape
;; [shape origin-root dest-root]
;; (let [shape-pos (fn [shape]
;; (gpt/point (get-in shape [:selrect :x])
;; (get-in shape [:selrect :y])))
;;
;; origin-root-pos (shape-pos origin-root)
;; dest-root-pos (shape-pos dest-root)
;; delta (gpt/subtract dest-root-pos origin-root-pos)]
;; (gsh/move shape delta)))
;;
;; (defn- sync-shape
;; [main-shape copy-shape copy-root main-root]
;; ;; (js/console.log "+++")
;; ;; (js/console.log "main-shape" (clj->js main-shape))
;; ;; (js/console.log "copy-shape" (clj->js copy-shape))
;; (if (ctk/touched-group? copy-shape :geometry-group)
;; {}
;; (let [main-shape (reposition-shape main-shape main-root copy-root)
;;
;; translation (gpt/subtract (gsh/orig-pos main-shape)
;; (gsh/orig-pos copy-shape))
;;
;; center (gsh/orig-pos copy-shape)
;; mult-w (/ (gsh/width main-shape) (gsh/width copy-shape))
;; mult-h (/ (gsh/height main-shape) (gsh/height copy-shape))
;; resize (gpt/point mult-w mult-h)]
;;
;; (cond-> {}
;; (not (gpt/almost-zero? translation))
;; (assoc :displacement (gmt/translate-matrix translation))
;;
;; (not (gpt/close? resize (gpt/point 1 1)))
;; (assoc :resize-vector resize
;; :resize-origin center)))))
;;
;; (defn- process-text-modifiers
;; "For texts we only use the displacement because resize
;; needs to recalculate the text layout"
;; [shape modifiers]
;; modifiers)
;; ;; (cond-> modifiers
;; ;; (= :text (:type shape))
;; ;; (select-keys [:displacement :rotation])))
;;
;; (defn- add-copies-modifiers
;; "Add modifiers to all necessary shapes inside the copies"
;; [copies objects modifiers]
;; (letfn [(add-copy-modifiers-one [modifiers copy-shape copy-root main-root main-shapes main-shapes-modif]
;; (let [main-shape-modif (d/seek #(ctk/is-main-of? % copy-shape) main-shapes-modif)
;; modifier (cond-> (sync-shape main-shape-modif copy-shape copy-root main-root)
;; (some? (:rotation (get-in modifiers [(:id main-shape-modif) :modifiers])))
;; (assoc :rotation (:rotation (get-in modifiers [(:id main-shape-modif) :modifiers])))
;; )]
;; (if (seq modifier)
;; (assoc-in modifiers [(:id copy-shape) :modifiers] modifier)
;; modifiers)))
;;
;; (add-copy-modifiers [modifiers copy-root main-root main-shapes main-shapes-modif]
;; (let [copy-shapes (into [copy-root] (cph/get-children objects (:id copy-root)))]
;; (reduce #(add-copy-modifiers-one %1 %2 copy-root main-root main-shapes main-shapes-modif)
;; modifiers
;; copy-shapes)))
;;
;; (add-copies-modifiers-one [modif-tree [main-root copy-roots]]
;; (let [main-shapes (into [main-root] (cph/get-children objects (:id main-root)))
;; main-shapes-modif (map (fn [shape]
;; (let [; shape (cond-> shape
;; ; (some? (:transform-inverse shape))
;; ; (gsh/apply-transform (:transform-inverse shape)))
;; ]
;; (->> (get-in modifiers [(:id shape) :modifiers])
;; (process-text-modifiers shape)
;; ;; %% (gsh/apply-modifiers shape)
;; )))
;; main-shapes)]
;; (reduce #(add-copy-modifiers %1 %2 main-root main-shapes main-shapes-modif)
;; modifiers
;; copy-roots)))]
;;
;; (reduce add-copies-modifiers-one
;; modifiers
;; (vals copies))))
;; -- Helpers --------------------------------------------------------
@@ -230,37 +231,39 @@
(declare get-ignore-tree)
(defn set-modifiers
([ids]
(set-modifiers ids nil false))
([ids modifiers]
(set-modifiers ids modifiers false))
([ids modifiers ignore-constraints]
(set-modifiers ids modifiers ignore-constraints false))
([ids modifiers ignore-constraints ignore-snap-pixel]
(us/verify (s/coll-of uuid?) ids)
(ptk/reify ::set-modifiers
ptk/UpdateEvent
(update [_ state]
(let [objects (wsh/lookup-page-objects state)
ids (into #{} (remove #(get-in objects [% :blocked] false)) ids)
snap-pixel? (and (not ignore-snap-pixel)
(contains? (:workspace-layout state) :snap-pixel-grid))
modif-tree
{}
;; %% (gsh/set-objects-modifiers ids objects (constantly modifiers) ignore-constraints snap-pixel?)
copies (get-copies (mapv (d/getf objects) ids) objects modif-tree)
;; TODO: mark new modifiers to be ignored in apply-modifiers
modif-tree (add-copies-modifiers copies objects modif-tree)]
(update state :workspace-modifiers merge modif-tree))))))
;; $$ ESTE ES EL BUENO que estoy moviendo a data/worskpace/modifiers
;; (defn set-modifiers
;; ([ids]
;; (set-modifiers ids nil false))
;;
;; ([ids modifiers]
;; (set-modifiers ids modifiers false))
;;
;; ([ids modifiers ignore-constraints]
;; (set-modifiers ids modifiers ignore-constraints false))
;;
;; ([ids modifiers ignore-constraints ignore-snap-pixel]
;; (us/verify (s/coll-of uuid?) ids)
;; (ptk/reify ::set-modifiers
;; ptk/UpdateEvent
;; (update [_ state]
;; (let [objects (wsh/lookup-page-objects state)
;; ids (into #{} (remove #(get-in objects [% :blocked] false)) ids)
;;
;; snap-pixel? (and (not ignore-snap-pixel)
;; (contains? (:workspace-layout state) :snap-pixel-grid))
;;
;; modif-tree
;; {}
;; ;; %% (gsh/set-objects-modifiers ids objects (constantly modifiers) ignore-constraints snap-pixel?)
;;
;; copies (get-copies (mapv (d/getf objects) ids) objects modif-tree)
;; _ (js/console.log "copies" (clj->js copies))
;;
;; ;; TODO: mark new modifiers to be ignored in apply-modifiers
;; modif-tree (add-copies-modifiers copies objects modif-tree)]
;;
;; (update state :workspace-modifiers merge modif-tree))))))
;; (defn set-modifiers-raw
;; [modifiers]
@@ -278,7 +281,8 @@
(ptk/reify ::set-rotation-modifiers
ptk/UpdateEvent
(update [_ state]
(let [objects (wsh/lookup-page-objects state)
(let [objects (wsh/lookup-page-objects state)
ids
(->> shapes
(remove #(get % :blocked false))
@@ -327,11 +331,13 @@
shapes (map (d/getf objects) ids)
ignore-tree (->> (map #(get-ignore-tree object-modifiers objects %) shapes)
(reduce merge {}))]
(reduce merge {}))
undo-id (uuid/next)]
(rx/concat
(if undo-transation?
(rx/of (dwu/start-undo-transaction))
(rx/of (dwu/start-undo-transaction undo-id))
(rx/empty))
(rx/of (ptk/event ::dwg/move-frame-guides ids-with-children)
(ptk/event ::dwcm/move-frame-comment-threads ids-with-children)
@@ -365,7 +371,7 @@
:grow-type]})
(clear-local-transform))
(if undo-transation?
(rx/of (dwu/commit-undo-transaction))
(rx/of (dwu/commit-undo-transaction undo-id))
(rx/empty))))))))
(defn- check-delta