Skip to content

Commit d153021

Browse files
feat: serialization
1 parent e774ed9 commit d153021

13 files changed

Lines changed: 361 additions & 234 deletions

File tree

FreeFrame/Components/Importer.cs

Lines changed: 38 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -22,106 +22,46 @@ static private (List<Shape>, SortedDictionary<int, List<Shape>>, bool) ImportFro
2222

2323
using (XmlReader reader = XmlReader.Create(pStream))
2424
{
25-
while (reader.Read())
25+
26+
try
2627
{
27-
if (reader.HasAttributes)
28+
while (reader.Read())
2829
{
29-
//Console.WriteLine("Attributes of <" + reader.Name + ">");
30-
switch (reader.Name)
30+
if (reader.HasAttributes)
3131
{
32-
case "xml":
33-
case "svg":
34-
break; // Skip knowned elements
35-
case "polygon":
36-
shapes.Add(new SVGPolygon(reader));
37-
previous = shapes.Last();
38-
break;
39-
case "path":
40-
shapes.Add(new SVGPath(reader));
41-
previous = shapes.Last();
42-
break;
43-
case "rect":
44-
shapes.Add(new SVGRectangle(reader));
45-
previous = shapes.Last();
46-
break;
47-
case "circle":
48-
shapes.Add(new SVGCircle(reader));
49-
previous = shapes.Last();
50-
break;
51-
case "line":
52-
shapes.Add(new SVGLine(reader));
53-
previous = shapes.Last();
54-
break;
55-
case "animate":
56-
if (previous != null)
57-
{
58-
string attr = reader["attributeName"].ToString();
59-
int nbrKeyFrames = reader["values"].ToString().Count(c => c == ';');
60-
// regex
61-
Regex rx = new Regex(@"\d|#\d{8}");
62-
63-
MatchCollection matches = rx.Matches(reader["values"]);
64-
65-
for (int i = 0; i < nbrKeyFrames + 1; i++)
66-
{
67-
int positionInTimeline = i * (Timeline.MAX_TIMELINE / (nbrKeyFrames + 1));
68-
if (timeline.ContainsKey(positionInTimeline) == false)
69-
timeline.Add(positionInTimeline, null);
70-
if (timeline[positionInTimeline] == null)
71-
timeline[positionInTimeline] = new List<Shape>();
72-
Shape shape = previous.ShallowCopy();
73-
74-
string matchResult = matches[i].Value;
75-
76-
switch (attr)
77-
{
78-
case "rx":
79-
case "ry":
80-
shape.CornerRadius = Convert.ToInt32(matchResult);
81-
break;
82-
case "width":
83-
shape.Width = Convert.ToInt32(matchResult);
84-
break;
85-
case "height":
86-
shape.Height = Convert.ToInt32(matchResult);
87-
break;
88-
case "x":
89-
shape.X = Convert.ToInt32(matchResult);
90-
break;
91-
case "y":
92-
shape.Y = Convert.ToInt32(matchResult);
93-
break;
94-
case "fill":
95-
shape.Color = HexadecimalToRGB(matchResult);
96-
break;
97-
default:
98-
break;
99-
}
100-
timeline[positionInTimeline].Add(shape);
101-
}
102-
103-
104-
//reader["dur"]; //TODO: fps base on duration ?
105-
}
106-
break;
107-
case "animateTransform":
108-
if (reader["attributeName"].ToString() == "transform" && reader["type"].ToString() == "rotate")
109-
{
110-
// Only read first one Z from
111-
// from="z _ _"
112-
// to="z _ _"
113-
// get angle
114-
}
115-
break;
116-
default:
117-
compatibilityFlag = true; // If an element is unknow, the flag is trigger
118-
break;
32+
//Console.WriteLine("Attributes of <" + reader.Name + ">");
33+
switch (reader.Name)
34+
{
35+
case "xml":
36+
case "svg":
37+
break; // Skip knowned elements
38+
case "polygon":
39+
shapes.Add(new SVGPolygon(reader));
40+
previous = shapes.Last();
41+
break;
42+
case "path":
43+
shapes.Add(new SVGPath(reader));
44+
previous = shapes.Last();
45+
break;
46+
case "rect":
47+
shapes.Add(new SVGRectangle(reader));
48+
previous = shapes.Last();
49+
break;
50+
case "circle":
51+
shapes.Add(new SVGCircle(reader));
52+
previous = shapes.Last();
53+
break;
54+
case "line":
55+
shapes.Add(new SVGLine(reader));
56+
previous = shapes.Last();
57+
break;
58+
default:
59+
compatibilityFlag = true; // If an element is unknow, the flag is trigger
60+
break;
61+
}
11962
}
120-
}
12163

122-
}
123-
try //TODO: fix trycatch
124-
{
64+
}
12565
}
12666
catch (Exception)
12767
{
@@ -133,7 +73,7 @@ static private (List<Shape>, SortedDictionary<int, List<Shape>>, bool) ImportFro
13373
static public (List<Shape>, SortedDictionary<int, List<Shape>>, bool) ImportFromFile(string pFilename)
13474
{
13575
if (!File.Exists(pFilename))
136-
throw new ArgumentException($"'{pFilename}' file cannot be found.", nameof(pFilename)); // TODO: replace by a simple alert window
76+
throw new ArgumentException($"'{pFilename}' file cannot be found.", nameof(pFilename));
13777

13878
byte[] byteArray = Encoding.UTF8.GetBytes(File.ReadAllText(pFilename));
13979

@@ -146,10 +86,10 @@ static public (List<Shape>, SortedDictionary<int, List<Shape>>, bool) ImportFrom
14686
return ImportFromStream(new MemoryStream(byteArray));
14787
}
14888

149-
static public void ExportToFile(List<Shape> shapes, Vector2i clientSize)
89+
static public void ExportToFile(List<Shape> shapes, Vector2i clientSize, string path)
15090
{
15191

152-
using (FileStream fs = new FileStream("output.svg", FileMode.Create))
92+
using (FileStream fs = new FileStream(path, FileMode.Create))
15393
{
15494
byte[] bytes = Encoding.ASCII.GetBytes($@"<?xml version=""1.0"" encoding=""utf-8"" ?>
15595
<svg xmlns=""http://www.w3.org/2000/svg"" version=""1.1"" width=""{clientSize.X}"" height=""{clientSize.Y}"" >" + Environment.NewLine);

FreeFrame/Components/Shapes/Path/DrawAttribute.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace FreeFrame.Components.Shapes.Path
1515
/// </summary>
1616
public abstract class DrawAttribute
1717
{
18+
public const int CURVE_ACCURACY = 100;
19+
1820
int _x, _y, _x1, _y1 = 0;
1921

2022
private bool _isRelative;
@@ -135,7 +137,7 @@ public override float[] GetVertices()
135137

136138
return vertices;
137139
}
138-
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1 }; // TODO: Please dont hardcode this
140+
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1 };
139141

140142
public override string ToString() => String.Format("{0} {1},{2}", IsRelative ? 'l' : 'L', X, Y);
141143

@@ -214,7 +216,7 @@ public override float[] GetVertices()
214216
return vertices;
215217
}
216218

217-
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1 }; // TODO: Please dont hardcode this
219+
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1 };
218220

219221
public override string ToString() => String.Format("{0} {1}", IsRelative ? 'h' : 'H', X);
220222

@@ -285,7 +287,7 @@ public override float[] GetVertices()
285287

286288
return vertices;
287289
}
288-
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1 }; // TODO: Please dont hardcode this
290+
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1 };
289291

290292
public override List<Vector2i> GetSelectablePoints()
291293
{
@@ -378,9 +380,9 @@ public override float[] GetVertices()
378380
// Only edges
379381
if (IsRelative)
380382
{
381-
for (int i = 0; i < 100; i++) // TODO: Magic value please dont hard code this
383+
for (int i = 0; i < CURVE_ACCURACY; i++)
382384
{
383-
t = i / 100.0f;
385+
t = i / CURVE_ACCURACY;
384386

385387
x = (float)(Math.Pow((1 - t), 3) * Last.X + 3 * Math.Pow((1 - t), 2) * t * (Last.X + X1) + 3 * (1 - t) * Math.Pow(t, 2) * (Last.X + X2) + Math.Pow(t, 3) * (Last.X + X));
386388
y = (float)(Math.Pow((1 - t), 3) * Last.Y + 3 * Math.Pow((1 - t), 2) * t * (Last.Y + Y1) + 3 * (1 - t) * Math.Pow(t, 2) * (Last.Y + Y2) + Math.Pow(t, 3) * (Last.Y + Y));
@@ -390,9 +392,9 @@ public override float[] GetVertices()
390392
}
391393
else
392394
{
393-
for (int i = 0; i < 100; i++) // TODO: Magic value please dont hard code this
395+
for (int i = 0; i < CURVE_ACCURACY; i++)
394396
{
395-
t = i / 100.0f;
397+
t = i / CURVE_ACCURACY;
396398

397399
x = (float)(Math.Pow((1 - t), 3) * Last.X + 3 * Math.Pow((1 - t), 2) * t * X1 + 3 * (1 - t) * Math.Pow(t, 2) * X2 + Math.Pow(t, 3) * X);
398400
y = (float)(Math.Pow((1 - t), 3) * Last.Y + 3 * Math.Pow((1 - t), 2) * t * Y1 + 3 * (1 - t) * Math.Pow(t, 2) * Y2 + Math.Pow(t, 3) * Y);
@@ -530,9 +532,9 @@ public override float[] GetVertices()
530532
// Only edges
531533
if (IsRelative)
532534
{
533-
for (int i = 0; i < 100; i++) // TODO: Magic value please dont hard code this
535+
for (int i = 0; i < CURVE_ACCURACY; i++)
534536
{
535-
t = i / 100.0f;
537+
t = i / CURVE_ACCURACY;
536538

537539
x = (float)(Math.Pow((1 - t), 3) * Last.X + 3 * Math.Pow((1 - t), 2) * t * (Last.X + Last.X1) + 3 * (1 - t) * Math.Pow(t, 2) * (Last.X + X2) + Math.Pow(t, 3) * (Last.X + X));
538540
y = (float)(Math.Pow((1 - t), 3) * Last.Y + 3 * Math.Pow((1 - t), 2) * t * (Last.Y + Last.Y1) + 3 * (1 - t) * Math.Pow(t, 2) * (Last.Y + Y2) + Math.Pow(t, 3) * (Last.Y + Y));
@@ -542,9 +544,9 @@ public override float[] GetVertices()
542544
}
543545
else
544546
{
545-
for (int i = 0; i < 100; i++) // TODO: Magic value please dont hard code this
547+
for (int i = 0; i < CURVE_ACCURACY; i++)
546548
{
547-
t = i / 100.0f;
549+
t = i / CURVE_ACCURACY;
548550

549551
x = (float)(Math.Pow((1 - t), 3) * Last.X + 3 * Math.Pow((1 - t), 2) * t * Last.X1 + 3 * (1 - t) * Math.Pow(t, 2) * X2 + Math.Pow(t, 3) * X);
550552
y = (float)(Math.Pow((1 - t), 3) * Last.Y + 3 * Math.Pow((1 - t), 2) * t * Last.Y1 + 3 * (1 - t) * Math.Pow(t, 2) * Y2 + Math.Pow(t, 3) * Y);
@@ -672,9 +674,9 @@ public override float[] GetVertices()
672674
// Only edges
673675
if (IsRelative)
674676
{
675-
for (int i = 0; i < 100; i++) // TODO: Magic value please dont hard code this
677+
for (int i = 0; i < CURVE_ACCURACY; i++)
676678
{
677-
t = i / 100.0f;
679+
t = i / CURVE_ACCURACY;
678680

679681
x = (float)(Math.Pow((1 - t), 2) * Last.X + 2 * (1 - t) * t * (Last.X + X1) + Math.Pow(t, 2) * (Last.X + X));
680682
y = (float)(Math.Pow((1 - t), 2) * Last.Y + 2 * (1 - t) * t * (Last.Y + Y1) + Math.Pow(t, 2) * (Last.Y + Y));
@@ -684,9 +686,9 @@ public override float[] GetVertices()
684686
}
685687
else
686688
{
687-
for (int i = 0; i < 100; i++) // TODO: Magic value please dont hard code this
689+
for (int i = 0; i < CURVE_ACCURACY; i++)
688690
{
689-
t = i / 100.0f;
691+
t = i / CURVE_ACCURACY;
690692

691693
x = (float)(Math.Pow((1 - t), 2) * Last.X + 2 * (1 - t) * t * X1 + Math.Pow(t, 2) * X);
692694
y = (float)(Math.Pow((1 - t), 2) * Last.Y + 2 * (1 - t) * t * Y1 + Math.Pow(t, 2) * Y);
@@ -791,9 +793,9 @@ public override float[] GetVertices()
791793
// Only edges
792794
if (IsRelative)
793795
{
794-
for (int i = 0; i < 100; i++) // TODO: Magic value please dont hard code this
796+
for (int i = 0; i < CURVE_ACCURACY; i++)
795797
{
796-
t = i / 100.0f;
798+
t = i / CURVE_ACCURACY;
797799

798800
x = (float)(Math.Pow((1 - t), 2) * Last.X + 2 * (1 - t) * t * (Last.X + Last.X1) + Math.Pow(t, 2) * (Last.X + X));
799801
y = (float)(Math.Pow((1 - t), 2) * Last.Y + 2 * (1 - t) * t * (Last.Y + Last.Y1) + Math.Pow(t, 2) * (Last.Y + Y));
@@ -803,9 +805,9 @@ public override float[] GetVertices()
803805
}
804806
else
805807
{
806-
for (int i = 0; i < 100; i++) // TODO: Magic value please dont hard code this
808+
for (int i = 0; i < CURVE_ACCURACY; i++)
807809
{
808-
t = i / 100.0f;
810+
t = i / CURVE_ACCURACY;
809811

810812
x = (float)(Math.Pow((1 - t), 2) * Last.X + 2 * (1 - t) * t * Last.X1 + Math.Pow(t, 2) * X);
811813
y = (float)(Math.Pow((1 - t), 2) * Last.Y + 2 * (1 - t) * t * Last.Y1 + Math.Pow(t, 2) * Y);

FreeFrame/Components/Shapes/SVGCircle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public SVGCircle(int r, int cx, int cy, string color)
3232
public override string ToString() => $"<circle cx=\"{X + Width / 2}\" cy=\"{Y + Height / 2}\" r=\"{Width / 2}\" fill=\"{ColorToHexadecimal(Color)}\"/>";
3333

3434
public override float[] GetVertices() => new float[] { X, Y, X + Width, Y, X + Width, Y + Height, X, Y + Height }; // x, y, x, y, x, y, ... (clockwise)
35-
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1, 2, 0, 2, 3 }; // TODO: please dont hardcode
35+
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1, 2, 0, 2, 3 };
3636

3737

3838
public override List<Vector2i> GetSelectablePoints()

FreeFrame/Components/Shapes/SVGLine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public SVGLine(int x1, int y1, int x2, int y2, string color)
4141
ImplementObject();
4242
}
4343
public override float[] GetVertices() => new float[] { X, Y, X + Width, Y + Height }; // x, y, x, y, x, y, ... (clockwise)
44-
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1 }; // TODO: please dont hardcode
44+
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1 };
4545
public override string ToString() => $"<line x1=\"{X}\" y1=\"{Y}\" x2=\"{Width + X}\" y2=\"{Height + Y}\" fill=\"{ColorToHexadecimal(Color)}\"/>";
4646
public override List<Vector2i> GetSelectablePoints()
4747
{

FreeFrame/Components/Shapes/SVGPath.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public SVGPath(XmlReader reader)
3636
IsResizeable = false;
3737
IsCornerRadiusChangeable = false;
3838

39-
string d = reader["d"] ?? throw new Exception("d not here"); // TODO: Error handler if d is note here
39+
string d = reader["d"] ?? throw new Exception("d not here");
4040
Match match;
4141
int startIndex = 0;
4242

@@ -87,7 +87,7 @@ public SVGPath(XmlReader reader)
8787
}
8888
}
8989

90-
string color = reader["fill"] ?? throw new Exception("color not here"); // TODO: Error handler if d is note here
90+
string color = reader["fill"] ?? throw new Exception("color not here");
9191
Color = Importer.HexadecimalToRGB(color);
9292

9393

0 commit comments

Comments
 (0)