using UnityEngine; using System.Collections.Generic; using Lean.Common; namespace Lean.Touch { /// This class calculates gesture information (e.g. pinch) based on all fingers touching the screen, or a specified list of fingers. /// NOTE: This isn't a component, so it can only be used directly from C#. Many example components make use of this class so you don't have to use code though. public static class LeanGesture { /// Gets the average ScreenPosition of all. public static Vector2 GetScreenCenter() { return GetScreenCenter(LeanTouch.Fingers); } /// Gets the average ScreenPosition of the specified fingers. public static Vector2 GetScreenCenter(List fingers) { var center = default(Vector2); TryGetScreenCenter(fingers, ref center); return center; } /// Gets the average ScreenPosition of the specified fingers, if at least one exists. public static bool TryGetScreenCenter(List fingers, ref Vector2 center) { if (fingers != null) { var total = Vector2.zero; var count = 0; for (var i = fingers.Count - 1; i >= 0; i--) { var finger = fingers[i]; if (finger != null) { total += finger.ScreenPosition; count += 1; } } if (count > 0) { center = total / count; return true; } } return false; } /// Gets the last average ScreenPosition of all fingers. public static Vector2 GetLastScreenCenter() { return GetLastScreenCenter(LeanTouch.Fingers); } /// Gets the last average ScreenPosition of the specified fingers. public static Vector2 GetLastScreenCenter(List fingers) { var center = default(Vector2); TryGetLastScreenCenter(fingers, ref center); return center; } /// Gets the last average ScreenPosition of the specified fingers, if at least one exists. public static bool TryGetLastScreenCenter(List fingers, ref Vector2 center) { if (fingers != null) { var total = Vector2.zero; var count = 0; for (var i = fingers.Count - 1; i >= 0; i--) { var finger = fingers[i]; if (finger != null) { total += finger.LastScreenPosition; count += 1; } } if (count > 0) { center = total / count; return true; } } return false; } /// Gets the start average ScreenPosition of all fingers. public static Vector2 GetStartScreenCenter() { return GetStartScreenCenter(LeanTouch.Fingers); } /// Gets the start average ScreenPosition of the specified fingers. public static Vector2 GetStartScreenCenter(List fingers) { var center = default(Vector2); TryGetStartScreenCenter(fingers, ref center); return center; } /// Gets the start average ScreenPosition of the specified fingers, if at least one exists. public static bool TryGetStartScreenCenter(List fingers, ref Vector2 center) { if (fingers != null) { var total = Vector2.zero; var count = 0; for (var i = fingers.Count - 1; i >= 0; i--) { var finger = fingers[i]; if (finger != null) { total += finger.StartScreenPosition; count += 1; } } if (count > 0) { center = total / count; return true; } } return false; } /// Gets the average ScreenDelta of all fingers. public static Vector2 GetScreenDelta() { return GetScreenDelta(LeanTouch.Fingers); } /// Gets the average ScreenDelta of the specified fingers. public static Vector2 GetScreenDelta(List fingers) { var delta = default(Vector2); TryGetScreenDelta(fingers, ref delta); return delta; } /// Gets the average ScreenDelta of the specified fingers, if at least one exists. public static bool TryGetScreenDelta(List fingers, ref Vector2 delta) { if (fingers != null) { var total = Vector2.zero; var count = 0; for (var i = fingers.Count - 1; i >= 0; i--) { var finger = fingers[i]; if (finger != null) { total += finger.ScreenDelta; count += 1; } } if (count > 0) { delta = total / count; return true; } } return false; } /// This returns a resolution-independent 'GetScreenDelta' value. public static Vector2 GetScaledDelta() { return GetScreenDelta() * LeanTouch.ScalingFactor; } /// This returns a resolution-independent 'GetScreenDelta' value. public static Vector2 GetScaledDelta(List fingers) { return GetScreenDelta(fingers) * LeanTouch.ScalingFactor; } /// This returns a resolution-independent 'TryGetScreenDelta' value. public static bool TryGetScaledDelta(List fingers, ref Vector2 delta) { if (TryGetScreenDelta(fingers, ref delta) == true) { delta *= LeanTouch.ScalingFactor; return true; } return false; } /// Gets the average WorldDelta of all fingers. public static Vector3 GetWorldDelta(float distance, Camera camera = null) { return GetWorldDelta(LeanTouch.Fingers, distance, camera); } /// Gets the average WorldDelta of the specified fingers. public static Vector3 GetWorldDelta(List fingers, float distance, Camera camera = null) { var delta = default(Vector3); TryGetWorldDelta(fingers, distance, ref delta, camera); return delta; } /// Gets the average WorldDelta of the specified fingers, if at least one exists. public static bool TryGetWorldDelta(List fingers, float distance, ref Vector3 delta, Camera camera = null) { if (fingers != null) { var total = Vector3.zero; var count = 0; for (var i = fingers.Count - 1; i >= 0; i--) { var finger = fingers[i]; if (finger != null) { total += finger.GetWorldDelta(distance, camera); count += 1; } } if (count > 0) { delta = total / count; return true; } } return false; } /// Gets the average ScreenPosition distance between the fingers. public static float GetScreenDistance() { return GetScreenDistance(LeanTouch.Fingers); } /// Gets the average ScreenPosition distance between the fingers. public static float GetScreenDistance(List fingers) { var distance = default(float); var center = default(Vector2); if (TryGetScreenCenter(fingers, ref center) == true) { TryGetScreenDistance(fingers, center, ref distance); } return distance; } /// Gets the average ScreenPosition distance between the fingers. public static float GetScreenDistance(List fingers, Vector2 center) { var distance = default(float); TryGetScreenDistance(fingers, center, ref distance); return distance; } /// Gets the average ScreenPosition distance between the fingers. public static bool TryGetScreenDistance(List fingers, Vector2 center, ref float distance) { if (fingers != null) { var total = 0.0f; var count = 0; for (var i = fingers.Count - 1; i >= 0; i--) { var finger = fingers[i]; if (finger != null) { total += finger.GetScreenDistance(center); count += 1; } } if (count > 0) { distance = total / count; return true; } } return false; } /// Gets the average ScreenPosition distance * LeanTouch.ScalingFactor between the fingers. public static float GetScaledDistance() { return GetScreenDistance() * LeanTouch.ScalingFactor; } /// Gets the average ScreenPosition distance * LeanTouch.ScalingFactor between the fingers. public static float GetScaledDistance(List fingers) { return GetScreenDistance(fingers) * LeanTouch.ScalingFactor; } /// Gets the average ScreenPosition distance * LeanTouch.ScalingFactor between the fingers. public static float GetScaledDistance(List fingers, Vector2 center) { return GetScreenDistance(fingers, center) * LeanTouch.ScalingFactor; } /// Gets the average ScreenPosition distance * LeanTouch.ScalingFactor between the fingers. public static bool TryGetScaledDistance(List fingers, Vector2 center, ref float distance) { if (TryGetScreenDistance(fingers, center, ref distance) == true) { distance *= LeanTouch.ScalingFactor; return true; } return false; } /// Gets the last average ScreenPosition distance between all fingers. public static float GetLastScreenDistance() { return GetLastScreenDistance(LeanTouch.Fingers); } /// Gets the last average ScreenPosition distance between all fingers. public static float GetLastScreenDistance(List fingers) { var distance = default(float); var center = default(Vector2); if (TryGetLastScreenCenter(fingers, ref center) == true) { TryGetLastScreenDistance(fingers, center, ref distance); } return distance; } /// Gets the last average ScreenPosition distance between all fingers. public static float GetLastScreenDistance(List fingers, Vector2 center) { var distance = default(float); TryGetLastScreenDistance(fingers, center, ref distance); return distance; } /// Gets the last average ScreenPosition distance between all fingers. public static bool TryGetLastScreenDistance(List fingers, Vector2 center, ref float distance) { if (fingers != null) { var total = 0.0f; var count = 0; for (var i = fingers.Count - 1; i >= 0; i--) { var finger = fingers[i]; if (finger != null) { total += finger.GetLastScreenDistance(center); count += 1; } } if (count > 0) { distance = total / count; return true; } } return false; } /// Gets the last average ScreenPosition distance * LeanTouch.ScalingFactor between all fingers. public static float GetLastScaledDistance() { return GetLastScreenDistance() * LeanTouch.ScalingFactor; } /// Gets the last average ScreenPosition distance * LeanTouch.ScalingFactor between all fingers. public static float GetLastScaledDistance(List fingers) { return GetLastScreenDistance(fingers) * LeanTouch.ScalingFactor; } /// Gets the last average ScreenPosition distance * LeanTouch.ScalingFactor between all fingers. public static float GetLastScaledDistance(List fingers, Vector2 center) { return GetLastScreenDistance(fingers, center) * LeanTouch.ScalingFactor; } /// Gets the last average ScreenPosition distance * LeanTouch.ScalingFactor between all fingers. public static bool TryGetLastScaledDistance(List fingers, Vector2 center, ref float distance) { if (TryGetLastScreenDistance(fingers, center, ref distance) == true) { distance *= LeanTouch.ScalingFactor; return true; } return false; } /// Gets the start average ScreenPosition distance between all fingers. public static float GetStartScreenDistance() { return GetStartScreenDistance(LeanTouch.Fingers); } /// Gets the start average ScreenPosition distance between all fingers. public static float GetStartScreenDistance(List fingers) { var distance = default(float); var center = default(Vector2); if (TryGetStartScreenCenter(fingers, ref center) == true) { TryGetStartScreenDistance(fingers, center, ref distance); } return distance; } /// Gets the start average ScreenPosition distance between all fingers. public static float GetStartScreenDistance(List fingers, Vector2 center) { var distance = default(float); TryGetStartScreenDistance(fingers, center, ref distance); return distance; } /// Gets the start average ScreenPosition distance between all fingers. public static bool TryGetStartScreenDistance(List fingers, Vector2 center, ref float distance) { if (fingers != null) { var total = 0.0f; var count = 0; for (var i = fingers.Count - 1; i >= 0; i--) { var finger = fingers[i]; if (finger != null) { total += finger.GetStartScreenDistance(center); count += 1; } } if (count > 0) { distance = total / count; return true; } } return false; } /// Gets the start average ScreenPosition distance * LeanTouch.ScalingFactor between all fingers. public static float GetStartScaledDistance() { return GetStartScreenDistance() * LeanTouch.ScalingFactor; } /// Gets the start average ScreenPosition distance * LeanTouch.ScalingFactor between all fingers. public static float GetStartScaledDistance(List fingers) { return GetStartScreenDistance(fingers) * LeanTouch.ScalingFactor; } /// Gets the start average ScreenPosition distance * LeanTouch.ScalingFactor between all fingers. public static float GetStartScaledDistance(List fingers, Vector2 center) { return GetStartScreenDistance(fingers, center) * LeanTouch.ScalingFactor; } /// Gets the start average ScreenPosition distance * LeanTouch.ScalingFactor between all fingers. public static bool TryGetStartScaledDistance(List fingers, Vector2 center, ref float distance) { if (TryGetStartScreenDistance(fingers, center, ref distance) == true) { distance *= LeanTouch.ScalingFactor; return true; } return false; } /// Gets the pinch scale of the fingers. public static float GetPinchScale(float wheelSensitivity = 0.0f) { return GetPinchScale(LeanTouch.Fingers, wheelSensitivity); } /// Gets the pinch scale of the fingers. public static float GetPinchScale(List fingers, float wheelSensitivity = 0.0f) { var scale = 1.0f; var center = GetScreenCenter(fingers); var lastCenter = GetLastScreenCenter(fingers); TryGetPinchScale(fingers, center, lastCenter, ref scale, wheelSensitivity); return scale; } /// Gets the pinch scale of the fingers. public static bool TryGetPinchScale(List fingers, Vector2 center, Vector2 lastCenter, ref float scale, float wheelSensitivity = 0.0f) { var distance = GetScreenDistance(fingers, center); var lastDistance = GetLastScreenDistance(fingers, lastCenter); if (lastDistance > 0.0f) { scale = distance / lastDistance; return true; } if (wheelSensitivity != 0.0f) { var scroll = LeanInput.GetMouseWheelDelta(); if (scroll > 0.0f) { scale = 1.0f - wheelSensitivity; return true; } if (scroll < 0.0f) { scale = 1.0f + wheelSensitivity; return true; } } return false; } /// Gets the pinch ratio of the fingers (reciprocal of pinch scale). public static float GetPinchRatio(float wheelSensitivity = 0.0f) { return GetPinchRatio(LeanTouch.Fingers, wheelSensitivity); } /// Gets the pinch ratio of the fingers (reciprocal of pinch scale). public static float GetPinchRatio(List fingers, float wheelSensitivity = 0.0f) { var ratio = 1.0f; var center = GetScreenCenter(fingers); var lastCenter = GetLastScreenCenter(fingers); TryGetPinchRatio(fingers, center, lastCenter, ref ratio, wheelSensitivity); return ratio; } /// Gets the pinch ratio of the fingers (reciprocal of pinch scale). public static bool TryGetPinchRatio(List fingers, Vector2 center, Vector2 lastCenter, ref float ratio, float wheelSensitivity = 0.0f) { var distance = GetScreenDistance(fingers, center); var lastDistance = GetLastScreenDistance(fingers, lastCenter); if (distance > 0.0f) { ratio = lastDistance / distance; return true; } if (wheelSensitivity != 0.0f) { var scroll = LeanInput.GetMouseWheelDelta(); if (scroll > 0.0f) { ratio = 1.0f + wheelSensitivity; return true; } if (scroll < 0.0f) { ratio = 1.0f - wheelSensitivity; return true; } } return false; } /// Gets the average twist of the fingers in degrees. public static float GetTwistDegrees() { return GetTwistDegrees(LeanTouch.Fingers); } /// Gets the average twist of the fingers in degrees. public static float GetTwistDegrees(List fingers) { return GetTwistRadians(fingers) * Mathf.Rad2Deg; } /// Gets the average twist of the fingers in degrees. public static float GetTwistDegrees(List fingers, Vector2 center, Vector2 lastCenter) { return GetTwistRadians(fingers, center, lastCenter) * Mathf.Rad2Deg; } /// Gets the average twist of the fingers in degrees. public static bool TryGetTwistDegrees(List fingers, Vector2 center, Vector2 lastCenter, ref float degrees) { if (TryGetTwistRadians(fingers, center, lastCenter, ref degrees) == true) { degrees *= Mathf.Rad2Deg; return true; } return false; } /// Gets the average twist of the fingers in radians. public static float GetTwistRadians() { return GetTwistRadians(LeanTouch.Fingers); } /// Gets the average twist of the fingers in radians. public static float GetTwistRadians(List fingers) { var center = LeanGesture.GetScreenCenter(fingers); var lastCenter = LeanGesture.GetLastScreenCenter(fingers); return GetTwistRadians(fingers, center, lastCenter); } /// Gets the average twist of the fingers in radians. public static float GetTwistRadians(List fingers, Vector2 center, Vector2 lastCenter) { var radians = default(float); TryGetTwistRadians(fingers, center, lastCenter, ref radians); return radians; } /// Gets the average twist of the fingers in radians. public static bool TryGetTwistRadians(List fingers, Vector2 center, Vector2 lastCenter, ref float radians) { if (fingers != null) { var total = 0.0f; var count = 0; for (var i = fingers.Count - 1; i >= 0; i--) { var finger = fingers[i]; if (finger != null) { total += finger.GetDeltaRadians(center, lastCenter); count += 1; } } if (count > 0) { radians = total / count; return true; } } return false; } } }