Add geometry utils

This commit is contained in:
alonso.torres 2024-06-14 12:20:47 +02:00
parent 69fad7a920
commit e13d543dcd
5 changed files with 116 additions and 54 deletions

View file

@ -438,28 +438,28 @@
;; - It consideres the center for everyshape instead of the center of the total selrect ;; - It consideres the center for everyshape instead of the center of the total selrect
;; - The angle param is the desired final value, not a delta ;; - The angle param is the desired final value, not a delta
(defn set-delta-rotation-modifiers (defn set-delta-rotation-modifiers
([angle shapes] [angle shapes {:keys [center delta?] :or {center nil delta? false}}]
(ptk/reify ::set-delta-rotation-modifiers (ptk/reify ::set-delta-rotation-modifiers
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
(let [objects (wsh/lookup-page-objects state) (let [objects (wsh/lookup-page-objects state)
ids ids
(->> shapes (->> shapes
(remove #(get % :blocked false)) (remove #(get % :blocked false))
(filter #(contains? (get editable-attrs (:type %)) :rotation)) (filter #(contains? (get editable-attrs (:type %)) :rotation))
(map :id)) (map :id))
get-modifier get-modifier
(fn [shape] (fn [shape]
(let [delta (- angle (:rotation shape)) (let [delta (if delta? angle (- angle (:rotation shape)))
center (gsh/shape->center shape)] center (or center (gsh/shape->center shape))]
(ctm/rotation-modifiers shape center delta))) (ctm/rotation-modifiers shape center delta)))
modif-tree modif-tree
(-> (build-modif-tree ids objects get-modifier) (-> (build-modif-tree ids objects get-modifier)
(gm/set-objects-modifiers objects))] (gm/set-objects-modifiers objects))]
(assoc state :workspace-modifiers modif-tree)))))) (assoc state :workspace-modifiers modif-tree)))))
(defn apply-modifiers (defn apply-modifiers
([] ([]

View file

@ -400,17 +400,18 @@
(defn increase-rotation (defn increase-rotation
"Rotate shapes a fixed angle, from a keyboard action." "Rotate shapes a fixed angle, from a keyboard action."
[ids rotation] ([ids rotation]
(ptk/reify ::increase-rotation (increase-rotation ids rotation nil))
ptk/WatchEvent ([ids rotation params]
(watch [_ state _] (ptk/reify ::increase-rotation
ptk/WatchEvent
(let [page-id (:current-page-id state) (watch [_ state _]
objects (wsh/lookup-page-objects state page-id) (let [page-id (:current-page-id state)
shapes (->> ids (map #(get objects %)))] objects (wsh/lookup-page-objects state page-id)
(rx/concat shapes (->> ids (map #(get objects %)))]
(rx/of (dwm/set-delta-rotation-modifiers rotation shapes)) (rx/concat
(rx/of (dwm/apply-modifiers))))))) (rx/of (dwm/set-delta-rotation-modifiers rotation shapes params))
(rx/of (dwm/apply-modifiers))))))))
;; -- Move ---------------------------------------------------------- ;; -- Move ----------------------------------------------------------
@ -889,26 +890,32 @@
;; -- Flip ---------------------------------------------------------- ;; -- Flip ----------------------------------------------------------
(defn flip-horizontal-selected [] (defn flip-horizontal-selected
(ptk/reify ::flip-horizontal-selected ([]
ptk/WatchEvent (flip-horizontal-selected nil))
(watch [_ state _] ([ids]
(let [objects (wsh/lookup-page-objects state) (ptk/reify ::flip-horizontal-selected
selected (wsh/lookup-selected state {:omit-blocked? true}) ptk/WatchEvent
shapes (map #(get objects %) selected) (watch [_ state _]
selrect (gsh/shapes->rect shapes) (let [objects (wsh/lookup-page-objects state)
center (grc/rect->center selrect) selected (or ids (wsh/lookup-selected state {:omit-blocked? true}))
modifiers (dwm/create-modif-tree selected (ctm/resize-modifiers (gpt/point -1.0 1.0) center))] shapes (map #(get objects %) selected)
(rx/of (dwm/apply-modifiers {:modifiers modifiers :ignore-snap-pixel true})))))) selrect (gsh/shapes->rect shapes)
center (grc/rect->center selrect)
modifiers (dwm/create-modif-tree selected (ctm/resize-modifiers (gpt/point -1.0 1.0) center))]
(rx/of (dwm/apply-modifiers {:modifiers modifiers :ignore-snap-pixel true})))))))
(defn flip-vertical-selected [] (defn flip-vertical-selected
(ptk/reify ::flip-vertical-selected ([]
ptk/WatchEvent (flip-vertical-selected nil))
(watch [_ state _] ([ids]
(let [objects (wsh/lookup-page-objects state) (ptk/reify ::flip-vertical-selected
selected (wsh/lookup-selected state {:omit-blocked? true}) ptk/WatchEvent
shapes (map #(get objects %) selected) (watch [_ state _]
selrect (gsh/shapes->rect shapes) (let [objects (wsh/lookup-page-objects state)
center (grc/rect->center selrect) selected (or ids (wsh/lookup-selected state {:omit-blocked? true}))
modifiers (dwm/create-modif-tree selected (ctm/resize-modifiers (gpt/point 1.0 -1.0) center))] shapes (map #(get objects %) selected)
(rx/of (dwm/apply-modifiers {:modifiers modifiers :ignore-snap-pixel true})))))) selrect (gsh/shapes->rect shapes)
center (grc/rect->center selrect)
modifiers (dwm/create-modif-tree selected (ctm/resize-modifiers (gpt/point 1.0 -1.0) center))]
(rx/of (dwm/apply-modifiers {:modifiers modifiers :ignore-snap-pixel true})))))))

View file

@ -10,6 +10,7 @@
[app.main.features :as features] [app.main.features :as features]
[app.main.store :as st] [app.main.store :as st]
[app.plugins.api :as api] [app.plugins.api :as api]
[app.plugins.public-utils]
[app.util.globals :refer [global]] [app.util.globals :refer [global]]
[app.util.object :as obj] [app.util.object :as obj]
[beicon.v2.core :as rx] [beicon.v2.core :as rx]

View file

@ -0,0 +1,19 @@
;; 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) KALEIDOS INC
(ns app.plugins.public-utils
"Utilities that will be exposed to plugins developers"
(:require
[app.common.geom.rect :as grc]
[app.common.geom.shapes :as gsh]
[app.plugins.utils :as u]))
(defn ^:export centerShapes
[shapes]
(let [shapes (->> shapes (map u/proxy->shape))]
(-> (gsh/shapes->rect shapes)
(grc/rect->center)
(u/to-js))))

View file

@ -202,6 +202,20 @@
(st/emit! (dw/update-dimensions [$id] :width width) (st/emit! (dw/update-dimensions [$id] :width width)
(dw/update-dimensions [$id] :height height))) (dw/update-dimensions [$id] :height height)))
(rotate
[self angle center]
(let [center (when center {:x (obj/get center "x") :y (obj/get center "y")})]
(cond
(not (number? angle))
(u/display-not-valid :rotate-angle angle)
(and (some? center) (or (not (number? (:x center))) (not (number? (:y center)))))
(u/display-not-valid :rotate-center center)
:else
(let [id (obj/get self "$id")]
(st/emit! (dw/increase-rotation [id] angle {:center center :delta? true}))))))
(clone (clone
[_] [_]
(let [ret-v (atom nil)] (let [ret-v (atom nil)]
@ -624,11 +638,32 @@
{:name "height" {:name "height"
:get #(-> % u/proxy->shape :height)} :get #(-> % u/proxy->shape :height)}
{:name "rotation"
:get #(-> % u/proxy->shape :rotation)
:set
(fn [self value]
(if (number? value)
(let [shape (u/proxy->shape self)]
(st/emit! (dw/increase-rotation #{(:id shape)} value)))
(u/display-not-valid :rotation value)))}
{:name "flipX" {:name "flipX"
:get #(-> % u/proxy->shape :flip-x)} :get #(-> % u/proxy->shape :flip-x boolean)
:set
(fn [self value]
(if (boolean? value)
(let [id (obj/get self "$id")]
(st/emit! (dw/flip-horizontal-selected #{id})))
(u/display-not-valid :flipX value)))}
{:name "flipY" {:name "flipY"
:get #(-> % u/proxy->shape :flip-y)} :get #(-> % u/proxy->shape :flip-y boolean)
:set
(fn [self value]
(if (boolean? value)
(let [id (obj/get self "$id")]
(st/emit! (dw/flip-vertical-selected #{id})))
(u/display-not-valid :flipY value)))}
;; Strokes and fills ;; Strokes and fills
;; TODO: Validate fills input ;; TODO: Validate fills input