ParserHelpers.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using PixiEditor.Models.DataHolders;
  2. using PixiEditor.Models.Layers;
  3. using PixiEditor.Parser;
  4. using PixiEditor.Parser.Skia;
  5. using SkiaSharp;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. namespace PixiEditor.Helpers.Extensions
  10. {
  11. public static class ParserHelpers
  12. {
  13. public static Document ToDocument(this SerializableDocument serializableDocument)
  14. {
  15. Document document = new Document(serializableDocument.Width, serializableDocument.Height)
  16. {
  17. Layers = serializableDocument.ToLayers(),
  18. Swatches = new WpfObservableRangeCollection<SKColor>(serializableDocument.Swatches.ToSKColors()),
  19. Palette = new WpfObservableRangeCollection<SKColor>(serializableDocument.Palette.ToSKColors())
  20. };
  21. document.LayerStructure.Groups = serializableDocument.ToGroups(document);
  22. if (document.Layers.Count > 0)
  23. {
  24. document.SetMainActiveLayer(0);
  25. }
  26. document.Renderer.ForceRerender();
  27. return document;
  28. }
  29. public static WpfObservableRangeCollection<Layer> ToLayers(this SerializableDocument document)
  30. {
  31. WpfObservableRangeCollection<Layer> layers = new();
  32. foreach (SerializableLayer slayer in document)
  33. {
  34. layers.Add(slayer.ToLayer(document.Width, document.Height));
  35. }
  36. return layers;
  37. }
  38. public static Layer ToLayer(this SerializableLayer layer, int maxWidth, int maxHeight)
  39. {
  40. return new Layer(layer.Name, new Surface(layer.ToSKImage()), maxWidth, maxHeight)
  41. {
  42. Opacity = layer.Opacity,
  43. IsVisible = layer.IsVisible,
  44. Offset = new(layer.OffsetX, layer.OffsetY, 0, 0),
  45. };
  46. }
  47. public static WpfObservableRangeCollection<GuidStructureItem> ToGroups(this SerializableDocument sdocument, Document document)
  48. {
  49. WpfObservableRangeCollection<GuidStructureItem> groups = new();
  50. if (sdocument.Groups == null)
  51. {
  52. return groups;
  53. }
  54. foreach (SerializableGroup sgroup in sdocument.Groups)
  55. {
  56. groups.Add(sgroup.ToGroup(null, document));
  57. }
  58. return groups;
  59. }
  60. public static GuidStructureItem ToGroup(this SerializableGroup sgroup, GuidStructureItem parent, Document document)
  61. {
  62. GuidStructureItem group = new GuidStructureItem(sgroup.Name, Guid.Empty)
  63. {
  64. Opacity = sgroup.Opacity,
  65. IsVisible = sgroup.IsVisible,
  66. Parent = parent,
  67. StartLayerGuid = document.Layers[sgroup.StartLayer].GuidValue,
  68. EndLayerGuid = document.Layers[sgroup.EndLayer].GuidValue
  69. };
  70. group.Subgroups = new(sgroup.Subgroups.ToGroups(document, group));
  71. return group;
  72. }
  73. public static SerializableDocument ToSerializable(this Document document)
  74. {
  75. return new SerializableDocument(document.Width, document.Height,
  76. document.LayerStructure.Groups.ToSerializable(document),
  77. document.Layers.ToSerializable())
  78. .AddSwatches(document.Swatches)
  79. .AddPalette(document.Palette);
  80. }
  81. public static IEnumerable<SerializableLayer> ToSerializable(this IEnumerable<Layer> layers)
  82. {
  83. foreach (Layer layer in layers)
  84. {
  85. yield return layer.ToSerializable();
  86. }
  87. }
  88. public static SerializableLayer ToSerializable(this Layer layer)
  89. {
  90. return new SerializableLayer(layer.Width, layer.Height, layer.OffsetX, layer.OffsetY)
  91. {
  92. IsVisible = layer.IsVisible,
  93. Opacity = layer.Opacity,
  94. Name = layer.Name
  95. }.FromSKImage(layer.LayerBitmap.SkiaSurface.Snapshot());
  96. }
  97. public static IEnumerable<SerializableGroup> ToSerializable(this IEnumerable<GuidStructureItem> groups, Document document)
  98. {
  99. foreach (GuidStructureItem group in groups)
  100. {
  101. yield return group.ToSerializable(document);
  102. }
  103. }
  104. public static SerializableGroup ToSerializable(this GuidStructureItem group, Document document)
  105. {
  106. SerializableGroup serializable = new SerializableGroup(group.Name, group.Subgroups.ToSerializable(document))
  107. {
  108. Opacity = group.Opacity,
  109. IsVisible = group.IsVisible
  110. };
  111. for (int i = 0; i < document.Layers.Count; i++)
  112. {
  113. if (group.StartLayerGuid == document.Layers[i].GuidValue)
  114. {
  115. serializable.StartLayer = i;
  116. }
  117. if (group.EndLayerGuid == document.Layers[i].GuidValue)
  118. {
  119. serializable.EndLayer = i;
  120. }
  121. }
  122. return serializable;
  123. }
  124. private static IEnumerable<GuidStructureItem> ToGroups(this IEnumerable<SerializableGroup> groups, Document document, GuidStructureItem parent)
  125. {
  126. foreach (SerializableGroup sgroup in groups)
  127. {
  128. yield return sgroup.ToGroup(parent, document);
  129. }
  130. }
  131. private static SerializableDocument AddSwatches(this SerializableDocument document, IEnumerable<SKColor> colors)
  132. {
  133. document.Swatches.AddRange(colors);
  134. return document;
  135. }
  136. private static SerializableDocument AddPalette(this SerializableDocument document, IEnumerable<SKColor> palette)
  137. {
  138. document.Palette.AddRange(palette);
  139. return document;
  140. }
  141. }
  142. }