BsHandleSliderLine.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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), 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.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& inputDelta)
  53. {
  54. assert(getState() == State::Active);
  55. mCurrentPointerPos += inputDelta;
  56. mDelta = calcDelta(camera, mStartPosition, getTransform().multiplyAffine(mDirection), mStartPointerPos, mCurrentPointerPos);
  57. }
  58. }