Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

package com.gebauz.bauzoid.math.collisionx;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.gebauz.bauzoid.app.Consts;
import com.gebauz.bauzoid.parser.Preprocessor;
import com.gebauz.bauzoid.parser.ScanException;
import com.gebauz.bauzoid.parser.Tokenizer;

public class ShapeUtil
{

        public static final String LOG_TAG = Consts.LOG_TAG + ":SpriteUtil";
        public static boolean verbose = true;
       
        private ShapeUtil() {}
       
        public static void log(String tag, String msg)
        {
                if (verbose)
                        Gdx.app.log(tag, msg);
        }
       
        public static Shape createShapeFromFile(String filename)
        {
                return createShapeFromFile(Gdx.files.internal(filename));
        }
       
        public static Shape createShapeFromFile(FileHandle file)
        {
                String fileContents = Preprocessor.stripComments(file.readString());
               
                Shape shape = new Shape();
               
                float frameW = 0;
                float frameH = 0;
               
                try
                {
                        Tokenizer tokenizer = new Tokenizer(fileContents);
                        tokenizer.setStringDelimiter(new char[] {'\'', '"'} );
                        while (!tokenizer.checkNoMoreTokens())
                        {
                                String identifier = tokenizer.readIdentifier();
                                if (identifier.equalsIgnoreCase("framesize"))
                                {
                                        frameW = tokenizer.readNumber();
                                        tokenizer.readToken(",");
                                        frameH = tokenizer.readNumber();
                                        tokenizer.readToken(";");
                                }
                                else if (identifier.equalsIgnoreCase("rect"))
                                {
                                        // TODO: there is no check whether framesize is set at all!
                                       
                                        BaseShapeElement element = readRect(tokenizer, frameW, frameH);
                                        if (element != null)
                                                shape.addElement(element);
                                }
                                else if (identifier.equalsIgnoreCase("ellipse"))
                                {
                                        BaseShapeElement element = readEllipse(tokenizer, frameW, frameH);
                                        if (element != null)
                                                shape.addElement(element);
                                }
                                else
                                {
                                        throw new ScanException(LOG_TAG, "Syntax error at: " + tokenizer.getSurroundings());
                                }
                        }
                }
                catch (ScanException e)
                {
                        e.log(LOG_TAG);
                        return null;
                }
               
                return shape;
        }
       
        public static BaseShapeElement readRect(Tokenizer t, float frameW, float frameH) throws ScanException
        {
                if ((frameW == 0.0f) || (frameH == 0.0f))
                        throw new ScanException(LOG_TAG, "framesize not set or 0!");
               
                float x = t.readNumber();
                t.readToken(",");
                float y = t.readNumber();
                t.readToken(",");
                float w = t.readNumber();
                t.readToken(",");
                float h = t.readNumber();
                t.readToken(";");
               
                RectElement element = new RectElement(x / frameW, y / frameH, w / frameW, h / frameH);
                return element;
        }
       
        public static BaseShapeElement readEllipse(Tokenizer t, float frameW, float frameH) throws ScanException
        {
                if ((frameW == 0.0f) || (frameH == 0.0f))
                        throw new ScanException(LOG_TAG, "framesize not set or 0!");
               
                float x = t.readNumber();
                t.readToken(",");
                float y = t.readNumber();
                t.readToken(",");
                float radiusX = t.readNumber();
                t.readToken(",");
                float radiusY = t.readNumber();
                t.readToken(";");
               
                EllipseElement element = new EllipseElement(x / frameW, y / frameH, radiusX / frameW, radiusY / frameH);
                return element;
        }

}