Rev 1422 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1414 | chris | 1 | using System; |
| 2 | using System.Collections.Generic; |
||
| 3 | using System.Linq; |
||
| 4 | using System.Text; |
||
| 5 | using System.Threading.Tasks; |
||
| 6 | using System.IO; |
||
| 7 | |||
| 8 | using BauzoidNET.parser; |
||
| 1422 | chris | 9 | using BauzoidNET.math; |
| 1414 | chris | 10 | |
| 11 | using ShapeEditor.file.shapes; |
||
| 12 | |||
| 13 | namespace ShapeEditor.file |
||
| 14 | { |
||
| 15 | public class FileUtil |
||
| 16 | { |
||
| 17 | |||
| 18 | |||
| 19 | private FileUtil() |
||
| 20 | { |
||
| 21 | } |
||
| 22 | |||
| 23 | public static bool LoadLevel(string filename, Document doc) |
||
| 24 | { |
||
| 25 | string text = Preprocessor.stripComments(File.ReadAllText(filename)); |
||
| 26 | |||
| 27 | Tokenizer t = new Tokenizer(text); |
||
| 28 | t.setStringDelimiter(new char[] { '\'', '"' }); |
||
| 29 | |||
| 30 | float frameW = 0; |
||
| 31 | float frameH = 0; |
||
| 32 | |||
| 1533 | chris | 33 | int currentFrameStart = -1; |
| 34 | int currentFrameEnd = -1; |
||
| 35 | |||
| 1414 | chris | 36 | while (!t.checkNoMoreTokens()) |
| 37 | { |
||
| 38 | String identifier = t.readIdentifier(); |
||
| 1533 | chris | 39 | if (identifier.Equals("frameSize", StringComparison.OrdinalIgnoreCase)) |
| 1414 | chris | 40 | { |
| 41 | frameW = t.readNumber(); |
||
| 42 | t.readToken(","); |
||
| 43 | frameH = t.readNumber(); |
||
| 44 | t.readToken(";"); |
||
| 45 | } |
||
| 1533 | chris | 46 | else if (identifier.Equals("frameRange", StringComparison.OrdinalIgnoreCase)) |
| 47 | { |
||
| 48 | currentFrameStart = (int)t.readNumber(); |
||
| 49 | t.readToken(","); |
||
| 50 | currentFrameEnd = (int)t.readNumber(); |
||
| 51 | t.readToken(";"); |
||
| 52 | } |
||
| 1414 | chris | 53 | else if (identifier.Equals("rect", StringComparison.OrdinalIgnoreCase)) |
| 54 | { |
||
| 1533 | chris | 55 | readRect(t, doc, frameW, frameH, currentFrameStart, currentFrameEnd); |
| 56 | |||
| 1414 | chris | 57 | } |
| 58 | else if (identifier.Equals("ellipse", StringComparison.OrdinalIgnoreCase)) |
||
| 59 | { |
||
| 1533 | chris | 60 | readEllipse(t, doc, frameW, frameH, currentFrameStart, currentFrameEnd); |
| 1414 | chris | 61 | } |
| 1422 | chris | 62 | else if (identifier.Equals("polygon", StringComparison.OrdinalIgnoreCase)) |
| 63 | { |
||
| 1533 | chris | 64 | readPolygon(t, doc, frameW, frameH, currentFrameStart, currentFrameEnd); |
| 1422 | chris | 65 | } |
| 1414 | chris | 66 | else |
| 67 | { |
||
| 68 | throw new ScanException("Syntax error at: " + t.getSurroundings()); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | return true; |
||
| 73 | } |
||
| 74 | |||
| 1533 | chris | 75 | public static void readRect(Tokenizer t, Document doc, float frameW, float frameH, int startFrame, int endFrame) |
| 1414 | chris | 76 | { |
| 77 | if ((frameW == 0.0f) || (frameH == 0.0f)) |
||
| 78 | throw new ScanException("framesize not set or 0!"); |
||
| 79 | |||
| 80 | float x = t.readNumber(); |
||
| 81 | t.readToken(","); |
||
| 82 | float y = t.readNumber(); |
||
| 83 | t.readToken(","); |
||
| 84 | float w = t.readNumber(); |
||
| 85 | t.readToken(","); |
||
| 86 | float h = t.readNumber(); |
||
| 87 | t.readToken(";"); |
||
| 88 | |||
| 1533 | chris | 89 | doc.AddRectangle(x, y, w, h, startFrame, endFrame); |
| 1414 | chris | 90 | |
| 91 | /*RectangleElement element = new RectangleElement(doc, "rect", x / frameW, y / frameH, w / frameW, h / frameH); |
||
| 92 | element.UpdateName();*/ |
||
| 93 | } |
||
| 94 | |||
| 1533 | chris | 95 | public static void readEllipse(Tokenizer t, Document doc, float frameW, float frameH, int startFrame, int endFrame) |
| 1414 | chris | 96 | { |
| 97 | if ((frameW == 0.0f) || (frameH == 0.0f)) |
||
| 98 | throw new ScanException("framesize not set or 0!"); |
||
| 99 | |||
| 100 | float x = t.readNumber(); |
||
| 101 | t.readToken(","); |
||
| 102 | float y = t.readNumber(); |
||
| 103 | t.readToken(","); |
||
| 104 | float radiusX = t.readNumber(); |
||
| 105 | t.readToken(","); |
||
| 106 | float radiusY = t.readNumber(); |
||
| 107 | t.readToken(";"); |
||
| 108 | |||
| 1533 | chris | 109 | doc.AddEllipse(x, y, radiusX, radiusY, startFrame, endFrame); |
| 1416 | chris | 110 | |
| 1414 | chris | 111 | |
| 112 | /*EllipseElement element = new EllipseElement(shape, x / frameW, y / frameH, radiusX / frameW, radiusY / frameH); |
||
| 113 | //EllipseElement element = new EllipseElement(shape, x, y , radiusX, radiusY); |
||
| 114 | |||
| 115 | if (verbose) |
||
| 116 | Gdx.app.log(Consts.LOG_TAG, "EllipseElement: " + x + ", " + y + ", " + radiusX + ", " + radiusY); |
||
| 117 | |||
| 118 | return element;*/ |
||
| 119 | } |
||
| 120 | |||
| 1533 | chris | 121 | public static void readPolygon(Tokenizer t, Document doc, float frameW, float frameH, int startFrame, int endFrame) |
| 1422 | chris | 122 | { |
| 123 | if ((frameW == 0.0f) || (frameH == 0.0f)) |
||
| 124 | throw new ScanException("framesize not set or 0!"); |
||
| 125 | |||
| 126 | t.readToken("{"); |
||
| 127 | |||
| 128 | List<Vector2> vertices = new List<Vector2>(); |
||
| 129 | |||
| 130 | while (!t.checkToken("}")) |
||
| 131 | { |
||
| 132 | string p = t.readIdentifier(); |
||
| 133 | float x = t.readNumber(); |
||
| 134 | t.readToken(","); |
||
| 135 | float y = t.readNumber(); |
||
| 136 | t.readToken(";"); |
||
| 137 | |||
| 138 | vertices.Add(new Vector2(x, y)); |
||
| 139 | } |
||
| 140 | |||
| 1533 | chris | 141 | doc.AddPolygon(vertices, startFrame, endFrame); |
| 1422 | chris | 142 | |
| 143 | t.readToken("}"); |
||
| 144 | } |
||
| 145 | |||
| 1414 | chris | 146 | public static bool SaveLevel(string filename, Document doc) |
| 147 | { |
||
| 148 | TextWriter tw = new StreamWriter(filename); |
||
| 149 | |||
| 150 | float frameW = doc.Sprite.getSpriteRegion().getWidth() * doc.Sprite.getSprite().getTextureWidth(); |
||
| 151 | float frameH = doc.Sprite.getSpriteRegion().getHeight() * doc.Sprite.getSprite().getTextureHeight(); |
||
| 152 | |||
| 1533 | chris | 153 | tw.WriteLine("frameSize " + frameW + ", " + frameH + ";"); |
| 1414 | chris | 154 | |
| 155 | for (int i = 0; i < doc.ShapeElements.Count; i++) |
||
| 156 | { |
||
| 157 | doc.ShapeElements.ElementAt(i).WriteFile(tw); |
||
| 158 | } |
||
| 159 | |||
| 160 | // close the stream |
||
| 161 | tw.Close(); |
||
| 162 | return true; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |