0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-18 10:41:29 -05:00

♻️ Removed curves shapes

This commit is contained in:
alonso.torres 2020-11-19 18:12:15 +01:00
parent 55b71a111b
commit e2593c2dad
21 changed files with 41 additions and 511 deletions

View file

@ -30,24 +30,7 @@
dy (d/check-num dy)]
(-> shape
(assoc-in [:modifiers :displacement] (gmt/translate-matrix (gpt/point dx dy)))
(gtr/transform-shape)))
#_(let [inc-x (nilf (fn [x] (+ (-chk x) (-chk dx))))
inc-y (nilf (fn [y] (+ (-chk y) (-chk dy))))
inc-point (nilf (fn [p] (-> p
(update :x inc-x)
(update :y inc-y))))]
(-> shape
(update :x inc-x)
(update :y inc-y)
(update-in [:selrect :x] inc-x)
(update-in [:selrect :x1] inc-x)
(update-in [:selrect :x2] inc-x)
(update-in [:selrect :y] inc-y)
(update-in [:selrect :y1] inc-y)
(update-in [:selrect :y2] inc-y)
(update :points #(mapv inc-point %))
(update :segments #(mapv inc-point %)))))
(gtr/transform-shape))))
;; --- Absolute Movement
@ -60,17 +43,6 @@
dy (- (d/check-num y) (-> shape :selrect :y))]
(move shape (gpt/point dx dy))))
;; --- Paths
#_(defn update-path-point
"Update a concrete point in the path.
The point should exists before, this function
does not adds it automatically."
[shape index point]
(assoc-in shape [:segments index] point))
;; --- Resize (Dimensions)
;;; TODO: CHANGE TO USE THE MODIFIERS
(defn resize
@ -136,20 +108,6 @@
:image (setup-image shape props)
(setup-rect shape props)))
;; --- Resolve Shape
;; (declare resolve-rect-shape)
;; (declare translate-from-frame)
;; (declare translate-to-frame)
;;
;; (defn resolve-shape
;; [objects shape]
;; (loop [pobj (get objects parent)]
;; (if (= :frame (:type pobj))
;; (translate-from-frame shape pobj)
;; (recur (get objects (:parent pobj))))))
;; --- Outer Rect
(defn selection-rect

View file

@ -30,11 +30,6 @@
[selrect]
(center-rect selrect))
(defn center-shape
"Calculate the center of the shape."
[shape]
(center-rect (:selrect shape)))
(defn center-points [points]
(let [minx (transduce (map :x) min ##Inf points)
miny (transduce (map :y) min ##Inf points)
@ -43,6 +38,11 @@
(gpt/point (/ (+ minx maxx) 2)
(/ (+ miny maxy) 2))))
(defn center-shape
"Calculate the center of the shape."
[shape]
(center-rect (:selrect shape)))
(defn make-centered-rect
"Creates a rect given a center and a width and height"
[center width height]
@ -50,31 +50,3 @@
:y (- (:y center) (/ height 2))
:width width
:height height})
;; --- Center
#_(
(declare center-rect)
(declare center-path)
(defn- center-path
[{:keys [segments] :as shape}]
(let [minx (apply min (map :x segments))
miny (apply min (map :y segments))
maxx (apply max (map :x segments))
maxy (apply max (map :y segments))]
(gpt/point (/ (+ minx maxx) 2) (/ (+ miny maxy) 2))))
(defn center->rect
"Creates a rect given a center and a width and height"
[center width height]
{:x (- (:x center) (/ width 2))
:y (- (:y center) (/ height 2))
:width width
:height height})
)

View file

@ -58,70 +58,3 @@
:width (- maxx minx)
:height (- maxy miny)}))
;; --- SHAPE -> RECT
#_(
(defn- rect->rect-shape
[{:keys [x y width height] :as shape}]
(assoc shape
:x1 x
:y1 y
:x2 (+ x width)
:y2 (+ y height)))
(defn- path->rect-shape
[{:keys [segments] :as shape}]
(merge shape
{:type :rect}
(:selrect shape)))
(defn shape->rect-shape
"Coerce shape to rect like shape."
[{:keys [type] :as shape}]
(case type
(:curve :path) (path->rect-shape shape)
(rect->rect-shape shape)))
;; Shape->PATH
(declare rect->path)
(defn shape->path
[shape]
(case (:type shape)
(:curve :path) shape
(rect->path shape)))
(defn rect->path
[{:keys [x y width height] :as shape}]
(let [points [(gpt/point x y)
(gpt/point (+ x width) y)
(gpt/point (+ x width) (+ y height))
(gpt/point x (+ y height))
(gpt/point x y)]]
(-> shape
(assoc :type :path)
(assoc :segments points))))
;; -- Points
(defn points->selrect [points]
(let [minx (transduce (map :x) min ##Inf points)
miny (transduce (map :y) min ##Inf points)
maxx (transduce (map :x) max ##-Inf points)
maxy (transduce (map :y) max ##-Inf points)]
{:x1 minx
:y1 miny
:x2 maxx
:y2 maxy
:x minx
:y miny
:width (- maxx minx)
:height (- maxy miny)
:type :rect}))
)

View file

@ -60,26 +60,6 @@
(transform-points matrix))]
(gpr/points->rect points)))
(defn- shape->points [shape]
(let [transform-point
(fn [point]
(-> point
(transform-point-center (gco/center-shape shape)
(:transform shape (gmt/matrix)))
(gpt/round)))
points (cond
(and (= :path (:type shape)) (:content shape))
(gpa/content->points (:content shape))
(seq (:segments shape))
(gpa/segments->points (:content shape))
:else
(gpr/rect->points shape))]
(mapv transform-point points)))
(defn normalize-scale
"We normalize the scale so it's not too close to 0"
[scale]
@ -88,7 +68,6 @@
(and (>= scale 0) (< scale 0.01)) 0.01
:else scale))
(defn modifiers->transform
([center modifiers]
(modifiers->transform (gmt/matrix) center modifiers))
@ -223,10 +202,6 @@
:points points
:selrect selrect)))
(defn apply-transform-curve
[shape transform]
shape)
(defn apply-transform-rect
"Given a new set of points transformed, set up the rectangle so it keeps
its properties. We adjust de x,y,width,height and create a custom transform"
@ -267,7 +242,6 @@
(let [apply-transform-fn
(case (:type shape)
:path apply-transform-path
:curve apply-transform-curve
apply-transform-rect)]
(apply-transform-fn shape transform)))
@ -287,239 +261,3 @@
(apply-transform transform)
(dissoc :modifiers)))
shape)))
#_(defn transform-shape
"Transform the shape properties given the modifiers"
([shape]
(letfn [(transform-by-type [shape]
(case (:type shape)
(:curve :path)
(transform-path-shape shape)
#_:default
(transform-rect-shape shape)))]
(cond-> shape
(:modifiers shape) (transform-by-type)
:always (dissoc :modifiers)))))
;; --- Transform Shape
#_(
(declare transform-rect)
(declare transform-path)
(declare transform)
(defn center-transform [shape matrix]
(let [shape-center (gco/center shape)]
(-> shape
(transform
(-> (gmt/matrix)
(gmt/translate shape-center)
(gmt/multiply matrix)
(gmt/translate (gpt/negate shape-center)))))))
(defn- transform-rect
[{:keys [x y width height] :as shape} mx]
(let [tl (gpt/transform (gpt/point x y) mx)
tr (gpt/transform (gpt/point (+ x width) y) mx)
bl (gpt/transform (gpt/point x (+ y height)) mx)
br (gpt/transform (gpt/point (+ x width) (+ y height)) mx)
;; TODO: replace apply with transduce (performance)
minx (apply min (map :x [tl tr bl br]))
maxx (apply max (map :x [tl tr bl br]))
miny (apply min (map :y [tl tr bl br]))
maxy (apply max (map :y [tl tr bl br]))]
(assoc shape
:x minx
:y miny
:width (- maxx minx)
:height (- maxy miny))))
(defn- transform-path
[{:keys [segments] :as shape} xfmt]
(let [segments (mapv #(gpt/transform % xfmt) segments)]
(assoc shape :segments segments)))
(defn update-path-selrect [shape]
(as-> shape $
(assoc $ :points (shape->points $))
(assoc $ :selrect (gpr/points->selrect (:points $)))
(assoc $ :x (get-in $ [:selrect :x]))
(assoc $ :y (get-in $ [:selrect :y]))
(assoc $ :width (get-in $ [:selrect :width]))
(assoc $ :height (get-in $ [:selrect :height]))))
(defn fix-invalid-rect-values
[rect-shape]
(letfn [(check [num]
(if (or (nil? num) (mth/nan? num) (= ##Inf num) (= ##-Inf num)) 0 num))
(to-positive [num] (if (< num 1) 1 num))]
(-> rect-shape
(update :x check)
(update :y check)
(update :width (comp to-positive check))
(update :height (comp to-positive check)))))
(declare transform-points)
(defn apply-modifiers
[transform-stack modifiers points]
(let [ds-modifier (:displacement modifiers (gmt/matrix))
{res-x :x res-y :y} (:resize-vector modifiers (gpt/point 1 1))
;; Normalize x/y vector coordinates because scale by 0 is infinite
res-x (normalize-scale res-x)
res-y (normalize-scale res-y)
resize (gpt/point res-x res-y)
origin (:resize-origin modifiers (gpt/point 0 0))
resize-transform (:resize-transform modifiers (gmt/matrix))
resize-transform-inverse (:resize-transform-inverse modifiers (gmt/matrix))
rt-modif (or (:rotation modifiers) 0)
points (transform-points ds-modifier)
center (gco/center-points points)]
(-> points
(transform-points
(-> (gmt/matrix)
;; Applies the current resize transformation
(gmt/translate origin)
(gmt/multiply resize-transform)
(gmt/scale resize)
(gmt/multiply resize-transform-inverse)
(gmt/translate (gpt/negate origin))
;; Applies the stacked transformations
(gmt/translate center)
(gmt/multiply (gmt/rotate-matrix rt-modif))
(gmt/multiply transform-stack)
(gmt/translate (gpt/negate center)))))))
(defn transform-path-shape
[shape]
shape
#_(-> shape
transform-apply-modifiers
update-path-selrect)
;; TODO: Addapt for paths is not working
#_(let [shape-path (transform-apply-modifiers shape)
shape-path-center (center shape-path)
shape-transform-inverse' (-> (gmt/matrix)
(gmt/translate shape-path-center)
(gmt/multiply (:transform-inverse shape (gmt/matrix)))
(gmt/multiply (gmt/rotate-matrix (- (:rotation-modifier shape 0))))
(gmt/translate (gpt/negate shape-path-center)))]
(-> shape-path
(transform shape-transform-inverse')
(add-rotate-transform (:rotation-modifier shape 0)))))
(defn adjust-rect-transforms [shape]
)
(defn transform-rect-shape
[shape]
(let [points (-> (:points shape (shape->points shape))
(apply-modifiers (:transform shape) (:modifiers shape) points))
center (gco/center-points points)
resize-vector (-> (get-in shape [:modifiers :resize-vector] (gpt/point 1 1))
(update :x #(if (zero? %) 1 %))
(update :y #(if (zero? %) 1 %)))
;; Reverse the current transformation stack to get the base rectangle
tr-inverse (:transform-inverse shape (gmt/matrix))
points-temp (transform-poins points center tr-inverse)
points-temp-dim (gpr/rect-points-dimensions points)
points-temp-rec (gpr/points->selrect points)
;; This rectangle is the new data for the current rectangle. We want to change our rectangle
;; to have this width, height, x, y
rec (-> (gco/center->rect center (:width points-temp-dim) (:height points-temp-dim))
(gpr/rect->points))
;;rec (fix-invalid-rect-values rec)
;;rec-path (gpr/rect->path rec)
;; The next matrix is a series of transformations we have to do to the previous rec so that
;; after applying them the end result is the `shape-path-temp`
;; This is compose of three transformations: skew, resize and rotation
stretch-matrix (gmt/matrix)
skew-angle (calculate-rec-path-skew-angle shape-path-temp)
;; When one of the axis is flipped we have to reverse the skew
skew-angle (if (neg? (* (:x resize-vector) (:y resize-vector))) (- skew-angle) skew-angle )
skew-angle (if (mth/nan? skew-angle) 0 skew-angle)
stretch-matrix (gmt/multiply stretch-matrix (gmt/skew-matrix skew-angle 0))
h1 (calculate-rec-path-height shape-path-temp)
h2 (calculate-rec-path-height (center-transform rec-path stretch-matrix))
h3 (/ h1 h2)
h3 (if (mth/nan? h3) 1 h3)
stretch-matrix (gmt/multiply stretch-matrix (gmt/scale-matrix (gpt/point 1 h3)))
rotation-angle (calculate-rec-path-rotation (center-transform rec-path stretch-matrix)
shape-path-temp resize-vector)
stretch-matrix (gmt/multiply (gmt/rotate-matrix rotation-angle) stretch-matrix)
;; This is the inverse to be able to remove the transformation
stretch-matrix-inverse (-> (gmt/matrix)
(gmt/scale (gpt/point 1 h3))
(gmt/skew (- skew-angle) 0)
(gmt/rotate (- rotation-angle)))
new-shape (as-> shape $
(merge $ rec)
(update $ :x #(mth/precision % 0))
(update $ :y #(mth/precision % 0))
(update $ :width #(mth/precision % 0))
(update $ :height #(mth/precision % 0))
(update $ :transform #(gmt/multiply (or % (gmt/matrix)) stretch-matrix))
(update $ :transform-inverse #(gmt/multiply stretch-matrix-inverse (or % (gmt/matrix))))
(assoc $ :points (shape->points $))
(assoc $ :selrect (gpr/points->selrect (:points $)))
(update $ :selrect fix-invalid-rect-values)
(update $ :rotation #(mod (+ (or % 0)
(or (get-in $ [:modifiers :rotation]) 0)) 360)))]
new-shape))
(defn transform-points
"Apply the matrix transformation to points"
[points xfmt]
(cond->> points
(gmt/matrix? xfmt) (map #(gpt/transform % xfmt))))
#_(defn transform
"Apply the matrix transformation to shape"
[{:keys [type] :as shape} xfmt]
(if (gmt/matrix? xfmt)
(case type
:path (transform-path shape xfmt)
:curve (transform-path shape xfmt)
(transform-rect shape xfmt))
shape))
)

View file

@ -623,23 +623,15 @@
:stroke-color "#000000"
:stroke-opacity 0}
{:type :curve
:name "Path"
:fill-color "#000000"
:fill-opacity 0
:stroke-style :solid
:stroke-alignment :center
:stroke-width 2
:stroke-color "#000000"
:stroke-opacity 1}
{:type :text
:name "Text"
:content nil}])
(defn make-minimal-shape
[type]
(let [shape (d/seek #(= type (:type %)) minimal-shapes)]
(let [type (cond (= type :curve) :path
:else type)
shape (d/seek #(= type (:type %)) minimal-shapes)]
(when-not shape
(ex/raise :type :assertion
:code :shape-type-not-implemented
@ -649,7 +641,7 @@
:always
(assoc :id (uuid/next))
(not #{:path :curve})
(not= :path (:type shape))
(assoc :x 0
:y 0
:width 1
@ -771,6 +763,7 @@
{:rotation angle
:displacement displacement}))
;; reg-objects operation "regenerates" the values for the parent groups
(defmethod process-change :reg-objects
[data {:keys [page-id shapes]}]
(letfn [(reg-objects [objects]

View file

@ -99,7 +99,5 @@
;; default
(box/handle-drawing-box))))))
;; Export
#_(def close-drawing-path path/close-drawing-path)

View file

@ -28,10 +28,15 @@
(defn insert-point-segment [state point]
(update-in state [:workspace-drawing :object :segments] (fnil conj []) point))
(defn update-selrect [{:keys [segments] :as shape}]
(let [points (->> segments
(map #(apply gpt/point %)))]
(assoc shape :selrect (gsh/points->selrect points))))
(defn curve-to-path [{:keys [segments] :as shape}]
(let [content (path/segments->content segments)
selrect (gsh/content->selrect content)
points (gsh/rect->points selrect)]
(-> shape
(dissoc :segments)
(assoc :content content)
(assoc :selrect selrect)
(assoc :points points))))
(defn finish-drawing-curve [state]
(update-in
@ -39,7 +44,7 @@
(fn [shape]
(-> shape
(update :segments #(path/simplify % simplify-tolerance))
(update-selrect)))))
(curve-to-path)))))
(defn handle-drawing-curve []
(ptk/reify ::handle-drawing-curve

View file

@ -84,7 +84,7 @@
{:keys [rotation]} shape
shapev (-> (gpt/point width height))
rotation (if (#{:curve :path} (:type shape)) 0 rotation)
rotation (if (= :path (:type shape)) 0 rotation)
;; Vector modifiers depending on the handler
handler-modif (let [[x y] (handler-modifiers handler)] (gpt/point x y))

View file

@ -84,7 +84,6 @@
opts #js {:shape shape}]
[:> shape-container {:shape shape}
(case (:type shape)
:curve [:> path/path-shape opts]
:text [:> text/text-shape opts]
:rect [:> rect/rect-shape opts]
:path [:> path/path-shape opts]

View file

@ -28,7 +28,6 @@
:rect [:layout :fill :stroke :shadow :blur]
:circle [:layout :fill :stroke :shadow :blur]
:path [:layout :fill :stroke :shadow :blur]
:curve [:layout :fill :stroke :shadow :blur]
:image [:image :layout :shadow :blur]
:text [:layout :text :shadow :blur]})

View file

@ -127,7 +127,6 @@
opts #js {:shape shape
:frame frame}]
(case (:type shape)
:curve [:> path-wrapper opts]
:text [:> text-wrapper opts]
:rect [:> rect-wrapper opts]
:path [:> path-wrapper opts]

View file

@ -153,7 +153,6 @@
(geom/translate-to-frame frame))
opts #js {:shape shape}]
(case (:type shape)
:curve [:> path-wrapper opts]
:text [:> text-wrapper opts]
:rect [:> rect-wrapper opts]
:path [:> path-wrapper opts]

View file

@ -45,31 +45,3 @@
:fill "transparent"
:stroke-width (/ 1 zoom)}}])))
#_(mf/defc path-draw-area
[{:keys [shape] :as props}]
(let [locale (i18n/use-locale)
on-click
(fn [event]
(dom/stop-propagation event)
(st/emit! (dw/assign-cursor-tooltip nil)
(dd/close-drawing-path)))
on-mouse-enter
(fn [event]
(let [msg (t locale "workspace.viewport.click-to-close-path")]
(st/emit! (dw/assign-cursor-tooltip msg))))
on-mouse-leave
(fn [event]
(st/emit! (dw/assign-cursor-tooltip nil)))]
[:g.drawing
[:& shapes/shape-wrapper {:shape shape}]
#_(when (not= :curve (:type shape))
[:circle.close-bezier
{:cx x
:cy y
:r 5
:on-click on-click
:on-mouse-enter on-mouse-enter
:on-mouse-leave on-mouse-leave}])]))

View file

@ -215,44 +215,6 @@
:resize-side [:> resize-side-handler props])))])))
;; --- Selection Handlers (Component)
(mf/defc path-edition-selection-handlers
[{:keys [shape modifiers zoom color] :as props}]
(letfn [(on-mouse-down [event index]
(dom/stop-propagation event)
;; TODO: this need code ux refactor
(let [stoper (get-edition-stream-stoper)
stream (->> (ms/mouse-position-deltas @ms/mouse-position)
(rx/take-until stoper))]
;; (when @refs/selected-alignment
;; (st/emit! (dw/initial-path-point-align (:id shape) index)))
(rx/subscribe stream #(on-handler-move % index))))
(get-edition-stream-stoper []
(let [stoper? #(and (ms/mouse-event? %) (= (:type %) :up))]
(rx/merge
(rx/filter stoper? st/stream)
(->> st/stream
(rx/filter #(= % :interrupt))
(rx/take 1)))))
(on-handler-move [delta index]
(st/emit! (dw/update-path (:id shape) index delta)))]
(let [transform (geom/transform-matrix shape)
displacement (:displacement modifiers)
segments (cond->> (:segments shape)
displacement (map #(gpt/transform % displacement)))]
[:g.controls
(for [[index {:keys [x y]}] (map-indexed vector segments)]
(let [{:keys [x y]} (gpt/transform (gpt/point x y) transform)]
[:circle {:cx x :cy y
:r (/ 6.0 zoom)
:key index
:on-mouse-down #(on-mouse-down % index)
:fill "#ffffff"
:stroke color
:style {:cursor cur/move-pointer}}]))])))
;; TODO: add specs for clarity
(mf/defc text-edition-selection-handlers
@ -372,12 +334,6 @@
[:& path-editor {:zoom zoom
:shape shape}]
(and (= type :curve)
(= edition (:id shape)))
[:& path-edition-selection-handlers {:shape shape
:zoom zoom
:color color}]
:else
[:& single-selection-handlers {:shape shape
:zoom zoom

View file

@ -108,7 +108,6 @@
:on-mouse-leave on-mouse-leave
:style {:cursor (if @alt? cur/duplicate nil)}}
(case (:type shape)
:curve [:> path/path-wrapper opts]
:path [:> path/path-wrapper opts]
:text [:> text/text-wrapper opts]
:group [:> group-wrapper opts]

View file

@ -28,7 +28,7 @@
outline-type (case (:type shape)
:circle "ellipse"
(:curve :path) "path"
:path "path"
"rect")
common {:fill "transparent"
@ -44,7 +44,7 @@
:rx (/ width 2)
:ry (/ height 2)}
(:curve :path)
:path
{:d (path/render-path shape)}
{:x x

View file

@ -129,7 +129,6 @@
:rect i/box
:circle i/circle
:text i/text
:curve i/curve
:path i/curve
:frame i/artboard
:group i/folder
@ -141,7 +140,7 @@
i/layers))
(defn is-shape? [type]
#{:shape :rect :circle :text :curve :path :frame :group})
#{:shape :rect :circle :text :path :frame :group})
(defn parse-entry [{:keys [redo-changes]}]
(->> redo-changes

View file

@ -39,7 +39,6 @@
:circle i/circle
:path i/curve
:rect i/box
:curve i/curve
:text i/text
:group (if (some? (:component-id shape))
i/component

View file

@ -48,7 +48,6 @@
:icon [:& icon/options {:shape shape}]
:circle [:& circle/options {:shape shape}]
:path [:& path/options {:shape shape}]
:curve [:& path/options {:shape shape}]
:image [:& image/options {:shape shape}]
nil)
[:& exports-menu

View file

@ -213,3 +213,18 @@
opposite (gpt/add point (gpt/negate phv))]
opposite))
(defn segments->content [segments]
(let [initial (first segments)
closed? (= (first segments) (last segments))
lines (if closed?
(take (- (count segments) 2) (rest segments))
(rest segments))]
(d/concat [{:command :move-to
:params (select-keys initial [:x :y])}]
(->> lines
(mapv #(hash-map :command :line-to
:params (select-keys % [:x :y]))))
(when closed?
[{:command :close-path}]))))

View file

@ -32,6 +32,4 @@
(let [shape (gsh/transform-shape shape)]
(case (:type shape)
:frame (-> shape :selrect frame-snap-points)
(:path :curve) (-> shape :selrect selrect-snap-points)
(into #{(gsh/center-shape shape)} (:points shape)))
))
(into #{(gsh/center-shape shape)} (:points shape)))))