//-------------------------------------------------------- 
 | 
//    [Author]:           玩个游戏 
 | 
//    [  Date ]:           Wednesday, September 13, 2017 
 | 
//-------------------------------------------------------- 
 | 
using UnityEngine; 
 | 
using System.Collections; 
 | 
using UnityEngine.UI; 
 | 
using System.Collections.Generic; 
 | 
using System; 
 | 
using System.Text.RegularExpressions; 
 | 
  
 | 
    public class ChatFriend : MonoBehaviour 
 | 
    { 
 | 
        [SerializeField] ScrollerController m_Controller; 
 | 
  
 | 
        private List<ChatFriendData> chatList = null; 
 | 
  
 | 
        [SerializeField] RichText destText; 
 | 
        [SerializeField] RichText destSysText; 
 | 
        private bool open = false; 
 | 
        public bool IsOpen 
 | 
        { 
 | 
            get { return open; } 
 | 
        } 
 | 
  
 | 
        private void Awake() 
 | 
        { 
 | 
            m_Controller.OnGetDynamicSize += OnGetChatDynamicSize; 
 | 
            ChatManager.Instance.SetChatFreind(this); 
 | 
        } 
 | 
  
 | 
        #region 计算动态宽度长度 
 | 
        private bool OnGetChatDynamicSize(ScrollerDataType type, int index, out float height) 
 | 
        { 
 | 
            height = 90; 
 | 
            if (chatList == null || index >= chatList.Count) 
 | 
            { 
 | 
                return false; 
 | 
            } 
 | 
            ChatData chat = chatList[index]; 
 | 
            if (type == ScrollerDataType.Extra2) 
 | 
            { 
 | 
                height = chat.content.Equals(string.Empty) ? 90 : 120; 
 | 
            } 
 | 
            else if (type == ScrollerDataType.Tail) 
 | 
            { 
 | 
                height = 30; 
 | 
            } 
 | 
            float width = 0; 
 | 
            OnGetChatDynamicHeight(chat.content, ref height, ref width, type, chat.infoList); 
 | 
            return true; 
 | 
        } 
 | 
        private void OnGetChatDynamicHeight(string content, ref float height, ref float width, ScrollerDataType type, ArrayList infoList = null) 
 | 
        { 
 | 
            if (type == ScrollerDataType.Tail) 
 | 
            { 
 | 
                destSysText.SetExtenalData(infoList); 
 | 
                destSysText.text = content; 
 | 
                width = destSysText.preferredWidth; 
 | 
                height += Mathf.Max(0, destSysText.preferredHeight - 23); 
 | 
            } 
 | 
            else 
 | 
            { 
 | 
                destText.SetExtenalData(infoList); 
 | 
                destText.text = content; 
 | 
                width = destText.preferredWidth; 
 | 
                height += Mathf.Max(0, destText.preferredHeight - 30); 
 | 
            } 
 | 
        } 
 | 
        #endregion 
 | 
  
 | 
        private void OnEnable() 
 | 
        { 
 | 
            ChatManager.OnRefreshPteChat += OnRefreshPteChat; 
 | 
            RefreshChatInfo(); 
 | 
            open = true; 
 | 
            ChatManager.Instance.lockUpdate = true; 
 | 
            OnSetLock(); 
 | 
        } 
 | 
  
 | 
        public void OnSetLock() 
 | 
        { 
 | 
            m_Controller.lockType = ChatManager.Instance.lockUpdate ? EnhanceLockType.LockVerticalBottom : EnhanceLockType.KeepVertical; 
 | 
            if (ChatManager.Instance.lockUpdate) 
 | 
            { 
 | 
                m_Controller.ResetScrollPos(); 
 | 
            } 
 | 
        } 
 | 
  
 | 
        private void OnDisable() 
 | 
        { 
 | 
            ChatManager.OnRefreshPteChat -= OnRefreshPteChat; 
 | 
            open = false; 
 | 
        } 
 | 
  
 | 
        public void RefreshChatInfo() 
 | 
        { 
 | 
            int id = ChatManager.Instance.PteChatID; 
 | 
            chatList = ChatManager.Instance.GetChatInfo(id); 
 | 
            m_Controller.Refresh(); 
 | 
            if (chatList != null) 
 | 
            { 
 | 
                for (int i = 0; i < chatList.Count; i++) 
 | 
                { 
 | 
                    if (chatList[i].soundTick != 0) 
 | 
                    { 
 | 
                        m_Controller.AddCell(ScrollerDataType.Extra2, i); 
 | 
                        continue; 
 | 
                    } 
 | 
                    if (Regex.IsMatch(chatList[i].content, ChatManager.KILL_IDENTIFY)) 
 | 
                    { 
 | 
                        m_Controller.AddCell(ScrollerDataType.Tail, i); 
 | 
                        continue; 
 | 
                    } 
 | 
                    if (chatList[i].player == PlayerDatas.Instance.baseData.PlayerID) 
 | 
                    { 
 | 
                        m_Controller.AddCell(ScrollerDataType.Header, i); 
 | 
                    } 
 | 
                    else 
 | 
                    { 
 | 
                        m_Controller.AddCell(ScrollerDataType.Normal, i); 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
            m_Controller.Restart(); 
 | 
        } 
 | 
  
 | 
        private void OnRefreshPteChat(ChatFriendData data) 
 | 
        { 
 | 
            if (data == null) 
 | 
            { 
 | 
                return; 
 | 
            } 
 | 
            if (data.toPlayer != ChatManager.Instance.PteChatID && data.player != ChatManager.Instance.PteChatID) 
 | 
            { 
 | 
                return; 
 | 
            } 
 | 
            if (data.player == PlayerDatas.Instance.baseData.PlayerID) 
 | 
            { 
 | 
  
 | 
            } 
 | 
            RefreshChatInfo(); 
 | 
        } 
 | 
    } 
 |