diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index e4fe694603..ce976a06e8 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -1966,6 +1966,28 @@ (turn shape) shape)))) +(defn- stored-turn + "The rotation that `shape`, just turned by `angle` with its subtree, stores: + `rotation` plus `angle`, or minus it when only that one decomposes its + transform with its flips. A mirror that flips after its turn turns the + other way." + [shape rotation angle] + (let [{:keys [a b c d]} (or (:transform shape) (gmt/matrix)) + mirrored (not= (boolean (:flip-x shape)) (boolean (:flip-y shape))) + sign (if (:flip-x shape) -1 1) + turn (mth/degrees (mth/atan2 (* sign b) (* sign a))) + same? #(mth/close? (mod (- %1 %2 -180) 360) 180 0.01) + stores? (fn [value] + (or (same? value turn) + (and mirrored (same? value (- turn))) + (and (:flip-x shape) (:flip-y shape) (same? value (+ turn 180))))) + plus (mod (+ rotation angle) 360) + minus (mod (- rotation angle) 360)] + (if (and (= mirrored (neg? (- (* a d) (* b c)))) + (not (stores? plus)) (stores? minus)) + minus + plus))) + (defn orient-like "Turn the subtree of `head` around `center` to match `target`'s matrix. Compose flip flags with the roots, independent of rotation step size." @@ -1984,12 +2006,13 @@ flip-x? flip-y?) (fn [shape] (let [flip-x (cond-> (:flip-x shape) flip-x? not) - flip-y (cond-> (:flip-y shape) flip-y? not)] - (-> shape - (assoc-in [:modifiers :rotation] angle) - (gsh/apply-transform transform) - (dissoc :modifiers) - (assoc :flip-x flip-x :flip-y flip-y))))))))) + flip-y (cond-> (:flip-y shape) flip-y? not) + turned (-> shape + (assoc-in [:modifiers :rotation] angle) + (gsh/apply-transform transform) + (dissoc :modifiers) + (assoc :flip-x flip-x :flip-y flip-y))] + (assoc turned :rotation (stored-turn turned (or (:rotation shape) 0) angle))))))))) (defn- maybe-sync [c enabled? done? f] 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 dbfbbce842..43c59c9ec3 100644 --- a/common/test/common_tests/logic/comp_expected_geometry_test.cljc +++ b/common/test/common_tests/logic/comp_expected_geometry_test.cljc @@ -509,3 +509,22 @@ (t/is (gpt/close? (first (:points (ths/get-shape (tm/file before) label))) (first (:points copy))) (str case))))) + +(t/deftest a-turn-stores-a-mirrored-childs-rotation-as-its-transform-decomposes + ;; a child mirrored across its diagonal, stored as a vertical flip after a + ;; 270 degree turn, turned 45 degrees with its unflipped parent: only 225 + ;; decomposes its new transform with the flip + (let [head (cts/setup-shape {:type :frame :x 0 :y 0 :width 100 :height 100}) + target (gsh/transform-shape head (ctm/rotation-modifiers head (gsh/shape->center head) 45)) + square (cts/setup-shape {:type :rect :x 25 :y 25 :width 50 :height 50}) + mirror (gmt/matrix 0 1 1 0 0 0) + child (assoc square + :transform mirror :transform-inverse (gmt/inverse mirror) + :rotation 270 :flip-y true + :points [(gpt/point 25 25) (gpt/point 25 75) (gpt/point 75 75) (gpt/point 75 25)]) + turned ((cll/orient-like head target (gsh/shape->center head)) child) + {:keys [a b c d]} (:transform turned)] + (t/is (mth/close? 225 (:rotation turned) 0.01)) + (t/is (every? true? (map #(mth/close? %1 %2 1e-6) + [a b c d] + [(- (/ (mth/sqrt 2) 2)) (/ (mth/sqrt 2) 2) (/ (mth/sqrt 2) 2) (/ (mth/sqrt 2) 2)]))))) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 30bfe76b6a..bf213b05e7 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1033,3 +1033,11 @@ ;; later sync leaves it there (t/is (map? (cg/run {:world [2 2 {:text true :text-styles true}] :ops [[:style 8 9 3 1] [:resize 2 0 0 0] [:detach 3 0 0 0]]})))) + +(t/deftest generated-turned-main-keeps-a-flipped-copys-rotation-consistent + ;; a main turned 45 degrees turns the subtree of a flipped copy; a mirrored + ;; child stores the rotation its transform and flip decompose into + (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :variant-diff false + :rich-variants true :layout-copies true :text true}] + :ops [[:rotate 3 0 5 0] [:duplicate 0 0 0 0] [:duplicate 0 0 0 0] + [:transform 5 4 0 4] [:rotate 0 0 0 0]]}))))