guiAutoScrollCtrl.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "gui/containers/guiAutoScrollCtrl.h"
  23. #include "console/consoleTypes.h"
  24. //////////////////////////////////////////////////////////////////////////
  25. // GuiAutoScrollCtrl
  26. //////////////////////////////////////////////////////////////////////////
  27. IMPLEMENT_CONOBJECT(GuiAutoScrollCtrl);
  28. GuiAutoScrollCtrl::GuiAutoScrollCtrl()
  29. {
  30. mScrolling = false;
  31. mCurrentTime = 0.0f;
  32. mStartDelay = 3.0f;
  33. mResetDelay = 5.0f;
  34. mChildBorder = 10;
  35. mScrollSpeed = 1.0f;
  36. mTickCallback = false;
  37. mIsContainer = true;
  38. // Make sure we receive our ticks.
  39. setProcessTicks();
  40. }
  41. GuiAutoScrollCtrl::~GuiAutoScrollCtrl()
  42. {
  43. }
  44. //////////////////////////////////////////////////////////////////////////
  45. // Persistence
  46. //////////////////////////////////////////////////////////////////////////
  47. void GuiAutoScrollCtrl::initPersistFields()
  48. {
  49. Parent::initPersistFields();
  50. addField("startDelay", TypeF32, Offset(mStartDelay, GuiAutoScrollCtrl));
  51. addField("resetDelay", TypeF32, Offset(mResetDelay, GuiAutoScrollCtrl));
  52. addField("childBorder", TypeS32, Offset(mChildBorder, GuiAutoScrollCtrl));
  53. addField("scrollSpeed", TypeF32, Offset(mScrollSpeed, GuiAutoScrollCtrl));
  54. addField("tickCallback", TypeBool, Offset(mTickCallback, GuiAutoScrollCtrl));
  55. }
  56. void GuiAutoScrollCtrl::onChildAdded(GuiControl* control)
  57. {
  58. resetChild(control);
  59. }
  60. void GuiAutoScrollCtrl::onChildRemoved(GuiControl* control)
  61. {
  62. mScrolling = false;
  63. }
  64. void GuiAutoScrollCtrl::resetChild(GuiControl* control)
  65. {
  66. Point2I extent = control->getExtent();
  67. control->setPosition(Point2I(mChildBorder, mChildBorder));
  68. control->setExtent(Point2I(getExtent().x - (mChildBorder * 2), extent.y));
  69. mControlPositionY = (F32)control->getPosition().y;
  70. if ((mControlPositionY + extent.y) > getExtent().y)
  71. mScrolling = true;
  72. else
  73. mScrolling = false;
  74. mCurrentTime = 0.0f;
  75. }
  76. void GuiAutoScrollCtrl::resize( const Point2I &newPosition, const Point2I &newExtent )
  77. {
  78. Parent::resize( newPosition, newExtent );
  79. for (iterator i = begin(); i != end(); i++)
  80. {
  81. GuiControl* control = static_cast<GuiControl*>(*i);
  82. if (control)
  83. resetChild(control);
  84. }
  85. }
  86. void GuiAutoScrollCtrl::childResized(GuiControl *child)
  87. {
  88. Parent::childResized( child );
  89. resetChild(child);
  90. }
  91. void GuiAutoScrollCtrl::processTick()
  92. {
  93. if (mTickCallback && isMethod("onTick") )
  94. Con::executef(this, 1, "onTick");
  95. }
  96. void GuiAutoScrollCtrl::advanceTime(F32 timeDelta)
  97. {
  98. if (!mScrolling)
  99. return;
  100. if ((mCurrentTime + timeDelta) < mStartDelay)
  101. {
  102. mCurrentTime += timeDelta;
  103. return;
  104. }
  105. GuiControl* control = static_cast<GuiControl*>(at(0));
  106. if (!control)
  107. return;
  108. Point2I oldPosition = control->getPosition();
  109. if ((oldPosition.y + control->getExtent().y) < (getExtent().y - mChildBorder))
  110. {
  111. mCurrentTime += timeDelta;
  112. if (mCurrentTime > (mStartDelay + mResetDelay))
  113. {
  114. resetChild(control);
  115. return;
  116. }
  117. }
  118. else
  119. {
  120. mControlPositionY -= mScrollSpeed * timeDelta;
  121. control->setPosition(Point2I(oldPosition.x, (S32)mControlPositionY));
  122. }
  123. }