TeamColor.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // The Command & Conquer Map Editor and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // The Command & Conquer Map Editor and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. using System.Drawing;
  15. using System.Numerics;
  16. using System.Xml;
  17. namespace MobiusEditor.Utility
  18. {
  19. public class TeamColor
  20. {
  21. private readonly TeamColorManager teamColorManager;
  22. private readonly MegafileManager megafileManager;
  23. public string Variant { get; private set; }
  24. public string Name { get; private set; }
  25. private Color? lowerBounds;
  26. public Color LowerBounds => lowerBounds.HasValue ? lowerBounds.Value : ((Variant != null) ? teamColorManager[Variant].LowerBounds : default);
  27. private Color? upperBounds;
  28. public Color UpperBounds => upperBounds.HasValue ? upperBounds.Value : ((Variant != null) ? teamColorManager[Variant].UpperBounds : default);
  29. private float? fudge;
  30. public float Fudge => fudge.HasValue ? fudge.Value : ((Variant != null) ? teamColorManager[Variant].Fudge : default);
  31. private Vector3? hsvShift;
  32. public Vector3 HSVShift => hsvShift.HasValue ? hsvShift.Value : ((Variant != null) ? teamColorManager[Variant].HSVShift : default);
  33. private Vector3? inputLevels;
  34. public Vector3 InputLevels => inputLevels.HasValue ? inputLevels.Value : ((Variant != null) ? teamColorManager[Variant].InputLevels : default);
  35. private Vector2? outputLevels;
  36. public Vector2 OutputLevels => outputLevels.HasValue ? outputLevels.Value : ((Variant != null) ? teamColorManager[Variant].OutputLevels : default);
  37. private Vector3? overallInputLevels;
  38. public Vector3 OverallInputLevels => overallInputLevels.HasValue ? overallInputLevels.Value : ((Variant != null) ? teamColorManager[Variant].OverallInputLevels : default);
  39. private Vector2? overallOutputLevels;
  40. public Vector2 OverallOutputLevels => overallOutputLevels.HasValue ? overallOutputLevels.Value : ((Variant != null) ? teamColorManager[Variant].OverallOutputLevels : default);
  41. private Color? radarMapColor;
  42. public Color RadarMapColor => radarMapColor.HasValue ? radarMapColor.Value : ((Variant != null) ? teamColorManager[Variant].RadarMapColor : default);
  43. public TeamColor(TeamColorManager teamColorManager, MegafileManager megafileManager)
  44. {
  45. this.teamColorManager = teamColorManager;
  46. this.megafileManager = megafileManager;
  47. }
  48. public void Load(string xml)
  49. {
  50. XmlDocument xmlDoc = new XmlDocument();
  51. xmlDoc.LoadXml(xml);
  52. var node = xmlDoc.FirstChild;
  53. Name = node.Attributes["Name"].Value;
  54. Variant = node.Attributes["Variant"]?.Value;
  55. var lowerBoundsNode = node.SelectSingleNode("LowerBounds");
  56. if (lowerBoundsNode != null)
  57. {
  58. lowerBounds = Color.FromArgb(
  59. (int)(float.Parse(lowerBoundsNode.SelectSingleNode("R").InnerText) * 255),
  60. (int)(float.Parse(lowerBoundsNode.SelectSingleNode("G").InnerText) * 255),
  61. (int)(float.Parse(lowerBoundsNode.SelectSingleNode("B").InnerText) * 255)
  62. );
  63. }
  64. var upperBoundsNode = node.SelectSingleNode("UpperBounds");
  65. if (upperBoundsNode != null)
  66. {
  67. upperBounds = Color.FromArgb(
  68. (int)(float.Parse(upperBoundsNode.SelectSingleNode("R").InnerText) * 255),
  69. (int)(float.Parse(upperBoundsNode.SelectSingleNode("G").InnerText) * 255),
  70. (int)(float.Parse(upperBoundsNode.SelectSingleNode("B").InnerText) * 255)
  71. );
  72. }
  73. var fudgeNode = node.SelectSingleNode("Fudge");
  74. if (fudgeNode != null)
  75. {
  76. fudge = float.Parse(fudgeNode.InnerText);
  77. }
  78. var hsvShiftNode = node.SelectSingleNode("HSVShift");
  79. if (hsvShiftNode != null)
  80. {
  81. hsvShift = new Vector3(
  82. float.Parse(hsvShiftNode.SelectSingleNode("X").InnerText),
  83. float.Parse(hsvShiftNode.SelectSingleNode("Y").InnerText),
  84. float.Parse(hsvShiftNode.SelectSingleNode("Z").InnerText)
  85. );
  86. }
  87. var inputLevelsNode = node.SelectSingleNode("InputLevels");
  88. if (inputLevelsNode != null)
  89. {
  90. inputLevels = new Vector3(
  91. float.Parse(inputLevelsNode.SelectSingleNode("X").InnerText),
  92. float.Parse(inputLevelsNode.SelectSingleNode("Y").InnerText),
  93. float.Parse(inputLevelsNode.SelectSingleNode("Z").InnerText)
  94. );
  95. }
  96. var outputLevelsNode = node.SelectSingleNode("OutputLevels");
  97. if (outputLevelsNode != null)
  98. {
  99. outputLevels = new Vector2(
  100. float.Parse(outputLevelsNode.SelectSingleNode("X").InnerText),
  101. float.Parse(outputLevelsNode.SelectSingleNode("Y").InnerText)
  102. );
  103. }
  104. var overallInputLevelsNode = node.SelectSingleNode("OverallInputLevels");
  105. if (overallInputLevelsNode != null)
  106. {
  107. overallInputLevels = new Vector3(
  108. float.Parse(overallInputLevelsNode.SelectSingleNode("X").InnerText),
  109. float.Parse(overallInputLevelsNode.SelectSingleNode("Y").InnerText),
  110. float.Parse(overallInputLevelsNode.SelectSingleNode("Z").InnerText)
  111. );
  112. }
  113. var overallOutputLevelsNode = node.SelectSingleNode("OverallOutputLevels");
  114. if (outputLevelsNode != null)
  115. {
  116. overallOutputLevels = new Vector2(
  117. float.Parse(overallOutputLevelsNode.SelectSingleNode("X").InnerText),
  118. float.Parse(overallOutputLevelsNode.SelectSingleNode("Y").InnerText)
  119. );
  120. }
  121. var radarMapColorNode = node.SelectSingleNode("RadarMapColor");
  122. if (radarMapColorNode != null)
  123. {
  124. radarMapColor = Color.FromArgb(
  125. (int)(float.Parse(radarMapColorNode.SelectSingleNode("R").InnerText) * 255),
  126. (int)(float.Parse(radarMapColorNode.SelectSingleNode("G").InnerText) * 255),
  127. (int)(float.Parse(radarMapColorNode.SelectSingleNode("B").InnerText) * 255)
  128. );
  129. }
  130. }
  131. public void Flatten()
  132. {
  133. lowerBounds = LowerBounds;
  134. upperBounds = UpperBounds;
  135. fudge = Fudge;
  136. hsvShift = HSVShift;
  137. inputLevels = InputLevels;
  138. outputLevels = OutputLevels;
  139. overallInputLevels = OverallInputLevels;
  140. overallOutputLevels = OverallOutputLevels;
  141. radarMapColor = RadarMapColor;
  142. }
  143. }
  144. }