PageRenderTime 232ms CodeModel.GetById 101ms app.highlight 4ms RepoModel.GetById 86ms app.codeStats 0ms

/ContentSystem/UserInterfaces/BaseControlData.cs

#
C# | 163 lines | 102 code | 19 blank | 42 comment | 5 complexity | 118f42b6d8fe307396f267c5a1a2d8e9 MD5 | raw file
  1using System;
  2using System.ComponentModel;
  3using System.IO;
  4using Delta.Engine.Dynamic;
  5using Delta.Utilities;
  6using Delta.Utilities.Helpers;
  7
  8namespace Delta.ContentSystem.UserInterfaces
  9{
 10	/// <summary>
 11	/// The data container class which contains the basic data of a control.
 12	/// This data is used in UI Screen content data.
 13	/// </summary>
 14	public abstract class BaseControlData : ISaveLoadBinary, IDisposable
 15	{
 16		#region Constants
 17		/// <summary>
 18		/// The version number of the class implementation.
 19		/// </summary>
 20		private const int VersionNumber = 1;
 21		#endregion
 22
 23		#region Public
 24
 25		#region Name
 26		/// <summary>
 27		/// Name of this element.
 28		/// </summary>
 29		[Browsable(true)]
 30		public string Name
 31		{
 32			get;
 33			set;
 34		}
 35		#endregion
 36
 37
 38		#region Rotation
 39
 40		/// <summary>
 41		/// Rotation
 42		/// </summary>
 43		private float rotation;
 44
 45		/// <summary>
 46		/// Faster, but we need to update Parent Rotation ourself
 47		/// </summary>
 48		public float Rotation
 49		{
 50			get
 51			{
 52				return rotation;
 53			} // get
 54			set
 55			{
 56				rotation = value;
 57				if (rotation != 0.0f)
 58				{
 59					Log.Info(
 60						"TODO: The rotation of UI controls is currently not supported.");
 61					rotation = 0.0f;
 62				} // if
 63			} // set
 64		}
 65		#endregion
 66
 67		#region ToolTipText
 68		/// <summary>
 69		/// The info text about this element.
 70		/// </summary>
 71		/// <value>The tool tip text.</value>
 72		public string ToolTipText
 73		{
 74			get;
 75			set;
 76		}
 77		#endregion
 78
 79		#region DataBag
 80		/// <summary>
 81		/// The "bag" to store any custom data for this element.
 82		/// </summary>
 83		public ISaveLoadBinary DataBag
 84		{
 85			get;
 86			set;
 87		}
 88		#endregion
 89
 90		#endregion
 91
 92		#region ISaveLoadBinary Methods (virtual)
 93
 94		#region Save (virtual)
 95		/// <summary>
 96		/// Saves all necessary data of the object into a binary stream.
 97		/// </summary>
 98		/// <param name="dataWriter">
 99		/// The container object which will store all the saved data.
100		/// </param>
101		public virtual void Save(BinaryWriter dataWriter)
102		{
103			dataWriter.Write(VersionNumber);
104
105			Name.Save(dataWriter);
106			// Make sure the text we save is a valid string.
107			ToolTipText.Save(dataWriter);
108			dataWriter.Write(Rotation);
109
110			// incl. the "DataBag" values
111			bool isDataBagUsed = DataBag != null;
112			dataWriter.Write(isDataBagUsed);
113			if (isDataBagUsed)
114			{
115				Factory.Save(dataWriter, DataBag);
116			} // if
117		}
118		#endregion
119
120		#region Load (virtual)
121		/// <summary>
122		/// Loads all data of the object again which were previously saved.
123		/// </summary>
124		/// <param name="dataReader">
125		/// The container object which contains the data which were saved before.
126		/// </param>
127		public virtual void Load(BinaryReader dataReader)
128		{
129			int version = dataReader.ReadInt32();
130			switch (version)
131			{
132				case 1:
133					Name = dataReader.ReadString();
134					ToolTipText = dataReader.ReadString();
135					Rotation = dataReader.ReadSingle();
136
137					// Include the "DataBag" values
138					DataBag = dataReader.ReadBoolean()
139					          	? Factory.Load<ISaveLoadBinary>(dataReader)
140					          	: null;
141					break;
142
143				default:
144					Log.Warning("Failed to load the ControlData because the version " +
145					            "number " + version + " is not supported.");
146					break;
147			} // switch
148		}
149		#endregion
150
151		#endregion
152
153		#region IDisposable Members (virtual)
154		/// <summary>
155		/// Dispose
156		/// </summary>
157		public virtual void Dispose()
158		{
159			// Nothing to here and the 'DataBag' doesn't need disposing yet
160		}
161		#endregion
162	}
163}