1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.tests; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.graphics.Color; 21 import com.badlogic.gdx.graphics.GL20; 22 import com.badlogic.gdx.graphics.OrthographicCamera; 23 import com.badlogic.gdx.graphics.Pixmap; 24 import com.badlogic.gdx.graphics.Texture; 25 import com.badlogic.gdx.graphics.g2d.NinePatch; 26 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 27 import com.badlogic.gdx.graphics.g2d.TextureRegion; 28 import com.badlogic.gdx.math.Interpolation; 29 import com.badlogic.gdx.math.MathUtils; 30 import com.badlogic.gdx.tests.utils.GdxTest; 31 import com.badlogic.gdx.utils.Array; 32 33 public class NinePatchTest extends GdxTest { 34 /** A string name for the type of test, and the NinePatch being tested. */ 35 private static class TestPatch { 36 public final String name; 37 public final NinePatch ninePatch; 38 39 TestPatch (String n) { 40 this.name = n; 41 this.ninePatch = NinePatchTest.newNinePatch(); 42 } 43 44 TestPatch (String n, NinePatch np) { 45 this.name = n; 46 this.ninePatch = np; 47 } 48 } 49 50 private OrthographicCamera camera; 51 private SpriteBatch b; 52 private Array<TestPatch> ninePatches = new Array<TestPatch>(10); 53 54 private final long start = System.currentTimeMillis(); 55 56 @Override 57 public void create () { 58 TestPatch tp; 59 60 // Create all the NinePatches to test 61 ninePatches.add(new TestPatch("default")); 62 63 tp = new TestPatch("20px width"); 64 int bWidth = 20; 65 tp.ninePatch.setLeftWidth(bWidth); 66 tp.ninePatch.setRightWidth(bWidth); 67 tp.ninePatch.setTopHeight(bWidth); 68 tp.ninePatch.setBottomHeight(bWidth); 69 ninePatches.add(tp); 70 71 tp = new TestPatch("fat left"); 72 tp.ninePatch.setLeftWidth(3 * tp.ninePatch.getRightWidth()); 73 ninePatches.add(tp); 74 75 tp = new TestPatch("fat top"); 76 tp.ninePatch.setTopHeight(3 * tp.ninePatch.getBottomHeight()); 77 ninePatches.add(tp); 78 79 tp = new TestPatch("degenerate", newDegenerateNinePatch()); 80 ninePatches.add(tp); 81 82 tp = new TestPatch("upper-left quad", newULQuadPatch()); 83 ninePatches.add(tp); 84 85 tp = new TestPatch("no middle row", newMidlessPatch()); 86 ninePatches.add(tp); 87 88 b = new SpriteBatch(); 89 } 90 91 // Make a new 'pixmapSize' square texture region with 'patchSize' patches in it. Each patch is a different color. 92 static TextureRegion newPatchPix (int patchSize, int pixmapSize) { 93 final int pixmapDim = MathUtils.nextPowerOfTwo(pixmapSize); 94 95 Pixmap p = new Pixmap(pixmapDim, pixmapDim, Pixmap.Format.RGBA8888); 96 p.setColor(1, 1, 1, 0); 97 p.fill(); 98 99 for (int x = 0; x < pixmapSize; x += patchSize) { 100 for (int y = 0; y < pixmapSize; y += patchSize) { 101 p.setColor(x / (float)pixmapSize, y / (float)pixmapSize, 1.0f, 1.0f); 102 p.fillRectangle(x, y, patchSize, patchSize); 103 } 104 } 105 106 return new TextureRegion(new Texture(p), pixmapSize, pixmapSize); 107 } 108 109 // Make a degenerate NinePatch 110 static NinePatch newDegenerateNinePatch () { 111 final int patchSize = 8; 112 final int pixmapSize = patchSize * 3; 113 TextureRegion tr = newPatchPix(patchSize, pixmapSize); 114 return new NinePatch(tr); 115 } 116 117 // Make a basic NinePatch with different colors in each of the nine patches 118 static NinePatch newNinePatch () { 119 final int patchSize = 8; 120 final int pixmapSize = patchSize * 3; 121 TextureRegion tr = newPatchPix(patchSize, pixmapSize); 122 123 return new NinePatch(tr, patchSize, patchSize, patchSize, patchSize); 124 } 125 126 // Make a upper-left "quad" patch (only 4 patches defined in the top-left corner of the ninepatch) 127 static NinePatch newULQuadPatch () { 128 final int patchSize = 8; 129 final int pixmapSize = patchSize * 2; 130 TextureRegion tr = newPatchPix(patchSize, pixmapSize); 131 132 return new NinePatch(tr, patchSize, 0, patchSize, 0); 133 } 134 135 // Make a ninepatch with no middle band, just top three and bottom three. 136 static NinePatch newMidlessPatch () { 137 final int patchSize = 8; 138 final int fullPatchHeight = patchSize * 2; 139 final int fullPatchWidth = patchSize * 3; 140 final int pixmapDim = MathUtils.nextPowerOfTwo(Math.max(fullPatchWidth, fullPatchHeight)); 141 142 Pixmap testPatch = new Pixmap(pixmapDim, pixmapDim, Pixmap.Format.RGBA8888); 143 testPatch.setColor(1, 1, 1, 0); 144 testPatch.fill(); 145 146 for (int x = 0; x < fullPatchWidth; x += patchSize) { 147 for (int y = 0; y < fullPatchHeight; y += patchSize) { 148 testPatch.setColor(x / (float)fullPatchWidth, y / (float)fullPatchHeight, 1.0f, 1.0f); 149 testPatch.fillRectangle(x, y, patchSize, patchSize); 150 } 151 } 152 153 return new NinePatch(new TextureRegion(new Texture(testPatch), fullPatchWidth, fullPatchHeight), patchSize, patchSize, 154 patchSize, patchSize); 155 } 156 157 private float timePassed = 0; 158 private final Color filterColor = new Color(); 159 private final Color oldColor = new Color(); 160 161 @Override 162 public void render () { 163 final int screenWidth = Gdx.graphics.getWidth(); 164 final int screenHeight = Gdx.graphics.getHeight(); 165 166 Gdx.gl.glClearColor(0, 0, 0, 0); 167 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 168 169 timePassed += Gdx.graphics.getDeltaTime(); 170 171 b.begin(); 172 final int sz = ninePatches.size; 173 final int XGAP = 10; 174 final int pheight = (int)((screenHeight * 0.5f) / ((sz + 1) / 2)); 175 int x = XGAP; 176 int y = 10; 177 178 // Test that batch color is applied to NinePatch 179 if (timePassed < 2) { 180 b.setColor(1, 1, 1, Interpolation.sine.apply(timePassed / 2f)); 181 } 182 183 // Test that the various nine patches render 184 for (int i = 0; i < sz; i += 2) { 185 int pwidth = (int)(0.44f * screenWidth); 186 187 final NinePatch np1 = ninePatches.get(i).ninePatch; 188 np1.draw(b, x, y, pwidth, pheight); 189 190 if (i + 1 < sz) { 191 final NinePatch np2 = ninePatches.get(i + 1).ninePatch; 192 final int x2 = x + pwidth + XGAP; 193 final int pwidth2 = screenWidth - XGAP - x2; 194 195 np2.draw(b, x2, y, pwidth2, pheight); 196 } 197 198 y += pheight + 2; 199 } 200 201 // Dim a np by setting its color. Also test sending same np to batch twice 202 NinePatch np = ninePatches.get(0).ninePatch; 203 oldColor.set(np.getColor()); 204 filterColor.set(0.3f, 0.3f, 0.3f, 1.0f); 205 np.setColor(filterColor); 206 np.draw(b, x, y, 100, 30); 207 np.setColor(oldColor); 208 209 b.end(); 210 } 211 212 @Override 213 public void resize (int width, int height) { 214 float ratio = ((float)Gdx.graphics.getWidth() / (float)Gdx.graphics.getHeight()); 215 int h = 10; 216 int w = (int)(h * ratio); 217 camera = new OrthographicCamera(w, h); 218 } 219 } 220