PageRenderTime 123ms CodeModel.GetById 80ms app.highlight 5ms RepoModel.GetById 35ms app.codeStats 0ms

/ContentSystem/UserInterfaces/BaseUIScreenData.cs

#
C# | 163 lines | 92 code | 15 blank | 56 comment | 4 complexity | 12b5ae38f5970073bd4ace5c53ae57ee MD5 | raw file
  1using System;
  2using System.IO;
  3using Delta.Engine;
  4using Delta.Utilities;
  5using Delta.Utilities.Datatypes;
  6using Delta.Utilities.Helpers;
  7
  8namespace Delta.ContentSystem.UserInterfaces
  9{
 10	/// <summary>
 11	/// The basic content class which is the base of all UI screen classes.
 12	/// Usually contains some screen global data, a link to the used UI Theme and
 13	/// a lot of controls in the form of BaseControlData.
 14	/// </summary>
 15	public abstract class BaseUIScreenData : Content, ISaveLoadBinary
 16	{
 17		#region Constants
 18		/// <summary>
 19		/// The newest version number of the screen data.
 20		/// </summary>
 21		private const int VersionNumber = 1;
 22		#endregion
 23
 24		#region Get (Static)
 25		/// <summary>
 26		/// Get and load content based on the content name. This method makes sure
 27		/// we do not load the same content twice (the constructor is protected).
 28		/// </summary>
 29		/// <param name="contentName">Content name we want to load, this is
 30		/// passed onto the Content System, which will do the actual loading with
 31		/// help of the Load method in this class.</param>
 32		/// <returns>The loaded Content object, always unique for the same
 33		/// name, this helps comparing data.</returns>
 34		public static BaseUIScreenData Get(string contentName)
 35		{
 36			return Get<BaseUIScreenData>(contentName, ContentType.UIScreen);
 37		}
 38		#endregion
 39
 40		#region DrawArea (Public)
 41		/// <summary>
 42		/// By default we render to Rectangle.One, which will be converted
 43		/// to Graphic.Instance.DrawArea, else use this size as a draw area!
 44		/// Note: Only used for UIScene.BackgroundDesign.Draw
 45		/// </summary>
 46		public Rectangle DrawArea
 47		{
 48			get
 49			{
 50				return (drawArea.HasValue)
 51				       	? drawArea.Value
 52				       	: ScreenSpace.DrawArea;
 53			} // get
 54			protected set
 55			{
 56				drawArea = (value != Rectangle.Zero)
 57				           	? drawArea = value
 58				           	: drawArea = null;
 59			} // set
 60		}
 61		#endregion
 62
 63		#region Private
 64
 65		#region drawArea (Private)
 66		/// <summary>
 67		/// Draw area
 68		/// </summary>
 69		private Rectangle? drawArea;
 70		#endregion
 71
 72		#endregion
 73
 74		#region Constructors
 75		/// <summary>
 76		/// Create scene content based on the content system. Loading happens in
 77		/// the Load method, all content is loaded right away.
 78		/// </summary>
 79		/// <param name="setContentName">Name of the scene name to load.</param>
 80		protected BaseUIScreenData(string setContentName)
 81			: base(setContentName, ContentType.UIScreen)
 82		{
 83		}
 84		#endregion
 85
 86		#region ISaveLoadBinary Members
 87		/// <summary>
 88		/// Loads all data of the object again which were previously saved.
 89		/// </summary>
 90		/// <param name="dataReader">
 91		/// The container object which contains the data which were saved before.
 92		/// </param>
 93		public virtual void Load(BinaryReader dataReader)
 94		{
 95			int version = dataReader.ReadInt32();
 96
 97			switch (version)
 98			{
 99					// Version 1
100				case VersionNumber:
101					DrawArea = new Rectangle(dataReader);
102					break;
103
104				default:
105					Log.InvalidVersionWarning(GetType().Name, version, VersionNumber);
106					return;
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 virtual void Save(BinaryWriter dataWriter)
117		{
118			dataWriter.Write(VersionNumber);
119
120			// Determine if we have to save a custom screen area
121			Rectangle areaToSave = (drawArea.HasValue)
122			                       	? drawArea.Value
123			                       	: Rectangle.Zero;
124			areaToSave.Save(dataWriter);
125		}
126		#endregion
127
128		#region Methods (Private)
129
130		#region Load
131		/// <summary>
132		/// Native load method, will just load the data.
133		/// </summary>
134		/// <param name="alreadyLoadedNativeData">
135		/// The first instance that has already loaded the required content data
136		/// of this content class or just 'null' if there is none loaded yet (or
137		/// anymore).
138		/// </param>
139		protected override void Load(Content alreadyLoadedNativeData)
140		{
141			try
142			{
143				// Note: Cloning is not needed for UI Screens (each is unique and we
144				// don't share content or names, we can always load the same one again)
145				if (String.IsNullOrEmpty(RelativeFilePath) == false)
146				{
147					// Load via the ISaveLoadBinary interface methods below.
148					// Cloning should not really happen for screens anyway.
149					FileHelper.Load(RelativeFilePath, this);
150				}
151			}
152			catch (Exception ex)
153			{
154				Log.Warning("Failed to load mesh animation data from file '" +
155				            RelativeFilePath + "': " + ex);
156				FailedToLoad = true;
157			}
158		}
159		#endregion
160
161		#endregion
162	}
163}