From f631626060a7f85298049926e010634859da77d1 Mon Sep 17 00:00:00 2001 From: oakes Date: Thu, 9 Jan 2014 22:46:57 -0500 Subject: [PATCH] Make is-pressed? work with all keys --- src/play_clj/core.clj | 2 +- src/play_clj/core_global.clj | 19 ++++---------- src/play_clj/core_ui.clj | 2 +- src/play_clj/utils.clj | 50 +++++++++++++++++++----------------- 4 files changed, 34 insertions(+), 39 deletions(-) diff --git a/src/play_clj/core.clj b/src/play_clj/core.clj index f17588f..820a9b0 100644 --- a/src/play_clj/core.clj +++ b/src/play_clj/core.clj @@ -1,7 +1,7 @@ (ns play-clj.core (:require [clojure.set :as set] [play-clj.utils :as utils]) - (:import [com.badlogic.gdx Game Gdx Input$Keys Screen] + (:import [com.badlogic.gdx Game Gdx Screen] [com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera PerspectiveCamera Texture] [com.badlogic.gdx.graphics.g2d Animation BitmapFont SpriteBatch diff --git a/src/play_clj/core_global.clj b/src/play_clj/core_global.clj index 488a054..5816079 100644 --- a/src/play_clj/core_global.clj +++ b/src/play_clj/core_global.clj @@ -20,6 +20,8 @@ [& options] `(BitmapFont. ~@options)) +; input/output + (defn game [key] (case key @@ -32,19 +34,8 @@ :y (.getY (Gdx/input)) nil)) -; input - -(defn resolve-key - [key] - (if (keyword? key) - (case key - :up Input$Keys/DPAD_UP - :down Input$Keys/DPAD_DOWN - :left Input$Keys/DPAD_LEFT - :right Input$Keys/DPAD_RIGHT - nil) - key)) - (defmacro is-pressed? [key] - `(.isKeyPressed (Gdx/input) ~(resolve-key key))) + `(.isKeyPressed (Gdx/input) + ~(symbol (str utils/gdx-package ".Input$Keys/" + (utils/key->static-field key))))) diff --git a/src/play_clj/core_ui.clj b/src/play_clj/core_ui.clj index f6c7aaa..fb2fcd8 100644 --- a/src/play_clj/core_ui.clj +++ b/src/play_clj/core_ui.clj @@ -2,7 +2,7 @@ (defmacro style [type & options] - `(~(symbol (str utils/gdx-package "scenes.scene2d.ui." + `(~(symbol (str utils/gdx-package ".scenes.scene2d.ui." (utils/key->class type) "$" (utils/key->class type) "Style.")) ~@options)) diff --git a/src/play_clj/utils.clj b/src/play_clj/utils.clj index ec095ec..7ae8987 100644 --- a/src/play_clj/utils.clj +++ b/src/play_clj/utils.clj @@ -2,7 +2,7 @@ (:require [clojure.string :as s]) (:import [com.badlogic.gdx.utils Array])) -(def ^:const gdx-package "com.badlogic.gdx.") +(def ^:const gdx-package "com.badlogic.gdx") (defn- split-key [key] @@ -10,14 +10,35 @@ (defn- join-keys [keys] - (->> keys (map name) (s/join ".") (str gdx-package))) + (->> keys (map name) (s/join ".") (str gdx-package "."))) + +(defn key->static-field + [key] + (->> (split-key key) + (map s/upper-case) + (s/join "_") + symbol)) + +(defn key->class + [key] + (->> (split-key key) + (map s/capitalize) + (s/join "") + symbol)) + +(defn key->method + [key] + (let [parts (split-key key)] + (->> (rest parts) + (map s/capitalize) + (cons (first parts)) + (s/join "") + (str ".") + symbol))) (defn gdx-static-field* [args] - (->> (last args) - split-key - (map s/upper-case) - (s/join "_") + (->> (key->static-field (last args)) (str (join-keys (butlast args)) "/") symbol)) @@ -29,23 +50,6 @@ [a] (Array. true (into-array a) 1 (count a))) -(defn key->class - [k] - (->> (split-key k) - (map s/capitalize) - (s/join "") - symbol)) - -(defn key->method - [k] - (let [parts (split-key k)] - (->> (rest parts) - (map s/capitalize) - (cons (first parts)) - (s/join "") - (str ".") - symbol))) - (defmacro call! [obj k & args] `(~(key->method k) ~obj ~@args))