Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1051 | chris | 1 | package com.gebauz.bauzoid.math.collisionx; |
| 2 | |||
| 3 | import com.badlogic.gdx.Gdx; |
||
| 4 | import com.badlogic.gdx.files.FileHandle; |
||
| 5 | import com.gebauz.bauzoid.app.Consts; |
||
| 6 | import com.gebauz.bauzoid.parser.Preprocessor; |
||
| 7 | import com.gebauz.bauzoid.parser.ScanException; |
||
| 8 | import com.gebauz.bauzoid.parser.Tokenizer; |
||
| 9 | |||
| 10 | public class ShapeUtil |
||
| 11 | { |
||
| 12 | |||
| 13 | public static final String LOG_TAG = Consts.LOG_TAG + ":SpriteUtil"; |
||
| 14 | public static boolean verbose = true; |
||
| 15 | |||
| 16 | private ShapeUtil() {} |
||
| 17 | |||
| 18 | public static void log(String tag, String msg) |
||
| 19 | { |
||
| 20 | if (verbose) |
||
| 21 | Gdx.app.log(tag, msg); |
||
| 22 | } |
||
| 23 | |||
| 24 | public static Shape createShapeFromFile(String filename) |
||
| 25 | { |
||
| 26 | return createShapeFromFile(Gdx.files.internal(filename)); |
||
| 27 | } |
||
| 28 | |||
| 29 | public static Shape createShapeFromFile(FileHandle file) |
||
| 30 | { |
||
| 31 | String fileContents = Preprocessor.stripComments(file.readString()); |
||
| 32 | |||
| 33 | Shape shape = new Shape(); |
||
| 34 | |||
| 35 | float frameW = 0; |
||
| 36 | float frameH = 0; |
||
| 37 | |||
| 38 | try |
||
| 39 | { |
||
| 40 | Tokenizer tokenizer = new Tokenizer(fileContents); |
||
| 41 | tokenizer.setStringDelimiter(new char[] {'\'', '"'} ); |
||
| 42 | while (!tokenizer.checkNoMoreTokens()) |
||
| 43 | { |
||
| 44 | String identifier = tokenizer.readIdentifier(); |
||
| 45 | if (identifier.equalsIgnoreCase("framesize")) |
||
| 46 | { |
||
| 47 | frameW = tokenizer.readNumber(); |
||
| 48 | tokenizer.readToken(","); |
||
| 49 | frameH = tokenizer.readNumber(); |
||
| 50 | tokenizer.readToken(";"); |
||
| 51 | } |
||
| 52 | else if (identifier.equalsIgnoreCase("rect")) |
||
| 53 | { |
||
| 54 | // TODO: there is no check whether framesize is set at all! |
||
| 55 | |||
| 56 | BaseShapeElement element = readRect(tokenizer, frameW, frameH); |
||
| 57 | if (element != null) |
||
| 58 | shape.addElement(element); |
||
| 59 | } |
||
| 60 | else if (identifier.equalsIgnoreCase("ellipse")) |
||
| 61 | { |
||
| 62 | BaseShapeElement element = readEllipse(tokenizer, frameW, frameH); |
||
| 63 | if (element != null) |
||
| 64 | shape.addElement(element); |
||
| 65 | } |
||
| 66 | else |
||
| 67 | { |
||
| 68 | throw new ScanException(LOG_TAG, "Syntax error at: " + tokenizer.getSurroundings()); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | } |
||
| 72 | catch (ScanException e) |
||
| 73 | { |
||
| 74 | e.log(LOG_TAG); |
||
| 75 | return null; |
||
| 76 | } |
||
| 77 | |||
| 78 | return shape; |
||
| 79 | } |
||
| 80 | |||
| 81 | public static BaseShapeElement readRect(Tokenizer t, float frameW, float frameH) throws ScanException |
||
| 82 | { |
||
| 83 | if ((frameW == 0.0f) || (frameH == 0.0f)) |
||
| 84 | throw new ScanException(LOG_TAG, "framesize not set or 0!"); |
||
| 85 | |||
| 86 | float x = t.readNumber(); |
||
| 87 | t.readToken(","); |
||
| 88 | float y = t.readNumber(); |
||
| 89 | t.readToken(","); |
||
| 90 | float w = t.readNumber(); |
||
| 91 | t.readToken(","); |
||
| 92 | float h = t.readNumber(); |
||
| 93 | t.readToken(";"); |
||
| 94 | |||
| 95 | RectElement element = new RectElement(x / frameW, y / frameH, w / frameW, h / frameH); |
||
| 96 | return element; |
||
| 97 | } |
||
| 98 | |||
| 99 | public static BaseShapeElement readEllipse(Tokenizer t, float frameW, float frameH) throws ScanException |
||
| 100 | { |
||
| 101 | if ((frameW == 0.0f) || (frameH == 0.0f)) |
||
| 102 | throw new ScanException(LOG_TAG, "framesize not set or 0!"); |
||
| 103 | |||
| 104 | float x = t.readNumber(); |
||
| 105 | t.readToken(","); |
||
| 106 | float y = t.readNumber(); |
||
| 107 | t.readToken(","); |
||
| 108 | float radiusX = t.readNumber(); |
||
| 109 | t.readToken(","); |
||
| 110 | float radiusY = t.readNumber(); |
||
| 111 | t.readToken(";"); |
||
| 112 | |||
| 113 | EllipseElement element = new EllipseElement(x / frameW, y / frameH, radiusX / frameW, radiusY / frameH); |
||
| 114 | return element; |
||
| 115 | } |
||
| 116 | |||
| 117 | } |
||
| 118 | |||
| 119 | |||
| 120 |