Skip to content

Commit e774ed9

Browse files
chore: serialization
1 parent cc7863c commit e774ed9

6 files changed

Lines changed: 68 additions & 18 deletions

File tree

FreeFrame/Components/Shapes/Path/DrawAttribute.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public abstract class DrawAttribute
1818
int _x, _y, _x1, _y1 = 0;
1919

2020
private bool _isRelative;
21-
public bool IsRelative { get => _isRelative; protected set => _isRelative = value; }
21+
public bool IsRelative { get => _isRelative; set => _isRelative = value; }
2222
public int Y1 { get => _y1; set => _y1 = value; }
2323
public int X1 { get => _x1; set => _x1 = value; }
2424
public int X { get => _x; set => _x = value; }
@@ -47,6 +47,7 @@ public abstract class DrawAttribute
4747
/// </summary>
4848
public class MoveTo : DrawAttribute
4949
{
50+
public MoveTo() { }
5051
/// <summary>
5152
/// Moving the current point to another point.
5253
/// </summary>
@@ -102,6 +103,7 @@ public override void UpdateLast()
102103
/// </summary>
103104
public class LineTo : DrawAttribute
104105
{
106+
public LineTo() { }
105107
/// <summary>
106108
/// Use to draw a straight line from the current point to the given point.
107109
/// </summary>
@@ -182,6 +184,7 @@ public override void UpdateLast()
182184
/// </summary>
183185
public class HorizontalLineTo : DrawAttribute
184186
{
187+
public HorizontalLineTo() { }
185188
/// <summary>
186189
/// Use to draw a horizontal line from the current point to the given point.
187190
/// </summary>
@@ -253,6 +256,7 @@ public override void UpdateLast()
253256
/// </summary>
254257
public class VerticalLineTo : DrawAttribute
255258
{
259+
public VerticalLineTo() { }
256260
/// <summary>
257261
/// Use to draw a vertical line from the current point to the given point.
258262
/// </summary>
@@ -333,6 +337,7 @@ public class CurveTo : DrawAttribute
333337
public int X2 { get => _x2; set => _x2 = value; }
334338
public int Y2 { get => _y2; set => _y2 = value; }
335339

340+
public CurveTo() { }
336341
/// <summary>
337342
/// Use to draw a cubic Bézier curve from the current point to the given point.
338343
/// </summary>
@@ -489,6 +494,7 @@ public class SmoothCurveTo : DrawAttribute
489494
public int X2 { get => _x2; set => _x2 = value; }
490495
public int Y2 { get => _y2; set => _y2 = value; }
491496

497+
public SmoothCurveTo() { }
492498
/// <summary>
493499
/// Use to draw a smooth cubic Bézier curve from the current point to the given point.
494500
/// </summary>
@@ -497,6 +503,7 @@ public class SmoothCurveTo : DrawAttribute
497503
/// <param name="x">end point x</param>
498504
/// <param name="y">end point y</param>
499505
/// <param name="isRelative">if true, the points becames relatives to the last point</param>
506+
500507
public SmoothCurveTo(Group x2, Group y2, Group x, Group y, bool isRelative = false) : this(Convert.ToInt32(x2.Value), Convert.ToInt32(y2.Value), Convert.ToInt32(x.Value), Convert.ToInt32(y.Value), isRelative) { }
501508
/// <summary>
502509
/// Use to draw a smooth cubic Bézier curve from the current point to the given point.
@@ -629,6 +636,7 @@ public override void UpdateLast()
629636
/// </summary>
630637
public class QuadraticBezierCurveTo : DrawAttribute
631638
{
639+
public QuadraticBezierCurveTo() { }
632640
/// <summary>
633641
/// Use to draw a quadratic Bézier curve.
634642
/// </summary>
@@ -753,6 +761,7 @@ public override void UpdateLast()
753761
/// </summary>
754762
public class SmoothQuadraticBezierCurveTo : DrawAttribute
755763
{
764+
public SmoothQuadraticBezierCurveTo() { }
756765
/// <summary>
757766
/// Use to draw a smooth quadratic Bézier curve.
758767
/// </summary>
@@ -878,6 +887,7 @@ public class EllipticalArc : DrawAttribute
878887
bool _largeArcFlag;
879888
bool _sweepFlag;
880889

890+
public EllipticalArc() { }
881891
/// <summary>
882892
/// Use to draw an ellipse.
883893
/// </summary>
@@ -889,6 +899,7 @@ public class EllipticalArc : DrawAttribute
889899
/// <param name="x">new current point x</param>
890900
/// <param name="y">new current point y</param>
891901
/// <param name="isRelative">if true, the points becames relatives to the last point</param>
902+
892903
public EllipticalArc(Group rx, Group ry, Group angle, Group largeArcFlag, Group sweepFlag, Group x, Group y, bool isRelative = false) : this(Convert.ToInt32(rx.Value), Convert.ToInt32(ry.Value), Convert.ToInt32(angle.Value), Convert.ToInt32(largeArcFlag.Value) == 0 ? false : true, Convert.ToInt32(sweepFlag.Value) == 0 ? false : true, Convert.ToInt32(x.Value), Convert.ToInt32(y.Value), isRelative) { }
893904
/// <summary>
894905
/// Use to draw an ellipse.
@@ -949,11 +960,10 @@ public override void UpdateLast()
949960
/// </summary>
950961
public class ClosePath : DrawAttribute
951962
{
963+
public ClosePath() { }
952964
/// <summary>
953965
/// Draw a straight line from the current position to the first point in the path.
954966
/// </summary>
955-
public ClosePath() { }
956-
957967
public override List<Vector2i> GetSelectablePoints()
958968
{
959969
throw new NotImplementedException();

FreeFrame/Components/Shapes/SVGPath.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class SVGPath : Shape
3030
List<DrawAttribute> _drawAttributes = new();
3131

3232
public List<DrawAttribute> DrawAttributes { get => _drawAttributes; set => _drawAttributes = value; }
33-
33+
public SVGPath() { }
3434
public SVGPath(XmlReader reader)
3535
{
3636
IsResizeable = false;
@@ -147,7 +147,7 @@ public override void Move(Vector2i position)
147147
}
148148
else if (attrType == typeof(SmoothCurveTo))
149149
{
150-
150+
151151
Console.WriteLine(attr.GetType().Name + " - " + attr.ToString());
152152
Console.WriteLine("x2: {0}, y2: {1}", ((SmoothCurveTo)attr).X2, ((SmoothCurveTo)attr).Y2);
153153
Console.WriteLine("x1: {0}, y1: {1}", DrawAttribute.Last.X1, DrawAttribute.Last.Y1);

FreeFrame/Components/Shapes/Shape.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using FreeFrame.Components.Shapes.Path;
22
using MathNet.Numerics.LinearAlgebra;
3+
using Newtonsoft.Json;
34
using OpenTK.Graphics.OpenGL4;
45
using OpenTK.Mathematics;
56
using OpenTK.Windowing.Desktop;
@@ -25,18 +26,18 @@ public abstract class Shape : IDrawable
2526
public int Height { get => _height; set => _height = Math.Max(0, value); }
2627
public Color4 Color { get => _color; set => _color = value; }
2728
public int Angle { get => _angle; set => _angle = value; }
28-
public bool IsMoveable { get => _isMoveable; protected set => _isMoveable = value; }
29-
public bool IsResizeable { get => _isResizeable; protected set => _isResizeable = value; }
29+
public bool IsMoveable { get => _isMoveable; set => _isMoveable = value; }
30+
public bool IsResizeable { get => _isResizeable; set => _isResizeable = value; }
3031
public bool IsAngleChangeable { get => _isAngleChangeable; set => _isAngleChangeable = value; }
3132
public bool IsCornerRadiusChangeable { get => _isCornerRadiusChangeable; set => _isCornerRadiusChangeable = value; }
3233
public int CornerRadius { get => _cornerRadius; set => _cornerRadius = value; }
33-
public Guid Id { get => _id; private set => _id = value; }
34-
public List<Renderer> Renderers { get => renderers; protected set => renderers = value; }
34+
public Guid Id { get => _id; set => _id = value; }
35+
public List<Renderer> Renderers { get => renderers; set => renderers = value; }
3536
public string ShortId
3637
{
3738
get => Id.ToString().Substring(0, 6);
3839
}
39-
public Shape()
40+
public Shape()
4041
{
4142
Renderers = new List<Renderer>();
4243
Color = Color4.Black;

FreeFrame/FreeFrame.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<PackageReference Include="GifMotion" Version="1.0.4" />
1818
<PackageReference Include="ImGui.NET" Version="1.87.3" />
1919
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
20+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
2021
<PackageReference Include="OpenTK" Version="4.7.1" />
2122
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.1" />
2223
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />

FreeFrame/Renderer.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ public class Renderer
2222
public int VertexBufferObjectID { get => _vertexBufferObjectID; private set => _vertexBufferObjectID = value; }
2323
public int VertexArrayObjectID { get => _vertexArrayObjectID; private set => _vertexArrayObjectID = value; }
2424
public int IndexBufferObjectID { get => _indexBufferObjectID; private set => _indexBufferObjectID = value; }
25-
25+
public Renderer()
26+
{
27+
_primitiveType = PrimitiveType.Triangles;
28+
VertexArrayObjectID = GL.GenVertexArray();
29+
VertexBufferObjectID = GL.GenBuffer();
30+
IndexBufferObjectID = GL.GenBuffer();
31+
_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
32+
}
2633
public Renderer(PrimitiveType primitiveType)
2734
{
2835
_primitiveType = primitiveType;

FreeFrame/Window.cs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@
2828
using System.Drawing.Drawing2D;
2929
using System.Text.Json;
3030
using System.Text.Json.Serialization;
31+
using Newtonsoft.Json;
3132

3233
namespace FreeFrame
3334
{
3435
public class Window : GameWindow
3536
{
37+
struct Workspace
38+
{
39+
public List<Shape> Shapes { get; set; }
40+
public SortedDictionary<int, List<Shape>> SortedTimeline { get; set; }
41+
public System.Numerics.Vector3 BgColor { get; set; }
42+
}
3643
enum UserMode
3744
{
3845
Idle,
@@ -67,10 +74,8 @@ enum CreateMode
6774
bool _dialogCompatibility = false;
6875
bool _dialogError = false;
6976

70-
7177
Vector2i _mouseOriginalState;
7278

73-
7479
Timeline _timeline;
7580

7681
SelectorType _selectorType = SelectorType.None;
@@ -490,9 +495,12 @@ private void ShowUIDebug()
490495
ImGui.Text("Timeline");
491496
int numberColumns = 0, numberRows = 0;
492497

493-
numberColumns = _timeline.SortedTimeline.Count;
494-
if (_timeline.SortedTimeline.Count > 0)
495-
numberRows = _timeline.SortedTimeline.Max(i => i.Value != null ? i.Value.Count : 0);
498+
if (_timeline.SortedTimeline != null)
499+
{
500+
numberColumns = _timeline.SortedTimeline.Count;
501+
if (_timeline.SortedTimeline.Count > 0)
502+
numberRows = _timeline.SortedTimeline.Max(i => i.Value != null ? i.Value.Count : 0);
503+
}
496504

497505
if (numberColumns > 0)
498506
{
@@ -966,7 +974,9 @@ private void ShowUI()
966974
}
967975
if (ImGui.BeginMenu("Save"))
968976
{
969-
if (ImGui.MenuItem("Save as SVG", "Ctrl+S"))
977+
if (ImGui.MenuItem("Save"))
978+
SaveFreeFrameWorkspace();
979+
if (ImGui.MenuItem("Save as SVG"))
970980
SaveCurrentScreenToSVG();
971981
if (ImGui.MenuItem("Save as MP4"))
972982
SaveCurrentScreenToMP4();
@@ -1121,7 +1131,28 @@ public void SaveCurrentScreenToMP4()
11211131
}
11221132
public void SaveFreeFrameWorkspace()
11231133
{
1124-
string jsonString = JsonSerializer.Serialize(weatherForecast);
1134+
Workspace workspace = new()
1135+
{
1136+
SortedTimeline = _timeline.SortedTimeline,
1137+
BgColor = _ioBgColor,
1138+
Shapes = Shapes
1139+
};
1140+
1141+
string jsonString = JsonConvert.SerializeObject(workspace, Formatting.Indented, new JsonSerializerSettings
1142+
{
1143+
TypeNameHandling = TypeNameHandling.Auto // Because I have abstract classes
1144+
});
1145+
1146+
File.WriteAllText("serialize.json", jsonString);
1147+
1148+
Workspace fromJson = JsonConvert.DeserializeObject<Workspace>(jsonString, new JsonSerializerSettings
1149+
{
1150+
TypeNameHandling = TypeNameHandling.Auto // Because I have abstract classes
1151+
});
1152+
1153+
Shapes = fromJson.Shapes;
1154+
_timeline.SortedTimeline = fromJson.SortedTimeline;
1155+
_ioBgColor = fromJson.BgColor;
11251156
}
11261157
public void SaveCurrentScreenToGIF()
11271158
{

0 commit comments

Comments
 (0)