374 lines
13 KiB
C#
374 lines
13 KiB
C#
using BrewMonster.Network;
|
|
using BrewMonster.Scripts.Pet;
|
|
using BrewMonster.UI;
|
|
using CSNetwork;
|
|
using CSNetwork.Protocols;
|
|
using CSNetwork.GPDataType;
|
|
using ModelRenderer.Scripts.GameData;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace BrewMonster
|
|
{
|
|
public class CDlgFriendList : AUIDialog
|
|
{
|
|
private enum FriendTab
|
|
{
|
|
Friend = 0,
|
|
Blacklist = 1,
|
|
Archrival = 2
|
|
}
|
|
|
|
[Header("Buttons")]
|
|
[SerializeField] private Button m_helpBtn;
|
|
[SerializeField] private Button m_friendBtn;
|
|
[SerializeField] private Button m_blacklistBtn;
|
|
[SerializeField] private Button m_archrivalBtn;
|
|
[SerializeField] private Button m_addBtn;
|
|
[SerializeField] private Button m_deleteBtn;
|
|
[SerializeField] private Button m_fuctionBtn;
|
|
[SerializeField] private Button m_confirmBtn;
|
|
[SerializeField] private Button m_cancelBtn;
|
|
[SerializeField] private Button m_closeBtn;
|
|
|
|
[Header("Buttons ContextMenu")]
|
|
[SerializeField] private Button m_associate;
|
|
[SerializeField] private Button m_addSubgrounp;
|
|
[SerializeField] private Button m_autoReply;
|
|
[SerializeField] private Button m_notifiOnl;
|
|
[SerializeField] private Button m_close;
|
|
|
|
[Header("Container")]
|
|
[SerializeField] private Transform m_friendContainer;
|
|
[SerializeField] private GameObject m_friendDetailPrefabs;
|
|
|
|
[Header("GameObject")]
|
|
[SerializeField] private GameObject m_contextMenu;
|
|
[SerializeField] private GameObject m_inputName;
|
|
[SerializeField] private Sprite m_normalBtn;
|
|
[SerializeField] private Sprite m_grayBtn;
|
|
|
|
private CSNetwork.GameSession _session;
|
|
private FriendTab _currentTab = FriendTab.Friend;
|
|
private int _selectedFriendId;
|
|
private string _selectedFriendName;
|
|
private Image _selectedRowImage;
|
|
private Color _selectedRowOriginalColor;
|
|
private TMP_InputField _addNameInput;
|
|
private static readonly Color SelectedRowColor = new Color32(255, 255, 255, 60);
|
|
|
|
public void OnInitDialog()
|
|
{
|
|
if (!IsShow())
|
|
{
|
|
Show(true);
|
|
}
|
|
|
|
_session = UnityGameSession.Instance?.GameSession;
|
|
if (_session != null)
|
|
{
|
|
_session.FriendListReceived -= OnFriendListReceived;
|
|
_session.FriendListReceived += OnFriendListReceived;
|
|
_session.FriendStatusChanged -= OnFriendStatusChanged;
|
|
_session.FriendStatusChanged += OnFriendStatusChanged;
|
|
_session.Friend_GetList();
|
|
}
|
|
|
|
UpdateButtonTabUI(_currentTab);
|
|
RefreshListFromHost();
|
|
|
|
if (m_helpBtn != null)
|
|
{
|
|
m_helpBtn.onClick.RemoveAllListeners();
|
|
m_helpBtn.onClick.AddListener(() =>
|
|
{
|
|
// TODO: Show help content
|
|
});
|
|
}
|
|
|
|
if (m_friendBtn != null)
|
|
{
|
|
m_friendBtn.onClick.RemoveAllListeners();
|
|
m_friendBtn.onClick.AddListener(() =>
|
|
{
|
|
_currentTab = FriendTab.Friend;
|
|
UpdateButtonTabUI(_currentTab);
|
|
RefreshListFromHost();
|
|
});
|
|
}
|
|
|
|
if (m_blacklistBtn != null)
|
|
{
|
|
m_blacklistBtn.onClick.RemoveAllListeners();
|
|
m_blacklistBtn.onClick.AddListener(() =>
|
|
{
|
|
_currentTab = FriendTab.Blacklist;
|
|
UpdateButtonTabUI(_currentTab);
|
|
RefreshListFromHost();
|
|
});
|
|
}
|
|
|
|
if (m_archrivalBtn != null)
|
|
{
|
|
m_archrivalBtn.onClick.RemoveAllListeners();
|
|
m_archrivalBtn.onClick.AddListener(() =>
|
|
{
|
|
_currentTab = FriendTab.Archrival;
|
|
UpdateButtonTabUI(_currentTab);
|
|
RefreshListFromHost();
|
|
});
|
|
}
|
|
|
|
if (m_addBtn != null)
|
|
{
|
|
m_addBtn.onClick.RemoveAllListeners();
|
|
m_addBtn.onClick.AddListener(() =>
|
|
{
|
|
EnsureAddNameInput();
|
|
if (m_inputName != null)
|
|
m_inputName.SetActive(true);
|
|
});
|
|
}
|
|
|
|
if (m_deleteBtn != null)
|
|
{
|
|
m_deleteBtn.onClick.RemoveAllListeners();
|
|
m_deleteBtn.onClick.AddListener(() => CECUIManager.Instance.ShowMessageBoxYesAndNo(
|
|
title: "Del Friend Requets",
|
|
message: $"Bạn có chắc muốn xóa {_selectedFriendName} ra khỏi danh sách hảo hữu",
|
|
dlg: null,
|
|
onClickedYes:() => OnDeleteFriendClicked(true),
|
|
onClickedNo: () => OnDeleteFriendClicked(false)));
|
|
}
|
|
|
|
if (m_fuctionBtn != null)
|
|
{
|
|
m_fuctionBtn.onClick.RemoveAllListeners();
|
|
m_fuctionBtn.onClick.AddListener(() =>
|
|
{
|
|
m_contextMenu.SetActive(true);
|
|
});
|
|
}
|
|
|
|
if (m_confirmBtn != null)
|
|
{
|
|
m_confirmBtn.onClick.RemoveAllListeners();
|
|
m_confirmBtn.onClick.AddListener(() =>
|
|
{
|
|
if (_session == null) return;
|
|
EnsureAddNameInput();
|
|
var n = _addNameInput != null ? _addNameInput.text : string.Empty;
|
|
n = (n ?? string.Empty).Trim();
|
|
if (n.Length <= 0) return;
|
|
|
|
_session.Friend_Add(0, n);
|
|
if (m_inputName != null)
|
|
m_inputName.SetActive(false);
|
|
});
|
|
}
|
|
|
|
if (m_cancelBtn != null)
|
|
{
|
|
m_cancelBtn.onClick.RemoveAllListeners();
|
|
m_cancelBtn.onClick.AddListener(() =>
|
|
{
|
|
_addNameInput.text = string.Empty;
|
|
m_inputName.SetActive(false);
|
|
});
|
|
}
|
|
|
|
if (m_closeBtn != null)
|
|
{
|
|
m_closeBtn.onClick.RemoveAllListeners();
|
|
m_closeBtn.onClick.AddListener(() =>
|
|
{
|
|
m_contextMenu.SetActive(false);
|
|
CloseDialogue();
|
|
});
|
|
}
|
|
|
|
if (m_associate != null)
|
|
{
|
|
m_associate.onClick.RemoveAllListeners();
|
|
m_associate.onClick.AddListener(() =>
|
|
{
|
|
m_inputName.SetActive(true);
|
|
m_contextMenu.SetActive(false);
|
|
});
|
|
}
|
|
|
|
if (m_addSubgrounp != null)
|
|
{
|
|
m_addSubgrounp.onClick.RemoveAllListeners();
|
|
m_addSubgrounp.onClick.AddListener(() =>
|
|
{
|
|
// TODO: Add subgroup for friend
|
|
});
|
|
}
|
|
|
|
if (m_autoReply != null)
|
|
{
|
|
m_autoReply.onClick.RemoveAllListeners();
|
|
m_autoReply.onClick.AddListener(() =>
|
|
{
|
|
// TODO: Set auto-reply message for friend requests
|
|
});
|
|
}
|
|
|
|
if (m_notifiOnl != null)
|
|
{
|
|
m_notifiOnl.onClick.RemoveAllListeners();
|
|
m_notifiOnl.onClick.AddListener(() =>
|
|
{
|
|
// TODO: Toggle online notifications for this friend
|
|
});
|
|
}
|
|
|
|
if (m_close != null)
|
|
{
|
|
m_close.onClick.RemoveAllListeners();
|
|
m_close.onClick.AddListener(() =>
|
|
{
|
|
m_contextMenu.SetActive(false);
|
|
});
|
|
}
|
|
}
|
|
|
|
public override void OnDisable()
|
|
{
|
|
base.OnDisable();
|
|
if (_session != null)
|
|
{
|
|
_session.FriendListReceived -= OnFriendListReceived;
|
|
_session.FriendStatusChanged -= OnFriendStatusChanged;
|
|
}
|
|
}
|
|
|
|
private void OnFriendListReceived(getfriends_re _)
|
|
{
|
|
RefreshListFromHost();
|
|
}
|
|
|
|
private void OnFriendStatusChanged(int _, byte __)
|
|
{
|
|
RefreshListFromHost();
|
|
}
|
|
|
|
private void RefreshListFromHost()
|
|
{
|
|
if (m_friendContainer == null || m_friendDetailPrefabs == null)
|
|
return;
|
|
|
|
ClearSelection();
|
|
|
|
for (int i = m_friendContainer.childCount - 1; i >= 0; i--)
|
|
{
|
|
var child = m_friendContainer.GetChild(i);
|
|
if (child != null)
|
|
Destroy(child.gameObject);
|
|
}
|
|
|
|
if (_currentTab != FriendTab.Friend)
|
|
return;
|
|
|
|
var host = GetHostPlayer();
|
|
var man = host?.GetFriendMan();
|
|
if (man == null || !man.CheckInit())
|
|
return;
|
|
|
|
int groupNum = man.GetGroupNum();
|
|
for (int gi = 0; gi < groupNum; gi++)
|
|
{
|
|
var g = man.GetGroupByIndex(gi);
|
|
if (g == null) continue;
|
|
|
|
foreach (var f in g.Friends)
|
|
{
|
|
var go = Instantiate(m_friendDetailPrefabs, m_friendContainer);
|
|
var nameTxt = go.transform.Find("text_name")?.GetComponent<TextMeshProUGUI>();
|
|
var stateTxt = go.transform.Find("text_state")?.GetComponent<TextMeshProUGUI>();
|
|
string displayName = string.IsNullOrEmpty(f.Name) ? f.Id.ToString() : f.Name;
|
|
|
|
if (nameTxt != null) nameTxt.text = string.IsNullOrEmpty(f.Name) ? f.Id.ToString() : f.Name;
|
|
if (stateTxt != null)
|
|
{
|
|
bool online = f.IsGameOnline();
|
|
Debug.Log($"[Friend] id={f.Id}, name={f.Name}, status={f.Status}, isGame={f.IsGameOnline()}, isGT={f.IsGTOnline()}");
|
|
stateTxt.text = online ? "Online" : "Offline";
|
|
}
|
|
|
|
WireRowSelection(go, f.Id, displayName);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void WireRowSelection(GameObject rowGo, int friendId, string displayName)
|
|
{
|
|
if (rowGo == null) return;
|
|
|
|
var btn = rowGo.GetComponent<Button>();
|
|
var img = rowGo.GetComponent<Image>();
|
|
if (btn == null) return;
|
|
|
|
btn.onClick.RemoveAllListeners();
|
|
btn.onClick.AddListener(() => SelectRow(friendId, displayName, img));
|
|
}
|
|
|
|
private void SelectRow(int friendId, string displayName, Image rowImage)
|
|
{
|
|
if (_selectedRowImage != null)
|
|
_selectedRowImage.color = _selectedRowOriginalColor;
|
|
|
|
_selectedFriendId = friendId;
|
|
_selectedFriendName = displayName;
|
|
_selectedRowImage = rowImage;
|
|
if (_selectedRowImage != null)
|
|
{
|
|
_selectedRowOriginalColor = _selectedRowImage.color;
|
|
_selectedRowImage.color = SelectedRowColor;
|
|
}
|
|
}
|
|
|
|
private void ClearSelection()
|
|
{
|
|
if (_selectedRowImage != null)
|
|
_selectedRowImage.color = _selectedRowOriginalColor;
|
|
|
|
_selectedFriendId = 0;
|
|
_selectedRowImage = null;
|
|
_selectedRowOriginalColor = default;
|
|
}
|
|
|
|
/// <summary>Delete friend: send PROTOCOL_DELFRIEND and wait for server to refresh list.</summary>
|
|
private void OnDeleteFriendClicked(bool isDel)
|
|
{
|
|
if (isDel)
|
|
{
|
|
if (_session == null || _selectedFriendId <= 0) return;
|
|
_session.Friend_Del(_selectedFriendId);
|
|
}
|
|
}
|
|
|
|
private void EnsureAddNameInput()
|
|
{
|
|
if (_addNameInput != null) return;
|
|
if (m_inputName == null) return;
|
|
_addNameInput = m_inputName.GetComponentInChildren<TMP_InputField>(true);
|
|
}
|
|
|
|
private void UpdateButtonTabUI(FriendTab friendTab)
|
|
{
|
|
if (m_friendBtn != null)
|
|
m_friendBtn.image.sprite = friendTab == FriendTab.Friend ? m_normalBtn : m_grayBtn;
|
|
if (m_blacklistBtn != null)
|
|
m_blacklistBtn.image.sprite = friendTab == FriendTab.Blacklist ? m_normalBtn : m_grayBtn;
|
|
if (m_archrivalBtn != null)
|
|
m_archrivalBtn.image.sprite = friendTab == FriendTab.Archrival ? m_normalBtn : m_grayBtn;
|
|
}
|
|
}
|
|
}
|