BsHandleSliderLine.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "BsHandleSliderLine.h"
  2. #include "BsHandleManager.h"
  3. #include "BsHandleSliderManager.h"
  4. #include "BsCapsule.h"
  5. #include "BsLineSegment3.h"
  6. #include "BsSphere.h"
  7. #include "BsRay.h"
  8. #include "BsDebug.h"
  9. namespace BansheeEngine
  10. {
  11. const float HandleSliderLine::CAPSULE_RADIUS = 0.05f;
  12. const float HandleSliderLine::SPHERE_RADIUS = 0.2f;
  13. HandleSliderLine::HandleSliderLine(const Vector3& direction, float length, bool fixedScale)
  14. :HandleSlider(fixedScale), mLength(length), mDelta(0.0f)
  15. {
  16. mDirection = Vector3::normalize(direction);
  17. Vector3 start = Vector3::ZERO;
  18. Vector3 end = start + mDirection * length;
  19. Vector3 sphereCenter = start + mDirection * std::max(0.0f, length - SPHERE_RADIUS);
  20. mCapsuleCollider = Capsule(LineSegment3(start, end), CAPSULE_RADIUS);
  21. mSphereCollider = Sphere(sphereCenter, SPHERE_RADIUS);
  22. HandleSliderManager& sliderManager = HandleManager::instance().getSliderManager();
  23. sliderManager._registerSlider(this);
  24. }
  25. HandleSliderLine::~HandleSliderLine()
  26. {
  27. HandleSliderManager& sliderManager = HandleManager::instance().getSliderManager();
  28. sliderManager._unregisterSlider(this);
  29. }
  30. bool HandleSliderLine::intersects(const Ray& ray, float& t) const
  31. {
  32. Ray localRay = ray;
  33. localRay.transformAffine(getTransformInv());
  34. auto capsuleIntersect = mCapsuleCollider.intersects(localRay);
  35. auto sphereIntersect = mSphereCollider.intersects(localRay);
  36. t = std::numeric_limits<float>::max();
  37. bool gotIntersect = false;
  38. if (capsuleIntersect.first)
  39. {
  40. t = capsuleIntersect.second;
  41. gotIntersect = true;
  42. }
  43. if (sphereIntersect.first)
  44. {
  45. if (sphereIntersect.second < t)
  46. {
  47. t = sphereIntersect.second;
  48. gotIntersect = true;
  49. }
  50. }
  51. if (gotIntersect)
  52. {
  53. Vector3 intrPoint = localRay.getPoint(t);
  54. intrPoint = getTransform().multiplyAffine(intrPoint);
  55. t = (intrPoint - ray.getOrigin()).length(); // Get distance in world space
  56. }
  57. return gotIntersect;
  58. }
  59. void HandleSliderLine::handleInput(const CameraPtr& camera, const Vector2I& inputDelta)
  60. {
  61. assert(getState() == State::Active);
  62. mCurrentPointerPos += inputDelta;
  63. Vector3 worldDir = getRotation().rotate(mDirection);
  64. mDelta = calcDelta(camera, mStartPosition, worldDir, mStartPointerPos, mCurrentPointerPos);
  65. }
  66. }