/Scenes/UserInterfaces/Controls/Scrollbar.cs
C# | 297 lines | 162 code | 36 blank | 99 comment | 5 complexity | cf12f76c0a6d27b6b9649e2cd3c59edb MD5 | raw file
Possible License(s): Apache-2.0
- using Delta.InputSystem;
- using Delta.Scenes.Enums;
- using Delta.Utilities.Datatypes;
-
- namespace Delta.Scenes.UserInterfaces.Controls
- {
- /// <summary>
- /// Scrollbar
- /// Note: Is only horizontal yet.
- /// </summary>
- internal class Scrollbar : BaseControl
- {
- #region ScrollingType Enum
- /// <summary>
- /// Scrolling type
- /// Note: That is currently NO CONTROL YET.
- /// </summary>
- protected enum ScrollingType
- {
- Horizontal,
-
- Vertical,
- }
- #endregion
-
- #region Constants
- /// <summary>
- /// ScrollbarHeight
- /// </summary>
- public const float ScrollbarHeight = 0.02f;
- #endregion
-
- #region CurrentScrollOffset (Public)
- /// <summary>
- /// The current amount that the scrollbar was scrolled in the panel.
- /// </summary>
- public float CurrentScrollOffset;
- #endregion
-
- #region VisibleScrollArea (Public)
- /// <summary>
- /// Represents the current visible area resulting from the current used/set
- /// scrolled offset.
- /// </summary>
- public Rectangle VisibleScrollArea
- {
- get
- {
- return new Rectangle(CurrentScrollOffset, 0.0f, ScrollWidth,
- owner.Size.Height);
- } // get
- }
- #endregion
-
- #region ScrollWidth (Public)
- /// <summary>
- /// Represents the amount that the inner panel elements would be
- /// moved/scrolled at the next time the "Forward" or "Backward" action
- /// of the scrollbar is executed.
- /// </summary>
- public float ScrollWidth
- {
- get
- {
- return owner.Size.Width;
- } // get
- }
- #endregion
-
- #region Protected
-
- #region ScrollingMode (Protected)
- /// <summary>
- /// Scrolling mode
- /// </summary>
- protected ScrollingType ScrollingMode
- {
- get;
- private set;
- }
- #endregion
-
- // ScrollWidth
-
- ///// <summary>
- ///// Is visible
- ///// </summary>
- //public bool IsVisible
- //{
- // get
- // {
- // return backwardButton.State > ControlState.Invisible;
- // }
- // set
- // {
- // // You do something if the state would change now
- // if (value == IsVisible)
- // {
- // return;
- // } // if
-
- // // If the scrollbar should be hidden now
- // if (value == false)
- // {
- // // then hide all inner controls
- // backwardButton.State = ControlState.Invisible;
- // forwardButton.State = ControlState.Invisible;
- // } // if
-
- // // if the it should be visible now again
- // else
- // {
- // // then show all inner controls again too
- // backwardButton.State = ControlState.Enabled;
- // forwardButton.State = ControlState.Enabled;
- // } // else
- // } // set
- //} // IsVisible
-
- #region FallbackDesign (Protected)
- /// <summary>
- /// Fallback design
- /// </summary>
- protected override ControlDesign FallbackDesign
- {
- get
- {
- return Theme.Current.ImageDesign;
- } // get
- }
- #endregion
-
- #endregion
-
- #region Private
-
- #region owner (Private)
- private readonly Panel owner;
- #endregion
-
- #region backwardButton (Private)
- private readonly Button backwardButton;
- #endregion
-
- #region forwardButton (Private)
- private readonly Button forwardButton;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Create scrollbar
- /// </summary>
- /// <param name="setOwner">Set owner</param>
- public Scrollbar(Panel setOwner)
- {
- // Init the data of the scrollbar
- owner = setOwner;
- ScrollingMode = ScrollingType.Horizontal;
-
- CurrentScrollOffset = 0.0f;
-
- Size buttonSize = new Size(ScrollbarHeight, ScrollbarHeight);
- backwardButton = new Button
- {
- Name = setOwner.Name + "." + ScrollingMode + GetType().Name +
- "BackwardButton",
- Size = buttonSize,
- //LocalArea = new Rectangle(0, 0, ScrollbarHeight, ScrollbarHeight),
- //LocalArea = GetDockingArea(owner.Size, buttonSize,
- // DockingMode.BottomLeft),
- Text = "<",
- };
- backwardButton.Clicked += delegate(BaseControl sender,
- CommandTrigger input)
- {
- ScrollBackwards();
- input.IsHandled = true;
- };
- Add(backwardButton);
-
- forwardButton = new Button
- {
- Name = setOwner.Name + "." + ScrollingMode + GetType().Name +
- "ForewardButton",
- Size = buttonSize,
- //LocalArea = new Rectangle(0, 0, ScrollbarHeight, ScrollbarHeight),
- //LocalArea = GetDockingArea(owner.Size, buttonSize,
- // DockingMode.BottomRight),
- Text = ">",
- };
- forwardButton.Clicked += delegate(BaseControl sender,
- CommandTrigger input)
- {
- ScrollForewards();
- input.IsHandled = true;
- };
- Add(forwardButton);
-
- // The initial Size and MinSize of the scrollbar is the size of the
- // back and forward button
- //MinSize = buttonSize;
- Size = buttonSize;
- }
- #endregion
-
- #region ScrollForewards (Public)
- /// <summary>
- /// Scroll foreward
- /// </summary>
- public void ScrollForewards()
- {
- float horizontalContentWidth = owner.GetContentWidth();
-
- if (horizontalContentWidth <= ScrollWidth)
- {
- // It only make sense to scroll if the content area is bigger than the
- // panel area
- return;
- } // if
-
- float newScrollOffset = CurrentScrollOffset + ScrollWidth;
- if ((newScrollOffset + ScrollWidth) < horizontalContentWidth)
- {
- CurrentScrollOffset = newScrollOffset;
- } // if
- else
- {
- CurrentScrollOffset = horizontalContentWidth - ScrollWidth;
- } // else
- }
- #endregion
-
- #region ScrollBackwards (Public)
- /// <summary>
- /// Scroll backwards
- /// </summary>
- public void ScrollBackwards()
- {
- float horizontalContentWidth = owner.GetContentWidth();
-
- if (horizontalContentWidth <= ScrollWidth)
- {
- // It only make sense to scroll if the content area is bigger than the
- // panel area
- return;
- } // if
-
- float newScrollOffset = CurrentScrollOffset - ScrollWidth;
- // If there is even after this scrolling content left that we can scroll
- // next time
- if (newScrollOffset > 0.0f)
- {
- // then make a "full" scrolling
- CurrentScrollOffset = newScrollOffset;
- } // if
-
- // else if we have reached the beginning again
- else
- {
- // then let's start there
- CurrentScrollOffset = 0.0f;
- } // else
- }
- #endregion
-
- #region Methods (Private)
-
- #region OnSizeChanging
- /// <summary>
- /// On size changing
- /// </summary>
- /// <param name="oldSize">Old size</param>
- /// <returns>
- /// 'True' if the new value can be used or 'false' if the change should be
- /// aborted.
- /// </returns>
- protected override bool OnSizeChanging(Size oldSize)
- {
- if (base.OnSizeChanging(oldSize))
- {
- backwardButton.LocalArea = Panel.GetAlignmentArea(Size,
- backwardButton.Size, Alignment.BottomLeft);
- forwardButton.LocalArea = Panel.GetAlignmentArea(Size,
- forwardButton.Size, Alignment.BottomRight);
-
- return true;
- } // if
-
- return false;
- }
- #endregion
-
- #endregion
- }
- }