wangguan
2020-10-29 33df6342bf773286a8876abbe0e43321fb7b93d2
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using Core.Utilities;
using DG.Tweening;
using MoreMountains.NiceVibrations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
/// <summary>
/// 主界面底部的Tab按钮界,确保动画正确,按钮显示正确。 
/// </summary>
public class BottomMainTab : MonoBehaviour
{
    /// <summary>
    /// 按钮列表.
    /// </summary>
    public List<Button> mainButtonList;
 
    /// <summary>
    /// 主界面的Panel列表
    /// </summary>
    public List<Image> panelList;
 
    /// <summary>
    /// 选中Tab的背景.
    /// </summary>
    public Image selectBtnBg;
 
    /// <summary>
    /// 主界面视图:
    /// </summary>
    public PageView mainPageView;
 
    /// <summary>
    /// 当前正在选择的按钮.
    /// </summary>
    protected Button currentSelectBtn;
    /// <summary>
    /// 选择按钮在队列内的索引.
    /// </summary>
    protected int selectIdx = 2;
 
    /// <summary>
    /// 几个关键的位置信息
    /// </summary>
    protected float defaultBtnWidth = 120.0f;
    protected float selectBgWidth = 240.0f;
    protected float halfScreenWidth = 360.0f;
 
    protected Timer pageViewAdjTimer;
 
 
    // Start is called before the first frame update
    void Start()
    {
        this.onButtonSelected(2);
    }
 
 
    /// <summary>
    /// 点击按钮,触发对应的震动数据
    /// </summary>
    public void onTouchButton()
    {
        MMVibrationManager.Haptic(HapticTypes.Success);
    }
 
    public void onClickShop()
    {
        onButtonSelected(0);
    }
    public void onClickHero()
    {
        onButtonSelected(1);
    }
    public void onClickCombat()
    {
        onButtonSelected(2);
    }
    public void onClickActivity()
    {
        onButtonSelected(3);
    }
    public void onClickGuild()
    {
        onButtonSelected(4);
    }
 
    /// <summary>
    /// 某一个索引的按钮被选中.
    /// </summary>
    /// <param name="idx"></param>
    protected void onButtonSelected( int idx)
    {
        selectIdx = idx;
        // 调整主Tab按钮的位置信息.
        onButtonSelectAdjPos(idx);
        // 调整每一个Tab按钮的显示信息.
        for (int ti = 0; ti < mainButtonList.Count; ti++)
        {
            if (ti == selectIdx)
            {
                ButtonSelectDisplay(mainButtonList[ti]);
            }
            else
            {
                ButtonUnselDisplay(mainButtonList[ti]);
            }
        }
 
        // 调整主Panel数据
        //onMainPanelAdjPos(selectIdx);
        mainPageView.pageTo(selectIdx);
    }
 
    /// <summary>
    /// 页面切换相关的内容
    /// </summary>
    /// <param name="index"></param>
    public void onPageSwith( int idx )
    {
        selectIdx = idx;
        // 调整主Tab按钮的位置信息.
        onButtonSelectAdjPos(idx);
        // 调整每一个Tab按钮的显示信息.
        for (int ti = 0; ti < mainButtonList.Count; ti++)
        {
            if (ti == selectIdx)
            {
                ButtonSelectDisplay(mainButtonList[ti]);
            }
            else
            {
                ButtonUnselDisplay(mainButtonList[ti]);
            }
        }
    }
 
    /// <summary>
    /// 调整每一个主Panel的位置信息.
    /// 算法描述: 
    /// 选中的idx Panel的位置是0.
    /// 向前设置每一个Panel.
    /// 向后设置每一个Panel.
    /// </summary>
    /// <param name="idx"></param>
    protected void onMainPanelAdjPos( int idx )
    {
        if ((idx < 0) || (idx >= panelList.Count) ) 
            return;
 
        RectTransform tr;
        tr = panelList[idx].GetComponent<RectTransform>();
        tr.DOKill();
        tr.DOAnchorPosX(0, 0.4f);
 
        // 向前设置.
        float xMove = 0;
        for( int ti = idx-1;ti>=0;ti --)
        {
            xMove -= 720;
            tr = panelList[ti].GetComponent<RectTransform>();
            tr.DOKill();
            tr.DOAnchorPosX(xMove, 0.4f);
        }
 
        // 向后设置.
        xMove = 0;
        for( int ti = idx + 1;ti <panelList.Count;ti ++)
        {
            xMove += 720;
            tr = panelList[ti].GetComponent<RectTransform>();
            tr.DOKill();
            tr.DOAnchorPosX(xMove, 0.4f);
        }
 
        return;
    }
 
    /// <summary>
    /// 处理某一个按钮被选中,需要重新设置位置等相关的信息
    /// 算法描述:
    /// 1:先设置所有按钮的位置信息,根据不同被选中的按钮。
    /// 2:再设置选中背景的位置信息.
    /// 3: 设置选中和非选中按钮的显示相关信息。
    /// </summary>
    /// <param name="idx"></param>
    protected void onButtonSelectAdjPos( int idx)
    {
        float posStart = defaultBtnWidth / 2.0f;
        //Vector2 tpos;
        RectTransform tr;
 
        // 设置选中按钮之前的按钮位置信息.
        for ( int ti = 0;ti<idx;ti ++)
        {
            tr = mainButtonList[ti].GetComponent<RectTransform>();
            if( tr)
            {
                /*
                tpos = tr.anchoredPosition;
                tpos.x = posStart;
                tr.anchoredPosition = tpos;*/
                tr.DOKill();
                tr.DOAnchorPosX(posStart, 0.25f);
            }
            posStart += defaultBtnWidth;
        }
 
        // 设置选中按钮的位置信息和BackGround的位置信息.
        posStart -= defaultBtnWidth;
        posStart += (defaultBtnWidth / 2.0f);
        posStart += (selectBgWidth / 2.0f);
        tr = mainButtonList[idx].GetComponent<RectTransform>();
        if( tr)
        {
            /*
            tpos = tr.anchoredPosition;
            tpos.x = posStart;
            tr.anchoredPosition = tpos;
            */
            tr.DOKill();
            tr.DOAnchorPosX(posStart, 0.25f);
        }
        tr = selectBtnBg.GetComponent<RectTransform>();
        if (tr)
        {
            tr.DOKill();
            tr.DOAnchorPosX(posStart, 0.25f);
        }
        posStart += (defaultBtnWidth / 2.0f);
        posStart += (selectBgWidth / 2.0f);
 
        // 设置选中位置按钮之后的位置信息。
        for(int ti = idx + 1;ti<mainButtonList.Count;ti ++)
        {
            tr = mainButtonList[ti].GetComponent<RectTransform>();
            if (tr)
            {
                /*
                tpos = tr.anchoredPosition;
                tpos.x = posStart;
                tr.anchoredPosition = tpos;*/
                tr.DOKill();
                tr.DOAnchorPosX(posStart, 0.25f);
            }
            posStart += defaultBtnWidth;
        }
    }
 
    protected void ButtonSelectDisplay( Button btn )
    {
        btn.image.color = new Color32(255, 255, 255, 0);
 
        
        Transform[] father = btn.GetComponentsInChildren<Transform>();
        foreach (var child in father)
        {
            Image timg;
            if ( child.name == "imgBig")
            {
                timg = child.GetComponent<Image>();
                if (timg)
                    timg.color = new Color32(255, 255, 255, 255);
            }
            else
            {
                timg = child.GetComponent<Image>();
                if (timg)
                    timg.color = new Color32(255, 255, 255, 0);
            }
        }
    }
 
    protected void ButtonUnselDisplay( Button btn)
    {
        Transform[] father = btn.GetComponentsInChildren<Transform>();
        foreach (var child in father)
        {
            Image timg;
            if (child.name == "imgBig")
            {
                timg = child.GetComponent<Image>();
                if (timg)
                    timg.color = new Color32(255, 255, 255, 0);
            }
            else
            {
                timg = child.GetComponent<Image>();
                if (timg)
                    timg.color = new Color32(255, 255, 255, 255);
            }
        }
    }
 
    // Update is called once per frame
    void Update()
    {
    }
}