/ContentSystem/UserInterfaces/SceneData.cs
C# | 264 lines | 155 code | 27 blank | 82 comment | 11 complexity | 9e432fb0828d63fe8b8141959e9de726 MD5 | raw file
Possible License(s): Apache-2.0
- using System;
- using System.Collections.Generic;
- using System.IO;
- using Delta.Utilities;
- using Delta.Utilities.Helpers;
-
- namespace Delta.ContentSystem.UserInterfaces
- {
- /// <summary>
- /// Scene data content class, which loads all the required data for a scene.
- /// Most importantly we always have only one scene active. All content that
- /// is loaded during the time that scene is open will automatically be
- /// disposed when closing the scene. This way we can open new scenes and
- /// make sure all content from the previous scenes has been unloaded. If
- /// you load new scenes while the old one is still open (stack add), you can
- /// load additional content, but nothing gets disposed automatically.
- /// <para />
- /// Note: This class is not publicly available in the namespace
- /// Delta.ContentSystem.UserInterfaces because it is sealed and only used
- /// in the also sealed and protected Scene class (in Delta.Scenes).
- /// </summary>
- public sealed class SceneData : EmptySceneData, ISaveLoadBinary
- {
- #region Constants
- /// <summary>
- /// The current version of the implementation of this class.
- /// </summary>
- private const int VersionNumber = 1;
- #endregion
-
- #region Get (Static)
- /// <summary>
- /// This is the only method to load SceneData content. If a content object
- /// has already been loaded, it will be returned again.
- /// </summary>
- /// <param name="contentName">Name of the Scene content to load</param>
- /// <returns>The loaded SceneData object (or fallback if it failed)
- /// </returns>
- public static SceneData Get(string contentName)
- {
- return Get<SceneData>(contentName, ContentType.Scene);
- }
- #endregion
-
- #region LoadingBackgroundImageName (Public)
- /// <summary>
- /// Name of the loading background image that will be shown when a scene
- /// is loading (optional).
- /// </summary>
- public string LoadingBackgroundImageName
- {
- get;
- set;
- }
- #endregion
-
- #region Screens (Public)
- /// <summary>
- /// The list of screen data which represents the screens which are used in
- /// the scene if it get loaded.
- /// </summary>
- public List<BaseUIScreenData> Screens
- {
- get;
- set;
- }
- #endregion
-
- #region Theme (Public)
- /// <summary>
- /// Theme to be used for this scene and all UI Screens.
- /// </summary>
- public BaseUIThemeData Theme
- {
- get;
- set;
- }
- #endregion
-
- #region MusicName (Public)
- /// <summary>
- /// Music name
- /// </summary>
- public string MusicName
- {
- get;
- set;
- }
- #endregion
-
- #region VideoName (Public)
- /// <summary>
- /// Video name
- /// </summary>
- public string VideoName
- {
- get;
- set;
- }
- #endregion
-
- #region CameraName (Public)
- /// <summary>
- /// Camera name
- /// </summary>
- public string CameraName
- {
- get;
- set;
- }
- #endregion
-
- #region Constructors
- /// <summary>
- /// Create scene data, used for the Scene Editor to define the data.
- /// </summary>
- public SceneData()
- : this(EmptyName)
- {
- }
-
- /// <summary>
- /// Create scene content based on the content system. Loading happens in
- /// the Load method, all content is loaded right away.
- /// </summary>
- /// <param name="setContentName">Name of the scene name to load.</param>
- private SceneData(string setContentName)
- : base(setContentName)
- {
- // Make sure Screens is not null, we should at least have an empty list
- Screens = new List<BaseUIScreenData>();
- }
- #endregion
-
- #region ISaveLoadBinary Members
- /// <summary>
- /// Loads and restores all previously saved values that belongs to this
- /// class only from the given data reader.
- /// </summary>
- /// <param name="dataReader">The reader which contains the stream with the
- /// saved data which needs to be loaded now.</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:
- LoadingBackgroundImageName = dataReader.ReadString();
-
- // Just load all saved screens
- int numberOfScreens = dataReader.ReadInt32();
- for (int index = 0; index < numberOfScreens; index++)
- {
- string screenName = dataReader.ReadString();
- Screens.Add(BaseUIScreenData.Get(screenName));
- } // for
-
- bool isThemeAvailable = dataReader.ReadBoolean();
- if (isThemeAvailable)
- {
- string themeContentName = dataReader.ReadString();
- Theme = BaseUIThemeData.Get(themeContentName);
- }
-
- MusicName = dataReader.ReadString();
- VideoName = dataReader.ReadString();
- CameraName = dataReader.ReadString();
- break;
-
- default:
- Log.InvalidVersionWarning(GetType().Name, version, VersionNumber);
- return;
- } // switch
- }
-
- /// <summary>
- /// Save data
- /// </summary>
- /// <param name="dataWriter">Writer</param>
- public void Save(BinaryWriter dataWriter)
- {
- // At first we write the current version number of the class data format
- dataWriter.Write(VersionNumber);
-
- LoadingBackgroundImageName.Save(dataWriter);
-
- // Save all screen names
- dataWriter.Write(Screens.Count);
- foreach (BaseUIScreenData screen in Screens)
- {
- dataWriter.Write(screen.Name);
- } // foreach
-
- // Save the theme name as well if we have one
- dataWriter.Write(Theme != null);
- if (Theme != null)
- {
- dataWriter.Write(Theme.Name);
- }
-
- // And save the rest of data we might have
- MusicName.Save(dataWriter);
- VideoName.Save(dataWriter);
- CameraName.Save(dataWriter);
- }
- #endregion
-
- #region Methods (Private)
-
- #region Load
- /// <summary>
- /// Native load method, will load all the scene data.
- /// </summary>
- /// <param name="alreadyLoadedNativeData">
- /// The first instance that has already loaded the required content data
- /// of this content class or just 'null' if there is none loaded yet (or
- /// anymore).
- /// </param>
- protected override void Load(Content alreadyLoadedNativeData)
- {
- try
- {
- if (alreadyLoadedNativeData != null)
- {
- SceneData otherSceneData = alreadyLoadedNativeData as SceneData;
- LoadingBackgroundImageName =
- otherSceneData.LoadingBackgroundImageName;
- Screens = otherSceneData.Screens;
- Theme = otherSceneData.Theme;
- MusicName = otherSceneData.MusicName;
- VideoName = otherSceneData.VideoName;
- CameraName = otherSceneData.CameraName;
-
- // Also handle all base class functionality (scene management)
- base.Load(alreadyLoadedNativeData);
- // This object cannot be used for cloning yet (no NativeObjectClass),
- // but if the caller (Scene) sets it itself, it all works out great :)
- }
-
- else if (String.IsNullOrEmpty(RelativeFilePath) == false)
- {
- FileHelper.Load(RelativeFilePath, this);
- alreadyLoadedNativeData = this;
- }
- }
- catch (Exception ex)
- {
- Log.Warning("Failed to load scene data from file '" +
- RelativeFilePath + "': " + ex);
- FailedToLoad = true;
- }
-
- // Also handle all base class functionality (scene management)
- base.Load(alreadyLoadedNativeData);
- }
- #endregion
-
- #endregion
- }
- }
-