Skip to content
Prev Previous commit
Next Next commit
feat: multiple primitives for one shape
  • Loading branch information
JeremyMeissner committed Apr 27, 2022
commit d8df9fd4bbc8f0b7f9fc22b5467a05e826d4d168
150 changes: 138 additions & 12 deletions FreeFrame/Components/Shapes/Path/DrawAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public abstract class DrawAttribute
/// </summary>
/// <returns>array of vertices position. x, y, x, y, ... (clockwise)</returns>
public abstract float[] GetVertices();
public abstract uint[] GetVerticesIndexes();

public abstract override string ToString();
}
Expand Down Expand Up @@ -73,7 +74,25 @@ public MoveTo(int x, int y, bool isRelative = false)
Y = y;
IsRelative = isRelative;
}
public override float[] GetVertices() => throw new NotImplementedException("MoveTo doesnt have any vertices");
public override float[] GetVertices()
{
if (IsRelative)
{
LastX += X;
LastY += Y;
}
else
{
LastX = X;
LastY = Y;
}

return new float[] { }; // Move doesnt have any vertices
}
public override uint[] GetVerticesIndexes()
{
return new uint[] { };
}

public override string ToString() => String.Format("{0} {1},{2}", IsRelative ? 'm' : 'M', X, Y);
}
Expand Down Expand Up @@ -114,11 +133,24 @@ public LineTo(int x, int y, bool isRelative = false)

public override float[] GetVertices()
{
float[] vertices;

if (IsRelative)
return new float[] { LastX, LastY, LastX + X, LastY + Y };
{
vertices = new float[] { LastX, LastY, LastX + X, LastY + Y };
LastX += X; // Update last position
LastY += Y; // Update last position
}
else
return new float[] { LastX, LastY, X, Y };
{
vertices = new float[] { LastX, LastY, X, Y };
LastX = X; // Update last position
LastY = Y; // Update last position
}

return vertices;
}
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1 }; // TODO: Please dont hardcode this

public override string ToString() => String.Format("{0} {1},{2}", IsRelative ? 'l' : 'L', X, Y);
}
Expand Down Expand Up @@ -150,20 +182,30 @@ public HorizontalLineTo(int x, bool isRelative = false)
{
X = x;
IsRelative = isRelative;

LastX = X;
}



public override float[] GetVertices()
{
float[] vertices;

if (IsRelative)
return new float[] { LastX, LastY, LastX + X, LastY };
{
vertices = new float[] { LastX, LastY, LastX + X, LastY };
LastX += X; // Update last position
}
else
return new float[] { LastX, LastY, X, LastY };
{
vertices = new float[] { LastX, LastY, X, LastY };
LastX = X; // Update last position
}

return vertices;
}

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

public override string ToString() => String.Format("{0} {1}", IsRelative ? 'h' : 'H', X);
}
/// <summary>
Expand Down Expand Up @@ -191,17 +233,26 @@ public VerticalLineTo(int y, bool isRelative = false)
{
_y = y;
_isRelative = isRelative;

LastY = _y;
}

public override float[] GetVertices()
{
float[] vertices;

if (_isRelative)
return new float[] { LastX, LastY, LastX, LastY + _y };
{
vertices = new float[] { LastX, LastY, LastX, LastY + _y };
LastY += _y; // Update last position
}
else
return new float[] { LastX, LastY, LastX, _y };
{
vertices = new float[] { LastX, LastY, LastX, _y };
LastY = _y; // Update last position
}

return vertices;
}
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1 }; // TODO: Please dont hardcode this

public override string ToString() => String.Format("{0} {1}", _isRelative ? 'v' : 'V', _y);
}
Expand Down Expand Up @@ -276,6 +327,14 @@ public override float[] GetVertices()

vertices.AddRange(new float[] { x, y });
}
if (X > X2)
LastControlPointX += X + X2;
else if (X < X2)
LastControlPointX += X - X2;
else
LastControlPointX += X;
LastX += X;
LastY += Y;
}
else
{
Expand All @@ -288,10 +347,19 @@ public override float[] GetVertices()

vertices.AddRange(new float[] { x, y });
}
if (X > X2)
LastControlPointX = X + X2;
else if (X < X2)
LastControlPointX = X - X2;
else
LastControlPointX = X;
LastX = X;
LastY = Y;
}

return vertices.ToArray();
}
public override uint[] GetVerticesIndexes() => Enumerable.Range(0, 100).Select(i => (uint)i).ToArray(); // Magic value please dont hard code this

public override string ToString() => String.Format("{0} {1},{2} {3},{4} {5},{6}", _isRelative ? 'c' : 'C', X1, Y1, X2, Y2, X, Y);
}
Expand Down Expand Up @@ -357,6 +425,14 @@ public override float[] GetVertices()

vertices.AddRange(new float[] { x, y });
}
if (X > X2)
LastControlPointX += X + X2;
else if (X < X2)
LastControlPointX += X - X2;
else
LastControlPointX += X;
LastX += X;
LastY += Y;
}
else
{
Expand All @@ -369,10 +445,19 @@ public override float[] GetVertices()

vertices.AddRange(new float[] { x, y });
}
if (X > X2)
LastControlPointX = X + X2;
else if (X < X2)
LastControlPointX = X - X2;
else
LastControlPointX = X;
LastX = X;
LastY = Y;
}

return vertices.ToArray();
}
public override uint[] GetVerticesIndexes() => Enumerable.Range(0, 100).Select(i => (uint)i).ToArray(); // Magic value please dont hard code this

public override string ToString() => String.Format("{0} {1},{2} {3},{4}", IsRelative ? 's' : 'S', X2, Y2, X, Y);

Expand Down Expand Up @@ -437,6 +522,14 @@ public override float[] GetVertices()

vertices.AddRange(new float[] { x, y });
}
if (_x > X1)
LastControlPointX += _x + X1;
else if (_x < X1)
LastControlPointX += _x - X1;
else
LastControlPointX += _x;
LastX += _x;
LastY += _y;
}
else
{
Expand All @@ -449,10 +542,19 @@ public override float[] GetVertices()

vertices.AddRange(new float[] { x, y });
}
if (_x > X1)
LastControlPointX = _x + X1;
else if (_x < X1)
LastControlPointX = _x - X1;
else
LastControlPointX = _x;
LastX = _x;
LastY = _y;
}

return vertices.ToArray();
}
public override uint[] GetVerticesIndexes() => Enumerable.Range(0, 100).Select(i => (uint)i).ToArray(); // Magic value please dont hard code this

public override string ToString() => String.Format("{0} {1},{2} {3},{4}", IsRelative ? 'q' : 'Q', X1, Y1, _x, _y);

Expand Down Expand Up @@ -506,6 +608,14 @@ public override float[] GetVertices()

vertices.AddRange(new float[] { x, y });
}
if (_x > LastControlPointX)
LastControlPointX += _x + LastControlPointX;
else if (_x < LastControlPointX)
LastControlPointX += _x - LastControlPointX;
else
LastControlPointX += _x;
LastX += _x;
LastY += _y;
}
else
{
Expand All @@ -518,10 +628,19 @@ public override float[] GetVertices()

vertices.AddRange(new float[] { x, y });
}
if (_x > LastControlPointX)
LastControlPointX = _x + LastControlPointX;
else if (_x < LastControlPointX)
LastControlPointX = _x - LastControlPointX;
else
LastControlPointX = _x;
LastX = _x;
LastY = _y;
}

return vertices.ToArray();
}
public override uint[] GetVerticesIndexes() => Enumerable.Range(0, 100).Select(i => (uint)i).ToArray(); // Magic value please dont hard code this

public override string ToString() => String.Format("{0} {1},{2}", _isRelative ? 't' : 'T', _x, _y);
}
Expand Down Expand Up @@ -581,6 +700,10 @@ public override float[] GetVertices()
{
throw new NotImplementedException();
}
public override uint[] GetVerticesIndexes()
{
throw new NotImplementedException();
}

public override string ToString() => String.Format("{0} {1} {2} {3} {4} {5} {6},{7}", _isRelative ? 'a' : 'A', _rx, _ry, _angle, Convert.ToInt32(_largeArcFlag), Convert.ToInt32(_sweepFlag), _x, _y);
}
Expand All @@ -600,7 +723,10 @@ public override float[] GetVertices()
{
throw new NotImplementedException();
}

public override uint[] GetVerticesIndexes()
{
throw new NotImplementedException();
}

public override string ToString() => "z";
}
Expand Down
5 changes: 4 additions & 1 deletion FreeFrame/Components/Shapes/SVGCircle.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using OpenTK.Mathematics;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
Expand All @@ -25,6 +26,7 @@ public SVGCircle(int r, int cx, int cy)
_cy = cy;
_r = r;
}
public override void Draw(Vector2i clientSize) => throw new NotImplementedException();

public override string ToString() => $"cx: {_cx}, cy: {_cy}, r: {_r}";

Expand All @@ -36,5 +38,6 @@ public override Hitbox Hitbox()
{
throw new NotImplementedException();
}

}
}
5 changes: 3 additions & 2 deletions FreeFrame/Components/Shapes/SVGLine.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using OpenTK.Mathematics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -46,7 +47,7 @@ public SVGLine(int x1, int y1, int x2, int y2)
public override float[] GetVertices() => new float[] { X1, Y1, X2, Y2 }; // x, y, x, y, x, y, ... (clockwise)
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1 }; // TODO: please dont hardcode


public override void Draw(Vector2i clientSize) => throw new NotImplementedException();
public override string ToString() => $"x1: {X1}, y1: {Y1}, x2: {X2}, y2: {Y2}";

public override Hitbox Hitbox()
Expand Down
18 changes: 16 additions & 2 deletions FreeFrame/Components/Shapes/SVGPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,25 @@ public SVGPath(XmlReader reader) //: this()
startIndex += match.Groups[0].Length + 1;
}
}
foreach (DrawAttribute attr in DrawAttributes)
{
if (attr.GetType() == typeof(CurveTo) ||
attr.GetType() == typeof(SmoothCurveTo) ||
attr.GetType() == typeof(QuadraticBezierCurveTo) ||
attr.GetType() == typeof(SmoothQuadraticBezierCurveTo) ||
attr.GetType() == typeof(EllipticalArc)
)
{
_vaos.Add(new VertexArrayObject(attr.GetVertices(), attr.GetVerticesIndexes(), PrimitiveType.LineStrip));
}
else
{
_vaos.Add(new VertexArrayObject(attr.GetVertices(), attr.GetVerticesIndexes(), PrimitiveType.Lines));
}
}
}
public override void Draw(Vector2i clientSize)
{
base.Draw(clientSize);

foreach (VertexArrayObject vao in _vaos)
vao.Draw(clientSize);
}
Expand Down
2 changes: 2 additions & 0 deletions FreeFrame/Components/Shapes/SVGPolygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;
using OpenTK.Mathematics;

namespace FreeFrame.Components.Shapes
{
Expand All @@ -25,6 +26,7 @@ public SVGPolygon(XmlReader reader)
_points.Add((Convert.ToInt32(match.Groups[1].Value), Convert.ToInt32(match.Groups[2].Value)));
}

public override void Draw(Vector2i clientSize) => throw new NotImplementedException();
public override float[] GetVertices()
{
throw new NotImplementedException();
Expand Down
8 changes: 8 additions & 0 deletions FreeFrame/Components/Shapes/SVGRectangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using System.Xml;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;

namespace FreeFrame.Components.Shapes
{
Expand Down Expand Up @@ -55,6 +56,13 @@ public SVGRectangle(int width, int height, int x, int y, int rx, int ry)
Height = height;
Rx = rx;
Ry = ry;

_vaos.Add(new VertexArrayObject(GetVertices(), GetVerticesIndexes(), PrimitiveType.Triangles));
}
public override void Draw(Vector2i clientSize)
{
foreach (VertexArrayObject vao in _vaos)
vao.Draw(clientSize);
}
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)
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1, 2, 0, 2, 3 }; // TODO: please dont hardcode
Expand Down
Loading