OSDN Git Service

Minor cleanup for ninepatch padding.
authorNathanSweet <nathan.sweet@gmail.com>
Thu, 4 Oct 2012 06:32:14 +0000 (23:32 -0700)
committerNathanSweet <nathan.sweet@gmail.com>
Thu, 4 Oct 2012 06:32:14 +0000 (23:32 -0700)
extensions/gdx-tools/src/com/badlogic/gdx/tools/imagepacker/ImageProcessor.java
extensions/gdx-tools/src/com/badlogic/gdx/tools/imagepacker/TexturePacker2.java
gdx/src/com/badlogic/gdx/graphics/g2d/NinePatch.java
gdx/src/com/badlogic/gdx/graphics/g2d/TextureAtlas.java
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Skin.java

index d28062e..4e04e45 100644 (file)
@@ -1,6 +1,10 @@
 \r
 package com.badlogic.gdx.tools.imagepacker;\r
 \r
+import com.badlogic.gdx.tools.imagepacker.TexturePacker2.Rect;\r
+import com.badlogic.gdx.tools.imagepacker.TexturePacker2.Settings;\r
+import com.badlogic.gdx.utils.Array;\r
+\r
 import java.awt.image.BufferedImage;\r
 import java.awt.image.WritableRaster;\r
 import java.io.File;\r
@@ -15,10 +19,6 @@ import java.util.regex.Pattern;
 \r
 import javax.imageio.ImageIO;\r
 \r
-import com.badlogic.gdx.tools.imagepacker.TexturePacker2.Rect;\r
-import com.badlogic.gdx.tools.imagepacker.TexturePacker2.Settings;\r
-import com.badlogic.gdx.utils.Array;\r
-\r
 public class ImageProcessor {\r
        static private final BufferedImage emptyImage = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);\r
        static private Pattern indexPattern = Pattern.compile("(.+)_(\\d+)$");\r
@@ -185,19 +185,18 @@ public class ImageProcessor {
         * has left, right, top, bottom. */\r
        private int[] getSplits (BufferedImage image, String name) {\r
                WritableRaster raster = image.getRaster();\r
-               \r
+\r
                int startX = getSplitPoint(raster, name, 1, 0, true, true);\r
                int endX = getSplitPoint(raster, name, startX, 0, false, true);\r
                int startY = getSplitPoint(raster, name, 0, 1, true, false);\r
                int endY = getSplitPoint(raster, name, 0, startY, false, false);\r
 \r
                // Ensure pixels after the end are not invalid.\r
-               getSplitPoint(raster, name, endX+1, 0, true, true);\r
-               getSplitPoint(raster, name, 0, endY+1, true, false);\r
-               \r
+               getSplitPoint(raster, name, endX + 1, 0, true, true);\r
+               getSplitPoint(raster, name, 0, endY + 1, true, false);\r
+\r
                // No splits, or all splits.\r
-               if (startX == 0 && endX == 0 && startY == 0 && endY == 0) \r
-                       return null;\r
+               if (startX == 0 && endX == 0 && startY == 0 && endY == 0) return null;\r
 \r
                // Subtraction here is because the coordinates were computed before the 1px border was stripped.\r
                if (startX != 0) {\r
@@ -214,37 +213,35 @@ public class ImageProcessor {
                        // If no start point was ever found, we assume full stretch.\r
                        endY = raster.getHeight() - 2;\r
                }\r
-               \r
+\r
                return new int[] {startX, endX, startY, endY};\r
        }\r
-       \r
-       /** Returns the pads, or null if the image had no pads or the pads match the splits. Pads are an int[4] that\r
-        * has left, right, top, bottom. */\r
+\r
+       /** Returns the pads, or null if the image had no pads or the pads match the splits. Pads are an int[4] that has left, right,\r
+        * top, bottom. */\r
        private int[] getPads (BufferedImage image, String name, int[] splits) {\r
                WritableRaster raster = image.getRaster();\r
-               \r
+\r
                int bottom = raster.getHeight() - 1;\r
                int right = raster.getWidth() - 1;\r
-               \r
+\r
                int startX = getSplitPoint(raster, name, 1, bottom, true, true);\r
                int startY = getSplitPoint(raster, name, right, 1, true, false);\r
-               \r
+\r
                // No need to hunt for the end if a start was never found.\r
                int endX = 0;\r
                int endY = 0;\r
-               if (startX != 0)\r
-                       endX = getSplitPoint(raster, name, startX+1, bottom, false, true);\r
-               if (startY != 0)\r
-                       endY = getSplitPoint(raster, name, right, startY+1, false, false);\r
-               \r
+               if (startX != 0) endX = getSplitPoint(raster, name, startX + 1, bottom, false, true);\r
+               if (startY != 0) endY = getSplitPoint(raster, name, right, startY + 1, false, false);\r
+\r
                // Ensure pixels after the end are not invalid.\r
-               getSplitPoint(raster, name, endX+1, bottom, true, true);\r
-               getSplitPoint(raster, name, right, endY+1, true, false);\r
-               \r
+               getSplitPoint(raster, name, endX + 1, bottom, true, true);\r
+               getSplitPoint(raster, name, right, endY + 1, true, false);\r
+\r
                // No pads.\r
                if (startX == 0 && endX == 0 && startY == 0 && endY == 0) {\r
                        return null;\r
-               }               \r
+               }\r
 \r
                // Subtraction here is because the coordinates were computed before the 1px border was stripped.\r
                if (startX > 0) {\r
@@ -261,28 +258,27 @@ public class ImageProcessor {
                        // If no start point was ever found, we assume full stretch.\r
                        endY = raster.getHeight() - 2;\r
                }\r
-               \r
+\r
                int[] pads = new int[] {startX, endX, startY, endY};\r
-               \r
-               if ((splits != null) && Arrays.equals(pads, splits)) {\r
+\r
+               if (splits != null && Arrays.equals(pads, splits)) {\r
                        return null;\r
                }\r
-               \r
+\r
                return pads;\r
        }\r
-       \r
-       /** Hunts for the start or end of a sequence of split pixels.  Begins searching at (startX, startY) then follows\r
-        * along the x or y axis (depending on value of xAxis) for the first non-transparent pixel if startPoint is true,\r
-        * or the first transparent pixel if startPoint is false.  Returns 0 if none found, as 0 is considered an invalid\r
-        * split point being in the outer border which will be stripped. */\r
-       private int getSplitPoint(WritableRaster raster, String name, int startX, int startY, boolean startPoint, boolean xAxis) \r
-       {\r
+\r
+       /** Hunts for the start or end of a sequence of split pixels. Begins searching at (startX, startY) then follows along the x or y\r
+        * axis (depending on value of xAxis) for the first non-transparent pixel if startPoint is true, or the first transparent pixel\r
+        * if startPoint is false. Returns 0 if none found, as 0 is considered an invalid split point being in the outer border which\r
+        * will be stripped. */\r
+       private int getSplitPoint (WritableRaster raster, String name, int startX, int startY, boolean startPoint, boolean xAxis) {\r
                int[] rgba = new int[4];\r
 \r
                int next = xAxis ? startX : startY;\r
                int end = xAxis ? raster.getWidth() : raster.getHeight();\r
                int breakA = startPoint ? 255 : 0;\r
-               \r
+\r
                int x = startX;\r
                int y = startY;\r
                while (next != end) {\r
@@ -290,17 +286,15 @@ public class ImageProcessor {
                                x = next;\r
                        else\r
                                y = next;\r
-                       \r
+\r
                        raster.getPixel(x, y, rgba);\r
-                       if (rgba[3] == breakA)\r
-                               return next;\r
-                       \r
-                       if (!startPoint && (rgba[0] != 0 || rgba[1] != 0 || rgba[2] != 0 || rgba[3] != 255))\r
-                               splitError(x, y, rgba, name);\r
-                       \r
+                       if (rgba[3] == breakA) return next;\r
+\r
+                       if (!startPoint && (rgba[0] != 0 || rgba[1] != 0 || rgba[2] != 0 || rgba[3] != 255)) splitError(x, y, rgba, name);\r
+\r
                        next++;\r
                }\r
-               \r
+\r
                return 0;\r
        }\r
 \r
index a70b840..0d9c41d 100644 (file)
@@ -1,6 +1,12 @@
 \r
 package com.badlogic.gdx.tools.imagepacker;\r
 \r
+import com.badlogic.gdx.graphics.Pixmap.Format;\r
+import com.badlogic.gdx.graphics.Texture.TextureFilter;\r
+import com.badlogic.gdx.graphics.Texture.TextureWrap;\r
+import com.badlogic.gdx.math.MathUtils;\r
+import com.badlogic.gdx.utils.Array;\r
+\r
 import java.awt.Color;\r
 import java.awt.Graphics2D;\r
 import java.awt.image.BufferedImage;\r
@@ -16,12 +22,6 @@ import javax.imageio.ImageWriteParam;
 import javax.imageio.ImageWriter;\r
 import javax.imageio.stream.ImageOutputStream;\r
 \r
-import com.badlogic.gdx.graphics.Pixmap.Format;\r
-import com.badlogic.gdx.graphics.Texture.TextureFilter;\r
-import com.badlogic.gdx.graphics.Texture.TextureWrap;\r
-import com.badlogic.gdx.math.MathUtils;\r
-import com.badlogic.gdx.utils.Array;\r
-\r
 /** @author Nathan Sweet */\r
 public class TexturePacker2 {\r
        private final Settings settings;\r
@@ -194,7 +194,7 @@ public class TexturePacker2 {
                        writer\r
                                .write("  split: " + rect.splits[0] + ", " + rect.splits[1] + ", " + rect.splits[2] + ", " + rect.splits[3] + "\n");\r
                        if (rect.pads != null) {\r
-                               writer.write("  pads: " + rect.pads[0] + ", " + rect.pads[1] + ", " + rect.pads[2] + ", " + rect.pads[3] + "\n");\r
+                               writer.write("  pad: " + rect.pads[0] + ", " + rect.pads[1] + ", " + rect.pads[2] + ", " + rect.pads[3] + "\n");\r
                        }\r
                }\r
                writer.write("  orig: " + rect.originalWidth + ", " + rect.originalHeight + "\n");\r
index 008c27d..f928124 100644 (file)
@@ -20,6 +20,8 @@ import com.badlogic.gdx.graphics.Color;
 import com.badlogic.gdx.graphics.Texture;\r
 import com.badlogic.gdx.utils.GdxRuntimeException;\r
 \r
+/** A 3x3 grid of texture regions. Any of the regions may be omitted. Padding may be set as a hint on how to inset content on top\r
+ * of the ninepatch. */\r
 public class NinePatch {\r
        public static final int TOP_LEFT = 0;\r
        public static final int TOP_CENTER = 1;\r
@@ -41,13 +43,8 @@ public class NinePatch {
        private float[] vertices = new float[9 * 4 * 5];\r
        private int idx;\r
        private final Color color = new Color(Color.WHITE);\r
-       \r
-       /** Padding overrides are stored by NinePatch for use by elements such as tables that use\r
-        *  a NinePatch for their background.   If these values are not set, the width/height of \r
-        *  border patches are used (eg. leftWidth, rightWidth, topHeight, and bottomHeight).\r
-        */\r
        private int padLeft = -1, padRight = -1, padTop = -1, padBottom = -1;\r
-       \r
+\r
        public NinePatch (Texture texture, int left, int right, int top, int bottom) {\r
                this(new TextureRegion(texture), left, right, top, bottom);\r
        }\r
@@ -373,51 +370,55 @@ public class NinePatch {
        public float getTotalHeight () {\r
                return topHeight + middleHeight + bottomHeight;\r
        }\r
-       \r
-       public void setPadding(int left, int right, int top, int bottom) {\r
+\r
+       public void setPadding (int left, int right, int top, int bottom) {\r
                this.padLeft = left;\r
                this.padRight = right;\r
                this.padTop = top;\r
                this.padBottom = bottom;\r
        }\r
-       \r
-       public void setPadLeft(int left) {\r
+\r
+       /** Returns the left padding if set, else returns {@link #getLeftWidth()}. */\r
+       public float getPadLeft () {\r
+               if (padLeft == -1) return getLeftWidth();\r
+               return padLeft;\r
+       }\r
+\r
+       public void setPadLeft (int left) {\r
                this.padLeft = left;\r
        }\r
-       \r
-       public void setPadRight(int right) {\r
+\r
+       /** Returns the right padding if set, else returns {@link #getRightWidth()}. */\r
+       public float getPadRight () {\r
+               if (padRight == -1) return getRightWidth();\r
+               return padRight;\r
+       }\r
+\r
+       public void setPadRight (int right) {\r
                this.padRight = right;\r
        }\r
-       \r
-       public void setPadTop(int top) {\r
+\r
+       /** Returns the top padding if set, else returns {@link #getTopHeight()}. */\r
+       public float getPadTop () {\r
+               if (padTop == -1) return getTopHeight();\r
+               return padTop;\r
+       }\r
+\r
+       public void setPadTop (int top) {\r
                this.padTop = top;\r
        }\r
-       \r
-       public void setPadBottom(int bottom) {\r
+\r
+       /** Returns the bottom padding if set, else returns {@link #getBottomHeight()}. */\r
+       public float getPadBottom () {\r
+               if (padBottom == -1) return getBottomHeight();\r
+               return padBottom;\r
+       }\r
+\r
+       public void setPadBottom (int bottom) {\r
                this.padBottom = bottom;\r
        }\r
-       \r
-       public float getPadLeft() {\r
-               if (this.padLeft == -1)\r
-                       return this.getLeftWidth();\r
-               return this.padLeft;\r
-       }\r
-       \r
-       public float getPadRight() {\r
-               if (this.padRight == -1)\r
-                       return this.getRightWidth();\r
-               return this.padRight;\r
-       }\r
-       \r
-       public float getPadTop() {\r
-               if (this.padTop == -1)\r
-                       return this.getTopHeight();\r
-               return this.padTop;\r
-       }\r
-       \r
-       public float getPadBottom() {\r
-               if (this.padBottom == -1)\r
-                       return this.getBottomHeight();\r
-               return this.padBottom;\r
-       }       \r
+\r
+       public Texture getTexture () {\r
+               return texture;\r
+       }\r
 }\r
index a549af7..21f1426 100644 (file)
 \r
 package com.badlogic.gdx.graphics.g2d;\r
 \r
-import java.io.BufferedReader;\r
-import java.io.IOException;\r
-import java.io.InputStreamReader;\r
-import java.util.Comparator;\r
-import java.util.HashSet;\r
-import java.util.Set;\r
+import static com.badlogic.gdx.graphics.Texture.TextureWrap.*;\r
 \r
 import com.badlogic.gdx.Files.FileType;\r
 import com.badlogic.gdx.Gdx;\r
@@ -30,7 +25,6 @@ import com.badlogic.gdx.graphics.Pixmap.Format;
 import com.badlogic.gdx.graphics.Texture;\r
 import com.badlogic.gdx.graphics.Texture.TextureFilter;\r
 import com.badlogic.gdx.graphics.Texture.TextureWrap;\r
-import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite;\r
 import com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page;\r
 import com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region;\r
 import com.badlogic.gdx.utils.Array;\r
@@ -38,7 +32,12 @@ import com.badlogic.gdx.utils.Disposable;
 import com.badlogic.gdx.utils.GdxRuntimeException;\r
 import com.badlogic.gdx.utils.ObjectMap;\r
 \r
-import static com.badlogic.gdx.graphics.Texture.TextureWrap.*;\r
+import java.io.BufferedReader;\r
+import java.io.IOException;\r
+import java.io.InputStreamReader;\r
+import java.util.Comparator;\r
+import java.util.HashSet;\r
+import java.util.Set;\r
 \r
 /** Loads images from texture atlases created by TexturePacker.<br>\r
  * <br>\r
@@ -149,11 +148,11 @@ public class TextureAtlas implements Disposable {
                                                if (readTuple(reader) == 4) { // split is optional\r
                                                        region.splits = new int[] {Integer.parseInt(tuple[0]), Integer.parseInt(tuple[1]),\r
                                                                Integer.parseInt(tuple[2]), Integer.parseInt(tuple[3])};\r
-                                                       \r
-                                                       if (readTuple(reader) == 4) { // pads are optional, but only present with splits\r
+\r
+                                                       if (readTuple(reader) == 4) { // pad is optional, but only present with splits\r
                                                                region.pads = new int[] {Integer.parseInt(tuple[0]), Integer.parseInt(tuple[1]),\r
                                                                        Integer.parseInt(tuple[2]), Integer.parseInt(tuple[3])};\r
-                                                               \r
+\r
                                                                readTuple(reader);\r
                                                        }\r
                                                }\r
@@ -389,11 +388,9 @@ public class TextureAtlas implements Disposable {
                        if (region.name.equals(name)) {\r
                                int[] splits = region.splits;\r
                                if (splits == null) throw new IllegalArgumentException("Region does not have ninepatch splits: " + name);\r
-                               NinePatch retVal = new NinePatch(region, splits[0], splits[1], splits[2], splits[3]);\r
-                               if (region.pads != null) {\r
-                                       retVal.setPadding(region.pads[0], region.pads[1], region.pads[2], region.pads[3]);\r
-                               }\r
-                               return retVal;\r
+                               NinePatch patch = new NinePatch(region, splits[0], splits[1], splits[2], splits[3]);\r
+                               if (region.pads != null) patch.setPadding(region.pads[0], region.pads[1], region.pads[2], region.pads[3]);\r
+                               return patch;\r
                        }\r
                }\r
                return null;\r
@@ -485,9 +482,8 @@ public class TextureAtlas implements Disposable {
 \r
                /** The ninepatch splits, or null if not a ninepatch. Has 4 elements: left, right, top, bottom. */\r
                public int[] splits;\r
-               \r
-               /** The ninepatch pads, or null if not a ninepatch or ninepatch does not override padding.\r
-                *  Has 4 elements: left, right, top, bottom. */\r
+\r
+               /** The ninepatch pads, or null if not a ninepatch or the has no padding. Has 4 elements: left, right, top, bottom. */\r
                public int[] pads;\r
 \r
                public AtlasRegion (Texture texture, int x, int y, int width, int height) {\r
index 47ddaa3..c4681d7 100644 (file)
@@ -214,9 +214,7 @@ public class Skin implements Disposable {
                                if (splits != null) {\r
                                        patch = new NinePatch(region, splits[0], splits[1], splits[2], splits[3]);\r
                                        int[] pads = ((AtlasRegion)region).pads;\r
-                                       if (pads != null) {\r
-                                               patch.setPadding(pads[0], pads[1], pads[2], pads[3]);\r
-                                       }\r
+                                       if (pads != null) patch.setPadding(pads[0], pads[1], pads[2], pads[3]);\r
                                }\r
                        }\r
                        if (patch == null) patch = new NinePatch(region);\r