Refactor and add groups

This commit is contained in:
oakes
2014-01-11 22:40:40 -05:00
parent 140a21ea8a
commit 330f2a347d
4 changed files with 64 additions and 35 deletions

View File

@@ -70,9 +70,9 @@
(defmacro animation
[duration textures & args]
`(Animation. ~duration
(u/gdx-into-array (map :object ~textures))
(u/gdx-static-field :graphics :g2d :Animation
~(or (first args) :normal))))
(u/convert-array (map :object ~textures))
(u/static-field-upper :graphics :g2d :Animation
~(or (first args) :normal))))
(defn animation->texture
([{:keys [total-time]} ^Animation animation]

View File

@@ -13,7 +13,7 @@
(defmacro color
[& args]
`~(if (keyword? (first args))
`(Color. ^Color (u/gdx-static-field :graphics :Color ~(first args)))
`(Color. ^Color (u/static-field-upper :graphics :Color ~(first args)))
`(Color. ~@args)))
; interop
@@ -62,7 +62,7 @@
(defmacro key-code
[key]
`~(symbol (str u/gdx-package ".Input$Keys/" (u/key->static-field key))))
`~(symbol (str u/main-package ".Input$Keys/" (u/key->static-field key true))))
(defmacro is-pressed?
[key]

View File

@@ -6,7 +6,7 @@
[com.badlogic.gdx.scenes.scene2d Actor Stage]
[com.badlogic.gdx.scenes.scene2d.ui ButtonGroup CheckBox Dialog
HorizontalGroup Image ImageButton ImageTextButton Label Skin Slider
TextButton TextField VerticalGroup WidgetGroup]
Table TextButton TextField VerticalGroup WidgetGroup]
[com.badlogic.gdx.scenes.scene2d.utils ActorGestureListener
ChangeListener ClickListener DragListener FocusListener
NinePatchDrawable SpriteDrawable TextureRegionDrawable
@@ -14,7 +14,7 @@
(defmacro drawable
[type & options]
`(~(symbol (str u/gdx-package ".scenes.scene2d.u."
`(~(symbol (str u/main-package ".scenes.scene2d.u."
(u/key->class type) "Drawable."))
~@options))
@@ -24,7 +24,7 @@
(defmacro style
[type & options]
`(~(symbol (str u/gdx-package ".scenes.scene2d.ui."
`(~(symbol (str u/main-package ".scenes.scene2d.ui."
(u/key->class type) "$"
(u/key->class type) "Style."))
~@options))
@@ -119,30 +119,43 @@
; groups
(defn add-children
[^WidgetGroup group children]
(doseq [{:keys [object]} children]
(assert (isa? (type object) Actor))
(.addActor group ^Actor object))
group)
(defmulti add-to-group! #(-> % first :object type) :default WidgetGroup)
(defn horizontal*
[children]
(add-children (HorizontalGroup.) children))
(defmethod add-to-group! WidgetGroup
[[{:keys [^WidgetGroup object]} children]]
(doseq [child children]
(.addActor object ^Actor (:object child))))
(defmethod add-to-group! Table
[[{:keys [^Table object]} children]]
(doseq [child children]
(.add object ^Actor (:object child))))
(defn add!
[group children]
(add-to-group! [group children])
group)
(defmacro horizontal
[children & options]
`(u/create-entity (u/calls! ^HorizontalGroup (horizontal* ~children)
~@options)))
(defn vertical*
[children]
(add-children (VerticalGroup.) children))
`(-> (u/calls! ^HorizontalGroup (HorizontalGroup.) ~@options)
(doto (.setFillParent true))
u/create-entity
(add! ~children)))
(defmacro vertical
[children & options]
`(u/create-entity (u/calls! ^VerticalGroup (vertical* ~children)
~@options)))
`(-> (u/calls! ^VerticalGroup (VerticalGroup.) ~@options)
(doto (.setFillParent true))
u/create-entity
(add! ~children)))
(defmacro table
[children & options]
`(-> (u/calls! ^Table (Table.) ~@options)
(doto (.setFillParent true))
u/create-entity
(add! ~children)))
; listeners
@@ -266,3 +279,15 @@
(defmacro dialog!
[entity k & options]
`(u/call! ^Dialog (:object ~entity) ~k ~@options))
(defmacro horizontal!
[entity k & options]
`(u/call! ^HorizontalGroup (:object ~entity) ~k ~@options))
(defmacro vertical!
[entity k & options]
`(u/call! ^VerticalGroup (:object ~entity) ~k ~@options))
(defmacro table!
[entity k & options]
`(u/call! ^Table (:object ~entity) ~k ~@options))

View File

@@ -4,7 +4,7 @@
[com.badlogic.gdx.utils Array]
[com.badlogic.gdx.scenes.scene2d Actor]))
(def ^:const gdx-package "com.badlogic.gdx")
(def ^:const main-package "com.badlogic.gdx")
(defn ^:private split-key
[key]
@@ -12,12 +12,12 @@
(defn ^:private join-keys
[keys]
(->> keys (map name) (s/join ".") (str gdx-package ".")))
(->> keys (map name) (s/join ".") (str main-package ".")))
(defn key->static-field
[key]
[key upper?]
(->> (split-key key)
(map s/upper-case)
(map (if upper? s/upper-case s/lower-case))
(s/join "_")
symbol))
@@ -38,17 +38,21 @@
(str ".")
symbol)))
(defn gdx-static-field*
[args]
(->> (key->static-field (last args))
(defn ^:private static-field
[args upper?]
(->> (key->static-field (last args) upper?)
(str (join-keys (butlast args)) "/")
symbol))
(defmacro gdx-static-field
(defmacro static-field-lower
[& args]
`~(gdx-static-field* args))
`~(static-field args false))
(defn gdx-into-array
(defmacro static-field-upper
[& args]
`~(static-field args true))
(defn convert-array
[a]
(Array. true (into-array a) 1 (count a)))