(ns advent.screens.dialogue (:require [play-clj.core :refer :all] [play-clj.ui :refer :all] [play-clj.utils :refer :all] [play-clj.g2d :refer :all] [clojure.pprint] [clojure.set :as set] [advent.pathfind] [advent.utils :as utils] [clojure.core.async :refer [put! ! >!! chan go thread take! alts!!]] #_[advent.screens.scene :as scene]) (:import [com.badlogic.gdx.graphics Pixmap Pixmap$Filter Texture Texture$TextureFilter] [com.badlogic.gdx.graphics.g2d TextureRegion] [com.badlogic.gdx.scenes.scene2d.utils Align] [com.badlogic.gdx Screen])) (defn ensure-on-screen [talk] (let [margin-width (* 0.05 (game :width)) minimum-x margin-width maximum-x (- (game :width) margin-width) label-width (label! talk :get-width) label-right (+ (:x talk) label-width)] (cond (> label-right maximum-x) (assoc talk :x (- maximum-x label-width)) (< (:x talk) minimum-x) (assoc talk :x minimum-x) :else talk))) (defscreen talking-screen :on-show (fn [screen entities] (utils/setup-viewport screen 1280 960) {}) :on-render (fn [screen [entities]] (render! screen (vals entities)) entities) :on-talk (fn [{:keys [create-talk target-id color text x y scale]} [entities]] (let [font (bitmap-font "ego/font.fnt" ) p (nine-patch {:region (:object (texture "talk-bg-2.png")) :left 9 :top 9 :right 9 :bottom 9}) _ (nine-patch! p :set-padding 25 25 5 15) bg (drawable :nine-patch (:object p)) _ (bitmap-font! font :set-markup-enabled true) tr (bitmap-font! font :get-region) scale (or (min (max scale 0.75) 1) 1) tx (.getTexture tr) _ (texture! tx :set-filter Texture$TextureFilter/Linear Texture$TextureFilter/Linear) style (style :label font color) #__ #_(set! (.background style) bg) talk (assoc (label text style) :y (* 4 y) )] (label! talk :set-font-scale scale) (label! talk :set-alignment Align/center) (assoc entities target-id (-> talk (assoc :x (- (* 4 x) (/ (label! talk :get-width) 2))) ensure-on-screen )))) :stop-talk (fn [{:keys [target-id] } [entities]] (dissoc entities target-id)) :on-resize (fn [{:keys [viewport width height]} entities] (.update viewport width height))) (def choice-height 30) (defn get-color [e mouse-pos] (if (utils/intersects? e mouse-pos) (color :yellow) (color 0.6 1.0 1.0 1.0) )) (defn style-label [e font mouse-pos] (label! e :set-style (style :label font (get-color e mouse-pos))) e) (defscreen choice-screen :on-show (fn [screen entities] (utils/setup-viewport screen 1280 960) (let [font (bitmap-font "ego/font.fnt" ) tr (bitmap-font! font :get-region) scale 1 tx (.getTexture tr) _ (texture! tx :set-filter Texture$TextureFilter/Linear Texture$TextureFilter/Linear)] {:state {:object nil :callback nil :choices [] :last-pos [0 0] :font font :np (assoc (nine-patch {:region (:object (texture "talk-bg-2.png")) :left 9 :top 9 :right 9 :bottom 9}) :x 5 :y 5 :width 1270)}})) :on-render (fn [screen [entities]] (when (seq (get-in entities [:state :choices])) (render! screen [(get-in entities [:state :np])]) (render! screen (vals entities))) entities) :on-present-choices (fn [{:keys [choices callback]} [entities]] (let [choice-count (count choices) font (get-in entities [:state :font])] (-> entities (into (for [[[text] i] (map vector choices (range)) :let [e (label text (style :label font (color 0.6 1.0 1.0 1.0))) e (assoc e :height 30 :x 30 :y (+ 30 (* choice-height (- choice-count i 1))) :index i) e (style-label e font (get-in entities [:state :last-pos]))]] [i e])) (assoc-in [:state :choices] choices) (assoc-in [:state :callback] callback) (assoc-in [:state :np :height] (* 30 (inc choice-count)))))) :on-touch-up (fn [screen [entities]] (let [[x y] (utils/unproject screen)] (when (seq (get-in entities [:state :choices])) (when-let [choice (first (filter #(utils/intersects? % [x y]) (vals entities)))] ((get-in entities [:state :callback]) (:index choice)) (-> entities (assoc-in [:state :callback] nil) (assoc-in [:state :choices] []) (select-keys [:state])))))) :on-mouse-moved (fn [screen [entities]] (let [[x y] (utils/unproject screen) entities (assoc-in entities [:state :last-pos] [x y]) choice-count (dec (count entities))] (doseq [e (vals entities) :when (:object e)] (style-label e (get-in entities [:state :font]) [x y])) entities)) :on-resize (fn [{:keys [width height viewport]} entities] (.update viewport width height)))