PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Utilities/Datatypes/Advanced/Margin.cs

#
C# | 298 lines | 179 code | 26 blank | 93 comment | 16 complexity | dec26e36e67ab6af96b7309ed5d456a3 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System.IO;
  2. using Delta.Utilities.Helpers;
  3. using NUnit.Framework;
  4. namespace Delta.Utilities.Datatypes.Advanced
  5. {
  6. /// <summary>
  7. /// Margin helper class to allow aligning and docking UI elements with some
  8. /// spacing in between them, but can be used for any other purpose as well.
  9. /// </summary>
  10. public struct Margin : ISaveLoadBinary
  11. {
  12. #region Constants
  13. /// <summary>
  14. /// The current version of the implementation of this class.
  15. /// </summary>
  16. private const int VersionNumber = 1;
  17. #endregion
  18. #region Left (Public)
  19. /// <summary>
  20. /// Left
  21. /// </summary>
  22. public float Left;
  23. #endregion
  24. #region Top (Public)
  25. /// <summary>
  26. /// Top
  27. /// </summary>
  28. public float Top;
  29. #endregion
  30. #region Right (Public)
  31. /// <summary>
  32. /// Right
  33. /// </summary>
  34. public float Right;
  35. #endregion
  36. #region Bottom (Public)
  37. /// <summary>
  38. /// Bottom
  39. /// </summary>
  40. public float Bottom;
  41. #endregion
  42. #region Constructors
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="Spacing"/> struct.
  45. /// </summary>
  46. /// <param name="setOverallSpacing">Set overall spacing</param>
  47. public Margin(float setOverallSpacing)
  48. : this(setOverallSpacing, setOverallSpacing)
  49. {
  50. }
  51. /// <summary>
  52. /// Initializes a new instance of the <see cref="Spacing"/> struct.
  53. /// </summary>
  54. /// <param name="setHorizontal">Set horizontal</param>
  55. /// <param name="setVertical">Set vertical</param>
  56. public Margin(float setHorizontal, float setVertical)
  57. {
  58. Left = setHorizontal;
  59. Right = setHorizontal;
  60. Top = setVertical;
  61. Bottom = setVertical;
  62. }
  63. /// <summary>
  64. /// Create spacing
  65. /// </summary>
  66. /// <param name="setDataReader">Set data reader</param>
  67. public Margin(BinaryReader setDataReader)
  68. : this()
  69. {
  70. Load(setDataReader);
  71. }
  72. #endregion
  73. #region ISaveLoadBinary Members
  74. /// <summary>
  75. /// Loads all data of the object again which were previously saved.
  76. /// </summary>
  77. /// <param name="dataReader">
  78. /// The container object which contains the data which were saved before.
  79. /// </param>
  80. public void Load(BinaryReader dataReader)
  81. {
  82. // We currently only support our version, if more versions are added,
  83. // we need to do different loading code depending on the version here.
  84. int version = dataReader.ReadInt32();
  85. switch (version)
  86. {
  87. // Version 1
  88. case VersionNumber:
  89. Left = dataReader.ReadSingle();
  90. Top = dataReader.ReadSingle();
  91. Right = dataReader.ReadSingle();
  92. Bottom = dataReader.ReadSingle();
  93. break;
  94. default:
  95. Log.InvalidVersionWarning(GetType().Name, version, VersionNumber);
  96. break;
  97. } // switch
  98. }
  99. /// <summary>
  100. /// Saves all necessary data of the object into a binary stream.
  101. /// </summary>
  102. /// <param name="dataWriter">
  103. /// The container object which will store all the saved data.
  104. /// </param>
  105. public void Save(BinaryWriter dataWriter)
  106. {
  107. // At first we write the current version number of the class data format
  108. dataWriter.Write(VersionNumber);
  109. // Now we save all the values for that instance
  110. dataWriter.Write(Left);
  111. dataWriter.Write(Top);
  112. dataWriter.Write(Right);
  113. dataWriter.Write(Bottom);
  114. }
  115. #endregion
  116. #region op_Equality (Operator)
  117. /// <summary>
  118. /// Check for equality
  119. /// </summary>
  120. /// <param name="value1">Margin value 1</param>
  121. /// <param name="value2">Margin value 2</param>
  122. /// <returns>
  123. /// True if the margin values are equal, false otherwise.
  124. /// </returns>
  125. public static bool operator ==(Margin value1, Margin value2)
  126. {
  127. return
  128. value1.Left == value2.Left &&
  129. value1.Top == value2.Top &&
  130. value1.Right == value2.Right &&
  131. value1.Bottom == value2.Bottom;
  132. }
  133. #endregion
  134. #region op_Inequality (Operator)
  135. /// <summary>
  136. /// Check for inequality
  137. /// </summary>
  138. /// <param name="value1">Margin value 1</param>
  139. /// <param name="value2">Margin value 2</param>
  140. /// <returns>
  141. /// True if the margin values are not equal, false otherwise.
  142. /// </returns>
  143. public static bool operator !=(Margin value1, Margin value2)
  144. {
  145. return
  146. value1.Left != value2.Left ||
  147. value1.Top != value2.Top ||
  148. value1.Right != value2.Right ||
  149. value1.Bottom != value2.Bottom;
  150. }
  151. #endregion
  152. #region Equals (Public)
  153. /// <summary>
  154. /// Check if the object is a Margin instance and then compare them.
  155. /// </summary>
  156. /// <param name="obj">Object to check.</param>
  157. /// <returns>True if object is equal to this instance, otherwise False.
  158. /// </returns>
  159. public override bool Equals(object obj)
  160. {
  161. if (obj.GetType() == typeof(Margin))
  162. {
  163. return this == (Margin)obj;
  164. }
  165. return false;
  166. }
  167. #endregion
  168. #region GetHashCode (Public)
  169. /// <summary>
  170. /// Get the hash code.
  171. /// </summary>
  172. /// <returns>Hash code.</returns>
  173. public override int GetHashCode()
  174. {
  175. return Left.GetHashCode() ^ Top.GetHashCode() ^
  176. Right.GetHashCode() ^ Bottom.GetHashCode();
  177. }
  178. #endregion
  179. #region ToString (Public)
  180. /// <summary>
  181. /// To string
  182. /// </summary>
  183. public override string ToString()
  184. {
  185. return GetType().Name + "(" +
  186. Left.ToInvariantString("0.0000") + ", " +
  187. Top.ToInvariantString("0.0000") + ", " +
  188. Right.ToInvariantString("0.0000") + ", " +
  189. Bottom.ToInvariantString("0.0000") + ")";
  190. }
  191. #endregion
  192. /// <summary>
  193. /// Margin test class.
  194. /// </summary>
  195. internal class MarginTests
  196. {
  197. #region Constructor (Static)
  198. /// <summary>
  199. /// Test the constructor of the Margin class.
  200. /// </summary>
  201. [Test]
  202. public static void Constructor()
  203. {
  204. Margin testMargin = new Margin();
  205. Assert.NearlyEqual(testMargin.Left, 0f);
  206. Assert.NearlyEqual(testMargin.Right, 0f);
  207. Assert.NearlyEqual(testMargin.Top, 0f);
  208. Assert.NearlyEqual(testMargin.Bottom, 0f);
  209. testMargin = new Margin(0.1f);
  210. Assert.NearlyEqual(testMargin.Left, 0.1f);
  211. Assert.NearlyEqual(testMargin.Right, 0.1f);
  212. Assert.NearlyEqual(testMargin.Top, 0.1f);
  213. Assert.NearlyEqual(testMargin.Bottom, 0.1f);
  214. testMargin = new Margin(0.05f, 0.1f);
  215. Assert.NearlyEqual(testMargin.Left, 0.05f);
  216. Assert.NearlyEqual(testMargin.Right, 0.05f);
  217. Assert.NearlyEqual(testMargin.Top, 0.1f);
  218. Assert.NearlyEqual(testMargin.Bottom, 0.1f);
  219. }
  220. #endregion
  221. #region Equality (Static)
  222. /// <summary>
  223. /// Test the equality methods.
  224. /// </summary>
  225. [Test]
  226. public void Equality()
  227. {
  228. Margin testMargin1 = new Margin();
  229. Margin testMargin2 = new Margin(0.3f, 0.3f);
  230. Margin testMargin3 = new Margin(0.3f);
  231. Assert.Equal(testMargin2, testMargin3);
  232. Assert.NotEqual(testMargin1, testMargin2);
  233. Assert.True(testMargin2 == testMargin3);
  234. Assert.False(testMargin1 == testMargin2);
  235. Assert.False(testMargin2 != testMargin3);
  236. Assert.True(testMargin1 != testMargin2);
  237. }
  238. #endregion
  239. #region ToString (Static)
  240. /// <summary>
  241. /// Test the ToString method.
  242. /// </summary>
  243. [Test]
  244. public new void ToString()
  245. {
  246. Margin testMargin = new Margin(0.3f, 0.1f);
  247. Assert.Equal(testMargin.ToString(),
  248. "Margin(0.3000, 0.1000, 0.3000, 0.1000)");
  249. }
  250. #endregion
  251. #region SaveLoad (Static)
  252. /// <summary>
  253. /// Test the Save and Load methods.
  254. /// </summary>
  255. [Test]
  256. public void SaveLoad()
  257. {
  258. Margin testMargin = new Margin(0.3f, 0.1f);
  259. MemoryStream stream = new MemoryStream();
  260. BinaryWriter writer = new BinaryWriter(stream);
  261. testMargin.Save(writer);
  262. writer.Flush();
  263. writer = null;
  264. stream.Position = 0;
  265. BinaryReader reader = new BinaryReader(stream);
  266. Margin newMargin = new Margin(reader);
  267. Assert.Equal(testMargin, newMargin);
  268. }
  269. #endregion
  270. }
  271. }
  272. }