Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. // (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.
  2.  
  3. using UnityEngine;
  4.  
  5. namespace HutongGames.PlayMaker.Actions
  6. {
  7.     // TODO: fairly basic right now
  8.     // should have more options and be more robust, e.g., other fingers.
  9.    
  10.     [ActionCategory(ActionCategory.Device)]
  11.     [Tooltip("Sends an event when a swipe is detected.")]
  12.     public class SwipeGestureEvent : FsmStateAction
  13.     {
  14.         [Tooltip("How far a touch has to travel to be considered a swipe. Uses normalized distance (e.g. 1 = 1 screen diagonal distance). Should generally be a very small number.")]
  15.         public FsmFloat minSwipeDistance;
  16.        
  17.         [Tooltip("Event to send when swipe left detected.")]
  18.         public FsmEvent swipeLeftEvent;
  19.         [Tooltip("Event to send when swipe right detected.")]
  20.         public FsmEvent swipeRightEvent;
  21.         [Tooltip("Event to send when swipe up detected.")]
  22.         public FsmEvent swipeUpEvent;
  23.         [Tooltip("Event to send when swipe down detected.")]
  24.         public FsmEvent swipeDownEvent;
  25.        
  26.         // TODO
  27. /*      [UIHint(UIHint.Variable)]
  28.         [Tooltip("Store the speed of the swipe.")]
  29.         public FsmFloat getSpeed;
  30.         [UIHint(UIHint.Variable)]
  31.         [Tooltip("Store the distance the swipe traveled.")]
  32.         public FsmFloat getDistance;*/
  33.        
  34.         float screenDiagonalSize;
  35.         float minSwipeDistancePixels;
  36.         bool touchStarted;
  37.         Vector2 touchStartPos;
  38.         //float touchStartTime;
  39.        
  40.         public override void Reset()
  41.         {
  42.             minSwipeDistance = 0.1f;
  43.             swipeLeftEvent = null;
  44.             swipeRightEvent = null;
  45.             swipeUpEvent = null;
  46.             swipeDownEvent = null;
  47.         }
  48.        
  49.         public override void OnEnter()
  50.         {
  51.             screenDiagonalSize = Mathf.Sqrt(Screen.width * Screen.width + Screen.height * Screen.height);
  52.             minSwipeDistancePixels = minSwipeDistance.Value * screenDiagonalSize;
  53.         }
  54.        
  55.         public override void OnUpdate()
  56.         {
  57.             if (Input.touchCount > 0)
  58.             {
  59.                 var touch = Input.touches[0];
  60.                
  61.                 switch (touch.phase)
  62.                 {
  63.                 case TouchPhase.Began:
  64.                    
  65.                     touchStarted = true;
  66.                     touchStartPos = touch.position;
  67.                     //touchStartTime = FsmTime.RealtimeSinceStartup;
  68.                    
  69.                     break;
  70.  
  71.                 case TouchPhase.Moved:
  72.  
  73.                     if(touchStarted)
  74.                         TestForSwipeGesture(touch);
  75.  
  76.                     break;
  77.  
  78.                 case TouchPhase.Ended:
  79.                    
  80.                     touchStarted = false;
  81.                     break;
  82.                 }
  83.             }
  84.         }
  85.        
  86.         void TestForSwipeGesture(Touch touch)
  87.         {
  88.             // test min distance
  89.            
  90.             var lastPos = touch.position;
  91.             var distance = Vector2.Distance(lastPos, touchStartPos);
  92.            
  93.             if (distance > minSwipeDistancePixels)
  94.             {
  95.                 float dy = lastPos.y - touchStartPos.y;
  96.                 float dx = lastPos.x - touchStartPos.x;
  97.                
  98.                 float angle = Mathf.Rad2Deg * Mathf.Atan2(dx, dy);
  99.                
  100.                 angle = (360 + angle - 45) % 360;
  101.  
  102.                 Debug.Log (angle);
  103.                
  104.                 if (angle < 90)
  105.                 {
  106.                     Fsm.Event(swipeRightEvent);
  107.                 }
  108.                 else if (angle < 180)
  109.                 {
  110.                     Fsm.Event(swipeDownEvent);
  111.                 }
  112.                 else if (angle < 270)
  113.                 {
  114.                     Fsm.Event(swipeLeftEvent);
  115.                 }
  116.                 else
  117.                 {
  118.                     Fsm.Event(swipeUpEvent);
  119.                 }
  120.  
  121.                 touchStarted = false;
  122.             }
  123.         }
  124.            
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement