Exemplo n.º 1
0
    public virtual void OnBeginDrag(PointerEventData eventData)
    {
        GcEventData pointerEventData = eventData as GcEventData;

        if (pointerEventData == null)
        {
            return;
        }

        Pose pose = pointerEventData.Pointer.EndPointWorldPose;

        // Cache the initial grab state.
        this._initialGrabOffset =
            Quaternion.Inverse(this.transform.rotation) *
            (this.transform.position - pose.position);

        this._initialGrabRotation =
            Quaternion.Inverse(pose.rotation) *
            this.transform.rotation;

        _initialWorldPose     = pose;
        _initialTransformPose = new Pose(transform.position, transform.rotation);
        _initialTransScale    = transform.localScale;

        var rigidbody = this.GetComponent <Rigidbody>();

        if (rigidbody != null)
        {
            rigidbody.isKinematic = true;
        }
        _dragCount += 1;

        // Capture pointer events.
        pointerEventData.Pointer.CapturePointer(this.gameObject);
    }
Exemplo n.º 2
0
    public virtual void OnEndDrag(PointerEventData eventData)
    {
        GcEventData pointerEventData = eventData as GcEventData;

        if (pointerEventData == null)
        {
            _isTouching = false;
            return;
        }
        _dragCount -= 1;
        // Release the pointer.
        pointerEventData.Pointer.CapturePointer(null);

        // If the grabbable object has a rigidbody component,
        // restore its original isKinematic state.
        if (_dragCount <= 0)
        {
            var rigidbody = this.GetComponent <Rigidbody>();
            if (rigidbody != null)
            {
                rigidbody.isKinematic = this._isKinematic;
            }
        }

        _isTouching = false;
    }
Exemplo n.º 3
0
    private void OnBeginTouch(PointerEventData eventData)
    {
        GcEventData pointerEventData = eventData as GcEventData;

        if (pointerEventData == null)
        {
            return;
        }
        _beginTouch0 = pointerEventData.Pointer.GetTouch(0);
        _beginTouch1 = pointerEventData.Pointer.GetTouch(1);
    }
Exemplo n.º 4
0
    public virtual void OnDrag(PointerEventData eventData)
    {
        GcEventData pointerEventData = eventData as GcEventData;

        if (pointerEventData == null)
        {
            return;
        }

        if (pointerEventData.TouchCount > 1)
        {
            if (!_isTouching)
            {
                _isTouching = true;
                OnBeginTouch(eventData);
            }

            Touch currentTouch0 = pointerEventData.Pointer.GetTouch(0);
            Touch currentTouch1 = pointerEventData.Pointer.GetTouch(1);

            // rotate
            Vector2 lastCenterPos    = (_beginTouch0.position + _beginTouch1.position) / 2f;
            Vector2 currentCenterPos = (currentTouch0.position + currentTouch1.position) / 2f;

            float angleX = 12f * TwistSensitivity.x * 360f * (currentCenterPos.y - lastCenterPos.y) / Screen.height;
            float angleY = 12f * TwistSensitivity.y * 360f * (lastCenterPos.x - currentCenterPos.x) / Screen.width;

            this.transform.rotation = Quaternion.Euler(angleX, angleY, 0f) * _initialTransformPose.rotation;

            // scale
            float   lastDistance    = Vector2.Distance(_beginTouch0.position, _beginTouch1.position);
            float   currentDistance = Vector2.Distance(currentTouch0.position, currentTouch1.position);
            float   deltaScale      = currentDistance / lastDistance;
            Vector3 newScale        = _initialTransScale * deltaScale;
            newScale = Vector3.ClampMagnitude(newScale, maxScaleMagnitude);
            if (newScale.magnitude < minScaleMagnitude)
            {
                return;
            }
            transform.localScale = newScale;
        }
        else
        {
            if (_isTouching)
            {
                return;
            }
            if (pointerEventData.ButtonId == 0)// drag
            {
                Pose pose = pointerEventData.Pointer.EndPointWorldPose;
                // Update the grab object's rotation.
                this.transform.rotation =
                    pose.rotation * this._initialGrabRotation;

                //Update the grab object's position.
                this.transform.position =
                    pose.position +
                    (this.transform.rotation * this._initialGrabOffset);
            }
            else if (pointerEventData.ButtonId == 1)// rotate
            {
                Pose pose = pointerEventData.Pointer.EndPointWorldPose;

                float angleX = TwistSensitivity.x * 360f * (pose.position.y - _initialWorldPose.position.y);
                float angleY = TwistSensitivity.y * 360f * (_initialWorldPose.position.x - pose.position.x);

                this.transform.rotation = Quaternion.Euler(angleX, angleY, 0f) * _initialTransformPose.rotation;
            }
            else if (pointerEventData.ButtonId == 2)// scale
            {
                if (maxScaleMagnitude == minScaleMagnitude)
                {
                    return;
                }

                Pose pose = pointerEventData.Pointer.EndPointWorldPose;

                Vector3 newScale = _initialTransScale + _initialTransScale * (_initialWorldPose.position.z - pose.position.z);
                newScale = Vector3.ClampMagnitude(newScale, maxScaleMagnitude);
                if (newScale.magnitude < minScaleMagnitude)
                {
                    return;
                }
                if (Vector3.Dot(Vector3.Normalize(newScale), Vector3.Normalize(_initialTransScale)) < 0f)
                {
                    return;
                }
                transform.localScale = newScale;
            }
        }
    }