diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index ac98f0e52f..b13132985e 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -1670,15 +1670,21 @@ {} placement-attrs) + orient (when (untouched? :rotation) + (orient-like shape-inst placed (gsh/shape->center shape-inst))) + oriented (cond-> shape-inst (some? orient) (orient)) + ;; turned alike, both take their place at their first corner + turned? (and (some? (:transform placed)) + (not (gmt/unit? (:transform placed))) + (gmt/close? (:transform placed) (or (:transform oriented) (gmt/matrix)))) position (fn [shape] - (gpt/point (dm/get-in shape [:selrect :x]) - (dm/get-in shape [:selrect :y]))) - delta (gpt/subtract (position placed) (position shape-inst)) + (or (when turned? (first (:points shape))) + (gpt/point (dm/get-in shape [:selrect :x]) + (dm/get-in shape [:selrect :y])))) + delta (gpt/subtract (position placed) (position oriented)) move? (and (untouched? :x) (not layout-placed?) - (not (gpt/almost-zero? delta))) - orient (when (untouched? :rotation) - (orient-like shape-inst placed (gsh/shape->center shape-inst)))] + (not (gpt/almost-zero? delta)))] (cond-> changes (seq placement) (pcb/update-shapes [(:id shape-inst)] @@ -1878,22 +1884,32 @@ translation by the roots' (untransformed) position delta suffices — position is free per-instance placement. + A turned or flipped instance lines up with the origin at their first + corner, the one a resize keeps, so it follows a resize in its own frame. + If the dest root's geometry IS overridden (e.g. the user rotated the copy as a whole), the instance keeps its own placement transform, so the origin shape is additionally transformed by the roots' relative transformation (rotation / flips) around the dest root center — geometric changes then land expressed in the dest instance's own frame instead of wiping its placement." [shape origin-root dest-root] - (let [shape-pos (fn [shape] - (gpt/point (dm/get-in shape [:selrect :x]) - (dm/get-in shape [:selrect :y]))) + (let [touched? (ctk/touched-group? dest-root :geometry-group) + oriented? (and (not touched?) + (some? (:transform dest-root)) + (not (gmt/unit? (:transform dest-root))) + (gmt/close? (or (:transform origin-root) (gmt/matrix)) + (:transform dest-root))) + shape-pos (fn [shape] + (or (when oriented? (first (:points shape))) + (gpt/point (dm/get-in shape [:selrect :x]) + (dm/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) shape (gsh/move shape delta)] - (if-not (ctk/touched-group? dest-root :geometry-group) + (if-not touched? shape (if-let [turn (orient-like origin-root dest-root (grc/rect->center (:selrect dest-root)))] diff --git a/common/test/common_tests/logic/comp_expected_geometry_test.cljc b/common/test/common_tests/logic/comp_expected_geometry_test.cljc index 0883cb998b..d183d56021 100644 --- a/common/test/common_tests/logic/comp_expected_geometry_test.cljc +++ b/common/test/common_tests/logic/comp_expected_geometry_test.cljc @@ -459,3 +459,28 @@ (t/is (mth/close? 80 (:height after))) (t/is (mth/close? 100 (:width after))) (t/is (not (contains? (:touched after) :geometry-group))))) + +(t/deftest a-resized-turned-main-keeps-its-copies-at-their-first-corner + ;; a width change keeps a shape's first corner; a copy oriented like its + ;; main keeps its own first corner, whether the turn or flip is the main's + ;; or a parent's + (doseq [case [:turned :flipped :parent-turned]] + (let [file (-> (thf/sample-file :file) + (tho/add-simple-component :a :main-a :rect-a :root-params {:width 100 :height 100}) + (cond-> (= case :parent-turned) + (-> (tho/add-frame :main-b {:width 200 :height 200}) + (thc/instantiate-component :a :b-a :parent-label :main-b) + (thc/make-component :b :main-b)))) + before (-> (tm/make-situation file) + (r/step (n/rotate (if (= case :parent-turned) :main-b :main-a) 45)) r/settle + (cond-> (= case :flipped) (-> (r/step (n/transform-shape :main-a :flip-x nil)) r/settle))) + before (r/settle (tm/with-file before + (thc/instantiate-component (tm/file before) (if (= case :parent-turned) :b :a) + :copy :children-labels [:copy-a]))) + label (if (= case :parent-turned) :copy-a :copy) + after (-> before (r/step (n/transform-shape :main-a :width 80)) r/settle) + copy (ths/get-shape (tm/file after) label)] + (t/is (mth/close? 80 (:width copy) 0.001) (str case)) + (t/is (gpt/close? (first (:points (ths/get-shape (tm/file before) label))) + (first (:points copy))) + (str case))))) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 21e7dfd043..5c46250667 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -925,3 +925,20 @@ (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout :grid :rich-variants true :layout-children true :grid-empty true :layout-copies true :text true}] :ops [[:style 4 29 1] [:detach 0 0 0 0] [:rotate 5 0 0 0]]})))) + +(t/deftest generated-resized-main-keeps-a-turned-copy-at-its-first-corner + ;; a main is narrowed while a copy of it is turned by a detached parent; + ;; the copy keeps its first corner, and a child it overrides keeps its + ;; place against it + (t/is (map? (cg/run {:world [2 2 {:text true :text-styles true}] + :ops [[:detach 6 0 0 0] [:delete 0] [:restore 0] [:duplicate 2 0 0 0] [:edit 0 0 0 0] + [:transform 8 0 0 2] [:delete 0 0 0 0] [:rotate 8 0 0 0] [:delete 0] [:restore 0] + [:edit 7 0 1 1] [:update-main 7] [:transform 7 7 0 0]]})))) + +(t/deftest generated-turned-swap-of-another-size-keeps-the-corner-of-its-position + ;; a nested head switched to a wider variant is turned 45 degrees with the + ;; chain of copies it is in; it keeps the corner of its narrower position, + ;; and a reset lands on it + (t/is (map? (cg/run {:world [2 0 {:variants true :text-styles true :library true :rich-variants true + :layout-copies true :text true}] + :ops [[:switch 6 0 0 0] [:rotate 0 0 0 0] [:reset 5 0 0 0]]}))))