PageRenderTime 104ms CodeModel.GetById 60ms app.highlight 13ms RepoModel.GetById 27ms app.codeStats 1ms

/Scenes/UserInterfaces/Designs/CheckboxDesign.cs

#
C# | 370 lines | 263 code | 32 blank | 75 comment | 37 complexity | ff75d05d92fab8b002ccc7c9a4d1d18d MD5 | raw file
  1using System;
  2using System.IO;
  3using Delta.Rendering.Basics.Materials;
  4using Delta.Scenes.Enums;
  5using Delta.Scenes.UserInterfaces.Controls;
  6using Delta.Utilities;
  7using Delta.Utilities.Datatypes;
  8
  9namespace Delta.Scenes.UserInterfaces.Designs
 10{
 11	/// <summary>
 12	/// The design for a Checkbox or RadioButton control.
 13	/// </summary>
 14	public class CheckboxDesign : TextControlDesign
 15	{
 16		#region Constants
 17		/// <summary>
 18		/// The current version of the implementation of this class.
 19		/// </summary>
 20		private const int VersionNumber = 1;
 21		#endregion
 22
 23		#region CheckedSymbol (Public)
 24		/// <summary>
 25		/// Represents the design if the "Checked" property is enabled
 26		/// (control enabled state).
 27		/// </summary>
 28		public Material2DColored CheckedSymbol
 29		{
 30			get;
 31			set;
 32		}
 33		#endregion
 34
 35		#region CheckedSymbolHover (Public)
 36		/// <summary>
 37		/// Represents the design if the "Checked" property is enabled and the
 38		/// control is hovered.
 39		/// </summary>
 40		public Material2DColored CheckedSymbolHover
 41		{
 42			get;
 43			set;
 44		}
 45		#endregion
 46
 47		#region DisabledCheckedSymbol (Public)
 48		/// <summary>
 49		/// Represents the design if the "Checked" property is enabled
 50		/// (control disabled state).
 51		/// </summary>
 52		public Material2DColored DisabledCheckedSymbol
 53		{
 54			get;
 55			set;
 56		}
 57		#endregion
 58
 59		#region UncheckedSymbol (Public)
 60		/// <summary>
 61		/// Represents the design if the "Checked" property is not enabled
 62		/// (control enabled state).
 63		/// </summary>
 64		public Material2DColored UncheckedSymbol
 65		{
 66			get;
 67			set;
 68		}
 69		#endregion
 70
 71		#region UncheckedSymbolHover (Public)
 72		/// <summary>
 73		/// Represents the design if the "Checked" property is not enabled and the
 74		/// control is hovered.
 75		/// </summary>
 76		public Material2DColored UncheckedSymbolHover
 77		{
 78			get;
 79			set;
 80		}
 81		#endregion
 82
 83		#region DisabledUncheckedSymbol (Public)
 84		/// <summary>
 85		/// Represents the design if the "Checked" property is not enabled
 86		/// (control disabled state).
 87		/// </summary>
 88		public Material2DColored DisabledUncheckedSymbol
 89		{
 90			get;
 91			set;
 92		}
 93		#endregion
 94
 95		#region DrawControl (Public)
 96		/// <summary>
 97		/// Draw control
 98		/// </summary>
 99		/// <param name="control">Control</param>
100		/// <param name="drawArea">Draw area</param>
101		public override void DrawControl(BaseControl control, Rectangle drawArea)
102		{
103			if (control is Checkbox)
104			{
105				Checkbox checkbox = (Checkbox)control;
106				switch (checkbox.State)
107				{
108					case ElementState.Disabled:
109						// At first we have to draw the text background (if is set)
110						if (DisabledTextBackground != null)
111						{
112							DrawStyle(checkbox, DisabledTextBackground,
113								checkbox.TextDrawArea);
114						} // if
115
116						Material2DColored disabledCheckboxSymbol = (checkbox.IsChecked)
117						                                           	? DisabledCheckedSymbol
118						                                           	: DisabledUncheckedSymbol;
119						DrawStyle(checkbox, disabledCheckboxSymbol,
120							checkbox.CheckedSymbolArea.DrawArea);
121						break;
122
123					case ElementState.Enabled:
124					case ElementState.Hovered:
125					case ElementState.Pressed:
126						// At first we have to draw the text background (if is set)
127						if (TextBackground != null)
128						{
129							DrawStyle(checkbox, TextBackground, checkbox.TextDrawArea);
130						}
131
132						// Special drawing code for the hovered state
133						if (checkbox.State == ElementState.Hovered)
134						{
135							// Figure out if we have to show the checked or the unchecked
136							// symbol
137							// Note: We only can draw a hover symbol if there is one,
138							// otherwise we just draw the normal one
139							Material2DColored hoveredCheckboxSymbol;
140							if (checkbox.IsChecked)
141							{
142								hoveredCheckboxSymbol = (CheckedSymbolHover != null)
143								                        	? CheckedSymbolHover
144								                        	: CheckedSymbol;
145							} // if
146							else
147							{
148								hoveredCheckboxSymbol = (UncheckedSymbolHover != null)
149								                        	? UncheckedSymbolHover
150								                        	: UncheckedSymbol;
151							} // else
152
153							DrawStyle(checkbox, hoveredCheckboxSymbol,
154								checkbox.CheckedSymbolArea.DrawArea);
155						} // if
156						else // Enabled or Pressed state
157						{
158							Material2DColored checkboxSymbol = (checkbox.IsChecked)
159							                                   	? CheckedSymbol
160							                                   	: UncheckedSymbol;
161							DrawStyle(checkbox, checkboxSymbol,
162								checkbox.CheckedSymbolArea.DrawArea);
163						} // else
164						break;
165				} // switch
166			} // if
167			else
168			{
169				throw new InvalidOperationException("The given control '" + control +
170				                                    "' is no 'Checkbox'.");
171			} // else
172		}
173		#endregion
174
175		#region Save (Public)
176		/// <summary>
177		/// Saves all data which are necessary to restore the object again.
178		/// </summary>
179		/// <param name="dataWriter">The writer which contains the stream where the data should be saved
180		/// into now.</param>
181		public override void Save(BinaryWriter dataWriter)
182		{
183			// At first we write the data of the base class
184			base.Save(dataWriter);
185
186			// and then save the version of the current data format
187			dataWriter.Write(VersionNumber);
188
189			// before we can finally save the properties
190			// UncheckedSymbol style
191			bool hasUncheckedSymbol = UncheckedSymbol != null;
192			dataWriter.Write(hasUncheckedSymbol);
193			if (hasUncheckedSymbol)
194			{
195				SaveMaterial(dataWriter, UncheckedSymbol);
196			} // if
197
198			// UncheckedSymbolHover style
199			bool hasUncheckedSymbolHover = UncheckedSymbolHover != null;
200			dataWriter.Write(hasUncheckedSymbolHover);
201			if (hasUncheckedSymbolHover)
202			{
203				SaveMaterial(dataWriter, UncheckedSymbolHover);
204			} // if
205
206			// DisabledUncheckedSymbol style
207			bool hasDisabledUncheckedSymbol = DisabledUncheckedSymbol != null;
208			dataWriter.Write(hasDisabledUncheckedSymbol);
209			if (hasDisabledUncheckedSymbol)
210			{
211				SaveMaterial(dataWriter, DisabledUncheckedSymbol);
212			} // if
213
214			// Checked style
215			bool hasCheckedDesign = CheckedSymbol != null;
216			dataWriter.Write(hasCheckedDesign);
217			if (hasCheckedDesign)
218			{
219				SaveMaterial(dataWriter, CheckedSymbol);
220			} // if
221
222			// CheckedHover style
223			bool hasCheckedHoverDesign = CheckedSymbolHover != null;
224			dataWriter.Write(hasCheckedHoverDesign);
225			if (hasCheckedHoverDesign)
226			{
227				SaveMaterial(dataWriter, CheckedSymbolHover);
228			} // if
229
230			// DisabledChecked style
231			bool hasDisabledCheckedDesign = DisabledCheckedSymbol != null;
232			dataWriter.Write(hasDisabledCheckedDesign);
233			if (hasDisabledCheckedDesign)
234			{
235				SaveMaterial(dataWriter, DisabledCheckedSymbol);
236			} // if
237
238			// TextBackground style
239			bool hasTextBackgroundDesign = TextBackground != null;
240			dataWriter.Write(hasTextBackgroundDesign);
241			if (hasTextBackgroundDesign)
242			{
243				SaveMaterial(dataWriter, TextBackground);
244			} // if
245
246			// DisabledTextBackground style
247			bool hasDisabledTextBackgroundDesign = DisabledTextBackground != null;
248			dataWriter.Write(hasDisabledTextBackgroundDesign);
249			if (hasDisabledTextBackgroundDesign)
250			{
251				SaveMaterial(dataWriter, DisabledTextBackground);
252			} // if
253		}
254		#endregion
255
256		#region Load (Public)
257		/// <summary>
258		/// Loads and restores all previously saved values that belongs to this
259		/// class only from the given data reader.
260		/// </summary>
261		/// <param name="dataReader">The reader which contains the stream with the saved data which needs to
262		/// be loaded now.</param>
263		public override void Load(BinaryReader dataReader)
264		{
265			// At first we need to load all data of the base class
266			base.Load(dataReader);
267
268			// and then check which version of the data need to load now
269			int version = dataReader.ReadInt32();
270			switch (version)
271			{
272					// Version 1
273				case VersionNumber:
274					// UncheckedSymbol style
275					bool hasUncheckedSymbol = dataReader.ReadBoolean();
276					if (hasUncheckedSymbol)
277					{
278						UncheckedSymbol = LoadMaterial(dataReader);
279					} // if
280					else
281					{
282						UncheckedSymbol = null;
283					} // else
284
285					// UncheckedSymbolHover style
286					bool hasUncheckedSymbolHover = dataReader.ReadBoolean();
287					if (hasUncheckedSymbolHover)
288					{
289						UncheckedSymbolHover = LoadMaterial(dataReader);
290					} // if
291					else
292					{
293						UncheckedSymbolHover = null;
294					} // else
295
296					// DisabledUncheckedSymbol style
297					bool hasDisabledUncheckedSymbol = dataReader.ReadBoolean();
298					if (hasDisabledUncheckedSymbol)
299					{
300						DisabledUncheckedSymbol = LoadMaterial(dataReader);
301					} // if
302					else
303					{
304						DisabledUncheckedSymbol = null;
305					} // else
306
307					// Checked style
308					bool hasCheckedDesign = dataReader.ReadBoolean();
309					if (hasCheckedDesign)
310					{
311						CheckedSymbol = LoadMaterial(dataReader);
312					} // if
313					else
314					{
315						CheckedSymbol = null;
316					} // else
317
318					// CheckedHover style
319					bool hasCheckedHoverDesign = dataReader.ReadBoolean();
320					if (hasCheckedHoverDesign)
321					{
322						CheckedSymbolHover = LoadMaterial(dataReader);
323					} // if
324					else
325					{
326						CheckedSymbolHover = null;
327					} // else
328
329					// DisabledChecked style
330					bool hasDisabledCheckedDesign = dataReader.ReadBoolean();
331					if (hasDisabledCheckedDesign)
332					{
333						DisabledCheckedSymbol = LoadMaterial(dataReader);
334					} // if
335					else
336					{
337						DisabledCheckedSymbol = null;
338					} // else
339
340					// TextBackground style
341					bool hasTextBackgroundDesign = dataReader.ReadBoolean();
342					if (hasTextBackgroundDesign)
343					{
344						TextBackground = LoadMaterial(dataReader);
345					} // if
346					else
347					{
348						TextBackground = null;
349					} // else
350
351					// DisabledTextBackground style
352					bool hasDisabledTextBackgroundDesign = dataReader.ReadBoolean();
353					if (hasDisabledTextBackgroundDesign)
354					{
355						DisabledTextBackground = LoadMaterial(dataReader);
356					} // if
357					else
358					{
359						DisabledTextBackground = null;
360					} // else
361					break;
362
363				default:
364					Log.InvalidVersionWarning(GetType().Name, version, VersionNumber);
365					break;
366			} // switch
367		}
368		#endregion
369	}
370}