From cbffee14861d4acef9a4eed636114ba692aa6e66 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sat, 1 Feb 2020 01:14:30 +0100 Subject: [PATCH] :sparkles: Normalize shape absolute resize implementation. Making it more safe and less dynamic. --- frontend/src/uxbox/main/data/workspace.cljs | 32 +++---- frontend/src/uxbox/main/geom.cljs | 83 +++++++++---------- .../ui/workspace/sidebar/options/canvas.cljs | 2 +- .../ui/workspace/sidebar/options/circle.cljs | 2 +- .../ui/workspace/sidebar/options/icon.cljs | 2 +- .../ui/workspace/sidebar/options/image.cljs | 2 +- .../ui/workspace/sidebar/options/rect.cljs | 2 +- .../ui/workspace/sidebar/options/text.cljs | 2 +- 8 files changed, 54 insertions(+), 73 deletions(-) diff --git a/frontend/src/uxbox/main/data/workspace.cljs b/frontend/src/uxbox/main/data/workspace.cljs index 4a9748f94..94f220bf9 100644 --- a/frontend/src/uxbox/main/data/workspace.cljs +++ b/frontend/src/uxbox/main/data/workspace.cljs @@ -178,7 +178,6 @@ ;; (derive ::update-options ::undo-signal) ;; (derive ::move-selected-layer ::undo-signal) ;; (derive ::materialize-temporal-modifier-in-bulk ::undo-signal) -;; (derive ::update-dimensions ::undo-signal) ;; (derive ::add-shape ::undo-signal) ;; (derive ::add-canvas ::undo-signal))) @@ -1115,26 +1114,6 @@ ;; --- Update Dimensions -(s/def ::width ::us/number) -(s/def ::height ::us/number) - -(s/def ::update-dimensions - (s/keys :opt-un [::width ::height])) - -(defn update-dimensions - "A helper event just for update the position - of the shape using the width and height attrs - instread final point of coordinates." - [id dimensions] - (us/verify ::us/uuid id) - (us/verify ::update-dimensions dimensions) - (ptk/reify ::update-dimensions - IBatchedChange - ptk/UpdateEvent - (update [_ state] - (update-in state [:workspace-data :shapes-by-id id] geom/resize-dim dimensions)))) - - (defn update-rect-dimensions [id attr value] (us/verify ::us/uuid id) @@ -1146,6 +1125,17 @@ (update [_ state] (update-in state [:workspace-data :shapes-by-id id] geom/resize-rect attr value)))) +(defn update-circle-dimensions + [id attr value] + (us/verify ::us/uuid id) + (us/verify #{::rx ::ry} attr) + (us/verify ::us/number value) + (ptk/reify ::update-rect-dimensions + IBatchedChange + ptk/UpdateEvent + (update [_ state] + (update-in state [:workspace-data :shapes-by-id id] geom/resize-rect attr value)))) + ;; --- Shape Proportions (defn toggle-shape-proportion-lock diff --git a/frontend/src/uxbox/main/geom.cljs b/frontend/src/uxbox/main/geom.cljs index b3433a53c..8fe9c2496 100644 --- a/frontend/src/uxbox/main/geom.cljs +++ b/frontend/src/uxbox/main/geom.cljs @@ -5,10 +5,13 @@ ;; Copyright (c) 2016 Andrey Antukh (ns uxbox.main.geom - (:require [uxbox.util.geom.matrix :as gmt] - [uxbox.util.geom.point :as gpt] - [uxbox.util.math :as mth] - [uxbox.main.store :as st])) + (:require + [clojure.spec.alpha :as s] + [uxbox.common.spec :as us] + [uxbox.util.geom.matrix :as gmt] + [uxbox.util.geom.point :as gpt] + [uxbox.util.math :as mth] + [uxbox.main.store :as st])) ;; --- Relative Movement @@ -195,50 +198,38 @@ ;; --- Resize (Dimentsions) -(declare resize-dim-rect) -(declare resize-dim-circle) +(defn resize-rect + [shape attr value] + (us/assert map? shape) + (us/assert #{:width :height} attr) + (us/assert number? value) -(defn resize-dim - "Resize using calculated dimensions (eg, `width` and `height`) - instead of absolute positions." - [shape opts] - (case (:type shape) - :rect (resize-dim-rect shape opts) - :canvas (resize-dim-rect shape opts) - :icon (resize-dim-rect shape opts) - :image (resize-dim-rect shape opts) - :text (resize-dim-rect shape opts) - :circle (resize-dim-circle shape opts))) + (let [{:keys [proportion proportion-lock]} shape] + (if-not proportion-lock + (assoc shape attr value) + (if (= attr :width) + (-> shape + (assoc :width value) + (assoc :height (/ value proportion))) + (-> shape + (assoc :height value) + (assoc :width (* value proportion))))))) -(defn- resize-dim-rect - [{:keys [proportion proportion-lock x y] :as shape} - {:keys [width height] :as dimensions}] - (if-not proportion-lock - (if width - (assoc shape :width width) - (assoc shape :height height)) - (if width - (-> shape - (assoc :width width) - (assoc :height (/ width proportion))) - (-> shape - (assoc :height height) - (assoc :width (* height proportion)))))) - -(defn- resize-dim-circle - [{:keys [proportion proportion-lock] :as shape} - {:keys [rx ry]}] - (if-not proportion-lock - (if rx - (assoc shape :rx rx) - (assoc shape :ry ry)) - (if rx - (-> shape - (assoc :rx rx) - (assoc :ry (/ rx proportion))) - (-> shape - (assoc :ry ry) - (assoc :rx (* ry proportion)))))) +(defn resize-circle + [shape attr value] + (us/assert map? shape) + (us/assert #{:rx :ry} attr) + (us/assert number? value) + (let [{:keys [proportion proportion-lock]} shape] + (if-not proportion-lock + (assoc shape attr value) + (if (= attr :rx) + (-> shape + (assoc :rx value) + (assoc :ry (/ value proportion))) + (-> shape + (assoc :ry value) + (assoc :rx (* value proportion))))))) ;; --- Resize diff --git a/frontend/src/uxbox/main/ui/workspace/sidebar/options/canvas.cljs b/frontend/src/uxbox/main/ui/workspace/sidebar/options/canvas.cljs index 15d6d5cb6..371bbab63 100644 --- a/frontend/src/uxbox/main/ui/workspace/sidebar/options/canvas.cljs +++ b/frontend/src/uxbox/main/ui/workspace/sidebar/options/canvas.cljs @@ -28,7 +28,7 @@ (let [value (-> (dom/get-target event) (dom/get-value) (d/parse-integer 0))] - (st/emit! (udw/update-dimensions (:id shape) {attr value})))) + (st/emit! (udw/update-rect-dimensions (:id shape) attr value)))) on-proportion-lock-change (fn [event] diff --git a/frontend/src/uxbox/main/ui/workspace/sidebar/options/circle.cljs b/frontend/src/uxbox/main/ui/workspace/sidebar/options/circle.cljs index 0caa53dd1..44ad5a51d 100644 --- a/frontend/src/uxbox/main/ui/workspace/sidebar/options/circle.cljs +++ b/frontend/src/uxbox/main/ui/workspace/sidebar/options/circle.cljs @@ -27,7 +27,7 @@ (let [value (-> (dom/get-target event) (dom/get-value) (d/parse-integer 0))] - (st/emit! (udw/update-dimensions (:id shape) {attr value})))) + (st/emit! (udw/update-circle-dimensions (:id shape) attr value)))) on-proportion-lock-change (fn [event] diff --git a/frontend/src/uxbox/main/ui/workspace/sidebar/options/icon.cljs b/frontend/src/uxbox/main/ui/workspace/sidebar/options/icon.cljs index 028e8f87e..7e0c81491 100644 --- a/frontend/src/uxbox/main/ui/workspace/sidebar/options/icon.cljs +++ b/frontend/src/uxbox/main/ui/workspace/sidebar/options/icon.cljs @@ -30,7 +30,7 @@ (let [value (-> (dom/get-target event) (dom/get-value) (d/parse-integer 0))] - (st/emit! (udw/update-dimensions (:id shape) {attr value})))) + (st/emit! (udw/update-rect-dimensions (:id shape) attr value)))) on-proportion-lock-change (fn [event] diff --git a/frontend/src/uxbox/main/ui/workspace/sidebar/options/image.cljs b/frontend/src/uxbox/main/ui/workspace/sidebar/options/image.cljs index 9067fd91a..c6dc210d2 100644 --- a/frontend/src/uxbox/main/ui/workspace/sidebar/options/image.cljs +++ b/frontend/src/uxbox/main/ui/workspace/sidebar/options/image.cljs @@ -25,7 +25,7 @@ (let [value (-> (dom/get-target event) (dom/get-value) (d/parse-integer 0))] - (st/emit! (udw/update-dimensions (:id shape) {attr value})))) + (st/emit! (udw/update-rect-dimensions (:id shape) attr value)))) on-proportion-lock-change (fn [event] diff --git a/frontend/src/uxbox/main/ui/workspace/sidebar/options/rect.cljs b/frontend/src/uxbox/main/ui/workspace/sidebar/options/rect.cljs index 135d9d20e..118ffd7ba 100644 --- a/frontend/src/uxbox/main/ui/workspace/sidebar/options/rect.cljs +++ b/frontend/src/uxbox/main/ui/workspace/sidebar/options/rect.cljs @@ -30,7 +30,7 @@ (let [value (-> (dom/get-target event) (dom/get-value) (d/parse-integer 0))] - (st/emit! (udw/update-dimensions (:id shape) {attr value})))) + (st/emit! (udw/update-rect-dimensions (:id shape) attr value)))) on-proportion-lock-change (fn [event] diff --git a/frontend/src/uxbox/main/ui/workspace/sidebar/options/text.cljs b/frontend/src/uxbox/main/ui/workspace/sidebar/options/text.cljs index 0853a6e44..6aacbdf7f 100644 --- a/frontend/src/uxbox/main/ui/workspace/sidebar/options/text.cljs +++ b/frontend/src/uxbox/main/ui/workspace/sidebar/options/text.cljs @@ -33,7 +33,7 @@ (let [value (-> (dom/get-target event) (dom/get-value) (d/parse-integer 0))] - (st/emit! (udw/update-dimensions (:id shape) {attr value})))) + (st/emit! (udw/update-rect-dimensions (:id shape) attr value)))) on-proportion-lock-change (fn [event]