Skip to content

Commit 8d841ba

Browse files
feat: circle svg import compatible
1 parent dccf11a commit 8d841ba

7 files changed

Lines changed: 226 additions & 0 deletions

File tree

FreeFrame/Components/Importer.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Xml;
7+
using FreeFrame.Components.Shapes;
8+
9+
namespace FreeFrame.Components
10+
{
11+
static class Importer
12+
{
13+
static public void Import()
14+
{
15+
//TODO: find a solution for opening a crossplatform file dialog. Do I need a crossplatform dialog?
16+
//using (OpenFileDialog openFileDialog = new OpenFileDialog())
17+
//{
18+
//}
19+
}
20+
static public List<Shape> Import(string filename)
21+
{
22+
if (!File.Exists(filename))
23+
throw new ArgumentException($"'{nameof(filename)}' file cannot be found.", nameof(filename)); //TODO: replace by a simple alert window
24+
25+
List<Shape> shapes = new List<Shape>();
26+
27+
using (XmlReader reader = XmlReader.Create(filename))
28+
{
29+
while(reader.Read())
30+
{
31+
if (reader.HasAttributes)
32+
{
33+
Console.WriteLine("Attributes of <" + reader.Name + ">");
34+
switch (reader.Name)
35+
{
36+
case "circle":
37+
shapes.Add(new SVGCircle(reader));
38+
break;
39+
default:
40+
break;
41+
}
42+
}
43+
}
44+
}
45+
return (shapes);
46+
}
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Text;
5+
using System.Xml;
6+
7+
namespace FreeFrame.Components.Shapes
8+
{
9+
class SVGCircle : Shape
10+
{
11+
#region Geometry properties
12+
private float _cx;
13+
private float _cy;
14+
private float _r;
15+
#endregion
16+
/*
17+
18+
// https://www.w3.org/TR/SVG2/shapes.html#CircleElement
19+
TODO: take in account the attributes (listed on the doc Basic Shapes)
20+
21+
private Color _fill;
22+
private float _fillOpacity;
23+
private float _opacity;
24+
25+
public SVGCircle(XmlReader reader)
26+
{
27+
for (int i = 0; i < reader.AttributeCount; i++)
28+
{
29+
30+
}
31+
}
32+
*/
33+
public SVGCircle(XmlReader reader) : this(
34+
Convert.ToInt32(reader["r"]),
35+
Convert.ToInt32(reader["cx"]),
36+
Convert.ToInt32(reader["cy"]))
37+
{ }
38+
public SVGCircle() : this(0, 0, 0) { }
39+
public SVGCircle(float r, float cx, float cy)
40+
{
41+
_cx = cx;
42+
_cy = cy;
43+
_r = r;
44+
}
45+
46+
public override string ToString() => $"cx: {_cx}, cy: {_cy}, r: {_r}";
47+
}
48+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Xml;
5+
6+
namespace FreeFrame.Components.Shapes
7+
{
8+
class SVGPath : Shape
9+
{
10+
// https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths
11+
// https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d
12+
// Bézier Curves and Quadratique
13+
#region Geometry properties
14+
private float _Mx;
15+
private float _My;
16+
17+
private float _Cx1;
18+
private float _Cy1;
19+
20+
private float _Cx2;
21+
private float _Cy2;
22+
23+
private float _Cx;
24+
private float _Cy;
25+
#endregion
26+
27+
28+
//public SVGPath(XmlReader reader) : this(){ } # TODO: This constructor
29+
public SVGPath(float Mx, float My, float Cx1, float Cy1, float Cx2, float Cy2, float Cx, float Cy)
30+
{
31+
_Mx = Mx;
32+
_My = My;
33+
_Cx1 = Cx1;
34+
_Cy1 = Cy1;
35+
_Cx2 = Cx2;
36+
_Cy2 = Cy2;
37+
_Cx = Cx;
38+
_Cy = Cy;
39+
}
40+
41+
public override string ToString() => $"M: {_Mx} {_My}, C: {_Cx1} {_Cy1}, {_Cx2} {_Cy2}, {_Cx} {_Cy}";
42+
}
43+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace FreeFrame.Components.Shapes
6+
{
7+
class SVGPolygon : Shape
8+
{
9+
#region Geometry properties
10+
List<float> points = new List<float>();
11+
#endregion
12+
public SVGPolygon()
13+
{
14+
15+
}
16+
17+
public override string ToString()
18+
{
19+
throw new NotImplementedException();
20+
}
21+
}
22+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Xml;
5+
6+
namespace FreeFrame.Components.Shapes
7+
{
8+
class SVGRectangle : Shape
9+
{
10+
#region Geometry properties
11+
private float _x;
12+
private float _y;
13+
14+
private float _width;
15+
private float _height;
16+
17+
private float _rx; // Rounded in the x axes
18+
private float _ry; // Rounded in the y axes
19+
#endregion
20+
21+
public SVGRectangle(XmlReader reader) : this(
22+
Convert.ToInt32(reader["x"]),
23+
Convert.ToInt32(reader["y"]),
24+
Convert.ToInt32(reader["width"]),
25+
Convert.ToInt32(reader["height"]),
26+
Convert.ToInt32(reader["rx"]),
27+
Convert.ToInt32(reader["ry"]))
28+
{ }
29+
public SVGRectangle(float width, float height) : this(width, height, 0, 0) { }
30+
public SVGRectangle(float width, float height, float x, float y) : this(width, height, x, y, 0, 0) { }
31+
public SVGRectangle(float width, float height, float x, float y, float rx, float ry)
32+
{
33+
_x = x;
34+
_y = y;
35+
_width = width;
36+
_height = height;
37+
_rx = rx;
38+
_ry = ry;
39+
}
40+
41+
public override string ToString() => $"x: {_x}, y: {_y}, width: {_width}, height: {_height}, rx: {_rx}, ry: {_ry}";
42+
}
43+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Xml;
5+
6+
namespace FreeFrame.Components.Shapes
7+
{
8+
abstract class Shape
9+
{
10+
public Shape()
11+
{
12+
13+
}
14+
15+
//TODO: add method to convert shape position and size to NDC
16+
public abstract override string ToString();
17+
}
18+
}

FreeFrame/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
using OpenTK;
22
using OpenTK.Graphics;
3+
using FreeFrame.Components;
4+
using FreeFrame.Components.Shapes;
35

46
namespace FreeFrame
57
{
68
class Program
79
{
810
static void Main()
911
{
12+
List<Shape> shapes = Importer.Import("test.svg");
1013

14+
shapes.ForEach(shape => Console.WriteLine(shape));
1115
}
1216
}
1317
}

0 commit comments

Comments
 (0)