2
0

BsHandleSlider.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "BsHandleSlider.h"
  2. #include "BsCamera.h"
  3. #include "BsHandleManager.h"
  4. namespace BansheeEngine
  5. {
  6. HandleSlider::HandleSlider(bool fixedScale)
  7. :mFixedScale(fixedScale), mScale(Vector3::ONE), mTransformDirty(true),
  8. mDistanceScale(1.0f)
  9. {
  10. }
  11. void HandleSlider::update(const CameraHandlerPtr& camera)
  12. {
  13. if (mFixedScale)
  14. {
  15. mDistanceScale = HandleManager::instance().getHandleSize(camera, mPosition);
  16. mTransformDirty = true;
  17. }
  18. }
  19. void HandleSlider::setPosition(const Vector3& position)
  20. {
  21. mPosition = position;
  22. mTransformDirty = true;
  23. }
  24. void HandleSlider::setRotation(const Quaternion& rotation)
  25. {
  26. mRotation = rotation;
  27. mTransformDirty = true;
  28. }
  29. void HandleSlider::setScale(const Vector3& scale)
  30. {
  31. mScale = scale;
  32. mTransformDirty = true;
  33. }
  34. const Matrix4& HandleSlider::getTransform() const
  35. {
  36. if (mTransformDirty)
  37. updateCachedTransform();
  38. return mTransform;
  39. }
  40. const Matrix4& HandleSlider::getTransformInv() const
  41. {
  42. if (mTransformDirty)
  43. updateCachedTransform();
  44. return mTransformInv;
  45. }
  46. void HandleSlider::updateCachedTransform() const
  47. {
  48. if (mFixedScale)
  49. {
  50. mTransform.setTRS(mPosition, mRotation, mScale * mDistanceScale);
  51. mTransformInv.setInverseTRS(mPosition, mRotation, mScale * mDistanceScale);
  52. }
  53. else
  54. {
  55. mTransform.setTRS(mPosition, mRotation, mScale);
  56. mTransformInv.setInverseTRS(mPosition, mRotation, mScale);
  57. }
  58. mTransformDirty = false;
  59. }
  60. void HandleSlider::setInactive()
  61. {
  62. mState = State::Inactive;
  63. reset();
  64. }
  65. void HandleSlider::setActive(const CameraHandlerPtr& camera, const Vector2I& pointerPos)
  66. {
  67. mState = State::Active;
  68. mStartPointerPos = pointerPos;
  69. mCurrentPointerPos = pointerPos;
  70. activate(camera, pointerPos);
  71. }
  72. void HandleSlider::setHover()
  73. {
  74. mState = State::Hover;
  75. reset();
  76. }
  77. float HandleSlider::calcDelta(const CameraHandlerPtr& camera, const Vector3& position, const Vector3& direction,
  78. const Vector2I& pointerStart, const Vector2I& pointerEnd)
  79. {
  80. // position + direction can sometimes project behind the camera (if the camera is looking at position
  81. // from very close and at an angle), which will cause the delta to be reversed, so we compensate.
  82. float negate = 1.0f;
  83. Vector3 cameraDir = -camera->getRotation().zAxis();
  84. if (cameraDir.dot((position + direction) - camera->getPosition()) <= 0.0f)
  85. negate = -1.0f; // Point behind the camera
  86. Vector2I handleStart2D = camera->worldToScreenPoint(position);
  87. Vector2I handleEnd2D = camera->worldToScreenPoint(position + direction);
  88. Vector2I handleDir2D = handleEnd2D - handleStart2D;
  89. INT32 sqrdMag = handleDir2D.squaredLength();
  90. if (sqrdMag == 0)
  91. return 0.0f;
  92. Vector2I diffStart = pointerStart - handleStart2D;
  93. Vector2I diffEnd = pointerEnd - handleStart2D;
  94. float tStart = handleDir2D.dot(diffStart) / (float)sqrdMag;
  95. float tEnd = handleDir2D.dot(diffEnd) / (float)sqrdMag;
  96. return negate * (tEnd - tStart);
  97. }
  98. }