From a3879572894a1171c454e5650f74d174f535a90d Mon Sep 17 00:00:00 2001 From: oakes Date: Mon, 6 Jan 2014 03:46:43 -0500 Subject: [PATCH] Add dynamic method generation and change the API --- src/play_clj/core.clj | 4 +-- src/play_clj/core_2d.clj | 57 +++++++++++------------------------- src/play_clj/core_global.clj | 6 ++++ src/play_clj/core_ui.clj | 17 ----------- src/play_clj/utils.clj | 22 ++++++++++++++ 5 files changed, 47 insertions(+), 59 deletions(-) delete mode 100644 src/play_clj/core_ui.clj diff --git a/src/play_clj/core.clj b/src/play_clj/core.clj index 82b80e5..dd23a96 100644 --- a/src/play_clj/core.clj +++ b/src/play_clj/core.clj @@ -1,5 +1,6 @@ (ns play-clj.core - (:require [play-clj.utils :as utils]) + (:require [clojure.set :as set] + [play-clj.utils :as utils]) (:import [com.badlogic.gdx Game Gdx Input$Keys Screen] [com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera PerspectiveCamera Texture] @@ -19,7 +20,6 @@ (load "core_2d") (load "core_global") (load "core_render") -(load "core_ui") (defn- dummy [& args]) diff --git a/src/play_clj/core_2d.clj b/src/play_clj/core_2d.clj index 5062e52..97a1923 100644 --- a/src/play_clj/core_2d.clj +++ b/src/play_clj/core_2d.clj @@ -16,14 +16,6 @@ (defn draw-actor! [^SpriteBatch batch {:keys [^Actor actor] :as entity}] - (doseq [[k v] entity] - (case k - :x (.setX actor (float v)) - :y (.setY actor (float v)) - :width (.setWidth actor (float v)) - :height (.setHeight actor (float v)) - :text (.setText actor (str v)) - nil)) (.draw ^Actor actor batch 1)) (defn draw-image! @@ -33,12 +25,12 @@ (defn draw-entity! [^SpriteBatch batch entity] (cond + (isa? (type entity) Actor) + (draw-actor! batch {:actor entity}) (:actor entity) (draw-actor! batch entity) (:image entity) - (draw-image! batch entity) - (isa? (type entity) Actor) - (draw-actor! batch {:actor entity}))) + (draw-image! batch entity))) (defn draw! [{:keys [renderer] :as screen} entities] (assert renderer) @@ -51,38 +43,23 @@ ; textures -(defn image - [val & {:keys [] :as options}] - (let [^TextureRegion - img (if (string? val) - (-> ^String val Texture. TextureRegion.) - (TextureRegion. ^TextureRegion val))] - (doseq [[k v] options] - (case k - :x (.setRegionX img v) - :y (.setRegionY img v) - :width (.setRegionWidth img v) - :height (.setRegionHeight img v) - :region (.setRegion img - ^long (nth v 0) ^long (nth v 1) - ^long (nth v 2) ^long (nth v 3)) - :flip (.flip img (nth v 0) (nth v 1)) - nil)) +(defn create-image* + [img] + (cond + (string? img) + (-> ^String img Texture. TextureRegion.) + (isa? img TextureRegion) + (TextureRegion. ^TextureRegion img) + :else img)) -(defn image-width - [^TextureRegion img] - (.getRegionWidth img)) +(defmacro create-image + [img & options] + `(utils/calls! ^TextureRegion (create-image* ~img) ~@options)) -(defn image-height - [^TextureRegion img] - (.getRegionHeight img)) - -(defn split-image - ([val size] - (split-image val size size)) - ([val width height] - (-> val ^TextureRegion image (.split width height)))) +(defmacro image + [img k & options] + `(utils/call! ^TextureRegion ~img ~k ~@options)) (defmacro animation [duration images & args] diff --git a/src/play_clj/core_global.clj b/src/play_clj/core_global.clj index 8f8f4c6..49d8b28 100644 --- a/src/play_clj/core_global.clj +++ b/src/play_clj/core_global.clj @@ -10,6 +10,12 @@ (.glClearColor (float r) (float g) (float b) (float a)) (.glClear GL20/GL_COLOR_BUFFER_BIT)))) +(defmacro color + [& args] + `~(if (keyword? (first args)) + `(Color. ^Color (utils/gdx-static-field :graphics :Color ~(first args))) + `(Color. ~@args))) + (defn game* [key] (case key diff --git a/src/play_clj/core_ui.clj b/src/play_clj/core_ui.clj deleted file mode 100644 index 701d9d3..0000000 --- a/src/play_clj/core_ui.clj +++ /dev/null @@ -1,17 +0,0 @@ -(in-ns 'play-clj.core) - -(defmacro color - [& args] - `~(if (keyword? (first args)) - `(Color. ^Color (utils/gdx-static-field :graphics :Color ~(first args))) - `(Color. ~@args))) - -(defn create-actor - [actor] - {:actor actor :x 0 :y 0}) - -(defn label - [text color] - (->> (Label$LabelStyle. (BitmapFont.) color) - (Label. text) - create-actor)) diff --git a/src/play_clj/utils.clj b/src/play_clj/utils.clj index 803d439..ae90d19 100644 --- a/src/play_clj/utils.clj +++ b/src/play_clj/utils.clj @@ -26,3 +26,25 @@ (defn gdx-into-array [a] (Array. true (into-array a) 1 (count a))) + +(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 ~@(flatten args))) + +(defn calls!* + [[k v]] + (flatten (list (key->method k) v))) + +(defmacro calls! + [obj & {:keys [] :as args}] + `(doto ~obj ~@(map calls!* args)))