using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using BauzoidNET.parser;
using BauzoidNET.math;
using ShapeEditor.file.shapes;
namespace ShapeEditor
.file
{
public class FileUtil
{
private FileUtil
()
{
}
public static bool LoadLevel
(string filename, Document doc
)
{
string text
= Preprocessor
.stripComments(File
.ReadAllText(filename
));
Tokenizer t
= new Tokenizer
(text
);
t
.setStringDelimiter(new char[] { '\'',
'"' });
float frameW
= 0;
float frameH
= 0;
int currentFrameStart
= -1;
int currentFrameEnd
= -1;
while (!t
.checkNoMoreTokens())
{
String identifier
= t
.readIdentifier();
if (identifier
.Equals("frameSize", StringComparison
.OrdinalIgnoreCase))
{
frameW
= t
.readNumber();
t
.readToken(",");
frameH
= t
.readNumber();
t
.readToken(";");
}
else if (identifier
.Equals("frameRange", StringComparison
.OrdinalIgnoreCase))
{
currentFrameStart
= (int)t
.readNumber();
t
.readToken(",");
currentFrameEnd
= (int)t
.readNumber();
t
.readToken(";");
}
else if (identifier
.Equals("rect", StringComparison
.OrdinalIgnoreCase))
{
readRect
(t, doc, frameW, frameH, currentFrameStart, currentFrameEnd
);
}
else if (identifier
.Equals("ellipse", StringComparison
.OrdinalIgnoreCase))
{
readEllipse
(t, doc, frameW, frameH, currentFrameStart, currentFrameEnd
);
}
else if (identifier
.Equals("polygon", StringComparison
.OrdinalIgnoreCase))
{
readPolygon
(t, doc, frameW, frameH, currentFrameStart, currentFrameEnd
);
}
else
{
throw new ScanException
("Syntax error at: " + t
.getSurroundings());
}
}
return true;
}
public static void readRect
(Tokenizer t, Document doc,
float frameW,
float frameH,
int startFrame,
int endFrame
)
{
if ((frameW
== 0
.0f
) || (frameH
== 0
.0f
))
throw new ScanException
("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(";");
doc
.AddRectangle(x, y, w, h, startFrame, endFrame
);
/*RectangleElement element = new RectangleElement(doc, "rect", x / frameW, y / frameH, w / frameW, h / frameH);
element.UpdateName();*/
}
public static void readEllipse
(Tokenizer t, Document doc,
float frameW,
float frameH,
int startFrame,
int endFrame
)
{
if ((frameW
== 0
.0f
) || (frameH
== 0
.0f
))
throw new ScanException
("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(";");
doc
.AddEllipse(x, y, radiusX, radiusY, startFrame, endFrame
);
/*EllipseElement element = new EllipseElement(shape, x / frameW, y / frameH, radiusX / frameW, radiusY / frameH);
//EllipseElement element = new EllipseElement(shape, x, y , radiusX, radiusY);
if (verbose)
Gdx.app.log(Consts.LOG_TAG, "EllipseElement: " + x + ", " + y + ", " + radiusX + ", " + radiusY);
return element;*/
}
public static void readPolygon
(Tokenizer t, Document doc,
float frameW,
float frameH,
int startFrame,
int endFrame
)
{
if ((frameW
== 0
.0f
) || (frameH
== 0
.0f
))
throw new ScanException
("framesize not set or 0!");
t
.readToken("{");
List
<Vector2
> vertices
= new List
<Vector2
>();
while (!t
.checkToken("}"))
{
string p
= t
.readIdentifier();
float x
= t
.readNumber();
t
.readToken(",");
float y
= t
.readNumber();
t
.readToken(";");
vertices
.Add(new Vector2
(x, y
));
}
doc
.AddPolygon(vertices, startFrame, endFrame
);
t
.readToken("}");
}
public static bool SaveLevel
(string filename, Document doc
)
{
TextWriter tw
= new StreamWriter
(filename
);
float frameW
= doc
.Sprite.getSpriteRegion().getWidth() * doc
.Sprite.getSprite().getTextureWidth();
float frameH
= doc
.Sprite.getSpriteRegion().getHeight() * doc
.Sprite.getSprite().getTextureHeight();
tw
.WriteLine("frameSize " + frameW
+ ", " + frameH
+ ";");
for (int i
= 0; i
< doc
.ShapeElements.Count; i
++)
{
doc
.ShapeElements.ElementAt(i
).WriteFile(tw
);
}
// close the stream
tw
.Close();
return true;
}
}
}