|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
| 1 | +using FreeFrame.Components.Shapes; |
| 2 | +using OpenTK.Mathematics; |
4 | 3 | using System.Text; |
5 | | -using System.Threading.Tasks; |
6 | 4 | using System.Xml; |
7 | | -using FreeFrame.Components.Shapes; |
8 | 5 |
|
9 | 6 | namespace FreeFrame.Components |
10 | 7 | { |
11 | 8 | public static class Importer |
12 | 9 | { |
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) |
14 | 17 | { |
15 | 18 | List<Shape> shapes = new List<Shape>(); |
| 19 | + SortedDictionary<int, List<Shape>> timeline = new SortedDictionary<int, List<Shape>>(); |
16 | 20 | bool compatibilityFlag = false; |
17 | 21 |
|
| 22 | + Shape? previous = null; |
| 23 | + |
18 | 24 | using (XmlReader reader = XmlReader.Create(pStream)) |
19 | 25 | { |
20 | | - while (reader.Read()) |
| 26 | + try |
21 | 27 | { |
22 | | - //if (reader.HasAttributes) |
| 28 | + while (reader.Read()) |
23 | 29 | { |
24 | | - //Console.WriteLine("Attributes of <" + reader.Name + ">"); |
25 | | - switch (reader.Name) |
| 30 | + if (reader.HasAttributes) |
26 | 31 | { |
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 | + } |
48 | 61 | } |
| 62 | + |
49 | 63 | } |
50 | 64 | } |
| 65 | + catch (Exception) |
| 66 | + { |
| 67 | + throw new Exception("Error while importing"); |
| 68 | + } |
51 | 69 | } |
52 | | - return (shapes, compatibilityFlag); |
| 70 | + return (shapes, new SortedDictionary<int, List<Shape>>(), compatibilityFlag); |
53 | 71 | } |
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) |
55 | 80 | { |
56 | 81 | 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)); |
58 | 83 |
|
59 | 84 | byte[] byteArray = Encoding.UTF8.GetBytes(File.ReadAllText(pFilename)); |
60 | 85 |
|
61 | 86 | return ImportFromStream(new MemoryStream(byteArray)); |
62 | 87 | } |
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) |
64 | 95 | { |
65 | 96 | byte[] byteArray = Encoding.UTF8.GetBytes(pString); |
66 | 97 |
|
67 | 98 | return ImportFromStream(new MemoryStream(byteArray)); |
68 | 99 | } |
| 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 | + } |
69 | 142 | } |
70 | 143 | } |
0 commit comments