\r
package com.badlogic.gdx.graphics.g2d;\r
\r
+import com.badlogic.gdx.graphics.Color;\r
import com.badlogic.gdx.graphics.Texture;\r
import com.badlogic.gdx.utils.GdxRuntimeException;\r
\r
public static final int BOTTOM_RIGHT = 8;\r
\r
private TextureRegion[] patches;\r
+ private Color color;\r
\r
private NinePatch () {\r
}\r
}\r
\r
public NinePatch (TextureRegion region, int left, int right, int top, int bottom) {\r
+ if (region == null) throw new IllegalArgumentException("region cannot be null.");\r
int middleWidth = region.getRegionWidth() - left - right;\r
int middleHeight = region.getRegionHeight() - top - bottom;\r
\r
}\r
\r
public NinePatch (TextureRegion... patches) {\r
- if (patches.length != 9) throw new IllegalArgumentException("NinePatch needs nine TextureRegions");\r
+ if (patches == null || patches.length != 9) throw new IllegalArgumentException("NinePatch needs nine TextureRegions");\r
this.patches = patches;\r
checkValidity();\r
}\r
\r
+ public NinePatch (NinePatch ninePatch) {\r
+ this(ninePatch, new Color(ninePatch.color));\r
+ }\r
+\r
+ public NinePatch (NinePatch ninePatch, Color color) {\r
+ this.patches = new TextureRegion[9];\r
+ System.arraycopy(ninePatch.patches, 0, patches, 0, 9);\r
+ this.color = color;\r
+ }\r
+\r
private void checkValidity () {\r
float leftWidth = getLeftWidth();\r
if ((patches[TOP_LEFT] != null && patches[TOP_LEFT].getRegionWidth() != leftWidth)\r
float middleRowY = y + getBottomHeight();\r
float topRowY = y + height - getTopHeight();\r
\r
+ if (color != null) batch.setColor(color);\r
+\r
// Bottom row\r
if (patches[BOTTOM_LEFT] != null) batch.draw(patches[BOTTOM_LEFT], x, y, centerColumnX - x, middleRowY - y);\r
if (patches[BOTTOM_CENTER] != null)\r
public TextureRegion[] getPatches () {\r
return patches;\r
}\r
+\r
+ public void setColor (Color color) {\r
+ this.color = color;\r
+ }\r
+\r
+ public Color getColor () {\r
+ return color;\r
+ }\r
}\r
import com.badlogic.gdx.graphics.g2d.TextureRegion;\r
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;\r
import com.badlogic.gdx.scenes.scene2d.ui.CheckBox.CheckBoxStyle;\r
+import com.badlogic.gdx.utils.Array;\r
import com.badlogic.gdx.utils.Disposable;\r
import com.badlogic.gdx.utils.GdxRuntimeException;\r
import com.badlogic.gdx.utils.Json;\r
}\r
\r
public Object read (Json json, Object jsonData, Class type) {\r
+ if (jsonData instanceof ObjectMap) {\r
+ json.setSerializer(type, null);\r
+ try {\r
+ return json.readValue(type, jsonData);\r
+ } finally {\r
+ json.setSerializer(type, this);\r
+ }\r
+ }\r
String name = (String)jsonData;\r
Object object = map.get(name);\r
if (object != null) return object;\r
json.setSerializer(NinePatch.class, new Serializer<NinePatch>() {\r
public void write (Json json, NinePatch ninePatch, Class valueType) {\r
TextureRegion[] patches = ninePatch.getPatches();\r
+ json.writeObjectStart();\r
+ if (ninePatch.getColor() != null) json.writeValue("color", ninePatch.getColor());\r
if (patches[0] == null && patches[1] == null && patches[2] == null && patches[3] == null && patches[4] != null\r
&& patches[5] == null && patches[6] == null && patches[7] == null && patches[8] == null)\r
- json.writeValue(new TextureRegion[] {patches[4]});\r
+ json.writeValue("region", patches[4]);\r
else\r
- json.writeValue(ninePatch.getPatches());\r
+ json.writeValue("regions", ninePatch.getPatches());\r
+ json.writeObjectEnd();\r
}\r
\r
public NinePatch read (Json json, Object jsonData, Class type) {\r
- TextureRegion[] regions = json.readValue(TextureRegion[].class, jsonData);\r
- if (regions.length == 1) return new NinePatch(regions[0]);\r
- return new NinePatch(regions);\r
+ if (jsonData instanceof Array) {\r
+ TextureRegion[] regions = json.readValue(TextureRegion[].class, jsonData);\r
+ if (regions.length == 1) return new NinePatch(regions[0]);\r
+ return new NinePatch(regions);\r
+ } else {\r
+ ObjectMap map = (ObjectMap)jsonData;\r
+ NinePatch ninePatch;\r
+ if (map.containsKey("regions"))\r
+ ninePatch = new NinePatch(json.readValue("regions", TextureRegion[].class, jsonData));\r
+ else if (map.containsKey("region"))\r
+ ninePatch = new NinePatch(json.readValue("region", TextureRegion.class, jsonData));\r
+ else\r
+ throw new SerializationException("Missing ninepatch regions: " + map);\r
+ if (map.containsKey("color")) ninePatch.setColor(json.readValue("color", Color.class, jsonData));\r
+ return ninePatch;\r
+ }\r
}\r
});\r
\r
+ json.setSerializer(Color.class, new Serializer<Color>() {\r
+ public void write (Json json, Color color, Class valueType) {\r
+ json.writeObjectStart();\r
+ json.writeFields(color);\r
+ json.writeObjectEnd();\r
+ }\r
+\r
+ public Color read (Json json, Object jsonData, Class type) {\r
+ ObjectMap map = (ObjectMap)jsonData;\r
+ float r = json.readValue("r", float.class, 0f, jsonData);\r
+ float g = json.readValue("g", float.class, 0f, jsonData);\r
+ float b = json.readValue("b", float.class, 0f, jsonData);\r
+ float a = json.readValue("a", float.class, 1f, jsonData);\r
+ return new Color(r, g, b, a);\r
+ }\r
+ });\r
return json;\r
}\r
}\r