Ejemplo n.º 1
0
        /** initializes a CCTMXLayer with a tileset info, a layer info and a map info */

        public bool InitWithTilesetInfo(CCTMXTilesetInfo tilesetInfo, CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo)
        {
            // XXX: is 35% a good estimate ?
            CCSize size = layerInfo.LayerSize;
            float  totalNumberOfTiles = size.Width * size.Height;
            float  capacity           = totalNumberOfTiles * 0.35f + 1; // 35 percent is occupied ?

            CCTexture2D texture = null;

            if (tilesetInfo != null)
            {
                texture = CCTextureCache.SharedTextureCache.AddImage(tilesetInfo.m_sSourceImage);
            }

            if (base.InitWithTexture(texture, (int)capacity))
            {
                // layerInfo
                m_sLayerName          = layerInfo.Name;
                m_tLayerSize          = size;
                m_pTiles              = layerInfo.Tiles;
                m_uMinGID             = layerInfo.MinGID;
                m_uMaxGID             = layerInfo.MaxGID;
                m_cOpacity            = layerInfo.Opacity;
                Properties            = new Dictionary <string, string>(layerInfo.Properties);
                m_fContentScaleFactor = CCDirector.SharedDirector.ContentScaleFactor;

                // tilesetInfo
                m_pTileSet = tilesetInfo;

                // mapInfo
                m_tMapTileSize      = mapInfo.TileSize;
                m_uLayerOrientation = (CCTMXOrientation)mapInfo.Orientation;

                // offset (after layer orientation is set);
                CCPoint offset = CalculateLayerOffset(layerInfo.Offset);
                Position = CCMacros.CCPointPixelsToPoints(offset);

                m_pAtlasIndexArray = new List <int>((int)totalNumberOfTiles);

                ContentSize =
                    CCMacros.CCSizePixelsToPoints(new CCSize(m_tLayerSize.Width * m_tMapTileSize.Width,
                                                             m_tLayerSize.Height * m_tMapTileSize.Height));

                m_bUseAutomaticVertexZ = false;
                m_nVertexZvalue        = 0;

                return(true);
            }
            return(false);
        }
Ejemplo n.º 2
0
        protected virtual bool InitWithTextureFilename(String filename, CCRect rect, bool rotated, CCPoint offset,
                                                       CCSize originalSize)
        {
            m_pobTexture             = null;
            m_strTextureFilename     = filename;
            m_obRectInPixels         = rect;
            m_obRect                 = CCMacros.CCRectanglePixelsToPoints(rect);
            m_obOffsetInPixels       = offset;
            m_obOffset               = CCMacros.CCPointPixelsToPoints(m_obOffsetInPixels);
            m_obOriginalSizeInPixels = originalSize;
            m_obOriginalSize         = CCMacros.CCSizePixelsToPoints(m_obOriginalSizeInPixels);
            m_bRotated               = rotated;

            return(true);
        }
Ejemplo n.º 3
0
        protected virtual bool InitWithTexture(CCTexture2D pobTexture, CCRect rect, bool rotated, CCPoint offset,
                                               CCSize originalSize)
        {
            m_pobTexture = pobTexture;

            m_obRectInPixels         = rect;
            m_obRect                 = CCMacros.CCRectanglePixelsToPoints(rect);
            m_obOffsetInPixels       = offset;
            m_obOffset               = CCMacros.CCPointPixelsToPoints(m_obOffsetInPixels);
            m_obOriginalSizeInPixels = originalSize;
            m_obOriginalSize         = CCMacros.CCSizePixelsToPoints(m_obOriginalSizeInPixels);
            m_bRotated               = rotated;

            return(true);
        }
Ejemplo n.º 4
0
        public void CreateFontChars()
        {
            int nextFontPositionX = 0;
            int nextFontPositionY = 0;
            //unsigned short prev = -1;
            int kerningAmount = 0;

            CCSize tmpSize = CCSize.Zero;

            int longestLine = 0;
            int totalHeight = 0;

            int quantityOfLines = 1;

            int stringLen = m_sString.Length;

            if (stringLen == 0)
            {
                return;
            }

            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);

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

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

                // unichar is a short, and an int is needed on HASH_FIND_INT
                CCBMFontConfiguration.ccBMFontDef fontDef = m_pConfiguration.m_pFontDefDictionary[c];

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

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

                CCSprite fontChar;

                fontChar = (CCSprite)(GetChildByTag(i));
                if (fontChar == null)
                {
                    fontChar = new CCSprite(m_pobTextureAtlas.Texture, rect);
                    AddChild(fontChar, 0, i);
                }
                else
                {
                    // reusing fonts
                    fontChar.SetTextureRect(rect, false, rect.Size);

                    // restore to default in case they were modified
                    fontChar.Visible = true;
                    fontChar.Opacity = 255;
                }

                // 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;

                // Apply label properties
                fontChar.IsOpacityModifyRGB = m_bIsOpacityModifyRGB;
                // Color MUST be set before opacity, since opacity might change color if OpacityModifyRGB is on
                fontChar.Color = m_tColor;

                // only apply opacity if it is different than 255 )
                // to prevent modifying the color too (issue #610)
                if (m_cOpacity != 255)
                {
                    fontChar.Opacity = m_cOpacity;
                }

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

            tmpSize.Width  = longestLine;
            tmpSize.Height = totalHeight;

            ContentSize = CCMacros.CCSizePixelsToPoints(tmpSize);
        }
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(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);
        }