Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//----------------------------------------------
//            NGUI: Next-Gen UI kit
// Copyright © 2011-2013 Tasharen Entertainment
//----------------------------------------------

using UnityEngine;

/// <summary>
/// Similar to SpringPosition, but also moves the panel's clipping. Works in local coordinates.
/// </summary>

[RequireComponent(typeof(UIPanel))]
[AddComponentMenu("NGUI/Internal/Spring Panel")]
public class SpringPanel : MonoBehaviour
{
	public Vector3 target = Vector3.zero;
	public float strength = 10f;

	public delegate void OnFinished ();
	public OnFinished onFinished;

	UIPanel mPanel;
	Transform mTrans;
	float mThreshold = 0f;
	UIScrollView mDrag;

	/// <summary>
	/// Cache the transform.
	/// </summary>

	void Start ()
	{
		mPanel = GetComponent<UIPanel>();
		mDrag = GetComponent<UIScrollView>();
		mTrans = transform;
	}

	/// <summary>
	/// Advance toward the target position.
	/// </summary>

	void Update ()
	{
	    AdvanceTowardsPosition();
	}

    /// <summary>
    /// Advance toward the target position.
    /// </summary>
    
    protected virtual void AdvanceTowardsPosition()
    {
        float delta = RealTime.deltaTime;

        if (mThreshold == 0f)
        {
            mThreshold = (target - mTrans.localPosition).magnitude * 0.005f;
            mThreshold = Mathf.Max(mThreshold, 0.00001f);
        }

        bool trigger = false;
        Vector3 before = mTrans.localPosition;
        Vector3 after = NGUIMath.SpringLerp(mTrans.localPosition, target, strength, delta);

        if (mThreshold >= Vector3.Magnitude(after - target))
        {
            after = target;
            enabled = false;
            trigger = true;
        }
        mTrans.localPosition = after;

        Vector3 offset = after - before;
        Vector4 cr = mPanel.clipRange;
        cr.x -= offset.x;
        cr.y -= offset.y;
        mPanel.clipRange = cr;

        if (mDrag != null) mDrag.UpdateScrollbars(false);
        if (trigger && onFinished != null) onFinished();
    }

	/// <summary>
	/// Start the tweening process.
	/// </summary>

	static public SpringPanel Begin (GameObject go, Vector3 pos, float strength)
	{
		SpringPanel sp = go.GetComponent<SpringPanel>();
		if (sp == null) sp = go.AddComponent<SpringPanel>();
		sp.target = pos;
		sp.strength = strength;
		sp.onFinished = null;
		sp.mThreshold = 0f;
		sp.enabled = true;
		return sp;
	}
}

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/NGUI/Scripts/Internal/SpringPanel.cs

Diff revisions: vs.
Revision Author Commited Message
83 FMMortaroli picture FMMortaroli Tue 13 May, 2014 11:32:51 +0000