Files
back_cantanBuilding/Assets/Scripts/UI/Social/ClubHeadPopupPanel.cs
2026-05-26 16:15:54 +08:00

167 lines
5.9 KiB
C#

using asap.core;
using cfg;
using game;
using GameCore;
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class ClubHeadPopupPanel : MonoBehaviour
{
public Action<string> OnChange;
Button btn_close;
Button btn_mask;
Button btn_save;
ClubHead club_head;
TMP_Text text_title;
Transform iconContent;
Transform frameContent;
GameObject iconItem;
GameObject frameItem;
string iconUrl;
string frameUrl;
List<GameObject> iconSelecteds = new List<GameObject>();
List<GameObject> frameSelecteds = new List<GameObject>();
IUIService uIService;
List<ClubAvatar> avatars;
List<ClubFrame> frames;
int iconIndex;
int frameIndex;
private void Awake()
{
uIService = GContext.container.Resolve<IUIService>();
btn_close = transform.Find("btn_close").GetComponent<Button>();
btn_mask = transform.Find("mask").GetComponent<Button>();
btn_save = transform.Find("root/btn_save/btn_green").GetComponent<Button>();
club_head = transform.Find("root/btn_head").GetComponent<ClubHead>();
text_title = transform.Find("root/text_title").GetComponent<TMP_Text>();
iconContent = transform.Find("root/ScrollView1/Viewport/Content");
frameContent = transform.Find("root/ScrollView2/Viewport/Content");
iconItem = transform.Find("root/ScrollView1/Viewport/Content/icon").gameObject;
frameItem = transform.Find("root/ScrollView2/Viewport/Content/frame").gameObject;
iconItem.SetActive(false);
frameItem.SetActive(false);
}
private void Start()
{
btn_close.onClick.AddListener(OnClose);
btn_mask.onClick.AddListener(OnClose);
btn_save.onClick.AddListener(OnClickSave);
int id = GContext.container.Resolve<Tables>().TbGlobalConfig.ClubInitAvatar;
int frameId = GContext.container.Resolve<Tables>().TbGlobalConfig.ClubInitFrame;
iconUrl = GContext.container.Resolve<Tables>().TbClubAvatar[id].Avatar;
frameUrl = GContext.container.Resolve<Tables>().TbClubFrame[frameId].Frame;
if (GContext.container.Resolve<ClubService>().IsClubJoined())
{
var curGroupSettingData = GContext.container.Resolve<ClubService>().myClubInfo.clubSettingData;
text_title.text = LocalizationMgr.GetText("UI_ClubCreatePopupPanel_8");
if (curGroupSettingData != null && !string.IsNullOrEmpty(curGroupSettingData.icon))
{
var icon = curGroupSettingData.icon.Split('#');
iconUrl = icon[0];
frameUrl = icon[1];
}
}
else
{
text_title.text = LocalizationMgr.GetText("UI_ClubCreatePopupPanel_1");
}
SetScrollView();
club_head.SetIcon(iconUrl);
club_head.SetFrame(frameUrl);
}
void SetScrollView()
{
avatars = GContext.container.Resolve<Tables>().TbClubAvatar.DataList;
frames = GContext.container.Resolve<Tables>().TbClubFrame.DataList;
Image image;
GameObject select;
ClubAvatar avatar;
for (int i = 0; i < avatars.Count; i++)
{
int ii = i;
avatar = avatars[i];
var item = Instantiate(iconItem, iconContent);
item.SetActive(true);
image = item.transform.Find("icon").GetComponent<Image>();
uIService.SetImageSprite(image, avatar.Avatar);
select = item.transform.Find("selected").gameObject;
select.SetActive(avatar.Avatar == iconUrl);
if (avatar.Avatar == iconUrl)
{
iconIndex = ii;
}
iconSelecteds.Add(select);
item.GetComponent<Button>().onClick.AddListener(() =>
{
ChangeIcon(ii);
});
}
ClubFrame frame;
for (int i = 0; i < frames.Count; i++)
{
int ii = i;
frame = frames[i];
var item = Instantiate(frameItem, frameContent);
item.SetActive(true);
image = item.transform.Find("icon").GetComponent<Image>();
uIService.SetImageSprite(image, frame.Frame);
select = item.transform.Find("selected").gameObject;
select.SetActive(frame.Frame == frameUrl);
if (frame.Frame == frameUrl)
{
frameIndex = ii;
}
frameSelecteds.Add(select);
item.GetComponent<Button>().onClick.AddListener(() =>
{
ChangeFrame(ii);
});
}
}
void ChangeIcon(int index)
{
iconSelecteds[iconIndex].SetActive(false);
iconIndex = index;
iconSelecteds[iconIndex].SetActive(true);
iconUrl = avatars[index].Avatar;
club_head.SetIcon(iconUrl);
}
void ChangeFrame(int index)
{
frameSelecteds[frameIndex].SetActive(false);
frameIndex = index;
frameSelecteds[frameIndex].SetActive(true);
frameUrl = frames[index].Frame;
club_head.SetFrame(frameUrl);
}
async void OnClickSave()
{
btn_save.enabled = false;
string newAvatar = $"{iconUrl}#{frameUrl}";
OnChange?.Invoke(newAvatar);
OnClose();
if (GContext.container.Resolve<ClubService>().IsClubJoined())
{
ClubSettingData groupSettingData = GContext.container.Resolve<ClubService>().myClubInfo.clubSettingData;
if (newAvatar != groupSettingData.icon)
{
groupSettingData.icon = newAvatar;
bool result = await GContext.container.Resolve<ClubService>().UpdateClub(groupSettingData);
if (result)
{
ToastPanel.Show(LocalizationMgr.GetText("UI_ToastPanel_39"));
}
}
}
}
void OnClose()
{
UIManager.Instance.DestroyUI(UITypes.ClubHeadPopupPanel);
}
}