Beispiel #1
0
        public void CreateFontChars()
        {
            int nextFontPositionX = 0;
            int nextFontPositionY = 0;
            char prev = (char)255;
            int kerningAmount = 0;

            CCSize tmpSize = CCSize.Zero;

            int longestLine = 0;
            int totalHeight = 0;
            int quantityOfLines = 1;
            if (string.IsNullOrEmpty(this._text))
            {
                return;
            }

            int textLength = this._text.Length;
            var charSet = this._fontConfig.CharacterSet;
            if (charSet.Count == 0)
            {
                throw (new InvalidOperationException("Can not compute the size of the font because the character set is empty."));
            }

            for (int i = 0; i < textLength - 1; ++i)
            {
                if (this._text[i] == '\n')
                    quantityOfLines++;
            }

            totalHeight = this._fontConfig.CommonHeight * quantityOfLines;
            nextFontPositionY = 0 - (this._fontConfig.CommonHeight - this._fontConfig.CommonHeight * quantityOfLines);

            CCBMFontDef fontDef = null;
            CCRect rect;

            for (int i = 0; i < textLength; i++)
            {
                char c = this._text[i];
                if (c == '\n')
                {
                    nextFontPositionX = 0;
                    nextFontPositionY -= _fontConfig.CommonHeight;
                    continue;
                }

                if (charSet.IndexOf(c) == -1)
                {
                    CCLog.Log("Cocos2D.CCLabelBMFont: Attempted to use character not defined in this bitmap: {0}", (int)c);
                    continue;
                }

                kerningAmount = this._kerningAmountForFirst(prev, c);

                // unichar is a short, and an int is needed on HASH_FIND_INT
                if (this._fontConfig.FontDefDictionary.TryGetValue(c, out fontDef) == false)
                {
                    CCLog.Log("cocos2d::CCLabelBMFont: characer not found {0}", (int)c);
                    continue;
                }

                rect = fontDef.rect;
                rect = rect.PixelsToPoints();
                rect.Origin.X += _imageOffset.X;
                rect.Origin.Y += _imageOffset.Y;

                CCSprite fontChar = (CCSprite)base.GetChildByTag(i);
                if (fontChar != null)
                {
                    // Reusing previous Sprite
                    fontChar.Visible = true;
                }
                else
                {
                    fontChar = new CCSprite();
                    fontChar.InitWithTexture(base.TextureAtlas.Texture, rect);
                    AddChild(fontChar, i, i);
                    fontChar.IsOpacityModifyRGB = this._isOpacityModifyRGB;
                    fontChar.UpdateDisplayedColor(this._displayedColor);
                    fontChar.UpdateDisplayedOpacity(this._displayedOpacity);
                }
                // updating previous sprite
                fontChar.SetTextureRect(rect, false, rect.Size);

                // See issue 1343. cast( signed short + unsigned integer ) == unsigned integer (sign is lost!)
                int yOffset = this._fontConfig.CommonHeight - fontDef.yOffset;
                var fontPos = new CCPoint(
                    (float)nextFontPositionX + fontDef.xOffset + fontDef.rect.Size.Width * 0.5f + kerningAmount,
                    (float)nextFontPositionY + yOffset - rect.Size.Height * 0.5f * CCMacros.CCContentScaleFactor()
                    );
                fontChar.Position = fontPos.PixelsToPoints();

                // update kerning
                nextFontPositionX += fontDef.xAdvance + kerningAmount;
                prev = c;

                if (longestLine < nextFontPositionX)
                {
                    longestLine = nextFontPositionX;
                }
            }

            // If the last character processed has an xAdvance which is less that the width of the characters image,
            // then we need to adjust the width of the string to take this into account,
            // or the character will overlap the end of the bounding box
            if (fontDef.xAdvance < fontDef.rect.Size.Width)
            {
                tmpSize.Width = longestLine + fontDef.rect.Size.Width - fontDef.xAdvance;
            }
            else
            {
                tmpSize.Width = longestLine;
            }
            tmpSize.Height = totalHeight;
            tmpSize = new CCSize(
                this._dimensions.Width > 0 ? this._dimensions.Width : tmpSize.Width,
                this._dimensions.Height > 0 ? this._dimensions.Height : tmpSize.Height
                );
            ContentSize = tmpSize.PixelsToPoints();
        }
Beispiel #2
0
        public void CreateFontChars()
        {
            int nextFontPositionX = 0;
            int nextFontPositionY = 0;
            char prev = (char) 255;
            int kerningAmount = 0;

            CCSize tmpSize = CCSize.Zero;

            int longestLine = 0;
            int totalHeight = 0;

            int quantityOfLines = 1;

            if (String.IsNullOrEmpty(m_sString))
            {
                return;
            }

            int stringLen = m_sString.Length;

            var charSet = m_pConfiguration.CharacterSet;
            if (charSet.Count == 0)
            {
                throw (new InvalidOperationException(
                    "Can not compute the size of the font because the character set is empty."));
            }

            for (int i = 0; i < stringLen - 1; ++i)
            {
                if (m_sString[i] == '\n')
                {
                    quantityOfLines++;
                }
            }

            totalHeight = m_pConfiguration.m_nCommonHeight * quantityOfLines;
            nextFontPositionY = 0 -
                                (m_pConfiguration.m_nCommonHeight - m_pConfiguration.m_nCommonHeight * quantityOfLines);

            CCBMFontConfiguration.CCBMFontDef fontDef = null;
            CCRect rect;

            for (int i = 0; i < stringLen; i++)
            {
                char c = m_sString[i];

                if (c == '\n')
                {
                    nextFontPositionX = 0;
                    nextFontPositionY -= m_pConfiguration.m_nCommonHeight;
                    continue;
                }

                if (charSet.IndexOf(c) == -1)
                {
                    CCLog.Log("Cocos2D.CCLabelBMFont: Attempted to use character not defined in this bitmap: {0}",
                              (int) c);
                    continue;
                }

                kerningAmount = this.KerningAmountForFirst(prev, c);

                // unichar is a short, and an int is needed on HASH_FIND_INT
                if (!m_pConfiguration.m_pFontDefDictionary.TryGetValue(c, out fontDef))
                {
                    CCLog.Log("cocos2d::CCLabelBMFont: characer not found {0}", (int) c);
                    continue;
                }

                rect = fontDef.rect;
                rect = CCMacros.CCRectanglePixelsToPoints(rect);

                rect.Origin.X += m_tImageOffset.X;
                rect.Origin.Y += m_tImageOffset.Y;

                CCSprite fontChar;

                //bool hasSprite = true;
                fontChar = (CCSprite) (GetChildByTag(i));
                if (fontChar != null)
                {
                    // Reusing previous Sprite
                    fontChar.Visible = true;
                }
                else
                {
                    // New Sprite ? Set correct color, opacity, etc...
                    //if( false )
                    //{
                    //    /* WIP: Doesn't support many features yet.
                    //     But this code is super fast. It doesn't create any sprite.
                    //     Ideal for big labels.
                    //     */
                    //    fontChar = m_pReusedChar;
                    //    fontChar.BatchNode = null;
                    //    hasSprite = false;
                    //}
                    //else
                    {
                        fontChar = new CCSprite();
                        fontChar.InitWithTexture(m_pobTextureAtlas.Texture, rect);
                        AddChild(fontChar, i, i);
                    }

                    // Apply label properties
                    fontChar.IsOpacityModifyRGB = m_bIsOpacityModifyRGB;

                    // Color MUST be set before opacity, since opacity might change color if OpacityModifyRGB is on
                    fontChar.UpdateDisplayedColor(m_tDisplayedColor);
                    fontChar.UpdateDisplayedOpacity(m_cDisplayedOpacity);
                }

                // updating previous sprite
                fontChar.SetTextureRect(rect, false, rect.Size);

                // See issue 1343. cast( signed short + unsigned integer ) == unsigned integer (sign is lost!)
                int yOffset = m_pConfiguration.m_nCommonHeight - fontDef.yOffset;
                var fontPos =
                    new CCPoint(
                        (float) nextFontPositionX + fontDef.xOffset + fontDef.rect.Size.Width * 0.5f + kerningAmount,
                        (float) nextFontPositionY + yOffset - rect.Size.Height * 0.5f * CCMacros.CCContentScaleFactor());
                fontChar.Position = CCMacros.CCPointPixelsToPoints(fontPos);

                // update kerning
                nextFontPositionX += fontDef.xAdvance + kerningAmount;
                prev = c;

                if (longestLine < nextFontPositionX)
                {
                    longestLine = nextFontPositionX;
                }

                //if (! hasSprite)
                //{
                //  UpdateQuadFromSprite(fontChar, i);
                //}
            }

            // If the last character processed has an xAdvance which is less that the width of the characters image, then we need
            // to adjust the width of the string to take this into account, or the character will overlap the end of the bounding
            // box
            if (fontDef.xAdvance < fontDef.rect.Size.Width)
            {
                tmpSize.Width = longestLine + fontDef.rect.Size.Width - fontDef.xAdvance;
            }
            else
            {
                tmpSize.Width = longestLine;
            }
            tmpSize.Height = totalHeight;

            tmpSize = new CCSize(
                m_tDimensions.Width > 0 ? m_tDimensions.Width : tmpSize.Width,
                m_tDimensions.Height > 0 ? m_tDimensions.Height : tmpSize.Height
                );

            ContentSize = CCMacros.CCSizePixelsToPoints(tmpSize);
        }
Beispiel #3
0
        public void CreateFontChars()
        {
            int  nextFontPositionX = 0;
            int  nextFontPositionY = 0;
            char prev          = (char)255;
            int  kerningAmount = 0;

            CCSize tmpSize = CCSize.Zero;

            int longestLine = 0;
            int totalHeight = 0;

            int quantityOfLines = 1;

            if (String.IsNullOrEmpty(m_sString))
            {
                return;
            }

            int stringLen = m_sString.Length;

            var charSet = m_pConfiguration.CharacterSet;

            if (charSet.Count == 0)
            {
                throw (new InvalidOperationException(
                           "Can not compute the size of the font because the character set is empty."));
            }

            for (int i = 0; i < stringLen - 1; ++i)
            {
                if (m_sString[i] == '\n')
                {
                    quantityOfLines++;
                }
            }

            totalHeight       = m_pConfiguration.m_nCommonHeight * quantityOfLines;
            nextFontPositionY = 0 -
                                (m_pConfiguration.m_nCommonHeight - m_pConfiguration.m_nCommonHeight * quantityOfLines);

            CCBMFontConfiguration.CCBMFontDef fontDef = null;
            CCRect rect;

            for (int i = 0; i < stringLen; i++)
            {
                char c = m_sString[i];

                if (c == '\n')
                {
                    nextFontPositionX  = 0;
                    nextFontPositionY -= m_pConfiguration.m_nCommonHeight;
                    continue;
                }

                if (charSet.IndexOf(c) == -1)
                {
                    CCLog.Log("Cocos2D.CCLabelBMFont: Attempted to use character not defined in this bitmap: {0}",
                              (int)c);
                    continue;
                }

                kerningAmount = this.KerningAmountForFirst(prev, c);

                // unichar is a short, and an int is needed on HASH_FIND_INT
                if (!m_pConfiguration.m_pFontDefDictionary.TryGetValue(c, out fontDef))
                {
                    CCLog.Log("cocos2d::CCLabelBMFont: characer not found {0}", (int)c);
                    continue;
                }

                rect = fontDef.rect;
                rect = CCMacros.CCRectanglePixelsToPoints(rect);

                rect.Origin.X += m_tImageOffset.X;
                rect.Origin.Y += m_tImageOffset.Y;

                CCSprite fontChar;

                //bool hasSprite = true;
                fontChar = (CCSprite)(GetChildByTag(i));
                if (fontChar != null)
                {
                    // Reusing previous Sprite
                    fontChar.Visible = true;
                }
                else
                {
                    // New Sprite ? Set correct color, opacity, etc...
                    //if( false )
                    //{
                    //    /* WIP: Doesn't support many features yet.
                    //     But this code is super fast. It doesn't create any sprite.
                    //     Ideal for big labels.
                    //     */
                    //    fontChar = m_pReusedChar;
                    //    fontChar.BatchNode = null;
                    //    hasSprite = false;
                    //}
                    //else
                    {
                        fontChar = new CCSprite();
                        fontChar.InitWithTexture(m_pobTextureAtlas.Texture, rect);
                        AddChild(fontChar, i, i);
                    }

                    // Apply label properties
                    fontChar.IsOpacityModifyRGB = m_bIsOpacityModifyRGB;

                    // Color MUST be set before opacity, since opacity might change color if OpacityModifyRGB is on
                    fontChar.UpdateDisplayedColor(m_tDisplayedColor);
                    fontChar.UpdateDisplayedOpacity(m_cDisplayedOpacity);
                }

                // updating previous sprite
                fontChar.SetTextureRect(rect, false, rect.Size);

                // See issue 1343. cast( signed short + unsigned integer ) == unsigned integer (sign is lost!)
                int yOffset = m_pConfiguration.m_nCommonHeight - fontDef.yOffset;
                var fontPos =
                    new CCPoint(
                        (float)nextFontPositionX + fontDef.xOffset + fontDef.rect.Size.Width * 0.5f + kerningAmount,
                        (float)nextFontPositionY + yOffset - rect.Size.Height * 0.5f * CCMacros.CCContentScaleFactor());
                fontChar.Position = CCMacros.CCPointPixelsToPoints(fontPos);

                // update kerning
                nextFontPositionX += fontDef.xAdvance + kerningAmount;
                prev = c;

                if (longestLine < nextFontPositionX)
                {
                    longestLine = nextFontPositionX;
                }

                //if (! hasSprite)
                //{
                //  UpdateQuadFromSprite(fontChar, i);
                //}
            }

            // If the last character processed has an xAdvance which is less that the width of the characters image, then we need
            // to adjust the width of the string to take this into account, or the character will overlap the end of the bounding
            // box
            if (fontDef.xAdvance < fontDef.rect.Size.Width)
            {
                tmpSize.Width = longestLine + fontDef.rect.Size.Width - fontDef.xAdvance;
            }
            else
            {
                tmpSize.Width = longestLine;
            }
            tmpSize.Height = totalHeight;

            tmpSize = new CCSize(
                m_tDimensions.Width > 0 ? m_tDimensions.Width : tmpSize.Width,
                m_tDimensions.Height > 0 ? m_tDimensions.Height : tmpSize.Height
                );

            ContentSize = CCMacros.CCSizePixelsToPoints(tmpSize);
        }