From 176c9d7ede5678e80d28b167fd61226c7b6b18c1 Mon Sep 17 00:00:00 2001 From: Remington Covert Date: Wed, 29 Apr 2015 18:04:03 -0700 Subject: [PATCH] made inventory ux much better by fading in. --- .../src-common/advent/screens/inventory.clj | 81 ++++++++++++------- 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/desktop/src-common/advent/screens/inventory.clj b/desktop/src-common/advent/screens/inventory.clj index 255d0fc3..f3de9a24 100644 --- a/desktop/src-common/advent/screens/inventory.clj +++ b/desktop/src-common/advent/screens/inventory.clj @@ -6,6 +6,7 @@ [clojure.pprint] [advent.pathfind] [advent.zone :as zone] + [advent.tween :as tween] [advent.utils :as utils]) (:import [com.badlogic.gdx.graphics Pixmap Pixmap$Filter Texture Texture$TextureFilter] [com.badlogic.gdx.graphics.g2d TextureRegion] @@ -14,11 +15,12 @@ InputMultiplexer InputProcessor Net Preferences Screen])) -(defn close [entities] +(defn close [screen entities] (screen! @(resolve 'advent.screens.scene/scene) :on-reactivate) (-> entities - (assoc :shown? false) - (assoc :start-showing? false))) + (assoc :start-showing? false) + (assoc-in [:tweens :fade-out] (tween/tween :fade-out screen [:opacity] 1.0 0.0 0.2 :ease tween/ease-out-cubic + :finish #(assoc % :shown? false))))) (defscreen inventory-screen @@ -26,9 +28,9 @@ (fn [screen entities] (utils/setup-viewport screen 1280 960) - (let [highlighted-text (assoc (label "Hello" (style :label (utils/get-font "ego/font.fnt") (color :white))) :x 0 :y 850 :width 1280)] + (let [highlighted-text (assoc (label "Hello" (style :label (utils/get-font "ego/font.fnt") (color :white))) :x 0 :y 850 :width 1280 )] (label! highlighted-text :set-alignment Align/center) - {:overlay (assoc (texture "inventory-overlay.png" ) :x 0 :y 0 :scale-x 4 :scale-y 4 :origin-x 0 :origin-y 0) + {:overlay (assoc (texture "inventory-overlay.png" ) :x 0 :y 0 :scale-x 4 :scale-y 4 :origin-x 0 :origin-y 0 :opacity 0.0) :fade (assoc (texture "black.png") :scale-x 80 :scale-y 80 @@ -40,6 +42,8 @@ :shown? false :start-showing? false :highlighted-item nil + :opacity 0.0 + :tweens {} :highlighted-text highlighted-text})) :on-render @@ -48,9 +52,18 @@ (-> entities (assoc :start-showing? false) (assoc :shown? true)) - entities)] + entities) + entities (utils/apply-tweens screen entities (:tweens entities)) + opacity (get-in entities [:opacity]) + entities (-> entities + (assoc-in [:overlay :opacity] opacity) + (assoc-in [:fade :opacity] (* 0.3 opacity)) + (assoc-in [:highlighted-text :opacity] opacity) + (update-in [:items] (fn [i] + (map #(assoc % :opacity opacity) i))))] (when (:shown? entities) + (doto (:highlighted-text entities) (label! :set-color (color 1 1 1 opacity))) (render! screen [(:fade entities) (:overlay entities)]) (render! screen (:items entities)) (if-let [item (:highlighted-item entities)] @@ -59,28 +72,34 @@ (render! screen [(:highlighted-text entities)])) entities)) - :show-screen (fn [{items :items} [entities]] - (assoc entities :start-showing? true - :items (for [[item index] (map vector items (range)) - :let [row (int (/ index 8)) - column (mod index 8) - base-x (* 79 4) - base-y (* 180 4) - x (+ base-x (* column (* 24 4))) - y (- base-y (* row (* 24 4))) - item-width 16 - offset-x (+ x (/ item-width 2)) - offset-y (+ y (/ item-width 2)) - padding (/ item-width 2) - padding (* 4 padding)]] - (assoc (texture (aget (:all-items entities) 0 (.indexOf utils/+all-cursors+ (:cursor item)))) - :x x :y y - :scale-x 4 - :scale-y 4 - :origin-x 0 - :origin-y 0 - :item item - :box (zone/box (- offset-x padding) (- offset-y padding) (+ offset-x item-width padding) (+ offset-y item-width padding)))))) + :show-screen (fn [{items :items :as screen} [entities]] + (if (:shown? entities) + entities + + (-> entities + (assoc :start-showing? true + :opacity 0.0 + :items (for [[item index] (map vector items (range)) + :let [row (int (/ index 8)) + column (mod index 8) + base-x (* 79 4) + base-y (* 180 4) + x (+ base-x (* column (* 24 4))) + y (- base-y (* row (* 24 4))) + item-width 16 + offset-x (+ x (/ item-width 2)) + offset-y (+ y (/ item-width 2)) + padding (/ item-width 2) + padding (* 4 padding)]] + (assoc (texture (aget (:all-items entities) 0 (.indexOf utils/+all-cursors+ (:cursor item)))) + :x x :y y + :scale-x 4 + :scale-y 4 + :origin-x 0 + :origin-y 0 + :item item + :box (zone/box (- offset-x padding) (- offset-y padding) (+ offset-x item-width padding padding) (+ offset-y item-width padding padding))))) + (assoc-in [:tweens :fade-in] (tween/tween :fade-in screen [:opacity] 0.0 1.0 0.2 :ease tween/ease-out-cubic))))) :on-mouse-moved (fn [screen [entities]] (let [[x y] (utils/unproject screen) @@ -90,7 +109,7 @@ (assoc entities :highlighted-item nil)))) :on-touch-up (fn [screen [entities]] - (when (and (:shown? entities) (= (button-code :left) (:button screen))) + (when (and (:shown? entities) (= (button-code :left) (:button screen)) (= 1.0 (:opacity entities))) (let [{:keys [highlighted-item]} entities room-entities (-> @(resolve 'advent.screens.scene/scene) :entities @@ -102,8 +121,8 @@ (screen! @(resolve 'advent.screens.scene/scene) :on-chose-item :item highlighted-item) (when-let [interaction-script ((or (:scripts highlighted-item) (constantly nil)) (:value current-cursor))] (interaction-script room-entities) - (close entities))) - (close entities))))) + (close screen entities))) + (close screen entities))))) :on-resize (fn [screen entities] (.update (:viewport screen) (:width screen) (:height screen) true)))