Skip to content

Commit 667075a

Browse files
committed
refactor rulers and start working on grid snapping
1 parent d254ea9 commit 667075a

File tree

6 files changed

+71
-29
lines changed

6 files changed

+71
-29
lines changed

.clj-kondo/metosin/malli-types-cljs/config.edn

+5
Original file line numberDiff line numberDiff line change
@@ -19073,6 +19073,11 @@
1907319073
:ret :boolean}}},
1907419074
->snapping-points {:arities {2 {:args [:seqable :set],
1907519075
:ret :seqable}}}},
19076+
renderer.ruler.handlers {step {:arities {1 {:args [:any], :ret :number}}},
19077+
steps-coll {:arities {3 {:args [:number
19078+
:seqable
19079+
:keyword],
19080+
:ret :sequential}}}},
1907619081
renderer.history.handlers {state-count {:arities {1 {:args [{:op :keys,
1907719082
:opt {:system-fonts :vector,
1907819083
:copied-bounds :seqable,

deps.edn

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
metosin/malli {:mvn/version "0.16.4"}
1414
net.mikera/core.matrix {:mvn/version "0.63.0"}
1515
org.clj-commons/hickory {:mvn/version "0.7.5"}
16+
org.clojure/math.combinatorics {:mvn/version "0.3.0"}
1617
org.slf4j/slf4j-nop {:mvn/version "2.0.16"}
1718
re-frame/re-frame {:mvn/version "1.4.3"}
1819
re-pressed/re-pressed {:mvn/version "0.3.2"}

src/renderer/ruler/handlers.cljs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
(ns renderer.ruler.handlers
2+
(:require
3+
[clojure.math.combinatorics :as combo]
4+
[malli.core :as m]
5+
[renderer.document.db :refer [ZoomFactor]]
6+
[renderer.frame.db :refer [Viewbox]]
7+
[renderer.frame.handlers :as frame.h]
8+
[renderer.ruler.db :refer [Orientation]]))
9+
10+
(m/=> step [:-> ZoomFactor number?])
11+
(defn step
12+
"Returns the grid step given a zoom level.
13+
14+
Any attemt to ingeniously produce this mapping was proven inferior.
15+
Simply returning a fixed step depending on the zoom range works fine.
16+
Zoom levels outside of this range are considered invalid for now.
17+
Maybe we need to revisit this at some point."
18+
[zoom]
19+
(condp > zoom
20+
0.001 2000
21+
0.025 1000
22+
0.05 500
23+
0.1 200
24+
0.25 100
25+
0.5 50
26+
1 15
27+
2 10
28+
5 5
29+
10 2
30+
25 1
31+
50 0.5
32+
0.1))
33+
34+
(m/=> steps-coll [:-> number? Viewbox Orientation [:sequential number?]])
35+
(defn steps-coll
36+
"Returns a collection of steps per orientaion, contained on the given viewbox."
37+
[ruler-step viewbox orientation]
38+
(let [[x y width height] viewbox
39+
sections 10]
40+
(range (- (+ (* sections ruler-step)
41+
(rem (if (= orientation :vertical) y x)
42+
(* sections ruler-step))))
43+
(if (= orientation :vertical) height width)
44+
ruler-step)))
45+
46+
(defn steps-intersections
47+
"Returns the intersection points of the rulers."
48+
[db]
49+
(let [zoom (get-in db [:documents (:active-document db) :zoom])
50+
pan (get-in db [:documents (:active-document db) :pan])
51+
viewbox (frame.h/viewbox zoom pan (:dom-rect db))
52+
ruler-step (step zoom)]
53+
(combo/cartesian-product (steps-coll ruler-step viewbox :vertical)
54+
(steps-coll ruler-step viewbox :horizontal))))

src/renderer/ruler/subs.cljs

+5-27
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
[renderer.app.subs :as-alias app.s]
55
[renderer.document.subs :as-alias document.s]
66
[renderer.element.subs :as-alias element.s]
7-
[renderer.frame.subs :as-alias frame.s]))
7+
[renderer.frame.subs :as-alias frame.s]
8+
[renderer.ruler.handlers :as h]))
89

910
(rf/reg-sub
1011
::ruler
@@ -28,37 +29,14 @@
2829
(rf/reg-sub
2930
::step
3031
:<- [::document.s/zoom]
31-
(fn [zoom _]
32-
;; Any attemt to ingeniously produce this mapping was proven inferior.
33-
;; Simply returning a fixed step depending on the zoom range works fine.
34-
;; Zoom levels outside of this range are considered invalid for now.
35-
;; Maybe we need to revisit this at some point.
36-
(condp > zoom
37-
0.001 2000
38-
0.025 1000
39-
0.05 500
40-
0.1 200
41-
0.25 100
42-
0.5 50
43-
1 15
44-
2 10
45-
5 5
46-
10 2
47-
25 1
48-
50 0.5
49-
0.1)))
32+
h/step)
5033

5134
(rf/reg-sub
5235
::steps-coll
5336
:<- [::step]
5437
:<- [::frame.s/viewbox]
55-
(fn [[ruler-step [x y width height]] [_ orientation]]
56-
(let [sections 10]
57-
(range (- (+ (* sections ruler-step)
58-
(rem (if (= orientation :vertical) y x)
59-
(* sections ruler-step))))
60-
(if (= orientation :vertical) height width)
61-
ruler-step))))
38+
(fn [[step viewbox] [_ orientation]]
39+
(h/steps-coll step viewbox orientation)))
6240

6341
(rf/reg-sub
6442
::bounds-rect-attrs

src/renderer/snap/db.cljs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(:require [renderer.utils.math :refer [Vec2D]]))
33

44
(def snap-options
5-
[:centers :midpoints :corners :nodes])
5+
[:centers :midpoints :corners :nodes #_:grid])
66

77
(def SnapOption
88
(into [:enum] snap-options))

src/renderer/snap/handlers.cljs

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
[renderer.element.handlers :as element.h]
88
[renderer.frame.db :refer [Viewbox]]
99
[renderer.frame.handlers :as frame.h]
10+
[renderer.ruler.handlers :as ruler.h]
1011
[renderer.snap.db :refer [SnapOption NearestNeighbor]]
1112
[renderer.snap.subs :as-alias snap.s]
1213
[renderer.tool.hierarchy :as tool.hierarchy]
@@ -53,7 +54,10 @@
5354
(defn update-tree
5455
[db]
5556
(if (-> db :snap :active)
56-
(let [points (element.h/snapping-points db (tool.hierarchy/snapping-elements db))]
57+
(let [points (element.h/snapping-points db (tool.hierarchy/snapping-elements db))
58+
points (cond-> points
59+
(contains? (-> db :snap :options) :grid)
60+
(into (ruler.h/steps-intersections db)))]
5761
(-> (assoc db
5862
:snapping-points points
5963
:kdtree (kdtree/build-tree points))

0 commit comments

Comments
 (0)