BsHandleSliderPlane.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "BsHandleSliderPlane.h"
  2. #include "BsHandleManager.h"
  3. #include "BsHandleSliderManager.h"
  4. #include "BsVector3.h"
  5. #include "BsRay.h"
  6. #include "BsPlane.h"
  7. #include "BsCamera.h"
  8. namespace BansheeEngine
  9. {
  10. HandleSliderPlane::HandleSliderPlane(const Vector3& dir1, const Vector3& dir2, float length, bool fixedScale)
  11. :HandleSlider(fixedScale), mLength(length)
  12. {
  13. mDirection1 = Vector3::normalize(dir1);
  14. mDirection2 = Vector3::normalize(dir2);
  15. float halfLength = length * 0.5f;
  16. std::array<Vector3, 2> axes = { mDirection1, mDirection2 };
  17. std::array<float, 2> extents = { halfLength, halfLength };
  18. Vector3 center = (dir1 * length + dir2 * length) * 0.5f;
  19. mCollider = Rect3(center, axes, extents);
  20. HandleSliderManager& sliderManager = HandleManager::instance().getSliderManager();
  21. sliderManager._registerSlider(this);
  22. }
  23. HandleSliderPlane::~HandleSliderPlane()
  24. {
  25. HandleSliderManager& sliderManager = HandleManager::instance().getSliderManager();
  26. sliderManager._unregisterSlider(this);
  27. }
  28. bool HandleSliderPlane::intersects(const Ray& ray, float& t) const
  29. {
  30. Ray localRay = ray;
  31. localRay.transform(getTransformInv());
  32. auto intersect = mCollider.intersects(localRay);
  33. if (intersect.first)
  34. {
  35. t = intersect.second;
  36. return true;
  37. }
  38. return false;
  39. }
  40. void HandleSliderPlane::activate(const CameraPtr& camera, const Vector2I& pointerPos)
  41. {
  42. mStartPlanePosition = getPosition();
  43. mStartClickPosition = getPositionOnPlane(camera, pointerPos);
  44. }
  45. void HandleSliderPlane::handleInput(const CameraPtr& camera, const Vector2I& inputDelta)
  46. {
  47. assert(getState() == State::Active);
  48. mCurrentPointerPos += inputDelta;
  49. Vector3 worldDir1 = getRotation().rotate(mDirection1);
  50. Vector3 worldDir2 = getRotation().rotate(mDirection2);
  51. Vector3 intersectPosition = getPositionOnPlane(camera, mCurrentPointerPos);
  52. Vector3 positionDelta = intersectPosition - mStartClickPosition;
  53. mDelta.x = positionDelta.dot(worldDir1);
  54. mDelta.y = positionDelta.dot(worldDir2);
  55. }
  56. Vector3 HandleSliderPlane::getPositionOnPlane(const CameraPtr& camera, const Vector2I& pointerPos) const
  57. {
  58. Vector3 worldDir1 = getRotation().rotate(mDirection1);
  59. Vector3 worldDir2 = getRotation().rotate(mDirection2);
  60. Vector3 normal = worldDir1.cross(worldDir2);
  61. float dot = normal.dot(camera->getForward());
  62. if (dot > 0)
  63. normal = -normal;
  64. Plane plane(normal, mStartPlanePosition);
  65. Ray clickRay = camera->screenPointToRay(pointerPos);
  66. auto intersectResult = plane.intersects(clickRay);
  67. if (intersectResult.first)
  68. return clickRay.getPoint(intersectResult.second);
  69. return mStartClickPosition;
  70. }
  71. }