From c4e496f72d0486390188c3a8e7a2aea017f4228e Mon Sep 17 00:00:00 2001 From: oakes Date: Fri, 10 Jan 2014 02:37:42 -0500 Subject: [PATCH] Use reify instead of proxy, add input function, and add global interop macros --- src/play_clj/core.clj | 22 ++++++++++++---------- src/play_clj/core_global.clj | 27 ++++++++++++++++++++++++--- src/play_clj/core_interop.clj | 30 ++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 13 deletions(-) diff --git a/src/play_clj/core.clj b/src/play_clj/core.clj index 820a9b0..8c476bc 100644 --- a/src/play_clj/core.clj +++ b/src/play_clj/core.clj @@ -1,7 +1,8 @@ (ns play-clj.core (:require [clojure.set :as set] [play-clj.utils :as utils]) - (:import [com.badlogic.gdx Game Gdx Screen] + (:import [com.badlogic.gdx Application Audio Files Game Gdx Graphics Input + InputProcessor Net Screen] [com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera PerspectiveCamera Texture] [com.badlogic.gdx.graphics.g2d Animation BitmapFont SpriteBatch @@ -28,6 +29,8 @@ [obj] {:type :actor :object obj}) +(defn- dummy [& args]) + (load "core_2d") (load "core_deprecated") (load "core_global") @@ -35,8 +38,6 @@ (load "core_render") (load "core_ui") -(defn- dummy [& args]) - (defn defscreen* [{:keys [on-show on-render on-hide on-pause on-resize on-resume] :or {on-show dummy on-render dummy on-hide dummy @@ -95,13 +96,14 @@ (let [run-fn! (fn [k & args] (doseq [screen screens] (apply (get screen k) args)))] - (.setScreen game (proxy [Screen] [] - (show [] (run-fn! :show)) - (render [delta-time] (run-fn! :render delta-time)) - (hide [] (run-fn! :hide)) - (pause [] (run-fn! :pause)) - (resize [w h] (run-fn! :resize)) - (resume [] (run-fn! :resume)))))) + (.setScreen game (reify Screen + (show [this] (run-fn! :show)) + (render [this delta-time] (run-fn! :render delta-time)) + (hide [this] (run-fn! :hide)) + (pause [this] (run-fn! :pause)) + (resize [this w h] (run-fn! :resize)) + (resume [this] (run-fn! :resume)) + (dispose [this]))))) (defn update! [{:keys [update-fn!]} & {:keys [] :as args}] diff --git a/src/play_clj/core_global.clj b/src/play_clj/core_global.clj index 5816079..a0ae3fa 100644 --- a/src/play_clj/core_global.clj +++ b/src/play_clj/core_global.clj @@ -34,8 +34,29 @@ :y (.getY (Gdx/input)) nil)) +(defmacro input-key + [key] + `~(symbol + (str utils/gdx-package ".Input$Keys/" (utils/key->static-field key)))) + (defmacro is-pressed? [key] - `(.isKeyPressed (Gdx/input) - ~(symbol (str utils/gdx-package ".Input$Keys/" - (utils/key->static-field key))))) + `(.isKeyPressed (Gdx/input) (input-key ~key))) + +(defn input + [& {:keys [key-down key-typed key-up mouse-moved + scrolled touch-down touch-dragged touch-up] + :or {key-down dummy key-typed dummy key-up dummy mouse-moved dummy + scrolled dummy touch-down dummy touch-dragged dummy touch-up dummy}}] + (reify InputProcessor + (keyDown [this keycode] (key-down keycode)) + (keyTyped [this character] (key-typed character)) + (keyUp [this keycode] (key-up keycode)) + (mouseMoved [this screen-x screen-y] (mouse-moved screen-x screen-y)) + (scrolled [this amount] (scrolled amount)) + (touchDown [this screen-x screen-y pointer button] + (touch-down screen-x screen-y pointer button)) + (touchDragged [this screen-x screen-y pointer] + (touch-dragged screen-x screen-y pointer)) + (touchUp [this screen-x screen-y pointer button] + (touch-up screen-x screen-y pointer button)))) diff --git a/src/play_clj/core_interop.clj b/src/play_clj/core_interop.clj index 2f4c2ef..a707d38 100644 --- a/src/play_clj/core_interop.clj +++ b/src/play_clj/core_interop.clj @@ -36,6 +36,36 @@ [{:keys [camera]} k & options] `(utils/call! ^PerspectiveCamera ~camera ~k ~@options)) +; global + +(defmacro app! + [k & options] + `(utils/call! ^Application (Gdx/app) ~k ~@options)) + +(defmacro audio! + [k & options] + `(utils/call! ^Audio (Gdx/audio) ~k ~@options)) + +(defmacro files! + [k & options] + `(utils/call! ^Files (Gdx/files) ~k ~@options)) + +(defmacro gl! + [k & options] + `(utils/call! ^GL20 (Gdx/gl20) ~k ~@options)) + +(defmacro graphics! + [k & options] + `(utils/call! ^Graphics (Gdx/graphics) ~k ~@options)) + +(defmacro input! + [k & options] + `(utils/call! ^Input (Gdx/input) ~k ~@options)) + +(defmacro net! + [k & options] + `(utils/call! ^Net (Gdx/net) ~k ~@options)) + ; ui (defmacro check-box!