Skip to content

Commit a7cea3a

Browse files
feat: add triangle
1 parent 053ccc0 commit a7cea3a

6 files changed

Lines changed: 326 additions & 108 deletions

File tree

FreeFrame/Components/Importer.cs

Lines changed: 83 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,102 @@ namespace FreeFrame.Components
1111
{
1212
public static class Importer
1313
{
14-
static public (List<Shape>, bool) ImportFromStream(Stream pStream)
14+
static public (List<Shape>, SortedDictionary<int, List<Shape>>, bool) ImportFromStream(Stream pStream)
1515
{
1616
List<Shape> shapes = new List<Shape>();
1717
bool compatibilityFlag = false;
1818

19+
Shape? previous = null;
20+
1921
using (XmlReader reader = XmlReader.Create(pStream))
2022
{
21-
while (reader.Read())
23+
try
2224
{
23-
//if (reader.HasAttributes)
25+
while (reader.Read())
2426
{
25-
//Console.WriteLine("Attributes of <" + reader.Name + ">");
26-
switch (reader.Name)
27+
//if (reader.HasAttributes)
2728
{
28-
case "xml":
29-
case "svg":
30-
break; // Skip knowned elements
31-
case "polygon":
32-
shapes.Add(new SVGPolygon(reader));
33-
break;
34-
case "path":
35-
shapes.Add(new SVGPath(reader));
36-
break;
37-
case "rect":
38-
shapes.Add(new SVGRectangle(reader));
39-
break;
40-
case "circle":
41-
shapes.Add(new SVGCircle(reader));
42-
break;
43-
case "line":
44-
shapes.Add(new SVGLine(reader));
45-
break;
46-
default:
47-
compatibilityFlag = true; // If an element is unknow, the flag is trigger
48-
break;
29+
//Console.WriteLine("Attributes of <" + reader.Name + ">");
30+
switch (reader.Name)
31+
{
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+
62+
for (int i = 0; i < nbrKeyFrames+1; i++)
63+
{
64+
int positionInTimeline = i * (Timeline.MAX_TIMELINE / (nbrKeyFrames + 1));
65+
}
66+
switch (attr)
67+
{
68+
case "rx":
69+
70+
case "ry":
71+
case "width":
72+
case "height":
73+
case "x":
74+
case "y":
75+
case "fill":
76+
case "color":
77+
default:
78+
break;
79+
}
80+
81+
82+
//reader["dur"];
83+
}
84+
break;
85+
case "animateTransform":
86+
if (reader["attributeName"].ToString() == "transform" && reader["type"].ToString() == "rotate")
87+
{
88+
// Only read first one Z from
89+
// from="z _ _"
90+
// to="z _ _"
91+
// get angle
92+
}
93+
break;
94+
default:
95+
compatibilityFlag = true; // If an element is unknow, the flag is trigger
96+
break;
97+
}
4998
}
99+
50100
}
51101
}
102+
catch (Exception)
103+
{
104+
throw new Exception("Error while importing");
105+
}
52106
}
53-
return (shapes, compatibilityFlag);
107+
return (shapes, new SortedDictionary<int, List<Shape>>(), compatibilityFlag);
54108
}
55-
static public (List<Shape>, bool) ImportFromFile(string pFilename)
109+
static public (List<Shape>, SortedDictionary<int, List<Shape>>, bool) ImportFromFile(string pFilename)
56110
{
57111
if (!File.Exists(pFilename))
58112
throw new ArgumentException($"'{pFilename}' file cannot be found.", nameof(pFilename)); // TODO: replace by a simple alert window
@@ -61,7 +115,7 @@ static public (List<Shape>, bool) ImportFromFile(string pFilename)
61115

62116
return ImportFromStream(new MemoryStream(byteArray));
63117
}
64-
static public (List<Shape>, bool) ImportFromString(string pString)
118+
static public (List<Shape>, SortedDictionary<int, List<Shape>>, bool) ImportFromString(string pString)
65119
{
66120
byte[] byteArray = Encoding.UTF8.GetBytes(pString);
67121

FreeFrame/Components/Shapes/Path/DrawAttribute.cs

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public abstract class DrawAttribute
1919

2020
private bool _isRelative;
2121
public bool IsRelative { get => _isRelative; protected set => _isRelative = value; }
22-
public int Y1 { get => _y1; protected set => _y1 = value; }
23-
public int X1 { get => _x1; protected set => _x1 = value; }
22+
public int Y1 { get => _y1; set => _y1 = value; }
23+
public int X1 { get => _x1; set => _x1 = value; }
2424
public int X { get => _x; set => _x = value; }
2525
public int Y { get => _y; set => _y = value; }
2626

@@ -330,8 +330,8 @@ public class CurveTo : DrawAttribute
330330
int _x2;
331331
int _y2;
332332

333-
public int X2 { get => _x2; private set => _x2 = value; }
334-
public int Y2 { get => _y2; private set => _y2 = value; }
333+
public int X2 { get => _x2; set => _x2 = value; }
334+
public int Y2 { get => _y2; set => _y2 = value; }
335335

336336
/// <summary>
337337
/// Use to draw a cubic Bézier curve from the current point to the given point.
@@ -429,35 +429,54 @@ public override void MoveDelta(Vector2i deltaPosition)
429429
Y += deltaPosition.Y;
430430
}
431431

432-
433432
public override (Vector2i position, Vector2i? controlPosition) Information()
434433
{
435434
throw new NotImplementedException();
436435
}
437436

438437
public override void UpdateLast()
439438
{
439+
Console.WriteLine("Update last");
440440
if (IsRelative)
441441
{
442+
Console.WriteLine("relativeee");
442443
if (X > X2)
443444
Last.X1 += X + X2;
444445
else if (X < X2)
445446
Last.X1 += X - X2;
446447
else
447448
Last.X1 += X;
449+
450+
if (Y > Y2)
451+
Last.Y1 += Y + Y2;
452+
else if (Y < Y2)
453+
Last.Y1 += Y - Y2;
454+
else
455+
Last.Y1 += Y;
456+
457+
448458
Last.X += X;
449459
Last.Y += Y;
450460
}
451461
else
452462
{
453-
if (X > X2)
454-
Last.X1 = X + X2;
455-
else if (X < X2)
456-
Last.X1 = X - X2;
457-
else
458-
Last.X1 = X;
459-
Last.X = X;
460-
Last.Y = Y;
463+
Console.WriteLine("absolute");
464+
//if (X > X2)
465+
// Last.X1 = X + X2;
466+
//else if (X < X2)
467+
// Last.X1 = X - X2;
468+
//else
469+
// Last.X1 = X;
470+
471+
//if (Y > Y2)
472+
// Last.Y1 = Y + Y2;
473+
//else if (Y < Y2)
474+
// Last.Y1 = Y - Y2;
475+
//else
476+
// Last.Y1 = Y;
477+
478+
//Last.X = X;
479+
//Last.Y = Y;
461480
}
462481
}
463482
}
@@ -471,8 +490,8 @@ public class SmoothCurveTo : DrawAttribute
471490
int _x2;
472491
int _y2;
473492

474-
public int X2 { get => _x2; private set => _x2 = value; }
475-
public int Y2 { get => _y2; private set => _y2 = value; }
493+
public int X2 { get => _x2; set => _x2 = value; }
494+
public int Y2 { get => _y2; set => _y2 = value; }
476495

477496
/// <summary>
478497
/// Use to draw a smooth cubic Bézier curve from the current point to the given point.
@@ -573,6 +592,14 @@ public override void UpdateLast()
573592
Last.X1 += X - X2;
574593
else
575594
Last.X1 += X;
595+
596+
if (Y > Y2)
597+
Last.Y1 += Y + Y2;
598+
else if (Y < Y2)
599+
Last.Y1 += Y - Y2;
600+
else
601+
Last.Y1 += Y;
602+
576603
Last.X += X;
577604
Last.Y += Y;
578605
}
@@ -584,6 +611,14 @@ public override void UpdateLast()
584611
Last.X1 = X - X2;
585612
else
586613
Last.X1 = X;
614+
615+
if (Y > Y2)
616+
Last.Y1 = Y + Y2;
617+
else if (Y < Y2)
618+
Last.Y1 = Y - Y2;
619+
else
620+
Last.Y1 = Y;
621+
587622
Last.X = X;
588623
Last.Y = Y;
589624
}

FreeFrame/Components/Shapes/SVGPath.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ public override void Move(Vector2i position)
145145
foreach (DrawAttribute attr in DrawAttributes)
146146
{
147147
Type attrType = attr.GetType();
148-
if (attrType == typeof(MoveTo) && attr.IsRelative == false) // Update position of each absolute MoveTo
148+
if (attr.IsRelative == false) // Update position of each absolute attr
149149
{
150+
Console.WriteLine("{0} is relative? =>{1}", attr.GetType().Name, attr.IsRelative);
150151
if (deltaX == null || deltaY == null)
151152
{
152153
// Get delta X and Y only one time
@@ -155,6 +156,24 @@ public override void Move(Vector2i position)
155156
}
156157
attr.X += (int)deltaX;
157158
attr.Y += (int)deltaY;
159+
160+
attr.X1 += (int)deltaX;
161+
attr.Y1 += (int)deltaY;
162+
163+
if (attrType == typeof(CurveTo))
164+
{
165+
((CurveTo)attr).X2 += (int)deltaX;
166+
((CurveTo)attr).Y2 += (int)deltaY;
167+
}
168+
else if (attrType == typeof(SmoothCurveTo))
169+
{
170+
171+
Console.WriteLine(attr.GetType().Name + " - " + attr.ToString());
172+
Console.WriteLine("x2: {0}, y2: {1}", ((SmoothCurveTo)attr).X2, ((SmoothCurveTo)attr).Y2);
173+
Console.WriteLine("x1: {0}, y1: {1}", DrawAttribute.Last.X1, DrawAttribute.Last.Y1);
174+
((SmoothCurveTo)attr).X2 += (int)deltaX;
175+
((SmoothCurveTo)attr).Y2 += (int)deltaY;
176+
}
158177
}
159178
if (attrType == typeof(CurveTo) ||
160179
attrType == typeof(SmoothCurveTo) ||

0 commit comments

Comments
 (0)