From e8fcb385976a3a47c31a4414869888a1864887cc Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 14 Feb 2017 21:57:28 +0100 Subject: [PATCH] Rename `:points` attr to `:segments` on path shape. --- .../uxbox/main/data/workspace/drawing.cljs | 11 +++--- frontend/src/uxbox/main/geom.cljs | 35 +++++++++---------- frontend/src/uxbox/main/ui/shapes/path.cljs | 27 ++++++++++---- .../src/uxbox/main/ui/shapes/selection.cljs | 4 +-- .../src/uxbox/main/ui/workspace/drawarea.cljs | 10 ++---- 5 files changed, 48 insertions(+), 39 deletions(-) diff --git a/frontend/src/uxbox/main/data/workspace/drawing.cljs b/frontend/src/uxbox/main/data/workspace/drawing.cljs index 41362a86d..e612eda28 100644 --- a/frontend/src/uxbox/main/data/workspace/drawing.cljs +++ b/frontend/src/uxbox/main/data/workspace/drawing.cljs @@ -122,7 +122,7 @@ (deftype FinishPathDrawing [] ptk/UpdateEvent (update [_ state] - (update-in state [:workspace :drawing :points] #(vec (butlast %))))) + (update-in state [:workspace :drawing :segments] #(vec (butlast %))))) (defn finish-path-drawing [] @@ -133,7 +133,7 @@ (deftype InsertDrawingPathPoint [point] ptk/UpdateEvent (update [_ state] - (update-in state [:workspace :drawing :points] (fnil conj []) point))) + (update-in state [:workspace :drawing :segments] (fnil conj []) point))) (defn insert-drawing-path-point [point] @@ -145,9 +145,10 @@ (deftype UpdateDrawingPathPoint [index point] ptk/UpdateEvent (update [_ state] - (let [points (count (get-in state [:workspace :drawing :points]))] + (let [segments (count (get-in state [:workspace :drawing :segments])) + exists? (< -1 index segments)] (cond-> state - (< -1 index points) (assoc-in [:workspace :drawing :points index] point))))) + exists? (assoc-in [:workspace :drawing :segments index] point))))) (defn update-drawing-path-point [index point] @@ -174,7 +175,7 @@ (deftype SimplifyDrawingPath [tolerance] ptk/UpdateEvent (update [_ state] - (update-in state [:workspace :drawing :points] pth/simplify tolerance))) + (update-in state [:workspace :drawing :segments] pth/simplify tolerance))) (defn simplify-drawing-path [tolerance] diff --git a/frontend/src/uxbox/main/geom.cljs b/frontend/src/uxbox/main/geom.cljs index a0938d7e5..41eebe1d3 100644 --- a/frontend/src/uxbox/main/geom.cljs +++ b/frontend/src/uxbox/main/geom.cljs @@ -5,8 +5,7 @@ ;; Copyright (c) 2016 Andrey Antukh (ns uxbox.main.geom - (:require [cljs.pprint :refer [pprint]] - [uxbox.util.geom.matrix :as gmt] + (:require [uxbox.util.geom.matrix :as gmt] [uxbox.util.geom.point :as gpt] [uxbox.util.math :as mth] [uxbox.main.store :as st])) @@ -61,11 +60,11 @@ "A specialized function for relative movement for path shapes." [shape {dx :x dy :y}] - (let [points (:points shape) + (let [segments (:segments shape) xf (comp (map #(update % :x + dx)) (map #(update % :y + dy)))] - (assoc shape :points (into [] xf points)))) + (assoc shape :segments (into [] xf segments)))) ;; --- Absolute Movement @@ -134,15 +133,15 @@ :path (size-path shape))) (defn- size-path - [{:keys [points x1 y1 x2 y2] :as shape}] + [{:keys [segments x1 y1 x2 y2] :as shape}] (if (and x1 y1 x2 y2) (assoc shape :width (- x2 x1) :height (- y2 y1)) - (let [minx (apply min (map :x points)) - miny (apply min (map :y points)) - maxx (apply max (map :x points)) - maxy (apply max (map :y points))] + (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))] (assoc shape :width (- maxx minx) :height (- maxy miny))))) @@ -169,7 +168,7 @@ The point should exists before, this function does not adds it automatically." [shape index point] - (assoc-in shape [:points index] point)) + (assoc-in shape [:segments index] point)) ;; --- Setup Proportions @@ -407,11 +406,11 @@ (assoc :id id)))) (defn- path->rect-shape - [state {:keys [points] :as shape}] - (let [minx (apply min (map :x points)) - miny (apply min (map :y points)) - maxx (apply max (map :x points)) - maxy (apply max (map :y points))] + [state {: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))] (assoc shape :x1 minx :y1 miny @@ -485,9 +484,9 @@ (assoc shape :cx cx :cy cy :rx rx :ry ry))) (defn- transform-path - [{:keys [points] :as shape} xfmt] - (let [points (mapv #(gpt/transform % xfmt) points)] - (assoc shape :points points))) + [{:keys [segments] :as shape} xfmt] + (let [segments (mapv #(gpt/transform % xfmt) segments)] + (assoc shape :segments segments))) ;; --- Outer Rect diff --git a/frontend/src/uxbox/main/ui/shapes/path.cljs b/frontend/src/uxbox/main/ui/shapes/path.cljs index 707324748..c97a3e32c 100644 --- a/frontend/src/uxbox/main/ui/shapes/path.cljs +++ b/frontend/src/uxbox/main/ui/shapes/path.cljs @@ -6,6 +6,7 @@ (ns uxbox.main.ui.shapes.path (:require [potok.core :as ptk] + [cuerdas.core :as str :include-macros true] [uxbox.main.store :as st] [uxbox.main.ui.shapes.common :as common] [uxbox.main.ui.shapes.attrs :as attrs] @@ -14,7 +15,6 @@ [uxbox.util.geom.matrix :as gmt] [uxbox.util.geom.point :as gpt] [uxbox.util.mixins :as mx :include-macros true])) - ;; --- Path Component (declare path-shape) @@ -39,12 +39,25 @@ ;; --- Path Shape (defn- render-path - [{:keys [points close?] :as shape}] - (let [start (first points) - init (str "M " (:x start) " " (:y start)) - path (reduce #(str %1 " L" (:x %2) " " (:y %2)) init points)] - (cond-> path - close? (str " Z")))) + [{:keys [segments close?] :as shape}] + (let [numsegs (count segments)] + (loop [buffer [] + index 0] + (cond + (>= index numsegs) + (if close? + (str/join " " (conj buffer "Z")) + (str/join " " buffer)) + + (zero? index) + (let [{:keys [x y] :as segment} (nth segments index) + buffer (conj buffer (str/istr "M~{x},~{y}"))] + (recur buffer (inc index))) + + :else + (let [{:keys [x y] :as segment} (nth segments index) + buffer (conj buffer (str/istr "L~{x},~{y}"))] + (recur buffer (inc index))))))) (mx/defc path-shape {:mixins [mx/static]} diff --git a/frontend/src/uxbox/main/ui/shapes/selection.cljs b/frontend/src/uxbox/main/ui/shapes/selection.cljs index f0168c64c..b80f25c7d 100644 --- a/frontend/src/uxbox/main/ui/shapes/selection.cljs +++ b/frontend/src/uxbox/main/ui/shapes/selection.cljs @@ -327,12 +327,12 @@ (rx/subscribe stream on-move)))) (mx/defc path-edition-selection-handlers - [{:keys [id points] :as shape} zoom] + [{:keys [id segments] :as shape} zoom] (letfn [(on-mouse-down [index event] (dom/stop-propagation event) (start-path-edition id index))] [:g.controls - (for [[index {:keys [x y]}] (map-indexed vector points)] + (for [[index {:keys [x y]}] (map-indexed vector segments)] [:circle {:cx x :cy y :r (/ 6.0 zoom) :on-mouse-down (partial on-mouse-down index) diff --git a/frontend/src/uxbox/main/ui/workspace/drawarea.cljs b/frontend/src/uxbox/main/ui/workspace/drawarea.cljs index 1fbb00df7..51c4d1f3e 100644 --- a/frontend/src/uxbox/main/ui/workspace/drawarea.cljs +++ b/frontend/src/uxbox/main/ui/workspace/drawarea.cljs @@ -48,7 +48,7 @@ (shapes/render-component))) (mx/defc path-draw-area - [{:keys [points] :as shape}] + [{:keys [segments] :as shape}] (letfn [(on-click [event] (dom/stop-propagation event) (st/emit! (udw/set-tooltip nil) @@ -56,12 +56,8 @@ (on-mouse-enter [event] (st/emit! (udw/set-tooltip "Click to close the path"))) (on-mouse-leave [event] - (st/emit! (udw/set-tooltip nil))) - (drop-last-point [shape] - (let [points (:points shape) - points (vec (butlast points))] - (assoc shape :points points :close? true)))] - (when-let [{:keys [x y]} (first points)] + (st/emit! (udw/set-tooltip nil)))] + (when-let [{:keys [x y] :as segment} (first segments)] [:g (-> (assoc shape :drawing? true) (shapes/render-component))