Advertisement
Guest User

Untitled

a guest
Oct 11th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.05 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class AnimationTimeLineItem : MonoBehaviour
  5. {
  6.     public ResizeArrow leftArrow;
  7.     public ResizeArrow rightArrow;
  8.     public UISprite mSprite;
  9.     public DraggableTimelineItem draggableBg;
  10.    
  11.     public AnimationItemVO ValueObject {get;set;}
  12.     public Vector3 Postion { get{ return mTrans.position; } }
  13.     public Transform GetTransform { get { return mTrans; } }
  14.    
  15.     Transform mTrans;
  16.     bool mIsDragging;
  17.     Transform mParent;
  18.     UIRoot mRoot;
  19.    
  20.     void Start()
  21.     {
  22.         //Cache components
  23.         mTrans = transform;
  24.         //Listen to the ResizeDone event
  25.         leftArrow.onResizeDone  += OnResizeDone;
  26.         rightArrow.onResizeDone += OnResizeDone;
  27.         //Listen for Drag events
  28.         leftArrow.onDragArrow   += OnDrag;
  29.         rightArrow.onDragArrow  += OnDrag;
  30.         draggableBg.onDrag      += OnDragItem;
  31.         draggableBg.onDrop      += OnDropItem;
  32.        
  33.         //TODO Debug stuff; DELETE THIS
  34.         Init( new AnimationItemVO() );
  35.     }
  36.    
  37.     void OnDestroy()
  38.     {
  39.         leftArrow.onResizeDone  -= OnResizeDone;
  40.         rightArrow.onResizeDone -= OnResizeDone;
  41.         leftArrow.onDragArrow   -= OnDrag;
  42.         rightArrow.onDragArrow  -= OnDrag;
  43.         draggableBg.onDrag      -= OnDragItem;
  44.         draggableBg.onDrop      -= OnDropItem;
  45.     }
  46.    
  47.     public void Init( AnimationItemVO vo )
  48.     {
  49.         ValueObject = vo;
  50.         //Set the sprite size based on length of the clip
  51.         mSprite.width = vo.length * Config.pixelsPerSecond;
  52.         //Resize Collider
  53.         BoxCollider boxCol = mSprite.collider as BoxCollider;
  54.         boxCol.size = new Vector3( mSprite.width, mSprite.height, 0 );
  55.         //Set the arrows
  56.         Bounds bounds = mSprite.CalculateBounds( mTrans );
  57.         rightArrow.transform.localPosition =
  58.             new Vector3( bounds.max.x - rightArrow.Width, rightArrow.transform.localPosition.y, rightArrow.transform.localPosition.z);
  59.         leftArrow.transform.localPosition =
  60.             new Vector3( bounds.min.x + leftArrow.Width, leftArrow.transform.localPosition.y, leftArrow.transform.localPosition.z);
  61.     }
  62.    
  63.     void OnResizeDone()
  64.     {
  65.         //Adjust the objects root position
  66.         mTrans.position = mSprite.cachedTransform.position;
  67.         mSprite.cachedTransform.localPosition =
  68.             new Vector3( 0, mSprite.cachedTransform.localPosition.y,mSprite.cachedTransform.localPosition.z );
  69.         //Set the arrows
  70.         Bounds bounds = mSprite.CalculateBounds( mTrans );
  71.         rightArrow.transform.localPosition = new Vector3( bounds.max.x - rightArrow.Width, rightArrow.transform.localPosition.y, rightArrow.transform.localPosition.z);
  72.         leftArrow.transform.localPosition = new Vector3( bounds.min.x + leftArrow.Width, leftArrow.transform.localPosition.y, leftArrow.transform.localPosition.z);
  73.         //Set the data
  74.         ValueObject.length = mSprite.width / Config.pixelsPerSecond;                                //Update the new length
  75.         ValueObject.playSpeed = (float)ValueObject.length / (float)ValueObject.originalLength;      //Update the play speed
  76.         ValueObject.center = Mathf.RoundToInt( mTrans.localPosition.x );
  77.         //Update the center of the sprite
  78.         int average = Mathf.RoundToInt( ( ValueObject.length * Config.pixelsPerSecond ) / 2f );
  79.         ValueObject.start = ValueObject.center - average;
  80.         ValueObject.end = ValueObject.center + average;
  81.     }
  82.    
  83.     void OnDrag( ResizeArrow.Direction direction, int delta )
  84.     {
  85.         //Resize Sprite
  86.         mSprite.width += delta;
  87.         //Resize Collider
  88.         BoxCollider boxCol = mSprite.collider as BoxCollider;
  89.         boxCol.size = new Vector3( mSprite.width, mSprite.height, 0 );
  90.         //Reposition Arrows
  91.         Bounds bounds = mSprite.CalculateBounds( transform );
  92.         if( direction == ResizeArrow.Direction.RIGHT )
  93.             rightArrow.transform.localPosition = new Vector3( bounds.max.x - rightArrow.Width, rightArrow.transform.localPosition.y, rightArrow.transform.localPosition.z);
  94.         else
  95.             leftArrow.transform.localPosition = new Vector3( bounds.min.x + leftArrow.Width, leftArrow.transform.localPosition.y, leftArrow.transform.localPosition.z);
  96.     }
  97.    
  98.     void OnDragItem( Vector2 delta )
  99.     {
  100.         if (!mIsDragging)
  101.         {
  102.             mIsDragging = true;
  103.             mParent = mTrans.parent;
  104.             mRoot = NGUITools.FindInParents<UIRoot>(mTrans.gameObject);
  105.            
  106.             if (DragDropRoot.root != null)
  107.                 mTrans.parent = DragDropRoot.root;
  108.  
  109.             Vector3 pos = mTrans.localPosition;
  110.             pos.z = 0f;
  111.             mTrans.localPosition = pos;
  112.  
  113.             // Inflate the depth so that the dragged item appears in front of everything else
  114.             UIWidget[] widgets = GetComponentsInChildren<UIWidget>();
  115.             for (int i = 0; i < widgets.Length; ++i) widgets[i].depth = widgets[i].depth + 100;
  116.  
  117.             NGUITools.MarkParentAsChanged(gameObject);
  118.         }
  119.         else
  120.         {
  121.             mTrans.localPosition += (Vector3)delta * mRoot.pixelSizeAdjustment;
  122.            
  123.             Collider col = UICamera.lastHit.collider;
  124.             AnimationTimeLineDropArea container = (col != null) ? col.gameObject.GetComponent<AnimationTimeLineDropArea>() : null;
  125.            
  126.             if( container != null )
  127.             {
  128.                 ValueObject.center = Mathf.RoundToInt( container.transform.InverseTransformPoint( UICamera.lastHit.point ).x  );
  129.                 int average = Mathf.RoundToInt( ( ValueObject.length * Config.pixelsPerSecond ) / 2f );
  130.                 ValueObject.start = ValueObject.center - average;
  131.                 ValueObject.end = ValueObject.center + average;
  132.                
  133.                 mSprite.color = container.CheckForValidPosition( ValueObject ) ? Color.white : Color.red;
  134.             }
  135.             else if( mSprite.color != Color.white )
  136.                 mSprite.color = Color.white;
  137.         }
  138.     }
  139.    
  140.     void OnDropItem()
  141.     {
  142.         Collider col = UICamera.lastHit.collider;
  143.         AnimationTimeLineDropArea container = (col != null) ? col.gameObject.GetComponent<AnimationTimeLineDropArea>() : null;
  144.  
  145.         if (container != null)
  146.         {
  147.             // Container found -- parent this object to the container
  148.             mTrans.parent = container.transform;
  149.  
  150.             Vector3 pos = mTrans.localPosition;
  151.             pos.z = 0f;
  152.             mTrans.localPosition = pos;
  153.            
  154.             container.parent.AddTimeLineItem( this );
  155.         }
  156.         else
  157.         {
  158.             //Remove from the timeline
  159.             ComponentRoot.GetTimeLine.RemoveTimeLineItem( this );
  160.             Destroy( gameObject );
  161.         }
  162.        
  163.         // Restore the depth
  164.         UIWidget[] widgets = GetComponentsInChildren<UIWidget>();
  165.         for (int i = 0; i < widgets.Length; ++i) widgets[i].depth = widgets[i].depth - 100;
  166.         // Make all widgets update their parents
  167.         NGUITools.MarkParentAsChanged(gameObject);
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement