/ContentSystem/UserInterfaces/BaseUIScreenData.cs
# · C# · 163 lines · 92 code · 15 blank · 56 comment · 4 complexity · 12b5ae38f5970073bd4ace5c53ae57ee MD5 · raw file
- using System;
- using System.IO;
- using Delta.Engine;
- using Delta.Utilities;
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Helpers;
-
- namespace Delta.ContentSystem.UserInterfaces
- {
- /// <summary>
- /// The basic content class which is the base of all UI screen classes.
- /// Usually contains some screen global data, a link to the used UI Theme and
- /// a lot of controls in the form of BaseControlData.
- /// </summary>
- public abstract class BaseUIScreenData : Content, ISaveLoadBinary
- {
- #region Constants
- /// <summary>
- /// The newest version number of the screen data.
- /// </summary>
- private const int VersionNumber = 1;
- #endregion
-
- #region Get (Static)
- /// <summary>
- /// Get and load content based on the content name. This method makes sure
- /// we do not load the same content twice (the constructor is protected).
- /// </summary>
- /// <param name="contentName">Content name we want to load, this is
- /// passed onto the Content System, which will do the actual loading with
- /// help of the Load method in this class.</param>
- /// <returns>The loaded Content object, always unique for the same
- /// name, this helps comparing data.</returns>
- public static BaseUIScreenData Get(string contentName)
- {
- return Get<BaseUIScreenData>(contentName, ContentType.UIScreen);
- }
- #endregion
-
- #region DrawArea (Public)
- /// <summary>
- /// By default we render to Rectangle.One, which will be converted
- /// to Graphic.Instance.DrawArea, else use this size as a draw area!
- /// Note: Only used for UIScene.BackgroundDesign.Draw
- /// </summary>
- public Rectangle DrawArea
- {
- get
- {
- return (drawArea.HasValue)
- ? drawArea.Value
- : ScreenSpace.DrawArea;
- } // get
- protected set
- {
- drawArea = (value != Rectangle.Zero)
- ? drawArea = value
- : drawArea = null;
- } // set
- }
- #endregion
-
- #region Private
-
- #region drawArea (Private)
- /// <summary>
- /// Draw area
- /// </summary>
- private Rectangle? drawArea;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <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>
- protected BaseUIScreenData(string setContentName)
- : base(setContentName, ContentType.UIScreen)
- {
- }
- #endregion
-
- #region ISaveLoadBinary Members
- /// <summary>
- /// Loads all data of the object again which were previously saved.
- /// </summary>
- /// <param name="dataReader">
- /// The container object which contains the data which were saved before.
- /// </param>
- public virtual void Load(BinaryReader dataReader)
- {
- int version = dataReader.ReadInt32();
-
- switch (version)
- {
- // Version 1
- case VersionNumber:
- DrawArea = new Rectangle(dataReader);
- break;
-
- default:
- Log.InvalidVersionWarning(GetType().Name, version, VersionNumber);
- return;
- } // switch
- }
-
- /// <summary>
- /// Saves all necessary data of the object into a binary stream.
- /// </summary>
- /// <param name="dataWriter">
- /// The container object which will store all the saved data.
- /// </param>
- public virtual void Save(BinaryWriter dataWriter)
- {
- dataWriter.Write(VersionNumber);
-
- // Determine if we have to save a custom screen area
- Rectangle areaToSave = (drawArea.HasValue)
- ? drawArea.Value
- : Rectangle.Zero;
- areaToSave.Save(dataWriter);
- }
- #endregion
-
- #region Methods (Private)
-
- #region Load
- /// <summary>
- /// Native load method, will just load the 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
- {
- // Note: Cloning is not needed for UI Screens (each is unique and we
- // don't share content or names, we can always load the same one again)
- if (String.IsNullOrEmpty(RelativeFilePath) == false)
- {
- // Load via the ISaveLoadBinary interface methods below.
- // Cloning should not really happen for screens anyway.
- FileHelper.Load(RelativeFilePath, this);
- }
- }
- catch (Exception ex)
- {
- Log.Warning("Failed to load mesh animation data from file '" +
- RelativeFilePath + "': " + ex);
- FailedToLoad = true;
- }
- }
- #endregion
-
- #endregion
- }
- }