/Scenes/UserInterfaces/Designs/TextControlDesign.cs
C# | 411 lines | 272 code | 44 blank | 95 comment | 26 complexity | 19aa59fcc19a9a3cccbc682f903cbc59 MD5 | raw file
Possible License(s): Apache-2.0
- using System.IO;
- using Delta.Engine.Dynamic;
- using Delta.Rendering.Basics.Fonts;
- using Delta.Rendering.Basics.Materials;
- using Delta.Scenes.Enums;
- using Delta.Scenes.UserInterfaces.Controls;
- using Delta.Utilities;
- using Delta.Utilities.Datatypes;
- using NUnit.Framework;
-
- namespace Delta.Scenes.UserInterfaces.Designs
- {
- /// <summary>
- /// Label design.
- /// </summary>
- public class TextControlDesign : ControlDesign, ITextDesign
- {
- #region Constants
- /// <summary>
- /// The current version of the implementation of this class.
- /// </summary>
- private const int VersionNumber = 1;
- #endregion
-
- #region TextFont (Public)
- /// <summary>
- /// Gets or sets the font which is used to displaying the text.
- /// </summary>
- public Font TextFont
- {
- get
- {
- if (textFont == null)
- {
- Log.Warning(
- "The text font is not valid for '" + this + "' will now use " +
- "the default font of the engine instead.");
- textFont = Font.Default;
- }
-
- return textFont;
- }
- set
- {
- textFont = value;
- }
- }
- #endregion
-
- #region DisabledTextFont (Public)
- /// <summary>
- /// Gets or sets the font which is used to displaying the text if the
- /// control is in the "Disabled" state.
- /// </summary>
- public Font DisabledTextFont
- {
- get
- {
- if (disabledTextFont == null)
- {
- Log.Warning(
- "The disabled text font is not valid for '" + this + "' will " +
- "now use the default font of the engine instead.");
- disabledTextFont = Font.Default;
- }
-
- return disabledTextFont;
- }
- set
- {
- disabledTextFont = value;
- }
- }
- #endregion
-
- #region TextColor (Public)
- /// <summary>
- /// Get the color of the displayed text. To change the text color a new
- /// font must be initialized (like with all font properties).
- /// </summary>
- public Color TextColor
- {
- get
- {
- return TextFont.Color;
- } // get
- }
- #endregion
-
- #region TextBackground (Public)
- /// <summary>
- /// The own background design for the text area instead of the "global"
- /// background design (enabled state).
- /// </summary>
- public Material2DColored TextBackground
- {
- get;
- set;
- }
- #endregion
-
- #region TextHoverBackground (Public)
- /// <summary>
- /// The own background design for the text area instead of the "global"
- /// background design (hovered state).
- /// </summary>
- public Material2DColored TextHoverBackground
- {
- get;
- set;
- }
- #endregion
-
- #region DisabledTextBackground (Public)
- /// <summary>
- /// The own background design for the text area instead of the "global"
- /// background design (disabled state).
- /// </summary>
- public Material2DColored DisabledTextBackground
- {
- get;
- set;
- }
- #endregion
-
- #region Pressed (Public)
- /// <summary>
- /// Gets or sets the design to show when the button is pressed. Note: This
- /// design is optional, which means if it isn't set, the "basic" rendering
- /// (with BackgroundDesign and HoverDesign) behavior will be used instead.
- /// </summary>
- public Material2DColored Pressed
- {
- get;
- set;
- }
- #endregion
-
- #region Private
-
- #region textFont (Private)
- /// <summary>
- /// Text font used, usually set because this is a text control design
- /// after all. If nothing is set will be set to the fallback Font.Default.
- /// </summary>
- private Font textFont;
- #endregion
-
- #region disabledTextFont (Private)
- /// <summary>
- /// Disabled text font used, can be null when it is never accessed.
- /// </summary>
- private Font disabledTextFont;
- #endregion
-
- #endregion
-
- #region DrawControl (Public)
- /// <summary>
- /// Draw control. Note: This method will only be called if the control
- /// state is at least visible.
- /// </summary>
- /// <param name="control">Control to draw in this design.</param>
- /// <param name="drawArea">The draw area.</param>
- public override void DrawControl(BaseControl control, Rectangle drawArea)
- {
- // First draw the general design of the control
- base.DrawControl(control, drawArea);
-
- Label textControl = control as Label;
- if (textControl == null)
- {
- Log.Warning(
- "The design '" + this + "' will be applied on the non-text " +
- "control '" + control + "' which is not really useful because " +
- "there is no text to draw, so please use a non-text design " +
- "instead for this control.");
- return;
- } // if
-
- // and next draw the text area related style
- switch (textControl.State)
- {
- case ElementState.Disabled:
- if (DisabledTextBackground != null)
- {
- DrawStyle(textControl, DisabledTextBackground,
- textControl.TextDrawArea);
- } // if
- break;
-
- case ElementState.Hovered:
- case ElementState.Active:
- Material2DColored textHoverStyle =
- TextHoverBackground != null
- ? TextHoverBackground
- : TextBackground;
- if (textHoverStyle != null)
- {
- DrawStyle(textControl, textHoverStyle, textControl.TextDrawArea);
- } // if
- break;
-
- default:
- Material2DColored textBackground = TextBackground;
- if (textBackground != null)
- {
- DrawStyle(textControl, textBackground, textControl.TextDrawArea);
- } // if
- break;
- } // switch
- }
- #endregion
-
- #region Save (Public)
- /// <summary>
- /// Saves all data which are necessary to restore the object again.
- /// </summary>
- /// <param name="dataWriter">The writer which contains the stream where the data should be saved
- /// into now.</param>
- public override void Save(BinaryWriter dataWriter)
- {
- // At first we write the data of the base class
- base.Save(dataWriter);
-
- // and then save the version of the current data format
- dataWriter.Write(VersionNumber);
-
- // before we can finally save the properties
- bool isTextFontAvailable = textFont != null;
- dataWriter.Write(isTextFontAvailable);
- if (isTextFontAvailable)
- {
- Factory.Save(dataWriter, textFont);
- }
-
- bool isDisabledTextFontAvailable = disabledTextFont != null;
- dataWriter.Write(isDisabledTextFontAvailable);
- if (isDisabledTextFontAvailable)
- {
- Factory.Save(dataWriter, disabledTextFont);
- }
-
- // Pressed style
- bool hasPressedDesign = Pressed != null;
- dataWriter.Write(hasPressedDesign);
- if (hasPressedDesign)
- {
- SaveMaterial(dataWriter, Pressed);
- }
- }
- #endregion
-
- #region Load (Public)
- /// <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 override void Load(BinaryReader dataReader)
- {
- // At first we need to load all data of the base class
- base.Load(dataReader);
-
- // and then check which version of the data need to load now
- int version = dataReader.ReadInt32();
- switch (version)
- {
- // Version 1
- case VersionNumber:
- // Is a text font available ?
- if (dataReader.ReadBoolean())
- {
- // then load the saved font
- textFont = Factory.Load<Font>(dataReader);
- }
-
- if (dataReader.ReadBoolean())
- {
- // then load the saved font
- disabledTextFont = Factory.Load<Font>(dataReader);
- }
-
- // Pressed style
- bool hasPressedDesign = dataReader.ReadBoolean();
- if (hasPressedDesign)
- {
- Pressed = LoadMaterial(dataReader);
- }
- else
- {
- Pressed = null;
- }
- break;
-
- default:
- Log.InvalidVersionWarning(GetType().Name, version, VersionNumber);
- break;
- }
- }
- #endregion
-
- #region Methods (Private)
-
- #region GetControlStyle
- /// <summary>
- /// Returns the set style of the controls based on its current state.
- /// </summary>
- /// <param name="control">Control</param>
- protected override Material2DColored GetControlStyle(BaseControl control)
- {
- switch (control.State)
- {
- case ElementState.Pressed:
- return (Pressed != null)
- ? Pressed
- : base.GetControlStyle(control);
-
- default:
- return base.GetControlStyle(control);
- }
- }
- #endregion
-
- #endregion
-
- /// <summary>
- /// Tests for TextControlDesign
- /// </summary>
- [Category("LongRunning")]
- internal class TextControlDesignTests
- {
- #region SaveAndLoad
- /// <summary>
- /// Save and load
- /// </summary>
- [Test]
- public void SaveAndLoad()
- {
- // Create a design
- TextControlDesign textDesign = new TextControlDesign
- {
- Background = BaseTheme.GetUIMaterial("ButtonBackground"),
- DisabledBackground = BaseTheme.GetUIMaterial("ButtonBackground",
- Color.Grey),
- Hover = BaseTheme.GetUIMaterial("ButtonHover", Color.Yellow),
- Pressed = BaseTheme.GetUIMaterial("ButtonPressed", Color.Yellow),
- TextFont = new Font(Font.Default, Color.Blue),
- DisabledTextFont = new Font(Font.Default, Color.DarkBlue),
- };
-
- // Next create a second instance
- TextControlDesign loadedDesign = new TextControlDesign();
-
- //// and save it out
- //byte[] savedData = textDesign.Save();
-
- //// and make a sanity check that it hasn't already the same data
-
- //// Now load the saved data of the first instance
- //loadedDesign.Load(savedData);
-
- // and check that all values are the same
- // Background design
- Assert.Equal(loadedDesign.Background.Name, textDesign.Background.Name);
- Assert.Equal(loadedDesign.Background.DrawLayer,
- textDesign.Background.DrawLayer);
- Assert.Equal(loadedDesign.Background.BlendColor,
- textDesign.Background.BlendColor);
-
- // Disabled background design
- Assert.Equal(loadedDesign.DisabledBackground.Name,
- textDesign.DisabledBackground.Name);
- Assert.Equal(loadedDesign.DisabledBackground.DrawLayer,
- textDesign.DisabledBackground.DrawLayer);
- Assert.Equal(loadedDesign.DisabledBackground.BlendColor,
- textDesign.DisabledBackground.BlendColor);
-
- // Hover design
- Assert.Equal(loadedDesign.Hover.Name, textDesign.Hover.Name);
- Assert.Equal(loadedDesign.Hover.DrawLayer,
- textDesign.Hover.DrawLayer);
- Assert.Equal(loadedDesign.Hover.BlendColor,
- textDesign.Hover.BlendColor);
-
- // Pressed design
- Assert.Equal(loadedDesign.Pressed.Name, textDesign.Pressed.Name);
- Assert.Equal(loadedDesign.Pressed.DrawLayer,
- textDesign.Pressed.DrawLayer);
- Assert.Equal(loadedDesign.Pressed.BlendColor,
- textDesign.Hover.BlendColor);
-
- // Text font
- Assert.Equal(loadedDesign.TextFont.FamilyName,
- textDesign.TextFont.FamilyName);
- Assert.Equal(loadedDesign.DisabledTextFont.FamilyName,
- textDesign.DisabledTextFont.FamilyName);
- Assert.Equal(loadedDesign.TextFont.LineHeight,
- textDesign.TextFont.LineHeight);
-
- // Text colors
- Assert.Equal(loadedDesign.TextFont.Color,
- textDesign.TextFont.Color);
- Assert.Equal(loadedDesign.DisabledTextFont.Color,
- textDesign.DisabledTextFont.Color);
- }
- #endregion
- }
- }
- }