PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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