BsHandleSlider.cpp 3.0 KB

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