shaoxiadiablo/Assets/AGame/Scripts/UI/MainWindow/MainWindow.TopWidget.cs

402 lines
12 KiB
C#
Raw Normal View History

2025-05-18 01:04:31 +08:00
// ***********************************************************************
// Assembly : Game
// Author : Kimch
// Created : 2021-04-12
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
// <copyright file= "MainWindow" company="Kunpo"></copyright>
// <summary></summary>
// ***********************************************************************
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace G.UI
{
partial class MainWindow
{
/// <summary>
///
/// </summary>
class TopWidget : KUIWidget
{
#region Field
#pragma warning disable CS0649 // 从未对字段赋值,字段将一直保持其默认值 null
[KUIFlag]
GameObject _goHead;
[KUIFlag]
TextMeshProUGUI _tmpName;
[KUIFlag]
Button _imgVip;
[KUIFlag]
TextMeshProUGUI _tmpVip;
[KUIFlag]
GameObject _imgAdVip;
[KUIFlag]
GameObject _goCombat;
[KUIFlag]
TextMeshProUGUI _tmpCombatValue_01;
[KUIFlag]
TextMeshProUGUI _tmpCombatValue_02;
[KUIFlag]
TextMeshProUGUI _tmpCombatValue_03;
[KUIFlag]
Image _imgFrame;
[KUIFlag]
GameObject _imgFrame2;
[KUIFlag]
Image _imgHead;
[KUIFlag]
Button _btnHead;
[KUIFlag]
TextMeshProUGUI _tmpGrade;
[KUIFlag]
Image _imgExp;
[KUIFlag]
GameObject _goEnergy;
[KUIFlag]
TextMeshProUGUI _tmpEnergy;
[KUIFlag]
Slider _sldEnergy;
[KUIFlag]
GameObject _goEnergyTime;
[KUIFlag]
TextMeshProUGUI _tmpEnergyTime;
[KUIFlag]
Button _btnEnergyAdd;
[KUIFlag]
GameObject _goCoin;
[KUIFlag]
TextMeshProUGUI _tmpCoin;
[KUIFlag]
Button _btnCoinAdd;
[KUIFlag]
GameObject _goGem;
[KUIFlag]
TextMeshProUGUI _tmpGem;
[KUIFlag]
Button _btnGemAdd;
[KUIFlag]
GameObject _goChip;
[KUIFlag]
TextMeshProUGUI _tmpChip;
[KUIFlag]
Button _btnChipAdd;
[KUIFlag]
GameObject _goChip2;
[KUIFlag]
TextMeshProUGUI _tmpChip2;
[KUIFlag]
Button _btnChip2Add;
[KUIFlag]
GameObject _goStone;
[KUIFlag]
TextMeshProUGUI _tmpStone;
[KUIFlag]
Button _btnStoneAdd;
[KUIFlag]
Button _btnDetails;
#pragma warning restore CS0649 // 从未对字段赋值,字段将一直保持其默认值 null
#endregion
#region Method
public void RefreshView()
{
RefreshName();
RefreshAvatar();
RefreshMoney();
RefreshCombatValue();
//RefreshGrade();
RefreshEnergy();
RefreshVip();
}
private void RefreshMoney()
{
_tmpCoin.text = GlobalUtils.GetNumberString(MoneyProxy.Instance.coin);
_tmpGem.text = GlobalUtils.GetNumberString(MoneyProxy.Instance.gem);
_tmpChip.text = GlobalUtils.GetNumberString(MoneyProxy.Instance.chip);
_tmpStone.text = GlobalUtils.GetNumberString(MoneyProxy.Instance.stone);
_tmpChip2.text = GlobalUtils.GetNumberString(MoneyProxy.Instance.GetMoney(36));
}
private void RefreshName()
{
_tmpName.text = PlayerProxy.Instance.nickName;
}
private void RefreshAvatar()
{
var headURL = PlayerProxy.Instance.headURL;
if (int.TryParse(headURL, out var avatarId))
{
var chaAvatar = ItemProxy.Instance.GetStaticItem<ItemChaAvatar>(avatarId);
if (chaAvatar != null)
IconProxy.Instance.SetSprite(_imgHead, chaAvatar.icon);
}
}
private void RefreshCombatValue()
{
_tmpCombatValue_01.text = PlayerProxy.Instance.combatValue.ToString();
_tmpCombatValue_02.text = PlayerProxy.Instance.combatValue.ToString();
_tmpCombatValue_03.text = PlayerProxy.Instance.combatValue.ToString();
}
private void RefreshVip()
{
if (FunctionProxy.Instance.GetFunctionUnlock(FunctionProxy.FunctionId.Vip))
{
_imgVip.gameObject.SetActive(true);
int vipId = VipProxy.Instance.curVip;
_tmpVip.text = vipId.ToString();
//var vipItem = ItemProxy.Instance.GetStaticItem<ItemVip>(vipId);
//if (vipItem != null)
_imgFrame2.SetActive(vipId > 1);
}
else
{
_imgVip.gameObject.SetActive(false);
_imgFrame2.SetActive(false);
}
//else
//{
// _tmpVip.text = "";
//}
if (RmbShopProxy.Instance.IsRemoveAd())
_imgAdVip.SetActive(true);
else
_imgAdVip.SetActive(false);
}
private void RefreshGrade()
{
//_tmpGrade.text = PlayerProxy.Instance.grade.ToString();
//_imgExp.fillAmount = PlayerProxy.Instance.expRatio;
}
private void RefreshEnergy()
{
if (_goEnergy.activeSelf)
{
int cur = PlayerProxy.Instance.energy;
int max = PlayerProxy.Instance.energyMax;
_sldEnergy.value = Mathf.Clamp01(cur / (float)max);
_tmpEnergy.text = $"{cur}/{max}";
var energyTime = PlayerProxy.Instance.energyRecoverTime;
if (string.IsNullOrEmpty(energyTime))
{
_goEnergyTime.SetActive(false);
}
else
{
_goEnergyTime.SetActive(true);
_tmpEnergyTime.text = energyTime;
}
}
}
public void OnPageChange(int index)
{
//_goCombat.SetActive(index != 0 && index != 4);
for (int i = 0; i < 3; i++)
{
_goCombat.transform.GetChild(i).gameObject.SetActive(i == index - 1);
}
if (index == 2)
{
_goHead.SetActive(true);
_goEnergy.SetActive(true);
}
else
{
_goHead.SetActive(false);
_goEnergy.SetActive(false);
}
_btnDetails.gameObject.SetActive(index == 3);
if (KPlatform.Instance.QueryPay())
{
_goGem.SetActive(index != 3);
_goCoin.SetActive(index != 2 && index != 3);
}
else
{
_goGem.SetActive(false);
_goCoin.SetActive(index != 3);
}
_goChip.SetActive(index == 3);
_goChip2.SetActive(index == 3);
_goStone.SetActive(index == 3);
}
/// <summary>
///
/// </summary>
private void UpdateView()
{
RefreshEnergy();
}
private void OnHeadBtnClick()
{
OpenWindow<SettingWindow>();
SoundProxy.PlayFxAsync(GlobalDefine.BUTTON_CLICK_SOUND);
}
private void OnCoinAddBtnClick()
{
AdMoneyBox.ShowAdMoney(Item.Id.kCoin, false);
SoundProxy.PlayFxAsync(GlobalDefine.BUTTON_CLICK_SOUND);
}
private void OnGemAddBtnClick()
{
if (KPlatform.Instance.QueryPay())
OpenWindow<RmbShopWindow>();
else
AdMoneyBox.ShowAdMoney(Item.Id.kGem, false);
SoundProxy.PlayFxAsync(GlobalDefine.BUTTON_CLICK_SOUND);
}
private void OnChipAddBtnClick()
{
AdMoneyBox.ShowAdMoney(Item.Id.kChip, false);
SoundProxy.PlayFxAsync(GlobalDefine.BUTTON_CLICK_SOUND);
}
private void OnEnergyAddBtnClick()
{
AdMoneyBox.ShowAdMoney(Item.Id.kEnergy, false);
SoundProxy.PlayFxAsync(GlobalDefine.BUTTON_CLICK_SOUND);
}
private void OnDetailBtnClick()
{
KUIWindow.OpenWindow<ChaDetailBox>();
SoundProxy.PlayFxAsync(GlobalDefine.BUTTON_CLICK_SOUND);
}
private void OnVipBtnClick()
{
OpenWindow<VipWindow>();
SoundProxy.PlayFxAsync(GlobalDefine.BUTTON_CLICK_SOUND);
}
#endregion
#region Unity
/// <summary>
///
/// </summary>
private void Awake()
{
SetViewData();
_btnCoinAdd.onClick.AddListener(this.OnCoinAddBtnClick);
_btnGemAdd.onClick.AddListener(this.OnGemAddBtnClick);
_btnChipAdd.onClick.AddListener(this.OnChipAddBtnClick);
_btnEnergyAdd.onClick.AddListener(this.OnEnergyAddBtnClick);
_btnHead.onClick.AddListener(this.OnHeadBtnClick);
_btnDetails.onClick.AddListener(this.OnDetailBtnClick);
_imgVip.onClick.AddListener(this.OnVipBtnClick);
}
private void OnEnable()
{
RegisterThis();
RefreshView();
}
private void OnDisable()
{
UnregisterThis();
}
private float _lastUpdateTime;
private void Update()
{
if (Time.time - _lastUpdateTime > 1f)
{
_lastUpdateTime = Time.time;
UpdateView();
}
}
#endregion
#region Mediator
readonly int[] _notificationInterests = new int[]
{
GlobalDefine.EVENT_PLAYER_ATTRIBUTES_CHANGED,
GlobalDefine.EVENT_MONEY_CHANGED,
GlobalDefine.EVENT_NAME_CHANGED,
//GlobalDefine.EVENT_GRADE_CHANGED,
GlobalDefine.EVENT_PLAYER_AVATAR_CHANGED,
GlobalDefine.EVENT_VIP_CHANGED
};
public override IList<int> ListNotificationInterests()
{
return _notificationInterests;
}
public override void HandleNotification(PureMVC.Interfaces.INotification notification)
{
if (notification.Name == GlobalDefine.EVENT_PLAYER_ATTRIBUTES_CHANGED)
{
RefreshCombatValue();
}
else if (notification.Name == GlobalDefine.EVENT_MONEY_CHANGED)
{
RefreshMoney();
}
else if (notification.Name == GlobalDefine.EVENT_NAME_CHANGED)
{
RefreshName();
}
//else if (notification.Name == GlobalDefine.EVENT_GRADE_CHANGED)
//{
// RefreshGrade();
//}
else if (notification.Name == GlobalDefine.EVENT_PLAYER_AVATAR_CHANGED)
{
RefreshAvatar();
}
else if (notification.Name == GlobalDefine.EVENT_VIP_CHANGED)
{
RefreshVip();
}
}
#endregion
}
}
}