Skip to content

Commit b86d121

Browse files
Merge pull request #19 from JeremyMeissner/5-timeline-implementation
Timeline implementation
2 parents 0f64610 + 78bdd4c commit b86d121

28 files changed

Lines changed: 2301 additions & 960 deletions

FreeFrame/Components/Importer.cs

Lines changed: 108 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,143 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
1+
using FreeFrame.Components.Shapes;
2+
using OpenTK.Mathematics;
43
using System.Text;
5-
using System.Threading.Tasks;
64
using System.Xml;
7-
using FreeFrame.Components.Shapes;
85

96
namespace FreeFrame.Components
107
{
118
public static class Importer
129
{
13-
static public (List<Shape>, bool) ImportFromStream(Stream pStream)
10+
/// <summary>
11+
/// Import shapes list and timeline from a stream
12+
/// </summary>
13+
/// <param name="pStream">Given stream</param>
14+
/// <returns>Shape list and timeline</returns>
15+
/// <exception cref="Exception">If something went wrong in the importation</exception>
16+
static private (List<Shape>, SortedDictionary<int, List<Shape>>, bool) ImportFromStream(Stream pStream)
1417
{
1518
List<Shape> shapes = new List<Shape>();
19+
SortedDictionary<int, List<Shape>> timeline = new SortedDictionary<int, List<Shape>>();
1620
bool compatibilityFlag = false;
1721

22+
Shape? previous = null;
23+
1824
using (XmlReader reader = XmlReader.Create(pStream))
1925
{
20-
while (reader.Read())
26+
try
2127
{
22-
//if (reader.HasAttributes)
28+
while (reader.Read())
2329
{
24-
//Console.WriteLine("Attributes of <" + reader.Name + ">");
25-
switch (reader.Name)
30+
if (reader.HasAttributes)
2631
{
27-
case "xml":
28-
case "svg":
29-
break; // Skip knowned elements
30-
case "polygon":
31-
shapes.Add(new SVGPolygon(reader));
32-
break;
33-
case "path":
34-
shapes.Add(new SVGPath(reader));
35-
break;
36-
case "rect":
37-
shapes.Add(new SVGRectangle(reader));
38-
break;
39-
case "circle":
40-
shapes.Add(new SVGCircle(reader));
41-
break;
42-
case "line":
43-
shapes.Add(new SVGLine(reader));
44-
break;
45-
default:
46-
compatibilityFlag = true; // If an element is unknow, the flag is trigger
47-
break;
32+
switch (reader.Name)
33+
{
34+
case "xml":
35+
case "svg":
36+
break; // Skip knowned elements
37+
case "polygon":
38+
shapes.Add(new SVGPolygon(reader));
39+
previous = shapes.Last();
40+
break;
41+
case "path":
42+
shapes.Add(new SVGPath(reader));
43+
previous = shapes.Last();
44+
break;
45+
case "rect":
46+
shapes.Add(new SVGRectangle(reader));
47+
previous = shapes.Last();
48+
break;
49+
case "circle":
50+
shapes.Add(new SVGCircle(reader));
51+
previous = shapes.Last();
52+
break;
53+
case "line":
54+
shapes.Add(new SVGLine(reader));
55+
previous = shapes.Last();
56+
break;
57+
default:
58+
compatibilityFlag = true; // If an element is unknow, the flag is trigger
59+
break;
60+
}
4861
}
62+
4963
}
5064
}
65+
catch (Exception)
66+
{
67+
throw new Exception("Error while importing");
68+
}
5169
}
52-
return (shapes, compatibilityFlag);
70+
return (shapes, new SortedDictionary<int, List<Shape>>(), compatibilityFlag);
5371
}
54-
static public (List<Shape>, bool) ImportFromFile(string pFilename)
72+
73+
/// <summary>
74+
/// Import shapes list and timeline from a file path
75+
/// </summary>
76+
/// <param name="pFilename">File path</param>
77+
/// <returns>Shape list and timeline</returns>
78+
/// <exception cref="ArgumentException">If file cannot be found</exception>
79+
static public (List<Shape>, SortedDictionary<int, List<Shape>>, bool) ImportFromFile(string pFilename)
5580
{
5681
if (!File.Exists(pFilename))
57-
throw new ArgumentException($"'{pFilename}' file cannot be found.", nameof(pFilename)); // TODO: replace by a simple alert window
82+
throw new ArgumentException($"'{pFilename}' file cannot be found.", nameof(pFilename));
5883

5984
byte[] byteArray = Encoding.UTF8.GetBytes(File.ReadAllText(pFilename));
6085

6186
return ImportFromStream(new MemoryStream(byteArray));
6287
}
63-
static public (List<Shape>, bool) ImportFromString(string pString)
88+
89+
/// <summary>
90+
/// Import shapes list and timeline from a string
91+
/// </summary>
92+
/// <param name="pString">Given string</param>
93+
/// <returns>Shape list and timeline</returns>
94+
static public (List<Shape>, SortedDictionary<int, List<Shape>>, bool) ImportFromString(string pString)
6495
{
6596
byte[] byteArray = Encoding.UTF8.GetBytes(pString);
6697

6798
return ImportFromStream(new MemoryStream(byteArray));
6899
}
100+
101+
/// <summary>
102+
/// Export a given shape list into an svg file
103+
/// </summary>
104+
/// <param name="shapes">Shape list</param>
105+
/// <param name="clientSize">Window size</param>
106+
/// <param name="path">File path</param>
107+
static public void ExportToFile(List<Shape> shapes, Vector2i clientSize, string path)
108+
{
109+
using (FileStream fs = new FileStream(path, FileMode.Create))
110+
{
111+
byte[] bytes = Encoding.ASCII.GetBytes($@"<?xml version=""1.0"" encoding=""utf-8"" ?>
112+
<svg xmlns=""http://www.w3.org/2000/svg"" version=""1.1"" width=""{clientSize.X}"" height=""{clientSize.Y}"" >" + Environment.NewLine);
113+
114+
for (int i = 0; i < bytes.Length; i++)
115+
fs.WriteByte(bytes[i]);
116+
117+
foreach (Shape shape in shapes)
118+
{
119+
bytes = Encoding.ASCII.GetBytes(shape.ToString() + Environment.NewLine);
120+
for (int i = 0; i < bytes.Length; i++)
121+
fs.WriteByte(bytes[i]);
122+
}
123+
bytes = Encoding.ASCII.GetBytes("</svg>");
124+
for (int i = 0; i < bytes.Length; i++)
125+
fs.WriteByte(bytes[i]);
126+
}
127+
}
128+
129+
/// <summary>
130+
/// Convert from a string to an Color4 value
131+
/// </summary>
132+
/// <param name="hexadecimal">hexadecimal string</param>
133+
/// <returns>Color4 value</returns>
134+
static public Color4 HexadecimalToRGB(string hexadecimal)
135+
{
136+
float r = Convert.ToInt32(hexadecimal.Substring(1, 2), 16) / 255f;
137+
float g = Convert.ToInt32(hexadecimal.Substring(3, 2), 16) / 255f;
138+
float b = Convert.ToInt32(hexadecimal.Substring(5, 2), 16) / 255f;
139+
float a = Convert.ToInt32(hexadecimal.Substring(7, 2), 16) / 255f;
140+
return new Color4(r, g, b, a);
141+
}
69142
}
70143
}

FreeFrame/Components/Shapes/Hitbox.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)