diff --git a/src/play_clj/core.clj b/src/play_clj/core.clj index bf083cc..d7ff929 100644 --- a/src/play_clj/core.clj +++ b/src/play_clj/core.clj @@ -5,7 +5,7 @@ [com.badlogic.gdx.audio Sound] [com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera PerspectiveCamera] - [com.badlogic.gdx.graphics.g2d SpriteBatch TextureRegion] + [com.badlogic.gdx.graphics.g2d NinePatch SpriteBatch TextureRegion] [com.badlogic.gdx.input GestureDetector GestureDetector$GestureListener] [com.badlogic.gdx.maps MapLayer MapLayers] diff --git a/src/play_clj/core_graphics.clj b/src/play_clj/core_graphics.clj index 0ac6229..54012ef 100644 --- a/src/play_clj/core_graphics.clj +++ b/src/play_clj/core_graphics.clj @@ -208,6 +208,24 @@ with the tiled map file at `path` and `unit` scale (defmethod draw-entity! nil [_ _]) +(defmethod draw-entity! :texture + [^SpriteBatch batch {:keys [^TextureRegion object x y width height]}] + (assert object) + (let [x (float (or x 0)) + y (float (or y 0)) + width (float (or width (.getRegionWidth object))) + height (float (or height (.getRegionHeight object)))] + (.draw batch object x y width height))) + +(defmethod draw-entity! :nine-patch + [^SpriteBatch batch {:keys [^NinePatch object x y width height]}] + (assert object) + (let [x (float (or x 0)) + y (float (or y 0)) + width (float (or width (.getTotalWidth object))) + height (float (or height (.getTotalHeight object)))] + (.draw object batch x y width height))) + (defmethod draw-entity! :actor [^SpriteBatch batch {:keys [^Actor object] :as entity}] (assert object) @@ -220,15 +238,6 @@ with the tiled map file at `path` and `unit` scale nil)) (.draw object batch 1)) -(defmethod draw-entity! :texture - [^SpriteBatch batch {:keys [^TextureRegion object x y width height]}] - (assert object) - (let [x (float (or x 0)) - y (float (or y 0)) - width (float (or width (.getRegionWidth object))) - height (float (or height (.getRegionHeight object)))] - (.draw batch object x y width height))) - (defn draw! "Draws the `entities` with the renderer from `screen` diff --git a/src/play_clj/g2d.clj b/src/play_clj/g2d.clj index c757161..317d3cd 100644 --- a/src/play_clj/g2d.clj +++ b/src/play_clj/g2d.clj @@ -1,7 +1,8 @@ (ns play-clj.g2d (:require [play-clj.utils :as u]) (:import [com.badlogic.gdx.graphics Texture] - [com.badlogic.gdx.graphics.g2d Animation BitmapFont TextureRegion])) + [com.badlogic.gdx.graphics.g2d Animation BitmapFont NinePatch + TextureRegion])) (defmacro bitmap-font "Returns a [BitmapFont](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/BitmapFont.html) @@ -18,8 +19,8 @@ (cond (string? arg) (-> ^String arg Texture. TextureRegion.) - (map? arg) - (TextureRegion. ^TextureRegion (u/get-obj arg :object)) + (:object arg) + (TextureRegion. ^TextureRegion (:object arg)) :else arg))) @@ -44,6 +45,40 @@ [entity k & options] `(u/call! ^TextureRegion (u/get-obj ~entity :object) ~k ~@options)) +(defn nine-patch* + "The function version of `nine-patch`" + [arg] + (u/create-entity + (cond + (string? arg) + (-> ^String arg Texture. TextureRegion. NinePatch.) + (:object arg) + (NinePatch. (:object arg)) + (map? arg) + (let [{:keys [^TextureRegion region left right top bottom]} arg] + (NinePatch. region left right top bottom)) + :else + arg))) + +(defmacro nine-patch + "Returns an entity based on [NinePatch](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/NinePatch.html) + + (nine-patch \"image.png\") + (nine-patch \"image.png\" :set-color (color :blue)) + (nine-patch {:image \"image.png\" :left 10 :right 10 :top 10 :bottom 10})" + [arg & options] + `(let [entity# (texture* ~arg)] + (u/calls! ^NinePatch (u/get-obj entity# :object) ~@options) + entity#)) + +(defmacro nine-patch! + "Calls a single method on a `nine-patch` + + (nine-patch! entity :set-color (color :blue)) + (nine-patch! entity :get-middle-width)" + [entity k & options] + `(u/call! ^TextureRegion (u/get-obj ~entity :object) ~k ~@options)) + (defmacro play-mode "Returns a static field from [Animation](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Animation.html) diff --git a/src/play_clj/utils.clj b/src/play_clj/utils.clj index 937d14a..58a7a8a 100644 --- a/src/play_clj/utils.clj +++ b/src/play_clj/utils.clj @@ -1,6 +1,6 @@ (ns play-clj.utils (:require [clojure.string :as s]) - (:import [com.badlogic.gdx.graphics.g2d TextureRegion] + (:import [com.badlogic.gdx.graphics.g2d NinePatch TextureRegion] [com.badlogic.gdx.scenes.scene2d Actor] [com.badlogic.gdx.utils Array ArrayMap])) @@ -193,6 +193,10 @@ new object to be created each time a field is set) [obj] {:type :texture :object obj}) +(defmethod create-entity NinePatch + [obj] + {:type :nine-patch :object obj}) + (defmethod create-entity Actor [obj] {:type :actor :object obj})