NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/SkinChangeController.cs

458 lines
14 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UIElements;
using Button = UnityEngine.UI.Button;
using Image = UnityEngine.UI.Image;
using Toggle = UnityEngine.UI.Toggle;
using Framework;
using Cysharp.Threading.Tasks;
using Gameplay;
using cfg.CharacterCfg;
using PhxhSDK;
using System.Threading.Tasks;
using UnityEngine.EventSystems;
using Gameplay.Performance;
using cfg.ActorCfg;
using Gameplay.Utils;
using UnityEngine.Playables;
public class SkinChangeController : UIWindow
{
//UI GameObj
///Auto Gen Start///
public Image Image_BG;
//public Button Button_Back;
public Button Button_back;
public TMP_Text Text_Back;
public Button Button_Home;
public Image Image_Dark;
public Image Image_LeftBG;
public Image Image_ModelPosition;
public Image Image_Poster;
public Image Image_PosterPreview;
public Image Image_MidBG;
public Button Button_ShowAllPic;
public TMP_Text Text_Name;
public TMP_Text Text_SkinName;
public TMP_Text Text_Info;
public TMP_Text Text_GetWay;
public Button Button_Buy;
public TMP_Text Text_Buy;
public Button Button_Wear;
public TMP_Text Text_Wear;
public GameObject UpperUI;
///Auto Gen End///
private PinchZoomModule PinchZoom;
private DragForUI Drag;
private PhxhSDK.PhxhScrollView ScrollView;
SkinScrollView skinScrollView = new SkinScrollView();
GameObject _sampleModel;
GameObject _instModel;
private bool isShowPosterPic = false;
private CharacterDataInfo curCharacterInfo;
private int curSelectSkinId;
private int curUsingSkinId;
List<DataCharacterSkin> characterSkinList;
Dictionary<int, Sprite> posterLargePics = new();
Dictionary<int, Sprite> posterHalfPics = new();
public static async UniTask<UIWindow> Open(CharacterDataInfo characterData)
{
CommonUtils.CaptureScreenshot();
return await UIManager.Instance.CreateAndOpenWindow(UINameConst.UI_SkinChange, characterData);
}
// deal with input data
public override void PreLoad(object data = null)
{
curCharacterInfo = data as CharacterDataInfo;
curSelectSkinId = (int)curCharacterInfo.SkinID + 1;
curUsingSkinId = curSelectSkinId;
characterSkinList = CommonUtils.GetCharacterSkinCfgList((int)curCharacterInfo.ID);
PreLoadSpritesAndModelAsync();
}
async void PreLoadSpritesAndModelAsync()
{
foreach (var dataCharacter in characterSkinList)
{
string path = CommonUtils.GetCharacterPicPath((int)curCharacterInfo.ID, CommonUtils.ECharacterPicPathType.Poster, dataCharacter.SkinID);
var sprite = await LoadAssetAsync<Sprite>(path);
if (!posterLargePics.ContainsKey(dataCharacter.SkinID))
posterLargePics.Add(dataCharacter.SkinID, sprite);
else
posterLargePics[dataCharacter.SkinID] = sprite;
path = CommonUtils.GetCharacterPicPath((int)curCharacterInfo.ID, CommonUtils.ECharacterPicPathType.Favorable, dataCharacter.SkinID);
var halfSprite = await LoadAssetAsync<Sprite>(path);
if (!posterHalfPics.ContainsKey(dataCharacter.SkinID))
posterHalfPics.Add(dataCharacter.SkinID, halfSprite);
else
posterHalfPics[dataCharacter.SkinID] = halfSprite;
}
//var prefabPath = PathEx.GetCharacterDisplayModelPath(curCharacterInfo.Cfg.NameID);
//_sampleModel = await LoadAssetAsync<GameObject>(prefabPath);
}
async void LoadModel()
{
var prefabPath = PathEx.GetCharacterDisplayModelPath(curCharacterInfo.Cfg.NameID);
_sampleModel = await LoadAssetAsync<GameObject>(prefabPath);
GameObject model = Object.Instantiate(_sampleModel, Image_ModelPosition.transform, true);
_instModel = model;
model.transform.localPosition = new Vector3(0, -200, -300);
model.transform.localRotation = new(0, 180, 0, 1);
model.transform.localScale = 300 * Vector3.one;
model.gameObject.SetLayerEx(5);
var pd = model.GetComponentInChildren<PlayableDirector>();
if (pd != null)
{
pd.enabled = false;
}
var animator = model.GetComponentInChildren<Animator>();
if (animator != null)
{
animator.Play("standready001");
}
}
// Use this for initialization
public override void OnInit()
{
//bind UI GameObj
SkinChangeBinder.GetComponents(this);
UpperUI = FindObj("Image_BG/UpperUI");
PinchZoom = GetComponent<PinchZoomModule>("Image_BG/Image_Poster");
Drag = GetComponent<DragForUI>("Image_BG/Image_Poster");
ScrollView = GetComponent<PhxhSDK.PhxhScrollView>("Image_BG/UpperUI/ChangeSkin/ScrollView");
skinScrollView.SetScrollRect(ScrollView);
skinScrollView.SetItemCountFunc(() => characterSkinList.Count + 2);
skinScrollView.SetItemSizeFunc((index) => skinScrollView.GetItemSizeFromList(index));
skinScrollView.SetUpdateFunc(_RefreshSkinItem);
skinScrollView.InitItemCellList(characterSkinList.Count);
BindButton(Button_ShowAllPic, OnShowPicButton);
BindButton(Button_back, OnReturnButton);
LoadModel();
Drag.OnDragStarted += OnDragBegin;
Drag.OnDraging += OnDrag;
Drag.OnDragCompleted += OnDragEnd;
}
//invoke when open window
protected override void OnShowWindow(object data = null)
{
if (PinchZoom != null)
PinchZoom.enabled = false;
isShowPosterPic = false;
SetPosterSkinPic();
SetSkinInfo();
SetPosterSkinPic();
_SetSkinPosYListData();
skinScrollView.UpdateData();
skinScrollView.ScrollTo(curSelectSkinId);
}
//invoke when reload window
protected override void OnReloadWindow(object data = null)
{
}
//invoke when close window
protected override void OnHideWindow()
{
}
//invoke when destroy
public override void OnRelease()
{
base.OnRelease();
}
#region Button Event
void OnShowPicButton()
{
ShowAllPic(true);
}
void OnReturnButton()
{
if(isShowPosterPic)
{
ShowAllPic(false);
SetSkinInfo();
}
else
{
CloseWindow();
}
}
void _OnClickSkinItem(int to)
{
if (curSelectSkinId == to)
{
return;
}
int from = curSelectSkinId;
curSelectSkinId = to;
//_RefreshPosterButton();
skinScrollView.DoTwoItemScale(to - 1, from - 1, skinScrollView.ItemFullRectTrans.rect.size, skinScrollView.ItemRectTrans.rect.size, 0.1f,
() =>
{
_SetSkinPosYListData(false);
}, () =>
{
skinScrollView.AnimScrollTo(to);
SetPosterSkinPic();
SetSkinInfo();
});
}
#endregion
#region drag event
float scaleFactor;
void OnDragBegin(PointerEventData data)
{
if(!isShowPosterPic)
{
return;
}
scaleFactor = UIRoot.Instance.RootCanvas.GetComponent<RectTransform>().sizeDelta.x / PerformanceManager.instance.fullScreenWidth;
}
void OnDrag(PointerEventData data)
{
if(!isShowPosterPic)
{
return;
}
Drag.GetComponent<RectTransform>().anchoredPosition += data.delta * scaleFactor;
//drag.transform.position = GoRoot.transform.position;
}
void OnDragEnd(PointerEventData data)
{
if (!isShowPosterPic)
{
return;
}
}
#endregion
private void _SetSkinPosYListData(bool setSize = true)
{
if (setSize)
{
for (int i = 0; i < characterSkinList.Count; i++)
{
Vector2 size = skinScrollView.SkinItemSize(i + 1, curSelectSkinId);
skinScrollView._itemCells[i].size = size;
}
}
var posY = -skinScrollView.SkinItemSize(-1, curSelectSkinId).y;
for (int i = 0; i < characterSkinList.Count; i++)
{
Vector2 size = skinScrollView._itemCells[i].size;
posY -= size.y / 2;
skinScrollView._itemCells[i].localPosY = posY;
posY -= size.y / 2;
}
}
private async void _RefreshSkinItem(int index, RectTransform item)
{
if (index <= 0 || index >= characterSkinList.Count + 1)
{
item.gameObject.SetActive(false);
return;
}
var go = item.gameObject;
go.SetActive(true);
var posterPic =
CommonUtils.GetCharacterPicPath((int)curCharacterInfo.ID, CommonUtils.ECharacterPicPathType.Favorable, index);
var imgSkin = CommonUtils.GetComponent<Image>(go, "ButtonChange/ImageSkin");
var imgSelect = CommonUtils.GetGameObject(go, "ButtonChange/ImageChooseRect");
var imgDoSelect = CommonUtils.GetGameObject(go, "ButtonChange/ImageCover");
var btnChange = CommonUtils.GetComponent<Button>(go, "ButtonChange");
var btnNotSelect = CommonUtils.GetComponent<Button>(go, "ButtonNotSelect");
if (posterHalfPics.ContainsKey(index))
imgSkin.sprite = posterHalfPics[index];
else
imgSkin.sprite = await LoadAssetAsync<Sprite>(posterPic);
btnChange.gameObject.SetActive(true);
btnNotSelect.gameObject.SetActive(false);
imgSelect.SetActive(curSelectSkinId == index);
imgDoSelect.SetActive(curSelectSkinId != index);
ClearBind(btnChange);
ClearBind(btnNotSelect);
BindButton(btnChange, _OnClickSkinItem, index);
btnChange.GetComponent<RectTransform>().sizeDelta = skinScrollView.GetItemSizeFromList(index);
}
void ShowAllPic(bool showPic)
{
var UI_HIDE_POSITION = new Vector3(0, -1800, 0);
isShowPosterPic = showPic;
PinchZoom.enabled = showPic;
UpperUI.transform.localPosition = showPic ? UI_HIDE_POSITION : Vector3.zero;
Image_LeftBG.gameObject.SetActive(!showPic);
if (!showPic)
{
Image_Poster.transform.localScale = Image_PosterPreview.transform.localScale;
Image_Poster.transform.position = Image_PosterPreview.transform.position;
var animator = _instModel.GetComponentInChildren<Animator>();
if (animator != null)
{
animator.Play("standready001");
}
}
}
async void SetPosterSkinPic()
{
if(posterLargePics.ContainsKey(curSelectSkinId))
{
Image_Poster.sprite = posterLargePics[curSelectSkinId];
return;
}
var posterPic =
CommonUtils.GetCharacterPicPath((int)curCharacterInfo.ID, CommonUtils.ECharacterPicPathType.Poster, curSelectSkinId);
Image_Poster.sprite = await LoadAssetAsync<Sprite>(posterPic);
}
void RefreshButton()
{
}
void SetSkinInfo()
{
if (curSelectSkinId == 0)
{
return;
}
var skinCfg = characterSkinList[curSelectSkinId - 1];
if (CategoryManager.Instance.TryGetSkinItemInfo((int)curCharacterInfo.ID, curSelectSkinId, out DataItem info))//<2F><>Ĭ<EFBFBD><C4AC>Ƥ<EFBFBD><C6A4>
{
if (CategoryManager.Instance.GetItemCount(info.ID) > 0)//ӵ<><D3B5>Ƥ<EFBFBD><C6A4>
{
Button_Buy.gameObject.SetActive(false);
Button_Wear.gameObject.SetActive(true);
Text_GetWay.text = string.Empty;
}
else
{
Button_Buy.gameObject.SetActive(true);
Button_Wear.gameObject.SetActive(false);
Text_GetWay.text = CommonUtils.GetLocalizeText(skinCfg.AcquireCondition);
}
}
else//<2F><>ʼƤ<CABC><C6A4>
{
Button_Buy.gameObject.SetActive(false);
Button_Wear.gameObject.SetActive(true);
Text_GetWay.text = string.Empty;
if (curSelectSkinId != 1)
{
DebugUtil.LogError("<22><><EFBFBD><EFBFBD>Ƥ<EFBFBD><C6A4><EFBFBD><EFBFBD>Item<65><6D><EFBFBD>ñ<EFBFBD><C3B1>ڲ<EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD>ΪƤ<CEAA><C6A4><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IDΪ1<CEAA>ij<EFBFBD>ʼƤ<CABC><C6A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>飡\n<><6E>ɫID<49><44>" + curCharacterInfo.ID + " Ƥ<><C6A4>ID<49><44>" + curSelectSkinId);
}
}
Text_Name.text = skinCfg.Name;
Text_SkinName.text = CommonUtils.GetLocalizeText(skinCfg.SkinName);
Text_Info.text = CommonUtils.GetLocalizeText(skinCfg.SkinDesc);
}
class SkinScrollView : IPanelScrollView
{
public override void SetScrollRect(PhxhScrollView scrollRect)
{
centerAnchor = true;
canDrag = false;
Self_aligning = false;
base.SetScrollRect(scrollRect);
SetItemRectTrans(CommonUtils.GetComponent<RectTransform>(_scrollRect.itemTemplate.gameObject, "ButtonNotSelect"),
CommonUtils.GetComponent<RectTransform>(_scrollRect.itemTemplate.gameObject, "ButtonChange"));
SetPaddingSize(_itemRectTrans.rect.size);
}
public Vector2 SkinItemSize(int index, int curIndex)
{
if (index == 0 || index == _itemCells.Count + 1)
{
return _itemRectTrans.rect.size;
}
if (index == curIndex)
{
return _itemFullRectTrans.rect.size;
}
else
{
return _itemRectTrans.rect.size;
}
}
/// <summary>
/// ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>_SetSkinPosYListData
/// </summary>
/// <param name="count"></param>
public void InitItemCellList(int count)
{
_itemCells.Clear();
for (int i = 1; i <= count; i++)
{
UIScrollViewItemCell cell = new UIScrollViewItemCell();
cell.index = i;
_itemCells.Add(cell);
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD><DABB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>ŵ<EFBFBD>item<65>Ĵ<EFBFBD>С
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public Vector2 GetItemSizeFromList(int index)
{
if (index > _itemCells.Count + 1)
{
DebugUtil.Log("index out of range!index: " + index);
return _itemFullRectTrans.rect.size;
}
if (index == 0 || index == _itemCells.Count + 1)
{
return _itemFullRectTrans.rect.size;
}
else
{
return _itemCells[index - 1].size;
}
}
}
}