PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 8ms RepoModel.GetById 32ms app.codeStats 0ms

/Scenes/UserInterfaces/Controls/Scrollbar.cs

#
C# | 297 lines | 162 code | 36 blank | 99 comment | 5 complexity | cf12f76c0a6d27b6b9649e2cd3c59edb MD5 | raw file
  1using Delta.InputSystem;
  2using Delta.Scenes.Enums;
  3using Delta.Utilities.Datatypes;
  4
  5namespace Delta.Scenes.UserInterfaces.Controls
  6{
  7	/// <summary>
  8	/// Scrollbar
  9	/// Note: Is only horizontal yet.
 10	/// </summary>
 11	internal class Scrollbar : BaseControl
 12	{
 13		#region ScrollingType Enum
 14		/// <summary>
 15		/// Scrolling type
 16		/// Note: That is currently NO CONTROL YET.
 17		/// </summary>
 18		protected enum ScrollingType
 19		{
 20			Horizontal,
 21
 22			Vertical,
 23		}
 24		#endregion
 25
 26		#region Constants
 27		/// <summary>
 28		/// ScrollbarHeight
 29		/// </summary>
 30		public const float ScrollbarHeight = 0.02f;
 31		#endregion
 32
 33		#region CurrentScrollOffset (Public)
 34		/// <summary>
 35		/// The current amount that the scrollbar was scrolled in the panel.
 36		/// </summary>
 37		public float CurrentScrollOffset;
 38		#endregion
 39
 40		#region VisibleScrollArea (Public)
 41		/// <summary>
 42		/// Represents the current visible area resulting from the current used/set
 43		/// scrolled offset.
 44		/// </summary>
 45		public Rectangle VisibleScrollArea
 46		{
 47			get
 48			{
 49				return new Rectangle(CurrentScrollOffset, 0.0f, ScrollWidth,
 50					owner.Size.Height);
 51			} // get
 52		}
 53		#endregion
 54
 55		#region ScrollWidth (Public)
 56		/// <summary>
 57		/// Represents the amount that the inner panel elements would be
 58		/// moved/scrolled at the next time the "Forward" or "Backward" action
 59		/// of the scrollbar is executed.
 60		/// </summary>
 61		public float ScrollWidth
 62		{
 63			get
 64			{
 65				return owner.Size.Width;
 66			} // get
 67		}
 68		#endregion
 69
 70		#region Protected
 71
 72		#region ScrollingMode (Protected)
 73		/// <summary>
 74		/// Scrolling mode
 75		/// </summary>
 76		protected ScrollingType ScrollingMode
 77		{
 78			get;
 79			private set;
 80		}
 81		#endregion
 82
 83		// ScrollWidth
 84
 85		///// <summary>
 86		///// Is visible
 87		///// </summary>
 88		//public bool IsVisible
 89		//{
 90		//  get
 91		//  {
 92		//    return backwardButton.State > ControlState.Invisible;
 93		//  }
 94		//  set
 95		//  {
 96		//    // You do something if the state would change now
 97		//    if (value == IsVisible)
 98		//    {
 99		//      return;
100		//    } // if
101
102		//    // If the scrollbar should be hidden now
103		//    if (value == false)
104		//    {
105		//      // then hide all inner controls
106		//      backwardButton.State = ControlState.Invisible;
107		//      forwardButton.State = ControlState.Invisible;
108		//    } // if
109
110		//    // if the it should be visible now again
111		//    else
112		//    {
113		//      // then show all inner controls again too
114		//      backwardButton.State = ControlState.Enabled;
115		//      forwardButton.State = ControlState.Enabled;
116		//    } // else
117		//  } // set
118		//} // IsVisible
119
120		#region FallbackDesign (Protected)
121		/// <summary>
122		/// Fallback design
123		/// </summary>
124		protected override ControlDesign FallbackDesign
125		{
126			get
127			{
128				return Theme.Current.ImageDesign;
129			} // get
130		}
131		#endregion
132
133		#endregion
134
135		#region Private
136
137		#region owner (Private)
138		private readonly Panel owner;
139		#endregion
140
141		#region backwardButton (Private)
142		private readonly Button backwardButton;
143		#endregion
144
145		#region forwardButton (Private)
146		private readonly Button forwardButton;
147		#endregion
148
149		#endregion
150
151		#region Constructors
152		/// <summary>
153		/// Create scrollbar
154		/// </summary>
155		/// <param name="setOwner">Set owner</param>
156		public Scrollbar(Panel setOwner)
157		{
158			// Init the data of the scrollbar
159			owner = setOwner;
160			ScrollingMode = ScrollingType.Horizontal;
161
162			CurrentScrollOffset = 0.0f;
163
164			Size buttonSize = new Size(ScrollbarHeight, ScrollbarHeight);
165			backwardButton = new Button
166			{
167				Name = setOwner.Name + "." + ScrollingMode + GetType().Name +
168				       "BackwardButton",
169				Size = buttonSize,
170				//LocalArea = new Rectangle(0, 0, ScrollbarHeight, ScrollbarHeight),
171				//LocalArea = GetDockingArea(owner.Size, buttonSize,
172				//  DockingMode.BottomLeft),
173				Text = "<",
174			};
175			backwardButton.Clicked += delegate(BaseControl sender,
176				CommandTrigger input)
177			{
178				ScrollBackwards();
179				input.IsHandled = true;
180			};
181			Add(backwardButton);
182
183			forwardButton = new Button
184			{
185				Name = setOwner.Name + "." + ScrollingMode + GetType().Name +
186				       "ForewardButton",
187				Size = buttonSize,
188				//LocalArea = new Rectangle(0, 0, ScrollbarHeight, ScrollbarHeight),
189				//LocalArea = GetDockingArea(owner.Size, buttonSize,
190				//  DockingMode.BottomRight),
191				Text = ">",
192			};
193			forwardButton.Clicked += delegate(BaseControl sender,
194				CommandTrigger input)
195			{
196				ScrollForewards();
197				input.IsHandled = true;
198			};
199			Add(forwardButton);
200
201			// The initial Size and MinSize of the scrollbar is the size of the
202			// back and forward button
203			//MinSize = buttonSize;
204			Size = buttonSize;
205		}
206		#endregion
207
208		#region ScrollForewards (Public)
209		/// <summary>
210		/// Scroll foreward
211		/// </summary>
212		public void ScrollForewards()
213		{
214			float horizontalContentWidth = owner.GetContentWidth();
215
216			if (horizontalContentWidth <= ScrollWidth)
217			{
218				// It only make sense to scroll if the content area is bigger than the
219				// panel area
220				return;
221			} // if
222
223			float newScrollOffset = CurrentScrollOffset + ScrollWidth;
224			if ((newScrollOffset + ScrollWidth) < horizontalContentWidth)
225			{
226				CurrentScrollOffset = newScrollOffset;
227			} // if
228			else
229			{
230				CurrentScrollOffset = horizontalContentWidth - ScrollWidth;
231			} // else
232		}
233		#endregion
234
235		#region ScrollBackwards (Public)
236		/// <summary>
237		/// Scroll backwards
238		/// </summary>
239		public void ScrollBackwards()
240		{
241			float horizontalContentWidth = owner.GetContentWidth();
242
243			if (horizontalContentWidth <= ScrollWidth)
244			{
245				// It only make sense to scroll if the content area is bigger than the
246				// panel area
247				return;
248			} // if
249
250			float newScrollOffset = CurrentScrollOffset - ScrollWidth;
251			// If there is even after this scrolling content left that we can scroll
252			// next time
253			if (newScrollOffset > 0.0f)
254			{
255				// then make a "full" scrolling
256				CurrentScrollOffset = newScrollOffset;
257			} // if
258
259				// else if we have reached the beginning again
260			else
261			{
262				// then let's start there
263				CurrentScrollOffset = 0.0f;
264			} // else
265		}
266		#endregion
267
268		#region Methods (Private)
269
270		#region OnSizeChanging
271		/// <summary>
272		/// On size changing
273		/// </summary>
274		/// <param name="oldSize">Old size</param>
275		/// <returns>
276		/// 'True' if the new value can be used or 'false' if the change should be
277		/// aborted.
278		/// </returns>
279		protected override bool OnSizeChanging(Size oldSize)
280		{
281			if (base.OnSizeChanging(oldSize))
282			{
283				backwardButton.LocalArea = Panel.GetAlignmentArea(Size,
284					backwardButton.Size, Alignment.BottomLeft);
285				forwardButton.LocalArea = Panel.GetAlignmentArea(Size,
286					forwardButton.Size, Alignment.BottomRight);
287
288				return true;
289			} // if
290
291			return false;
292		}
293		#endregion
294
295		#endregion
296	}
297}