Example #1
0
        protected virtual void Update()
        {
            var finalTransform = Target != null ? Target : transform;

            // Store smoothed position
            var smoothPosition = finalTransform.localPosition;

            // Snap to target
            finalTransform.localPosition += remainingDelta;

            // Store old position
            var oldPosition = finalTransform.localPosition;

            // Update to new position
            UpdateTranslation();

            // Shift delta by old new delta
            remainingDelta += finalTransform.localPosition - oldPosition;

            // Get t value
            var factor = LeanHelper.GetDampenFactor(Damping, Time.deltaTime);

            // Dampen remainingDelta
            var newDelta = Vector3.Lerp(remainingDelta, Vector3.zero, factor);

            // Shift this position by the change in delta
            finalTransform.localPosition = smoothPosition + remainingDelta - newDelta;

            // Update remainingDelta with the dampened value
            remainingDelta = newDelta;
        }
Example #2
0
        protected virtual void Update()
        {
            // Is this selected and has a selecting finger?
            if (Selectable != null && Selectable.IsSelected == true)
            {
                var finger = Selectable.SelectingFinger;

                if (finger != null)
                {
                    // Camera exists?
                    var camera = LeanHelper.GetCamera(null);

                    if (camera != null)
                    {
                        // Find the screen point of the finger using the depth of this block
                        var screenPoint = new Vector3(finger.ScreenPosition.x, finger.ScreenPosition.y, camera.WorldToScreenPoint(transform.position).z);

                        // Find the world space point under the finger
                        var worldPoint = camera.ScreenToWorldPoint(screenPoint);

                        // Find the block coordinate at this point
                        var dragX = Mathf.RoundToInt(worldPoint.x / BlockSize);
                        var dragY = Mathf.RoundToInt(worldPoint.y / BlockSize);

                        // Is this block right next to this one?
                        var distX = Mathf.Abs(X - dragX);
                        var distY = Mathf.Abs(Y - dragY);

                        if (distX + distY == 1)
                        {
                            // Swap blocks if one exists at this coordinate
                            var block = FindBlock(dragX, dragY);

                            if (block != null)
                            {
                                Swap(this, block);

                                if (DeselectOnSwap == true)
                                {
                                    Selectable.Deselect();
                                }
                            }
                        }
                    }
                }
            }

            // Smoothly move to new position
            var targetPosition = Vector3.zero;
            var factor         = LeanHelper.GetDampenFactor(Damping, Time.deltaTime);

            targetPosition.x = X * BlockSize;
            targetPosition.y = Y * BlockSize;

            transform.localPosition = Vector3.Lerp(transform.localPosition, targetPosition, factor);
        }
Example #3
0
        protected virtual void Update()
        {
            var factor   = LeanHelper.GetDampenFactor(damping, Time.deltaTime);
            var position = default(Vector3);
            var rotation = default(Vector3);

            strength = Mathf.Lerp(strength, 0.0f, factor);
            strength = Mathf.MoveTowards(strength, 0.0f, reduction * Time.deltaTime);

            position.x = Sample(ref offsetPosition.x, 29.0f) * shakePosition.x;
            position.y = Sample(ref offsetPosition.y, 31.0f) * shakePosition.y;
            position.z = Sample(ref offsetPosition.z, 37.0f) * shakePosition.z;

            rotation.x = Sample(ref offsetRotation.x, 41.0f) * shakeRotation.x;
            rotation.y = Sample(ref offsetRotation.y, 43.0f) * shakeRotation.y;
            rotation.z = Sample(ref offsetRotation.z, 47.9f) * shakeRotation.z;

            var finalTransform     = transform;
            var finalRectTransform = transform as RectTransform;

            // Rotation
            var currentRotation = finalTransform.localRotation;

            if (currentRotation != expectedRotation)
            {
                localRotation = currentRotation;
            }

            finalTransform.localRotation = expectedRotation = localRotation * Quaternion.Euler(rotation * strength * multiplier);

            // Position
            if (finalRectTransform != null)
            {
                var currentPosition = finalRectTransform.anchoredPosition3D;

                if (currentPosition != expectedPosition)
                {
                    localPosition = currentPosition;
                }

                finalRectTransform.anchoredPosition3D = expectedPosition = localPosition + position * strength * multiplier;
            }
            else
            {
                var currentPosition = finalTransform.localPosition;

                if (currentPosition != expectedPosition)
                {
                    localPosition = currentPosition;
                }

                finalTransform.localPosition = expectedPosition = localPosition + position * strength * multiplier;
            }
        }
Example #4
0
        public void ContinuouslyZoom(float direction)
        {
            var factor = LeanHelper.GetDampenFactor(Mathf.Abs(direction), Time.deltaTime);

            if (direction > 0.0f)
            {
                zoom = Mathf.Lerp(zoom, ClampMax, factor);
            }
            else if (direction <= 0.0f)
            {
                zoom = Mathf.Lerp(zoom, ClampMin, factor);
            }
        }
Example #5
0
        protected virtual void LateUpdate()
        {
            if (disableWith != null && disableWith.Dragging == true)
            {
                return;
            }

            var anchoredPosition = cachedRectTransform.anchoredPosition;
            var rect             = cachedRectTransform.rect;
            var parentSize       = ParentSize;
            var intervalX        = horizontalIntervalPixel + horizontalIntervalRect * rect.width + horizontalIntervalParent * parentSize.x;
            var intervalY        = verticalIntervalPixel + verticalIntervalRect * rect.width + verticalIntervalParent * parentSize.y;
            var oldPosition      = position;

            if (intervalX != 0.0f)
            {
                position.x = Mathf.RoundToInt((anchoredPosition.x - horizontalOffset) / intervalX);
            }

            if (intervalY != 0.0f)
            {
                position.y = Mathf.RoundToInt((anchoredPosition.y - verticalOffset) / intervalY);
            }

            if (horizontal == true)
            {
                var target = position.x * intervalX + horizontalOffset;
                var factor = LeanHelper.GetDampenFactor(horizontalSpeed, Time.deltaTime);

                anchoredPosition.x = Mathf.Lerp(anchoredPosition.x, target, factor);
            }

            if (vertical == true)
            {
                var target = position.y * intervalY + verticalOffset;
                var factor = LeanHelper.GetDampenFactor(verticalSpeed, Time.deltaTime);

                anchoredPosition.y = Mathf.Lerp(anchoredPosition.y, target, factor);
            }

            cachedRectTransform.anchoredPosition = anchoredPosition;

            if (position != oldPosition)
            {
                if (onPositionChanged != null)
                {
                    onPositionChanged.Invoke(position);
                }
            }
        }
Example #6
0
        protected virtual void Update()
        {
            // Get the fingers we want to use
            var fingers = Use.GetFingers();

            // Calculate the rotation values based on these fingers
            var twistDegrees = -LeanGesture.GetTwistDegrees(fingers);

            // Store
            var oldPosition = transform.localPosition;
            var oldRotation = transform.localRotation;

            // Rotate
            if (Relative == true)
            {
                var screenPoint = default(Vector2);

                if (LeanGesture.TryGetScreenCenter(fingers, ref screenPoint) == true)
                {
                    var worldPoint = ScreenDepth.Convert(screenPoint);

                    transform.RotateAround(worldPoint, transform.forward, twistDegrees);
                }
            }
            else
            {
                transform.Rotate(transform.forward, twistDegrees);
            }

            // Increment
            remainingTranslation += transform.localPosition - oldPosition;
            remainingRotation    *= Quaternion.Inverse(oldRotation) * transform.localRotation;

            // Get t value
            var factor = LeanHelper.GetDampenFactor(Damping, Time.deltaTime);

            // Dampen remainingDelta
            var newRemainingTranslation = Vector3.Lerp(remainingTranslation, Vector3.zero, factor);
            var newRemainingRotation    = Quaternion.Slerp(remainingRotation, Quaternion.identity, factor);

            // Shift this transform by the change in delta
            transform.localPosition = oldPosition + remainingTranslation - newRemainingTranslation;
            transform.localRotation = oldRotation * Quaternion.Inverse(newRemainingRotation) * remainingRotation;

            // Update remainingDelta with the dampened value
            remainingTranslation = newRemainingTranslation;
            remainingRotation    = newRemainingRotation;
        }
Example #7
0
        protected virtual void FixedUpdate()
        {
            // Make sure the camera exists and the targetScreenPoint is set
            if (cachedCamera != null && targetSet == true)
            {
                // Calculate required velocity to arrive in one FixedUpdate
                var oldPosition = transform.position;
                var newPosition = cachedCamera.ScreenToWorldPoint(targetScreenPoint);
                var velocity    = (newPosition - oldPosition) / Time.fixedDeltaTime;

                var factor = LeanHelper.GetDampenFactor(Damping, Time.fixedDeltaTime);

                // Apply the velocity
                cachedRigidbody.velocity = velocity * factor;
            }
        }
        protected virtual void Update()
        {
            // Store
            var oldScale = transform.localPosition;

            // Get the fingers we want to use
            var fingers = Use.UpdateAndGetFingers();

            // Calculate pinch scale, and make sure it's valid
            var pinchScale = LeanGesture.GetPinchScale(fingers);

            if (pinchScale != 1.0f)
            {
                pinchScale = Mathf.Pow(pinchScale, sensitivity);

                // Perform the translation if this is a relative scale
                if (relative == true)
                {
                    var pinchScreenCenter = LeanGesture.GetScreenCenter(fingers);

                    if (transform is RectTransform)
                    {
                        TranslateUI(pinchScale, pinchScreenCenter);
                    }
                    else
                    {
                        Translate(pinchScale, pinchScreenCenter);
                    }
                }

                transform.localScale *= pinchScale;

                remainingScale += transform.localPosition - oldScale;
            }

            // Get t value
            var factor = LeanHelper.GetDampenFactor(damping, Time.deltaTime);

            // Dampen remainingDelta
            var newRemainingScale = Vector3.Lerp(remainingScale, Vector3.zero, factor);

            // Shift this transform by the change in delta
            transform.localPosition = oldScale + remainingScale - newRemainingScale;

            // Update remainingDelta with the dampened value
            remainingScale = newRemainingScale;
        }
Example #9
0
        protected virtual void Update()
        {
            counter += Time.deltaTime;

            if (counter >= PulseInterval)
            {
                counter %= PulseInterval;

                Size += PulseSize;
            }

            var factor = LeanHelper.GetDampenFactor(Damping, Time.deltaTime);

            Size = Mathf.Lerp(Size, 1.0f, factor);

            transform.localScale = Vector3.Lerp(transform.localScale, BaseScale * Size, factor);
        }
        protected virtual void Update()
        {
            // Store
            var oldPosition = transform.localPosition;

            // Get the fingers we want to use
            var fingers = Use.UpdateAndGetFingers();

            // Calculate the screenDelta value based on these fingers
            var screenDelta = LeanGesture.GetScreenDelta(fingers);

            if (screenDelta != Vector2.zero)
            {
                // Perform the translation
                if (transform is RectTransform)
                {
                    TranslateUI(screenDelta);
                }
                else
                {
                    Translate(screenDelta);
                }
            }

            // Increment
            remainingTranslation += transform.localPosition - oldPosition;

            // Get t value
            var factor = LeanHelper.GetDampenFactor(Damping, Time.deltaTime);

            // Dampen remainingDelta
            var newRemainingTranslation = Vector3.Lerp(remainingTranslation, Vector3.zero, factor);

            // Shift this transform by the change in delta
            transform.localPosition = oldPosition + remainingTranslation - newRemainingTranslation;

            if (fingers.Count == 0 && Inertia > 0.0f && Damping > 0.0f)
            {
                newRemainingTranslation = Vector3.Lerp(newRemainingTranslation, remainingTranslation, Inertia);
            }

            // Update remainingDelta with the dampened value
            remainingTranslation = newRemainingTranslation;
        }
Example #11
0
        protected virtual void LateUpdate()
        {
            // Get the fingers we want to use
            var fingers = Use.GetFingers();

            // Get the last and current screen point of all fingers
            var lastScreenPoint = LeanGesture.GetLastScreenCenter(fingers);
            var screenPoint     = LeanGesture.GetScreenCenter(fingers);

            // Get the world delta of them after conversion
            var worldDelta = ScreenDepth.ConvertDelta(lastScreenPoint, screenPoint, gameObject);

            // Store the current position
            var oldPosition = transform.localPosition;

            // Pan the camera based on the world delta
            transform.position -= worldDelta * Sensitivity;

            // Add to remainingDelta
            remainingDelta += transform.localPosition - oldPosition;

            // Get t value
            var factor = LeanHelper.GetDampenFactor(Damping, Time.deltaTime);

            // Dampen remainingDelta
            var newRemainingDelta = Vector3.Lerp(remainingDelta, Vector3.zero, factor);

            // Shift this position by the change in delta
            transform.localPosition = oldPosition + remainingDelta - newRemainingDelta;

            if (fingers.Count == 0 && Inertia > 0.0f && Damping > 0.0f)
            {
                newRemainingDelta = Vector3.Lerp(newRemainingDelta, remainingDelta, Inertia);
            }

            // Update remainingDelta with the dampened value
            remainingDelta = newRemainingDelta;
        }
        protected virtual void Update()
        {
            // Store
            var oldPosition = transform.localPosition;
            var oldRotation = transform.localRotation;

            // Get the fingers we want to use
            var fingers = Use.GetFingers();

            // Calculate the rotation values based on these fingers
            var twistDegrees = LeanGesture.GetTwistDegrees(fingers);

            if (twistDegrees != 0.0f)
            {
                if (Relative == true)
                {
                    var twistScreenCenter = LeanGesture.GetScreenCenter(fingers);

                    if (transform is RectTransform)
                    {
                        TranslateUI(twistDegrees, twistScreenCenter);
                        RotateUI(twistDegrees);
                    }
                    else
                    {
                        Translate(twistDegrees, twistScreenCenter);
                        Rotate(twistDegrees);
                    }
                }
                else
                {
                    if (transform is RectTransform)
                    {
                        RotateUI(twistDegrees);
                    }
                    else
                    {
                        Rotate(twistDegrees);
                    }
                }
            }

            // Increment
            remainingTranslation += transform.localPosition - oldPosition;
            remainingRotation    *= Quaternion.Inverse(oldRotation) * transform.localRotation;

            // Get t value
            var factor = LeanHelper.GetDampenFactor(Damping, Time.deltaTime);

            // Dampen remainingDelta
            var newRemainingTranslation = Vector3.Lerp(remainingTranslation, Vector3.zero, factor);
            var newRemainingRotation    = Quaternion.Slerp(remainingRotation, Quaternion.identity, factor);

            // Shift this transform by the change in delta
            transform.localPosition = oldPosition + remainingTranslation - newRemainingTranslation;
            transform.localRotation = oldRotation * Quaternion.Inverse(newRemainingRotation) * remainingRotation;

            // Update remainingDelta with the dampened value
            remainingTranslation = newRemainingTranslation;
            remainingRotation    = newRemainingRotation;
        }
Example #13
0
        protected virtual void LateUpdate()
        {
            // Get the fingers we want to use
            var fingers = Use.GetFingers();

            // Get the pinch ratio of these fingers
            var pinchRatio = LeanGesture.GetPinchRatio(fingers);

            // Store
            var oldPosition = transform.localPosition;

            // Make sure the zoom value is valid
            zoom = TryClamp(zoom);

            if (pinchRatio != 1.0f)
            {
                // Store old zoom value and then modify zoom
                var oldZoom = zoom;

                zoom = TryClamp(zoom * pinchRatio);

                // Zoom relative to a point on screen?
                if (Relative == true)
                {
                    var screenPoint = default(Vector2);

                    if (LeanGesture.TryGetScreenCenter(fingers, ref screenPoint) == true)
                    {
                        // Derive actual pinchRatio from the zoom delta (it may differ with clamping)
                        pinchRatio = zoom / oldZoom;

                        var worldPoint = ScreenDepth.Convert(screenPoint);

                        transform.position = worldPoint + (transform.position - worldPoint) * pinchRatio;

                        // Increment
                        remainingTranslation += transform.localPosition - oldPosition;

                        if (IgnoreZ == true)
                        {
                            remainingTranslation.z = 0.0f;
                        }
                    }
                }
            }

            // Get t value
            var factor = LeanHelper.GetDampenFactor(Damping, Time.deltaTime);

            // Lerp the current value to the target one
            currentZoom = Mathf.Lerp(currentZoom, zoom, factor);

            // Set the new zoom
            SetZoom(currentZoom);

            // Dampen remainingDelta
            var newRemainingTranslation = Vector3.Lerp(remainingTranslation, Vector3.zero, factor);

            // Shift this transform by the change in delta
            transform.localPosition = oldPosition + remainingTranslation - newRemainingTranslation;

            // Update remainingDelta with the dampened value
            remainingTranslation = newRemainingTranslation;
        }
Example #14
0
        protected virtual void Update()
        {
            var value = Vector2.zero;

            if (pointer != null)
            {
                if (IsInteractable() == true)
                {
                    if (RectTransformUtility.ScreenPointToLocalPointInRectangle(CachedRectTransform, pointer.position, pointer.pressEventCamera, out value) == true)
                    {
                        value -= offset;

                        // Clamp value
                        if (shape == ShapeType.Box)
                        {
                            value.x = Mathf.Clamp(value.x, -size.x, size.x);
                            value.y = Mathf.Clamp(value.y, -size.y, size.y);
                        }
                        else if (shape == ShapeType.Circle)
                        {
                            if (value.sqrMagnitude > radius * radius)
                            {
                                value = value.normalized * radius;
                            }
                        }
                        else if (shape == ShapeType.CircleEdge)
                        {
                            value = value.normalized * radius;
                        }
                    }
                }
                else
                {
                    NullPointerNow();
                }
            }

            // Update scaledValue
            if (shape == ShapeType.Box)
            {
                scaledValue.x = size.x > 0.0f ? value.x / size.x : 0.0f;
                scaledValue.y = size.y > 0.0f ? value.y / size.y : 0.0f;
            }
            else if (shape == ShapeType.Circle)
            {
                scaledValue = radius > 0.0f ? value / radius : Vector2.zero;
            }
            else if (shape == ShapeType.CircleEdge)
            {
                scaledValue = value.normalized;
            }

            // Update handle position
            if (handle != null)
            {
                var anchoredPosition = handle.anchoredPosition;
                var factor           = LeanHelper.GetDampenFactor(damping, Time.deltaTime);

                if (snapWhileHeld == true && pointer != null)
                {
                    factor = 1.0f;
                }

                anchoredPosition = Vector2.Lerp(anchoredPosition, value + offset, factor);

                handle.anchoredPosition = anchoredPosition;
            }

            // Update relative position
            if (relativeToOrigin == true && relativeRect != null)
            {
                relativeRect.anchoredPosition = offset;
            }

            // Fire event
            if (onSet != null)
            {
                onSet.Invoke(ScaledValue);
            }
        }