Example #1
0
        protected virtual void Update()
        {
            // If we require a selectable and it isn't selected, cancel scaling
            if (RequiredSelectable != null && RequiredSelectable.IsSelected == false)
            {
                return;
            }

            // Get the fingers we want to use
            var fingers = LeanTouch.GetFingers(IgnoreGuiFingers, RequiredFingerCount);

            // Calculate the scaling values based on these fingers
            var scale        = LeanGesture.GetPinchScale(fingers, WheelSensitivity);
            var screenCenter = LeanGesture.GetScreenCenter(fingers);

            // Perform the scaling
            Scale(scale, screenCenter);
        }
Example #2
0
        protected virtual void Update()
        {
            // If we require a selectable and it isn't selected, cancel rotation
            if (RequiredSelectable != null && RequiredSelectable.IsSelected == false)
            {
                return;
            }

            // Get the fingers we want to use
            var fingers = LeanTouch.GetFingers(IgnoreGuiFingers, RequiredFingerCount);

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

            // Perform the rotation
            Rotate(center, degrees);
        }
Example #3
0
        public void Apply(Vector2 screenDelta)
        {
            // Make sure the camera exists
            var camera = LeanTouch.GetCamera(Camera, gameObject);

            if (camera != null)
            {
                var oldPoint    = transform.position;
                var screenPoint = camera.WorldToScreenPoint(oldPoint);

                screenPoint.x += screenDelta.x;
                screenPoint.y += screenDelta.y;

                var newPoint = camera.ScreenToWorldPoint(screenPoint);

                ApplyBetween(oldPoint, newPoint);
            }
        }
Example #4
0
        protected virtual void Update()
        {
            var factor = LeanTouch.GetDampenFactor(Dampening, Time.deltaTime);

            if (AnchoredPosition == true)
            {
                var target = Toggled == true ? AnchoredPositionToggled : AnchoredPositionDefault;

                cachedRectTransform.anchoredPosition = Vector2.Lerp(cachedRectTransform.anchoredPosition, target, factor);
            }

            if (Pivot == true)
            {
                var target = Toggled == true ? PivotToggled : PivotDefault;

                cachedRectTransform.pivot = Vector2.Lerp(cachedRectTransform.pivot, target, factor);
            }
        }
        protected virtual void Translate(Vector2 screenDelta)
        {
            // Make sure the camera exists
            var camera = LeanTouch.GetCamera(Camera, gameObject);

            if (camera != null)
            {
                // Screen position of the transform
                var screenPoint = camera.WorldToScreenPoint(transform.position);

                // Add the deltaPosition
                screenPoint += (Vector3)screenDelta;
                Vector3 originalPos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
                // Convert back to world space
                transform.position = camera.ScreenToWorldPoint(screenPoint);
                transform.position = new Vector3(originalPos.x, transform.position.y, originalPos.z);
            }
        }
Example #6
0
        protected virtual void Translate(float pinchScale, Vector2 screenCenter)
        {
            // Make sure the camera exists
            var camera = LeanTouch.GetCamera(Camera, gameObject);

            if (camera != null)
            {
                // Screen position of the transform
                var screenPosition = camera.WorldToScreenPoint(transform.position);

                // Push the screen position away from the reference point based on the scale
                screenPosition.x = screenCenter.x + (screenPosition.x - screenCenter.x) * pinchScale;
                screenPosition.y = screenCenter.y + (screenPosition.y - screenCenter.y) * pinchScale;

                // Convert back to world space
                transform.position = camera.ScreenToWorldPoint(screenPosition);
            }
        }
Example #7
0
        private void Scale(float pinchScale, Vector2 screenCenter, RaycastHit hit)
        {
            // Make sure the scale is valid
            if (pinchScale > 0.0f)
            {
                var scale = hit.transform.localScale;

                if (Relative == true)
                {
                    // Make sure the camera exists
                    var camera = LeanTouch.GetCamera(Camera, gameObject);

                    if (camera != null)
                    {
                        // Screen position of the transform
                        var screenPosition = camera.WorldToScreenPoint(hit.transform.position);

                        // Push the screen position away from the reference point based on the scale
                        screenPosition.x = screenCenter.x + (screenPosition.x - screenCenter.x) * pinchScale;
                        screenPosition.y = screenCenter.y + (screenPosition.y - screenCenter.y) * pinchScale;

                        // Convert back to world space
                        hit.transform.position = camera.ScreenToWorldPoint(screenPosition);

                        // Grow the local scale by scale
                        scale *= pinchScale;
                    }
                }
                else
                {
                    // Grow the local scale by scale
                    scale *= pinchScale;
                }

                if (ScaleClamp == true)
                {
                    scale.x = Mathf.Clamp(scale.x, ScaleMin.x, ScaleMax.x);
                    scale.y = Mathf.Clamp(scale.y, ScaleMin.y, ScaleMax.y);
                    scale.z = Mathf.Clamp(scale.z, ScaleMin.z, ScaleMax.z);
                }

                hit.transform.localScale = scale;
            }
        }
        protected virtual void Update()
        {
            // Store
            var oldPosition = transform.localPosition;

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

            // 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 = LeanTouch.GetDampenFactor(Dampening, 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 && Dampening > 0.0f)
            {
                newRemainingTranslation = Vector3.Lerp(newRemainingTranslation, remainingTranslation, Inertia);
            }

            // Update remainingDelta with the dampened value
            remainingTranslation = newRemainingTranslation;
        }
Example #9
0
        protected virtual void Translate(Vector2 screenDelta, RaycastHit hit)
        {
            // Make sure the camera exists
            var camera = LeanTouch.GetCamera(Camera, gameObject);

            if (camera != null)
            {
                Debug.Log("Calculating new position...");
                Debug.Log(hit.transform.gameObject.name);
                // Screen position of the transform
                var screenPosition = camera.WorldToScreenPoint(hit.transform.position);

                // Add the deltaPosition
                screenPosition += (Vector3)screenDelta;

                // Convert back to world space
                hit.transform.position = camera.ScreenToWorldPoint(screenPosition);
            }
        }
Example #10
0
        private void Scale(float pinchScale, Vector2 screenCenter)
        {
            // Make sure the scale is valid
            if (pinchScale > 0.0f)
            {
                var scale = transform.localScale;

                if (Relative == true)
                {
                    // If camera is null, try and get the main camera, return true if a camera was found
                    if (LeanTouch.GetCamera(ref Camera) == true)
                    {
                        // Screen position of the transform
                        var screenPosition = Camera.WorldToScreenPoint(transform.position);

                        // Push the screen position away from the reference point based on the scale
                        screenPosition.x = screenCenter.x + (screenPosition.x - screenCenter.x) * pinchScale;
                        screenPosition.y = screenCenter.y + (screenPosition.y - screenCenter.y) * pinchScale;

                        // Convert back to world space
                        transform.position = Camera.ScreenToWorldPoint(screenPosition);

                        // Grow the local scale by scale
                        scale *= pinchScale;
                    }
                }
                else
                {
                    // Grow the local scale by scale
                    scale *= pinchScale;
                }

                if (ScaleClamp == true)
                {
                    scale.x = Mathf.Clamp(scale.x, ScaleMin.x, ScaleMax.x);
                    scale.y = Mathf.Clamp(scale.y, ScaleMin.y, ScaleMax.y);
                    scale.z = Mathf.Clamp(scale.z, ScaleMin.z, ScaleMax.z);
                }

                transform.localScale = scale;
                //transform.position = arController.itemSpawnPos;
            }
        }
        protected virtual void Update()
        {
            // Get the fingers we want to use to translate
            var fingers = LeanTouch.GetFingers(IgnoreGuiFingers, RequiredFingerCount);

            // Count positive and negative so opposite fingers cancel each other out
            var negative = 0;
            var positive = 0;
            var center   = 0;

            for (var i = 0; i < fingers.Count; i++)
            {
                var finger = fingers[i];

                if (IsCenter(finger) == true)
                {
                    center += 1;
                }
                else if (IsPositive(finger) == true)
                {
                    positive += 1;
                }
                else
                {
                    negative += 1;
                }
            }

            if (center > 0)
            {
                transform.Translate(TranslationCenter * Time.deltaTime, Space);
            }

            if (positive > negative)
            {
                transform.Translate(Translation * Time.deltaTime, Space);
            }

            if (negative > positive)
            {
                transform.Translate(-Translation * Time.deltaTime, Space);
            }
        }
Example #12
0
        private void Rotate(Vector3 center, float degrees)
        {
            if (Relative == true)
            {
                // If camera is null, try and get the main camera, return true if a camera was found
                if (LeanTouch.GetCamera(ref Camera) == true)
                {
                    // World position of the reference point
                    var worldReferencePoint = Camera.ScreenToWorldPoint(center);

                    // Rotate the transform around the world reference point
                    transform.RotateAround(worldReferencePoint, Camera.transform.forward, degrees);
                }
            }
            else
            {
                transform.rotation *= Quaternion.AngleAxis(degrees, RotateAxis);
            }
        }
Example #13
0
        protected virtual void FixedUpdate()
        {
            // Get the fingers we want to use
            var fingers = LeanSelectable.GetFingers(IgnoreStartedOverGui, IgnoreIsOverGui, RequiredFingerCount, RequiredSelectable);

            if (fingers.Count > 0)
            {
                var cachedCamera = LeanTouch.GetCamera(Camera, gameObject);

                if (cachedCamera != null)
                {
                    var targetPoint = LeanGesture.GetScreenCenter(fingers);
                    var newPosition = ScreenDepth.Convert(targetPoint, Camera, gameObject);
                    var factor      = LeanTouch.GetDampenFactor(Dampening, Time.fixedDeltaTime);

                    transform.position = Vector3.Lerp(transform.position, newPosition, factor);
                }
            }
        }
        protected virtual void TranslateUI(float pinchScale, Vector2 pinchScreenCenter)
        {
            var camera = LeanTouch.GetCamera(Camera, gameObject);

            // Screen position of the transform
            var screenPoint = RectTransformUtility.WorldToScreenPoint(camera, transform.position);

            // Push the screen position away from the reference point based on the scale
            screenPoint.x = pinchScreenCenter.x + (screenPoint.x - pinchScreenCenter.x) * pinchScale;
            screenPoint.y = pinchScreenCenter.y + (screenPoint.y - pinchScreenCenter.y) * pinchScale;

            // Convert back to world space
            var worldPoint = default(Vector3);

            if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.parent as RectTransform, screenPoint, camera, out worldPoint) == true)
            {
                transform.position = worldPoint;
            }
        }
Example #15
0
        protected virtual void LateUpdate()
        {
            if (isDragging)
            {
                // Get the fingers we want to use
                var fingers = LeanTouch.GetFingers(IgnoreStartedOverGui, IgnoreIsOverGui, RequiredFingerCount);

                // 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, Camera, gameObject);
                worldDelta *= Sensitivity;
                // Pan the camera based on the world delta
                var newPos = new Vector3(worldDelta.x, 0, worldDelta.z);
                transform.position -= newPos;
            }
        }
        private Vector3 GetStartPoint(LeanFinger finger)
        {
            // Use target position?
            if (Target != null)
            {
                return(Target.position);
            }

            // Make sure the camera exists
            var camera = LeanTouch.GetCamera(Camera, gameObject);

            if (camera != null)
            {
                // Get start and current world position of finger
                return(finger.GetStartWorldPosition(Distance, camera));
            }

            return(default(Vector3));
        }
Example #17
0
        /// <summary>This will return the recorded world of the current finger when it was at 'targetAge'.</summary>
        public Vector3 GetSnapshotWorldPosition(float targetAge, float distance, Camera camera = null)
        {
            // Make sure the camera exists
            camera = LeanTouch.GetCamera(camera);

            if (camera != null)
            {
                var screenPosition = GetSnapshotScreenPosition(targetAge);
                var point          = new Vector3(screenPosition.x, screenPosition.y, distance);

                return(camera.ScreenToWorldPoint(point));
            }
            else
            {
                Debug.LogError("Failed to find camera. Either tag your cameras MainCamera, or set one in this component.");
            }

            return(default(Vector3));
        }
Example #18
0
        protected virtual void Translate(Vector2 screenDelta)
        {
            // Make sure the camera exists
            var camera = LeanTouch.GetCamera(Camera, gameObject);

            if (camera != null)
            {
                // Screen position of the transform
                var screenPosition = camera.WorldToScreenPoint(transform.position);

                // Add the deltaPosition
                screenPosition += (Vector3)screenDelta;

                if (!collided)
                {
                    transform.position = camera.ScreenToWorldPoint(screenPosition);
                }
            }
        }
Example #19
0
        protected override void Translate(Vector2 screenDelta)
        {
            // Make sure the camera exists
            var camera = LeanTouch.GetCamera(Camera, gameObject);

            if (camera != null)
            {
                // Store old position
                var oldPosition = transform.position;
                // Screen position of the transform
                var screenPosition = camera.WorldToScreenPoint(oldPosition);
                // Add the deltaPosition
                screenPosition += (Vector3)screenDelta;
                // Convert back to world space
                var newPosition = camera.ScreenToWorldPoint(screenPosition);
                // Add to delta
                RemainingDelta += newPosition - oldPosition;
            }
        }
        protected override void UpdatePosition(float damping, float linear)
        {
            if (positionSet == true || Continuous == true)
            {
                if (destination != null)
                {
                    Position = destination.TransformPoint(DestinationOffset);
                }

                var currentPosition = transform.position;
                var targetPosition  = Position + Offset;

                if (IgnoreZ == true)
                {
                    targetPosition.z = currentPosition.z;
                }

                var direction = targetPosition - currentPosition;
                var velocity  = direction / Time.fixedDeltaTime;

                // Apply the velocity
                velocity *= LeanTouch.GetDampenFactor(damping, Time.fixedDeltaTime);
                velocity  = Vector3.MoveTowards(velocity, Vector3.zero, linear * Time.fixedDeltaTime);

                cachedRigidbody.velocity = velocity;

                /*
                 * if (Rotation == true && direction != Vector3.zero)
                 * {
                 *      var angle           = Mathf.Atan2(direction.x, direction.y) * Mathf.Rad2Deg;
                 *      var directionB      = (Vector2)transform.up;
                 *      var angleB          = Mathf.Atan2(directionB.x, directionB.y) * Mathf.Rad2Deg;
                 *      var delta           = Mathf.DeltaAngle(angle, angleB);
                 *      var angularVelocity = delta / Time.fixedDeltaTime;
                 *
                 *      angularVelocity *= LeanTouch.GetDampenFactor(RotationDamping, Time.fixedDeltaTime);
                 *
                 *      //cachedRigidbody.angularVelocity = angularVelocity;
                 * }
                 */
                fixedUpdateCalled = true;
            }
        }
Example #21
0
        private Vector3 GetPoint(Vector3 axis, Vector2 screenPoint)
        {
            // Make sure the camera exists
            var camera = LeanTouch.GetCamera(Camera, gameObject);

            if (camera != null)
            {
                var ray      = camera.ScreenPointToRay(screenPoint);
                var plane    = new Plane(axis, transform.position);
                var distance = default(float);

                if (plane.Raycast(ray, out distance) == true)
                {
                    return(ray.GetPoint(distance));
                }
            }

            return(oldPoint);
        }
        protected virtual void Update()
        {
            if (dragging == false)
            {
                // Get the current anchored position
                var anchoredPosition = TargetTransform.anchoredPosition;

                // Get t value
                var factor = LeanTouch.GetDampenFactor(Dampening, Time.deltaTime);

                // Dampen the current position toward the target
                anchoredPosition = Vector2.Lerp(anchoredPosition, Vector2.zero, factor);

                // Write updated anchored position
                TargetTransform.anchoredPosition = anchoredPosition;

                UpdateScaledValue();
            }
        }
Example #23
0
        protected virtual void LateUpdate()
        {
            // Get the fingers we want to use
            var fingers = LeanTouch.GetFingers(IgnoreGuiFingers, RequiredFingerCount);

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

            // Modify the zoom value
            Zoom *= pinchRatio;

            if (ZoomClamp == true)
            {
                Zoom = Mathf.Clamp(Zoom, ZoomMin, ZoomMax);
            }

            // Set the new zoom
            SetZoom(Zoom);
        }
Example #24
0
        protected virtual void LateUpdate()
        {
            // Make sure the camera exists
            if (LeanTouch.GetCamera(ref Camera, gameObject) == true)
            {
                // Get the fingers we want to use
                var fingers = LeanTouch.GetFingers(IgnoreGuiFingers);
                if (fingers.Count > 1)
                {
                    ChangeToolOnHand(true);
                }

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

                // Modify the zoom value
                Zoom *= pinchRatio;


                Zoom = Mathf.Clamp(Zoom, ZoomMin, ZoomMax);

                // Get the world delta of all the fingers
                if (fingers.Count > 1)
                {
                    var worldDelta = LeanGesture.GetWorldDelta(fingers, 0, Camera);

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

                    if (!isSpecialBounds)
                    {
                        bounds = new Vector2(Camera.main.pixelWidth / 100, Camera.main.pixelHeight / 100);
                    }

                    newPos.x           = Mathf.Clamp(newPos.x, -bounds.x, bounds.x);
                    newPos.y           = Mathf.Clamp(newPos.y, -bounds.y, bounds.y);
                    transform.position = newPos;
                }
                // Set the new zoom
                SetZoom(Zoom);
            }
        }
        protected virtual void FixedUpdate()
        {
            // Get the fingers we want to use
            var fingers = LeanSelectable.GetFingers(IgnoreStartedOverGui, IgnoreIsOverGui, RequiredFingerCount, RequiredSelectable);

            if (fingers.Count > 0)
            {
                var camera = LeanTouch.GetCamera(Camera, gameObject);

                if (camera != null)
                {
                    var oldPosition = transform.position;
                    var screenPoint = camera.WorldToScreenPoint(oldPosition);
                    var targetPoint = LeanGesture.GetScreenCenter(fingers);
                    var newPosition = camera.ScreenToWorldPoint(new Vector3(targetPoint.x, targetPoint.y, screenPoint.z));
                    var direction   = (Vector2)(newPosition - oldPosition);
                    var velocity    = direction / Time.fixedDeltaTime;

                    // Apply the velocity
                    velocity *= LeanTouch.GetDampenFactor(Dampening, Time.fixedDeltaTime);

                    cachedRigidbody.velocity = velocity;

                    if (Rotation == true && direction != Vector2.zero)
                    {
                        var angle           = Mathf.Atan2(direction.x, direction.y) * Mathf.Rad2Deg;
                        var directionB      = (Vector2)transform.up;
                        var angleB          = Mathf.Atan2(directionB.x, directionB.y) * Mathf.Rad2Deg;
                        var delta           = Mathf.DeltaAngle(angle, angleB);
                        var angularVelocity = delta / Time.fixedDeltaTime;

                        angularVelocity *= LeanTouch.GetDampenFactor(RotationDampening, Time.fixedDeltaTime);

                        cachedRigidbody.angularVelocity = angularVelocity;
                    }
                }
                else
                {
                    Debug.LogError("Failed to find camera. Either tag your cameras MainCamera, or set one in this component.", this);
                }
            }
        }
Example #26
0
        // This extends LeanTouch.GetFingers to incorporate a LeanSelectable requirement
        public static List <LeanFinger> GetFingers(bool ignoreIfStartedOverGui, bool ignoreIfOverGui, int requiredFingerCount = 0, LeanSelectable requiredSelectable = null)
        {
            var fingers = LeanTouch.GetFingers(ignoreIfStartedOverGui, ignoreIfOverGui, requiredFingerCount);

            if (requiredSelectable != null)
            {
                if (requiredSelectable.IsSelected == false)
                {
                    fingers.Clear();
                }
                else if (requiredSelectable.SelectingFinger != null)
                {
                    fingers.Clear();

                    fingers.Add(requiredSelectable.SelectingFinger);
                }
            }

            return(fingers);
        }
Example #27
0
        protected virtual void LateUpdate()
        {
            if (Pivot != null)
            {
                var angles = Quaternion.LookRotation(Pivot.position - transform.position, Vector3.up).eulerAngles;

                Pitch = angles.x;
                Yaw   = angles.y;
            }

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

            // Lerp the current values to the target ones
            currentPitch = Mathf.Lerp(currentPitch, Pitch, factor);
            currentYaw   = Mathf.Lerp(currentYaw, Yaw, factor);

            // Rotate to pitch and yaw values
            transform.localRotation = Quaternion.Euler(currentPitch, currentYaw, 0.0f);
        }
        public void Apply(Vector2 screenDelta, float force, float velocityClamp)
        {
            VelocityMultiplier = force;
            this.velocityClamp = velocityClamp;
            // Make sure the camera exists
            var camera = LeanTouch.GetCamera(Camera, gameObject);

            if (camera != null)
            {
                var oldPoint    = transform.position;
                var screenPoint = camera.WorldToScreenPoint(oldPoint);

                screenPoint.x += screenDelta.x;
                screenPoint.y += screenDelta.y;

                var newPoint = camera.ScreenToWorldPoint(screenPoint);

                ApplyBetween(oldPoint, newPoint);
            }
        }
Example #29
0
        protected void SetZoom(float current)
        {
            // Make sure the camera exists
            var camera = LeanTouch.GetCamera(Camera, gameObject);

            if (camera != null)
            {
                if (camera.orthographic == true)
                {
                    camera.orthographicSize = current;
                }
                else
                {
                    camera.fieldOfView = current;
                }
            }
            else
            {
                Debug.LogError("Failed to find camera. Either tag your cameras MainCamera, or set one in this component.", this);
            }
        }
        private void Rotate(Vector3 center, float degrees)
        {
            if (Relative == true)
            {
                // Make sure the camera exists
                var camera = LeanTouch.GetCamera(Camera, gameObject);

                if (camera != null)
                {
                    // World position of the reference point
                    var worldReferencePoint = camera.ScreenToWorldPoint(center);

                    // Rotate the transform around the world reference point
                    transform.RotateAround(worldReferencePoint, camera.transform.forward, degrees);
                }
            }
            else
            {
                transform.rotation *= Quaternion.AngleAxis(degrees, RotateAxis);
            }
        }