Use a centralized function to determine how to deal with objects used in entity macros

This commit is contained in:
oakes
2014-01-19 03:37:18 -05:00
parent a17d0ceb79
commit 6bac86a94f
5 changed files with 65 additions and 58 deletions

View File

@@ -14,12 +14,12 @@
(defmacro tiled-map! (defmacro tiled-map!
[screen k & options] [screen k & options]
`(let [^BatchTiledMapRenderer object# (or (:renderer ~screen) ~screen)] `(let [^BatchTiledMapRenderer object# (u/get-obj ~screen :renderer)]
(u/call! ^TiledMap (.getMap object#) ~k ~@options))) (u/call! ^TiledMap (.getMap object#) ~k ~@options)))
(defn tiled-map-layers (defn tiled-map-layers
[screen] [screen]
(let [^BatchTiledMapRenderer renderer (or (:renderer screen) screen) (let [^BatchTiledMapRenderer renderer (u/get-obj screen :renderer)
^MapLayers layers (-> renderer .getMap .getLayers)] ^MapLayers layers (-> renderer .getMap .getLayers)]
(for [^long i (range (.getCount layers))] (for [^long i (range (.getCount layers))]
(.get layers i)))) (.get layers i))))
@@ -67,7 +67,7 @@
(defmacro orthogonal-tiled-map! (defmacro orthogonal-tiled-map!
[screen k & options] [screen k & options]
`(let [^OrthogonalTiledMapRenderer object# (or (:renderer ~screen) ~screen)] `(let [^OrthogonalTiledMapRenderer object# (u/get-obj ~screen :renderer)]
(u/call! object# ~k ~@options))) (u/call! object# ~k ~@options)))
(defn isometric-tiled-map* (defn isometric-tiled-map*
@@ -81,7 +81,7 @@
(defmacro isometric-tiled-map! (defmacro isometric-tiled-map!
[screen k & options] [screen k & options]
`(let [^IsometricTiledMapRenderer object# (or (:renderer ~screen) ~screen)] `(let [^IsometricTiledMapRenderer object# (u/get-obj ~screen :renderer)]
(u/call! object# ~k ~@options))) (u/call! object# ~k ~@options)))
(defn isometric-staggered-tiled-map* (defn isometric-staggered-tiled-map*
@@ -98,7 +98,7 @@
(defmacro isometric-staggered-tiled-map! (defmacro isometric-staggered-tiled-map!
[screen k & options] [screen k & options]
`(let [^IsometricStaggeredTiledMapRenderer object# `(let [^IsometricStaggeredTiledMapRenderer object#
(or (:renderer ~screen) ~screen)] (u/get-obj ~screen :renderer)]
(u/call! object# ~k ~@options))) (u/call! object# ~k ~@options)))
(defn hexagonal-tiled-map* (defn hexagonal-tiled-map*
@@ -112,7 +112,7 @@
(defmacro hexagonal-tiled-map! (defmacro hexagonal-tiled-map!
[screen k & options] [screen k & options]
`(let [^HexagonalTiledMapRenderer object# (or (:renderer ~screen) ~screen)] `(let [^HexagonalTiledMapRenderer object# (u/get-obj ~screen :renderer)]
(u/call! object# ~k ~@options))) (u/call! object# ~k ~@options)))
(defn stage* (defn stage*
@@ -125,7 +125,7 @@
(defmacro stage! (defmacro stage!
[screen k & options] [screen k & options]
`(let [^Stage object# (or (:renderer ~screen) ~screen)] `(let [^Stage object# (u/get-obj ~screen :renderer)]
(u/call! object# ~k ~@options))) (u/call! object# ~k ~@options)))
; batch ; batch
@@ -209,7 +209,7 @@
(defmacro orthographic! (defmacro orthographic!
[screen k & options] [screen k & options]
`(let [^OrthographicCamera object# (or (:camera ~screen) ~screen)] `(let [^OrthographicCamera object# (u/get-obj ~screen :camera)]
(u/call! object# ~k ~@options))) (u/call! object# ~k ~@options)))
(defn perspective (defn perspective
@@ -223,12 +223,12 @@
(defmacro perspective! (defmacro perspective!
[screen k & options] [screen k & options]
`(let [^PerspectiveCamera object# (or (:camera ~screen) ~screen)] `(let [^PerspectiveCamera object# (u/get-obj ~screen :camera)]
(u/call! object# ~k ~@options))) (u/call! object# ~k ~@options)))
(defn size! (defn size!
[screen width height] [screen width height]
(let [^OrthographicCamera camera (or (:camera screen) screen)] (let [^OrthographicCamera camera (u/get-obj screen :camera)]
(assert camera) (assert camera)
(.setToOrtho camera false width height))) (.setToOrtho camera false width height)))
@@ -242,21 +242,21 @@
(defn x! (defn x!
[screen x] [screen x]
(let [^Camera camera (or (:camera screen) screen)] (let [^Camera camera (u/get-obj screen :camera)]
(assert camera) (assert camera)
(set! (. (. camera position) x) x) (set! (. (. camera position) x) x)
(.update camera))) (.update camera)))
(defn y! (defn y!
[screen y] [screen y]
(let [^Camera camera (or (:camera screen) screen)] (let [^Camera camera (u/get-obj screen :camera)]
(assert camera) (assert camera)
(set! (. (. camera position) y) y) (set! (. (. camera position) y) y)
(.update camera))) (.update camera)))
(defn z! (defn z!
[screen z] [screen z]
(let [^Camera camera (or (:camera screen) screen)] (let [^Camera camera (u/get-obj screen :camera)]
(assert camera) (assert camera)
(set! (. (. camera position) z) z) (set! (. (. camera position) z) z)
(.update camera))) (.update camera)))

View File

@@ -14,19 +14,19 @@
(string? arg) (string? arg)
(-> ^String arg Texture. TextureRegion.) (-> ^String arg Texture. TextureRegion.)
(map? arg) (map? arg)
(TextureRegion. ^TextureRegion (:object arg)) (TextureRegion. ^TextureRegion (u/get-obj arg :object))
:else :else
arg))) arg)))
(defmacro texture (defmacro texture
[arg & options] [arg & options]
`(let [entity# (texture* ~arg)] `(let [entity# (texture* ~arg)]
(u/calls! ^TextureRegion (:object entity#) ~@options) (u/calls! ^TextureRegion (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro texture! (defmacro texture!
[entity k & options] [entity k & options]
`(u/call! ^TextureRegion (:object ~entity) ~k ~@options)) `(u/call! ^TextureRegion (u/get-obj ~entity :object) ~k ~@options))
(defmacro play-mode (defmacro play-mode
[key] [key]
@@ -35,7 +35,7 @@
(defn animation* (defn animation*
[duration textures] [duration textures]
(Animation. duration (Animation. duration
(u/gdx-array (map :object textures)) (u/gdx-array (map #(u/get-obj % :object) textures))
(play-mode :normal))) (play-mode :normal)))
(defmacro animation (defmacro animation

View File

@@ -22,7 +22,7 @@
(defmacro box-2d! (defmacro box-2d!
[screen k & options] [screen k & options]
`(let [^World object# (or (:world ~screen) ~screen)] `(let [^World object# (u/get-obj ~screen :world)]
(u/call! object# ~k ~@options))) (u/call! object# ~k ~@options)))
; bodies ; bodies
@@ -40,7 +40,7 @@
(defmacro body! (defmacro body!
[entity k & options] [entity k & options]
`(let [^Body object# (or (:body ~entity) ~entity)] `(let [^Body object# (u/get-obj ~entity :body)]
(u/call! object# ~k ~@options))) (u/call! object# ~k ~@options)))
(defn create-body!* (defn create-body!*
@@ -180,7 +180,7 @@
(defn first-contact (defn first-contact
([screen] ([screen]
(let [^Contact contact (or (:contact screen) screen)] (let [^Contact contact (u/get-obj screen :contact)]
(assert contact) (assert contact)
(-> contact .getFixtureA .getBody))) (-> contact .getFixtureA .getBody)))
([screen entities] ([screen entities]
@@ -188,7 +188,7 @@
(defn second-contact (defn second-contact
([screen] ([screen]
(let [^Contact contact (or (:contact screen) screen)] (let [^Contact contact (u/get-obj screen :contact)]
(assert contact) (assert contact)
(-> contact .getFixtureB .getBody))) (-> contact .getFixtureB .getBody)))
([screen entities] ([screen entities]

View File

@@ -149,12 +149,12 @@
(defmacro check-box (defmacro check-box
[text arg & options] [text arg & options]
`(let [entity# (check-box* ~text ~arg)] `(let [entity# (check-box* ~text ~arg)]
(u/calls! ^CheckBox (:object entity#) ~@options) (u/calls! ^CheckBox (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro check-box! (defmacro check-box!
[entity k & options] [entity k & options]
`(u/call! ^Checkbox (:object ~entity) ~k ~@options)) `(u/call! ^Checkbox (u/get-obj ~entity :object) ~k ~@options))
(defn dialog* (defn dialog*
[text arg] [text arg]
@@ -163,12 +163,12 @@
(defmacro dialog (defmacro dialog
[text arg & options] [text arg & options]
`(let [entity# (dialog* ~text ~arg)] `(let [entity# (dialog* ~text ~arg)]
(u/calls! ^Dialog (:object entity#) ~@options) (u/calls! ^Dialog (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro dialog! (defmacro dialog!
[entity k & options] [entity k & options]
`(u/call! ^Dialog (:object ~entity) ~k ~@options)) `(u/call! ^Dialog (u/get-obj ~entity :object) ~k ~@options))
(defn horizontal* (defn horizontal*
[children] [children]
@@ -177,12 +177,12 @@
(defmacro horizontal (defmacro horizontal
[children & options] [children & options]
`(let [entity# (horizontal* ~children)] `(let [entity# (horizontal* ~children)]
(u/calls! ^HorizontalGroup (:object entity#) ~@options) (u/calls! ^HorizontalGroup (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro horizontal! (defmacro horizontal!
[entity k & options] [entity k & options]
`(u/call! ^HorizontalGroup (:object ~entity) ~k ~@options)) `(u/call! ^HorizontalGroup (u/get-obj ~entity :object) ~k ~@options))
(defn image* (defn image*
[arg] [arg]
@@ -198,12 +198,12 @@
(defmacro image (defmacro image
[arg & options] [arg & options]
`(let [entity# (image* ~arg)] `(let [entity# (image* ~arg)]
(u/calls! ^Image (:object entity#) ~@options) (u/calls! ^Image (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro image! (defmacro image!
[entity k & options] [entity k & options]
`(u/call! ^Image (:object ~entity) ~k ~@options)) `(u/call! ^Image (u/get-obj ~entity :object) ~k ~@options))
(defn image-button* (defn image-button*
[arg] [arg]
@@ -212,12 +212,12 @@
(defmacro image-button (defmacro image-button
[arg & options] [arg & options]
`(let [entity# (image-button* ~arg)] `(let [entity# (image-button* ~arg)]
(u/calls! ^ImageButton (:object entity#) ~@options) (u/calls! ^ImageButton (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro image-button! (defmacro image-button!
[entity k & options] [entity k & options]
`(u/call! ^ImageButton (:object ~entity) ~k ~@options)) `(u/call! ^ImageButton (u/get-obj ~entity :object) ~k ~@options))
(defn image-text-button* (defn image-text-button*
[^String text arg] [^String text arg]
@@ -226,12 +226,12 @@
(defmacro image-text-button (defmacro image-text-button
[text arg & options] [text arg & options]
`(let [entity# (image-text-button* ~text ~arg)] `(let [entity# (image-text-button* ~text ~arg)]
(u/calls! ^ImageTextButton (:object entity#) ~@options) (u/calls! ^ImageTextButton (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro image-text-button! (defmacro image-text-button!
[entity k & options] [entity k & options]
`(u/call! ^ImageTextButton (:object ~entity) ~k ~@options)) `(u/call! ^ImageTextButton (u/get-obj ~entity :object) ~k ~@options))
(defn label* (defn label*
[^String text arg] [^String text arg]
@@ -243,26 +243,26 @@
(defmacro label (defmacro label
[text arg & options] [text arg & options]
`(let [entity# (label* ~text ~arg)] `(let [entity# (label* ~text ~arg)]
(u/calls! ^Label (:object entity#) ~@options) (u/calls! ^Label (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro label! (defmacro label!
[entity k & options] [entity k & options]
`(u/call! ^Label (:object ~entity) ~k ~@options)) `(u/call! ^Label (u/get-obj ~entity :object) ~k ~@options))
(defn scroll-pane* (defn scroll-pane*
[child arg] [child arg]
(u/create-entity (ScrollPane. (:object child) arg))) (u/create-entity (ScrollPane. (u/get-obj child :object) arg)))
(defmacro scroll-pane (defmacro scroll-pane
[child arg & options] [child arg & options]
`(let [entity# (scroll-pane* ~child ~arg)] `(let [entity# (scroll-pane* ~child ~arg)]
(u/calls! ^ScrollPane (:object entity#) ~@options) (u/calls! ^ScrollPane (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro scroll-pane! (defmacro scroll-pane!
[entity k & options] [entity k & options]
`(u/call! ^ScrollPane (:object ~entity) ~k ~@options)) `(u/call! ^ScrollPane (u/get-obj ~entity :object) ~k ~@options))
(defn select-box* (defn select-box*
[items arg] [items arg]
@@ -271,12 +271,12 @@
(defmacro select-box (defmacro select-box
[items arg & options] [items arg & options]
`(let [entity# (select-box* ~items ~arg)] `(let [entity# (select-box* ~items ~arg)]
(u/calls! ^SelectBox (:object entity#) ~@options) (u/calls! ^SelectBox (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro select-box! (defmacro select-box!
[entity k & options] [entity k & options]
`(u/call! ^SelectBox (:object ~entity) ~k ~@options)) `(u/call! ^SelectBox (u/get-obj ~entity :object) ~k ~@options))
(defn slider* (defn slider*
[{:keys [min max step vertical?] [{:keys [min max step vertical?]
@@ -288,12 +288,12 @@
(defmacro slider (defmacro slider
[attrs arg & options] [attrs arg & options]
`(let [entity# (slider* ~attrs ~arg)] `(let [entity# (slider* ~attrs ~arg)]
(u/calls! ^Slider (:object entity#) ~@options) (u/calls! ^Slider (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro slider! (defmacro slider!
[entity k & options] [entity k & options]
`(u/call! ^Slider (:object ~entity) ~k ~@options)) `(u/call! ^Slider (u/get-obj ~entity :object) ~k ~@options))
(defn stack* (defn stack*
[children] [children]
@@ -302,12 +302,12 @@
(defmacro stack (defmacro stack
[children & options] [children & options]
`(let [entity# (stack* ~children)] `(let [entity# (stack* ~children)]
(u/calls! ^Stack (:object entity#) ~@options) (u/calls! ^Stack (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro stack! (defmacro stack!
[entity k & options] [entity k & options]
`(u/call! ^Stack (:object ~entity) ~k ~@options)) `(u/call! ^Stack (u/get-obj ~entity :object) ~k ~@options))
(defn table* (defn table*
[children] [children]
@@ -316,12 +316,12 @@
(defmacro table (defmacro table
[children & options] [children & options]
`(let [entity# (table* ~children)] `(let [entity# (table* ~children)]
(u/calls! ^Table (:object entity#) ~@options) (u/calls! ^Table (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro table! (defmacro table!
[entity k & options] [entity k & options]
`(u/call! ^Table (:object ~entity) ~k ~@options)) `(u/call! ^Table (u/get-obj ~entity :object) ~k ~@options))
(defn text-button* (defn text-button*
[^String text arg] [^String text arg]
@@ -330,12 +330,12 @@
(defmacro text-button (defmacro text-button
[text arg & options] [text arg & options]
`(let [entity# (text-button* ~text ~arg)] `(let [entity# (text-button* ~text ~arg)]
(u/calls! ^TextButton (:object entity#) ~@options) (u/calls! ^TextButton (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro text-button! (defmacro text-button!
[entity k & options] [entity k & options]
`(u/call! ^TextButton (:object ~entity) ~k ~@options)) `(u/call! ^TextButton (u/get-obj ~entity :object) ~k ~@options))
(defn text-field* (defn text-field*
[^String text arg] [^String text arg]
@@ -344,12 +344,12 @@
(defmacro text-field (defmacro text-field
[text arg & options] [text arg & options]
`(let [entity# (text-field* ~text ~arg)] `(let [entity# (text-field* ~text ~arg)]
(u/calls! ^TextField (:object entity#) ~@options) (u/calls! ^TextField (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro text-field! (defmacro text-field!
[entity k & options] [entity k & options]
`(u/call! ^TextField (:object ~entity) ~k ~@options)) `(u/call! ^TextField (u/get-obj ~entity :object) ~k ~@options))
(defn tree* (defn tree*
[children arg] [children arg]
@@ -358,12 +358,12 @@
(defmacro tree (defmacro tree
[children arg & options] [children arg & options]
`(let [entity# (tree* ~children ~arg)] `(let [entity# (tree* ~children ~arg)]
(u/calls! ^Tree (:object entity#) ~@options) (u/calls! ^Tree (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro tree! (defmacro tree!
[entity k & options] [entity k & options]
`(u/call! ^Tree (:object ~entity) ~k ~@options)) `(u/call! ^Tree (u/get-obj ~entity :object) ~k ~@options))
(defn vertical* (defn vertical*
[children] [children]
@@ -372,12 +372,12 @@
(defmacro vertical (defmacro vertical
[children & options] [children & options]
`(let [entity# (vertical* ~children)] `(let [entity# (vertical* ~children)]
(u/calls! ^VerticalGroup (:object entity#) ~@options) (u/calls! ^VerticalGroup (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro vertical! (defmacro vertical!
[entity k & options] [entity k & options]
`(u/call! ^VerticalGroup (:object ~entity) ~k ~@options)) `(u/call! ^VerticalGroup (u/get-obj ~entity :object) ~k ~@options))
(defn window* (defn window*
[children ^String title arg] [children ^String title arg]
@@ -386,9 +386,9 @@
(defmacro window (defmacro window
[children title arg & options] [children title arg & options]
`(let [entity# (window* ~children ~title ~arg)] `(let [entity# (window* ~children ~title ~arg)]
(u/calls! ^Window (:object entity#) ~@options) (u/calls! ^Window (u/get-obj entity# :object) ~@options)
entity#)) entity#))
(defmacro window! (defmacro window!
[entity k & options] [entity k & options]
`(u/call! ^Window (:object ~entity) ~k ~@options)) `(u/call! ^Window (u/get-obj ~entity :object) ~k ~@options))

View File

@@ -4,11 +4,18 @@
[com.badlogic.gdx.scenes.scene2d Actor] [com.badlogic.gdx.scenes.scene2d Actor]
[com.badlogic.gdx.utils Array ArrayMap])) [com.badlogic.gdx.utils Array ArrayMap]))
; exceptions ; misc
(defn throw-key-not-found (defn throw-key-not-found
[k] [k]
(throw (Exception. (str "The keyword " k " is not supported.")))) (throw (Exception. (str "The keyword " k " is not found."))))
(defn get-obj
[obj k]
(if (map? obj)
(or (get obj k)
(throw-key-not-found k))
obj))
; converting keys ; converting keys