BsGUITimeline.cpp 3.5 KB

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