wangguan
2020-11-17 3da3d10bfdd30a1ad7f8c48ab9fd7e7745e3d053
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
using TowerDefense.Towers;
using UnityEngine;
 
namespace TowerDefense.UI.HUD
{
    /// <summary>
    /// A class that controls the information display 
    /// whilst dragging the ghost tower
    /// </summary>
    [RequireComponent(typeof(TowerUI))]
    public class BuildInfoUI : MonoBehaviour
    {
        /// <summary>
        /// an enum for easily keeping track of UI animation
        /// </summary>
        public enum AnimationState
        {
            /// <summary>
            /// The UI is completely hidden
            /// </summary>
            Hidden,
            
            /// <summary>
            /// The UI is animation to be shown
            /// </summary>
            Showing,
            
            /// <summary>
            /// the UI is completely shown
            /// </summary>
            Shown,
            
            /// <summary>
            /// The UI is animating 
            /// </summary>
            Hiding
        }
        
        /// <summary>
        /// The attached animator
        /// </summary>
        public Animation anim;
 
        /// <summary>
        /// The name of the clip that shows the UI
        /// </summary>
        public string showClipName = "Show";
 
        /// <summary>
        /// The name of the clip that hides the UI
        /// </summary>
        public string hideClipName = "Hide";
 
        /// <summary>
        /// The attached <see cref="TowerUI"/>
        /// </summary>
        protected TowerUI m_TowerUI;
 
        /// <summary>
        /// The attached canvas
        /// </summary>
        protected Canvas m_Canvas;
 
        /// <summary>
        /// Tracks the animation of the UI
        /// </summary>
        AnimationState m_State;
 
        /// <summary>
        /// NOTE: Plays from Show animation clip event
        /// Fires at the end of the show animation
        /// Sets <see cref="m_State"/> to Show
        /// </summary>
        public void ShowEnd()
        {
            m_State = AnimationState.Shown;
        }
 
        /// <summary>
        /// NOTE: Plays from Hide animation clip event
        /// Fires at the end of the hide animation
        /// Sets <see cref="m_State"/> to Hidden
        /// </summary>
        public void HideEnd()
        {
            m_State = AnimationState.Hidden;
        }
 
        /// <summary>
        /// Shows the information
        /// </summary>
        /// <param name="controller">
        /// The tower information to display
        /// </param>
        public virtual void Show(Tower controller)
        {
            m_TowerUI.Show(controller);
            if (m_State == AnimationState.Shown)
            {
                return;
            }
            anim.Play(showClipName);
            if (m_State == AnimationState.Hiding)
            {
                anim[showClipName].normalizedTime = 1;
                m_State = AnimationState.Shown;
                return;
            }
            m_State = anim[showClipName].normalizedTime < 1 ? AnimationState.Showing : 
                AnimationState.Shown;
        }
 
        /// <summary>
        /// Hides the information
        /// </summary>
        public virtual void Hide()
        {
            if (m_State == AnimationState.Hidden)
            {
                return;
            }
            m_TowerUI.Hide();
            anim.Play(hideClipName);
            m_State = anim[hideClipName].normalizedTime < 1 ? AnimationState.Hiding : 
                AnimationState.Hidden;
        }
 
        /// <summary>
        /// Cache the attached Canvas and the attached TowerControllerUI
        /// </summary>
        protected virtual void Awake()
        {
            m_Canvas = GetComponent<Canvas>();
            m_TowerUI = GetComponent<TowerUI>();
        }
    }
}