PageRenderTime 167ms CodeModel.GetById 155ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/ContentSystem/UserInterfaces/SceneData.cs

#
C# | 264 lines | 155 code | 27 blank | 82 comment | 11 complexity | 9e432fb0828d63fe8b8141959e9de726 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.IO;
  4using Delta.Utilities;
  5using Delta.Utilities.Helpers;
  6
  7namespace Delta.ContentSystem.UserInterfaces
  8{
  9	/// <summary>
 10	/// Scene data content class, which loads all the required data for a scene.
 11	/// Most importantly we always have only one scene active. All content that
 12	/// is loaded during the time that scene is open will automatically be
 13	/// disposed when closing the scene. This way we can open new scenes and
 14	/// make sure all content from the previous scenes has been unloaded. If
 15	/// you load new scenes while the old one is still open (stack add), you can
 16	/// load additional content, but nothing gets disposed automatically.
 17	/// <para />
 18	/// Note: This class is not publicly available in the namespace
 19	/// Delta.ContentSystem.UserInterfaces because it is sealed and only used
 20	/// in the also sealed and protected Scene class (in Delta.Scenes).
 21	/// </summary>
 22	public sealed class SceneData : EmptySceneData, ISaveLoadBinary
 23	{
 24		#region Constants
 25		/// <summary>
 26		/// The current version of the implementation of this class.
 27		/// </summary>
 28		private const int VersionNumber = 1;
 29		#endregion
 30
 31		#region Get (Static)
 32		/// <summary>
 33		/// This is the only method to load SceneData content. If a content object
 34		/// has already been loaded, it will be returned again.
 35		/// </summary>
 36		/// <param name="contentName">Name of the Scene content to load</param>
 37		/// <returns>The loaded SceneData object (or fallback if it failed)
 38		/// </returns>
 39		public static SceneData Get(string contentName)
 40		{
 41			return Get<SceneData>(contentName, ContentType.Scene);
 42		}
 43		#endregion
 44
 45		#region LoadingBackgroundImageName (Public)
 46		/// <summary>
 47		/// Name of the loading background image that will be shown when a scene
 48		/// is loading (optional).
 49		/// </summary>
 50		public string LoadingBackgroundImageName
 51		{
 52			get;
 53			set;
 54		}
 55		#endregion
 56
 57		#region Screens (Public)
 58		/// <summary>
 59		/// The list of screen data which represents the screens which are used in
 60		/// the scene if it get loaded.
 61		/// </summary>
 62		public List<BaseUIScreenData> Screens
 63		{
 64			get;
 65			set;
 66		}
 67		#endregion
 68
 69		#region Theme (Public)
 70		/// <summary>
 71		/// Theme to be used for this scene and all UI Screens.
 72		/// </summary>
 73		public BaseUIThemeData Theme
 74		{
 75			get;
 76			set;
 77		}
 78		#endregion
 79
 80		#region MusicName (Public)
 81		/// <summary>
 82		/// Music name
 83		/// </summary>
 84		public string MusicName
 85		{
 86			get;
 87			set;
 88		}
 89		#endregion
 90
 91		#region VideoName (Public)
 92		/// <summary>
 93		/// Video name
 94		/// </summary>
 95		public string VideoName
 96		{
 97			get;
 98			set;
 99		}
100		#endregion
101
102		#region CameraName (Public)
103		/// <summary>
104		/// Camera name
105		/// </summary>
106		public string CameraName
107		{
108			get;
109			set;
110		}
111		#endregion
112
113		#region Constructors
114		/// <summary>
115		/// Create scene data, used for the Scene Editor to define the data.
116		/// </summary>
117		public SceneData()
118			: this(EmptyName)
119		{
120		}
121
122		/// <summary>
123		/// Create scene content based on the content system. Loading happens in
124		/// the Load method, all content is loaded right away.
125		/// </summary>
126		/// <param name="setContentName">Name of the scene name to load.</param>
127		private SceneData(string setContentName)
128			: base(setContentName)
129		{
130			// Make sure Screens is not null, we should at least have an empty list
131			Screens = new List<BaseUIScreenData>();
132		}
133		#endregion
134
135		#region ISaveLoadBinary Members
136		/// <summary>
137		/// Loads and restores all previously saved values that belongs to this
138		/// class only from the given data reader.
139		/// </summary>
140		/// <param name="dataReader">The reader which contains the stream with the
141		/// saved data which needs to be loaded now.</param>
142		public void Load(BinaryReader dataReader)
143		{
144			// We currently only support our version, if more versions are added,
145			// we need to do different loading code depending on the version here.
146			int version = dataReader.ReadInt32();
147			switch (version)
148			{
149					// Version 1
150				case VersionNumber:
151					LoadingBackgroundImageName = dataReader.ReadString();
152
153					// Just load all saved screens
154					int numberOfScreens = dataReader.ReadInt32();
155					for (int index = 0; index < numberOfScreens; index++)
156					{
157						string screenName = dataReader.ReadString();
158						Screens.Add(BaseUIScreenData.Get(screenName));
159					} // for
160
161					bool isThemeAvailable = dataReader.ReadBoolean();
162					if (isThemeAvailable)
163					{
164						string themeContentName = dataReader.ReadString();
165						Theme = BaseUIThemeData.Get(themeContentName);
166					}
167
168					MusicName = dataReader.ReadString();
169					VideoName = dataReader.ReadString();
170					CameraName = dataReader.ReadString();
171					break;
172
173				default:
174					Log.InvalidVersionWarning(GetType().Name, version, VersionNumber);
175					return;
176			} // switch
177		}
178
179		/// <summary>
180		/// Save data
181		/// </summary>
182		/// <param name="dataWriter">Writer</param>
183		public void Save(BinaryWriter dataWriter)
184		{
185			// At first we write the current version number of the class data format
186			dataWriter.Write(VersionNumber);
187
188			LoadingBackgroundImageName.Save(dataWriter);
189
190			// Save all screen names
191			dataWriter.Write(Screens.Count);
192			foreach (BaseUIScreenData screen in Screens)
193			{
194				dataWriter.Write(screen.Name);
195			} // foreach
196
197			// Save the theme name as well if we have one
198			dataWriter.Write(Theme != null);
199			if (Theme != null)
200			{
201				dataWriter.Write(Theme.Name);
202			}
203
204			// And save the rest of data we might have
205			MusicName.Save(dataWriter);
206			VideoName.Save(dataWriter);
207			CameraName.Save(dataWriter);
208		}
209		#endregion
210
211		#region Methods (Private)
212
213		#region Load
214		/// <summary>
215		/// Native load method, will load all the scene data.
216		/// </summary>
217		/// <param name="alreadyLoadedNativeData">
218		/// The first instance that has already loaded the required content data
219		/// of this content class or just 'null' if there is none loaded yet (or
220		/// anymore).
221		/// </param>
222		protected override void Load(Content alreadyLoadedNativeData)
223		{
224			try
225			{
226				if (alreadyLoadedNativeData != null)
227				{
228					SceneData otherSceneData = alreadyLoadedNativeData as SceneData;
229					LoadingBackgroundImageName =
230						otherSceneData.LoadingBackgroundImageName;
231					Screens = otherSceneData.Screens;
232					Theme = otherSceneData.Theme;
233					MusicName = otherSceneData.MusicName;
234					VideoName = otherSceneData.VideoName;
235					CameraName = otherSceneData.CameraName;
236
237					// Also handle all base class functionality (scene management)
238					base.Load(alreadyLoadedNativeData);
239					// This object cannot be used for cloning yet (no NativeObjectClass),
240					// but if the caller (Scene) sets it itself, it all works out great :)
241				}
242
243				else if (String.IsNullOrEmpty(RelativeFilePath) == false)
244				{
245					FileHelper.Load(RelativeFilePath, this);
246					alreadyLoadedNativeData = this;
247				}
248			}
249			catch (Exception ex)
250			{
251				Log.Warning("Failed to load scene data from file '" +
252				            RelativeFilePath + "': " + ex);
253				FailedToLoad = true;
254			}
255
256			// Also handle all base class functionality (scene management)
257			base.Load(alreadyLoadedNativeData);
258		}
259		#endregion
260
261		#endregion
262	}
263}
264