//----------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2015 Tasharen Entertainment
//----------------------------------------------
using UnityEngine;
///
/// Similar to SpringPosition, but also moves the panel's clipping. Works in local coordinates.
///
[RequireComponent(typeof(UIPanel))]
[AddComponentMenu("NGUI/Internal/Spring Panel")]
public class SpringPanel : MonoBehaviour
{
static public SpringPanel current;
///
/// Target position to spring the panel to.
///
public Vector3 target = Vector3.zero;
///
/// Strength of the spring. The higher the value, the faster the movement.
///
public float strength = 10f;
public delegate void OnFinished ();
///
/// Delegate function to call when the operation finishes.
///
public OnFinished onFinished;
UIPanel mPanel;
Transform mTrans;
UIScrollView mDrag;
///
/// Cache the transform.
///
void Start ()
{
mPanel = GetComponent();
mDrag = GetComponent();
mTrans = transform;
}
///
/// Advance toward the target position.
///
void Update ()
{
AdvanceTowardsPosition();
}
///
/// Advance toward the target position.
///
protected virtual void AdvanceTowardsPosition ()
{
float delta = RealTime.deltaTime;
bool trigger = false;
Vector3 before = mTrans.localPosition;
Vector3 after = NGUIMath.SpringLerp(mTrans.localPosition, target, strength, delta);
if ((after - target).sqrMagnitude < 0.01f)
{
after = target;
enabled = false;
trigger = true;
}
mTrans.localPosition = after;
Vector3 offset = after - before;
Vector2 cr = mPanel.clipOffset;
cr.x -= offset.x;
cr.y -= offset.y;
mPanel.clipOffset = cr;
if (mDrag != null) mDrag.UpdateScrollbars(false);
if (trigger && onFinished != null)
{
current = this;
onFinished();
current = null;
}
}
///
/// Start the tweening process.
///
static public SpringPanel Begin (GameObject go, Vector3 pos, float strength)
{
SpringPanel sp = go.GetComponent();
if (sp == null) sp = go.AddComponent();
sp.target = pos;
sp.strength = strength;
sp.onFinished = null;
sp.enabled = true;
return sp;
}
}