BsHandleSliderLine.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.2f;
  11. const float HandleSliderLine::SPHERE_RADIUS = 0.5f;
  12. HandleSliderLine::HandleSliderLine(const Vector3& direction, float length, float snapValue, bool fixedScale)
  13. :HandleSlider(fixedScale, snapValue), 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 * 2);
  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::update(const HCamera& camera, const Vector2I& pointerPos, const Ray& ray)
  53. {
  54. assert(getState() == State::Active);
  55. mLastPointerPos = mCurPointerPos;
  56. mCurPointerPos = pointerPos;
  57. mDelta = calcDelta(camera, getPosition(), mDirection, mLastPointerPos, mCurPointerPos);
  58. }
  59. void HandleSliderLine::reset()
  60. {
  61. mDelta = 0.0f;
  62. }
  63. Vector3 HandleSliderLine::getNewPosition() const
  64. {
  65. return getPosition() + mDirection * mDelta;
  66. }
  67. }