/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
- using System.IO;
- using Delta.Utilities.Helpers;
- using NUnit.Framework;
-
- namespace Delta.Utilities.Datatypes.Advanced
- {
- /// <summary>
- /// Margin helper class to allow aligning and docking UI elements with some
- /// spacing in between them, but can be used for any other purpose as well.
- /// </summary>
- public struct Margin : ISaveLoadBinary
- {
- #region Constants
- /// <summary>
- /// The current version of the implementation of this class.
- /// </summary>
- private const int VersionNumber = 1;
- #endregion
-
- #region Left (Public)
- /// <summary>
- /// Left
- /// </summary>
- public float Left;
- #endregion
-
- #region Top (Public)
- /// <summary>
- /// Top
- /// </summary>
- public float Top;
- #endregion
-
- #region Right (Public)
- /// <summary>
- /// Right
- /// </summary>
- public float Right;
- #endregion
-
- #region Bottom (Public)
- /// <summary>
- /// Bottom
- /// </summary>
- public float Bottom;
- #endregion
-
- #region Constructors
- /// <summary>
- /// Initializes a new instance of the <see cref="Spacing"/> struct.
- /// </summary>
- /// <param name="setOverallSpacing">Set overall spacing</param>
- public Margin(float setOverallSpacing)
- : this(setOverallSpacing, setOverallSpacing)
- {
- }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="Spacing"/> struct.
- /// </summary>
- /// <param name="setHorizontal">Set horizontal</param>
- /// <param name="setVertical">Set vertical</param>
- public Margin(float setHorizontal, float setVertical)
- {
- Left = setHorizontal;
- Right = setHorizontal;
- Top = setVertical;
- Bottom = setVertical;
- }
-
- /// <summary>
- /// Create spacing
- /// </summary>
- /// <param name="setDataReader">Set data reader</param>
- public Margin(BinaryReader setDataReader)
- : this()
- {
- Load(setDataReader);
- }
- #endregion
-
- #region ISaveLoadBinary Members
- /// <summary>
- /// Loads all data of the object again which were previously saved.
- /// </summary>
- /// <param name="dataReader">
- /// The container object which contains the data which were saved before.
- /// </param>
- public void Load(BinaryReader dataReader)
- {
- // We currently only support our version, if more versions are added,
- // we need to do different loading code depending on the version here.
- int version = dataReader.ReadInt32();
- switch (version)
- {
- // Version 1
- case VersionNumber:
- Left = dataReader.ReadSingle();
- Top = dataReader.ReadSingle();
- Right = dataReader.ReadSingle();
- Bottom = dataReader.ReadSingle();
- break;
-
- default:
- Log.InvalidVersionWarning(GetType().Name, version, VersionNumber);
- break;
- } // switch
- }
-
- /// <summary>
- /// Saves all necessary data of the object into a binary stream.
- /// </summary>
- /// <param name="dataWriter">
- /// The container object which will store all the saved data.
- /// </param>
- public void Save(BinaryWriter dataWriter)
- {
- // At first we write the current version number of the class data format
- dataWriter.Write(VersionNumber);
-
- // Now we save all the values for that instance
- dataWriter.Write(Left);
- dataWriter.Write(Top);
- dataWriter.Write(Right);
- dataWriter.Write(Bottom);
- }
- #endregion
-
- #region op_Equality (Operator)
- /// <summary>
- /// Check for equality
- /// </summary>
- /// <param name="value1">Margin value 1</param>
- /// <param name="value2">Margin value 2</param>
- /// <returns>
- /// True if the margin values are equal, false otherwise.
- /// </returns>
- public static bool operator ==(Margin value1, Margin value2)
- {
- return
- value1.Left == value2.Left &&
- value1.Top == value2.Top &&
- value1.Right == value2.Right &&
- value1.Bottom == value2.Bottom;
- }
- #endregion
-
- #region op_Inequality (Operator)
- /// <summary>
- /// Check for inequality
- /// </summary>
- /// <param name="value1">Margin value 1</param>
- /// <param name="value2">Margin value 2</param>
- /// <returns>
- /// True if the margin values are not equal, false otherwise.
- /// </returns>
- public static bool operator !=(Margin value1, Margin value2)
- {
- return
- value1.Left != value2.Left ||
- value1.Top != value2.Top ||
- value1.Right != value2.Right ||
- value1.Bottom != value2.Bottom;
- }
- #endregion
-
- #region Equals (Public)
- /// <summary>
- /// Check if the object is a Margin instance and then compare them.
- /// </summary>
- /// <param name="obj">Object to check.</param>
- /// <returns>True if object is equal to this instance, otherwise False.
- /// </returns>
- public override bool Equals(object obj)
- {
- if (obj.GetType() == typeof(Margin))
- {
- return this == (Margin)obj;
- }
- return false;
- }
- #endregion
-
- #region GetHashCode (Public)
- /// <summary>
- /// Get the hash code.
- /// </summary>
- /// <returns>Hash code.</returns>
- public override int GetHashCode()
- {
- return Left.GetHashCode() ^ Top.GetHashCode() ^
- Right.GetHashCode() ^ Bottom.GetHashCode();
- }
- #endregion
-
- #region ToString (Public)
- /// <summary>
- /// To string
- /// </summary>
- public override string ToString()
- {
- return GetType().Name + "(" +
- Left.ToInvariantString("0.0000") + ", " +
- Top.ToInvariantString("0.0000") + ", " +
- Right.ToInvariantString("0.0000") + ", " +
- Bottom.ToInvariantString("0.0000") + ")";
- }
- #endregion
-
- /// <summary>
- /// Margin test class.
- /// </summary>
- internal class MarginTests
- {
- #region Constructor (Static)
- /// <summary>
- /// Test the constructor of the Margin class.
- /// </summary>
- [Test]
- public static void Constructor()
- {
- Margin testMargin = new Margin();
- Assert.NearlyEqual(testMargin.Left, 0f);
- Assert.NearlyEqual(testMargin.Right, 0f);
- Assert.NearlyEqual(testMargin.Top, 0f);
- Assert.NearlyEqual(testMargin.Bottom, 0f);
-
- testMargin = new Margin(0.1f);
- Assert.NearlyEqual(testMargin.Left, 0.1f);
- Assert.NearlyEqual(testMargin.Right, 0.1f);
- Assert.NearlyEqual(testMargin.Top, 0.1f);
- Assert.NearlyEqual(testMargin.Bottom, 0.1f);
-
- testMargin = new Margin(0.05f, 0.1f);
- Assert.NearlyEqual(testMargin.Left, 0.05f);
- Assert.NearlyEqual(testMargin.Right, 0.05f);
- Assert.NearlyEqual(testMargin.Top, 0.1f);
- Assert.NearlyEqual(testMargin.Bottom, 0.1f);
- }
- #endregion
-
- #region Equality (Static)
- /// <summary>
- /// Test the equality methods.
- /// </summary>
- [Test]
- public void Equality()
- {
- Margin testMargin1 = new Margin();
- Margin testMargin2 = new Margin(0.3f, 0.3f);
- Margin testMargin3 = new Margin(0.3f);
- Assert.Equal(testMargin2, testMargin3);
- Assert.NotEqual(testMargin1, testMargin2);
- Assert.True(testMargin2 == testMargin3);
- Assert.False(testMargin1 == testMargin2);
- Assert.False(testMargin2 != testMargin3);
- Assert.True(testMargin1 != testMargin2);
- }
- #endregion
-
- #region ToString (Static)
- /// <summary>
- /// Test the ToString method.
- /// </summary>
- [Test]
- public new void ToString()
- {
- Margin testMargin = new Margin(0.3f, 0.1f);
- Assert.Equal(testMargin.ToString(),
- "Margin(0.3000, 0.1000, 0.3000, 0.1000)");
- }
- #endregion
-
- #region SaveLoad (Static)
- /// <summary>
- /// Test the Save and Load methods.
- /// </summary>
- [Test]
- public void SaveLoad()
- {
- Margin testMargin = new Margin(0.3f, 0.1f);
-
- MemoryStream stream = new MemoryStream();
- BinaryWriter writer = new BinaryWriter(stream);
- testMargin.Save(writer);
- writer.Flush();
- writer = null;
-
- stream.Position = 0;
- BinaryReader reader = new BinaryReader(stream);
- Margin newMargin = new Margin(reader);
-
- Assert.Equal(testMargin, newMargin);
- }
- #endregion
- }
- }
- }