List of usage examples for com.badlogic.gdx.utils Json readValue
public <T> T readValue(Class<T> type, JsonValue jsonData)
From source file:com.jupiter.europa.entity.component.SkillsComponent.java
@Override public void read(Json json, JsonValue jsonData) { if (jsonData.has(SKILL_SET_KEY)) { try {/* w ww .j a v a 2 s .c om*/ this.skills = json.readValue(SkillSet.class, jsonData.get(SKILL_SET_KEY)); } catch (Exception ex) { this.skills = new SkillSet(); } } else { this.skills = new SkillSet(); } if (jsonData.has(CLASS_SKILLS_KEY)) { this.classSkills.clear(); jsonData.get(CLASS_SKILLS_KEY).iterator().forEach((JsonValue value) -> { try { this.classSkills.add(json.readValue(Skills.class, value)); } catch (Exception ex) { } }); } }
From source file:com.lyeeedar.Graphics.ParticleEffects.ParticleEmitter.java
License:Open Source License
/** * Get a json instance set up for reading and writing a particle emitter * @return// ww w .ja v a 2s . com */ public static Json getJson(Json json) { json.setSerializer(ParticleEmitter.class, new Json.Serializer<ParticleEmitter>() { @SuppressWarnings("rawtypes") public void write(Json json, ParticleEmitter emitter, Class knownType) { emitter.write(json); } @SuppressWarnings({ "unchecked", "rawtypes" }) public ParticleEmitter read(Json json, Object jsonData, Class type) { // ----- Particle Parameters ----- // TimelineValue[] sprite = null; TimelineValue[] size = null; TimelineValue[] colour = null; TimelineValue[] velocity = null; // ----- End Particle Parameters ----- // // ----- Emitter parameters ----- // String name = null; int maxParticles = 0; float particleLifetime = 0; float particleLifetimeVar = 0; float emissionTime = 0; float ex = 0, ey = 0, ez = 0; int emissionType = 0; int blendFuncSRC = 0; int blendFuncDST = 0; String atlasName = null; // ----- End Emitter parameters ----- // // ----- Light ----- // float lightAttenuation = 0; float lightPower = 0; boolean isLightStatic = false; Color lightColour = null; boolean lightFlicker = false; float lightx = 0, lighty = 0, lightz = 0; // ----- End Light ----- // OrderedMap<String, Object> jsonMap = (OrderedMap<String, Object>) jsonData; Iterator<Entry<String, Object>> itr = jsonMap.entries(); while (itr.hasNext()) { Entry<String, Object> entry = itr.next(); if (entry.key.equals("sprite")) { sprite = json.readValue(TimelineValue[].class, entry.value); } else if (entry.key.equals("size")) { size = json.readValue(TimelineValue[].class, entry.value); } else if (entry.key.equals("colour")) { colour = json.readValue(TimelineValue[].class, entry.value); } else if (entry.key.equals("velocity")) { velocity = json.readValue(TimelineValue[].class, entry.value); } else if (entry.key.equals("name")) { name = (String) entry.value; } else if (entry.key.equals("max particles")) { maxParticles = ((Float) entry.value).intValue(); } else if (entry.key.equals("particle lifetime")) { particleLifetime = (Float) entry.value; } else if (entry.key.equals("particle lifetime variance")) { particleLifetimeVar = (Float) entry.value; } else if (entry.key.equals("emission time")) { emissionTime = (Float) entry.value; } else if (entry.key.equals("emission x")) { ex = (Float) entry.value; } else if (entry.key.equals("emission y")) { ey = (Float) entry.value; } else if (entry.key.equals("emission z")) { ez = (Float) entry.value; } else if (entry.key.equals("emission type")) { emissionType = ((Float) entry.value).intValue(); } else if (entry.key.equals("blend func SRC")) { blendFuncSRC = ((Float) entry.value).intValue(); } else if (entry.key.equals("blend func DST")) { blendFuncDST = ((Float) entry.value).intValue(); } else if (entry.key.equals("atlas name")) { atlasName = (String) entry.value; } else if (entry.key.equals("light attenuation")) { lightAttenuation = (Float) entry.value; } else if (entry.key.equals("light power")) { lightPower = (Float) entry.value; } else if (entry.key.equals("light static")) { isLightStatic = (Boolean) entry.value; } else if (entry.key.equals("light flicker")) { lightFlicker = (Boolean) entry.value; } else if (entry.key.equals("light colour")) { lightColour = json.readValue(Color.class, entry.value); } else if (entry.key.equals("light offset x")) { lightx = (Float) entry.value; } else if (entry.key.equals("light offset y")) { lighty = (Float) entry.value; } else if (entry.key.equals("light offset z")) { lightz = (Float) entry.value; } } ParticleEmitter emitter = new ParticleEmitter(particleLifetime, particleLifetimeVar, emissionTime, ex, ey, ez, emissionType, blendFuncSRC, blendFuncDST, atlasName, name); emitter.maxParticles = maxParticles; emitter.setTimeline(sprite, size, colour, velocity); if (lightColour != null) emitter.addLight(isLightStatic, lightAttenuation, lightPower, lightColour, lightFlicker, lightx, lighty, lightz); return emitter; } }); return json; }
From source file:org.ege.widget.StyleAtlas.java
License:Apache License
protected Json getJsonLoader(final FileHandle skinFile) { final Skin skin = this; final Json json = new Json() { public <T> T readValue(Class<T> type, Class elementType, Object jsonData) { // If the JSON is a string but the type is not, look up the actual value by name. if (jsonData instanceof String && !CharSequence.class.isAssignableFrom(type)) return get((String) jsonData, type); return super.readValue(type, elementType, jsonData); }/* www . j a v a 2 s.co m*/ }; json.setTypeName(null); json.setUsePrototypes(false); /* ------------------------------------------------ */ json.setSerializer(Skin.class, new ReadOnlySerializer<Skin>() { public Skin read(Json json, Object jsonData, Class ignored) { ObjectMap<String, ObjectMap> typeToValueMap = (ObjectMap) jsonData; for (Entry<String, ObjectMap> typeEntry : typeToValueMap.entries()) { String className = typeEntry.key; ObjectMap<String, ObjectMap> valueMap = (ObjectMap) typeEntry.value; try { readNamedObjects(json, Class.forName(className), valueMap); } catch (ClassNotFoundException ex) { throw new SerializationException(ex); } } return skin; } private void readNamedObjects(Json json, Class type, ObjectMap<String, ObjectMap> valueMap) { Class addType = type == TintedDrawable.class ? Drawable.class : type; for (Entry<String, ObjectMap> valueEntry : valueMap.entries()) { String name = valueEntry.key; Object object = json.readValue(type, valueEntry.value); if (object == null) continue; try { add(name, object, addType); } catch (Exception ex) { throw new SerializationException( "Error reading " + type.getSimpleName() + ": " + valueEntry.key, ex); } } } }); /* ------------------------------------------------ */ json.setSerializer(BitmapFont.class, new ReadOnlySerializer<BitmapFont>() { public BitmapFont read(Json json, Object jsonData, Class type) { String path = json.readValue("file", String.class, jsonData); FileHandle fontFile = skinFile.parent().child(path); if (!fontFile.exists()) fontFile = Gdx.files.internal(path); if (!fontFile.exists()) throw new SerializationException("Font file not found: " + fontFile); // Use a region with the same name as the font, else use a PNG file in the same directory as the FNT file. String regionName = fontFile.nameWithoutExtension(); try { TextureRegion region = skin.optional(regionName, TextureRegion.class); if (region != null) return new BitmapFont(fontFile, region, false); else { FileHandle imageFile = fontFile.parent().child(regionName + ".png"); if (imageFile.exists()) return new BitmapFont(fontFile, imageFile, false); else return new BitmapFont(fontFile, false); } } catch (RuntimeException ex) { throw new SerializationException("Error loading bitmap font: " + fontFile, ex); } } }); /* ------------------------------------------------ */ json.setSerializer(Color.class, new ReadOnlySerializer<Color>() { public Color read(Json json, Object jsonData, Class type) { if (jsonData instanceof String) return get((String) jsonData, Color.class); ObjectMap map = (ObjectMap) jsonData; float r = json.readValue("r", float.class, 0f, jsonData); float g = json.readValue("g", float.class, 0f, jsonData); float b = json.readValue("b", float.class, 0f, jsonData); float a = json.readValue("a", float.class, 1f, jsonData); return new Color(r, g, b, a); } }); /* ------------------------------------------------ */ json.setSerializer(TintedDrawable.class, new ReadOnlySerializer() { public Object read(Json json, Object jsonData, Class type) { String name = json.readValue("name", String.class, jsonData); Color color = json.readValue("color", Color.class, jsonData); return newDrawable(name, color); } }); /* ------------------------------------------------ */ // json.setSerializer(Orientation.class, new Serializer<Orientation>() { // // @Override // public void write (Json json, Orientation object, Class knownType) { // json.writeObjectStart(); // switch (object) { // case HORIZONTAL: // json.writeValue("orientation", "HORIZONTAL"); // break; // case VERTICAL: // json.writeValue("orientation", "VERTICAL"); // break; // case LANDSCAPE: // json.writeValue("orientation", "LANDSCAPE"); // break; // case PORTRAIT: // json.writeValue("orientation", "PORTRAIT"); // break; // } // json.writeObjectEnd(); // } // // @Override // public Orientation read (Json json, Object jsonData, Class type) { // String ori = (String)jsonData; // ori = ori.toLowerCase(); // if(ori.equals("landscape")) // return Orientation.LANDSCAPE; // else if(ori.equals("portrait")) // return Orientation.PORTRAIT; // else if(ori.equals("vertical")) // return Orientation.VERTICAL; // return Orientation.HORIZONTAL; // } // // }); /* ------------------------------------------------ */ json.setSerializer(Attributes.class, new ReadOnlySerializer<Attributes>() { @Override public Attributes read(Json json, Object jsonData, Class type) { float startX = json.readValue("startX", float.class, (float) 0, jsonData); float startY = json.readValue("startY", float.class, (float) 0, jsonData); float dstX = json.readValue("x", float.class, jsonData); float dstY = json.readValue("y", float.class, jsonData); float width = json.readValue("width", float.class, jsonData); float height = json.readValue("height", float.class, jsonData); Attributes attr = new Attributes(); attr.startX = startX; attr.startY = startY; attr.x = dstX; attr.y = dstY; attr.width = width; attr.height = height; return attr; } }); /* ------------------------------------------------ */ json.setSerializer(ScalingSet.class, new ReadOnlySerializer<ScalingSet>() { @Override public ScalingSet read(Json json, Object jsonData, Class type) { float layoutx = json.readValue("layoutx", float.class, jsonData); float layouty = json.readValue("layouty", float.class, jsonData); float layoutwidth = json.readValue("layoutwidth", float.class, jsonData); float layoutheight = json.readValue("layoutheight", float.class, jsonData); float whratio = json.readValue("whratio", float.class, jsonData); float hwratio = json.readValue("hwratio", float.class, jsonData); return new ScalingSet().set(layoutx, layouty, layoutwidth, layoutheight, whratio, hwratio); } }); /* ------------------------------------------------ */ json.setSerializer(Vector2.class, new ReadOnlySerializer<Vector2>() { @Override public Vector2 read(Json json, Object jsonData, Class type) { return new Vector2(json.readValue("x", float.class, jsonData), json.readValue("y", float.class, jsonData)); } }); return json; }
From source file:stu.tnt.gdx.widget.StyleAtlas.java
License:Apache License
protected Json getJsonLoader(final FileHandle skinFile) { final Skin skin = this; final Json json = new Json() { public <T> T readValue(Class<T> type, Class elementType, JsonValue jsonData) { // If the JSON is a string but the type is not, look up the // actual value by name. if (jsonData.isString() && !ClassReflection.isAssignableFrom(CharSequence.class, type)) return get(jsonData.asString(), type); return super.readValue(type, elementType, jsonData); }/* w w w . j a v a 2 s . com*/ }; json.setTypeName(null); json.setUsePrototypes(false); /* ------------------------------------------------ */ json.setSerializer(Skin.class, new ReadOnlySerializer<Skin>() { public Skin read(Json json, JsonValue typeToValueMap, Class ignored) { for (JsonValue valueMap = typeToValueMap.child(); valueMap != null; valueMap = valueMap.next()) { try { readNamedObjects(json, ClassReflection.forName(valueMap.name()), valueMap); } catch (ReflectionException ex) { throw new SerializationException(ex); } } return skin; } private void readNamedObjects(Json json, Class type, JsonValue valueMap) { Class addType = type == TintedDrawable.class ? Drawable.class : type; for (JsonValue valueEntry = valueMap.child(); valueEntry != null; valueEntry = valueEntry.next()) { Object object = json.readValue(type, valueEntry); if (object == null) continue; try { add(valueEntry.name(), object, addType); } catch (Exception ex) { throw new SerializationException( "Error reading " + ClassReflection.getSimpleName(type) + ": " + valueEntry.name(), ex); } } } }); json.setSerializer(BitmapFont.class, new ReadOnlySerializer<BitmapFont>() { public BitmapFont read(Json json, JsonValue jsonData, Class type) { String path = json.readValue("file", String.class, jsonData); FileHandle fontFile = skinFile.parent().child(path); if (!fontFile.exists()) fontFile = Gdx.files.internal(path); if (!fontFile.exists()) throw new SerializationException("Font file not found: " + fontFile); // Use a region with the same name as the font, else use // a PNG file in the same directory as the FNT file. String regionName = fontFile.nameWithoutExtension(); try { TextureRegion region = skin.optional(regionName, TextureRegion.class); if (region != null) return new BitmapFont(fontFile, region, false); else { FileHandle imageFile = fontFile.parent().child(regionName + ".png"); if (imageFile.exists()) return new BitmapFont(fontFile, imageFile, false); else return new BitmapFont(fontFile, false); } } catch (RuntimeException ex) { throw new SerializationException("Error loading bitmap font: " + fontFile, ex); } } }); json.setSerializer(Color.class, new ReadOnlySerializer<Color>() { public Color read(Json json, JsonValue jsonData, Class type) { if (jsonData.isString()) return get(jsonData.asString(), Color.class); String hex = json.readValue("hex", String.class, (String) null, jsonData); if (hex != null) return Color.valueOf(hex); float r = json.readValue("r", float.class, 0f, jsonData); float g = json.readValue("g", float.class, 0f, jsonData); float b = json.readValue("b", float.class, 0f, jsonData); float a = json.readValue("a", float.class, 1f, jsonData); return new Color(r, g, b, a); } }); json.setSerializer(TintedDrawable.class, new ReadOnlySerializer() { public Object read(Json json, JsonValue jsonData, Class type) { String name = json.readValue("name", String.class, jsonData); Color color = json.readValue("color", Color.class, jsonData); return newDrawable(name, color); } }); /* ------------------------------------------------ */ json.setSerializer(Attributes.class, new ReadOnlySerializer<Attributes>() { @Override public Attributes read(Json json, JsonValue jsonData, Class type) { float startX = json.readValue("startX", float.class, (float) 0, jsonData); float startY = json.readValue("startY", float.class, (float) 0, jsonData); float dstX = json.readValue("x", float.class, jsonData); float dstY = json.readValue("y", float.class, jsonData); float width = json.readValue("width", float.class, jsonData); float height = json.readValue("height", float.class, jsonData); Attributes attr = new Attributes(); attr.startX = startX; attr.startY = startY; attr.x = dstX; attr.y = dstY; attr.width = width; attr.height = height; return attr; } }); /* ------------------------------------------------ */ json.setSerializer(ScalingSet.class, new ReadOnlySerializer<ScalingSet>() { @Override public ScalingSet read(Json json, JsonValue jsonData, Class type) { float layoutx = json.readValue("layoutx", float.class, jsonData); float layouty = json.readValue("layouty", float.class, jsonData); float layoutwidth = json.readValue("layoutwidth", float.class, jsonData); float layoutheight = json.readValue("layoutheight", float.class, jsonData); float whratio = json.readValue("whratio", float.class, jsonData); float hwratio = json.readValue("hwratio", float.class, jsonData); return new ScalingSet().set(layoutx, layouty, layoutwidth, layoutheight, whratio, hwratio); } }); /* ------------------------------------------------ */ json.setSerializer(Vector2.class, new ReadOnlySerializer<Vector2>() { @Override public Vector2 read(Json json, JsonValue jsonData, Class type) { return new Vector2(json.readValue("x", float.class, jsonData), json.readValue("y", float.class, jsonData)); } }); return json; }