0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-26 16:39:49 -05:00

Normalize shape absolute resize implementation.

Making it more safe and less dynamic.
This commit is contained in:
Andrey Antukh 2020-02-01 01:14:30 +01:00
parent 566fa9cb91
commit cbffee1486
8 changed files with 54 additions and 73 deletions

View file

@ -178,7 +178,6 @@
;; (derive ::update-options ::undo-signal) ;; (derive ::update-options ::undo-signal)
;; (derive ::move-selected-layer ::undo-signal) ;; (derive ::move-selected-layer ::undo-signal)
;; (derive ::materialize-temporal-modifier-in-bulk ::undo-signal) ;; (derive ::materialize-temporal-modifier-in-bulk ::undo-signal)
;; (derive ::update-dimensions ::undo-signal)
;; (derive ::add-shape ::undo-signal) ;; (derive ::add-shape ::undo-signal)
;; (derive ::add-canvas ::undo-signal))) ;; (derive ::add-canvas ::undo-signal)))
@ -1115,26 +1114,6 @@
;; --- Update Dimensions ;; --- 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 (defn update-rect-dimensions
[id attr value] [id attr value]
(us/verify ::us/uuid id) (us/verify ::us/uuid id)
@ -1146,6 +1125,17 @@
(update [_ state] (update [_ state]
(update-in state [:workspace-data :shapes-by-id id] geom/resize-rect attr value)))) (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 ;; --- Shape Proportions
(defn toggle-shape-proportion-lock (defn toggle-shape-proportion-lock

View file

@ -5,10 +5,13 @@
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz> ;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.main.geom (ns uxbox.main.geom
(:require [uxbox.util.geom.matrix :as gmt] (:require
[uxbox.util.geom.point :as gpt] [clojure.spec.alpha :as s]
[uxbox.util.math :as mth] [uxbox.common.spec :as us]
[uxbox.main.store :as st])) [uxbox.util.geom.matrix :as gmt]
[uxbox.util.geom.point :as gpt]
[uxbox.util.math :as mth]
[uxbox.main.store :as st]))
;; --- Relative Movement ;; --- Relative Movement
@ -195,50 +198,38 @@
;; --- Resize (Dimentsions) ;; --- Resize (Dimentsions)
(declare resize-dim-rect) (defn resize-rect
(declare resize-dim-circle) [shape attr value]
(us/assert map? shape)
(us/assert #{:width :height} attr)
(us/assert number? value)
(defn resize-dim (let [{:keys [proportion proportion-lock]} shape]
"Resize using calculated dimensions (eg, `width` and `height`) (if-not proportion-lock
instead of absolute positions." (assoc shape attr value)
[shape opts] (if (= attr :width)
(case (:type shape) (-> shape
:rect (resize-dim-rect shape opts) (assoc :width value)
:canvas (resize-dim-rect shape opts) (assoc :height (/ value proportion)))
:icon (resize-dim-rect shape opts) (-> shape
:image (resize-dim-rect shape opts) (assoc :height value)
:text (resize-dim-rect shape opts) (assoc :width (* value proportion)))))))
:circle (resize-dim-circle shape opts)))
(defn- resize-dim-rect (defn resize-circle
[{:keys [proportion proportion-lock x y] :as shape} [shape attr value]
{:keys [width height] :as dimensions}] (us/assert map? shape)
(if-not proportion-lock (us/assert #{:rx :ry} attr)
(if width (us/assert number? value)
(assoc shape :width width) (let [{:keys [proportion proportion-lock]} shape]
(assoc shape :height height)) (if-not proportion-lock
(if width (assoc shape attr value)
(-> shape (if (= attr :rx)
(assoc :width width) (-> shape
(assoc :height (/ width proportion))) (assoc :rx value)
(-> shape (assoc :ry (/ value proportion)))
(assoc :height height) (-> shape
(assoc :width (* height proportion)))))) (assoc :ry value)
(assoc :rx (* value 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))))))
;; --- Resize ;; --- Resize

View file

@ -28,7 +28,7 @@
(let [value (-> (dom/get-target event) (let [value (-> (dom/get-target event)
(dom/get-value) (dom/get-value)
(d/parse-integer 0))] (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 on-proportion-lock-change
(fn [event] (fn [event]

View file

@ -27,7 +27,7 @@
(let [value (-> (dom/get-target event) (let [value (-> (dom/get-target event)
(dom/get-value) (dom/get-value)
(d/parse-integer 0))] (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 on-proportion-lock-change
(fn [event] (fn [event]

View file

@ -30,7 +30,7 @@
(let [value (-> (dom/get-target event) (let [value (-> (dom/get-target event)
(dom/get-value) (dom/get-value)
(d/parse-integer 0))] (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 on-proportion-lock-change
(fn [event] (fn [event]

View file

@ -25,7 +25,7 @@
(let [value (-> (dom/get-target event) (let [value (-> (dom/get-target event)
(dom/get-value) (dom/get-value)
(d/parse-integer 0))] (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 on-proportion-lock-change
(fn [event] (fn [event]

View file

@ -30,7 +30,7 @@
(let [value (-> (dom/get-target event) (let [value (-> (dom/get-target event)
(dom/get-value) (dom/get-value)
(d/parse-integer 0))] (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 on-proportion-lock-change
(fn [event] (fn [event]

View file

@ -33,7 +33,7 @@
(let [value (-> (dom/get-target event) (let [value (-> (dom/get-target event)
(dom/get-value) (dom/get-value)
(d/parse-integer 0))] (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 on-proportion-lock-change
(fn [event] (fn [event]