From 8d82c59453b88cf1b518bca072296e02c90e2f17 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sat, 6 Feb 2016 18:02:46 +0200 Subject: [PATCH] Remove old matrix/svg transformation code. --- src/uxbox/shapes.cljs | 27 +++++++++++---- src/uxbox/ui/shapes.cljs | 57 +++++++++--------------------- src/uxbox/util/matrix.cljs | 38 -------------------- src/uxbox/util/svg.cljs | 71 -------------------------------------- 4 files changed, 38 insertions(+), 155 deletions(-) delete mode 100644 src/uxbox/util/matrix.cljs delete mode 100644 src/uxbox/util/svg.cljs diff --git a/src/uxbox/shapes.cljs b/src/uxbox/shapes.cljs index 5764a0fa1..e3757c331 100644 --- a/src/uxbox/shapes.cljs +++ b/src/uxbox/shapes.cljs @@ -296,12 +296,27 @@ scale-y (/ height orig-height) center-x (- width (/ width 2)) center-y (- height (/ height 2))] - (as-> (gmt/matrix) $ - (gmt/translate $ x1 y1) - (gmt/translate $ center-x center-y) - (gmt/rotate $ rotation) - (gmt/translate $ (- center-x) (- center-y)) - (gmt/scale $ scale-x scale-y)))) + (-> (gmt/matrix) + (gmt/translate x1 y1) + (gmt/translate center-x center-y) + (gmt/rotate rotation) + (gmt/translate (- center-x) (- center-y)) + (gmt/scale scale-x scale-y)))) + +(declare outer-rect) + +(defmethod -transformation :builtin/group + [{:keys [dx dy rotation items] :or {rotation 0} :as shape}] + (let [shapes-by-id (get @st/state :shapes-by-id) + shapes (map #(get shapes-by-id %) items) + {:keys [x y width height]} (outer-rect shapes) + center-x (+ x (/ width 2)) + center-y (+ y (/ height 2))] + (-> (gmt/matrix) + (gmt/translate (or dx 0) (or dy 0)) + (gmt/translate center-x center-y) + (gmt/rotate rotation) + (gmt/translate (- center-x) (- center-y))))) (defmethod -transformation :default [shape _] diff --git a/src/uxbox/ui/shapes.cljs b/src/uxbox/ui/shapes.cljs index 7f9a78b73..d89ae187c 100644 --- a/src/uxbox/ui/shapes.cljs +++ b/src/uxbox/ui/shapes.cljs @@ -6,8 +6,6 @@ [uxbox.state :as st] [uxbox.shapes :as sh] [uxbox.ui.icons :as i] - [uxbox.util.svg :as svg] - [uxbox.util.matrix :as mtx] [uxbox.util.math :as mth] [uxbox.util.data :refer (remove-nil-vals)])) @@ -82,50 +80,29 @@ (defmethod sh/-render :builtin/rect [{:keys [id x1 y1 x2 y2] :as shape}] (let [key (str id) - props (select-keys shape [:x :y :width :height]) + props {:x x1 :y y1 :id key :key key} + size (sh/-size shape) attrs (-> (extract-style-attrs shape) - (merge {:id key :key key}) - (merge props))] + (merge props size))] (html [:rect attrs]))) -;; FIXME: the impl should be more clear. - (defmethod sh/-render :builtin/group [{:keys [items id dx dy rotation] :as shape} factory] - (letfn [(rotation-matrix [] - (let [shapes-by-id (get @st/state :shapes-by-id) - shapes (map #(get shapes-by-id %) items) - {:keys [x y width height]} (sh/outer-rect shapes) - center-x (+ x (/ width 2)) - center-y (+ y (/ height 2))] - (mtx/multiply (svg/translate-matrix center-x center-y) - (svg/rotation-matrix rotation) - (svg/translate-matrix (- center-x) - (- center-y))))) - (translate-matrix [] - (svg/translate-matrix (or dx 0) (or dy 0))) - - (transform [] - (let [result (mtx/multiply (rotation-matrix) - (translate-matrix)) - result (flatten @result)] - (->> (map #(nth result %) [0 3 1 4 2 5]) - (str/join ",") - (str/format "matrix(%s)"))))] - (let [key (str "group-" id) - tfm (transform) - attrs (merge {:id key :key key :transform tfm} - (make-debug-attrs shape)) - shapes-by-id (get @st/state :shapes-by-id)] - (html - [:g attrs - (for [item (->> items - (map #(get shapes-by-id %)) - (remove :hidden) - (reverse))] - (-> (factory item) - (rum/with-key (str (:id item)))))])))) + (let [key (str "group-" id) + rfm (sh/-transformation shape) + attrs (merge {:id key :key key :transform (str rfm)} + (extract-style-attrs shape) + (make-debug-attrs shape)) + shapes-by-id (get @st/state :shapes-by-id)] + (html + [:g attrs + (for [item (->> items + (map #(get shapes-by-id %)) + (remove :hidden) + (reverse))] + (-> (factory item) + (rum/with-key (str (:id item)))))]))) (defmethod sh/-render-svg :builtin/icon [{:keys [data id view-box] :as shape}] diff --git a/src/uxbox/util/matrix.cljs b/src/uxbox/util/matrix.cljs deleted file mode 100644 index b9c4157ef..000000000 --- a/src/uxbox/util/matrix.cljs +++ /dev/null @@ -1,38 +0,0 @@ -(ns uxbox.util.matrix - "A lightweight abstraction over Matrix library - of the Google Closure Library." - (:import goog.math.Matrix - goog.math.Size)) - -(extend-type Matrix - cljs.core/ICounted - (-count [v] - (let [^Size size (.getSize v)] - (* (.-width size) - (.-height size)))) - - cljs.core/IDeref - (-deref [v] - (js->clj (.toArray v))) - - IPrintWithWriter - (-pr-writer [v writer _] - (->> (str "#goog.math.Matrix " (js->clj (.toArray v))) - (cljs.core/-write writer)))) - -(defn matrix - "Create a matrix instance from coll. - The size is determined by the number - of elements of the collection." - [coll] - {:pre [(coll? coll) - (coll? (first coll))]} - (Matrix. (clj->js coll))) - -(defn multiply - ([n] n) - ([n m] - (.multiply n m)) - ([n m & more] - (reduce multiply (.multiply n m) more))) - diff --git a/src/uxbox/util/svg.cljs b/src/uxbox/util/svg.cljs deleted file mode 100644 index e7ddd251a..000000000 --- a/src/uxbox/util/svg.cljs +++ /dev/null @@ -1,71 +0,0 @@ -(ns uxbox.util.svg - "A svg related utils." - (:require [cuerdas.core :as str] - [uxbox.util.matrix :as mtx] - [uxbox.util.math :as mth] - [uxbox.util.data :refer (without-keys)])) - -(defn translate-matrix - ([x] - (translate-matrix x x)) - ([x y] - (mtx/matrix [[1 0 x] - [0 1 y] - [0 0 1]]))) - -(defn scale-matrix - ([w] - (scale-matrix w w)) - ([w h] - (mtx/matrix [[w 0 0] - [0 h 0] - [0 0 1]]))) - -(defn rotation-matrix - [^number degrees] - (let [v1 (mth/cos (mth/radians degrees)) - v2 (mth/sin (mth/radians degrees)) - v3 (mth/neg v2)] - (mtx/matrix [[v1 v3 0] - [v2 v1 0] - [0 0 1]]))) - -(defn apply-translate - [{:keys [x y]}] - (translate-matrix x y)) - -(defn apply-scale - [{:keys [width height view-box]}] - (if (and width height) - (let [orig-width (nth view-box 2) - orig-height (nth view-box 3) - scale-x (/ width orig-width) - scale-y (/ height orig-height)] - (scale-matrix scale-x scale-y)) - (scale-matrix 1 1))) - -(defn apply-rotation - [{:keys [rotation x y width height view-box] :or {rotation 0}}] - (let [width (nth view-box 2) - height (nth view-box 3) - x (- width (/ width 2)) - y (- height (/ height 2))] - (mtx/multiply (translate-matrix x y) - (rotation-matrix rotation) - (translate-matrix (- x) - (- y))))) - -(def interpret-attrs (juxt apply-translate - apply-scale - apply-rotation)) - -(defn calculate-transform - [shape] - (let [result (->> (interpret-attrs shape) - (apply mtx/multiply) - (deref) - (take 6) - (flatten))] - (->> (map #(nth result %) [0 3 1 4 2 5]) - (str/join ",") - (str/format "matrix(%s)"))))