/Scenes/UserInterfaces/Designs/BorderedControlDesign.cs
C# | 258 lines | 170 code | 38 blank | 50 comment | 1 complexity | f5776f6672b41e9f31f63235f0b2861f MD5 | raw file
Possible License(s): Apache-2.0
- using Delta.Rendering.Basics.Materials;
- using Delta.Utilities;
- using Delta.Utilities.Datatypes;
-
- namespace Delta.Scenes.UserInterfaces.Designs
- {
- /// <summary>
- /// That design is thought when the control can contain different sized
- /// content and the controls should scale to it. With a non-bordered design
- /// the control can be look totally miss-stretched but this design will
- /// prevent that (based on the set border width).
- /// </summary>
- public class BorderedControlDesign : ControlDesign, IBorderedDesign
- {
- #region DockingMode Enum
- /// <summary>
- /// Docking mode
- /// </summary>
- private enum DockingMode : byte
- {
- None,
-
- Left,
-
- TopLeft,
-
- Top,
-
- TopRight,
-
- Right,
-
- BottomRight,
-
- Bottom,
-
- BottomLeft,
-
- Center,
- }
- #endregion
-
- #region Constants
- /// <summary>
- /// The thicknes of the border for every "BorderedDesign" which is set by
- /// default.
- /// </summary>
- public const float DefaultBorderWidth = 0.025f;
- #endregion
-
- #region BorderWidth (Public)
- /// <summary>
- /// The width of the border which will be used for drawing the control.
- /// </summary>
- /// <returns>Float</returns>
- public float BorderWidth
- {
- get;
- set;
- }
- #endregion
-
- #region Constructors
- /// <summary>
- /// Create bordered design
- /// </summary>
- public BorderedControlDesign()
- {
- BorderWidth = DefaultBorderWidth;
- }
- #endregion
-
- #region DrawControl (Public)
- /// <summary>
- /// Draw control
- /// </summary>
- /// <param name="control">Control</param>
- /// <param name="drawArea">Draw area</param>
- public override void DrawControl(BaseControl control, Rectangle drawArea)
- {
- Material2DColored currentStyle = GetControlStyle(control);
- DrawStyleBordered(this, control, currentStyle, drawArea);
- }
- #endregion
-
- #region Methods (Private)
-
- #region GetDockingArea
- /// <summary>
- /// Get docking area
- /// </summary>
- /// <param name="totalArea">Total area</param>
- /// <param name="fillingStrength">Filling strength</param>
- /// <param name="wishedDocking">Wished docking</param>
- /// <param name="borderThickness">Border thickness</param>
- /// <returns></returns>
- private static Rectangle GetDockingArea(Rectangle totalArea,
- float fillingStrength, DockingMode wishedDocking,
- float borderThickness = 0.0f)
- {
- Rectangle dockedArea = totalArea;
- switch (wishedDocking)
- {
- case DockingMode.TopLeft:
- dockedArea.X = totalArea.X + borderThickness;
- dockedArea.Y = totalArea.Y + borderThickness;
- dockedArea.Width = fillingStrength;
- dockedArea.Height = fillingStrength;
- break;
-
- case DockingMode.Top:
- dockedArea.X = totalArea.X + borderThickness + fillingStrength;
- dockedArea.Y = totalArea.Y + borderThickness;
- dockedArea.Width = totalArea.Width -
- (2 * (borderThickness + fillingStrength));
- dockedArea.Height = fillingStrength;
- break;
-
- case DockingMode.TopRight:
- dockedArea.X = totalArea.Right - fillingStrength - borderThickness;
- dockedArea.Y = totalArea.Y + borderThickness;
- dockedArea.Width = fillingStrength;
- dockedArea.Height = fillingStrength;
- break;
-
- case DockingMode.Right:
- dockedArea.X = totalArea.Right - fillingStrength - borderThickness;
- dockedArea.Y = totalArea.Y + borderThickness + fillingStrength;
- dockedArea.Width = fillingStrength;
- dockedArea.Height = totalArea.Height -
- (2 * (borderThickness + fillingStrength));
- break;
-
- case DockingMode.BottomRight:
- dockedArea.X = totalArea.Right - fillingStrength - borderThickness;
- dockedArea.Y = totalArea.Bottom - fillingStrength - borderThickness;
- dockedArea.Width = fillingStrength;
- dockedArea.Height = fillingStrength;
- break;
-
- case DockingMode.Bottom:
- dockedArea.X = totalArea.X + borderThickness + fillingStrength;
- dockedArea.Y = totalArea.Bottom - fillingStrength - borderThickness;
- dockedArea.Width = totalArea.Width -
- (2 * (borderThickness + fillingStrength));
- dockedArea.Height = fillingStrength;
- break;
-
- case DockingMode.BottomLeft:
- dockedArea.X = totalArea.X + borderThickness;
- dockedArea.Y = totalArea.Bottom - fillingStrength - borderThickness;
- dockedArea.Width = fillingStrength;
- dockedArea.Height = fillingStrength;
- break;
-
- case DockingMode.Left:
- dockedArea.X = totalArea.X + borderThickness;
- dockedArea.Y = totalArea.Y + borderThickness + fillingStrength;
- dockedArea.Width = fillingStrength;
- dockedArea.Height = totalArea.Height -
- (2 * (borderThickness + fillingStrength));
- break;
-
- case DockingMode.Center:
- dockedArea.X = totalArea.X + borderThickness + fillingStrength;
- dockedArea.Y = totalArea.Y + borderThickness + fillingStrength;
- dockedArea.Width = totalArea.Width -
- (2 * (borderThickness + fillingStrength));
- dockedArea.Height = totalArea.Height -
- (2 * (borderThickness + fillingStrength));
- break;
-
- default:
- Log.Warning("Sorry, the DockingMode '" + wishedDocking +
- "' isn't supported yet.");
- break;
- } // switch
-
- return dockedArea;
- }
- #endregion
-
- // GetDockingArea(totalArea, fillingStrength, wishedDocking)
-
- #region DrawStyleBordered
- /// <summary>
- /// Draw style bordered
- /// </summary>
- /// <param name="controlDesign">Control design</param>
- /// <param name="control">Control</param>
- /// <param name="style">The style.</param>
- /// <param name="drawArea">Draw area</param>
- internal static void DrawStyleBordered(IBorderedDesign controlDesign,
- BaseControl control, Material2DColored style, Rectangle drawArea)
- {
- float borderStrenth = controlDesign.BorderWidth;
- Rectangle uvArea = style.UV;
- float rotation = control.Rotation;
-
- // Top-Left corner
- style.Draw(
- GetDockingArea(drawArea, borderStrenth, DockingMode.TopLeft),
- GetDockingArea(uvArea, borderStrenth, DockingMode.TopLeft),
- rotation);
-
- // Top border
- style.Draw(
- GetDockingArea(drawArea, borderStrenth, DockingMode.Top),
- GetDockingArea(uvArea, borderStrenth, DockingMode.Top),
- rotation);
-
- // Top-Right corner
- style.Draw(
- GetDockingArea(drawArea, borderStrenth, DockingMode.TopRight),
- GetDockingArea(uvArea, borderStrenth, DockingMode.TopRight),
- rotation);
-
- // Right border
- style.Draw(
- GetDockingArea(drawArea, borderStrenth, DockingMode.Right),
- GetDockingArea(uvArea, borderStrenth, DockingMode.Right),
- rotation);
-
- // Bottom-Right corner
- style.Draw(
- GetDockingArea(drawArea, borderStrenth, DockingMode.BottomRight),
- GetDockingArea(uvArea, borderStrenth, DockingMode.BottomRight),
- rotation);
-
- // Bottom border
- style.Draw(
- GetDockingArea(drawArea, borderStrenth, DockingMode.Bottom),
- GetDockingArea(uvArea, borderStrenth, DockingMode.Bottom),
- rotation);
-
- // Bottom-Left corner
- style.Draw(
- GetDockingArea(drawArea, borderStrenth, DockingMode.BottomLeft),
- GetDockingArea(uvArea, borderStrenth, DockingMode.BottomLeft),
- rotation);
-
- // Left border
- style.Draw(
- GetDockingArea(drawArea, borderStrenth, DockingMode.Left),
- GetDockingArea(uvArea, borderStrenth, DockingMode.Left),
- rotation);
-
- // Centered inner part
- style.Draw(
- GetDockingArea(drawArea, 0.0f, DockingMode.Center, borderStrenth),
- GetDockingArea(uvArea, 0.0f, DockingMode.Center, borderStrenth),
- rotation);
- }
- #endregion
-
- #endregion
- }
- }