🎉 Snap to grid

This commit is contained in:
alonso.torres 2020-05-14 16:02:45 +02:00 committed by Andrés Moya
parent 0b4996b31a
commit 3308d762f1
5 changed files with 91 additions and 87 deletions

View file

@ -12,38 +12,31 @@
[cljs.spec.alpha :as s]
[clojure.set :as set]
[uxbox.util.geom.shapes :as gsh]
[uxbox.util.geom.point :as gpt]))
[uxbox.util.geom.point :as gpt]
[uxbox.util.geom.layout :as gla]))
(defn- frame-snap-points [{:keys [x y width height]}]
(defn- layout-rect-snaps [{:keys [x y width height]}]
#{(gpt/point x y)
(gpt/point (+ x (/ width 2)) y)
(gpt/point (+ x width) y)
(gpt/point (+ x width) (+ y (/ height 2)))
(gpt/point (+ x width) (+ y height))
(gpt/point (+ x (/ width 2)) (+ y height))
(gpt/point x (+ y height))
(gpt/point x (+ y (/ height 2)))})
(gpt/point x (+ y height))})
(defn- frame-snap-points-resize [{:keys [x y width height]} handler]
(case handler
:top-left (gpt/point x y)
:top (gpt/point (+ x (/ width 2)) y)
:top-right (gpt/point (+ x width) y)
:right (gpt/point (+ x width) (+ y (/ height 2)))
:bottom-right (gpt/point (+ x width) (+ y height))
:bottom (gpt/point (+ x (/ width 2)) (+ y height))
:bottom-left (gpt/point x (+ y height))
:left (gpt/point x (+ y (/ height 2)))))
(defn- layout-snap-points [frame {:keys [type] :as layout}]
(mapcat layout-rect-snaps (gla/layout-rects frame layout)))
(def ^:private handler->point-idx
{:top-left 0
:top 0
:top-right 1
:right 1
:bottom-right 2
:bottom 2
:bottom-left 3
:left 3})
(defn- frame-snap-points [{:keys [x y width height layouts] :as frame}]
(into #{(gpt/point x y)
(gpt/point (+ x (/ width 2)) y)
(gpt/point (+ x width) y)
(gpt/point (+ x width) (+ y (/ height 2)))
(gpt/point (+ x width) (+ y height))
(gpt/point (+ x (/ width 2)) (+ y height))
(gpt/point x (+ y height))
(gpt/point x (+ y (/ height 2)))}
(->>
layouts
(filter #(and (not= :grid (:type %)) (:display %)))
(mapcat #(layout-snap-points frame %)))))
(defn shape-snap-points
[shape]