PageRenderTime 155ms CodeModel.GetById 80ms app.highlight 13ms RepoModel.GetById 48ms app.codeStats 11ms

/Scenes/UserInterfaces/Designs/BorderedControlDesign.cs

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