lcy
2025-12-03 b9a6e7e896b451e9c915e782a1789b2afe079cc9
Main/System/Chat/ChatBubbleBehaviour.cs
@@ -1,257 +1,223 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
    [DisallowMultipleComponent]
    [RequireComponent(typeof(RectTransform))]
    [ExecuteAlways]
    public class ChatBubbleBehaviour : MonoBehaviour
[DisallowMultipleComponent]
[RequireComponent(typeof(RectTransform))]
[ExecuteAlways]
public class ChatBubbleBehaviour : MonoBehaviour
{
    [SerializeField] Text m_Target;
    [SerializeField] RectOffset m_Padding;
    [SerializeField] ImageEx m_BubbleIcon;
    [SerializeField] UIFrame m_UIFrame;
    [SerializeField] bool left = false;
    [SerializeField] bool m_PreferredWidth = false;
    RectTransform m_Rect;
    RectTransform rect
    {
        [SerializeField] Text m_Target;
        [SerializeField] RectOffset m_Padding;
        [SerializeField] FlipImage m_Flip;
        [SerializeField] Image m_BubbleIcon;
        [SerializeField] bool left = false;
        [SerializeField] bool m_PreferredWidth = false;
        [SerializeField] RectTransform m_ContainerVoice;
        const float space = 5.0f;
        private int bubbleId = 0;
        RectTransform m_Rect;
        RectTransform rect
        get
        {
            get
            if (m_Rect == null)
            {
                if (m_Rect == null)
                {
                    m_Rect = transform as RectTransform;
                }
                return m_Rect;
                m_Rect = transform as RectTransform;
            }
            return m_Rect;
        }
    }
    public RectOffset padding
    {
        get { return m_Padding; }
    }
    private void OnEnable()
    {
        Refresh();
    }
    public void DisplayContent(string content, bool _left = false)
    {
        if (m_Target == null)
        {
            return;
        }
        left = _left;
        m_PreferredWidth = true;
        var richText = m_Target as RichText;
        if (richText != null && !left)
        {
            richText.AutoNewLine = false;
        }
        m_Target.text = content;
        if (richText != null)
        {
            richText.AutoNewLine = true;
        }
        public RectOffset padding
        Refresh();
    }
    public void DisplaySysBubble(int id, Color color)
    {
        ChatBubbleData bubble;
        if (ChatManager.Instance.TryGetBubble(id, out bubble))
        {
            get { return m_Padding; }
        }
            var bubblePadding = left ? bubble.leftPadding : bubble.rifhtPadding;
            padding.top = bubblePadding.top;
            padding.left = bubblePadding.left;
            padding.right = bubblePadding.right;
            padding.bottom = bubblePadding.bottom;
        private void OnEnable()
        {
            Refresh();
        }
        public void DisplayContent(string content, bool _left = false)
        {
            if (m_Target == null)
            int resourceType = PhantasmPavilionManager.Instance.GetResourceType(PhantasmPavilionType.ChatBox, id);
            string resourceValue = PhantasmPavilionManager.Instance.GetResourceValue(PhantasmPavilionType.ChatBox, id);
            PhantasmPavilionManager.Instance.ShowChatBox(m_BubbleIcon, m_UIFrame, resourceType, resourceValue);
            m_Target.color = color;
            var position = rect.anchoredPosition;
            if (rect.anchoredPosition != position)
            {
                return;
            }
            left = _left;
            m_PreferredWidth = true;
            var targetRect = m_Target.rectTransform;
            var richText = m_Target as RichText;
            if (richText != null && !left)
            {
                richText.AutoNewLine = false;
            }
            m_Target.text = content;
            if (!left)
            {
                if (m_Target.preferredWidth > targetRect.rect.width)
                {
                    m_Target.alignment = TextAnchor.UpperLeft;
                    m_PreferredWidth = false;
                }
                else
                {
                    m_Target.alignment = TextAnchor.UpperRight;
                }
            }
            if (richText != null)
            {
                richText.AutoNewLine = true;
                rect.anchoredPosition = position;
            }
            Refresh();
        }
        public void DisplayBubble(int id)
    }
    public void DisplayBubble(int id, int playerId)
    {
        ChatBubbleData bubble;
        if (ChatManager.Instance.TryGetBubble(id, out bubble))
        {
            bubbleId = id;
            ChatBubbleManager.ChatBubble bubble;
            if (ChatBubbleManager.Instance.TryGetBubble(id, out bubble))
            var bubblePadding = left ? bubble.leftPadding : bubble.rifhtPadding;
            padding.top = bubblePadding.top;
            padding.left = bubblePadding.left;
            padding.right = bubblePadding.right;
            padding.bottom = bubblePadding.bottom;
            int resourceType = PhantasmPavilionManager.Instance.GetResourceType(PhantasmPavilionType.ChatBox, id);
            string resourceValue = PhantasmPavilionManager.Instance.GetResourceValue(PhantasmPavilionType.ChatBox, id);
            PhantasmPavilionManager.Instance.ShowChatBox(m_BubbleIcon, m_UIFrame, resourceType, resourceValue);
            if (playerId == PlayerDatas.Instance.PlayerId)
            {
                var bubblePadding = left ? bubble.leftPadding : bubble.rifhtPadding;
                padding.top = bubblePadding.top;
                padding.left = bubblePadding.left;
                padding.right = bubblePadding.right;
                padding.bottom = bubblePadding.bottom;
                bool requireFlip = false;
                var iconKey = bubble.GetBubbleIcon(left, ref requireFlip);
                //m_BubbleIcon.SetSprite(iconKey);
                UIFrame frame = m_BubbleIcon.GetComponent<UIFrame>();
                if (UIFrameMgr.Inst.ContainsDynamicImage(iconKey))
                {
                    if (frame == null)
                        frame = m_BubbleIcon.gameObject.AddComponent<UIFrame>();
                    frame.ResetFrame(iconKey);
                    frame.enabled = true;
                }
                else
                {
                    if (frame != null)
                        frame.enabled = false;
                    m_BubbleIcon.SetSprite(iconKey);
                }
                m_Flip.flipHorizontal = requireFlip;
                m_Target.color = bubble.color;
                var position = rect.anchoredPosition;
                // TODO YYL
                // ChatBubbleBoxConfig config = ChatBubbleBoxConfig.Get(bubbleId);
                // position.y = -config.top;
                if (rect.anchoredPosition != position)
                {
                    rect.anchoredPosition = position;
                }
                Refresh();
            }
        }
        [ExecuteAlways]
        private void LateUpdate()
        {
            Refresh();
        }
        void Refresh()
        {
            if (m_Target == null)
            {
                return;
            }
            bool nullContent = string.IsNullOrEmpty(m_Target.text);
            var targetRect = m_Target.rectTransform;
            var sizeDelta = targetRect.sizeDelta;
            var width = m_PreferredWidth || !Application.isPlaying ? m_Target.preferredWidth : sizeDelta.x;
            if (nullContent)
            {
                width = 0f;
            }
            var height = sizeDelta.y;
            if (nullContent)
            {
                height = 0;
            }
            if (m_ContainerVoice != null)
            {
                width = Mathf.Max(m_ContainerVoice.sizeDelta.x, width);
                height += m_ContainerVoice.sizeDelta.y;
                if (!nullContent)
                {
                    height += space;
                }
            }
            sizeDelta.x = width + m_Padding.left + m_Padding.right;
            sizeDelta.y = height + m_Padding.top + m_Padding.bottom;
            if (sizeDelta != rect.sizeDelta)
            {
                rect.sizeDelta = sizeDelta;
            }
            SetAnchor(m_Target.rectTransform);
            if (m_ContainerVoice != null)
            {
                SetAnchor(m_ContainerVoice);
            }
            float top = padding.top;
            Vector2 position = Vector2.zero;
            position.x = left ? padding.left : -padding.right;
            if (m_ContainerVoice != null)
            {
                position.y = -top;
                if (m_ContainerVoice.anchoredPosition != position)
                {
                    m_ContainerVoice.anchoredPosition = position;
                }
                top = top + m_ContainerVoice.sizeDelta.y;
                top += space;
            }
            position.y = -top;
            if (targetRect.anchoredPosition != position)
            {
                targetRect.anchoredPosition = position;
            }
        }
        void SetAnchor(RectTransform targetRect)
        {
            if (!left)
            {
                if (targetRect.anchorMin != Vector2.one)
                {
                    targetRect.anchorMin = Vector2.one;
                }
                if (targetRect.anchorMax != Vector2.one)
                {
                    targetRect.anchorMax = Vector2.one;
                }
                if (targetRect.pivot != Vector2.one)
                {
                    targetRect.pivot = Vector2.one;
                }
                m_Target.color = bubble.myColor;
            }
            else
            {
                if (targetRect.anchorMin != Vector2.up)
                {
                    targetRect.anchorMin = Vector2.up;
                }
                if (targetRect.anchorMax != Vector2.up)
                {
                    targetRect.anchorMax = Vector2.up;
                }
                if (targetRect.pivot != Vector2.up)
                {
                    targetRect.pivot = Vector2.up;
                }
                m_Target.color = bubble.otherColor;
            }
            var position = rect.anchoredPosition;
            position.y = -bubble.top;
            if (rect.anchoredPosition != position)
            {
                rect.anchoredPosition = position;
            }
            Refresh();
        }
    }
    [ExecuteAlways]
    private void LateUpdate()
    {
        Refresh();
    }
    void Refresh()
    {
        if (m_Target == null)
        {
            return;
        }
        bool nullContent = string.IsNullOrEmpty(m_Target.text);
        var targetRect = m_Target.rectTransform;
        var sizeDelta = targetRect.sizeDelta;
        var width = m_PreferredWidth || !Application.isPlaying ? m_Target.preferredWidth : sizeDelta.x;
        if (nullContent)
        {
            width = 0f;
        }
        var height = sizeDelta.y;
        if (nullContent)
        {
            height = 0;
        }
        public float GetBubbleHeight(string content, ArrayList list)
        sizeDelta.x = width + m_Padding.left + m_Padding.right;
        sizeDelta.y = height + m_Padding.top + m_Padding.bottom;
        if (sizeDelta != rect.sizeDelta)
        {
            if (m_Target is RichText)
            {
                (m_Target as RichText).SetExtenalData(list);
            }
            m_Target.text = content;
            var height = m_Target.preferredHeight;
            bool nullContent = string.IsNullOrEmpty(content);
            if (nullContent)
            {
                height = 0f;
            }
            if (m_ContainerVoice != null)
            {
                height += m_ContainerVoice.sizeDelta.y;
                if (!nullContent)
                {
                    height += space;
                }
            }
            return height + padding.top + padding.bottom;
            rect.sizeDelta = sizeDelta;
        }
    }
        SetAnchor(m_Target.rectTransform);
        float top = padding.top;
        Vector2 position = Vector2.zero;
        position.x = left ? padding.left : -padding.right;
        position.y = -top;
        if (targetRect.anchoredPosition != position)
        {
            targetRect.anchoredPosition = position;
        }
    }
    void SetAnchor(RectTransform targetRect)
    {
        if (!left)
        {
            if (targetRect.anchorMin != Vector2.one)
            {
                targetRect.anchorMin = Vector2.one;
            }
            if (targetRect.anchorMax != Vector2.one)
            {
                targetRect.anchorMax = Vector2.one;
            }
            if (targetRect.pivot != Vector2.one)
            {
                targetRect.pivot = Vector2.one;
            }
        }
        else
        {
            if (targetRect.anchorMin != Vector2.up)
            {
                targetRect.anchorMin = Vector2.up;
            }
            if (targetRect.anchorMax != Vector2.up)
            {
                targetRect.anchorMax = Vector2.up;
            }
            if (targetRect.pivot != Vector2.up)
            {
                targetRect.pivot = Vector2.up;
            }
        }
    }
    public float GetBubbleHeight(string content, ArrayList list)
    {
        if (m_Target is RichText)
        {
            (m_Target as RichText).SetExtenalData(list);
        }
        m_Target.text = content;
        var height = m_Target.preferredHeight;
        bool nullContent = string.IsNullOrEmpty(content);
        if (nullContent)
        {
            height = 0f;
        }
        //Debug.Log($"GetBubbleHeight {height + padding.top + padding.bottom} height {height} padding.top {padding.top} padding.bottom {padding.bottom}");
        return height + padding.top + padding.bottom;
    }
}