Skip to content

Commit

Permalink
Rewrite handles with new :replace syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Jul 12, 2020
1 parent aa0e32a commit 9bd6ff6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
20 changes: 9 additions & 11 deletions public/lib/math.glisp
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,9 @@
{:id :scale :type "arrow" :pos p :angle (vec2/angle v)}
{:id :end :type "point" :pos v}])
:drag (fn [{:id id :pos p :params [v sc]}]
(let []
(case id
:scale
[v (/ (vec2/dot p v) (vec2/sqr-len v))]
:end
[p sc])))}
(case id
:scale {:replace [1 (/ (vec2/dot p v) (vec2/sqr-len v))]}
:end {:replace [0 p]}))}
:returns {:type "vec2"}}
[v s]
[(* s (.x v))
Expand Down Expand Up @@ -353,11 +350,12 @@
(let [ab (vec2/- b a)]
(case id
:lerp
[a b
(- 1 (/ (vec2/dot (vec2/- p a) ab)
(vec2/sqr-len ab)))]
:a [p b t]
:b [a p t])))}}
{:replace
[2
(- 1 (/ (vec2/dot (vec2/- p a) ab)
(vec2/sqr-len ab)))]}
:a {:replace [0 p]}
:b {:replace [1 p]})))}}
[a b t]
[(lerp (.x a) (.x b) t)
(lerp (.y a) (.y b) t)])
Expand Down
22 changes: 11 additions & 11 deletions public/lib/path.glisp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
:drag (fn [{:id id :pos p
:params [c r]}]
(case id
:center [p r]
:radius [c (vec2/dist c p)]))}}
:center {:replace [0 p]}
:radius {:replace [1 (vec2/dist c p)]}))}}
(let [K (/ (* 4 (dec (sqrt 2))) 3)]
(fn [[x y] r]
(let [k (* r K)
Expand Down Expand Up @@ -108,8 +108,8 @@
:params [from to]}]
(case id
:path [(vec2/+ from dp) (vec2/+ to dp)]
:from [p to]
:to [from p]))}}
:from {:replace [0 p]}
:to {:replace [1 p]}))}}
[from to]
[:path :M from :L to])

Expand Down Expand Up @@ -143,20 +143,20 @@
:drag (fn [{:id id :pos p :prev-pos pp
:params [center r start end]}]
(case id
:radius [center (vec2/dist center p) start end]
:center [p r start end]
:center {:replace [0 p]}
:radius {:replace [1 (vec2/dist center p)]}
:start (let [start (calc-dragged-rotation
:center center
:pos p
:prev-pos pp
:angle start)]
[center r start end])
{:replace [2 start]})
:end (let [end (calc-dragged-rotation
:center center
:pos p
:prev-pos pp
:angle end)]
[center r start end])))}}
{:replace [3 end]})))}}
path/arc)
(defalias arc path/arc)

Expand Down Expand Up @@ -240,11 +240,11 @@
{:id :sides :type "dia" :pos (vec2/+ center (vec2/dir (/ TWO_PI sides) (+ radius 40)))}])
:drag (fn [{:id id :pos p :params [center radius sides]}]
(case id
:center [p radius sides]
:radius [center (abs (- (.x p) (.x center))) sides]
:center {:replace [0 p]}
:radius {:replace [1 (abs (- (.x p) (.x center)))]}
:sides (let [angle (vec2/angle (vec2/- p center))
sides (round (/ TWO_PI (clamp angle (/ TWO_PI 100) (/ TWO_PI 3))))]
[center radius sides])))}}
{:replace [2 sides]})))}}
[center radius sides]
(let [angles (column 0 sides (/ TWO_PI sides))
vertices (map #(vec2/+ center (vec2/dir % radius)) angles)]
Expand Down

0 comments on commit 9bd6ff6

Please sign in to comment.