0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-04 13:29:14 -05:00

Add the ability to draw freely the path.

The path simplification is implemented but deactivated.
This commit is contained in:
Andrey Antukh 2016-08-12 22:03:55 +03:00
parent fa59407aec
commit 49c44766ba
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
2 changed files with 69 additions and 1 deletions

View file

@ -18,6 +18,7 @@
[uxbox.main.ui.workspace.rlocks :as rlocks]
[uxbox.main.geom :as geom]
[uxbox.util.geom.point :as gpt]
[uxbox.util.geom.path :as path]
[uxbox.util.dom :as dom]))
;; --- State
@ -62,6 +63,7 @@
(declare on-init)
(declare on-init-draw-icon)
(declare on-init-draw-path)
(declare on-init-draw-free-path)
(declare on-init-draw-generic)
(defn- watch-draw-actions
@ -77,7 +79,9 @@
(when-let [shape (:drawing @wb/workspace-ref)]
(case (:type shape)
:icon (on-init-draw-icon shape)
:path (on-init-draw-path shape)
:path (if (:free shape)
(on-init-draw-free-path shape)
(on-init-draw-path shape))
(on-init-draw-generic shape))))
(defn- on-init-draw-icon
@ -170,6 +174,57 @@
(rx/subscribe ptstream on-click)
(rx/subscribe stream on-draw nil on-end))))
(defn- on-init-draw-free-path
[shape]
(let [mouse (->> (rx/sample 10 wb/mouse-viewport-s)
(rx/mapcat (fn [point]
(if @wb/alignment-ref
(uds/align-point point)
(rx/of point))))
(rx/map #(gpt/subtract % canvas-coords)))
stoper (->> wb/events-s
(rx/map first)
(rx/filter #(= % :mouse/up))
(rx/take 1))
stream (rx/take-until stoper mouse)]
(letfn [(normalize-shape [{: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))
dx (- 0 minx)
dy (- 0 miny)
points (mapv #(gpt/add % [dx dy]) points)
;; points (path/simplify points)
width (- maxx minx)
height (- maxy miny)]
(assoc shape
:x1 minx
:y1 miny
:x2 maxx
:y2 maxy
:view-box [0 0 width height]
:points points)))
(on-draw [point]
(let [point (gpt/point point)
shape (-> (or @drawing-shape shape)
(update :points conj point))]
(reset! drawing-shape shape)))
(on-end []
(let [shape (normalize-shape @drawing-shape)]
(rs/emit! (uds/add-shape shape)
(udw/select-for-drawing nil)
(uds/select-first-shape))
(reset! drawing-shape nil)
(reset! drawing-position nil)
(rlocks/release! :ui/draw)))]
(rx/subscribe stream on-draw nil on-end))))
(defn- on-init-draw-generic
[shape]
(let [mouse (->> (rx/sample 10 wb/mouse-viewport-s)

View file

@ -0,0 +1,13 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.util.geom.path
(:require [uxbox.util.geom.path-impl-simplify :as impl-simplify]))
(defn simplify
[points]
(let [points (into-array points)]
(into [] (impl-simplify/simplify points 10 true))))