Example usage for com.badlogic.gdx.math Matrix3 getValues

List of usage examples for com.badlogic.gdx.math Matrix3 getValues

Introduction

In this page you can find the example usage for com.badlogic.gdx.math Matrix3 getValues.

Prototype

public float[] getValues() 

Source Link

Document

Get the values in this matrix.

Usage

From source file:magory.svg.Sovery.java

public void loadElement(Element el, float[] matrixOld) {
    String elname = el.getName();
    if (elname.equals("svg"))
        getSVGDimentions(el);//from ww  w.j a  v  a 2 s .co m

    int count = el.getChildCount();

    // magic for transforms and matrixes, don't touch unless you know what you are doing
    String trans = getAttribute(el, "transform", "", false);
    String title = getTitle(el);
    float elementX = getFloat(el.getAttribute("x", "0"));
    float elementY = getFloat(el.getAttribute("y", "0"));
    float elementWidth = getFloat(getAttribute(el, "width", "", false));
    float elementHeight = getFloat(getAttribute(el, "height", "", false));
    Vector2 origin = new Vector2(elementWidth / 2, elementHeight / 2);
    Vector2 scale = new Vector2(1, 1);
    Vector2 scaleOld = new Vector2(1, 1);
    Vector2 translate = new Vector2();
    float rr; // rotation

    Matrix3 matrix = getTransformMatrix(trans);
    float oldR = 0;
    if (matrixOld != null) // is a matrix
    {
        Matrix3 mOld = new Matrix3(matrixOld);
        oldR = mOld.getRotation();
        getScale(mOld, scaleOld);

        if (scaleOld.y < 0 || scaleOld.x < 0) {
            origin.rotate(oldR);
            matrix.mulLeft(translate(-origin.x, -origin.y));
            origin.rotate(-oldR);
            matrix.mulLeft(mOld);
            matrix.mul(translate(elementX, elementY));
            matrix.translate(origin.x, origin.y);
        } else {
            origin.rotate(-oldR);
            matrix.mulLeft(translate(-origin.x, -origin.y));
            origin.rotate(oldR);
            matrix.mulLeft(mOld);
            matrix.mul(translate(elementX, elementY));
            matrix.translate(origin.x, origin.y);
        }
    } else {
        matrix.mulLeft(translate(-origin.x, -origin.y));
        matrix.mul(translate(elementX, elementY));
        matrix.translate(origin.x, origin.y);
    }

    if (count != 0)
        for (int i = 0; i < count; i++)
            loadElement(el.getChild(i), matrix.getValues());

    // important magic for width,height and x,y
    // inspired by this: http://stackoverflow.com/questions/16359246/how-to-extract-position-rotation-and-scale-from-matrix-svg
    // and this: http://www.w3.org/TR/css3-transforms/

    matrix.getScale(scale);
    rr = matrix.getRotation();

    // magic which I don't understand
    getScale(matrix, scale);
    if (scale.y < 0 && scaleOld.y > 0) {
        Vector2 det = new Vector2(0, elementHeight * Math.signum(scale.y) * 2); // because y flip uses top border of the image 
        det.rotate(rr);
        matrix.translate(det);
    }

    if (scaleOld.y < 0 || scaleOld.x < 0) {
        Vector2 det = new Vector2(0, scaleOld.y * elementHeight / scale.y); // because... well, I don't know why
        if (scale.y < 0)
            det.rotate(rr + 180);
        else if (scale.x < 0)
            det.rotate(rr);
        else {
            if (scaleOld.x < 0)
                det.rotate(-rr + 180);
            else
                det.rotate(-rr);
        }

        matrix.translate(det);
    }

    matrix.getTranslation(translate);

    float width = elementWidth * scale.x;
    float height = elementHeight * scale.y;
    float yyy = SVGHeight - translate.y - height;
    float xxx = translate.x;

    if (title.startsWith("testing")) {
        Gdx.app.log("test", "oldR:" + oldR + " sx:" + scaleOld.x + " sy:" + scaleOld.y);
        Gdx.app.log("test", ":::" + title + ":" + xxx + "," + yyy + ":" + rr + " " + width + "x" + height
                + " rr:" + rr + " scalex:" + scale.x + " scaley:" + scale.y);
    }
    rr = -rr;
    if (width < 0)
        rr = rr + 180;
    if (rr < 0)
        rr += 360;

    if (title.startsWith("testing"))
        Gdx.app.log("test", ":::newrr:" + rr);

    if (elname.equals("path")) // path
    {
        String d = el.getAttribute("d", "");
        newPath(parsePathToArray(d, elementX, elementY, SVGHeight), el, title);
    } else if (isText(elname)) // text
    {
        Element e = getChild(el, "tspan");
        if (e == null)
            return;

        String text;
        if (e.getText() == null)
            text = "";
        else
            text = e.getText().trim();

        // font-size as height! - width not set unfortunately
        // example: font-size:44.03109741px;
        String style = getAttribute(el, "style", "", false);
        String styles[] = style.split("\\;");
        Color color = new Color(1, 1, 1, 1);
        if (styles != null && styles.length > 0) {
            for (int i = 0; i < styles.length; i++) {
                if (isStyle(styles[i], "font-size")) {
                    String stylesdata[] = styles[i].split("\\:");
                    stylesdata[1] = stylesdata[1].replace("px", "");
                    stylesdata[1] = stylesdata[1].replace(";", "");
                    height = getFloat(stylesdata[1].trim());
                } else if (isStyle(styles[i], "fill")) {
                    //fill:#effffa
                    String stylesdata[] = styles[i].split("\\:");
                    stylesdata[1] = stylesdata[1].replace("#", "");
                    stylesdata[1] = stylesdata[1].replace(";", "");
                    color = getColorFromString(stylesdata[1].trim());
                }
            }
        }
        newText(text, el, xxx, yyy, width, height, rr, color);
    } else if (isImage(elname)) // obraz
    {
        String name = getImageName(getAttribute(el, "xlink:href", "", false));
        newImage(name, el, xxx, yyy, width, height, rr);
    } else if (isRect(elname)) // obraz
    {
        Element title2 = getChild(el, "title");
        if (title2 != null)
            newRect(title2.getText(), el, xxx, yyy, width, height, rr);
        else
            newRect("", el, xxx, yyy, width, height, rr);
    }
}