BsHandleSliderLine.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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)
  13. :HandleSlider(fixedScale), mLength(length)
  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.transform(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. return gotIntersect;
  51. }
  52. void HandleSliderLine::handleInput(const CameraHandlerPtr& camera, const Vector2I& pointerPos, const Ray& ray)
  53. {
  54. assert(getState() == State::Active);
  55. mLastPointerPos = mCurPointerPos;
  56. mCurPointerPos = pointerPos;
  57. if (mHasLastPos)
  58. mDelta = calcDelta(camera, getPosition(), mDirection, mLastPointerPos, mCurPointerPos);
  59. mHasLastPos = true;
  60. }
  61. Vector3 HandleSliderLine::getNewPosition() const
  62. {
  63. return getPosition() + mDirection * getDelta();
  64. }
  65. }