Exemplo n.º 1
0
    void Update()
    {
        if (_disableDrag)
        {
            return;
        }

        if (_bPressed)
        {
            Vector3 mousePose = Input.mousePosition;//cam.ScreenToWorldPoint(Input.mousePosition);//hitt.transform.position; //
            float   offsetX   = mousePose.x - _pressedMousePos.x;
            float   offsetY   = mousePose.y - _pressedMousePos.y;
            if (Mathf.Abs(offsetX) >= 10f || Mathf.Abs(offsetY) >= 10f)
            {
                if (_bDragDirReverse)
                {
                    _curCameraPos.x = _pressedCameraPos.x + offsetX * _dragSpeedVec.x / 2000f;
                    _curCameraPos.y = _pressedCameraPos.y + offsetY * _dragSpeedVec.y / 2000f;
                    _curCameraPos.z = _pressedCameraPos.z + offsetY * _dragSpeedVec.z / 2000f;
                }
                else
                {
                    _curCameraPos.x = _pressedCameraPos.x - offsetX * _dragSpeedVec.x / 2000f;
                    _curCameraPos.y = _pressedCameraPos.y - offsetX * _dragSpeedVec.y / 2000f;
                    _curCameraPos.z = _pressedCameraPos.z - offsetY * _dragSpeedVec.z / 2000f;
                }

                _curCameraPos      = ClampPoint(_curCameraPos, _minPoint, _maxPoint);
                transform.position = _curCameraPos;
//                _movePath.UpdateRotation();
                if (_onDragEvent != null)
                {
                    _onDragEvent(_curCameraPos);
                }


                if (_lastFrameMousePos != Vector3.zero)
                {
                    _deltaDrag.x = _lastFrameMousePos.x - mousePose.x;
                    _deltaDrag.y = _lastFrameMousePos.y - mousePose.y;
                    Vector2 offset = Vector2.Scale(_deltaDrag, -_scale);
                    // Adjust the momentum
                    if (_bDragDirReverse)
                    {
                        _mMomentum = Vector2.Lerp(_mMomentum, _mMomentum + offset * (0.01f * momentumAmount), 0.67f);
                    }
                    else
                    {
                        _mMomentum = Vector2.Lerp(_mMomentum, _mMomentum - offset * (0.01f * momentumAmount), 0.67f);
                    }

                    // Utils.LogSys.Log("mMomentum" + _mMomentum.ToString() + ", " + offset.ToString());
                }
                _lastFrameMousePos = mousePose;
                _bDrag             = true;
            }
            NGUIMath.SpringDampen(ref _mMomentum, 9f, RealTime.deltaTime);
        }
        else
        {
            if (_dragType == EDragType.MOMENTUM)
            {
                //惯性运动
                if (_mMomentum.magnitude > 0.01f)
                {
                    // Apply the momentum
                    Vector3 offsetPos = (Vector3)NGUIMath.SpringDampen(ref _mMomentum, 10f, RealTime.deltaTime);
                    _curCameraPos.x   += offsetPos.x * _dragSpeedVec.x / 2000f;
                    _curCameraPos.z   += offsetPos.y * _dragSpeedVec.z / 2000f;
                    _curCameraPos.y   += offsetPos.y * _dragSpeedVec.y / 2000f;
                    _curCameraPos      = ClampPoint(_curCameraPos, _minPoint, _maxPoint);
                    transform.position = _curCameraPos;
                    // _movePath.UpdateRotation();
                    if (_onDragEvent != null)
                    {
                        _onDragEvent(_curCameraPos);
                    }
                }
                else
                {
                    NGUIMath.SpringDampen(ref _mMomentum, 9f, RealTime.deltaTime);
                }
            }
        }

        if (Input.GetMouseButtonDown(0))                       //点下去
        {
            if (UtilTools.ClickInValidArea())                  //点的是有效区
            {
                if (!HasUICoverSelf() || !UtilTools.ClickUI()) //忽略UI || 没点到UI
                {
                    if (_dragType != EDragType.NONE)
                    {
                        //还在滑动
                        if (_mMomentum.magnitude > 1f)
                        {
                            _bDrag = true;
                        }
                        else
                        {
                            _bDrag = false;
                        }
                    }
                    else
                    {
                        _bDrag = false;
                    }

                    _curCameraPos      = transform.position;
                    _bPressed          = true;
                    _pressedMousePos   = Input.mousePosition;
                    _pressedCameraPos  = _curCameraPos;
                    _mMomentum         = Vector2.zero;
                    _deltaDrag         = Vector2.zero;
                    _lastFrameMousePos = Vector3.zero;
                }
            }
        }
        else if (Input.GetMouseButtonUp(0))//弹起
        {
            _bPressed = false;
        }
    }
Exemplo n.º 2
0
    void Update()
    {
        if (_disableDrag)
        {
            return;
        }

        if (_movePath == null)
        {
            return;
        }

        if (_bPressed)
        {
            Vector3 mousePose = Input.mousePosition;//cam.ScreenToWorldPoint(Input.mousePosition);//hitt.transform.position; //
            float   offsetX   = mousePose.x - _pressedMousePos.x;
            if (Mathf.Abs(offsetX) >= 10f)
            {
                if (_bDragDirReverse)
                {
                    _curCameraPos = _pressedCameraPos + offsetX / 2000f;
                }
                else
                {
                    _curCameraPos = _pressedCameraPos - offsetX / 2000f;
                }


                _curCameraPos      = Mathf.Clamp(_curCameraPos, 0f, 1f);
                transform.position = _movePath.GetPointAtTime(_curCameraPos);
                _movePath.UpdateRotation();
                if (_onDragEvent != null)
                {
                    _onDragEvent(_curCameraPos);
                }


                if (_lastFrameMousePos != Vector3.zero)
                {
                    _deltaDrag.x = _lastFrameMousePos.x - mousePose.x;
                    _deltaDrag.y = _lastFrameMousePos.y - mousePose.y;
                    Vector2 offset = Vector2.Scale(_deltaDrag, -_scale);
                    // Adjust the momentum
                    if (_bDragDirReverse)
                    {
                        _mMomentum = Vector2.Lerp(_mMomentum, _mMomentum + offset * (0.01f * momentumAmount), 0.67f);
                    }
                    else
                    {
                        _mMomentum = Vector2.Lerp(_mMomentum, _mMomentum - offset * (0.01f * momentumAmount), 0.67f);
                    }

                    // Utils.LogSys.Log("mMomentum" + _mMomentum.ToString() + ", " + offset.ToString());
                }
                _lastFrameMousePos = mousePose;
                _bDrag             = true;
            }
            NGUIMath.SpringDampen(ref _mMomentum, 9f, RealTime.deltaTime);
        }
        else
        {
            if (_dragType == EDragType.MOMENTUM)
            {
                //惯性运动
                if (_mMomentum.magnitude > 0.01f)
                {
                    // Apply the momentum
                    Vector3 offsetPos = (Vector3)NGUIMath.SpringDampen(ref _mMomentum, 10f, RealTime.deltaTime);
                    _curCameraPos     += offsetPos.x / 2000f;
                    _curCameraPos      = Mathf.Clamp(_curCameraPos, 0f, 1f);
                    transform.position = _movePath.GetPointAtTime(_curCameraPos);
                    _movePath.UpdateRotation();
                    if (_onDragEvent != null)
                    {
                        _onDragEvent(_curCameraPos);
                    }
                }
                else
                {
                    NGUIMath.SpringDampen(ref _mMomentum, 9f, RealTime.deltaTime);
                }
            }
            else if (_dragType == EDragType.PAGEVIEW)
            {
                //滑到指定点
                if (_pageViewTargetPoint != -1f)
                {
                    bool bStop = false;
                    if (_mMomentum.magnitude > 0.01f)//正在滑动
                    {
                        float distance = _pageViewTargetPoint - _curCameraPos;
                        if (Mathf.Abs(distance) > 0.01f)
                        {
                            float dir = _bDragDirReverse?-1f:1f;
                            if (Mathf.Abs(distance) < _pageViewBeginSlowDistance)
                            {
                                Vector3 offsetPos = (Vector3)NGUIMath.SpringDampen(ref _mMomentum, 10f, RealTime.deltaTime);
                                _curCameraPos -= dir * offsetPos.x / 2000f;
                            }
                            else
                            {
                                _curCameraPos -= dir * _mMomentum.x / 2000f;
                            }
                            _curCameraPos = Mathf.Clamp(_curCameraPos, 0f, 1f);
                            float newDistance = _pageViewTargetPoint - _curCameraPos;
                            if (newDistance * distance > 0f)//还没到目标
                            {
                                transform.position = _movePath.GetPointAtTime(_curCameraPos);
                                _movePath.UpdateRotation();
                            }
                            else//超过目标点了
                            {
                                bStop = true;
                            }
                        }
                        else//已接近目标
                        {
                            bStop = true;
                        }
                    }
                    else//停止滑动
                    {
                        bStop = true;
                    }

                    if (bStop)
                    {
                        _curCameraPos      = _pageViewTargetPoint;
                        transform.position = _movePath.GetPointAtTime(_curCameraPos);
                        _movePath.UpdateRotation();
                        if (_onDragEvent != null)
                        {
                            _onDragEvent(_curCameraPos);
                        }
                        _mMomentum           = Vector2.zero;
                        _pageViewTargetPoint = -1f;
                    }
                }
            }
        }

        if (Input.GetMouseButtonDown(0))                              //点下去
        {
            if (!UtilTools.ClickUI() && UtilTools.ClickInValidArea()) //没点到UI && 点的是有效区
            {
                if (_dragType != EDragType.NONE)
                {
                    //还在滑动
                    if (_mMomentum.magnitude > 1f)
                    {
                        _bDrag = true;
                    }
                    else
                    {
                        _bDrag = false;
                    }
                }
                else
                {
                    _bDrag = false;
                }

                _bPressed          = true;
                _pressedMousePos   = Input.mousePosition;
                _pressedCameraPos  = _curCameraPos;
                _mMomentum         = Vector2.zero;
                _deltaDrag         = Vector2.zero;
                _lastFrameMousePos = Vector3.zero;
            }
        }
        else if (Input.GetMouseButtonUp(0))//弹起
        {
            _bPressed = false;
            if (_dragType == EDragType.PAGEVIEW)
            {
                if (_pageViewPoints.Count == 0)
                {
                    _pageViewTargetPoint = -1;
                }
                else
                {
                    int     dir       = 1;
                    Vector3 offsetPos = (Vector3)NGUIMath.SpringDampen(ref _mMomentum, 10f, RealTime.deltaTime);
                    if (Mathf.Abs(offsetPos.x) > 1f)  //拖动时,翻到拖动方向的那一页

                    {
                        int offPage = -1;//-1表示前一页,1表示后一页
                        if (_mMomentum.x > 0f)
                        {
                            offPage = 1;
                        }
                        _pageViewTargetPoint = GetTargetPage(_curCameraPos, offPage, ref dir);
                    }
                    else//原地弹起时,靠近哪页就滚动到哪一页
                    {
                        _pageViewTargetPoint = GetTargetPage(_curCameraPos, 0, ref dir);
                    }
                    if (_pageViewTargetPoint == -1f || Mathf.Abs(_curCameraPos - _pageViewTargetPoint) <= 0.01f)
                    {
                        _mMomentum = Vector2.zero;
                    }
                    else
                    {
                        _mMomentum = new Vector2(dir * _pageMomentum, 0f);
                    }
                }
            }
            //Utils.LogSys.Log("camera path value " + _curCameraPos);
        }
    }