少年修仙传客户端代码仓库
client_linchunjie
2018-09-14 a0ede150686a218c92b901b1f20aef12a9913890
System/Chat/ChatFriend.cs
@@ -1,152 +1,152 @@
//--------------------------------------------------------
//    [Author]:           第二世界
//    [  Date ]:           Wednesday, September 13, 2017
//--------------------------------------------------------
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using System;
using System.Text.RegularExpressions;
namespace Snxxz.UI
{
    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;
            ChatCtrl.Inst.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()
        {
            ChatCtrl.OnRefreshPteChat += OnRefreshPteChat;
            RefreshChatInfo();
            open = true;
            ChatCtrl.Inst.lockUpdate = true;
            OnSetLock();
        }
        public void OnSetLock()
        {
            m_Controller.lockType = ChatCtrl.Inst.lockUpdate ? EnhanceLockType.LockVerticalBottom : EnhanceLockType.KeepVertical;
            if (ChatCtrl.Inst.lockUpdate)
            {
                m_Controller.ResetScrollPos();
            }
        }
        private void OnDisable()
        {
            ChatCtrl.OnRefreshPteChat -= OnRefreshPteChat;
            open = false;
        }
        public void RefreshChatInfo()
        {
            int id = ChatCtrl.Inst.PteChatID;
            chatList = ChatCtrl.Inst.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, ChatCtrl.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 != ChatCtrl.Inst.PteChatID && data.player != ChatCtrl.Inst.PteChatID)
            {
                return;
            }
            if (data.player == PlayerDatas.Instance.baseData.PlayerID)
            {
            }
            RefreshChatInfo();
        }
    }
}
//--------------------------------------------------------
//    [Author]:           第二世界
//    [  Date ]:           Wednesday, September 13, 2017
//--------------------------------------------------------
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using System;
using System.Text.RegularExpressions;
namespace Snxxz.UI
{
    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;
            ChatCtrl.Inst.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()
        {
            ChatCtrl.OnRefreshPteChat += OnRefreshPteChat;
            RefreshChatInfo();
            open = true;
            ChatCtrl.Inst.lockUpdate = true;
            OnSetLock();
        }
        public void OnSetLock()
        {
            m_Controller.lockType = ChatCtrl.Inst.lockUpdate ? EnhanceLockType.LockVerticalBottom : EnhanceLockType.KeepVertical;
            if (ChatCtrl.Inst.lockUpdate)
            {
                m_Controller.ResetScrollPos();
            }
        }
        private void OnDisable()
        {
            ChatCtrl.OnRefreshPteChat -= OnRefreshPteChat;
            open = false;
        }
        public void RefreshChatInfo()
        {
            int id = ChatCtrl.Inst.PteChatID;
            chatList = ChatCtrl.Inst.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, ChatCtrl.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 != ChatCtrl.Inst.PteChatID && data.player != ChatCtrl.Inst.PteChatID)
            {
                return;
            }
            if (data.player == PlayerDatas.Instance.baseData.PlayerID)
            {
            }
            RefreshChatInfo();
        }
    }
}