2
0

BsHandleSlider.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "BsHandleSlider.h"
  2. #include "BsCCamera.h"
  3. #include "BsHandleManager.h"
  4. #include "BsDebug.h"
  5. namespace BansheeEngine
  6. {
  7. HandleSlider::HandleSlider(bool fixedScale, UINT64 layer)
  8. :mFixedScale(fixedScale), mScale(Vector3::ONE), mTransformDirty(true),
  9. mDistanceScale(1.0f), mLayer(layer), mEnabled(true)
  10. {
  11. }
  12. void HandleSlider::update(const CameraPtr& 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. void HandleSlider::setEnabled(bool enabled)
  36. {
  37. mEnabled = enabled;
  38. }
  39. const Matrix4& HandleSlider::getTransform() const
  40. {
  41. if (mTransformDirty)
  42. updateCachedTransform();
  43. return mTransform;
  44. }
  45. const Matrix4& HandleSlider::getTransformInv() const
  46. {
  47. if (mTransformDirty)
  48. updateCachedTransform();
  49. return mTransformInv;
  50. }
  51. void HandleSlider::updateCachedTransform() const
  52. {
  53. if (mFixedScale)
  54. {
  55. mTransform.setTRS(mPosition, mRotation, mScale * mDistanceScale);
  56. mTransformInv.setInverseTRS(mPosition, mRotation, mScale * mDistanceScale);
  57. }
  58. else
  59. {
  60. mTransform.setTRS(mPosition, mRotation, mScale);
  61. mTransformInv.setInverseTRS(mPosition, mRotation, mScale);
  62. }
  63. mTransformDirty = false;
  64. }
  65. void HandleSlider::setInactive()
  66. {
  67. mState = State::Inactive;
  68. reset();
  69. }
  70. void HandleSlider::setActive(const CameraPtr& camera, const Vector2I& pointerPos)
  71. {
  72. mState = State::Active;
  73. mStartPointerPos = pointerPos;
  74. mCurrentPointerPos = pointerPos;
  75. activate(camera, pointerPos);
  76. }
  77. void HandleSlider::setHover()
  78. {
  79. mState = State::Hover;
  80. reset();
  81. }
  82. float HandleSlider::calcDelta(const CameraPtr& camera, const Vector3& position, const Vector3& direction,
  83. const Vector2I& pointerStart, const Vector2I& pointerEnd)
  84. {
  85. // position + direction can sometimes project behind the camera (if the camera is looking at position
  86. // from very close and at an angle), which will cause the delta to be reversed, so we compensate.
  87. float negate = 1.0f;
  88. Vector3 cameraDir = -camera->getRotation().zAxis();
  89. if (cameraDir.dot((position + direction) - camera->getPosition()) <= 0.0f)
  90. negate = -1.0f; // Point behind the camera
  91. Vector2I handleStart2D = camera->worldToScreenPoint(position);
  92. Vector2I handleEnd2D = camera->worldToScreenPoint(position + direction);
  93. Vector2I handleDir2D = handleEnd2D - handleStart2D;
  94. INT32 sqrdMag = handleDir2D.squaredLength();
  95. if (sqrdMag == 0)
  96. return 0.0f;
  97. Vector2I diffStart = pointerStart - handleStart2D;
  98. Vector2I diffEnd = pointerEnd - handleStart2D;
  99. float mag = sqrt((float)sqrdMag);
  100. float tStart = handleDir2D.dot(diffStart) / mag;
  101. float tEnd = handleDir2D.dot(diffEnd) / mag;
  102. float arbitraryScale = 1.0f / 100.0f;
  103. return negate * (tEnd - tStart) * arbitraryScale;
  104. }
  105. }