BsHandleSliderLine.cpp 2.5 KB

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