|
5 | 5 | [malli.core :as m]
|
6 | 6 | [re-frame.core :as rf]
|
7 | 7 | [renderer.app.db :refer [App]]
|
8 |
| - [renderer.element.handlers :as element.h] |
9 | 8 | [renderer.snap.db :refer [SnapOption NearestNeighbor]]
|
10 | 9 | [renderer.snap.subs :as-alias snap.s]
|
11 |
| - [renderer.tool.handlers :as tool.h] |
12 |
| - [renderer.utils.bounds :as bounds] |
13 |
| - [renderer.utils.element :as element] |
| 10 | + [renderer.tool.hierarchy :as tool.hierarchy] |
14 | 11 | [renderer.utils.math :refer [Vec2D]]))
|
15 | 12 |
|
16 | 13 | (m/=> toggle-option [:-> App SnapOption App])
|
|
20 | 17 | (update-in db [:snap :options] disj option)
|
21 | 18 | (update-in db [:snap :options] conj option)))
|
22 | 19 |
|
23 |
| -(m/=> base-points [:-> App [:vector Vec2D]]) |
24 |
| -(defn base-points |
25 |
| - [db] |
26 |
| - (let [elements (vals (element.h/entities db)) |
27 |
| - selected (filter :selected elements) |
28 |
| - selected-visible (filter :visible selected) |
29 |
| - options (-> db :snap :options)] |
30 |
| - (when (-> db :snap :active) |
31 |
| - (cond |
32 |
| - (and (contains? #{:translate :clone} (:state db)) (seq selected)) |
33 |
| - (reduce (fn [points el] (into points (element/snapping-points el options))) |
34 |
| - (if (seq (rest selected)) |
35 |
| - (bounds/->snapping-points (element.h/bounds db) options) |
36 |
| - []) |
37 |
| - selected-visible) |
38 |
| - |
39 |
| - (contains? #{:edit :scale} (:state db)) |
40 |
| - [(mat/add [(-> db :clicked-element :x) (-> db :clicked-element :y)] |
41 |
| - (tool.h/pointer-delta db))] |
42 |
| - |
43 |
| - :else |
44 |
| - [(:adjusted-pointer-pos db)])))) |
45 |
| - |
46 | 20 | (m/=> find-nearest-neighbors [:-> App [:sequential NearestNeighbor]])
|
47 | 21 | (defn find-nearest-neighbors
|
48 | 22 | [db]
|
49 | 23 | (let [tree @(rf/subscribe [::snap.s/in-viewport-tree])] ; FIXME: Subscription in event.
|
50 | 24 | (map #(let [nearest-neighbor (kdtree/nearest-neighbor tree %)]
|
51 | 25 | (when nearest-neighbor
|
52 |
| - (assoc nearest-neighbor :base-point %))) (base-points db)))) |
| 26 | + (assoc nearest-neighbor :base-point %))) |
| 27 | + (tool.hierarchy/snapping-bases db)))) |
53 | 28 |
|
54 | 29 | (m/=> find-nearest-neighbor [:-> App [:maybe NearestNeighbor]])
|
55 | 30 | (defn find-nearest-neighbor
|
|
74 | 49 | (let [nearest-neighbor (find-nearest-neighbor db)]
|
75 | 50 | (cond-> db
|
76 | 51 | :always
|
77 |
| - (update :snap dissoc :nearest-neighbor) |
| 52 | + (dissoc :nearest-neighbor) |
78 | 53 |
|
79 | 54 | (and (-> db :snap :active) nearest-neighbor)
|
80 |
| - (assoc-in [:snap :nearest-neighbor] nearest-neighbor)))) |
81 |
| - |
82 |
| -(m/=> nearest-neighbor [:-> App [:maybe NearestNeighbor]]) |
83 |
| -(defn nearest-neighbor |
84 |
| - [db] |
85 |
| - (get-in db [:snap :nearest-neighbor])) |
| 55 | + (assoc :nearest-neighbor nearest-neighbor)))) |
86 | 56 |
|
87 | 57 | (m/=> nearest-delta [:-> App Vec2D])
|
88 | 58 | (defn nearest-delta
|
89 | 59 | [db]
|
90 |
| - (let [{:keys [point base-point]} (nearest-neighbor db)] |
| 60 | + (let [{:keys [point base-point]} (:nearest-neighbor db)] |
91 | 61 | (mat/sub point base-point)))
|
92 | 62 |
|
93 | 63 | (defn snap-with
|
94 | 64 | [db f & more]
|
95 | 65 | (let [db (update-nearest-neighbor db)]
|
96 |
| - (if (nearest-neighbor db) |
| 66 | + (if (:nearest-neighbor db) |
97 | 67 | (apply f db (nearest-delta db) more)
|
98 | 68 | db)))
|
0 commit comments