BsHandleSlider.cpp 3.4 KB

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