mirror of
https://github.com/penpot/penpot.git
synced 2025-01-25 07:58:49 -05:00
✨ Normalize shape absolute resize implementation.
Making it more safe and less dynamic.
This commit is contained in:
parent
566fa9cb91
commit
cbffee1486
8 changed files with 54 additions and 73 deletions
|
@ -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
|
||||
|
|
|
@ -5,10 +5,13 @@
|
|||
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
|
||||
|
||||
(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
|
||||
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Add table
Reference in a new issue