PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1. using Delta.Rendering.Basics.Materials;
  2. using Delta.Utilities;
  3. using Delta.Utilities.Datatypes;
  4. namespace Delta.Scenes.UserInterfaces.Designs
  5. {
  6. /// <summary>
  7. /// That design is thought when the control can contain different sized
  8. /// content and the controls should scale to it. With a non-bordered design
  9. /// the control can be look totally miss-stretched but this design will
  10. /// prevent that (based on the set border width).
  11. /// </summary>
  12. public class BorderedControlDesign : ControlDesign, IBorderedDesign
  13. {
  14. #region DockingMode Enum
  15. /// <summary>
  16. /// Docking mode
  17. /// </summary>
  18. private enum DockingMode : byte
  19. {
  20. None,
  21. Left,
  22. TopLeft,
  23. Top,
  24. TopRight,
  25. Right,
  26. BottomRight,
  27. Bottom,
  28. BottomLeft,
  29. Center,
  30. }
  31. #endregion
  32. #region Constants
  33. /// <summary>
  34. /// The thicknes of the border for every "BorderedDesign" which is set by
  35. /// default.
  36. /// </summary>
  37. public const float DefaultBorderWidth = 0.025f;
  38. #endregion
  39. #region BorderWidth (Public)
  40. /// <summary>
  41. /// The width of the border which will be used for drawing the control.
  42. /// </summary>
  43. /// <returns>Float</returns>
  44. public float BorderWidth
  45. {
  46. get;
  47. set;
  48. }
  49. #endregion
  50. #region Constructors
  51. /// <summary>
  52. /// Create bordered design
  53. /// </summary>
  54. public BorderedControlDesign()
  55. {
  56. BorderWidth = DefaultBorderWidth;
  57. }
  58. #endregion
  59. #region DrawControl (Public)
  60. /// <summary>
  61. /// Draw control
  62. /// </summary>
  63. /// <param name="control">Control</param>
  64. /// <param name="drawArea">Draw area</param>
  65. public override void DrawControl(BaseControl control, Rectangle drawArea)
  66. {
  67. Material2DColored currentStyle = GetControlStyle(control);
  68. DrawStyleBordered(this, control, currentStyle, drawArea);
  69. }
  70. #endregion
  71. #region Methods (Private)
  72. #region GetDockingArea
  73. /// <summary>
  74. /// Get docking area
  75. /// </summary>
  76. /// <param name="totalArea">Total area</param>
  77. /// <param name="fillingStrength">Filling strength</param>
  78. /// <param name="wishedDocking">Wished docking</param>
  79. /// <param name="borderThickness">Border thickness</param>
  80. /// <returns></returns>
  81. private static Rectangle GetDockingArea(Rectangle totalArea,
  82. float fillingStrength, DockingMode wishedDocking,
  83. float borderThickness = 0.0f)
  84. {
  85. Rectangle dockedArea = totalArea;
  86. switch (wishedDocking)
  87. {
  88. case DockingMode.TopLeft:
  89. dockedArea.X = totalArea.X + borderThickness;
  90. dockedArea.Y = totalArea.Y + borderThickness;
  91. dockedArea.Width = fillingStrength;
  92. dockedArea.Height = fillingStrength;
  93. break;
  94. case DockingMode.Top:
  95. dockedArea.X = totalArea.X + borderThickness + fillingStrength;
  96. dockedArea.Y = totalArea.Y + borderThickness;
  97. dockedArea.Width = totalArea.Width -
  98. (2 * (borderThickness + fillingStrength));
  99. dockedArea.Height = fillingStrength;
  100. break;
  101. case DockingMode.TopRight:
  102. dockedArea.X = totalArea.Right - fillingStrength - borderThickness;
  103. dockedArea.Y = totalArea.Y + borderThickness;
  104. dockedArea.Width = fillingStrength;
  105. dockedArea.Height = fillingStrength;
  106. break;
  107. case DockingMode.Right:
  108. dockedArea.X = totalArea.Right - fillingStrength - borderThickness;
  109. dockedArea.Y = totalArea.Y + borderThickness + fillingStrength;
  110. dockedArea.Width = fillingStrength;
  111. dockedArea.Height = totalArea.Height -
  112. (2 * (borderThickness + fillingStrength));
  113. break;
  114. case DockingMode.BottomRight:
  115. dockedArea.X = totalArea.Right - fillingStrength - borderThickness;
  116. dockedArea.Y = totalArea.Bottom - fillingStrength - borderThickness;
  117. dockedArea.Width = fillingStrength;
  118. dockedArea.Height = fillingStrength;
  119. break;
  120. case DockingMode.Bottom:
  121. dockedArea.X = totalArea.X + borderThickness + fillingStrength;
  122. dockedArea.Y = totalArea.Bottom - fillingStrength - borderThickness;
  123. dockedArea.Width = totalArea.Width -
  124. (2 * (borderThickness + fillingStrength));
  125. dockedArea.Height = fillingStrength;
  126. break;
  127. case DockingMode.BottomLeft:
  128. dockedArea.X = totalArea.X + borderThickness;
  129. dockedArea.Y = totalArea.Bottom - fillingStrength - borderThickness;
  130. dockedArea.Width = fillingStrength;
  131. dockedArea.Height = fillingStrength;
  132. break;
  133. case DockingMode.Left:
  134. dockedArea.X = totalArea.X + borderThickness;
  135. dockedArea.Y = totalArea.Y + borderThickness + fillingStrength;
  136. dockedArea.Width = fillingStrength;
  137. dockedArea.Height = totalArea.Height -
  138. (2 * (borderThickness + fillingStrength));
  139. break;
  140. case DockingMode.Center:
  141. dockedArea.X = totalArea.X + borderThickness + fillingStrength;
  142. dockedArea.Y = totalArea.Y + borderThickness + fillingStrength;
  143. dockedArea.Width = totalArea.Width -
  144. (2 * (borderThickness + fillingStrength));
  145. dockedArea.Height = totalArea.Height -
  146. (2 * (borderThickness + fillingStrength));
  147. break;
  148. default:
  149. Log.Warning("Sorry, the DockingMode '" + wishedDocking +
  150. "' isn't supported yet.");
  151. break;
  152. } // switch
  153. return dockedArea;
  154. }
  155. #endregion
  156. // GetDockingArea(totalArea, fillingStrength, wishedDocking)
  157. #region DrawStyleBordered
  158. /// <summary>
  159. /// Draw style bordered
  160. /// </summary>
  161. /// <param name="controlDesign">Control design</param>
  162. /// <param name="control">Control</param>
  163. /// <param name="style">The style.</param>
  164. /// <param name="drawArea">Draw area</param>
  165. internal static void DrawStyleBordered(IBorderedDesign controlDesign,
  166. BaseControl control, Material2DColored style, Rectangle drawArea)
  167. {
  168. float borderStrenth = controlDesign.BorderWidth;
  169. Rectangle uvArea = style.UV;
  170. float rotation = control.Rotation;
  171. // Top-Left corner
  172. style.Draw(
  173. GetDockingArea(drawArea, borderStrenth, DockingMode.TopLeft),
  174. GetDockingArea(uvArea, borderStrenth, DockingMode.TopLeft),
  175. rotation);
  176. // Top border
  177. style.Draw(
  178. GetDockingArea(drawArea, borderStrenth, DockingMode.Top),
  179. GetDockingArea(uvArea, borderStrenth, DockingMode.Top),
  180. rotation);
  181. // Top-Right corner
  182. style.Draw(
  183. GetDockingArea(drawArea, borderStrenth, DockingMode.TopRight),
  184. GetDockingArea(uvArea, borderStrenth, DockingMode.TopRight),
  185. rotation);
  186. // Right border
  187. style.Draw(
  188. GetDockingArea(drawArea, borderStrenth, DockingMode.Right),
  189. GetDockingArea(uvArea, borderStrenth, DockingMode.Right),
  190. rotation);
  191. // Bottom-Right corner
  192. style.Draw(
  193. GetDockingArea(drawArea, borderStrenth, DockingMode.BottomRight),
  194. GetDockingArea(uvArea, borderStrenth, DockingMode.BottomRight),
  195. rotation);
  196. // Bottom border
  197. style.Draw(
  198. GetDockingArea(drawArea, borderStrenth, DockingMode.Bottom),
  199. GetDockingArea(uvArea, borderStrenth, DockingMode.Bottom),
  200. rotation);
  201. // Bottom-Left corner
  202. style.Draw(
  203. GetDockingArea(drawArea, borderStrenth, DockingMode.BottomLeft),
  204. GetDockingArea(uvArea, borderStrenth, DockingMode.BottomLeft),
  205. rotation);
  206. // Left border
  207. style.Draw(
  208. GetDockingArea(drawArea, borderStrenth, DockingMode.Left),
  209. GetDockingArea(uvArea, borderStrenth, DockingMode.Left),
  210. rotation);
  211. // Centered inner part
  212. style.Draw(
  213. GetDockingArea(drawArea, 0.0f, DockingMode.Center, borderStrenth),
  214. GetDockingArea(uvArea, 0.0f, DockingMode.Center, borderStrenth),
  215. rotation);
  216. }
  217. #endregion
  218. #endregion
  219. }
  220. }