using Core.Utilities;
using DG.Tweening;
using MoreMountains.NiceVibrations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
///
/// 主界面底部的Tab按钮界,确保动画正确,按钮显示正确。
///
public class BottomMainTab : MonoBehaviour
{
///
/// 按钮列表.
///
public List mainButtonList;
///
/// 主界面的Panel列表
///
public List panelList;
///
/// 选中Tab的背景.
///
public Image selectBtnBg;
///
/// 主界面视图:
///
public PageView mainPageView;
///
/// 当前正在选择的按钮.
///
protected Button currentSelectBtn;
///
/// 选择按钮在队列内的索引.
///
protected int selectIdx = 2;
///
/// 几个关键的位置信息
///
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);
}
///
/// 点击按钮,触发对应的震动数据
///
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);
}
///
/// 某一个索引的按钮被选中.
///
///
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);
}
///
/// 页面切换相关的内容
///
///
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]);
}
}
}
///
/// 调整每一个主Panel的位置信息.
/// 算法描述:
/// 选中的idx Panel的位置是0.
/// 向前设置每一个Panel.
/// 向后设置每一个Panel.
///
///
protected void onMainPanelAdjPos( int idx )
{
if ((idx < 0) || (idx >= panelList.Count) )
return;
RectTransform tr;
tr = panelList[idx].GetComponent();
tr.DOKill();
tr.DOAnchorPosX(0, 0.4f);
// 向前设置.
float xMove = 0;
for( int ti = idx-1;ti>=0;ti --)
{
xMove -= 720;
tr = panelList[ti].GetComponent();
tr.DOKill();
tr.DOAnchorPosX(xMove, 0.4f);
}
// 向后设置.
xMove = 0;
for( int ti = idx + 1;ti ();
tr.DOKill();
tr.DOAnchorPosX(xMove, 0.4f);
}
return;
}
///
/// 处理某一个按钮被选中,需要重新设置位置等相关的信息
/// 算法描述:
/// 1:先设置所有按钮的位置信息,根据不同被选中的按钮。
/// 2:再设置选中背景的位置信息.
/// 3: 设置选中和非选中按钮的显示相关信息。
///
///
protected void onButtonSelectAdjPos( int idx)
{
float posStart = defaultBtnWidth / 2.0f;
//Vector2 tpos;
RectTransform tr;
// 设置选中按钮之前的按钮位置信息.
for ( int ti = 0;ti();
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();
if( tr)
{
/*
tpos = tr.anchoredPosition;
tpos.x = posStart;
tr.anchoredPosition = tpos;
*/
tr.DOKill();
tr.DOAnchorPosX(posStart, 0.25f);
}
tr = selectBtnBg.GetComponent();
if (tr)
{
tr.DOKill();
tr.DOAnchorPosX(posStart, 0.25f);
}
posStart += (defaultBtnWidth / 2.0f);
posStart += (selectBgWidth / 2.0f);
// 设置选中位置按钮之后的位置信息。
for(int ti = idx + 1;ti();
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();
foreach (var child in father)
{
Image timg;
if ( child.name == "imgBig")
{
timg = child.GetComponent();
if (timg)
timg.color = new Color32(255, 255, 255, 255);
}
else
{
timg = child.GetComponent();
if (timg)
timg.color = new Color32(255, 255, 255, 0);
}
}
}
protected void ButtonUnselDisplay( Button btn)
{
Transform[] father = btn.GetComponentsInChildren();
foreach (var child in father)
{
Image timg;
if (child.name == "imgBig")
{
timg = child.GetComponent();
if (timg)
timg.color = new Color32(255, 255, 255, 0);
}
else
{
timg = child.GetComponent();
if (timg)
timg.color = new Color32(255, 255, 255, 255);
}
}
}
// Update is called once per frame
void Update()
{
}
}