|
| 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)))) |
0 commit comments