BsGUITimeline.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include <utility>
  4. #include "GUI/BsGUITimeline.h"
  5. #include "GUI/BsGUIDimensions.h"
  6. #include "GUI/BsGUIMouseEvent.h"
  7. #include "GUI/BsGUICanvas.h"
  8. #include "Math/BsLine2.h"
  9. #include "GUI/BsGUIWidget.h"
  10. #include "GUI/BsGUITexture.h"
  11. namespace bs
  12. {
  13. GUITimeline::GUITimeline(const String& styleName, const GUIDimensions& dimensions)
  14. :GUIElementContainer(dimensions, styleName)
  15. {
  16. mCanvas = GUICanvas::create();
  17. mBackground = GUITexture::create(getSubStyleName(BACKGROUND_STYLE_TYPE));
  18. mBackground->_setElementDepth(255);
  19. _registerChildElement(mCanvas);
  20. _registerChildElement(mBackground);
  21. }
  22. const String& GUITimeline::getGUITypeName()
  23. {
  24. static String name = "Timeline";
  25. return name;
  26. }
  27. void GUITimeline::setRange(float range)
  28. {
  29. mRange = std::max(0.0f, range);
  30. _markContentAsDirty();
  31. }
  32. float GUITimeline::getRange() const
  33. {
  34. const float spf = 1.0f / (float)mFPS;
  35. return std::max(1.0f, mRange / spf) * spf;
  36. }
  37. void GUITimeline::setOffset(float offset)
  38. {
  39. mOffset = offset;
  40. _markContentAsDirty();
  41. }
  42. void GUITimeline::setFPS(UINT32 FPS)
  43. {
  44. mFPS = std::max(1U, FPS);
  45. _markContentAsDirty();
  46. }
  47. void GUITimeline::setMarkedFrame(UINT32 index)
  48. {
  49. mMarkedFrame = index;
  50. _markContentAsDirty();
  51. }
  52. void GUITimeline::setPadding(UINT32 padding)
  53. {
  54. mPadding = padding;
  55. _markContentAsDirty();
  56. }
  57. UINT32 GUITimeline::getFrame(const Vector2I& pixelCoords) const
  58. {
  59. const Rect2I& bounds = mLayoutData.area;
  60. if (pixelCoords.x < (INT32)mPadding ||
  61. pixelCoords.x >= ((INT32)bounds.width - (INT32)mPadding) ||
  62. pixelCoords.y < 0 ||
  63. pixelCoords.y >= (INT32)bounds.height)
  64. {
  65. return -1;
  66. }
  67. const Vector2I relativeCoords = pixelCoords - Vector2I(bounds.x + mPadding, bounds.y);
  68. const float lengthPerPixel = getRange() / getDrawableWidth();
  69. const float time = mOffset + relativeCoords.x * lengthPerPixel;
  70. return Math::roundToPosInt(time * mFPS);
  71. }
  72. float GUITimeline::getTime(INT32 pixel) const
  73. {
  74. const Rect2I& bounds = mLayoutData.area;
  75. const INT32 relativeCoords = pixel - (bounds.x + mPadding);
  76. const float lengthPerPixel = getRange() / getDrawableWidth();
  77. return mOffset + relativeCoords * lengthPerPixel;
  78. }
  79. INT32 GUITimeline::getOffset(float time) const
  80. {
  81. return (INT32)(((time - mOffset) / getRange()) * getDrawableWidth()) + mPadding;
  82. }
  83. float GUITimeline::getTimeForFrame(INT32 index) const
  84. {
  85. return index / (float)mFPS;
  86. }
  87. UINT32 GUITimeline::getDrawableWidth() const
  88. {
  89. return std::max(0, (INT32)mLayoutData.area.width - (INT32)mPadding * 2);
  90. }
  91. float GUITimeline::getRangeWithPadding() const
  92. {
  93. const float spf = 1.0f / (float)mFPS;
  94. const float lengthPerPixel = mRange / getDrawableWidth();
  95. const float range = mRange + lengthPerPixel * mPadding;
  96. return std::max(1.0f, range / spf) * spf;
  97. }
  98. void GUITimeline::drawFrameMarker(float t)
  99. {
  100. const INT32 xPos = (INT32)(((t - mOffset) / getRange()) * getDrawableWidth()) + mPadding;
  101. const Vector2I start(xPos, 0);
  102. const Vector2I end(xPos, mLayoutData.area.height);
  103. mCanvas->drawLine(start, end, Color::BansheeOrange);
  104. }
  105. void GUITimeline::drawFrameMarker()
  106. {
  107. if(mMarkedFrame != (UINT32)-1)
  108. drawFrameMarker(mMarkedFrame / (float)mFPS);
  109. }
  110. void GUITimeline::_updateLayoutInternal(const GUILayoutData& data)
  111. {
  112. mBackground->_setLayoutData(data);
  113. mCanvas->_setLayoutData(data);
  114. mCanvas->_updateLayoutInternal(data);
  115. }
  116. Vector2I GUITimeline::_getOptimalSize() const
  117. {
  118. return mCanvas->_getOptimalSize();
  119. }
  120. void GUITimeline::styleUpdated()
  121. {
  122. mCanvas->setStyle(GUICanvas::getGUITypeName());
  123. mBackground->setStyle(getSubStyleName(BACKGROUND_STYLE_TYPE));
  124. }
  125. }