コード例 #1
0
ファイル: GText.cs プロジェクト: frozen4/UnityPlus
    public void OnPointerClick(PointerEventData eventData)
    {
        if (OnClick == null)
        {
            return;
        }

        Vector2 lp;

        RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out lp);
        var clickLinkId = -1;

        for (int i = 0; i < _TextLinkModels.Count; i++)
        {
            GTextLinkModel linkModel = _TextLinkModels[i];
            for (int j = 0; j < linkModel.Rects.Count; ++j)
            {
                if (linkModel.Rects[j].Contains(lp))
                {
                    clickLinkId = i;
                    break;
                }
            }
        }
        //if (clickLinkId != -1)
        OnClick(_TextID, clickLinkId + 1);
    }
コード例 #2
0
ファイル: GText.cs プロジェクト: frozen4/UnityPlus
    public override void SetVerticesDirty()
    {
        //收集富文本信息
        _OriginText = m_Text;
        MatchCollection collections = _TextRegex.Matches(_OriginText);

        _GraphicItemModels.Clear();
        _TextLinkModels.Clear();
        int last_index  = 0;
        int start_index = 0;
        var sb          = HobaText.GetStringBuilder();

        for (var i = 0; i < collections.Count; i++)
        {
            Match  match       = collections[i];
            int    match_index = match.Index;
            string type        = match.Groups[1].Value;
            if (type == ID_EMOTION)
            {
                //TODO: 改成参数
                int my_size = (int)(this.fontSize * GraphicScale);

                sb.Append(_OriginText.Substring(last_index, match_index - last_index));
                start_index = sb.Length;
                string quad = HobaText.Format(TEXT_QUAD, match.Groups[2].Value, (int)(fontSize), (GraphicScale));
                sb.Append(quad);
                GTextEmotionModel model = new GTextEmotionModel();
                model.TextIndex = start_index;
                model.Size      = my_size;

                model.SpriteName = TranslateEmoji(match.Groups[2].Value);

                _GraphicItemModels.Add(model);
            }
            else if (type == ID_LINK)
            {
                string seg = _OriginText.Substring(last_index, match_index - last_index);
                sb.Append(seg);
                start_index = sb.Length;

                GTextLinkModel linkModel = new GTextLinkModel();

                //string link_txt;
                string inner_txt;
                string color_text;

                //colorful text
                MatchCollection collections_con = _LinkRegex.Matches(match.Groups[2].Value);
                if (collections_con.Count == 1 && collections_con[0].Groups.Count == 3)
                {
                    //link_txt = string.Format(TEXT_LINK, collections_con[0].Groups[1].Value, collections_con[0].Groups[2].Value);
                    color_text = collections_con[0].Groups[1].Value;
                    inner_txt  = collections_con[0].Groups[2].Value;
                }
                else
                {
                    //link_txt = string.Format(TEXT_LINK, "yellow", match.Groups[2].Value);
                    color_text = "yellow";
                    inner_txt  = match.Groups[2].Value;
                }

                sb.AppendFormat("<color={0}>", color_text);
                linkModel.StartIndex = sb.Length - 1;
                sb.AppendFormat(inner_txt);
                linkModel.EndIndex = sb.Length - 1;
                sb.Append("</color>");

                //linkModel.Text = inner_txt;

                _TextLinkModels.Add(linkModel);
            }
            last_index = match_index + match.Value.Length;
        }
        sb.Append(_OriginText.Substring(last_index, _OriginText.Length - last_index));
        _BuildText = sb.ToString();

        //重绘请求
        base.SetVerticesDirty();
    }
コード例 #3
0
ファイル: GText.cs プロジェクト: frozen4/UnityPlus
    protected override void OnPopulateMesh(VertexHelper toFill)
    {
        m_Text = _BuildText;
        base.OnPopulateMesh(toFill);//生成文本网格
        //↓ 根据富文本信息做后续处理
        int i, j, startIndex, indics;

        for (i = 0; i < _GraphicItemModels.Count; i++)
        {
            GTextEmotionModel model = _GraphicItemModels[i];
            startIndex = model.TextIndex * 4;

            for (j = 0; j < 4; j++)
            {
                //清楚乱码
                indics = startIndex + j;
                if (indics >= toFill.currentVertCount)
                {
                    break;
                }

                toFill.PopulateUIVertex(ref vert, indics);
                vert.uv0       = vert.uv1 = Vector2.zero;
                vert.position += new Vector3(GraphicOffset.x, GraphicOffset.y * 0.5f, GraphicOffset.z);

                toFill.SetUIVertex(vert, indics);

                //把quad的定点位置存下来在表情绘制的时候使用
                //只需要知道对角线的两个定点就能确定这个矩形
                if (j == 0)
                {
                    model.PosMin = vert.position + new Vector3(0, (GraphicScale - 1) / 2 * fontSize, 0);
                }
                if (j == 2)
                {
                    model.PosMax = vert.position + new Vector3(0, -(GraphicScale - 1) / 2 * fontSize, 0);
                }
            }
        }
        if (_GraphicBoard != null)
        {
            _GraphicBoard.SetVertexData(_GraphicItemModels);
        }

        for (i = 0; i < _TextLinkModels.Count; i++)
        {
            GTextLinkModel linkModel = _TextLinkModels[i];
            startIndex = linkModel.StartIndex * 4;
            int endIndex = linkModel.EndIndex * 4 + 3;
            if (startIndex >= toFill.currentVertCount)
            {
                continue;
            }
            toFill.PopulateUIVertex(ref vert, startIndex);
            Vector3 pos       = vert.position;
            Vector3 last_char = pos;
            Bounds  bounds    = new Bounds(pos, Vector3.zero);
            for (j = startIndex + 2; j < endIndex; j += 2)
            {
                if (j >= toFill.currentVertCount)
                {
                    break;
                }

                toFill.PopulateUIVertex(ref vert, j);
                pos = vert.position;

                if (j % 4 == 0)
                {
                    if (pos.x < last_char.x)
                    {
                        if (bounds.size.x > 0)
                        {
                            linkModel.AddRect(new Rect(bounds.min, bounds.size));
                        }
                        bounds = new Bounds(pos, Vector3.zero);
                    }
                    else
                    {
                        bounds.Encapsulate(pos); // 扩展包围框
                    }

                    last_char = pos;
                }
                else
                {
                    bounds.Encapsulate(pos); // 扩展包围框
                }
            }

            if (bounds.size.x > 0)
            {
                linkModel.AddRect(new Rect(bounds.min, bounds.size));
            }
        }
        m_Text = _OriginText;
    }