+++ /dev/null
-/*******************************************************************************\r
- * Copyright 2011 See AUTHORS file.\r
- * \r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- * \r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- * \r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- ******************************************************************************/\r
-\r
-package com.badlogic.gdx.scenes.scene2d.ui;\r
-\r
-import java.io.IOException;\r
-import java.io.Writer;\r
-\r
-import com.badlogic.gdx.Files.FileType;\r
-import com.badlogic.gdx.Gdx;\r
-import com.badlogic.gdx.files.FileHandle;\r
-import com.badlogic.gdx.graphics.Color;\r
-import com.badlogic.gdx.graphics.Texture;\r
-import com.badlogic.gdx.graphics.Texture.TextureFilter;\r
-import com.badlogic.gdx.graphics.g2d.BitmapFont;\r
-import com.badlogic.gdx.graphics.g2d.NinePatch;\r
-import com.badlogic.gdx.graphics.g2d.TextureRegion;\r
-import com.badlogic.gdx.scenes.scene2d.Actor;\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.scenes.scene2d.ui.Label.LabelStyle;\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
-import com.badlogic.gdx.utils.Json.Serializer;\r
-import com.badlogic.gdx.utils.JsonWriter.OutputType;\r
-import com.badlogic.gdx.utils.ObjectMap;\r
-import com.badlogic.gdx.utils.ObjectMap.Entry;\r
-import com.badlogic.gdx.utils.SerializationException;\r
-import com.badlogic.gwtref.client.Method;\r
-import com.badlogic.gwtref.client.ReflectionCache;\r
-\r
-/** A skin holds styles for widgets and the resources (texture regions, ninepatches, bitmap fonts, etc) for those styles. A skin\r
- * has a single texture that the resources may reference. This reduces the number of texture binds necessary for rendering many\r
- * different widgets.\r
- * <p>\r
- * The resources and styles for a skin are usually defined using JSON (or a format that is {@link OutputType#minimal JSON-like}),\r
- * which is formatted in this way:\r
- * \r
- * <pre>\r
- * {\r
- * resources: {\r
- * className: {\r
- * name: value,\r
- * ...\r
- * },\r
- * ...\r
- * },\r
- * styles: {\r
- * className: {\r
- * name: value,\r
- * ...\r
- * },\r
- * ...\r
- * }\r
- * }\r
- * </pre>\r
- * \r
- * There are two sections, one named "resources" and the other "styles". Each section has a class name, which has a number of\r
- * names and values. The name is the name of the resource or style for that class, and the value is the serialized resource or\r
- * style. Here is a real example:\r
- * \r
- * <pre>\r
- * {\r
- * resources: {\r
- * com.badlogic.gdx.graphics.g2d.TextureRegion: {\r
- * check-on: { x: 13, y: 77, width: 14, height: 14 },\r
- * check-off: { x: 2, y: 97, width: 14, height: 14 }\r
- * },\r
- * com.badlogic.gdx.graphics.Color: {\r
- * white: { r: 1, g: 1, b: 1, a: 1 }\r
- * },\r
- * com.badlogic.gdx.graphics.g2d.BitmapFont: {\r
- * default-font: { file: default.fnt }\r
- * }\r
- * },\r
- * styles: {\r
- * com.badlogic.gdx.scenes.scene2d.ui.CheckBox$CheckBoxStyle: {\r
- * default: {\r
- * checkboxOn: check-on, checkboxOff: check-off,\r
- * font: default-font, fontColor: white\r
- * }\r
- * }\r
- * }\r
- * }\r
- * </pre>\r
- * \r
- * Here some named resource are defined: texture regions, a color, and a bitmap font. Also, a {@link CheckBoxStyle} is defined\r
- * named "default" and it references the resources by name.\r
- * <p>\r
- * Styles and resources are retrieved from the skin using the type and name:\r
- * \r
- * <pre>\r
- * Color highlight = skin.getResource("highlight", Color.class);\r
- * TextureRegion someRegion = skin.getResource("logo", TextureRegion.class);\r
- * CheckBoxStyle checkBoxStyle = skin.getStyle("bigCheckbox", CheckBoxStyle.class);\r
- * CheckBox checkBox = new CheckBox("Check me!", checkBoxStyle);\r
- * </pre>\r
- * \r
- * For convenience, most widget constructors will accept a skin and look up the necessary style using the name "default".\r
- * <p>\r
- * The JSON required for a style is simply a JSON object with field names that match the Java field names. The JSON object's field\r
- * values can be an object to define a new Java object, or a string to reference a named resource of the expected type. Eg,\r
- * {@link LabelStyle} has two fields, font and fontColor, so the JSON could look like:\r
- * \r
- * <pre>\r
- * someLabel: { font: small, fontColor: { r: 1, g: 0, b: 0, a: 1 } }\r
- * </pre>\r
- * \r
- * When this is parsed, the "font" field is a BitmapFont and the string "small" is found, so a BitmapFont resource named "small"\r
- * is used. The "fontColor" field is a Color and a JSON object is found, so a new Color is created and the JSON object is used to\r
- * populate its fields.\r
- * <p>\r
- * The order resources are defined is important. Resources may reference previously defined resources. This is how a BitmapFont\r
- * can find a TextureRegion resource (see BitmapFont section below).\r
- * <p>\r
- * The following gives examples for the types of resources that are supported by default:\r
- * <p>\r
- * {@link Color}:\r
- * \r
- * <pre>\r
- * { r: 1, g: 1, b: 1, a: 1 }\r
- * </pre>\r
- * \r
- * {@link TextureRegion}:\r
- * \r
- * <pre>\r
- * { x: 13, y: 77, width: 14, height: 14 }\r
- * </pre>\r
- * \r
- * {@link NinePatch}:\r
- * \r
- * <pre>\r
- * [\r
- * { x: 2, y: 55, width: 5, height: 5 },\r
- * { x: 7, y: 55, width: 2, height: 5 },\r
- * { x: 9, y: 55, width: 5, height: 5 },\r
- * { x: 2, y: 60, width: 5, height: 11 },\r
- * { x: 7, y: 60, width: 2, height: 11 },\r
- * { x: 9, y: 60, width: 5, height: 11 },\r
- * { x: 2, y: 71, width: 5, height: 4 },\r
- * { x: 7, y: 71, width: 2, height: 4 },\r
- * { x: 9, y: 71, width: 5, height: 4 }\r
- * ]\r
- * </pre>\r
- * \r
- * {@link NinePatch} can also be specified as a single region, which is set as the center of the ninepatch:\r
- * \r
- * <pre>\r
- * [ { width: 20, height: 20, x: 6, y: 2 } ]\r
- * </pre>\r
- * \r
- * This notation is useful to use a single region as a ninepatch. Eg, when creating a button made up of a single image for the\r
- * {@link ButtonStyle#up} field, which is a ninepatch.\r
- * <p>\r
- * {@link BitmapFont}:\r
- * \r
- * <pre>\r
- * { file: default.fnt }\r
- * </pre>\r
- * \r
- * First the skin tries to find the font file in the directory containing the skin file. If not found there, it uses the specified\r
- * path as an {@link FileType#Internal} path. The bitmap font will use a texture region with the same name as the font file\r
- * without the file extension. If no texture region with that name is defined in the skin (note the order resources are defined is\r
- * important), it will look in the same directory as the font file for a PNG with the same name as the font file but with a "png"\r
- * file extension.\r
- * <p>\r
- * TintedNinePatch provides a mechanism for tinting an existing NinePatch:\r
- * \r
- * <pre>\r
- * { name: whiteButton, color: blue }\r
- * </pre>\r
- * \r
- * This would create a new NinePatch identical to the NinePatch named "whiteButton" and tint it with the color named "blue".\r
- * <p>\r
- * The skin JSON is extensible. Styles and resources for your own widgets may be included in the skin, usually without writing any\r
- * code. Deserialization is handled by the {@link Json} class, which automatically serializes and deserializes most objects. While\r
- * nearly any style object can be automatically deserialized, often resource objects require custom deserialization. Eg,\r
- * TextureRegion, BitmapFont, and NinePatch need to reference the skin's single texture. If needed,\r
- * {@link #getJsonLoader(FileHandle)} may be overridden to register additional custom {@link Serializer serializers}. See the\r
- * source for {@link Skin#getJsonLoader(FileHandle)} for examples on how to write serializers.\r
- * <p>\r
- * Note that there is a SkinPacker class in the gdx-tools project that can take a directory of individual images, pack them into a\r
- * single texture, and write the proper texture region and ninepatch entries to a skin JSON file. The styles and other resources\r
- * sections still need to be written by hand, but SkinPacker makes the otherwise tedious entry of pixel coordinates unnecessary.\r
- * @author Nathan Sweet */\r
-public class Skin implements Disposable {\r
- ObjectMap<Class, ObjectMap<String, Object>> resources = new ObjectMap();\r
- ObjectMap<Class, ObjectMap<String, Object>> styles = new ObjectMap();\r
- Texture texture;\r
-\r
- public Skin () {\r
- }\r
-\r
- public Skin (FileHandle skinFile) {\r
- texture = new Texture(skinFile.parent().child(skinFile.nameWithoutExtension() + ".png"));\r
- load(skinFile);\r
- }\r
-\r
- public Skin (FileHandle skinFile, FileHandle textureFile) {\r
- texture = new Texture(textureFile);\r
- load(skinFile);\r
- }\r
-\r
- public Skin (FileHandle skinFile, Texture texture) {\r
- this.texture = texture;\r
- texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);\r
- load(skinFile);\r
- }\r
-\r
- public void load (FileHandle skinFile) {\r
- try {\r
- getJsonLoader(skinFile).fromJson(Skin.class, skinFile);\r
- } catch (SerializationException ex) {\r
- throw new SerializationException("Error reading file: " + skinFile, ex);\r
- }\r
- }\r
-\r
- public void addResource (String name, Object resource) {\r
- if (name == null) throw new IllegalArgumentException("name cannot be null.");\r
- if (resource == null) throw new IllegalArgumentException("resource cannot be null.");\r
- ObjectMap<String, Object> typeResources = resources.get(resource.getClass());\r
- if (typeResources == null) {\r
- typeResources = new ObjectMap();\r
- resources.put(resource.getClass(), typeResources);\r
- }\r
- typeResources.put(name, resource);\r
- }\r
-\r
- public <T> T getResource (String name, Class<T> type) {\r
- if (name == null) throw new IllegalArgumentException("name cannot be null.");\r
- ObjectMap<String, Object> typeResources = resources.get(type);\r
- if (typeResources == null)\r
- throw new GdxRuntimeException("No " + type.getName() + " resource registered with name: " + name);\r
- Object resource = typeResources.get(name);\r
- if (resource == null) throw new GdxRuntimeException("No " + type.getName() + " resource registered with name: " + name);\r
- return (T)resource;\r
- }\r
-\r
- public boolean hasResource (String name, Class type) {\r
- ObjectMap<String, Object> typeResources = resources.get(type);\r
- if (typeResources == null) return false;\r
- Object resource = typeResources.get(name);\r
- if (resource == null) return false;\r
- return true;\r
- }\r
-\r
- public NinePatch getPatch (String name) {\r
- return getResource(name, NinePatch.class);\r
- }\r
-\r
- public Color getColor (String name) {\r
- return getResource(name, Color.class);\r
- }\r
-\r
- public BitmapFont getFont (String name) {\r
- return getResource(name, BitmapFont.class);\r
- }\r
-\r
- public TextureRegion getRegion (String name) {\r
- return getResource(name, TextureRegion.class);\r
- }\r
-\r
- public void addStyle (String name, Object style) {\r
- if (name == null) throw new IllegalArgumentException("name cannot be null.");\r
- if (style == null) throw new IllegalArgumentException("style cannot be null.");\r
- ObjectMap<String, Object> typeStyles = styles.get(style.getClass());\r
- if (typeStyles == null) {\r
- typeStyles = new ObjectMap();\r
- styles.put(style.getClass(), typeStyles);\r
- }\r
- typeStyles.put(name, style);\r
- }\r
-\r
- public <T> T getStyle (Class<T> type) {\r
- return getStyle("default", type);\r
- }\r
-\r
- public <T> T getStyle (String name, Class<T> type) {\r
- if (name == null) throw new IllegalArgumentException("name cannot be null.");\r
- ObjectMap<String, Object> typeStyles = styles.get(type);\r
- if (typeStyles == null) throw new GdxRuntimeException("No styles registered with type: " + type.getName());\r
- Object style = typeStyles.get(name);\r
- if (style == null) throw new GdxRuntimeException("No " + type.getName() + " style registered with name: " + name);\r
- return (T)style;\r
- }\r
-\r
- public boolean hasStyle (String name, Class type) {\r
- ObjectMap<String, Object> typeStyles = styles.get(type);\r
- if (typeStyles == null) return false;\r
- Object style = typeStyles.get(name);\r
- if (style == null) return false;\r
- return true;\r
- }\r
-\r
- /** Returns the name of the specified style object, or null if it is not in the skin. This compares potentially every style\r
- * object in the skin of the same type as the specified style, which may be a somewhat expensive operation. */\r
- public String findStyleName (Object style) {\r
- if (style == null) throw new IllegalArgumentException("style cannot be null.");\r
- ObjectMap<String, Object> typeStyles = styles.get(style.getClass());\r
- if (typeStyles == null) return null;\r
- return typeStyles.findKey(style, true);\r
- }\r
-\r
- /** Sets the style on the actor to disabled or enabled. This is done by appending "-disabled" to the style name when enabled is\r
- * false, and removing "-disabled" from the style name when enabled is true. A method named "getStyle" is called the actor via\r
- * reflection and the name of that style is found in the skin. If the actor doesn't have a "getStyle" method or the style was\r
- * not found in the skin, no exception is thrown and the actor is left unchanged. */\r
- public void setEnabled (Actor actor, boolean enabled) {\r
- actor.setTouchable(enabled);\r
- // Get current style.\r
- Method method = findMethod(actor.getClass(), "getStyle");\r
- if (method == null) return;\r
- Object style;\r
- try {\r
- style = method.invoke(actor);\r
- } catch (Exception ignored) {\r
- return;\r
- }\r
- // Determine new style.\r
- String name = findStyleName(style);\r
- if (name == null) return;\r
- name = name.replace("-disabled", "") + (enabled ? "" : "-disabled");\r
- style = getStyle(name, style.getClass());\r
- // Set new style.\r
- method = findMethod(actor.getClass(), "setStyle");\r
- if (method == null) return;\r
- try {\r
- method.invoke(actor, style);\r
- } catch (Exception ignored) {\r
- }\r
- }\r
-\r
- public NinePatch newTintedPatch (String patchName, String colorName) {\r
- return newTintedPatch(patchName, getColor(colorName));\r
- }\r
-\r
- public NinePatch newTintedPatch (String patchName, Color color) {\r
- return new NinePatch(getPatch(patchName), color);\r
- }\r
-\r
- public NinePatch newTintedRegion (String regionName, String colorName) {\r
- return newTintedRegion(regionName, getColor(colorName));\r
- }\r
-\r
- public NinePatch newTintedRegion (String regionName, Color color) {\r
- NinePatch patch = new NinePatch(getRegion(regionName));\r
- patch.setColor(color);\r
- return patch;\r
- }\r
-\r
- static private Method findMethod (Class type, String name) {\r
- Method[] methods = ReflectionCache.getType(type).getMethods();\r
- for (int i = 0, n = methods.length; i < n; i++) {\r
- Method method = methods[i];\r
- if (method.getName().equals(name)) return method;\r
- }\r
- return null;\r
- }\r
-\r
- public void setTexture (Texture texture) {\r
- this.texture = texture;\r
- }\r
-\r
- /** Returns the single {@link Texture} that all resources in this skin reference. */\r
- public Texture getTexture () {\r
- return texture;\r
- }\r
-\r
- /** Disposes the {@link Texture} and all {@link Disposable} resources of this Skin. */\r
- @Override\r
- public void dispose () {\r
- texture.dispose();\r
- for (Entry<Class, ObjectMap<String, Object>> entry : resources.entries()) {\r
- if (!ReflectionCache.getType(entry.key).isAssignableFrom(ReflectionCache.getType(Disposable.class))) continue;\r
- for (Object resource : entry.value.values())\r
- ((Disposable)resource).dispose();\r
- }\r
- }\r
-\r
- public void save (FileHandle skinFile) {\r
- String text = getJsonLoader(null).prettyPrint(this, 130);\r
- Writer writer = skinFile.writer(false);\r
- try {\r
- writer.write(text);\r
- writer.close();\r
- } catch (IOException ex) {\r
- throw new GdxRuntimeException(ex);\r
- }\r
- }\r
-\r
- protected Json getJsonLoader (final FileHandle skinFile) {\r
- final Skin skin = this;\r
-\r
- final Json json = new Json();\r
- json.setTypeName(null);\r
- json.setUsePrototypes(false);\r
-\r
- // Writes names of resources instead of objects.\r
- class AliasWriter implements Serializer {\r
- final ObjectMap<String, ?> map;\r
-\r
- public AliasWriter (Class type) {\r
- map = resources.get(type);\r
- }\r
-\r
- public void write (Json json, Object object, Class valueType) {\r
- for (Entry<String, ?> entry : map.entries()) {\r
- if (entry.value.equals(object)) {\r
- json.writeValue(entry.key);\r
- return;\r
- }\r
- }\r
- throw new SerializationException(object.getClass().getName() + " not found: " + object);\r
- }\r
-\r
- public Object read (Json json, Object jsonData, Class type) {\r
- throw new UnsupportedOperationException();\r
- }\r
- }\r
-\r
- json.setSerializer(Skin.class, new Serializer<Skin>() {\r
- public void write (Json json, Skin skin, Class valueType) {\r
- json.writeObjectStart();\r
- json.writeValue("resources", skin.resources);\r
- for (Entry<Class, ObjectMap<String, Object>> entry : resources.entries())\r
- json.setSerializer(entry.key, new AliasWriter(entry.key));\r
- json.writeField(skin, "styles");\r
- json.writeObjectEnd();\r
- }\r
-\r
- public Skin read (Json json, Object jsonData, Class ignored) {\r
- ObjectMap map = (ObjectMap)jsonData;\r
- readTypeMap(json, (ObjectMap)map.get("resources"), true);\r
- readTypeMap(json, (ObjectMap)map.get("styles"), false);\r
- return skin;\r
- }\r
-\r
- private void readTypeMap (Json json, ObjectMap<String, ObjectMap> typeToValueMap, boolean isResource) {\r
- if (typeToValueMap == null)\r
- throw new SerializationException("Skin file is missing a \"" + (isResource ? "resources" : "styles")\r
- + "\" section.");\r
- for (Entry<String, ObjectMap> typeEntry : typeToValueMap.entries()) {\r
- String className = typeEntry.key;\r
- ObjectMap<String, ObjectMap> valueMap = (ObjectMap)typeEntry.value;\r
- try {\r
- readNamedObjects(json, ReflectionCache.forName(className).getClassOfType(), valueMap, isResource);\r
- } catch (ClassNotFoundException ex) {\r
- throw new SerializationException(ex);\r
- }\r
- }\r
- }\r
-\r
- private void readNamedObjects (Json json, Class type, ObjectMap<String, ObjectMap> valueMap, boolean isResource) {\r
- for (Entry<String, ObjectMap> valueEntry : valueMap.entries()) {\r
- String name = valueEntry.key;\r
- Object object = json.readValue(type, valueEntry.value);\r
- if (object == null) continue;\r
- try {\r
- if (isResource)\r
- addResource(name, object);\r
- else\r
- addStyle(name, object);\r
- } catch (Exception ex) {\r
- throw new SerializationException("Error reading " + type.getName() + ": " + valueEntry.key, ex);\r
- }\r
- }\r
- }\r
- });\r
-\r
- json.setSerializer(TextureRegion.class, new Serializer<TextureRegion>() {\r
- public void write (Json json, TextureRegion region, Class valueType) {\r
- json.writeObjectStart();\r
- json.writeValue("x", region.getRegionX());\r
- json.writeValue("y", region.getRegionY());\r
- json.writeValue("width", region.getRegionWidth());\r
- json.writeValue("height", region.getRegionHeight());\r
- json.writeObjectEnd();\r
- }\r
-\r
- public TextureRegion read (Json json, Object jsonData, Class type) {\r
- if (jsonData instanceof String) return getResource((String)jsonData, TextureRegion.class);\r
- int x = json.readValue("x", int.class, jsonData);\r
- int y = json.readValue("y", int.class, jsonData);\r
- int width = json.readValue("width", int.class, jsonData);\r
- int height = json.readValue("height", int.class, jsonData);\r
- return new TextureRegion(skin.texture, x, y, width, height);\r
- }\r
- });\r
-\r
- json.setSerializer(BitmapFont.class, new Serializer<BitmapFont>() {\r
- public void write (Json json, BitmapFont font, Class valueType) {\r
- json.writeObjectStart();\r
- json.writeValue("file", font.getData().getFontFile().toString().replace('\\', '/'));\r
- json.writeObjectEnd();\r
- }\r
-\r
- public BitmapFont read (Json json, Object jsonData, Class type) {\r
- if (jsonData instanceof String) return getResource((String)jsonData, BitmapFont.class);\r
- String path = json.readValue("file", String.class, jsonData);\r
-\r
- FileHandle fontFile = skinFile.parent().child(path);\r
- if (!fontFile.exists()) fontFile = Gdx.files.internal(path);\r
- if (!fontFile.exists()) throw new SerializationException("Font file not found: " + fontFile);\r
-\r
- // Use a region with the same name as the font, else use a PNG file in the same directory as the FNT file.\r
- String regionName = fontFile.nameWithoutExtension();\r
- try {\r
- if (skin.hasResource(regionName, TextureRegion.class))\r
- return new BitmapFont(fontFile, skin.getResource(regionName, TextureRegion.class), false);\r
- else {\r
- FileHandle imageFile = fontFile.parent().child(regionName + ".png");\r
- if (imageFile.exists())\r
- return new BitmapFont(fontFile, imageFile, false);\r
- else\r
- return new BitmapFont(fontFile, false);\r
- }\r
- } catch (RuntimeException ex) {\r
- throw new SerializationException("Error loading bitmap font: " + fontFile, ex);\r
- }\r
- }\r
- });\r
-\r
- json.setSerializer(NinePatch.class, new Serializer<NinePatch>() {\r
- public void write (Json json, NinePatch ninePatch, Class valueType) {\r
- TextureRegion[] patches = ninePatch.getPatches();\r
- boolean singlePatch = patches[0] == null && patches[1] == null && patches[2] == null && patches[3] == null\r
- && patches[4] != null && patches[5] == null && patches[6] == null && patches[7] == null && patches[8] == null;\r
- if (ninePatch.getColor() != null) {\r
- json.writeObjectStart();\r
- json.writeValue("color", ninePatch.getColor());\r
- if (singlePatch)\r
- json.writeValue("region", patches[4]);\r
- else\r
- json.writeValue("regions", patches);\r
- json.writeObjectEnd();\r
- } else {\r
- if (singlePatch)\r
- json.writeValue(patches[4]);\r
- else\r
- json.writeValue(patches);\r
- }\r
- }\r
-\r
- public NinePatch read (Json json, Object jsonData, Class type) {\r
- if (jsonData instanceof String) return getResource((String)jsonData, NinePatch.class);\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
- ninePatch = new NinePatch(json.readValue(TextureRegion.class, jsonData));\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
- if (jsonData instanceof String) return getResource((String)jsonData, Color.class);\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
-\r
- json.setSerializer(TintedNinePatch.class, new Serializer() {\r
- public void write (Json json, Object tintedPatch, Class valueType) {\r
- json.writeObjectStart();\r
- json.writeField(tintedPatch, "name");\r
- json.writeField(tintedPatch, "color");\r
- json.writeObjectEnd();\r
- }\r
-\r
- public Object read (Json json, Object jsonData, Class type) {\r
- String name = json.readValue("name", String.class, jsonData);\r
- Color color = json.readValue("color", Color.class, jsonData);\r
- return new NinePatch(getResource(name, NinePatch.class), color);\r
- }\r
- });\r
-\r
- return json;\r
- }\r
-\r
- static public class TintedNinePatch extends NinePatch {\r
- public String name;\r
- public Color color;\r
-\r
- public TintedNinePatch (NinePatch ninePatch, Color color) {\r
- super(ninePatch, color);\r
- }\r
- }\r
-}\r