summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/UICommon/XUIPlayTween.cs
blob: 2dd8da68c0725eba1aa2c1f3d67afeb2afcc5233 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using AnimationOrTween;
using UILib;
using UnityEngine;

public class XUIPlayTween : MonoBehaviour, IXUITweenTool
{
    private bool m_bPlayForward;
    public bool bPlayForward { get { return m_bPlayForward; } }
    public int TweenGroup { get { return m_uiPlayTween.tweenGroup; } }

    private void Awake()
    {
        m_uiPlayTween = GetComponent<UIPlayTween>();

        if (null == m_uiPlayTween)
        {
            Debug.LogError("null == m_uiPlayTween " + gameObject.name);
        }
        oneCycleFinish = new EventDelegate(OnOneCycleFinish);
    }

    [ContextMenu("Execute")]
    public void Excute()
    {
        ResetTween(true);
        PlayTween(true);
    }

    public void PlayTween(bool bForward, float duaration = -1.0f)
    {
        //if (null != m_objectToPlay)
        //    m_uiPlayTween.tweenTarget = m_objectToPlay;

        m_uiPlayTween.Play(bForward);
        m_bPlayForward = bForward;

        if (duaration > 0.0f)
        {
            m_repeatStartTime = Time.time;
            m_repeatDuaration = duaration;
            EventDelegate.Add(m_uiPlayTween.onFinished, oneCycleFinish);
        }
    }

    public void ResetTween(bool bForward)
    {
        m_uiPlayTween.Reset(bForward);
    }

    public void ResetTweenByGroup(bool bForward, int resetGroup)
    {
    	m_uiPlayTween.ResetByGroup(bForward, resetGroup);
    }

    public void ResetTweenByCurGroup(bool bForward)
    {
        m_uiPlayTween.ResetByGroup(bForward, m_uiPlayTween.tweenGroup);
    }

    public void ResetTweenExceptGroup(bool bForward, int exceptGroup)
    {
        m_uiPlayTween.ResetExceptGroup(bForward, exceptGroup);
    }

    public void StopTween()
    {
        m_uiPlayTween.Stop();
    }

    public void StopTweenByGroup(int resetGroup)
    {
        m_uiPlayTween.StopByGroup(resetGroup);
    }

    public void StopTweenExceptGroup(int exceptGroup)
    {
        m_uiPlayTween.StopExceptGroup(exceptGroup);
    }

    public void SetTargetGameObject(GameObject go)
    {
        //m_objectToPlay = go;
        m_uiPlayTween.tweenTarget = go;
    }

    public void SetPositionTweenPos(int group, Vector3 from, Vector3 to)
    {
        GameObject go = (m_uiPlayTween.tweenTarget == null) ? gameObject : m_uiPlayTween.tweenTarget;
        TweenPosition[] _TweenPosition = go.GetComponents<TweenPosition>();

        for (int i = 0; i < _TweenPosition.Length; i++)
        {
            if (_TweenPosition[i].tweenGroup == group)
            {
                _TweenPosition[i].from = from;
                _TweenPosition[i].to = to;
            }
        }
    }

    public void SetTweenGroup(int group)
    {
        m_uiPlayTween.tweenGroup = group;
    }

    public void SetTweenEnabledWhenFinish(bool enabled)
    {
        if (enabled)
        {
            m_uiPlayTween.disableWhenFinished = DisableCondition.DoNotDisable;
        }
        else
        {
            m_uiPlayTween.disableWhenFinished = DisableCondition.DisableAfterForward;
        }
    }

    public void RegisterOnFinishEventHandler(OnTweenFinishEventHandler eventHandler)
    {
        m_OnFinishHandler = eventHandler;

       // m_uiPlayTween.customFinishCallback = OnFinish;

        EventDelegate.Add(m_uiPlayTween.onFinished, OnFinish);
    }

    void OnFinish()
    {
        if (m_OnFinishHandler != null)
        {
            m_OnFinishHandler(this);
        }
    }

    // 指定tween的时间长度时,每播完一次,跑到这里,如果还有剩余时间,继续跑
    void OnOneCycleFinish()
    {
        if (Time.time - m_repeatStartTime < m_repeatDuaration)
            m_uiPlayTween.Play(m_bPlayForward);
        else
        {
            EventDelegate.Remove(m_uiPlayTween.onFinished, oneCycleFinish);
        }
    }

    private UIPlayTween m_uiPlayTween;
    private GameObject m_objectToPlay;
    private OnTweenFinishEventHandler m_OnFinishHandler = null;

    private EventDelegate oneCycleFinish;
    private float m_repeatStartTime;  // 重复播放的开始时间
    private float m_repeatDuaration;  // 重复播放的时间长度
}