Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class AnimationTimeLineItem : MonoBehaviour
- {
- public ResizeArrow leftArrow;
- public ResizeArrow rightArrow;
- public UISprite mSprite;
- public DraggableTimelineItem draggableBg;
- public AnimationItemVO ValueObject {get;set;}
- public Vector3 Postion { get{ return mTrans.position; } }
- public Transform GetTransform { get { return mTrans; } }
- Transform mTrans;
- bool mIsDragging;
- Transform mParent;
- UIRoot mRoot;
- void Start()
- {
- //Cache components
- mTrans = transform;
- //Listen to the ResizeDone event
- leftArrow.onResizeDone += OnResizeDone;
- rightArrow.onResizeDone += OnResizeDone;
- //Listen for Drag events
- leftArrow.onDragArrow += OnDrag;
- rightArrow.onDragArrow += OnDrag;
- draggableBg.onDrag += OnDragItem;
- draggableBg.onDrop += OnDropItem;
- //TODO Debug stuff; DELETE THIS
- Init( new AnimationItemVO() );
- }
- void OnDestroy()
- {
- leftArrow.onResizeDone -= OnResizeDone;
- rightArrow.onResizeDone -= OnResizeDone;
- leftArrow.onDragArrow -= OnDrag;
- rightArrow.onDragArrow -= OnDrag;
- draggableBg.onDrag -= OnDragItem;
- draggableBg.onDrop -= OnDropItem;
- }
- public void Init( AnimationItemVO vo )
- {
- ValueObject = vo;
- //Set the sprite size based on length of the clip
- mSprite.width = vo.length * Config.pixelsPerSecond;
- //Resize Collider
- BoxCollider boxCol = mSprite.collider as BoxCollider;
- boxCol.size = new Vector3( mSprite.width, mSprite.height, 0 );
- //Set the arrows
- Bounds bounds = mSprite.CalculateBounds( mTrans );
- rightArrow.transform.localPosition =
- new Vector3( bounds.max.x - rightArrow.Width, rightArrow.transform.localPosition.y, rightArrow.transform.localPosition.z);
- leftArrow.transform.localPosition =
- new Vector3( bounds.min.x + leftArrow.Width, leftArrow.transform.localPosition.y, leftArrow.transform.localPosition.z);
- }
- void OnResizeDone()
- {
- //Adjust the objects root position
- mTrans.position = mSprite.cachedTransform.position;
- mSprite.cachedTransform.localPosition =
- new Vector3( 0, mSprite.cachedTransform.localPosition.y,mSprite.cachedTransform.localPosition.z );
- //Set the arrows
- Bounds bounds = mSprite.CalculateBounds( mTrans );
- rightArrow.transform.localPosition = new Vector3( bounds.max.x - rightArrow.Width, rightArrow.transform.localPosition.y, rightArrow.transform.localPosition.z);
- leftArrow.transform.localPosition = new Vector3( bounds.min.x + leftArrow.Width, leftArrow.transform.localPosition.y, leftArrow.transform.localPosition.z);
- //Set the data
- ValueObject.length = mSprite.width / Config.pixelsPerSecond; //Update the new length
- ValueObject.playSpeed = (float)ValueObject.length / (float)ValueObject.originalLength; //Update the play speed
- ValueObject.center = Mathf.RoundToInt( mTrans.localPosition.x );
- //Update the center of the sprite
- int average = Mathf.RoundToInt( ( ValueObject.length * Config.pixelsPerSecond ) / 2f );
- ValueObject.start = ValueObject.center - average;
- ValueObject.end = ValueObject.center + average;
- }
- void OnDrag( ResizeArrow.Direction direction, int delta )
- {
- //Resize Sprite
- mSprite.width += delta;
- //Resize Collider
- BoxCollider boxCol = mSprite.collider as BoxCollider;
- boxCol.size = new Vector3( mSprite.width, mSprite.height, 0 );
- //Reposition Arrows
- Bounds bounds = mSprite.CalculateBounds( transform );
- if( direction == ResizeArrow.Direction.RIGHT )
- rightArrow.transform.localPosition = new Vector3( bounds.max.x - rightArrow.Width, rightArrow.transform.localPosition.y, rightArrow.transform.localPosition.z);
- else
- leftArrow.transform.localPosition = new Vector3( bounds.min.x + leftArrow.Width, leftArrow.transform.localPosition.y, leftArrow.transform.localPosition.z);
- }
- void OnDragItem( Vector2 delta )
- {
- if (!mIsDragging)
- {
- mIsDragging = true;
- mParent = mTrans.parent;
- mRoot = NGUITools.FindInParents<UIRoot>(mTrans.gameObject);
- if (DragDropRoot.root != null)
- mTrans.parent = DragDropRoot.root;
- Vector3 pos = mTrans.localPosition;
- pos.z = 0f;
- mTrans.localPosition = pos;
- // Inflate the depth so that the dragged item appears in front of everything else
- UIWidget[] widgets = GetComponentsInChildren<UIWidget>();
- for (int i = 0; i < widgets.Length; ++i) widgets[i].depth = widgets[i].depth + 100;
- NGUITools.MarkParentAsChanged(gameObject);
- }
- else
- {
- mTrans.localPosition += (Vector3)delta * mRoot.pixelSizeAdjustment;
- Collider col = UICamera.lastHit.collider;
- AnimationTimeLineDropArea container = (col != null) ? col.gameObject.GetComponent<AnimationTimeLineDropArea>() : null;
- if( container != null )
- {
- ValueObject.center = Mathf.RoundToInt( container.transform.InverseTransformPoint( UICamera.lastHit.point ).x );
- int average = Mathf.RoundToInt( ( ValueObject.length * Config.pixelsPerSecond ) / 2f );
- ValueObject.start = ValueObject.center - average;
- ValueObject.end = ValueObject.center + average;
- mSprite.color = container.CheckForValidPosition( ValueObject ) ? Color.white : Color.red;
- }
- else if( mSprite.color != Color.white )
- mSprite.color = Color.white;
- }
- }
- void OnDropItem()
- {
- Collider col = UICamera.lastHit.collider;
- AnimationTimeLineDropArea container = (col != null) ? col.gameObject.GetComponent<AnimationTimeLineDropArea>() : null;
- if (container != null)
- {
- // Container found -- parent this object to the container
- mTrans.parent = container.transform;
- Vector3 pos = mTrans.localPosition;
- pos.z = 0f;
- mTrans.localPosition = pos;
- container.parent.AddTimeLineItem( this );
- }
- else
- {
- //Remove from the timeline
- ComponentRoot.GetTimeLine.RemoveTimeLineItem( this );
- Destroy( gameObject );
- }
- // Restore the depth
- UIWidget[] widgets = GetComponentsInChildren<UIWidget>();
- for (int i = 0; i < widgets.Length; ++i) widgets[i].depth = widgets[i].depth - 100;
- // Make all widgets update their parents
- NGUITools.MarkParentAsChanged(gameObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement