Ejemplo n.º 1
0
        void CreateSpriteWithFontDefinition(CCFontDefinition fontDefinition)
        {
            var texture = CreateTextSprite(Text, fontDefinition);

            textSprite = new CCSprite(texture);

            textSprite.AnchorPoint = CCPoint.AnchorLowerLeft;
            ContentSize            = textSprite.ContentSize;

            base.AddChild(textSprite, 0, TagInvalid);

            textSprite.UpdateDisplayedColor(DisplayedColor);
            textSprite.UpdateDisplayedOpacity(DisplayedOpacity);
        }
Ejemplo n.º 2
0
        void CreateSpriteWithFontDefinition(CCFontDefinition fontDefinition)
        {
            if (textSprite != null)
            {
                textSprite.RemoveFromParent();
            }

            var texture = CreateTextSprite(Text, fontDefinition);

            textSprite = new CCSprite(texture);
            textSprite.IsAntialiased = isAntialiased;
            textSprite.BlendFunc     = BlendFunc;
            textSprite.AnchorPoint   = CCPoint.AnchorLowerLeft;
            base.ContentSize         = textSprite.ContentSize;

            base.AddChild(textSprite, 0, TagInvalid);

            textSprite.UpdateDisplayedColor(DisplayedColor);
            textSprite.UpdateDisplayedOpacity(DisplayedOpacity);
        }
Ejemplo n.º 3
0
        void CreateSpriteWithFontDefinition(CCFontDefinition fontDefinition)
        {

            if (textSprite != null)
                textSprite.RemoveFromParent();

            var texture = CreateTextSprite(Text, fontDefinition);

            textSprite = new CCSprite(texture);
            textSprite.IsAntialiased = isAntialiased;
            textSprite.BlendFunc = BlendFunc;
            textSprite.AnchorPoint = CCPoint.AnchorLowerLeft;
            base.ContentSize = textSprite.ContentSize;

            base.AddChild(textSprite,0,TagInvalid);

            textSprite.UpdateDisplayedColor(DisplayedColor);
            textSprite.UpdateDisplayedOpacity(DisplayedOpacity);
        }
Ejemplo n.º 4
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(labelText))
            {
                return;
            }

            int stringLen = labelText.Length;

            var charSet = FontConfiguration.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 (labelText[i] == '\n')
                {
                    quantityOfLines++;
                }
            }

            LineHeight = FontConfiguration.CommonHeight;

            totalHeight       = LineHeight * quantityOfLines;
            nextFontPositionY = 0 -
                                (LineHeight - LineHeight * quantityOfLines);

            CCBMFontConfiguration.CCBMGlyphDef fontDef = null;
            CCRect fontCharTextureRect;
            CCSize fontCharContentSize;

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

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

                if (charSet.IndexOf(c) == -1)
                {
                    CCLog.Log("CocosSharp: 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 (!FontConfiguration.Glyphs.TryGetValue(c, out fontDef))
                {
                    CCLog.Log("CocosSharp: CCLabelBMFont: characer not found {0}", (int)c);
                    continue;
                }

                fontCharTextureRect           = fontDef.Subrect;
                fontCharTextureRect.Origin.X += ImageOffset.X;
                fontCharTextureRect.Origin.Y += ImageOffset.Y;

                fontCharContentSize = fontCharTextureRect.Size / DefaultTexelToContentSizeRatios;

                CCSprite fontChar;

                //bool hasSprite = true;
                fontChar = (CCSprite)(this[i]);
                if (fontChar != null)
                {
                    // Reusing previous Sprite
                    fontChar.Visible = true;

                    // updating previous sprite
                    fontChar.IsTextureRectRotated = false;
                    fontChar.ContentSize          = fontCharContentSize;
                    fontChar.TextureRectInPixels  = fontCharTextureRect;
                }
                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(TextureAtlas.Texture, fontCharTextureRect);
                        fontChar.ContentSize = fontCharContentSize;
                        AddChild(fontChar, i, i);
                    }

                    // Apply label properties
                    fontChar.IsColorModifiedByOpacity = IsColorModifiedByOpacity;

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

                // See issue 1343. cast( signed short + unsigned integer ) == unsigned integer (sign is lost!)
                int yOffset = FontConfiguration.CommonHeight - fontDef.YOffset;

                var fontPos =
                    new CCPoint(
                        (float)nextFontPositionX + fontDef.XOffset + fontDef.Subrect.Size.Width * 0.5f + kerningAmount,
                        (float)nextFontPositionY + yOffset - fontCharTextureRect.Size.Height * 0.5f);

                fontChar.Position = fontPos;

                // 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.Subrect.Size.Width)
            {
                tmpSize.Width = longestLine + fontDef.Subrect.Size.Width - fontDef.XAdvance;
            }
            else
            {
                tmpSize.Width = longestLine;
            }

            tmpSize.Height = totalHeight;
            var tmpDimensions = labelDimensions;

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

            ContentSize     = labelDimensions;
            labelDimensions = tmpDimensions;
        }
Ejemplo n.º 5
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(labelText))
            {
                return;
            }

            int stringLen = labelText.Length;

            var charSet = FontConfiguration.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 (labelText[i] == '\n')
                {
                    quantityOfLines++;
                }
            }

            LineHeight = FontConfiguration.CommonHeight;

            totalHeight = LineHeight * quantityOfLines;
            nextFontPositionY = 0 -
                (LineHeight - LineHeight * quantityOfLines);

            CCBMFontConfiguration.CCBMGlyphDef fontDef = null;
            CCRect fontCharTextureRect;
            CCSize fontCharContentSize;

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

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

                if (charSet.IndexOf(c) == -1)
                {
                    CCLog.Log("CocosSharp: 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 (!FontConfiguration.Glyphs.TryGetValue(c, out fontDef))
                {
                    CCLog.Log("CocosSharp: CCLabelBMFont: characer not found {0}", (int)c);
                    continue;
                }

                fontCharTextureRect = fontDef.Subrect;
                fontCharTextureRect.Origin.X += ImageOffset.X;
                fontCharTextureRect.Origin.Y += ImageOffset.Y;

                fontCharContentSize = fontCharTextureRect.Size / DefaultTexelToContentSizeRatios;

                CCSprite fontChar;

                //bool hasSprite = true;
                fontChar = (CCSprite)(this[i]);
                if (fontChar != null)
                {
                    // Reusing previous Sprite
                    fontChar.Visible = true;

                    // updating previous sprite
                    fontChar.IsTextureRectRotated = false;
                    fontChar.ContentSize = fontCharContentSize;
                    fontChar.TextureRectInPixels = fontCharTextureRect;

                }
                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(TextureAtlas.Texture, fontCharTextureRect);
                        fontChar.ContentSize = fontCharContentSize;
                        AddChild(fontChar, i, i);
                    }

                    // Apply label properties
                    fontChar.IsColorModifiedByOpacity = IsColorModifiedByOpacity;

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

                // See issue 1343. cast( signed short + unsigned integer ) == unsigned integer (sign is lost!)
                int yOffset = FontConfiguration.CommonHeight - fontDef.YOffset;

                var fontPos =
                    new CCPoint(
                        (float)nextFontPositionX + fontDef.XOffset + fontDef.Subrect.Size.Width * 0.5f + kerningAmount,
                        (float)nextFontPositionY + yOffset - fontCharTextureRect.Size.Height * 0.5f);

                fontChar.Position = fontPos;

                // 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.Subrect.Size.Width)
            {
                tmpSize.Width = longestLine + fontDef.Subrect.Size.Width - fontDef.XAdvance;
            }
            else
            {
                tmpSize.Width = longestLine;
            }

            tmpSize.Height = totalHeight;
            var tmpDimensions = labelDimensions;

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

            ContentSize = labelDimensions;
            labelDimensions = tmpDimensions;
        }