OSDN Git Service

[updated] NinePatch, added a tint color, copy ctor.
authornathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Thu, 3 Nov 2011 00:34:36 +0000 (00:34 +0000)
committernathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Thu, 3 Nov 2011 00:34:36 +0000 (00:34 +0000)
[updated] Skin, support NinePatch colors.

gdx/src/com/badlogic/gdx/graphics/g2d/NinePatch.java
gdx/src/com/badlogic/gdx/graphics/g2d/TextureRegion.java
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Skin.java

index ecfcb09..53dba33 100644 (file)
@@ -16,6 +16,7 @@
 \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
@@ -31,6 +32,7 @@ public class NinePatch {
        public static final int BOTTOM_RIGHT = 8;\r
 \r
        private TextureRegion[] patches;\r
+       private Color color;\r
 \r
        private NinePatch () {\r
        }\r
@@ -40,6 +42,7 @@ public class NinePatch {
        }\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
@@ -94,11 +97,21 @@ public class NinePatch {
        }\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
@@ -135,6 +148,8 @@ public class NinePatch {
                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
@@ -212,4 +227,12 @@ public class NinePatch {
        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
index a25e5c0..401dc31 100644 (file)
@@ -33,6 +33,7 @@ public class TextureRegion {
 \r
        /** Constructs a region the size of the specified texture. */\r
        public TextureRegion (Texture texture) {\r
+               if (texture == null) throw new IllegalArgumentException("texture cannot be null.");\r
                this.texture = texture;\r
                setRegion(0, 0, texture.getWidth(), texture.getHeight());\r
        }\r
index ab59b27..6215a5a 100644 (file)
@@ -30,6 +30,7 @@ import com.badlogic.gdx.graphics.g2d.NinePatch;
 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
@@ -309,6 +310,14 @@ public class Skin implements Disposable {
                        }\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
@@ -414,20 +423,52 @@ public class Skin implements Disposable {
                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