Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 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.                     TestForSwipeGesture(touch);
  74.                     break;
  75.                 }
  76.             }
  77.         }
  78.        
  79.         void TestForSwipeGesture(Touch touch)
  80.         {
  81.             // test min distance
  82.            
  83.             var lastPos = touch.position;
  84.             var distance = Vector2.Distance(lastPos, touchStartPos);
  85.            
  86.             if (distance > minSwipeDistancePixels)
  87.             {
  88.                 float dy = lastPos.y - touchStartPos.y;
  89.                 float dx = lastPos.x - touchStartPos.x;
  90.                
  91.                 float angle = Mathf.Rad2Deg * Mathf.Atan2(dx, dy);
  92.                
  93.                 angle = (360 + angle - 45) % 360;
  94.  
  95.                 Debug.Log (angle);
  96.                
  97.                 if (angle < 90)
  98.                 {
  99.                     Fsm.Event(swipeRightEvent);
  100.                 }
  101.                 else if (angle < 180)
  102.                 {
  103.                     Fsm.Event(swipeDownEvent);
  104.                 }
  105.                 else if (angle < 270)
  106.                 {
  107.                     Fsm.Event(swipeLeftEvent);
  108.                 }
  109.                 else
  110.                 {
  111.                     Fsm.Event(swipeUpEvent);
  112.                 }
  113.             }
  114.         }
  115.            
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement