Skip to content

Commit f8b11e6

Browse files
committed
fix nested in-place scale and refactor
1 parent 468ef76 commit f8b11e6

File tree

8 files changed

+41
-25
lines changed

8 files changed

+41
-25
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@
306306
:key-code :number,
307307
:key :string,
308308
:modifiers :set}}],
309-
:ret :any}}}},
309+
:ret :any}}},
310+
pointer-delta {:arities {1 {:args [:any], :ret :any}}}},
310311
renderer.utils.math {clamp {:arities {3 {:args [:number :number :number],
311312
:ret :number}}},
312313
angle-dx {:arities {2 {:args [:number :number],
@@ -330,7 +331,9 @@
330331
:ret :any}}},
331332
focus-bounds {:arities {2 {:args [:any :keyword],
332333
:ret :any},
333-
3 {:args [:any :any :any],
334+
3 {:args [:any
335+
:keyword
336+
:seqable],
334337
:ret :any}}},
335338
zoom-in-place {:arities {3 {:args [:any
336339
:number

src/renderer/app/handlers.cljs

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns renderer.app.handlers
22
(:require
3+
[clojure.core.matrix :as mat]
34
[malli.experimental :as mx]
45
[renderer.app.db :refer [State]]
56
[renderer.app.effects :as-alias fx]
@@ -34,6 +35,10 @@
3435
(assoc :tool tool)
3536
(tool.hierarchy/activate)))
3637

38+
(mx/defn pointer-delta
39+
[db]
40+
(mat/sub (:adjusted-pointer-pos db) (:adjusted-pointer-offset db)))
41+
3742
(mx/defn pointer-handler
3843
[db, e :- PointerEvent, now :- number?]
3944
(let [{:keys [pointer-offset tool dom-rect drag primary-tool drag-threshold]} db

src/renderer/element/events.cljs

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@
212212
[(finalize "Scale selection")]
213213
(fn [db [_ ratio]]
214214
(let [pivot-point (-> db h/bounds bounds/center)]
215-
(h/scale db ratio pivot-point true))))
215+
(h/scale db ratio pivot-point true true))))
216216

217217
(rf/reg-event-db
218218
::move-up

src/renderer/element/handlers.cljs

+14-8
Original file line numberDiff line numberDiff line change
@@ -445,14 +445,20 @@
445445
(update-el db id hierarchy/place pos)))
446446

447447
(defn scale
448-
[db ratio pivot-point recur?]
449-
(reduce
450-
(fn [db el]
451-
(let [pivot-point (->> (element db el) :bounds (take 2) (mat/sub pivot-point))]
452-
(update-el db el hierarchy/scale ratio pivot-point)))
453-
db
454-
(cond-> (selected-ids db)
455-
recur? (concat (descendant-ids db)))))
448+
[db ratio pivot-point in-place recursive]
449+
(let [ids-to-scale (cond-> (selected-ids db) recursive (set/union (descendant-ids db)))]
450+
(reduce
451+
(fn [db id]
452+
(let [pivot-point (->> (element db id) :bounds (take 2) (mat/sub pivot-point))
453+
db (update-el db id hierarchy/scale ratio pivot-point)]
454+
(if in-place
455+
(let [pointer-delta (mat/sub (:adjusted-pointer-pos db) (:adjusted-pointer-offset db))
456+
child-ids (set (children-ids db id))
457+
child-ids (set/intersection child-ids ids-to-scale)]
458+
(reduce (partial-right translate pointer-delta) db child-ids))
459+
db)))
460+
db
461+
ids-to-scale)))
456462

457463
(defn align
458464
([db direction]

src/renderer/frame/handlers.cljs

+7-5
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,21 @@
7373
(mat/add [x1 y1]))]
7474
(assoc-in db [:documents (:active-document db) :pan] pan)))
7575

76+
(def FocusType [:enum :original :fit :fill])
77+
7678
(mx/defn focus-bounds
77-
([db, focus-type :- [:enum :original :fit :fill]]
79+
([db, focus-type :- FocusType]
7880
(cond-> db
7981
(:active-document db)
8082
(focus-bounds focus-type (or (element.h/bounds db)
8183
(element/united-bounds (element.h/root-children db))))))
82-
([{:keys [active-document dom-rect] :as db} focus-type bounds]
84+
([db, focus-type :- FocusType, bounds :- Bounds]
8385
(let [[width height] (utils.bounds/->dimensions bounds)
84-
width-ratio (/ (:width dom-rect) width)
85-
height-ratio (/ (:height dom-rect) height)
86+
width-ratio (/ (-> db :dom-rect :width) width)
87+
height-ratio (/ (-> db :dom-rect :height) height)
8688
min-zoom (min width-ratio height-ratio)]
8789
(-> db
88-
(assoc-in [:documents active-document :zoom]
90+
(assoc-in [:documents (:active-document db) :zoom]
8991
(case focus-type
9092
:original (min (* min-zoom 0.9) 1)
9193
:fit min-zoom

src/renderer/snap/handlers.cljs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[kdtree :as kdtree]
55
[malli.experimental :as mx]
66
[re-frame.core :as rf]
7+
[renderer.app.handlers :as app.h]
78
[renderer.element.handlers :as element.h]
89
[renderer.snap.db :refer [SnapOption]]
910
[renderer.snap.subs :as-alias snap.s]
@@ -28,8 +29,7 @@
2829

2930
(contains? #{:edit :scale} (:state db))
3031
[(mat/add [(-> db :clicked-element :x) (-> db :clicked-element :y)]
31-
(mat/sub (:adjusted-pointer-pos db)
32-
(:adjusted-pointer-offset db)))]
32+
(app.h/pointer-delta db))]
3333

3434
:else
3535
[(:adjusted-pointer-pos db)]))))

src/renderer/tool/impl/transform/edit.cljs

+1-2
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,9 @@
6464
(defmethod hierarchy/drag :edit
6565
[db e]
6666
(let [clicked-element (:clicked-element db)
67-
pointer-delta (mat/sub (:adjusted-pointer-pos db) (:adjusted-pointer-offset db))
6867
db (history.h/swap db)
6968
el-id (:element clicked-element)
70-
delta (cond-> pointer-delta (pointer/ctrl? e) pointer/lock-direction)]
69+
delta (cond-> (app.h/pointer-delta db) (pointer/ctrl? e) pointer/lock-direction)]
7170
(cond-> db
7271
el-id
7372
(-> (element.h/update-el el-id element.hierarchy/edit delta (:id clicked-element))

src/renderer/tool/impl/transform/select.cljs

+6-5
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@
210210
ratio (mapv #(max % 0.01) ratio)]
211211
(-> db
212212
(assoc :pivot-point pivot-point)
213-
(element.h/scale ratio pivot-point recursive))))
213+
(element.h/scale ratio pivot-point in-place recursive))))
214214

215215
(mx/defn select-element
216216
[db, multiple :- boolean?]
@@ -222,13 +222,14 @@
222222

223223
(defmethod hierarchy/drag :select
224224
[db e]
225-
(let [delta (mat/sub (:adjusted-pointer-pos db) (:adjusted-pointer-offset db))
226-
state (:state db)
225+
(let [state (:state db)
227226
ctrl? (pointer/ctrl? e)
228-
delta (cond-> delta (and ctrl? (not= state :scale)) pointer/lock-direction)
229227
alt-key? (pointer/alt? e)
230228
ratio-locked? (or (pointer/ctrl? e) (element.h/ratio-locked? db))
231-
db (element.h/clear-ignored db)]
229+
db (element.h/clear-ignored db)
230+
delta (cond-> (app.h/pointer-delta db)
231+
(and ctrl? (not= state :scale))
232+
pointer/lock-direction)]
232233
(case state
233234
:select
234235
(-> db

0 commit comments

Comments
 (0)