| using System; | 
| using System.Collections; | 
| using System.Collections.Generic; | 
| using System.Text.RegularExpressions; | 
| using UnityEngine; | 
| using UnityEngine.UI; | 
| using EnhancedUI.EnhancedScroller; | 
|   | 
| // TODO YYL | 
|   | 
|     public class ChatContentBehaviour : MonoBehaviour | 
|     { | 
|         [SerializeField] ScrollerController m_ChatContentControl; | 
|         [SerializeField] RichText m_DestSysText; | 
|         [SerializeField] RichText m_DestTipText; | 
|         [SerializeField] RectTransform m_ContaienrNewInfo; | 
|         [SerializeField] Text m_NewInfoText; | 
|         [SerializeField] Button m_NewInfoBtn; | 
|         [SerializeField] Button m_LockAreaBtn; | 
|         [SerializeField] Image m_LockAreaCheck; | 
|         [SerializeField] RectTransform m_ContainerDisplay; | 
|         [SerializeField] RectTransform m_ContainerPrivateChatRemind; | 
|         [SerializeField] RectTransform m_Scroller; | 
|   | 
|         [SerializeField, Header("锁定当前区域比例"), Range(0, 1)] float m_Percent = 0.3f; | 
|   | 
|         [SerializeField] ChatPlayerMineCell m_ChatMineCell; | 
|         [SerializeField] ChatPlayerOtherCell m_ChatOtherCell; | 
|         // [SerializeField] ChatMineVoiceCell m_ChatMineVoiceCell; | 
|         // [SerializeField] ChatOtherVoiceCell m_ChatOtherVoiceCell; | 
|   | 
|         private ChatInfoType m_ChatType = ChatInfoType.System; | 
|         public ChatInfoType chatType | 
|         { | 
|             get | 
|             { | 
|                 return m_ChatType; | 
|             } | 
|             set | 
|             { | 
|                 bool isChange = m_ChatType != value; | 
|                 ResetNewInfo(); | 
|                 m_ChatType = value; | 
|                 DisplayChatContent(); | 
|                 if (isChange) | 
|                 { | 
|                     DisplayChatRemind(); | 
|                 } | 
|             } | 
|         } | 
|   | 
|         public ScrollerController chatContentControl | 
|         { | 
|             get | 
|             { | 
|                 return m_ChatContentControl; | 
|             } | 
|         } | 
|   | 
|         private int cacheChatCount = 0; | 
|         private bool onCheckArea = false; | 
|   | 
|         // FriendsModel friendModel | 
|         // { | 
|         //     get { return ModelCenter.Instance.GetModel<FriendsModel>(); } | 
|         // } | 
|   | 
|         private void Awake() | 
|         { | 
|             m_ChatContentControl.OnGetDynamicSize += OnGetChatDynamicSize; | 
|             m_NewInfoBtn.onClick.AddListener(OnNewInfo); | 
|             m_LockAreaBtn.onClick.AddListener(OnLockAreaBtn); | 
|             m_ChatContentControl.mScrollRect.onValueChanged.AddListener(OnScrollValChange); | 
|   | 
|             if (m_DestSysText.font == null) | 
|             { | 
|                 m_DestSysText.font = FontUtility.preferred; | 
|             } | 
|             if (m_DestTipText.font == null) | 
|             { | 
|                 m_DestTipText.font = FontUtility.preferred; | 
|             } | 
|         } | 
|   | 
|         private void OnEnable() | 
|         { | 
|             DisplayChatContent(); | 
|             DisplayChatRemind(); | 
|             ChatManager.OnRefreshChat += OnRefreshChat; | 
|             ChatManager.OnRefreshPteChat += OnRefreshPteChat; | 
|             ChatManager.OnRefreshSelf += OnRefreshSelf; | 
|             ChatManager.Instance.OnPteChatChangeEvent += OnPteChatChangeEvent; | 
|             ChatCenter.Instance.UpdateChatContent += UpdateChatContent; | 
|             PlayerDatas.Instance.playerDataRefreshEvent += PlayerDataRefreshInfoEvent; | 
|             // friendModel.RefreshFriendCntEvent += RefreshFriendCntEvent; | 
|             // friendModel.RefreshFriendModel += DisplayChatRemind; | 
|             ResetNewInfo(); | 
|             UpdateChatContent(); | 
|             if (m_ChatType == ChatInfoType.Friend) | 
|             { | 
|                 DisplayChatContent(); | 
|                 JumpPteChatBottom(); | 
|             } | 
|         } | 
|   | 
|         private void OnPteChatChangeEvent() | 
|         { | 
|             if (m_ChatType == ChatInfoType.Friend) | 
|             { | 
|                 DisplayChatContent(); | 
|                 DisplayChatRemind(); | 
|                 JumpPteChatBottom(); | 
|             } | 
|         } | 
|   | 
|         private void OnRefreshSelf(ChatData data) | 
|         { | 
|             ChatCenter.Instance.ChangeChatValue(string.Empty, false, false); | 
|             ChatManager.Instance.itemPlaceList.Clear(); | 
|             if (m_ChatType == ChatInfoType.Friend) | 
|             { | 
|                 JumpPteChatBottom(); | 
|             } | 
|             else | 
|             { | 
|                 ChatManager.Instance.lockUpdate = false; | 
|             } | 
|             UpdateChatContent(); | 
|         } | 
|   | 
|         private void OnRefreshPteChat(ChatFriendData data) | 
|         { | 
|             if (data == null) | 
|             { | 
|                 return; | 
|             } | 
|             if (data.toPlayer != ChatManager.Instance.PteChatID && data.player != ChatManager.Instance.PteChatID) | 
|             { | 
|                 return; | 
|             } | 
|             DisplayChatContent(true); | 
|             float _displaySize = 0; | 
|             if (m_ChatContentControl.mScrollRect != null) | 
|             { | 
|                 _displaySize = m_ChatContentControl.mScrollRect.content.sizeDelta.y; | 
|             } | 
|             if (data.player == PlayerDatas.Instance.baseData.PlayerID) | 
|             { | 
|                 OnRefreshSelf(data); | 
|             } | 
|             else if (ChatManager.Instance.lockUpdate && _displaySize > m_ChatContentControl.m_Scorller.ScrollRectSize) | 
|             { | 
|                 cacheChatCount++; | 
|                 cacheChatCount = Mathf.Min(cacheChatCount, 999); | 
|                 m_NewInfoText.text = Language.Get("ChatNewWord", cacheChatCount); | 
|                 m_ContaienrNewInfo.SetActive(true); | 
|             } | 
|         } | 
|   | 
|         private void OnScrollValChange(Vector2 _pos) | 
|         { | 
|             if (m_ChatContentControl.mScrollRect.verticalNormalizedPosition < 0.1f && m_ChatType == ChatInfoType.Friend) | 
|             { | 
|                 ResetNewInfo(); | 
|             } | 
|             if (m_ChatContentControl.m_Scorller._ScrollSize <= 0 || m_ChatType == ChatInfoType.Friend) | 
|             { | 
|                 return; | 
|             } | 
|             float _value = m_Percent / m_ChatContentControl.m_Scorller._ScrollSize * m_ChatContentControl.m_Scorller.ScrollRectSize; | 
|             if (_pos.y <= _value) | 
|             { | 
|                 if (ChatManager.Instance.lockUpdate && !onCheckArea) | 
|                 { | 
|                     ChatManager.Instance.lockUpdate = false; | 
|                     UpdateLockAreaState(); | 
|                     ResetNewInfo(); | 
|                 } | 
|                 onCheckArea = true; | 
|             } | 
|             else | 
|             { | 
|                 if (!ChatManager.Instance.lockUpdate) | 
|                 { | 
|                     ChatManager.Instance.lockUpdate = true; | 
|                     UpdateChatContent(); | 
|                 } | 
|                 onCheckArea = false; | 
|             } | 
|         } | 
|   | 
|         private void OnLockAreaBtn() | 
|         { | 
|             ChatManager.Instance.lockUpdate = !ChatManager.Instance.lockUpdate; | 
|             UpdateChatContent(); | 
|         } | 
|   | 
|         private void OnRefreshChat(ChatInfoType _type) | 
|         { | 
|             if (_type == m_ChatType) | 
|             { | 
|                 DisplayChatContent(true); | 
|                 float _displaySize = 0; | 
|                 if (m_ChatContentControl.mScrollRect != null) | 
|                 { | 
|                     _displaySize = m_ChatContentControl.mScrollRect.content.sizeDelta.y; | 
|                 } | 
|                 if (ChatManager.Instance.lockUpdate && _displaySize > m_ChatContentControl.m_Scorller.ScrollRectSize) | 
|                 { | 
|                     cacheChatCount++; | 
|                     cacheChatCount = Mathf.Min(cacheChatCount, 999); | 
|                     m_NewInfoText.text = Language.Get("ChatNewWord", cacheChatCount); | 
|                     m_ContaienrNewInfo.SetActive(true); | 
|                 } | 
|             } | 
|         } | 
|   | 
|         private void ResetNewInfo() | 
|         { | 
|             cacheChatCount = 0; | 
|             if (m_ContaienrNewInfo.gameObject.activeSelf) | 
|             { | 
|                 m_ContaienrNewInfo.SetActive(false); | 
|             } | 
|         } | 
|   | 
|         private void DisplayChatContent(bool _keep = false) | 
|         { | 
|             float _offset = KeepArea(); | 
|   | 
|             m_ChatContentControl.Refresh(); | 
|   | 
|             if (m_ChatType != ChatInfoType.Friend) | 
|             { | 
|                 List<ChatData> _list = ChatManager.Instance.GetChatInfo(m_ChatType); | 
|                 if (_list != null) | 
|                 { | 
|                     for (int i = 0; i < _list.Count; i++) | 
|                     { | 
|                         switch (_list[i].type) | 
|                         { | 
|                             case ChatInfoType.World: | 
|                             case ChatInfoType.Area: | 
|                             case ChatInfoType.CrossServer: | 
|                             case ChatInfoType.Team: | 
|                             case ChatInfoType.Trumpet: | 
|                             case ChatInfoType.Fairy: | 
|                             case ChatInfoType.default1: | 
|                             case ChatInfoType.default2: | 
|                                 ChatUeseData data = _list[i] as ChatUeseData; | 
|                                 if (data.detailType == ChatInfoType.FairyQuestion | 
|                                     || data.detailType == ChatInfoType.FairyTip | 
|                                     || data.detailType == ChatInfoType.TeamTip | 
|                                     || data.detailType == ChatInfoType.default2) | 
|                                 { | 
|                                     m_ChatContentControl.AddCell(ScrollerDataType.Extra1, i); | 
|                                 } | 
|                                 else | 
|                                 { | 
|                                     if (data.IsSound) | 
|                                     { | 
|                                         m_ChatContentControl.AddCell(data.player == PlayerDatas.Instance.PlayerId ? | 
|                                             ScrollerDataType.Extra2 : ScrollerDataType.Extra3, i); | 
|                                         break; | 
|                                     } | 
|                                     if (data.player == PlayerDatas.Instance.baseData.PlayerID) | 
|                                     { | 
|                                         m_ChatContentControl.AddCell(ScrollerDataType.Header, i); | 
|                                     } | 
|                                     else | 
|                                     { | 
|                                         m_ChatContentControl.AddCell(ScrollerDataType.Normal, i); | 
|                                     } | 
|                                 } | 
|                                 break; | 
|                             case ChatInfoType.Invite: | 
|                             case ChatInfoType.System: | 
|                                 { | 
|                                     m_ChatContentControl.AddCell(ScrollerDataType.Tail, i); | 
|                                 } | 
|                                 break; | 
|                         } | 
|                     } | 
|                 } | 
|             } | 
|             else | 
|             { | 
|                 List<ChatFriendData> _list = ChatManager.Instance.GetChatInfo(ChatManager.Instance.PteChatID); | 
|                 if (_list != null) | 
|                 { | 
|                     for (int i = 0; i < _list.Count; i++) | 
|                     { | 
|                         if (_list[i].IsSound) | 
|                         { | 
|                             m_ChatContentControl.AddCell(_list[i].player == PlayerDatas.Instance.PlayerId ? | 
|                                             ScrollerDataType.Extra2 : ScrollerDataType.Extra3, i); | 
|                             continue; | 
|                         } | 
|                         if (Regex.IsMatch(_list[i].content, ChatManager.KILL_IDENTIFY)) | 
|                         { | 
|                             m_ChatContentControl.AddCell(ScrollerDataType.Tail, i); | 
|                             continue; | 
|                         } | 
|                         if (_list[i].player == PlayerDatas.Instance.baseData.PlayerID) | 
|                         { | 
|                             m_ChatContentControl.AddCell(ScrollerDataType.Header, i); | 
|                         } | 
|                         else | 
|                         { | 
|                             m_ChatContentControl.AddCell(ScrollerDataType.Normal, i); | 
|                         } | 
|                     } | 
|                 } | 
|             } | 
|   | 
|             m_ChatContentControl.Restart(); | 
|   | 
|             if (_keep && ChatManager.Instance.lockUpdate) | 
|             { | 
|                 m_ChatContentControl.JumpIndex(-_offset, 0, EnhancedUI.EnhancedScroller.EnhancedScroller.TweenType.immediate); | 
|             } | 
|   | 
|             m_LockAreaBtn.SetActive(m_ChatType != ChatInfoType.Friend); | 
|         } | 
|   | 
|         private void DisplayChatRemind() | 
|         { | 
|             if (m_ContainerPrivateChatRemind != null) | 
|             { | 
|                 // bool requireRemind = false; | 
|                 // var _dict = friendModel.GetFriendInfoDict((byte)GroupType.RecentContact); | 
|                 // if (m_ChatType == ChatInfoType.Friend) | 
|                 // { | 
|                 //     if (WindowCenter.Instance.IsOpen<ChatWin>()) | 
|                 //     { | 
|                 //         if (((_dict != null && _dict.Count > 0) || | 
|                 //         friendModel.tempFriendData != null) && ChatManager.Instance.PteChatID > 0) | 
|                 //         { | 
|                 //             requireRemind = true; | 
|                 //         } | 
|                 //     } | 
|                 //     else | 
|                 //     { | 
|                 //         requireRemind = true; | 
|                 //     } | 
|                 // } | 
|                 // var displayAreaHeight = m_ContainerDisplay.rect.height; | 
|                 // m_ContainerPrivateChatRemind.SetActive(requireRemind); | 
|                 // if (requireRemind) | 
|                 // { | 
|                 //     displayAreaHeight = m_ContainerDisplay.rect.height - m_ContainerPrivateChatRemind.rect.height; | 
|                 // } | 
|                 // m_Scroller.sizeDelta = m_Scroller.sizeDelta.SetY(displayAreaHeight); | 
|             } | 
|         } | 
|   | 
|         float KeepArea() | 
|         { | 
|             if (m_ChatContentControl.GetNumberOfCells(m_ChatContentControl.m_Scorller) == ChatManager.Instance.CHAT_INFO_CNT) | 
|             { | 
|                 var count = 0; | 
|                 if (m_ChatType == ChatInfoType.Friend) | 
|                 { | 
|                     var list = ChatManager.Instance.GetChatInfo(ChatManager.Instance.PteChatID); | 
|                     count = list == null ? 0 : list.Count; | 
|                 } | 
|                 else | 
|                 { | 
|                     var list = ChatManager.Instance.GetChatInfo(m_ChatType); | 
|                     count = list == null ? 0 : list.Count; | 
|                 } | 
|                 if (count == ChatManager.Instance.CHAT_INFO_CNT) | 
|                 { | 
|                     var first_size = m_ChatContentControl.GetCellSize(0); | 
|                     return first_size; | 
|                 } | 
|             } | 
|             return 0; | 
|         } | 
|   | 
|         private void SoundReceiveEvent(ChatInfoType _type, int _index) | 
|         { | 
|             if (m_ChatType != _type) | 
|             { | 
|                 return; | 
|             } | 
|             m_ChatContentControl.RefreshSingleCellView(_index); | 
|         } | 
|   | 
|         private bool OnGetChatDynamicSize(ScrollerDataType _type, int _index, out float _height) | 
|         { | 
|             _height = 0; | 
|             ChatData chat = null; | 
|             if (m_ChatType == ChatInfoType.Friend) | 
|             { | 
|                 var _list = ChatManager.Instance.GetChatInfo(ChatManager.Instance.PteChatID); | 
|                 if (_list == null || _index >= _list.Count) | 
|                 { | 
|                     return false; | 
|                 } | 
|                 chat = _list[_index]; | 
|             } | 
|             else | 
|             { | 
|                 List<ChatData> _list = ChatManager.Instance.GetChatInfo(m_ChatType); | 
|                 if (_list == null || _index >= _list.Count) | 
|                 { | 
|                     return false; | 
|                 } | 
|                 chat = _list[_index]; | 
|             } | 
|             if (chat == null) | 
|             { | 
|                 return false; | 
|             } | 
|             switch (_type) | 
|             { | 
|                 case ScrollerDataType.Header: | 
|                     _height = m_ChatMineCell.GetHeight(chat.content, chat.infoList); | 
|                     return true; | 
|                 case ScrollerDataType.Normal: | 
|                     _height = m_ChatOtherCell.GetHeight(chat.content, chat.infoList); | 
|                     return true; | 
|                 case ScrollerDataType.Extra1: | 
|                 case ScrollerDataType.Tail: | 
|                     _height = 30; | 
|                     break; | 
|                 // case ScrollerDataType.Extra2: | 
|                     // _height = m_ChatMineVoiceCell.GetHeight(chat.content, chat.infoList); | 
|                     // return true; | 
|                 // case ScrollerDataType.Extra3: | 
|                     // _height = m_ChatOtherVoiceCell.GetHeight(chat.content, chat.infoList); | 
|                     // return true; | 
|                 default: | 
|                     return false; | 
|             } | 
|             OnGetChatDynamicHeight(chat.content, ref _height, _type, chat.infoList); | 
|             return true; | 
|         } | 
|   | 
|         private void OnGetChatDynamicHeight(string _content, ref float _height, ScrollerDataType _type, ArrayList _infoList) | 
|         { | 
|             float _textHeight = 0; | 
|             if (_type == ScrollerDataType.Tail) | 
|             { | 
|                 m_DestSysText.SetExtenalData(_infoList); | 
|                 m_DestSysText.text = _content; | 
|                 _textHeight = Mathf.Max(m_DestSysText.preferredHeight, m_DestSysText.fontSize); | 
|                 _height += Mathf.Max(0, _textHeight - 23); | 
|             } | 
|             else if (_type == ScrollerDataType.Extra1) | 
|             { | 
|                 m_DestTipText.SetExtenalData(_infoList); | 
|                 m_DestTipText.text = _content; | 
|                 _textHeight = Mathf.Max(m_DestTipText.preferredHeight, m_DestTipText.fontSize); | 
|                 _height += Mathf.Max(0, _textHeight - 23); | 
|             } | 
|         } | 
|   | 
|         private void UpdateChatContent() | 
|         { | 
|             UpdateLockAreaState(); | 
|             if (!ChatManager.Instance.lockUpdate) | 
|             { | 
|                 chatContentControl.ResetScrollPos(); | 
|                 ResetNewInfo(); | 
|             } | 
|         } | 
|   | 
|         private void UpdateLockAreaState() | 
|         { | 
|             chatContentControl.lockType = ChatManager.Instance.lockUpdate ? EnhanceLockType.KeepVertical : EnhanceLockType.LockVerticalBottom; | 
|             m_LockAreaCheck.SetActive(ChatManager.Instance.lockUpdate); | 
|         } | 
|   | 
|         private void OnNewInfo() | 
|         { | 
|             if (m_ChatType != ChatInfoType.Friend) | 
|             { | 
|                 ChatManager.Instance.lockUpdate = false; | 
|             } | 
|             else | 
|             { | 
|                 JumpPteChatBottom(); | 
|             } | 
|             UpdateChatContent(); | 
|         } | 
|   | 
|         private void JumpPteChatBottom() | 
|         { | 
|             ChatManager.Instance.lockUpdate = true; | 
|             chatContentControl.lockType = EnhanceLockType.LockVerticalBottom; | 
|             chatContentControl.ResetScrollPos(); | 
|             ResetNewInfo(); | 
|             chatContentControl.lockType = EnhanceLockType.KeepVertical; | 
|         } | 
|   | 
|         private void OnDisable() | 
|         { | 
|             ChatManager.OnRefreshChat -= OnRefreshChat; | 
|             ChatManager.OnRefreshPteChat -= OnRefreshPteChat; | 
|             ChatManager.OnRefreshSelf -= OnRefreshSelf; | 
|             ChatCenter.Instance.UpdateChatContent -= UpdateChatContent; | 
|             ChatManager.Instance.OnPteChatChangeEvent -= OnPteChatChangeEvent; | 
|             PlayerDatas.Instance.playerDataRefreshEvent -= PlayerDataRefreshInfoEvent; | 
|             // friendModel.RefreshFriendCntEvent -= RefreshFriendCntEvent; | 
|             // friendModel.RefreshFriendModel -= DisplayChatRemind; | 
|         } | 
|   | 
|         private void RefreshFriendCntEvent(/*GroupType type, bool arg2*/) | 
|         { | 
|             // if (type == GroupType.RecentContact && m_ChatType == ChatInfoType.Friend) | 
|             // { | 
|             //     DisplayChatRemind(); | 
|             //     if (WindowCenter.Instance.IsOpen<ChatWin>()) | 
|             //     { | 
|             //         var _dict = friendModel.GetFriendInfoDict((byte)GroupType.RecentContact); | 
|             //         if (!_dict.ContainsKey((uint)ChatManager.Instance.PteChatID)) | 
|             //         { | 
|             //             ChatManager.Instance.PteChatID = 0; | 
|             //         } | 
|             //     } | 
|             // } | 
|         } | 
|   | 
|         private void PlayerDataRefreshInfoEvent(PlayerDataType refreshType) | 
|         { | 
|             if (refreshType == PlayerDataType.ExAttr10) | 
|             { | 
|                 m_ChatContentControl.m_Scorller.RefreshActiveCellViews(); | 
|             } | 
|         } | 
|     } |