PageRenderTime 65ms CodeModel.GetById 20ms app.highlight 12ms RepoModel.GetById 18ms app.codeStats 1ms

/Utilities/Datatypes/Advanced/Margin.cs

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