PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/ContentSystem/UserInterfaces/BaseUIScreenData.cs

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