wangguan
2020-12-21 d9f84aaddcb55152d53eeb267ce30c5668fb7649
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
using System.Collections.Generic;
using Core.Game;
using Core.UI;
using TowerDefense.Game;
using UnityEngine;
using UnityEngine.UI;
 
namespace TowerDefense.UI
{
    /// <summary>
    /// A manager for the level select user interface
    /// </summary>
    public class LevelSelectScreen : SimpleMainMenuPage
    {
        /// <summary>
        /// The button to instantiate that 
        /// represents the level select buttons
        /// </summary>
        public LevelSelectButton selectionPrefab;
 
        /// <summary>
        /// The layout group to instantiate the buttons in
        /// </summary>
        public LayoutGroup layout;
 
        /// <summary>
        /// A buffer for the levels panel
        /// </summary>
        public Transform rightBuffer;
 
        public Button backButton;
 
        public MouseScroll mouseScroll;
 
        public Animation cameraAnimator;
 
        public string enterCameraAnim;
        
        public string exitCameraAnim;
 
        /// <summary>
        /// The reference to the list of levels to display
        /// </summary>
        protected LevelList m_LevelList;
        
        protected List<Button> m_Buttons = new List<Button>();
 
        /// <summary>
        /// Instantiate the buttons
        /// </summary>
        protected virtual void Start()
        {
            if (GameManager.instance == null)
            {
                return;
            }
 
            m_LevelList = GameManager.instance.levelList;
            if (layout == null || selectionPrefab == null || m_LevelList == null)
            {
                return;
            }
 
            int amount = m_LevelList.Count;
            for (int i = 0; i < amount; i++)
            {
                LevelSelectButton button = CreateButton(m_LevelList[i]);
                button.transform.SetParent(layout.transform);
                button.transform.localScale = Vector3.one;
                m_Buttons.Add(button.GetComponent<Button>());
            }
            if (rightBuffer != null)
            {
                rightBuffer.SetAsLastSibling();
            }
 
            for (int index = 1; index < m_Buttons.Count - 1; index++)
            {
                Button button = m_Buttons[index];
                SetUpNavigation(button, m_Buttons[index - 1], m_Buttons[index + 1]);
            }
            
 
            SetUpNavigation(m_Buttons[0], backButton, m_Buttons[1]);
            SetUpNavigation(m_Buttons[m_Buttons.Count - 1], m_Buttons[m_Buttons.Count - 2], null);
            
            mouseScroll.SetHasRightBuffer(rightBuffer != null);
        }
 
        /// <summary>
        /// Create and Initialise a Level select button based on item
        /// </summary>
        /// <param name="item">
        /// The level data
        /// </param>
        /// <returns>
        /// The initialised button
        /// </returns>
        protected LevelSelectButton CreateButton(LevelItem item)
        {
            LevelSelectButton button = Instantiate(selectionPrefab);
            button.Initialize(item, mouseScroll);
            return button;
        }
 
        /// <summary>
        /// Play camera animations
        /// </summary>
        public override void Show()
        {
            base.Show();
 
            if (cameraAnimator != null && enterCameraAnim != null)
            {
                cameraAnimator.Play(enterCameraAnim);
            }
        }
 
        /// <summary>
        /// Return camera to normal position
        /// </summary>
        public override void Hide()
        {
            base.Hide();
 
            if (cameraAnimator != null && exitCameraAnim != null)
            {
                cameraAnimator.Play(exitCameraAnim);
            }
        }
 
        /// <summary>
        /// Sets up the navigation for a selectable
        /// </summary>
        /// <param name="selectable">Selectable to set up</param>
        /// <param name="left">Select on left</param>
        /// <param name="right">Select on right</param>
        void SetUpNavigation(Selectable selectable, Selectable left, Selectable right)
        {
            Navigation navigation = selectable.navigation;
            navigation.selectOnLeft = left;
            navigation.selectOnRight = right;
            selectable.navigation = navigation;
        }
        
    }
}