Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: circle svg import compatible
  • Loading branch information
JeremyMeissner committed Apr 4, 2022
commit 8d841ba46b10f2d498633b83f93cc4ccb5a0d241
48 changes: 48 additions & 0 deletions FreeFrame/Components/Importer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using FreeFrame.Components.Shapes;

namespace FreeFrame.Components
{
static class Importer
{
static public void Import()
{
//TODO: find a solution for opening a crossplatform file dialog. Do I need a crossplatform dialog?
//using (OpenFileDialog openFileDialog = new OpenFileDialog())
//{
//}
}
static public List<Shape> Import(string filename)
{
if (!File.Exists(filename))
throw new ArgumentException($"'{nameof(filename)}' file cannot be found.", nameof(filename)); //TODO: replace by a simple alert window

List<Shape> shapes = new List<Shape>();

using (XmlReader reader = XmlReader.Create(filename))
{
while(reader.Read())
{
if (reader.HasAttributes)
{
Console.WriteLine("Attributes of <" + reader.Name + ">");
switch (reader.Name)
{
case "circle":
shapes.Add(new SVGCircle(reader));
break;
default:
break;
}
}
}
}
return (shapes);
}
}
}
48 changes: 48 additions & 0 deletions FreeFrame/Components/Shapes/SVGCircle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Xml;

namespace FreeFrame.Components.Shapes
{
class SVGCircle : Shape
{
#region Geometry properties
private float _cx;
private float _cy;
private float _r;
#endregion
/*

// https://www.w3.org/TR/SVG2/shapes.html#CircleElement
TODO: take in account the attributes (listed on the doc Basic Shapes)

private Color _fill;
private float _fillOpacity;
private float _opacity;

public SVGCircle(XmlReader reader)
{
for (int i = 0; i < reader.AttributeCount; i++)
{

}
}
*/
public SVGCircle(XmlReader reader) : this(
Convert.ToInt32(reader["r"]),
Convert.ToInt32(reader["cx"]),
Convert.ToInt32(reader["cy"]))
{ }
public SVGCircle() : this(0, 0, 0) { }
public SVGCircle(float r, float cx, float cy)
{
_cx = cx;
_cy = cy;
_r = r;
}

public override string ToString() => $"cx: {_cx}, cy: {_cy}, r: {_r}";
}
}
43 changes: 43 additions & 0 deletions FreeFrame/Components/Shapes/SVGPath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace FreeFrame.Components.Shapes
{
class SVGPath : Shape
{
// https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths
// https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d
// Bézier Curves and Quadratique
#region Geometry properties
private float _Mx;
private float _My;

private float _Cx1;
private float _Cy1;

private float _Cx2;
private float _Cy2;

private float _Cx;
private float _Cy;
#endregion


//public SVGPath(XmlReader reader) : this(){ } # TODO: This constructor
public SVGPath(float Mx, float My, float Cx1, float Cy1, float Cx2, float Cy2, float Cx, float Cy)
{
_Mx = Mx;
_My = My;
_Cx1 = Cx1;
_Cy1 = Cy1;
_Cx2 = Cx2;
_Cy2 = Cy2;
_Cx = Cx;
_Cy = Cy;
}

public override string ToString() => $"M: {_Mx} {_My}, C: {_Cx1} {_Cy1}, {_Cx2} {_Cy2}, {_Cx} {_Cy}";
}
}
22 changes: 22 additions & 0 deletions FreeFrame/Components/Shapes/SVGPolygon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace FreeFrame.Components.Shapes
{
class SVGPolygon : Shape
{
#region Geometry properties
List<float> points = new List<float>();
#endregion
public SVGPolygon()
{

}

public override string ToString()
{
throw new NotImplementedException();
}
}
}
43 changes: 43 additions & 0 deletions FreeFrame/Components/Shapes/SVGRectangle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace FreeFrame.Components.Shapes
{
class SVGRectangle : Shape
{
#region Geometry properties
private float _x;
private float _y;

private float _width;
private float _height;

private float _rx; // Rounded in the x axes
private float _ry; // Rounded in the y axes
#endregion

public SVGRectangle(XmlReader reader) : this(
Convert.ToInt32(reader["x"]),
Convert.ToInt32(reader["y"]),
Convert.ToInt32(reader["width"]),
Convert.ToInt32(reader["height"]),
Convert.ToInt32(reader["rx"]),
Convert.ToInt32(reader["ry"]))
{ }
public SVGRectangle(float width, float height) : this(width, height, 0, 0) { }
public SVGRectangle(float width, float height, float x, float y) : this(width, height, x, y, 0, 0) { }
public SVGRectangle(float width, float height, float x, float y, float rx, float ry)
{
_x = x;
_y = y;
_width = width;
_height = height;
_rx = rx;
_ry = ry;
}

public override string ToString() => $"x: {_x}, y: {_y}, width: {_width}, height: {_height}, rx: {_rx}, ry: {_ry}";
}
}
18 changes: 18 additions & 0 deletions FreeFrame/Components/Shapes/Shape.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace FreeFrame.Components.Shapes
{
abstract class Shape
{
public Shape()
{

}

//TODO: add method to convert shape position and size to NDC
public abstract override string ToString();
}
}
4 changes: 4 additions & 0 deletions FreeFrame/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
using OpenTK;
using OpenTK.Graphics;
using FreeFrame.Components;
using FreeFrame.Components.Shapes;

namespace FreeFrame
{
class Program
{
static void Main()
{
List<Shape> shapes = Importer.Import("test.svg");

shapes.ForEach(shape => Console.WriteLine(shape));
}
}
}