BsHandleSliderPlane.cpp 2.9 KB

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