diff --git a/src/play_clj/core_2d.clj b/src/play_clj/core_2d.clj index 87b8f8f..e4a5a77 100644 --- a/src/play_clj/core_2d.clj +++ b/src/play_clj/core_2d.clj @@ -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] diff --git a/src/play_clj/core_global.clj b/src/play_clj/core_global.clj index 5e6a61b..4ce248f 100644 --- a/src/play_clj/core_global.clj +++ b/src/play_clj/core_global.clj @@ -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] diff --git a/src/play_clj/ui.clj b/src/play_clj/ui.clj index 7f6dd4e..2428cd1 100644 --- a/src/play_clj/ui.clj +++ b/src/play_clj/ui.clj @@ -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)) diff --git a/src/play_clj/utils.clj b/src/play_clj/utils.clj index 7a4a12a..33d08a7 100644 --- a/src/play_clj/utils.clj +++ b/src/play_clj/utils.clj @@ -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)))