From 59af7071439c1ad0d561dd39936a8de249f47662 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sat, 23 Jan 2016 22:10:23 +0200 Subject: [PATCH] Add polymorphic impl for -outer-rect calculation. --- src/uxbox/shapes.cljs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/uxbox/shapes.cljs b/src/uxbox/shapes.cljs index f7d5fa6c02..1c01ba9d11 100644 --- a/src/uxbox/shapes.cljs +++ b/src/uxbox/shapes.cljs @@ -46,6 +46,10 @@ dispatch-by-type :hierarchy #'+hierarchy+) +(defmulti -outer-rect + dispatch-by-type + :hierarchy #'+hierarchy+) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Implementation ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -66,6 +70,37 @@ [shape rotation] (assoc shape :rotation rotation)) +(declare container-rect) +(declare resolve-position) + +(defmethod -outer-rect ::shape + [{:keys [group] :as shape}] + (as-> shape $ + (resolve-position $) + (container-rect $))) + +(defmethod -outer-rect :builtin/group + [{:keys [id group rotation view-box] :as shape}] + (let [shapes (->> (:items shape) + (map #(get-in @st/state [:shapes-by-id %])) + (map -outer-rect)) + + crect (-> shape + (resolve-position) + (container-rect)) + + shapes (into [crect] shapes) + + x (apply min (map :x shapes)) + y (apply min (map :y shapes)) + x' (apply max (map (fn [{:keys [x width]}] (+ x width)) shapes)) + y' (apply max (map (fn [{:keys [y height]}] (+ y height)) shapes)) + width (- x' x) + height (- y' y)] + (as-> shape $ + (merge $ {:width width :height height :x x :y y}) + (container-rect $)))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Helpers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;