BsHandleSliderLine.cpp 2.3 KB

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