PageRenderTime 39ms CodeModel.GetById 18ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Scenes/UserInterfaces/Designs/SliderDesign.cs

#
C# | 247 lines | 158 code | 21 blank | 68 comment | 17 complexity | 1ddba9be48118f241c361b74a4f8cac8 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 the slider control.
 13	/// </summary>
 14	public class SliderDesign : 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 Tracker (Public)
 24		/// <summary>
 25		/// The "knob" of the slider which visualize the current value
 26		/// (in enabled-state).
 27		/// </summary>
 28		/// <value>
 29		/// The tracker.
 30		/// </value>
 31		public Material2DColored Tracker
 32		{
 33			get;
 34			set;
 35		}
 36		#endregion
 37
 38		#region DisabledTracker (Public)
 39		/// <summary>
 40		/// The "knob" of the slider which visualize the current value
 41		/// (in disabled-state).
 42		/// </summary>
 43		/// <value>
 44		/// The disabled tracker.
 45		/// </value>
 46		public Material2DColored DisabledTracker
 47		{
 48			get;
 49			set;
 50		}
 51		#endregion
 52
 53		#region Trackerbar (Public)
 54		/// <summary>
 55		/// The visual design of the tracker-bar (in enabled-state).
 56		/// </summary>
 57		/// <value>
 58		/// The trackerbar.
 59		/// </value>
 60		public Material2DColored Trackerbar
 61		{
 62			get;
 63			set;
 64		}
 65		#endregion
 66
 67		#region DisabledTrackerbar (Public)
 68		/// <summary>
 69		/// The visual design of the tracker-bar (in disabled-state).
 70		/// </summary>
 71		/// <value>
 72		/// The disabled trackerbar.
 73		/// </value>
 74		public Material2DColored DisabledTrackerbar
 75		{
 76			get;
 77			set;
 78		}
 79		#endregion
 80
 81		#region DrawControl (Public)
 82		/// <summary>
 83		/// Draw control
 84		/// </summary>
 85		/// <param name="control">Control</param>
 86		/// <param name="drawArea">Draw area</param>
 87		public override void DrawControl(BaseControl control, Rectangle drawArea)
 88		{
 89			if (control is Slider)
 90			{
 91				Slider slider = (Slider)control;
 92				switch (slider.State)
 93				{
 94					case ElementState.Disabled:
 95						// Draw the tracker-bar
 96						DrawStyle(slider, DisabledTrackerbar, slider.TrackbarDrawArea);
 97
 98						// and the tracker-knob in "disabled" designs
 99						DrawStyle(slider, DisabledTracker, slider.MarkerDrawArea);
100						break;
101
102					case ElementState.Enabled:
103					case ElementState.Hovered:
104					case ElementState.Pressed:
105						// Draw the tracker-bar
106						DrawStyle(slider, Trackerbar, slider.TrackbarDrawArea);
107
108						// and display the tracker "knob" too
109						// but at first we have to check if the control is in "hover mode"
110						bool isInHoverMode = Hover != null &&
111						                     slider.State == ElementState.Hovered;
112						// before we can draw the style
113						DrawStyle(slider, isInHoverMode
114						                  	? Hover
115						                  	: Tracker,
116							slider.MarkerDrawArea);
117						break;
118				} // switch
119			} // if
120			else
121			{
122				throw new InvalidOperationException("The given control '" + control +
123				                                    "' is no 'Slider'.");
124			} // else
125		}
126		#endregion
127
128		#region Save (Public)
129		/// <summary>
130		/// Saves all data which are necessary to restore the object again.
131		/// </summary>
132		/// <param name="dataWriter">The writer which contains the stream where the data should be saved
133		/// into now.</param>
134		public override void Save(BinaryWriter dataWriter)
135		{
136			// At first we write the data of the base class
137			base.Save(dataWriter);
138
139			// and then save the version of the current data format
140			dataWriter.Write(VersionNumber);
141
142			// before we can finally save the properties
143			// Tracker style
144			bool hasTrackerDesign = Tracker != null;
145			dataWriter.Write(hasTrackerDesign);
146			if (hasTrackerDesign)
147			{
148				SaveMaterial(dataWriter, Tracker);
149			} // if
150
151			// DisabledTracker style
152			bool hasDisabledTrackerDesign = DisabledTracker != null;
153			dataWriter.Write(hasDisabledTrackerDesign);
154			if (hasDisabledTrackerDesign)
155			{
156				SaveMaterial(dataWriter, DisabledTracker);
157			} // if
158
159			// Trackerbar style
160			bool hasTrackerbarDesign = Trackerbar != null;
161			dataWriter.Write(hasTrackerbarDesign);
162			if (hasTrackerbarDesign)
163			{
164				SaveMaterial(dataWriter, Trackerbar);
165			} // if
166
167			// DisabledTrackerbar style
168			bool hasDisabledTrackerbarDesign = DisabledTrackerbar != null;
169			dataWriter.Write(hasDisabledTrackerbarDesign);
170			if (hasDisabledTrackerbarDesign)
171			{
172				SaveMaterial(dataWriter, DisabledTrackerbar);
173			} // if
174		}
175		#endregion
176
177		#region Load (Public)
178		/// <summary>
179		/// Loads and restores all previously saved values that belongs to this
180		/// class only from the given data reader.
181		/// </summary>
182		/// <param name="dataReader">The reader which contains the stream with the saved data which needs to
183		/// be loaded now.</param>
184		public override void Load(BinaryReader dataReader)
185		{
186			// At first we need to load all data of the base class
187			base.Load(dataReader);
188
189			// and then check which version of the data need to load now
190			int version = dataReader.ReadInt32();
191			switch (version)
192			{
193					// Version 1
194				case VersionNumber:
195					// Tracker style
196					bool hasTrackerDesign = dataReader.ReadBoolean();
197					if (hasTrackerDesign)
198					{
199						Tracker = LoadMaterial(dataReader);
200					} // if
201					else
202					{
203						Tracker = null;
204					} // else
205
206					// DisabledTracker style
207					bool hasDisabledTrackerDesign = dataReader.ReadBoolean();
208					if (hasDisabledTrackerDesign)
209					{
210						DisabledTracker = LoadMaterial(dataReader);
211					} // if
212					else
213					{
214						DisabledTracker = null;
215					} // else
216
217					// Trackerbar style
218					bool hasTrackerbarDesign = dataReader.ReadBoolean();
219					if (hasTrackerbarDesign)
220					{
221						Trackerbar = LoadMaterial(dataReader);
222					} // if
223					else
224					{
225						Trackerbar = null;
226					} // else
227
228					// DisabledTrackerbar style
229					bool hasDisabledTrackerbarDesign = dataReader.ReadBoolean();
230					if (hasDisabledTrackerbarDesign)
231					{
232						DisabledTrackerbar = LoadMaterial(dataReader);
233					} // if
234					else
235					{
236						DisabledTrackerbar = null;
237					} // else
238					break;
239
240				default:
241					Log.InvalidVersionWarning(GetType().Name, version, VersionNumber);
242					break;
243			} // switch
244		}
245		#endregion
246	}
247}