Esempio n. 1
0
        bool HandleText(RichElementText element, RichElementTextProxy proxy)
        {
            if (proxy != null)
            {
                return(HandleTextRecur(element.GetText(), element.GetStyle(), element.GetClickHandler(), element, proxy));
            }

            return(false);
        }
Esempio n. 2
0
        // NOTE now we always anchor of game object is at the top-left,
        //      also we assume rich text anchor is at the top-left
        // TODO improve
        bool HandleTextRecur(string text, string style, Action clickHandler, RichElementText element, RichElementTextProxy proxy)
        {
            // param check
            if (string.IsNullOrEmpty(text))
            {
                return(true);
            }

            if (m_leftWidth <= 0)
            {
                HandleNewline();
            }

            var textGO = proxy.Create(text, style, clickHandler);

            if (!textGO)
            {
                Debug.LogError("[RichText]Unable to create rich element : " + element.ToString());
                return(false);
            }

            // NOTE we need to add it to textGO immediately for retrieving correct size
            textGO.transform.SetParent(transform, false);
            AddElementRenderer(element, textGO);

            var size = proxy.GetSize(textGO);

            if (size.x < m_leftWidth)
            {
                // enough width space, just add game object
                textGO.transform.localPosition = m_renderPos;

                // update control data
                var oldMaxHeight = m_maxHeight;
                m_maxHeight  = size.y > m_maxHeight ? size.y : m_maxHeight;
                m_textHeight = m_textHeight - oldMaxHeight + m_maxHeight;
                m_leftWidth -= size.x;
                m_renderPos += new Vector3(size.x, 0, 0);

                return(true);
            }
            else
            {
                // not enough space, have to adjust
                float factor       = m_leftWidth / size.x;
                int   subStrLength = Mathf.FloorToInt(factor * text.Length);
                if (subStrLength <= 0)
                {
                    HandleNewline();

                    // recalculate sub string length
                    factor       = m_leftWidth / size.x;
                    factor       = Mathf.Min(factor, 1);
                    subStrLength = Mathf.FloorToInt(factor * text.Length);

                    // still not enough space, seems ill situation
                    if (subStrLength <= 0)
                    {
                        // NOTE not so sure about this ...
                        Debug.LogWarning("[RichText]Incorrect to handle rich text here, seems width is too small ...");
                        // we reset sub string length here
                        // NOTE not so sure about this ...
                        subStrLength = 1;
                    }
                }

                string subStr = text.Substring(0, subStrLength);
                proxy.SetText(textGO, subStr);

                // check width after adjust text
                var adjustSize = proxy.GetSize(textGO);
                if (adjustSize.x > m_leftWidth)
                {
                    // still out of width
                    for (int i = subStrLength - 1; i > 0; --i)
                    {
                        subStr = text.Substring(0, i);
                        proxy.SetText(textGO, subStr);
                        adjustSize = proxy.GetSize(textGO);
                        if (adjustSize.x <= m_leftWidth)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    // enough space, but we need to search most adjust
                    for (int i = subStrLength + 1; i < text.Length; ++i)
                    {
                        subStr = text.Substring(0, i);
                        proxy.SetText(textGO, subStr);
                        adjustSize = proxy.GetSize(textGO);
                        if (adjustSize.x > m_leftWidth)
                        {
                            // roll back
                            subStr = text.Substring(0, i - 1);
                            proxy.SetText(textGO, subStr);
                            adjustSize = proxy.GetSize(textGO);
                            break;
                        }
                    }
                }

                textGO.transform.localPosition = m_renderPos;

                // update control data
                m_renderPos += new Vector3(adjustSize.x, 0, 0);
                var oldMaxHeight = m_maxHeight;
                m_maxHeight  = size.y > m_maxHeight ? size.y : m_maxHeight;
                m_textHeight = m_textHeight - oldMaxHeight + m_maxHeight;
                m_leftWidth -= adjustSize.x;

                return(HandleTextRecur(text.Substring(subStr.Length, text.Length - subStr.Length), style, clickHandler, element, proxy));
            }
        }