Change overlay position algorithm and some refactor

This commit is contained in:
Andrés Moya 2021-09-27 16:13:50 +02:00
parent a189dc8243
commit 4df96b03eb
8 changed files with 225 additions and 187 deletions

View file

@ -113,7 +113,7 @@
;; -- Helpers for interaction
(declare calc-overlay-position)
(declare calc-overlay-pos-initial)
(defn set-event-type
[interaction event-type shape]
@ -135,7 +135,7 @@
(defn set-action-type
[interaction action-type shape objects]
[interaction action-type]
(us/verify ::interaction interaction)
(us/verify ::action-type action-type)
(if (= (:action-type interaction) action-type)
@ -148,19 +148,10 @@
:destination (get interaction :destination))
(:open-overlay :toggle-overlay)
(let [destination (get interaction :destination)
overlay-pos-type (get interaction :overlay-pos-type :center)
overlay-position (get interaction
:overlay-position
(calc-overlay-position
destination
interaction
shape
objects
overlay-pos-type))]
(let [overlay-pos-type (get interaction :overlay-pos-type :center)
overlay-position (get interaction :overlay-position (gpt/point 0 0))]
(assoc interaction
:action-type action-type
:destination destination
:overlay-pos-type overlay-pos-type
:overlay-position overlay-position))
@ -178,20 +169,32 @@
:action-type action-type
:url (get interaction :url "")))))
(defn has-delay
[interaction]
(= (:event-type interaction) :after-delay))
(defn set-delay
[interaction delay]
(us/verify ::interaction interaction)
(us/verify ::delay delay)
(assert (= (:event-type interaction) :after-delay))
(assert (has-delay interaction))
(assoc interaction :delay delay))
(defn has-destination
[interaction]
(#{:navigate :open-overlay :toggle-overlay :close-overlay}
(:action-type interaction)))
(defn destination?
[interaction]
(and (has-destination interaction)
(some? (:destination interaction))))
(defn set-destination
[interaction destination shape objects]
[interaction destination]
(us/verify ::interaction interaction)
(us/verify ::destination destination)
(assert (or (nil? destination)
(some? (get objects destination))))
(assert (#{:navigate :open-overlay :toggle-overlay :close-overlay} (:action-type interaction)))
(assert (has-destination interaction))
(cond-> interaction
:always
(assoc :destination destination)
@ -199,50 +202,53 @@
(or (= (:action-type interaction) :open-overlay)
(= (:action-type interaction) :toggle-overlay))
(assoc :overlay-pos-type :center
:overlay-position (calc-overlay-position destination
interaction
shape
objects
:center))))
:overlay-position (gpt/point 0 0))))
(defn has-url
[interaction]
(= (:action-type interaction) :open-url))
(defn set-url
[interaction url]
(us/verify ::interaction interaction)
(us/verify ::url url)
(assert (= (:action-type interaction) :open-url))
(assert (has-url interaction))
(assoc interaction :url url))
(defn has-overlay-opts
[interaction]
(#{:open-overlay :toggle-overlay} (:action-type interaction)))
(defn set-overlay-pos-type
[interaction overlay-pos-type shape objects]
(us/verify ::interaction interaction)
(us/verify ::overlay-pos-type overlay-pos-type)
(assert (#{:open-overlay :toggle-overlay} (:action-type interaction)))
(assert (has-overlay-opts interaction))
(assoc interaction
:overlay-pos-type overlay-pos-type
:overlay-position (calc-overlay-position (:destination interaction)
interaction
shape
objects
overlay-pos-type)))
:overlay-position (calc-overlay-pos-initial (:destination interaction)
shape
objects
overlay-pos-type)))
(defn toggle-overlay-pos-type
[interaction overlay-pos-type shape objects]
(us/verify ::interaction interaction)
(us/verify ::overlay-pos-type overlay-pos-type)
(assert (#{:open-overlay :toggle-overlay} (:action-type interaction)))
(assert (has-overlay-opts interaction))
(let [new-pos-type (if (= (:overlay-pos-type interaction) overlay-pos-type)
:manual
overlay-pos-type)]
(assoc interaction
:overlay-pos-type new-pos-type
:overlay-position (calc-overlay-position (:destination interaction)
interaction
shape
objects
new-pos-type))))
:overlay-position (calc-overlay-pos-initial (:destination interaction)
shape
objects
new-pos-type))))
(defn set-overlay-position
[interaction overlay-position]
(us/verify ::interaction interaction)
(us/verify ::overlay-position overlay-position)
(assert (#{:open-overlay :toggle-overlay} (:action-type interaction)))
(assert (has-overlay-opts interaction))
(assoc interaction
:overlay-pos-type :manual
:overlay-position overlay-position))
@ -251,58 +257,67 @@
[interaction close-click-outside]
(us/verify ::interaction interaction)
(us/verify ::us/boolean close-click-outside)
(assert (#{:open-overlay :toggle-overlay} (:action-type interaction)))
(assert (has-overlay-opts interaction))
(assoc interaction :close-click-outside close-click-outside))
(defn set-background-overlay
[interaction background-overlay]
(us/verify ::interaction interaction)
(us/verify ::us/boolean background-overlay)
(assert (#{:open-overlay :toggle-overlay} (:action-type interaction)))
(assert (has-overlay-opts interaction))
(assoc interaction :background-overlay background-overlay))
(defn- calc-overlay-position
[destination interaction shape objects overlay-pos-type]
(if (nil? destination)
(gpt/point 0 0)
(defn- calc-overlay-pos-initial
[destination shape objects overlay-pos-type]
(if (= overlay-pos-type :manual)
(let [dest-frame (get objects destination)
overlay-size (:selrect dest-frame)
orig-frame (if (= (:type shape) :frame)
shape
(get objects (:frame-id shape)))
frame-size (:selrect orig-frame)]
(case overlay-pos-type
(gpt/point (/ (- (:width frame-size) (:width overlay-size)) 2)
(/ (- (:height frame-size) (:height overlay-size)) 2)))
(gpt/point 0 0)))
(defn calc-overlay-position
[interaction base-frame dest-frame frame-offset]
(us/verify ::interaction interaction)
(assert (has-overlay-opts interaction))
(if (nil? dest-frame)
(gpt/point 0 0)
(let [overlay-size (:selrect dest-frame)
base-frame-size (:selrect base-frame)]
(case (:overlay-pos-type interaction)
:center
(gpt/point (/ (- (:width frame-size) (:width overlay-size)) 2)
(/ (- (:height frame-size) (:height overlay-size)) 2))
(gpt/point (/ (- (:width base-frame-size) (:width overlay-size)) 2)
(/ (- (:height base-frame-size) (:height overlay-size)) 2))
:top-left
(gpt/point 0 0)
:top-right
(gpt/point (- (:width frame-size) (:width overlay-size))
(gpt/point (- (:width base-frame-size) (:width overlay-size))
0)
:top-center
(gpt/point (/ (- (:width frame-size) (:width overlay-size)) 2)
(gpt/point (/ (- (:width base-frame-size) (:width overlay-size)) 2)
0)
:bottom-left
(gpt/point 0
(- (:height frame-size) (:height overlay-size)))
(- (:height base-frame-size) (:height overlay-size)))
:bottom-right
(gpt/point (- (:width frame-size) (:width overlay-size))
(- (:height frame-size) (:height overlay-size)))
(gpt/point (- (:width base-frame-size) (:width overlay-size))
(- (:height base-frame-size) (:height overlay-size)))
:bottom-center
(gpt/point (/ (- (:width frame-size) (:width overlay-size)) 2)
(- (:height frame-size) (:height overlay-size)))
(gpt/point (/ (- (:width base-frame-size) (:width overlay-size)) 2)
(- (:height base-frame-size) (:height overlay-size)))
:manual
(:overlay-position interaction)))))
(gpt/add (:overlay-position interaction) frame-offset)))))
;; -- Helpers for interactions