using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using System.Text.RegularExpressions;
|
using TableConfig;
|
using UnityEngine;
|
using UnityEngine.UI;
|
namespace Snxxz.UI
|
{
|
public class ChatSendComponent : MonoBehaviour
|
{
|
[SerializeField] Button m_SendBtn;
|
[SerializeField] InputField m_ChatInput;
|
[SerializeField] Button m_ExtentBtn;
|
[SerializeField] VoiceRecorder m_VoiceRecorder;
|
[SerializeField] RichText m_ChatValueDisplay;
|
[SerializeField] Text m_Placeholder;
|
[SerializeField, Header("上移高度")] float m_UpHeight = 300.0f;
|
|
public Text placeholder { get { return m_Placeholder; } }
|
public Button sendBtn { get { return m_SendBtn; } }
|
|
ChatCenter m_ChatCenter;
|
ChatCenter chatCenter
|
{
|
get
|
{
|
return m_ChatCenter ?? (m_ChatCenter = ModelCenter.Instance.GetModel<ChatCenter>());
|
}
|
}
|
|
[NonSerialized] public Window parent;
|
|
private bool m_OnChating = false;
|
|
private void Awake()
|
{
|
m_SendBtn.onClick.AddListener(OnChatSend);
|
m_ExtentBtn.onClick.AddListener(OnExtent);
|
m_ChatInput.onValueChanged.AddListener(OnChatValueChange);
|
}
|
|
private void OnEnable()
|
{
|
m_VoiceRecorder.voiceRecordSuccessEvent += OnVoiceRecordSuccessEvent;
|
FuncOpen.Instance.OnFuncStateChangeEvent += OnFuncStateChangeEvent;
|
chatCenter.UpdateChatValueEvent += UpdateChatValueEvent;
|
chatCenter.UpdateChatType += UpdateChatType;
|
m_ChatInput.characterLimit = chatCenter.chatCharacterLimit;
|
WindowCenter.Instance.windowAfterCloseEvent += WindowAfterCloseEvent;
|
m_OnChating = false;
|
CheckVoiceOpen();
|
}
|
|
private void WindowAfterCloseEvent(Window _window)
|
{
|
if (_window is ChatExtentWin)
|
{
|
if (parent != null)
|
{
|
parent.transform.localPosition = parent.transform.localPosition.SetY(0);
|
}
|
}
|
}
|
|
private void LateUpdate()
|
{
|
}
|
|
private void UpdateChatType()
|
{
|
m_Placeholder.text = ChatCtrl.Inst.presentChatType == ChatInfoType.Trumpet ? Language.Get("L1012") : Language.Get("L1111");
|
}
|
|
private void OnChatSend()
|
{
|
if (!FuncOpen.Instance.IsFuncOpen((int)FuncOpenEnum.Chat))
|
{
|
FuncOpen.Instance.ProcessorFuncErrorTip((int)FuncOpenEnum.Chat);
|
return;
|
}
|
ChatExtraData? info = null;
|
var _packModel = ModelCenter.Instance.GetModel<PlayerPackModel>();
|
SinglePackModel singlePack = _packModel.GetSinglePackModel(PackType.rptItem);
|
if (ChatCtrl.Inst.presentChatType == ChatInfoType.Trumpet)
|
{
|
if (chatCenter.IsChatBanned)
|
{
|
return;
|
}
|
List<ItemModel> _list = null;
|
var _count = singlePack.GetItemCountByID(chatCenter.bugleItem, out _list);
|
if (_count < 1)
|
{
|
ConfirmCancel.ShowPopConfirm(Language.Get("Mail101"), Language.Get("NoTrumpet"), () =>
|
{
|
WindowJumpMgr.Instance.WindowJumpTo(JumpUIType.StoreFunc2);
|
});
|
return;
|
}
|
info = new ChatExtraData() { infoint1 = _list[0].itemInfo.ItemPlace };
|
}
|
if (m_ChatInput.text.Length < 1)
|
{
|
return;
|
}
|
if (CheckRepeatContent(m_ChatInput.text))
|
{
|
SysNotifyMgr.Instance.ShowTip("RepeatChat");
|
return;
|
}
|
if (CheckChatCD())
|
{
|
SysNotifyMgr.Instance.ShowTip("OnChatCD");
|
return;
|
}
|
if (m_OnChating)
|
{
|
return;
|
}
|
m_OnChating = true;
|
string msg = m_ChatInput.text;
|
ChatCtrl.Inst.SendChatInfo(ChatCtrl.Inst.presentChatType, msg, info);
|
StartCoroutine(Co_CoolDowmChat());
|
}
|
|
IEnumerator Co_CoolDowmChat()
|
{
|
yield return WaitingForSecondConst.WaitMS1500;
|
m_OnChating = false;
|
}
|
|
private void OnChatValueChange(string _msg)
|
{
|
string _chat = Regex.Replace(_msg, @"\p{Cs}", "");
|
if (!_chat.Equals(_msg))
|
{
|
m_ChatInput.text = _chat;
|
chatCenter.chatInputLength = m_ChatInput.text.Length;
|
}
|
if (_msg.Equals(string.Empty))
|
{
|
chatCenter.recentlyChat = null;
|
}
|
m_ChatValueDisplay.text = _chat;
|
MatchCollection matchArray = HrefAnalysis.EquipRegex.Matches(_chat);
|
if (matchArray.Count == 0)
|
{
|
ChatCtrl.Inst.itemPlaceList.Clear();
|
chatCenter.recentlyChat = null;
|
return;
|
}
|
if (chatCenter.recentlyChat == null)
|
{
|
if (ChatCtrl.Inst.itemPlaceList.Count == matchArray.Count)
|
{
|
return;
|
}
|
}
|
if (chatCenter.recentlyChat != null)
|
{
|
var _recentlyChat = chatCenter.recentlyChat;
|
for (int i = 0, m = 0; i < _recentlyChat.itemIndexs.Count; i++)
|
{
|
if (m < matchArray.Count)
|
{
|
Match match = matchArray[m];
|
if (_recentlyChat.itemNames[_recentlyChat.itemIndexs[i]] != match.Groups[1].Value)
|
{
|
_recentlyChat.itemIndexs.RemoveAt(i);
|
i--;
|
}
|
else
|
{
|
m++;
|
}
|
}
|
}
|
return;
|
}
|
for (int i = 0, m = 0; i < ChatCtrl.Inst.itemPlaceList.Count; i++)
|
{
|
if (m < matchArray.Count)
|
{
|
Match match = matchArray[m];
|
ItemModel itemModel = ChatCtrl.Inst.itemPlaceList[i];
|
ItemConfig cfg = Config.Instance.Get<ItemConfig>((int)itemModel.itemInfo.ItemID);
|
if (cfg.ItemName != match.Groups[1].Value)
|
{
|
ChatCtrl.Inst.itemPlaceList.RemoveAt(i);
|
i--;
|
}
|
else
|
{
|
m++;
|
}
|
}
|
}
|
}
|
|
private void UpdateChatValueEvent(string _msg, bool _add, bool _force)
|
{
|
if (_add)
|
{
|
m_ChatInput.text += _msg;
|
chatCenter.chatInputLength = m_ChatInput.text.Length;
|
}
|
else
|
{
|
m_ChatInput.text = _msg;
|
chatCenter.chatInputLength = m_ChatInput.text.Length;
|
}
|
}
|
|
private void OnExtent()
|
{
|
if (!WindowCenter.Instance.CheckOpen<ChatExtentWin>())
|
{
|
WindowCenter.Instance.Open<ChatExtentWin>(true);
|
if (parent != null)
|
{
|
parent.transform.localPosition = parent.transform.localPosition.SetY(m_UpHeight);
|
}
|
}
|
else
|
{
|
WindowCenter.Instance.CloseImmediately<ChatExtentWin>();
|
if (parent != null)
|
{
|
parent.transform.localPosition = parent.transform.localPosition.SetY(0);
|
}
|
}
|
}
|
|
private void OnVoiceRecordSuccessEvent(int _instance, float _length)
|
{
|
var _type = ChatCtrl.Inst.presentChatType;
|
chatCenter.SetVoice(_instance, _type, _length, _type == ChatInfoType.Friend ? ChatCtrl.Inst.PteChatID : 0);
|
}
|
|
private void OnFuncStateChangeEvent(int _id)
|
{
|
if (_id == (int)FuncOpenEnum.Chat)
|
{
|
CheckVoiceOpen();
|
}
|
}
|
|
#region 重复内容
|
private string repeatChatContent = string.Empty;
|
private int repeatChatCount = 0;
|
private DateTime repeatTime = DateTime.Now;
|
private bool CheckRepeatContent(string _content)
|
{
|
if (repeatChatContent.Equals(_content) && (DateTime.Now - repeatTime).TotalSeconds < 10)
|
{
|
repeatChatCount++;
|
if (repeatChatCount > 3)
|
{
|
return true;
|
}
|
else
|
{
|
repeatTime = DateTime.Now;
|
}
|
}
|
else
|
{
|
repeatChatContent = _content;
|
repeatChatCount = 1;
|
repeatTime = DateTime.Now;
|
}
|
return false;
|
}
|
#endregion
|
|
#region 聊天CD
|
private DateTime chatCdTime = DateTime.Now;
|
private int chatCdCount = 0;
|
private bool chatBusy = false;
|
public bool CheckChatCD()
|
{
|
if (chatBusy && (DateTime.Now - chatCdTime).TotalSeconds < 10)
|
{
|
return true;
|
}
|
else if (chatBusy)
|
{
|
chatBusy = false;
|
chatCdCount = 0;
|
}
|
if ((DateTime.Now - chatCdTime).TotalSeconds < 2)
|
{
|
chatCdCount++;
|
}
|
else
|
{
|
chatCdCount = 1;
|
}
|
if (chatCdCount > 5)
|
{
|
chatBusy = true;
|
return true;
|
}
|
chatCdTime = DateTime.Now;
|
return false;
|
}
|
#endregion
|
|
private void CheckVoiceOpen()
|
{
|
m_VoiceRecorder.gameObject.SetActive(FuncOpen.Instance.IsFuncOpen((int)FuncOpenEnum.Chat));
|
}
|
|
private void OnDisable()
|
{
|
m_VoiceRecorder.voiceRecordSuccessEvent -= OnVoiceRecordSuccessEvent;
|
FuncOpen.Instance.OnFuncStateChangeEvent -= OnFuncStateChangeEvent;
|
chatCenter.UpdateChatValueEvent -= UpdateChatValueEvent;
|
chatCenter.UpdateChatType -= UpdateChatType;
|
WindowCenter.Instance.windowAfterCloseEvent -= WindowAfterCloseEvent;
|
}
|
}
|
}
|
|