PageFlipControl.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //-----------------------------------------------------------------------------
  2. // PageFlipControl.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. namespace UserInterfaceSample.Controls
  9. {
  10. /// <summary>
  11. /// This control aligns its child controls horizontally, and allows the user to flick
  12. /// through them.
  13. /// </summary>
  14. class PageFlipControl : PanelControl
  15. {
  16. #region Fields
  17. // PageFlipTracker handles the logic of scrolling / tracking etc.
  18. private PageFlipTracker tracker = new PageFlipTracker();
  19. #endregion
  20. #region Method overrides
  21. protected override void OnChildAdded(int index, Control child)
  22. {
  23. tracker.PageWidthList.Insert(index, (int)child.Size.X);
  24. }
  25. protected override void OnChildRemoved(int index, Control child)
  26. {
  27. tracker.PageWidthList.RemoveAt(index);
  28. }
  29. public override void Update(GameTime gametime)
  30. {
  31. tracker.Update();
  32. base.Update(gametime);
  33. }
  34. public override void HandleInput(InputState input)
  35. {
  36. tracker.HandleInput(input);
  37. if(ChildCount > 0)
  38. {
  39. // Only the child that currently has focus gets input
  40. int current = tracker.CurrentPage;
  41. this[current].HandleInput(input);
  42. }
  43. }
  44. public override void Draw(DrawContext context)
  45. {
  46. int childCount = ChildCount;
  47. if (childCount < 2)
  48. {
  49. // Default rendering behavior if we don't have enough
  50. // children to flip through.
  51. base.Draw(context);
  52. return;
  53. }
  54. Vector2 origin = context.DrawOffset;
  55. int iCurrent = tracker.CurrentPage;
  56. float horizontalOffset = tracker.CurrentPageOffset;
  57. context.DrawOffset = origin + new Vector2 { X = horizontalOffset };
  58. this[iCurrent].Draw(context);
  59. if (horizontalOffset > 0)
  60. {
  61. // The screen has been dragged to the right, so the edge of another
  62. // page is visible to the left.
  63. int iLeft = (iCurrent + childCount - 1) % childCount;
  64. context.DrawOffset.X = origin.X + horizontalOffset - tracker.EffectivePageWidth(iLeft);
  65. this[iLeft].Draw(context);
  66. }
  67. if (horizontalOffset + this[iCurrent].Size.X < context.Device.Viewport.Width)
  68. {
  69. // The edge of another page is visible to the right.
  70. // Note that if we have two pages, it's possible that a page will be
  71. // drawn twice, with parts of it visible on each edge of the screen.
  72. int iRight = (iCurrent + 1) % childCount;
  73. context.DrawOffset.X = origin.X + horizontalOffset + tracker.EffectivePageWidth(iCurrent);
  74. this[iRight].Draw(context);
  75. }
  76. }
  77. #endregion
  78. }
  79. }