Example usage for com.badlogic.gdx.graphics Color valueOf

List of usage examples for com.badlogic.gdx.graphics Color valueOf

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics Color valueOf.

Prototype

public static Color valueOf(String hex) 

Source Link

Document

Returns a new color from a hex string with the format RRGGBBAA.

Usage

From source file:com.agateau.ui.UiBuilder.java

License:Apache License

protected ImageButton createImageButton(XmlReader.Element element) {
    String styleName = element.getAttribute("style", "default");
    ImageButton.ImageButtonStyle style = new ImageButton.ImageButtonStyle(
            mSkin.get(styleName, ImageButton.ImageButtonStyle.class));
    String imageName = element.getAttribute("imageName", "");
    if (!imageName.isEmpty()) {
        style.imageUp = mSkin.getDrawable(imageName);
    }//w  w  w.j a va2  s  .c  o  m
    ImageButton button = new ImageButton(style);
    String imageColor = element.getAttribute("imageColor", "");
    if (!imageColor.isEmpty()) {
        Color color = Color.valueOf(imageColor);
        button.getImage().setColor(color);
    }
    return button;
}

From source file:com.company.minery.utils.spine.SkeletonJson.java

License:Open Source License

private void readAnimation(String name, JsonValue map, SkeletonData skeletonData) {
    float scale = this.scale;
    Array<Timeline> timelines = new Array();
    float duration = 0;

    // Slot timelines.
    for (JsonValue slotMap = map.getChild("slots"); slotMap != null; slotMap = slotMap.next) {
        int slotIndex = skeletonData.findSlotIndex(slotMap.name);
        if (slotIndex == -1)
            throw new SerializationException("Slot not found: " + slotMap.name);

        for (JsonValue timelineMap = slotMap.child; timelineMap != null; timelineMap = timelineMap.next) {
            String timelineName = timelineMap.name;
            if (timelineName.equals("color")) {
                ColorTimeline timeline = new ColorTimeline(timelineMap.size);
                timeline.slotIndex = slotIndex;

                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) {
                    Color color = Color.valueOf(valueMap.getString("color"));
                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), color.r, color.g, color.b,
                            color.a);//  w  w w.  jav a  2  s  .com
                    readCurve(timeline, frameIndex, valueMap);
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 5 - 5]);

            } else if (timelineName.equals("attachment")) {
                AttachmentTimeline timeline = new AttachmentTimeline(timelineMap.size);
                timeline.slotIndex = slotIndex;

                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next)
                    timeline.setFrame(frameIndex++, valueMap.getFloat("time"), valueMap.getString("name"));
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
            } else
                throw new RuntimeException(
                        "Invalid timeline type for a slot: " + timelineName + " (" + slotMap.name + ")");
        }
    }

    // Bone timelines.
    for (JsonValue boneMap = map.getChild("bones"); boneMap != null; boneMap = boneMap.next) {
        int boneIndex = skeletonData.findBoneIndex(boneMap.name);
        if (boneIndex == -1)
            throw new SerializationException("Bone not found: " + boneMap.name);

        for (JsonValue timelineMap = boneMap.child; timelineMap != null; timelineMap = timelineMap.next) {
            String timelineName = timelineMap.name;
            if (timelineName.equals("rotate")) {
                RotateTimeline timeline = new RotateTimeline(timelineMap.size);
                timeline.boneIndex = boneIndex;

                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) {
                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), valueMap.getFloat("angle"));
                    readCurve(timeline, frameIndex, valueMap);
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 2 - 2]);

            } else if (timelineName.equals("translate") || timelineName.equals("scale")) {
                TranslateTimeline timeline;
                float timelineScale = 1;
                if (timelineName.equals("scale"))
                    timeline = new ScaleTimeline(timelineMap.size);
                else {
                    timeline = new TranslateTimeline(timelineMap.size);
                    timelineScale = scale;
                }
                timeline.boneIndex = boneIndex;

                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) {
                    float x = valueMap.getFloat("x", 0), y = valueMap.getFloat("y", 0);
                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), x * timelineScale,
                            y * timelineScale);
                    readCurve(timeline, frameIndex, valueMap);
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]);

            } else if (timelineName.equals("flipX") || timelineName.equals("flipY")) {
                boolean x = timelineName.equals("flipX");
                FlipXTimeline timeline = x ? new FlipXTimeline(timelineMap.size)
                        : new FlipYTimeline(timelineMap.size);
                timeline.boneIndex = boneIndex;

                String field = x ? "x" : "y";
                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) {
                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), valueMap.getBoolean(field, false));
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 2 - 2]);

            } else
                throw new RuntimeException(
                        "Invalid timeline type for a bone: " + timelineName + " (" + boneMap.name + ")");
        }
    }

    // IK timelines.
    for (JsonValue ikMap = map.getChild("ik"); ikMap != null; ikMap = ikMap.next) {
        IkConstraintData ikConstraint = skeletonData.findIkConstraint(ikMap.name);
        IkConstraintTimeline timeline = new IkConstraintTimeline(ikMap.size);
        timeline.ikConstraintIndex = skeletonData.getIkConstraints().indexOf(ikConstraint, true);
        int frameIndex = 0;
        for (JsonValue valueMap = ikMap.child; valueMap != null; valueMap = valueMap.next) {
            timeline.setFrame(frameIndex, valueMap.getFloat("time"), valueMap.getFloat("mix"),
                    valueMap.getBoolean("bendPositive") ? 1 : -1);
            readCurve(timeline, frameIndex, valueMap);
            frameIndex++;
        }
        timelines.add(timeline);
        duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]);
    }

    // FFD timelines.
    for (JsonValue ffdMap = map.getChild("ffd"); ffdMap != null; ffdMap = ffdMap.next) {
        Skin skin = skeletonData.findSkin(ffdMap.name);
        if (skin == null)
            throw new SerializationException("Skin not found: " + ffdMap.name);
        for (JsonValue slotMap = ffdMap.child; slotMap != null; slotMap = slotMap.next) {
            int slotIndex = skeletonData.findSlotIndex(slotMap.name);
            if (slotIndex == -1)
                throw new SerializationException("Slot not found: " + slotMap.name);
            for (JsonValue meshMap = slotMap.child; meshMap != null; meshMap = meshMap.next) {
                FfdTimeline timeline = new FfdTimeline(meshMap.size);
                Attachment attachment = skin.getAttachment(slotIndex, meshMap.name);
                if (attachment == null)
                    throw new SerializationException("FFD attachment not found: " + meshMap.name);
                timeline.slotIndex = slotIndex;
                timeline.attachment = attachment;

                int vertexCount;
                if (attachment instanceof MeshAttachment)
                    vertexCount = ((MeshAttachment) attachment).getVertices().length;
                else
                    vertexCount = ((SkinnedMeshAttachment) attachment).getWeights().length / 3 * 2;

                int frameIndex = 0;
                for (JsonValue valueMap = meshMap.child; valueMap != null; valueMap = valueMap.next) {
                    float[] vertices;
                    JsonValue verticesValue = valueMap.get("vertices");
                    if (verticesValue == null) {
                        if (attachment instanceof MeshAttachment)
                            vertices = ((MeshAttachment) attachment).getVertices();
                        else
                            vertices = new float[vertexCount];
                    } else {
                        vertices = new float[vertexCount];
                        int start = valueMap.getInt("offset", 0);
                        System.arraycopy(verticesValue.asFloatArray(), 0, vertices, start, verticesValue.size);
                        if (scale != 1) {
                            for (int i = start, n = i + verticesValue.size; i < n; i++)
                                vertices[i] *= scale;
                        }
                        if (attachment instanceof MeshAttachment) {
                            float[] meshVertices = ((MeshAttachment) attachment).getVertices();
                            for (int i = 0; i < vertexCount; i++)
                                vertices[i] += meshVertices[i];
                        }
                    }

                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), vertices);
                    readCurve(timeline, frameIndex, valueMap);
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
            }
        }
    }

    // Draw order timeline.
    JsonValue drawOrdersMap = map.get("drawOrder");
    if (drawOrdersMap == null)
        drawOrdersMap = map.get("draworder");
    if (drawOrdersMap != null) {
        DrawOrderTimeline timeline = new DrawOrderTimeline(drawOrdersMap.size);
        int slotCount = skeletonData.slots.size;
        int frameIndex = 0;
        for (JsonValue drawOrderMap = drawOrdersMap.child; drawOrderMap != null; drawOrderMap = drawOrderMap.next) {
            int[] drawOrder = null;
            JsonValue offsets = drawOrderMap.get("offsets");
            if (offsets != null) {
                drawOrder = new int[slotCount];
                for (int i = slotCount - 1; i >= 0; i--)
                    drawOrder[i] = -1;
                int[] unchanged = new int[slotCount - offsets.size];
                int originalIndex = 0, unchangedIndex = 0;
                for (JsonValue offsetMap = offsets.child; offsetMap != null; offsetMap = offsetMap.next) {
                    int slotIndex = skeletonData.findSlotIndex(offsetMap.getString("slot"));
                    if (slotIndex == -1)
                        throw new SerializationException("Slot not found: " + offsetMap.getString("slot"));
                    // Collect unchanged items.
                    while (originalIndex != slotIndex)
                        unchanged[unchangedIndex++] = originalIndex++;
                    // Set changed items.
                    drawOrder[originalIndex + offsetMap.getInt("offset")] = originalIndex++;
                }
                // Collect remaining unchanged items.
                while (originalIndex < slotCount)
                    unchanged[unchangedIndex++] = originalIndex++;
                // Fill in unchanged items.
                for (int i = slotCount - 1; i >= 0; i--)
                    if (drawOrder[i] == -1)
                        drawOrder[i] = unchanged[--unchangedIndex];
            }
            timeline.setFrame(frameIndex++, drawOrderMap.getFloat("time"), drawOrder);
        }
        timelines.add(timeline);
        duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
    }

    // Event timeline.
    JsonValue eventsMap = map.get("events");
    if (eventsMap != null) {
        EventTimeline timeline = new EventTimeline(eventsMap.size);
        int frameIndex = 0;
        for (JsonValue eventMap = eventsMap.child; eventMap != null; eventMap = eventMap.next) {
            EventData eventData = skeletonData.findEvent(eventMap.getString("name"));
            if (eventData == null)
                throw new SerializationException("Event not found: " + eventMap.getString("name"));
            Event event = new Event(eventData);
            event.intValue = eventMap.getInt("int", eventData.getInt());
            event.floatValue = eventMap.getFloat("float", eventData.getFloat());
            event.stringValue = eventMap.getString("string", eventData.getString());
            timeline.setFrame(frameIndex++, eventMap.getFloat("time"), event);
        }
        timelines.add(timeline);
        duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
    }

    timelines.shrink();
    skeletonData.animations.add(new Animation(name, timelines, duration));
}

From source file:es.eucm.ead.editor.editorui.HexToRGB.java

License:Open Source License

public static void main(String args[]) {
    String hex = args[0] + "ff";
    Color color = Color.valueOf(hex);
    System.out.println("            \"r\": " + color.r + ",\n" + "            \"g\": " + color.g + ",\n"
            + "            \"b\": " + color.b + ",\n" + "            \"a\": 1");
}

From source file:me.scarlet.undertailor.gfx.text.TextComponent.java

License:Open Source License

/**
 * Internal method./*  ww  w  .j  ava  2  s. c o m*/
 * 
 * <p>Applies the parameters found on a single
 * {@link TextPiece} onto the provided {@link Builder}
 * .</p>
 * 
 * @param builder the Builder to apply parameters to
 * @param tailor the current Undertailor instance
 * @param piece the TextPiece to read from
 */
protected static void applyParameters(Builder builder, Undertailor tailor, TextPiece piece) {
    piece.getParams().entries().forEach(entry -> {
        TextParam paramType = entry.key;
        String value = entry.value;
        if (value == null || value.trim().isEmpty()) {
            return;
        }

        switch (paramType) {
        case FONT:
            Font font = tailor.getAssetManager().getFontManager().getFont(value);
            if (font == null && (value != null && !value.trim().isEmpty()))
                logger.warn("Couldn't find font " + value + " to assign to text");
            builder.setFont(font);
            break;
        case SOUND:
            Sound sound = tailor.getAssetManager().getAudioManager().getSound(value);
            if (sound == null)
                logger.warn("Couldn't find sound " + value + " to assign to text");
            builder.setSound(sound);
            break;
        case STYLE:
            String[] split = value.split(",");
            for (String styleValue : split) {
                TextStyle style = tailor.getAssetManager().getStyleManager().getStyle(styleValue);
                if (style == null)
                    logger.warn("Couldn't find style " + value + " to assign to text");
                builder.addStyles(style);
            }

            break;
        case COLOR:
        case DELAY:
        case SEGMENTSIZE:
        case SPEED:
            try {
                if (paramType == TextParam.COLOR) {
                    String[] rgb = value.trim().split(",");
                    if (rgb.length > 1) {
                        Color color = new Color();
                        color.set(Float.valueOf(rgb[0]), Float.valueOf(rgb[1]), Float.valueOf(rgb[2]),
                                rgb.length >= 4 ? Float.valueOf(rgb[3]) : 255F);
                    } else {
                        if (!value.startsWith("#")) {
                            value = "#" + value;
                        }

                        if (value.length() < 7) {
                            throw new IllegalArgumentException();
                        }

                        builder.setColor(Color.valueOf(value));
                    }
                } else {
                    if (paramType == TextParam.DELAY) {
                        builder.setDelay(Float.valueOf(value));
                    } else if (paramType == TextParam.SEGMENTSIZE) {
                        builder.setSegmentSize(Integer.valueOf(value));
                    } else if (paramType == TextParam.SPEED) {
                        builder.setSpeed(Float.valueOf(value));
                    }
                }
            } catch (Exception e) {
                logger.warn("Invalid value " + value + " for parameter " + paramType.toString());
            }

            break;
        default:
            break;
        }
    });
}

From source file:spine.SkeletonJson.java

License:Open Source License

private void readAnimation(String name, JsonValue map, SkeletonData skeletonData) {
    float scale = this.scale;
    Array<Timeline> timelines = new Array();
    float duration = 0;

    // Slot timelines.
    for (JsonValue slotMap = map.getChild("slots"); slotMap != null; slotMap = slotMap.next) {
        int slotIndex = skeletonData.findSlotIndex(slotMap.name);
        if (slotIndex == -1)
            throw new SerializationException("Slot not found: " + slotMap.name);

        for (JsonValue timelineMap = slotMap.child; timelineMap != null; timelineMap = timelineMap.next) {
            String timelineName = timelineMap.name;
            if (timelineName.equals("color")) {
                ColorTimeline timeline = new ColorTimeline(timelineMap.size);
                timeline.slotIndex = slotIndex;

                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) {
                    Color color = Color.valueOf(valueMap.getString("color"));
                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), color.r, color.g, color.b,
                            color.a);/*from w ww .j a  va 2  s.  c  o  m*/
                    readCurve(timeline, frameIndex, valueMap);
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 5 - 5]);

            } else if (timelineName.equals("attachment")) {
                AttachmentTimeline timeline = new AttachmentTimeline(timelineMap.size);
                timeline.slotIndex = slotIndex;

                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next)
                    timeline.setFrame(frameIndex++, valueMap.getFloat("time"), valueMap.getString("name"));
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
            } else
                throw new RuntimeException(
                        "Invalid timeline type for a slot: " + timelineName + " (" + slotMap.name + ")");
        }
    }

    // Bone timelines.
    for (JsonValue boneMap = map.getChild("bones"); boneMap != null; boneMap = boneMap.next) {
        int boneIndex = skeletonData.findBoneIndex(boneMap.name);
        if (boneIndex == -1)
            throw new SerializationException("Bone not found: " + boneMap.name);

        for (JsonValue timelineMap = boneMap.child; timelineMap != null; timelineMap = timelineMap.next) {
            String timelineName = timelineMap.name;
            if (timelineName.equals("rotate")) {
                RotateTimeline timeline = new RotateTimeline(timelineMap.size);
                timeline.boneIndex = boneIndex;

                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) {
                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), valueMap.getFloat("angle"));
                    readCurve(timeline, frameIndex, valueMap);
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 2 - 2]);

            } else if (timelineName.equals("translate") || timelineName.equals("scale")) {
                TranslateTimeline timeline;
                float timelineScale = 1;
                if (timelineName.equals("scale"))
                    timeline = new ScaleTimeline(timelineMap.size);
                else {
                    timeline = new TranslateTimeline(timelineMap.size);
                    timelineScale = scale;
                }
                timeline.boneIndex = boneIndex;

                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) {
                    float x = valueMap.getFloat("x", 0), y = valueMap.getFloat("y", 0);
                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), x * timelineScale,
                            y * timelineScale);
                    readCurve(timeline, frameIndex, valueMap);
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]);

            } else
                throw new RuntimeException(
                        "Invalid timeline type for a bone: " + timelineName + " (" + boneMap.name + ")");
        }
    }

    // IK timelines.
    for (JsonValue ikMap = map.getChild("ik"); ikMap != null; ikMap = ikMap.next) {
        IkConstraintData ikConstraint = skeletonData.findIkConstraint(ikMap.name);
        IkConstraintTimeline timeline = new IkConstraintTimeline(ikMap.size);
        timeline.ikConstraintIndex = skeletonData.getIkConstraints().indexOf(ikConstraint, true);
        int frameIndex = 0;
        for (JsonValue valueMap = ikMap.child; valueMap != null; valueMap = valueMap.next) {
            timeline.setFrame(frameIndex, valueMap.getFloat("time"), valueMap.getFloat("mix"),
                    valueMap.getBoolean("bendPositive") ? 1 : -1);
            readCurve(timeline, frameIndex, valueMap);
            frameIndex++;
        }
        timelines.add(timeline);
        duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]);
    }

    // FFD timelines.
    for (JsonValue ffdMap = map.getChild("ffd"); ffdMap != null; ffdMap = ffdMap.next) {
        Skin skin = skeletonData.findSkin(ffdMap.name);
        if (skin == null)
            throw new SerializationException("Skin not found: " + ffdMap.name);
        for (JsonValue slotMap = ffdMap.child; slotMap != null; slotMap = slotMap.next) {
            int slotIndex = skeletonData.findSlotIndex(slotMap.name);
            if (slotIndex == -1)
                throw new SerializationException("Slot not found: " + slotMap.name);
            for (JsonValue meshMap = slotMap.child; meshMap != null; meshMap = meshMap.next) {
                FfdTimeline timeline = new FfdTimeline(meshMap.size);
                Attachment attachment = skin.getAttachment(slotIndex, meshMap.name);
                if (attachment == null)
                    throw new SerializationException("FFD attachment not found: " + meshMap.name);
                timeline.slotIndex = slotIndex;
                timeline.attachment = attachment;

                int vertexCount;
                if (attachment instanceof MeshAttachment)
                    vertexCount = ((MeshAttachment) attachment).getVertices().length;
                else
                    vertexCount = ((SkinnedMeshAttachment) attachment).getWeights().length / 3 * 2;

                int frameIndex = 0;
                for (JsonValue valueMap = meshMap.child; valueMap != null; valueMap = valueMap.next) {
                    float[] vertices;
                    JsonValue verticesValue = valueMap.get("vertices");
                    if (verticesValue == null) {
                        if (attachment instanceof MeshAttachment)
                            vertices = ((MeshAttachment) attachment).getVertices();
                        else
                            vertices = new float[vertexCount];
                    } else {
                        vertices = new float[vertexCount];
                        int start = valueMap.getInt("offset", 0);
                        System.arraycopy(verticesValue.asFloatArray(), 0, vertices, start, verticesValue.size);
                        if (scale != 1) {
                            for (int i = start, n = i + verticesValue.size; i < n; i++)
                                vertices[i] *= scale;
                        }
                        if (attachment instanceof MeshAttachment) {
                            float[] meshVertices = ((MeshAttachment) attachment).getVertices();
                            for (int i = 0; i < vertexCount; i++)
                                vertices[i] += meshVertices[i];
                        }
                    }

                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), vertices);
                    readCurve(timeline, frameIndex, valueMap);
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
            }
        }
    }

    // Draw order timeline.
    JsonValue drawOrdersMap = map.get("drawOrder");
    if (drawOrdersMap == null)
        drawOrdersMap = map.get("draworder");
    if (drawOrdersMap != null) {
        DrawOrderTimeline timeline = new DrawOrderTimeline(drawOrdersMap.size);
        int slotCount = skeletonData.slots.size;
        int frameIndex = 0;
        for (JsonValue drawOrderMap = drawOrdersMap.child; drawOrderMap != null; drawOrderMap = drawOrderMap.next) {
            int[] drawOrder = null;
            JsonValue offsets = drawOrderMap.get("offsets");
            if (offsets != null) {
                drawOrder = new int[slotCount];
                for (int i = slotCount - 1; i >= 0; i--)
                    drawOrder[i] = -1;
                int[] unchanged = new int[slotCount - offsets.size];
                int originalIndex = 0, unchangedIndex = 0;
                for (JsonValue offsetMap = offsets.child; offsetMap != null; offsetMap = offsetMap.next) {
                    int slotIndex = skeletonData.findSlotIndex(offsetMap.getString("slot"));
                    if (slotIndex == -1)
                        throw new SerializationException("Slot not found: " + offsetMap.getString("slot"));
                    // Collect unchanged items.
                    while (originalIndex != slotIndex)
                        unchanged[unchangedIndex++] = originalIndex++;
                    // Set changed items.
                    drawOrder[originalIndex + offsetMap.getInt("offset")] = originalIndex++;
                }
                // Collect remaining unchanged items.
                while (originalIndex < slotCount)
                    unchanged[unchangedIndex++] = originalIndex++;
                // Fill in unchanged items.
                for (int i = slotCount - 1; i >= 0; i--)
                    if (drawOrder[i] == -1)
                        drawOrder[i] = unchanged[--unchangedIndex];
            }
            timeline.setFrame(frameIndex++, drawOrderMap.getFloat("time"), drawOrder);
        }
        timelines.add(timeline);
        duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
    }

    // Event timeline.
    JsonValue eventsMap = map.get("events");
    if (eventsMap != null) {
        EventTimeline timeline = new EventTimeline(eventsMap.size);
        int frameIndex = 0;
        for (JsonValue eventMap = eventsMap.child; eventMap != null; eventMap = eventMap.next) {
            EventData eventData = skeletonData.findEvent(eventMap.getString("name"));
            if (eventData == null)
                throw new SerializationException("Event not found: " + eventMap.getString("name"));
            Event event = new Event(eventMap.getFloat("time"), eventData);
            event.intValue = eventMap.getInt("int", eventData.getInt());
            event.floatValue = eventMap.getFloat("float", eventData.getFloat());
            event.stringValue = eventMap.getString("string", eventData.getString());
            timeline.setFrame(frameIndex++, event);
        }
        timelines.add(timeline);
        duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
    }

    timelines.shrink();
    skeletonData.animations.add(new Animation(name, timelines, duration));
}

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  . co m*/
    };
    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;
}