From 43550af17575db6313391cf8d8bd2ecce1b72c7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Fri, 10 Apr 2020 12:43:58 +0200 Subject: [PATCH] :bug: Fix align and distribute grouped objects --- frontend/src/uxbox/main/data/workspace.cljs | 8 +++---- frontend/src/uxbox/main/geom.cljs | 23 +++++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/frontend/src/uxbox/main/data/workspace.cljs b/frontend/src/uxbox/main/data/workspace.cljs index 7deb6acd9..6cc58d89a 100644 --- a/frontend/src/uxbox/main/data/workspace.cljs +++ b/frontend/src/uxbox/main/data/workspace.cljs @@ -1461,7 +1461,7 @@ objects (get-in state [:workspace-data page-id :objects]) selected (get-in state [:workspace-local :selected]) moved-objs (if (= 1 (count selected)) - [(align-object-to-frame objects (first selected) axis)] + (align-object-to-frame objects (first selected) axis) (align-objects-list objects selected axis)) updated-objs (merge objects (d/index-by :id moved-objs))] (assoc-in state [:workspace-data page-id :objects] updated-objs))))) @@ -1470,13 +1470,13 @@ [objects object-id axis] (let [object (get objects object-id) frame (get objects (:frame-id object))] - (geom/align-to-rect object frame axis))) + (geom/align-to-rect object frame axis objects))) (defn align-objects-list [objects selected axis] (let [selected-objs (map #(get objects %) selected) rect (geom/selection-rect selected-objs)] - (map #(geom/align-to-rect % rect axis) selected-objs))) + (mapcat #(geom/align-to-rect % rect axis objects) selected-objs))) (defn distribute-objects [axis] @@ -1489,7 +1489,7 @@ objects (get-in state [:workspace-data page-id :objects]) selected (get-in state [:workspace-local :selected]) selected-objs (map #(get objects %) selected) - moved-objs (geom/distribute-space selected-objs axis) + moved-objs (geom/distribute-space selected-objs axis objects) updated-objs (merge objects (d/index-by :id moved-objs))] (assoc-in state [:workspace-data page-id :objects] updated-objs))))) diff --git a/frontend/src/uxbox/main/geom.cljs b/frontend/src/uxbox/main/geom.cljs index 6a6ca56ad..6cfd0a121 100644 --- a/frontend/src/uxbox/main/geom.cljs +++ b/frontend/src/uxbox/main/geom.cljs @@ -11,6 +11,7 @@ [uxbox.util.geom.matrix :as gmt] [uxbox.util.geom.point :as gpt] [uxbox.util.math :as mth] + [uxbox.main.data.helpers :as helpers] [uxbox.main.store :as st])) ;; --- Relative Movement @@ -61,6 +62,13 @@ (map #(update % :y + dy)))] (assoc shape :segments (into [] xf segments)))) +(defn recursive-move + "Move the shape and all its recursive children." + [shape dpoint objects] + (let [children-ids (helpers/get-children (:id shape) objects) + children (map #(get objects %) children-ids)] + (map #(move % dpoint) (cons shape children)))) + ;; --- Absolute Movement (declare absolute-move-rect) @@ -608,13 +616,14 @@ "Move the shape so that it is aligned with the given rectangle in the given axis. Take account the form of the shape and the possible rotation. What is aligned is the rectangle that wraps - the shape with the given rectangle." - [shape rect axis] + the shape with the given rectangle. If the shape is a group, + move also all of its recursive children." + [shape rect axis objects] (let [wrapper-rect (selection-rect [shape]) align-pos (calc-align-pos wrapper-rect rect axis) delta {:x (- (:x align-pos) (:x wrapper-rect)) :y (- (:y align-pos) (:y wrapper-rect))}] - (move shape delta))) + (recursive-move shape delta objects))) (defn calc-align-pos [wrapper-rect rect axis] @@ -651,8 +660,9 @@ "Distribute equally the space between shapes in the given axis. If there is no space enough, it does nothing. It takes into account the form of the shape and the rotation, what is distributed is - the wrapping recangles of the shapes." - [shapes axis] + the wrapping recangles of the shapes. If any shape is a group, + move also all of its recursive children." + [shapes axis objects] (let [coord (if (= axis :horizontal) :x :y) other-coord (if (= axis :horizontal) :y :x) size (if (= axis :horizontal) :width :height) @@ -685,7 +695,8 @@ new-pos (conj deltas delta)))))] - (map #(move %1 {coord %2 other-coord 0}) sorted-shapes deltas))))) + (mapcat #(recursive-move %1 {coord %2 other-coord 0} objects) + sorted-shapes deltas))))) ;; --- Helpers