Skip to content

Commit d8df9fd

Browse files
feat: multiple primitives for one shape
1 parent a454c95 commit d8df9fd

9 files changed

Lines changed: 194 additions & 48 deletions

File tree

FreeFrame/Components/Shapes/Path/DrawAttribute.cs

Lines changed: 138 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public abstract class DrawAttribute
3636
/// </summary>
3737
/// <returns>array of vertices position. x, y, x, y, ... (clockwise)</returns>
3838
public abstract float[] GetVertices();
39+
public abstract uint[] GetVerticesIndexes();
3940

4041
public abstract override string ToString();
4142
}
@@ -73,7 +74,25 @@ public MoveTo(int x, int y, bool isRelative = false)
7374
Y = y;
7475
IsRelative = isRelative;
7576
}
76-
public override float[] GetVertices() => throw new NotImplementedException("MoveTo doesnt have any vertices");
77+
public override float[] GetVertices()
78+
{
79+
if (IsRelative)
80+
{
81+
LastX += X;
82+
LastY += Y;
83+
}
84+
else
85+
{
86+
LastX = X;
87+
LastY = Y;
88+
}
89+
90+
return new float[] { }; // Move doesnt have any vertices
91+
}
92+
public override uint[] GetVerticesIndexes()
93+
{
94+
return new uint[] { };
95+
}
7796

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

115134
public override float[] GetVertices()
116135
{
136+
float[] vertices;
137+
117138
if (IsRelative)
118-
return new float[] { LastX, LastY, LastX + X, LastY + Y };
139+
{
140+
vertices = new float[] { LastX, LastY, LastX + X, LastY + Y };
141+
LastX += X; // Update last position
142+
LastY += Y; // Update last position
143+
}
119144
else
120-
return new float[] { LastX, LastY, X, Y };
145+
{
146+
vertices = new float[] { LastX, LastY, X, Y };
147+
LastX = X; // Update last position
148+
LastY = Y; // Update last position
149+
}
150+
151+
return vertices;
121152
}
153+
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1 }; // TODO: Please dont hardcode this
122154

123155
public override string ToString() => String.Format("{0} {1},{2}", IsRelative ? 'l' : 'L', X, Y);
124156
}
@@ -150,20 +182,30 @@ public HorizontalLineTo(int x, bool isRelative = false)
150182
{
151183
X = x;
152184
IsRelative = isRelative;
153-
154-
LastX = X;
155185
}
156186

157187

158188

159189
public override float[] GetVertices()
160190
{
191+
float[] vertices;
192+
161193
if (IsRelative)
162-
return new float[] { LastX, LastY, LastX + X, LastY };
194+
{
195+
vertices = new float[] { LastX, LastY, LastX + X, LastY };
196+
LastX += X; // Update last position
197+
}
163198
else
164-
return new float[] { LastX, LastY, X, LastY };
199+
{
200+
vertices = new float[] { LastX, LastY, X, LastY };
201+
LastX = X; // Update last position
202+
}
203+
204+
return vertices;
165205
}
166206

207+
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1 }; // TODO: Please dont hardcode this
208+
167209
public override string ToString() => String.Format("{0} {1}", IsRelative ? 'h' : 'H', X);
168210
}
169211
/// <summary>
@@ -191,17 +233,26 @@ public VerticalLineTo(int y, bool isRelative = false)
191233
{
192234
_y = y;
193235
_isRelative = isRelative;
194-
195-
LastY = _y;
196236
}
197237

198238
public override float[] GetVertices()
199239
{
240+
float[] vertices;
241+
200242
if (_isRelative)
201-
return new float[] { LastX, LastY, LastX, LastY + _y };
243+
{
244+
vertices = new float[] { LastX, LastY, LastX, LastY + _y };
245+
LastY += _y; // Update last position
246+
}
202247
else
203-
return new float[] { LastX, LastY, LastX, _y };
248+
{
249+
vertices = new float[] { LastX, LastY, LastX, _y };
250+
LastY = _y; // Update last position
251+
}
252+
253+
return vertices;
204254
}
255+
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1 }; // TODO: Please dont hardcode this
205256

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

277328
vertices.AddRange(new float[] { x, y });
278329
}
330+
if (X > X2)
331+
LastControlPointX += X + X2;
332+
else if (X < X2)
333+
LastControlPointX += X - X2;
334+
else
335+
LastControlPointX += X;
336+
LastX += X;
337+
LastY += Y;
279338
}
280339
else
281340
{
@@ -288,10 +347,19 @@ public override float[] GetVertices()
288347

289348
vertices.AddRange(new float[] { x, y });
290349
}
350+
if (X > X2)
351+
LastControlPointX = X + X2;
352+
else if (X < X2)
353+
LastControlPointX = X - X2;
354+
else
355+
LastControlPointX = X;
356+
LastX = X;
357+
LastY = Y;
291358
}
292359

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

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

358426
vertices.AddRange(new float[] { x, y });
359427
}
428+
if (X > X2)
429+
LastControlPointX += X + X2;
430+
else if (X < X2)
431+
LastControlPointX += X - X2;
432+
else
433+
LastControlPointX += X;
434+
LastX += X;
435+
LastY += Y;
360436
}
361437
else
362438
{
@@ -369,10 +445,19 @@ public override float[] GetVertices()
369445

370446
vertices.AddRange(new float[] { x, y });
371447
}
448+
if (X > X2)
449+
LastControlPointX = X + X2;
450+
else if (X < X2)
451+
LastControlPointX = X - X2;
452+
else
453+
LastControlPointX = X;
454+
LastX = X;
455+
LastY = Y;
372456
}
373457

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

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

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

438523
vertices.AddRange(new float[] { x, y });
439524
}
525+
if (_x > X1)
526+
LastControlPointX += _x + X1;
527+
else if (_x < X1)
528+
LastControlPointX += _x - X1;
529+
else
530+
LastControlPointX += _x;
531+
LastX += _x;
532+
LastY += _y;
440533
}
441534
else
442535
{
@@ -449,10 +542,19 @@ public override float[] GetVertices()
449542

450543
vertices.AddRange(new float[] { x, y });
451544
}
545+
if (_x > X1)
546+
LastControlPointX = _x + X1;
547+
else if (_x < X1)
548+
LastControlPointX = _x - X1;
549+
else
550+
LastControlPointX = _x;
551+
LastX = _x;
552+
LastY = _y;
452553
}
453554

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

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

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

507609
vertices.AddRange(new float[] { x, y });
508610
}
611+
if (_x > LastControlPointX)
612+
LastControlPointX += _x + LastControlPointX;
613+
else if (_x < LastControlPointX)
614+
LastControlPointX += _x - LastControlPointX;
615+
else
616+
LastControlPointX += _x;
617+
LastX += _x;
618+
LastY += _y;
509619
}
510620
else
511621
{
@@ -518,10 +628,19 @@ public override float[] GetVertices()
518628

519629
vertices.AddRange(new float[] { x, y });
520630
}
631+
if (_x > LastControlPointX)
632+
LastControlPointX = _x + LastControlPointX;
633+
else if (_x < LastControlPointX)
634+
LastControlPointX = _x - LastControlPointX;
635+
else
636+
LastControlPointX = _x;
637+
LastX = _x;
638+
LastY = _y;
521639
}
522640

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

526645
public override string ToString() => String.Format("{0} {1},{2}", _isRelative ? 't' : 'T', _x, _y);
527646
}
@@ -581,6 +700,10 @@ public override float[] GetVertices()
581700
{
582701
throw new NotImplementedException();
583702
}
703+
public override uint[] GetVerticesIndexes()
704+
{
705+
throw new NotImplementedException();
706+
}
584707

585708
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);
586709
}
@@ -600,7 +723,10 @@ public override float[] GetVertices()
600723
{
601724
throw new NotImplementedException();
602725
}
603-
726+
public override uint[] GetVerticesIndexes()
727+
{
728+
throw new NotImplementedException();
729+
}
604730

605731
public override string ToString() => "z";
606732
}

FreeFrame/Components/Shapes/SVGCircle.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using OpenTK.Mathematics;
2+
using System;
23
using System.Collections.Generic;
34
using System.Drawing;
45
using System.Text;
@@ -25,6 +26,7 @@ public SVGCircle(int r, int cx, int cy)
2526
_cy = cy;
2627
_r = r;
2728
}
29+
public override void Draw(Vector2i clientSize) => throw new NotImplementedException();
2830

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

@@ -36,5 +38,6 @@ public override Hitbox Hitbox()
3638
{
3739
throw new NotImplementedException();
3840
}
41+
3942
}
4043
}

FreeFrame/Components/Shapes/SVGLine.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using OpenTK.Mathematics;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -46,7 +47,7 @@ public SVGLine(int x1, int y1, int x2, int y2)
4647
public override float[] GetVertices() => new float[] { X1, Y1, X2, Y2 }; // x, y, x, y, x, y, ... (clockwise)
4748
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1 }; // TODO: please dont hardcode
4849

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

5253
public override Hitbox Hitbox()

FreeFrame/Components/Shapes/SVGPath.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,25 @@ public SVGPath(XmlReader reader) //: this()
104104
startIndex += match.Groups[0].Length + 1;
105105
}
106106
}
107+
foreach (DrawAttribute attr in DrawAttributes)
108+
{
109+
if (attr.GetType() == typeof(CurveTo) ||
110+
attr.GetType() == typeof(SmoothCurveTo) ||
111+
attr.GetType() == typeof(QuadraticBezierCurveTo) ||
112+
attr.GetType() == typeof(SmoothQuadraticBezierCurveTo) ||
113+
attr.GetType() == typeof(EllipticalArc)
114+
)
115+
{
116+
_vaos.Add(new VertexArrayObject(attr.GetVertices(), attr.GetVerticesIndexes(), PrimitiveType.LineStrip));
117+
}
118+
else
119+
{
120+
_vaos.Add(new VertexArrayObject(attr.GetVertices(), attr.GetVerticesIndexes(), PrimitiveType.Lines));
121+
}
122+
}
107123
}
108124
public override void Draw(Vector2i clientSize)
109125
{
110-
base.Draw(clientSize);
111-
112126
foreach (VertexArrayObject vao in _vaos)
113127
vao.Draw(clientSize);
114128
}

FreeFrame/Components/Shapes/SVGPolygon.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Text;
44
using System.Xml;
55
using System.Text.RegularExpressions;
6+
using OpenTK.Mathematics;
67

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

29+
public override void Draw(Vector2i clientSize) => throw new NotImplementedException();
2830
public override float[] GetVertices()
2931
{
3032
throw new NotImplementedException();

FreeFrame/Components/Shapes/SVGRectangle.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Text;
44
using System.Xml;
55
using OpenTK.Graphics.OpenGL4;
6+
using OpenTK.Mathematics;
67

78
namespace FreeFrame.Components.Shapes
89
{
@@ -55,6 +56,13 @@ public SVGRectangle(int width, int height, int x, int y, int rx, int ry)
5556
Height = height;
5657
Rx = rx;
5758
Ry = ry;
59+
60+
_vaos.Add(new VertexArrayObject(GetVertices(), GetVerticesIndexes(), PrimitiveType.Triangles));
61+
}
62+
public override void Draw(Vector2i clientSize)
63+
{
64+
foreach (VertexArrayObject vao in _vaos)
65+
vao.Draw(clientSize);
5866
}
5967
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)
6068
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1, 2, 0, 2, 3 }; // TODO: please dont hardcode

0 commit comments

Comments
 (0)