zooming and panning!

This commit is contained in:
Bryce Covert
2016-12-01 09:10:13 -08:00
parent 9d12bc549c
commit 3370756f1c
3 changed files with 92 additions and 32 deletions

View File

@@ -124,15 +124,12 @@
~@forms
(change-script-state ~entities false))))))))
(defn bound-to-camera [x length zoom]
(min (- length (* length 0.5 zoom ))
(max (* length 0.5 zoom )
x)))
(defn pan-to [screen entities x y scale-fn & [ease duration]]
(let [ease (or ease tween/ease-in-out-quadratic)
duration (or duration 3.0)
target-zoom (min 0.95 (max 0.75 (scale-fn [(max 0 (min 319 x)) (max 0 (min 240 y))])))
target-zoom (min utils/min-zoom (max utils/max-zoom (scale-fn [(max 0 (min 319 x)) (max 0 (min 240 y))])))
current-zoom (get-in entities [:cam :zoom] 1.0)
;; don't zoom if it's a subtle difference
@@ -140,13 +137,15 @@
0.07)
current-zoom
target-zoom)
target-x (bound-to-camera x 320 target-zoom)
target-y (bound-to-camera y 240 target-zoom)]
target-x (utils/bound-to-camera x 320 target-zoom)
target-y (utils/bound-to-camera y 240 target-zoom)]
(if (and (or (not= target-x (get-in entities [:cam :x]))
(not= target-y (get-in entities [:cam :y]))
(not= target-zoom (get-in entities [:cam :zoom])))
(not (get-in entities [:cam :paused?])))
(-> entities
(assoc-in [:cam :ideal-x] x)
(assoc-in [:cam :ideal-y] y)
(assoc-in [:tweens :cam-zoom]
(tween/tween :cam-zoom screen
[:cam :zoom]
@@ -715,7 +714,7 @@
(update-in [:tweens] dissoc :cam-zoom :cam-x :cam-y)
(assoc-in [:cam :x] 160)
(assoc-in [:cam :y] 120)
(assoc-in [:cam :zoom] 0.95))
(assoc-in [:cam :zoom] utils/max-zoom))
e)
(if music-changed?

View File

@@ -187,6 +187,8 @@ void main ()
:else l)]
l))
(defn get-script-selector [entities]
(or (get-selected-inventory-item) :main))
@@ -275,6 +277,27 @@ void main ()
((:get-script default-interaction) (get-script-selector entities) [x y])) entities))
(assoc-in entities [:cursor :down-target] nil)))
(defn handle-touch-up [{:keys [^FitViewport viewport] :as screen} entities {:keys [input-x input-y] :as options}]
(let [entities (assoc-in entities [:cursor :depressed?] false)]
(cond
(not (utils/contains-point? (.getScreenX viewport) (.getScreenY viewport)
(.getScreenWidth viewport) (.getScreenHeight viewport)
input-x input-y))
entities
(= (button-code :right)
(:button options))
(do (screen! hud :on-return-item {})
entities)
(and (get-in entities [:state :active?])
(or (not (get-in entities [:state :hud-active?]))
(get-in entities [:fg-actions :script-running?]))
(= 0.0 (get-in entities [:fade :opacity])))
(left-click screen entities options)
:else
entities)))
(defn get-ego-script [cursor [x y]]
@@ -1152,7 +1175,7 @@ void main ()
(log/info "Initializing scene.")
(let [{cam :camera :as screen} (utils/setup-viewport screen 320 240)
screen (assoc screen :total-time 0)]
(set! (. cam zoom) 0.95)
(set! (. cam zoom) utils/min-zoom)
(let [shader (ShaderProgram. ^String v-shader ^String pix-shader)
_ (log/info "shader log:" (.getLog shader))
state (get-state @utils/selected-save)
@@ -1179,7 +1202,11 @@ void main ()
rooms (add-georgia-to-all-rooms screen rooms)
entities {:rooms rooms
:step-particles (assoc (particle-effect "particles/step") :x 100 :y 100 :baseline 241)
:cam {:zoom 0.95
:cam {:zoom utils/min-zoom
:ideal-x 160
:ideal-y 120
:x 160
:y 120
:paused? false
:object nil}
@@ -1279,6 +1306,47 @@ void main ()
(when (= (key-code :escape) (:key options))
(utils/toggle-fullscreen!))
nil)
:on-pan
(fn [screen entities {:keys [input-x input-y delta-x delta-y]}]
(let [ideal-x (+ (get-in entities [:cam :x]) (- (/ delta-x 4)))
ideal-y (+ (get-in entities [:cam :y]) (/ delta-y 4))]
(-> entities
(assoc-in [:cam :ideal-x] ideal-x)
(assoc-in [:cam :ideal-y] ideal-y)
(assoc-in [:cam :x] (utils/bound-to-camera ideal-x 320 (get-in entities [:cam :zoom])) )
(assoc-in [:cam :y] (utils/bound-to-camera ideal-y 240 (get-in entities [:cam :zoom])) ))))
:pinch-stop
(fn [screen entities options]
(update-in entities [:cam] dissoc :start-zoom))
:on-pinch
(fn [screen entities {:keys [initial-pointer-1 initial-pointer-2]}]
(let [[pointer-1-x pointer-1-y] (utils/unproject screen nil [(.x initial-pointer-1) (.y initial-pointer-1)])
[pointer-2-x pointer-2-y] (utils/unproject screen nil [(.x initial-pointer-2) (.y initial-pointer-2)])]
(-> entities
(assoc-in [:cam :ideal-x] (* 0.5 (+ pointer-1-x pointer-2-x)))
(assoc-in [:cam :ideal-y] (* 0.5 (+ pointer-1-y pointer-2-y))))))
:on-zoom
(fn [{:keys [^OrthographicCamera camera ^FitViewport viewport ^Stage renderer] :as screen} entities {:keys [initial-distance distance]}]
(let [start-zoom (get-in entities [:cam :start-zoom])
zoom (get-in entities [:cam :zoom])
entities (as-> entities entities
(if (not start-zoom)
(assoc-in entities [:cam :start-zoom] zoom)
entities)
(assoc-in entities [:cam :zoom] (max utils/max-zoom (min utils/min-zoom (* (or start-zoom zoom) (/ initial-distance distance)))))
(assoc-in entities [:cam :x] (utils/bound-to-camera (get-in entities [:cam :ideal-x]) 320 (get-in entities [:cam :zoom])) )
(assoc-in entities [:cam :y] (utils/bound-to-camera (get-in entities [:cam :ideal-y]) 240 (get-in entities [:cam :zoom])) ))]
entities
))
:on-scrolled
(fn [{:keys [^OrthographicCamera camera ^FitViewport viewport ^Stage renderer] :as screen} entities options]
(update-in entities [:cam :zoom] #(* % (+ 1 (/ (double (:amount options)) 100.0 )))))
:on-render
(fn [{:keys [^OrthographicCamera camera ^FitViewport viewport ^Stage renderer] :as screen} {{:keys [last-pos ] [cursor-offset-x cursor-offset-y] :offset} :cursor :keys [tweens] :as entities} options]
@@ -1322,7 +1390,7 @@ void main ()
[last-pos-x last-pos-y] (utils/unproject screen nil last-pos)
last-pos-x (double last-pos-x)
last-pos-y (double last-pos-y)
entities (if (and (@utils/settings :camera-man? true)
#_#_entities (if (and (@utils/settings :camera-man? true)
(not (get-in entities [:cam :paused?]))
(nil? (get-in entities [:tweens :cam-x]))
(= 1 (rand-int 20)))
@@ -1426,27 +1494,13 @@ void main ()
(assoc-in [:cursor :depressed?] true)
(assoc-in [:cursor :down-target] (or (:id interacting-entity ) (:id interaction) nil)))))))
:on-touch-up (fn [{:keys [^FitViewport viewport] :as screen} entities {:keys [input-x input-y] :as options}]
(let [entities (assoc-in entities [:cursor :depressed?] false)]
(cond
(not (utils/contains-point? (.getScreenX viewport) (.getScreenY viewport)
(.getScreenWidth viewport) (.getScreenHeight viewport)
input-x input-y))
entities
:on-touch-up (fn [screen entities options]
(when-not utils/mobile?
(handle-touch-up screen entities options)))
(= (button-code :right)
(:button options))
(do (screen! hud :on-return-item {})
entities)
(and (get-in entities [:state :active?])
(or (not (get-in entities [:state :hud-active?]))
(get-in entities [:fg-actions :script-running?]))
(= 0.0 (get-in entities [:fade :opacity])))
(left-click screen entities options)
:else
entities)))
:on-tap (fn [screen entities options]
(when utils/mobile?
(handle-touch-up screen entities options)))
:on-deactivate (fn [screen entities options]
(assoc-in entities [:state :active?] false))

View File

@@ -30,8 +30,10 @@
entity)
(def ui-scale 1.0)
(def ui-scale 1.5)
(def mobile? (= 1.5 ui-scale))
(def max-zoom (if mobile? 0.5 0.75))
(def min-zoom 0.95)
(defn clear-stage [{:keys [^Stage renderer]}]
(.clear renderer))
@@ -657,3 +659,8 @@
:talk :talk-depressed
:look :look-depressed} cursor cursor)
cursor))
(defn bound-to-camera [x length zoom]
(min (- length (* length 0.5 zoom ))
(max (* length 0.5 zoom )
x)))