Add dynamic method generation and change the API

This commit is contained in:
oakes
2014-01-06 03:46:43 -05:00
parent a848ebac32
commit a387957289
5 changed files with 47 additions and 59 deletions

View File

@@ -1,5 +1,6 @@
(ns play-clj.core (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] (:import [com.badlogic.gdx Game Gdx Input$Keys Screen]
[com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera [com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera
PerspectiveCamera Texture] PerspectiveCamera Texture]
@@ -19,7 +20,6 @@
(load "core_2d") (load "core_2d")
(load "core_global") (load "core_global")
(load "core_render") (load "core_render")
(load "core_ui")
(defn- dummy [& args]) (defn- dummy [& args])

View File

@@ -16,14 +16,6 @@
(defn draw-actor! (defn draw-actor!
[^SpriteBatch batch {:keys [^Actor actor] :as entity}] [^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)) (.draw ^Actor actor batch 1))
(defn draw-image! (defn draw-image!
@@ -33,12 +25,12 @@
(defn draw-entity! (defn draw-entity!
[^SpriteBatch batch entity] [^SpriteBatch batch entity]
(cond (cond
(isa? (type entity) Actor)
(draw-actor! batch {:actor entity})
(:actor entity) (:actor entity)
(draw-actor! batch entity) (draw-actor! batch entity)
(:image entity) (:image entity)
(draw-image! batch entity) (draw-image! batch entity)))
(isa? (type entity) Actor)
(draw-actor! batch {:actor entity})))
(defn draw! [{:keys [renderer] :as screen} entities] (defn draw! [{:keys [renderer] :as screen} entities]
(assert renderer) (assert renderer)
@@ -51,38 +43,23 @@
; textures ; textures
(defn image (defn create-image*
[val & {:keys [] :as options}] [img]
(let [^TextureRegion (cond
img (if (string? val) (string? img)
(-> ^String val Texture. TextureRegion.) (-> ^String img Texture. TextureRegion.)
(TextureRegion. ^TextureRegion val))] (isa? img TextureRegion)
(doseq [[k v] options] (TextureRegion. ^TextureRegion img)
(case k :else
: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))
img)) img))
(defn image-width (defmacro create-image
[^TextureRegion img] [img & options]
(.getRegionWidth img)) `(utils/calls! ^TextureRegion (create-image* ~img) ~@options))
(defn image-height (defmacro image
[^TextureRegion img] [img k & options]
(.getRegionHeight img)) `(utils/call! ^TextureRegion ~img ~k ~@options))
(defn split-image
([val size]
(split-image val size size))
([val width height]
(-> val ^TextureRegion image (.split width height))))
(defmacro animation (defmacro animation
[duration images & args] [duration images & args]

View File

@@ -10,6 +10,12 @@
(.glClearColor (float r) (float g) (float b) (float a)) (.glClearColor (float r) (float g) (float b) (float a))
(.glClear GL20/GL_COLOR_BUFFER_BIT)))) (.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* (defn game*
[key] [key]
(case key (case key

View File

@@ -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))

View File

@@ -26,3 +26,25 @@
(defn gdx-into-array (defn gdx-into-array
[a] [a]
(Array. true (into-array a) 1 (count 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)))