Last active
December 20, 2015 19:19
-
-
Save AlexTiTanium/6182703 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections.Generic; | |
| using UnityEngine; | |
| using System.Collections; | |
| [RequireComponent(typeof(SelectLevelDataSource))] | |
| public class SelectLevelSwipe : MonoBehaviour, IGuiMove, IGuiTouch | |
| { | |
| const float SWIPE_MAX_DURATION = 0.3f; | |
| //************************************************************************** | |
| // | |
| // Public for inspector | |
| // | |
| //************************************************************************** | |
| public float height = 8f; | |
| public float kinectSpeed = 0.95f; | |
| public float kinectMaxVelocity = 0.4f; | |
| //************************************************************************** | |
| // | |
| // MonoBehaviour | |
| // | |
| //************************************************************************** | |
| void Awake() | |
| { | |
| swipeArea = gameObject.GetComponent<BoxCollider>() ?? gameObject.AddComponent<BoxCollider>(); | |
| swipeArea.isTrigger = true; | |
| selectLevelDataSource = gameObject.GetComponent<SelectLevelDataSource>(); | |
| if(selectLevelDataSource == null) { | |
| Debug.LogError("select level data source component not found"); | |
| } | |
| } | |
| //-------------------------------------------------------------------------- | |
| void Update() | |
| { | |
| if(dragInfo.Count > 0) { | |
| DragInfo info = dragInfo.Peek(); | |
| if(Time.time - info.time > SWIPE_MAX_DURATION) { | |
| dragInfo.Dequeue(); | |
| } | |
| } | |
| CalculateVisibleArea(); | |
| CalculateScrollableArea(); | |
| CalculateSwapeArea(); | |
| } | |
| //------------------------------------------------------------------------ | |
| void FixedUpdate() | |
| { | |
| velocity = velocity * kinectSpeed; | |
| if (Mathf.Abs(velocity) < 0.02f) | |
| { | |
| velocity = 0f; | |
| return; | |
| } | |
| float velocityAdd = velocity; | |
| if (Mathf.Abs(velocity) > kinectMaxVelocity) { | |
| int sign = velocity < 0 ? -1 : 1; | |
| velocityAdd = sign * kinectMaxVelocity; | |
| } | |
| Vector3 targetPosition = transformPosition + new Vector3(velocityAdd, 0f); | |
| transformPosition = AllowedTransformPosition(targetPosition); | |
| } | |
| //------------------------------------------------------------------------ | |
| void OnDrawGizmos() | |
| { | |
| Gizmos.color = Color.red; | |
| Gizmos.DrawWireCube(visibleArea.center, new Vector3(visibleArea.width, visibleArea.height)); | |
| Gizmos.color = Color.cyan; | |
| Gizmos.DrawWireCube(scrollableArea.center, new Vector3(scrollableArea.width, scrollableArea.height)); | |
| } | |
| //************************************************************************** | |
| // | |
| // Runtime Interactive | |
| // | |
| //************************************************************************** | |
| //************************************************************************** | |
| // On touch begin | |
| public void OnGuiTouchBegin(Vector3 position) | |
| { | |
| velocity = 0f; | |
| tochBeginPosition = transformPosition - TransformFromScreenPoint(position); | |
| } | |
| //************************************************************************** | |
| // On touch move | |
| public void OnGuiTouchMove(Vector3 position) | |
| { | |
| Vector3 targetPosition = tochBeginPosition + TransformFromScreenPoint(position); | |
| targetPosition = AllowedTransformPosition(targetPosition); | |
| transformPosition = targetPosition; | |
| EnqueueDragInfo(position); | |
| } | |
| //************************************************************************** | |
| // On touch end | |
| public void OnGuiTouchEnd(Vector3 currentPosition) | |
| { | |
| if(dragInfo.Count == 0) return; | |
| DragInfo firstDraginfo = dragInfo.Dequeue(); | |
| float currentTime = Time.time; | |
| float firstTime = firstDraginfo.time; | |
| Vector3 firstPosition = firstDraginfo.position; | |
| Vector3 differencePosition = currentPosition - firstPosition; | |
| float differenceTime = (currentTime - firstTime) * 1000; | |
| velocity = differencePosition.x / differenceTime; | |
| dragInfo.Clear(); | |
| } | |
| //----------------------------------------------------------------------- | |
| public void OnGuiTouchDismiss() | |
| { | |
| } | |
| //************************************************************************** | |
| // | |
| // Private | |
| // | |
| //************************************************************************** | |
| private Vector3 tochBeginPosition = Vector3.zero; | |
| private float velocity; | |
| private Queue<DragInfo> dragInfo = new Queue<DragInfo>(); | |
| private SelectLevelDataSource selectLevelDataSource; | |
| private BoxCollider swipeArea; | |
| private Rect scrollableArea; | |
| private Rect visibleArea; | |
| // Cache | |
| private float cachedCameraAspect; | |
| private Vector3 transformPosition | |
| { | |
| get { return transform.position; } | |
| set { transform.position =new Vector3(value.x, 0); } | |
| } | |
| //----------------------------------------------------------------------- | |
| private void CalculateScrollableArea() | |
| { | |
| scrollableArea = new Rect(visibleArea.x + transform.position.x, -visibleArea.height / 2f, selectLevelDataSource.GetWidth(), visibleArea.height); | |
| } | |
| //---------------------------------------------------------------------- | |
| private void CalculateVisibleArea() | |
| { | |
| if (Mathf.Approximately(cachedCameraAspect, Camera.main.aspect)) return; | |
| visibleArea = new Rect(-(Const.MENU_DEFAULT_HEIGHT * Camera.main.aspect) / 2f, -height / 2f, Const.MENU_DEFAULT_HEIGHT * Camera.main.aspect, height); | |
| cachedCameraAspect = Camera.main.aspect; | |
| } | |
| //------------------------------------------------------------------------- | |
| private void CalculateSwapeArea() | |
| { | |
| swipeArea.center = new Vector3(-transform.position.x, 0f); | |
| swipeArea.size = new Vector3(visibleArea.width, visibleArea.height, 1f); | |
| } | |
| //-------------------------------------------------------------------------- | |
| private Vector3 TransformFromScreenPoint(Vector3 screenPoint) | |
| { | |
| Vector3 viewportPoint = Camera.main.ScreenToViewportPoint(screenPoint); | |
| return new Vector3(viewportPoint.x * visibleArea.width, viewportPoint.y * visibleArea.height, 0f); | |
| } | |
| //------------------------------------------------------------------------- | |
| private Vector3 AllowedTransformPosition(Vector3 position) | |
| { | |
| Vector3 allowedPosition = position; | |
| allowedPosition = Vector3.Max(new Vector3(visibleArea.width - scrollableArea.width, 0f), allowedPosition); | |
| allowedPosition = Vector3.Min(new Vector3(0f, 0f), allowedPosition); | |
| return allowedPosition; | |
| } | |
| //-------------------------------------------------------------------------- | |
| private void EnqueueDragInfo(Vector3 position) | |
| { | |
| DragInfo info = new DragInfo(); | |
| info.position = position; | |
| info.time = Time.time; | |
| dragInfo.Enqueue(info); | |
| } | |
| //************************************************************************** | |
| // | |
| // DragInfo | |
| // | |
| //************************************************************************** | |
| private struct DragInfo | |
| { | |
| public float time; | |
| public Vector3 position; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment