Пример #1
0
    void  ShowTalk(string szTalk, int nColorIndex = 0)
    {
        if (nColorIndex < 0)
        {
            nColorIndex = 0;
        }
        else if (nColorIndex >= HudSetting.Instance.TalkTitle.Length)
        {
            nColorIndex = HudSetting.Instance.TalkTitle.Length - 1;
        }

        HudTitleAttribute titleAttrib = HudSetting.Instance.TalkTitle[nColorIndex];

        int          nTalkWidth = HudSetting.Instance.m_nTalkWidth;
        int          nSpriteID  = HudSetting.Instance.m_nTalkBk;
        UISpriteInfo sp         = CAtlasMng.instance.GetSafeSpriteByID(nSpriteID);
        int          nAtlasID   = sp != null ? sp.m_nAtlasID : 0;

        // 先分析文本
        UIFont       mFont    = GetHUDTitleFont();
        HUDTextParse hudParse = HUDTextParse.Instance;

        hudParse.ParseText(szTalk);
        mFont.PrepareQueryText(hudParse.m_szText);
        int nFontH = mFont.GetFontHeight();

        m_szTalk = hudParse.m_szText;

        m_HyperlinkNode.Clear();

        // 先搞背景
        for (int i = 0; i < 9; ++i)
        {
            HUDVertex node = HUDVertex.QueryVertex();
            node.WorldPos  = m_vPos;
            node.ScreenPos = m_vScreenPos;
            node.SpriteID  = nSpriteID;
            node.AtlasID   = nAtlasID;
            node.Scale     = 1f;
            node.Offset.Set(0f, 0f);
            node.Move.Set(0f, 0f);
            node.width  = (short)nTalkWidth;
            node.height = (short)100;
            m_aSprite.Add(node);
        }

        int nCharGap = titleAttrib.CharGap;
        int nLineGap = titleAttrib.LineGap;

        HudTitleAttribute.Effect nStyle = titleAttrib.Style;
        float   fShadowX     = titleAttrib.OffsetX;
        float   fShadowY     = titleAttrib.OffsetY;
        Color32 clrLeftUp    = titleAttrib.clrLeftUp;
        Color32 clrLeftDown  = titleAttrib.clrLeftDown;
        Color32 clrRightUp   = titleAttrib.clrRightUp;
        Color32 clrRightDown = titleAttrib.clrRightDown;
        Color32 clrShadow    = titleAttrib.clrShadow;
        Color32 clrCustom;

        // 计算文本的行数
        CharacterInfo tempCharInfo = new CharacterInfo();

        HUDCharInfo[] Sprites      = hudParse.m_Sprites;
        int           nSpriteCount = hudParse.m_SpriteCount;

        int  nX = 0;
        int  nY = 0;
        char ch;
        int  nCurLineHeight = nFontH;
        int  nLines         = 0;
        int  nTalkHeight    = 0;

        short[] LineHeight  = hudParse.LineHeight;
        int     nWidthMax   = 0;
        int     nSpaceWidth = 0;

        for (int i = 0; i < nSpriteCount; ++i)
        {
            if (Sprites[i].bChar)
            {
                ch = Sprites[i].ch;
                if (ch == ' ')
                {
                    if (nSpaceWidth == 0)
                    {
                        mFont.GetCharacterInfo('o', ref tempCharInfo);
                        nSpaceWidth = HUDVertex.GetCharWidth(tempCharInfo);
                    }
                    Sprites[i].SpriteWidth  = (short)nSpaceWidth;
                    Sprites[i].SpriteHeight = (short)nFontH;
                }
                else
                {
                    mFont.GetCharacterInfo(ch, ref tempCharInfo);
                    Sprites[i].SpriteWidth  = (short)HUDVertex.GetCharWidth(tempCharInfo);
                    Sprites[i].SpriteHeight = (short)tempCharInfo.glyphHeight;
                }
            }
            else
            {
                if (Sprites[i].CharType == UIFontUnitType.UnitType_Icon)
                {
                    sp = CAtlasMng.instance.GetSafeSpriteByID(Sprites[i].SpriteID);
                    if (sp != null)
                    {
                        Sprites[i].SpriteWidth  = (short)(sp.outer.width + 0.5f);
                        Sprites[i].SpriteHeight = (short)(sp.outer.height + 0.5f);
                    }
                }
                else if (Sprites[i].CharType == UIFontUnitType.UnitType_Gif)
                {
                    int nGifID = Sprites[i].SpriteID;
                    Sprites[i].SpriteWidth  = (short)UISpriteGifManager.Instance.GetSpriteGifWidth(nGifID);
                    Sprites[i].SpriteHeight = (short)UISpriteGifManager.Instance.GetSpriteGifHeight(nGifID);
                }
            }
            if (nX + Sprites[i].SpriteWidth > nTalkWidth || Sprites[i].CharType == UIFontUnitType.UnitType_Enter)
            {
                LineHeight[nLines] = (short)nCurLineHeight;
                nTalkHeight       += nCurLineHeight + nLineGap;
                nX = 0;
                nY = nTalkHeight;
                // 按下对齐
                nCurLineHeight = nFontH;
                ++nLines;
            }
            Sprites[i].nX    = nX;
            Sprites[i].nY    = nY;
            Sprites[i].nLine = nLines;
            nX += Sprites[i].SpriteWidth + nCharGap;
            if (nCurLineHeight < Sprites[i].SpriteHeight)
            {
                nCurLineHeight = Sprites[i].SpriteHeight;
            }
            if (nX > nWidthMax)
            {
                nWidthMax = nX;
            }
        }
        LineHeight[nLines] = (short)nCurLineHeight;
        nTalkHeight       += nCurLineHeight;
        nTalkWidth         = nWidthMax;
        nY = nTalkHeight;
        // 需要倒过来
        for (int i = 0; i < nSpriteCount; ++i)
        {
            Sprites[i].nY = nY - Sprites[i].nY - LineHeight[Sprites[i].nLine];
        }

        // -------- 更新
        for (int i = 0; i < nSpriteCount; ++i)
        {
            nX = Sprites[i].nX;
            nY = Sprites[i].nY;

            if (Sprites[i].bChar)
            {
                ch = Sprites[i].ch;
                mFont.GetCharacterInfo(ch, ref tempCharInfo);
                if (nStyle != HudTitleAttribute.Effect.None)
                {
                    PushShadow(ref tempCharInfo, ch, nX, nY, clrShadow, fShadowX, fShadowY);
                    if (nStyle == HudTitleAttribute.Effect.Outline)
                    {
                        PushShadow(ref tempCharInfo, ch, nX, nY, clrShadow, fShadowX, -fShadowY);
                        PushShadow(ref tempCharInfo, ch, nX, nY, clrShadow, -fShadowX, fShadowY);
                        PushShadow(ref tempCharInfo, ch, nX, nY, clrShadow, -fShadowX, -fShadowY);
                    }
                }
                if (Sprites[i].bCustomColor)
                {
                    clrCustom = Sprites[i].CustomColor;
                    HUDVertex node = PushChar(ref tempCharInfo, ch, nX, nY, clrCustom, clrCustom, clrCustom, clrCustom);
                }
                else
                {
                    HUDVertex node = PushChar(ref tempCharInfo, ch, nX, nY, clrLeftUp, clrLeftDown, clrRightUp, clrRightDown);
                }
            }
            else
            {
                // 图片
                if (Sprites[i].CharType == UIFontUnitType.UnitType_Icon)
                {
                    HUDVertex node = PushSprite(Sprites[i].SpriteID, Sprites[i].SpriteWidth, Sprites[i].SpriteHeight, nX, nY);
                }
                else if (Sprites[i].CharType == UIFontUnitType.UnitType_Gif)
                {
                    m_bHaveHyperlink = true;
                    HUDGif gif = new HUDGif();
                    gif.InitGif(Sprites[i].SpriteID);
                    HUDVertex node = PushSprite(gif.GetSpriteID(), Sprites[i].SpriteWidth, Sprites[i].SpriteHeight, nX, nY);
                    node.hudGif = gif;
                    m_HyperlinkNode.Add(node);
                }
            }
        }
        int nTalkBkBorderWidth  = HudSetting.Instance.TalkBorderWidth;
        int nTalkBkBorderHeight = HudSetting.Instance.TalkBorderHeight;
        // 填充背景气泡
        int nTalkBkWidth  = nTalkWidth + nTalkBkBorderWidth * 2;
        int nTalkBkHeight = nTalkHeight + nTalkBkBorderHeight * 2;

        for (int i = 0; i < 9; ++i)
        {
            m_aSprite[i].width  = (short)nTalkBkWidth;
            m_aSprite[i].height = (short)nTalkBkHeight;
        }
        SlicedFill(nSpriteID, nTalkBkWidth, nTalkBkHeight, 0, 1.0f);
        Offset(0, 9, -nTalkBkBorderWidth - nTalkWidth / 2, -nTalkBkBorderHeight + HudSetting.Instance.m_nTalkBkOffsetY); // Y越小,越在屏幕下面
        Offset(9, m_aSprite.size, -nTalkWidth / 2, 0.0f);

        for (int i = 0; i < m_aSprite.size; ++i)
        {
            HUDVertex v = m_aSprite[i];
            if (v.hudMesh == null)
            {
                if (v.AtlasID != 0)
                {
                    v.hudMesh = m_MeshRender.QueryMesh(v.AtlasID);
                }
                else
                {
                    v.hudMesh = m_MeshRender.FontMesh();
                }
                v.hudMesh.PushHUDVertex(v);
            }
        }
    }
Пример #2
0
    // 必须先有居中的
    public void PushTitle(string szText, HUDTilteType titleType, int nColorIndex)
    {
        if (m_nTitleNumb >= m_TitleLine.Length)
        {
            return;
        }

        UIFont        mFont        = GetHUDTitleFont();
        CharacterInfo tempCharInfo = new CharacterInfo();

        HudTitleAttribute titleAttrib = HudSetting.Instance.TitleSets[(int)titleType].GetTitle(nColorIndex);
        int nLineGap = titleAttrib.LineGap;
        int nCharGap = titleAttrib.CharGap;

        HUDTextParse hudParse = HUDTextParse.Instance;

        hudParse.ParseText(szText);
        mFont.PrepareQueryText(hudParse.m_szText);

        HudTitleAttribute.Effect nStyle = titleAttrib.Style;
        int nSpriteCount = hudParse.m_SpriteCount;

        HUDCharInfo[] Sprites = hudParse.m_Sprites;
        char          ch;
        float         fShadowX     = titleAttrib.OffsetX;
        float         fShadowY     = titleAttrib.OffsetY;
        float         fx           = 0.0f;
        Color32       clrLeftUp    = titleAttrib.clrLeftUp;
        Color32       clrLeftDown  = titleAttrib.clrLeftDown;
        Color32       clrRightUp   = titleAttrib.clrRightUp;
        Color32       clrRightDown = titleAttrib.clrRightDown;
        Color32       clrShadow    = titleAttrib.clrShadow;
        Color32       clrCustom;

        int nStart       = m_aSprite.size;
        int nHeight      = mFont.GetFontHeight();
        int nFontH       = titleAttrib.Height;
        int nFontOffsetY = titleAttrib.FontOffsetY;
        int nY           = 0;

        for (int i = 0; i < nSpriteCount; ++i)
        {
            if (Sprites[i].bChar)
            {
                ch = Sprites[i].ch;
                mFont.GetCharacterInfo(ch, ref tempCharInfo);
                nY = (tempCharInfo.glyphHeight - nFontH) / 2 + nFontOffsetY;

                if (nStyle != HudTitleAttribute.Effect.None)
                {
                    PushShadow(ref tempCharInfo, ch, fx, nY, clrShadow, fShadowX, fShadowY);
                    if (nStyle == HudTitleAttribute.Effect.Outline)
                    {
                        PushShadow(ref tempCharInfo, ch, fx, nY, clrShadow, fShadowX, -fShadowY);
                        PushShadow(ref tempCharInfo, ch, fx, nY, clrShadow, -fShadowX, fShadowY);
                        PushShadow(ref tempCharInfo, ch, fx, nY, clrShadow, -fShadowX, -fShadowY);
                    }
                }
                if (Sprites[i].bCustomColor)
                {
                    clrCustom = Sprites[i].CustomColor;
                    HUDVertex node = PushChar(ref tempCharInfo, ch, fx, nY, clrCustom, clrCustom, clrCustom, clrCustom);
                    fx += node.width + nCharGap;
                }
                else
                {
                    HUDVertex node = PushChar(ref tempCharInfo, ch, fx, nY, clrLeftUp, clrLeftDown, clrRightUp, clrRightDown);
                    fx += node.width + nCharGap;
                }
            }
            else
            {
                // 图片
                if (Sprites[i].CharType == UIFontUnitType.UnitType_Icon)
                {
                    HUDVertex node = PushSprite(Sprites[i].SpriteID, Sprites[i].SpriteWidth, Sprites[i].SpriteHeight, fx, titleAttrib.SpriteOffsetY);
                    fx += node.width + nCharGap;
                    if (nHeight < node.height - titleAttrib.SpriteReduceHeight)
                    {
                        nHeight = node.height - titleAttrib.SpriteReduceHeight;
                    }
                }
            }
        }

        if (m_TitleLine[m_nTitleNumb] == null)
        {
            m_TitleLine[m_nTitleNumb] = new HUDTilteLine();
        }
        m_TitleLine[m_nTitleNumb].m_nType       = titleType;
        m_TitleLine[m_nTitleNumb].m_fWidth      = fx - nCharGap;
        m_TitleLine[m_nTitleNumb].m_nHeight     = nHeight;
        m_TitleLine[m_nTitleNumb].m_nStart      = nStart;
        m_TitleLine[m_nTitleNumb].m_nEnd        = m_aSprite.size;
        m_TitleLine[m_nTitleNumb].m_szText      = szText;
        m_TitleLine[m_nTitleNumb].m_szValidText = hudParse.m_szText;
        m_TitleLine[m_nTitleNumb].m_nColorIndex = nColorIndex;
        m_TitleLine[m_nTitleNumb].m_nLine       = m_nLines;
        m_TitleLine[m_nTitleNumb].m_nSpriteID   = 0;

        if (titleAttrib.AlignType == HUDAlignType.align_center)
        {
            m_fCurLineWidth = fx - nCharGap;
        }
        if (nHeight < titleAttrib.LockMaxHeight && titleAttrib.LockMaxHeight > 0)
        {
            nHeight = titleAttrib.LockMaxHeight;
        }
        if (m_fCurLineHeight < nHeight)
        {
            m_fCurLineHeight = nHeight;
        }
        ++m_nTitleNumb;
    }