PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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