Vinno2DVisual.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System.IO;
  2. using System.Collections.Generic;
  3. namespace fis.Vid.Visuals
  4. {
  5. public class Vinno2DVisual:VinnoVisual
  6. {
  7. /// <summary>
  8. /// Gets all LogicalCoordinates of this visual.
  9. /// </summary>
  10. public Dictionary<VinnoVisualAreaType, VinnoLogicalCoordinate> LogicalCoordinates { get; }
  11. /// <summary>
  12. /// Gets all PhysicalCoordinates of this visual.
  13. /// </summary>
  14. public Dictionary<VinnoVisualAreaType, VinnoPhysicalCoordinate> PhysicalCoordinates { get; }
  15. public Vinno2DVisual()
  16. {
  17. VisualType = VinnoVisualType.V2D;
  18. LogicalCoordinates = new Dictionary<VinnoVisualAreaType, VinnoLogicalCoordinate>();
  19. PhysicalCoordinates = new Dictionary<VinnoVisualAreaType, VinnoPhysicalCoordinate>();
  20. }
  21. public override byte[] ToBytes()
  22. {
  23. var baseData = base.ToBytes();
  24. byte[] result;
  25. using (var stream = new MemoryStream())
  26. {
  27. stream.Write(baseData,0, baseData.Length);
  28. var writer = new VinnoStreamWriter(stream);
  29. writer.WriteByte((byte)PhysicalCoordinates.Count);
  30. foreach (var key in PhysicalCoordinates.Keys)
  31. {
  32. writer.WriteByte((byte)key);
  33. writer.WriteBytes(PhysicalCoordinates[key].ToBytes());
  34. }
  35. writer.WriteByte((byte)LogicalCoordinates.Count);
  36. foreach (var key in LogicalCoordinates.Keys)
  37. {
  38. writer.WriteByte((byte)key);
  39. writer.WriteBytes(LogicalCoordinates[key].ToBytes());
  40. }
  41. result = stream.ToArray();
  42. }
  43. return result;
  44. }
  45. public new static VinnoVisual FromBytes(byte[] bytes)
  46. {
  47. Vinno2DVisual result = null;
  48. using (var stream = new MemoryStream(bytes))
  49. {
  50. stream.Position = 0;
  51. var reader = new VinnoStreamReader(stream);
  52. var visualType = (VinnoVisualType)reader.ReadByte();
  53. var displayMode = (VinnoDisplayMode)reader.ReadByte();
  54. if (visualType == VinnoVisualType.V2D)
  55. {
  56. var indicator = (VinnoVisualIndicator) reader.ReadByte();
  57. var activeModeType = (VinnoModeType)reader.ReadByte();
  58. var modeCount = reader.ReadByte();
  59. var modes = new List<VinnoMode>();
  60. for (var i = 0; i < modeCount; i++)
  61. {
  62. var mode = VinnoMode.FromBytes(reader.ReadBytes());
  63. modes.Add(mode);
  64. }
  65. result = new Vinno2DVisual {Indicator = indicator };
  66. foreach (var mode in modes)
  67. {
  68. result.Modes.Add(mode);
  69. }
  70. result.ActiveModeType = activeModeType;
  71. var physicalCoordinateCount = reader.ReadByte();
  72. var physicalCoordinates = new Dictionary<VinnoVisualAreaType, VinnoPhysicalCoordinate>();
  73. for (var i = 0; i < physicalCoordinateCount; i++)
  74. {
  75. var visualAreaType = (VinnoVisualAreaType)reader.ReadByte();
  76. var physicalCoordinate = VinnoPhysicalCoordinate.FromBytes(reader.ReadBytes());
  77. physicalCoordinates.Add(visualAreaType, physicalCoordinate);
  78. }
  79. var logicalCoordinateCount = reader.ReadByte();
  80. var logicalCoordinates = new Dictionary<VinnoVisualAreaType, VinnoLogicalCoordinate>();
  81. for (var i = 0; i < logicalCoordinateCount; i++)
  82. {
  83. var visualAreaType = (VinnoVisualAreaType)reader.ReadByte();
  84. var logicalCoordinate = VinnoLogicalCoordinate.FromBytes(reader.ReadBytes());
  85. logicalCoordinates.Add(visualAreaType, logicalCoordinate);
  86. }
  87. foreach (var key in physicalCoordinates.Keys)
  88. {
  89. result.PhysicalCoordinates.Add(key, physicalCoordinates[key]);
  90. }
  91. foreach (var key in logicalCoordinates.Keys)
  92. {
  93. result.LogicalCoordinates.Add(key, logicalCoordinates[key]);
  94. }
  95. result.DisplayMode = displayMode;
  96. }
  97. }
  98. return result;
  99. }
  100. }
  101. }