BsHandleSlider.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "BsHandleSlider.h"
  2. #include "BsCamera.h"
  3. namespace BansheeEngine
  4. {
  5. HandleSlider::HandleSlider(bool fixedScale, float snapValue)
  6. :mFixedScale(fixedScale), mSnapValue(snapValue), mScale(Vector3::ONE)
  7. {
  8. mTransform.setTRS(mPosition, mRotation, mScale);
  9. }
  10. void HandleSlider::setPosition(const Vector3& position)
  11. {
  12. mPosition = position;
  13. mTransform.setTRS(mPosition, mRotation, mScale);
  14. }
  15. void HandleSlider::setRotation(const Quaternion& rotation)
  16. {
  17. mRotation = rotation;
  18. mTransform.setTRS(mPosition, mRotation, mScale);
  19. }
  20. void HandleSlider::setScale(const Vector3& scale)
  21. {
  22. mScale = scale;
  23. mTransform.setTRS(mPosition, mRotation, mScale);
  24. }
  25. void HandleSlider::registerDrag(const Vector2I& pointerPos)
  26. {
  27. assert(getState() == State::Active);
  28. mLastPointerPos = mCurPointerPos;
  29. mCurPointerPos = pointerPos;
  30. }
  31. float HandleSlider::calcDelta(const HCamera& camera, const Vector3& position, const Vector3& direction,
  32. const Vector2I& pointerStart, const Vector2I& pointerEnd)
  33. {
  34. Vector2I handleStart2D = camera->worldToScreenPoint(position);
  35. Vector2I handleEnd2D = camera->worldToScreenPoint(position + direction);
  36. Vector2I handleDir2D = handleEnd2D - handleStart2D;
  37. INT32 sqrdMag = handleDir2D.squaredLength();
  38. if (sqrdMag == 0)
  39. return 0.0f;
  40. Vector2I diffStart = pointerStart - handleStart2D;
  41. Vector2I diffEnd = pointerEnd - handleStart2D;
  42. float tStart = handleDir2D.dot(diffStart) / (float)sqrdMag;
  43. float tEnd = handleDir2D.dot(diffEnd) / (float)sqrdMag;
  44. return tEnd - tStart;
  45. }
  46. }