| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- #include "BsHandleSliderLine.h"
- #include "BsHandleManager.h"
- #include "BsHandleSliderManager.h"
- #include "BsCapsule.h"
- #include "BsLineSegment3.h"
- #include "BsSphere.h"
- #include "BsRay.h"
- namespace BansheeEngine
- {
- const float HandleSliderLine::CAPSULE_RADIUS = 0.05f;
- const float HandleSliderLine::SPHERE_RADIUS = 0.2f;
- HandleSliderLine::HandleSliderLine(const Vector3& direction, float length, float snapValue, bool fixedScale)
- :HandleSlider(fixedScale, snapValue), mLength(length), mDelta(0.0f), mHasLastPos(false)
- {
- mDirection = Vector3::normalize(direction);
- Vector3 start = Vector3::ZERO;
- Vector3 end = start + mDirection * length;
- Vector3 sphereCenter = start + mDirection * std::max(0.0f, length - SPHERE_RADIUS);
- mCapsuleCollider = Capsule(LineSegment3(start, end), CAPSULE_RADIUS);
- mSphereCollider = Sphere(sphereCenter, SPHERE_RADIUS);
- HandleSliderManager& sliderManager = HandleManager::instance().getSliderManager();
- sliderManager._registerSlider(this);
- }
- HandleSliderLine::~HandleSliderLine()
- {
- HandleSliderManager& sliderManager = HandleManager::instance().getSliderManager();
- sliderManager._unregisterSlider(this);
- }
- bool HandleSliderLine::intersects(const Ray& ray, float& t) const
- {
- Ray localRay = ray;
- localRay.transform(getTransformInv());
- auto capsuleIntersect = mCapsuleCollider.intersects(localRay);
- auto sphereIntersect = mSphereCollider.intersects(localRay);
- t = std::numeric_limits<float>::max();
- bool gotIntersect = false;
- if (capsuleIntersect.first)
- {
- t = capsuleIntersect.second;
- gotIntersect = true;
- }
- if (sphereIntersect.first)
- {
- if (sphereIntersect.second < t)
- {
- t = sphereIntersect.second;
- gotIntersect = true;
- }
- }
- return gotIntersect;
- }
- void HandleSliderLine::update(const HCamera& camera, const Vector2I& pointerPos, const Ray& ray)
- {
- assert(getState() == State::Active);
- mLastPointerPos = mCurPointerPos;
- mCurPointerPos = pointerPos;
- if (mHasLastPos)
- mDelta = calcDelta(camera, getPosition(), mDirection, mLastPointerPos, mCurPointerPos);
- mHasLastPos = true;
- }
- void HandleSliderLine::reset()
- {
- mDelta = 0.0f;
- mHasLastPos = false;
- }
- Vector3 HandleSliderLine::getNewPosition() const
- {
- return getPosition() + mDirection * mDelta;
- }
- }
|