Ejemplo n.º 1
0
        private void updateTexture()
        {
            CCTexture2D tex;

            // Dump the old one
            if (Texture != null)
            {
                Texture.Dispose();
            }

            // let system compute label's width or height when its value is 0
            // refer to cocos2d-x issue #1430
            tex = new CCTexture2D();

            tex.InitWithString(m_pString,
                               CCMacros.CCSizePointsToPixels(m_tDimensions),
                               m_hAlignment,
                               m_vAlignment,
                               m_pFontName,
                               m_fFontSize * CCMacros.CCContentScaleFactor());

            Texture = tex;

            CCRect rect = CCRect.Zero;

            rect.Size = m_pobTexture.ContentSize;
            SetTextureRect(rect);
        }
Ejemplo n.º 2
0
        private void UpdateAtlasValueAt(CCGridSize pos, Color value, int index)
        {
            int x = pos.X;
            int y = pos.Y;

            float row = (float)(value.R % m_uItemsPerRow);
            float col = (float)(value.R / m_uItemsPerRow);

            float textureWide = (m_pTextureAtlas.Texture.PixelsWide);
            float textureHigh = (m_pTextureAtlas.Texture.PixelsHigh);

            float itemWidthInPixels  = m_uItemWidth * CCMacros.CCContentScaleFactor();
            float itemHeightInPixels = m_uItemHeight * CCMacros.CCContentScaleFactor();

#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
            float left   = (2 * row * itemWidthInPixels + 1) / (2 * textureWide);
            float right  = left + (itemWidthInPixels * 2 - 2) / (2 * textureWide);
            float top    = (2 * col * itemHeightInPixels + 1) / (2 * textureHigh);
            float bottom = top + (itemHeightInPixels * 2 - 2) / (2 * textureHigh);
#else
            float left   = (row * itemWidthInPixels) / textureWide;
            float right  = left + itemWidthInPixels / textureWide;
            float top    = (col * itemHeightInPixels) / textureHigh;
            float bottom = top + itemHeightInPixels / textureHigh;
#endif

            CCV3F_C4B_T2F_Quad quad;

            quad.TopLeft.TexCoords.U     = left;
            quad.TopLeft.TexCoords.V     = top;
            quad.TopRight.TexCoords.U    = right;
            quad.TopRight.TexCoords.V    = top;
            quad.BottomLeft.TexCoords.U  = left;
            quad.BottomLeft.TexCoords.V  = bottom;
            quad.BottomRight.TexCoords.U = right;
            quad.BottomRight.TexCoords.V = bottom;

            quad.BottomLeft.Vertices.X  = (x * m_uItemWidth);
            quad.BottomLeft.Vertices.Y  = (y * m_uItemHeight);
            quad.BottomLeft.Vertices.Z  = 0.0f;
            quad.BottomRight.Vertices.X = (x * m_uItemWidth + m_uItemWidth);
            quad.BottomRight.Vertices.Y = (y * m_uItemHeight);
            quad.BottomRight.Vertices.Z = 0.0f;
            quad.TopLeft.Vertices.X     = (x * m_uItemWidth);
            quad.TopLeft.Vertices.Y     = (y * m_uItemHeight + m_uItemHeight);
            quad.TopLeft.Vertices.Z     = 0.0f;
            quad.TopRight.Vertices.X    = (x * m_uItemWidth + m_uItemWidth);
            quad.TopRight.Vertices.Y    = (y * m_uItemHeight + m_uItemHeight);
            quad.TopRight.Vertices.Z    = 0.0f;

            var color = new CCColor4B(m_tColor.R, m_tColor.G, m_tColor.B, m_cOpacity);
            quad.TopRight.Colors    = color;
            quad.TopLeft.Colors     = color;
            quad.BottomRight.Colors = color;
            quad.BottomLeft.Colors  = color;

            m_pTextureAtlas.UpdateQuad(ref quad, index);
        }
Ejemplo n.º 3
0
 protected virtual bool InitWithWidthAndHeight(int w, int h, SurfaceFormat colorFormat, DepthFormat depthFormat, RenderTargetUsage usage)
 {
     m_Width       = (int)Math.Ceiling(w * CCMacros.CCContentScaleFactor());
     m_Height      = (int)Math.Ceiling(h * CCMacros.CCContentScaleFactor());
     m_ColorFormat = colorFormat;
     m_DepthFormat = depthFormat;
     m_Usage       = usage;
     MakeTexture();
     return(true);
 }
Ejemplo n.º 4
0
        public void Set2DProjection()
        {
            CCSize size = m_pTexture.ContentSizeInPixels;

            CCDrawManager.SetViewPort(0, 0,
                                      (int)(size.Width * CCMacros.CCContentScaleFactor()),
                                      (int)(size.Height * CCMacros.CCContentScaleFactor())
                                      );

            /*
             * CCDrawManager.ProjectionMatrix = Matrix.Identity;
             *
             * Matrix orthoMatrix = Matrix.CreateOrthographicOffCenter(
             *  0, size.width * ccMacros.CC_CONTENT_SCALE_FACTOR(),
             *  0, size.height * ccMacros.CC_CONTENT_SCALE_FACTOR(),
             *  -1, 1
             *  );
             *
             * CCDrawManager.MultMatrix(ref orthoMatrix);
             */

            CCDrawManager.ViewMatrix       = Matrix.Identity;
            CCDrawManager.ProjectionMatrix = Matrix.Identity;

            Matrix projection      = Matrix.CreateOrthographicOffCenter(0, size.Width, 0, size.Height, -1024.0f, 1024.0f);
            Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);

            CCDrawManager.WorldMatrix = (halfPixelOffset * projection);


            /*
             * CCDirector *director = CCDirector::sharedDirector();
             *
             * CCSize    size = director->getWinSizeInPixels();
             *
             * glViewport(0, 0, (GLsizei)(size.width * CC_CONTENT_SCALE_FACTOR()), (GLsizei)(size.height * CC_CONTENT_SCALE_FACTOR()) );
             * kmGLMatrixMode(KM_GL_PROJECTION);
             * kmGLLoadIdentity();
             *
             * kmMat4 orthoMatrix;
             * kmMat4OrthographicProjection(&orthoMatrix, 0, size.width * CC_CONTENT_SCALE_FACTOR(), 0, size.height * CC_CONTENT_SCALE_FACTOR(), -1, 1);
             * kmGLMultMatrix( &orthoMatrix );
             *
             * kmGLMatrixMode(KM_GL_MODELVIEW);
             * kmGLLoadIdentity();
             *
             *
             * ccSetProjectionMatrixDirty();
             */
        }
Ejemplo n.º 5
0
        public bool InitWithString(string theString, string fntFile)
        {
            string data = CCFileUtils.GetFileData(fntFile);

            PlistDocument doc  = new PlistDocument(data);
            var           dict = doc.Root as PlistDictionary;

            Debug.Assert(dict["version"].AsInt == 1, "Unsupported version. Upgrade cocos2d version");

            string textureFilename = dict["textureFilename"].AsString;
            int    width           = (int)Math.Ceiling(dict["itemWidth"].AsInt / CCMacros.CCContentScaleFactor());
            int    height          = (int)Math.Ceiling(dict["itemHeight"].AsInt / CCMacros.CCContentScaleFactor());
            var    startChar       = (char)dict["firstChar"].AsInt;

            return(InitWithString(theString, textureFilename, width, height, startChar));
        }
Ejemplo n.º 6
0
        private void updateTexture()
        {
            CCTexture2D tex;

            // Dump the old one
            if (Texture != null)
            {
                CCTexture2D tmp = Texture;
                Texture = null;
                tmp.Dispose();
            }

            // let system compute label's width or height when its value is 0
            // refer to cocos2d-x issue #1430
            tex = new CCTexture2D();

            var result = tex.InitWithString(m_pString,
                                            m_tDimensions.PointsToPixels(),
                                            m_hAlignment,
                                            m_vAlignment,
                                            m_pFontName,
                                            m_fFontSize * CCMacros.CCContentScaleFactor());

//#if MACOS || IPHONE || IOS
//			// There was a problem loading the text for some reason or another if result is not true
//			// For MonoMac and IOS Applications we will try to create a Native Label automatically
//			// If the font is not found then a default font will be selected by the device and used.
//			if (!result && !string.IsNullOrEmpty(m_pString))
//			{
//				tex = CCLabelUtilities.CreateLabelTexture (m_pString,
//				                                           CCMacros.CCSizePointsToPixels (m_tDimensions),
//				                                           m_hAlignment,
//				                                           m_vAlignment,
//				                                           m_pFontName,
//				                                           m_fFontSize * CCMacros.CCContentScaleFactor (),
//				                                           new CCColor4B(Microsoft.Xna.Framework.Color.White) );
//			}
//#endif
            if (result)
            {
                Texture = tex;

                CCRect rect = CCRect.Zero;
                rect.Size = m_pobTexture.ContentSize;
                SetTextureRect(rect);
            }
        }
Ejemplo n.º 7
0
        public void Set2DProjection()
        {
            CCSize size = m_pTexture.ContentSizeInPixels;

            CCDrawManager.SetViewPort(0, 0,
                                      (int)(size.Width * CCMacros.CCContentScaleFactor()),
                                      (int)(size.Height * CCMacros.CCContentScaleFactor())
                                      );

            CCDrawManager.ViewMatrix       = Matrix.Identity;
            CCDrawManager.ProjectionMatrix = Matrix.Identity;

            Matrix projection      = Matrix.CreateOrthographicOffCenter(0, size.Width, 0, size.Height, -1024.0f, 1024.0f);
            Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);

            CCDrawManager.WorldMatrix = (halfPixelOffset * projection);
        }
Ejemplo n.º 8
0
        protected virtual bool InitWithWidthAndHeight(int w, int h, SurfaceFormat colorFormat, DepthFormat depthFormat, RenderTargetUsage usage)
        {
            w = (int)Math.Ceiling(w * CCMacros.CCContentScaleFactor());
            h = (int)Math.Ceiling(h * CCMacros.CCContentScaleFactor());

            m_pTexture = new CCTexture2D();
            m_pTexture.IsAntialiased = false;

            m_pRenderTarget2D = CCDrawManager.CreateRenderTarget(w, h, colorFormat, depthFormat, usage);
            m_pTexture.InitWithTexture(m_pRenderTarget2D, colorFormat, true, false);

            m_bFirstUsage = true;

            m_pSprite = new CCSprite(m_pTexture);
            //m_pSprite.scaleY = -1;
            m_pSprite.BlendFunc = CCBlendFunc.AlphaBlend;

            AddChild(m_pSprite);

            return(true);
        }
Ejemplo n.º 9
0
        protected virtual bool InitWithWidthAndHeight(int w, int h, SurfaceFormat colorFormat, DepthFormat depthFormat, RenderTargetUsage usage)
        {
            w = (w * CCMacros.CCContentScaleFactor());
            h = (h * CCMacros.CCContentScaleFactor());

            m_pTexture = new CCTexture2D();
            m_pTexture.SetAliasTexParameters();

            m_pRenderTarget2D = CCDrawManager.CreateRenderTarget(w, h, colorFormat, depthFormat, usage);
            m_pTexture.InitWithTexture(m_pRenderTarget2D);

            m_bFirstUsage = true;

            m_pSprite = new CCSprite(m_pTexture);
            //m_pSprite.scaleY = -1;
            m_pSprite.BlendFunc = new CCBlendFunc(CCMacros.CCDefaultSourceBlending, CCMacros.CCDefaultDestinationBlending); // OGLES.GL_ONE, OGLES.GL_ONE_MINUS_SRC_ALPHA);

            AddChild(m_pSprite);

            return(true);
        }
Ejemplo n.º 10
0
        public override void UpdateAtlasValues()
        {
            int n = m_sString.Length;

            CCTexture2D texture = m_pTextureAtlas.Texture;

            float textureWide = texture.PixelsWide;
            float textureHigh = texture.PixelsHigh;

            float itemWidthInPixels  = m_uItemWidth * CCMacros.CCContentScaleFactor();
            float itemHeightInPixels = m_uItemHeight * CCMacros.CCContentScaleFactor();

            if (m_bIgnoreContentScaleFactor)
            {
                itemWidthInPixels  = m_uItemWidth;
                itemHeightInPixels = m_uItemHeight;
            }

            for (int i = 0; i < n; i++)
            {
                var   a   = (char)(m_sString[i] - m_cMapStartChar);
                float row = (a % m_uItemsPerRow);
                float col = (a / m_uItemsPerRow);

#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
                // Issue #938. Don't use texStepX & texStepY
                float left   = (2 * row * itemWidthInPixels + 1) / (2 * textureWide);
                float right  = left + (itemWidthInPixels * 2 - 2) / (2 * textureWide);
                float top    = (2 * col * itemHeightInPixels + 1) / (2 * textureHigh);
                float bottom = top + (itemHeightInPixels * 2 - 2) / (2 * textureHigh);
#else
                float left   = row * itemWidthInPixels / textureWide;
                float right  = left + itemWidthInPixels / textureWide;
                float top    = col * itemHeightInPixels / textureHigh;
                float bottom = top + itemHeightInPixels / textureHigh;
#endif
                // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL

                CCV3F_C4B_T2F_Quad quad;

                quad.TopLeft.TexCoords.U     = left;
                quad.TopLeft.TexCoords.V     = top;
                quad.TopRight.TexCoords.U    = right;
                quad.TopRight.TexCoords.V    = top;
                quad.BottomLeft.TexCoords.U  = left;
                quad.BottomLeft.TexCoords.V  = bottom;
                quad.BottomRight.TexCoords.U = right;
                quad.BottomRight.TexCoords.V = bottom;

                quad.BottomLeft.Vertices.X  = i * m_uItemWidth;
                quad.BottomLeft.Vertices.Y  = 0.0f;
                quad.BottomLeft.Vertices.Z  = 0.0f;
                quad.BottomRight.Vertices.X = i * m_uItemWidth + m_uItemWidth;
                quad.BottomRight.Vertices.Y = 0.0f;
                quad.BottomRight.Vertices.Z = 0.0f;
                quad.TopLeft.Vertices.X     = i * m_uItemWidth;
                quad.TopLeft.Vertices.Y     = m_uItemHeight;
                quad.TopLeft.Vertices.Z     = 0.0f;
                quad.TopRight.Vertices.X    = i * m_uItemWidth + m_uItemWidth;
                quad.TopRight.Vertices.Y    = m_uItemHeight;
                quad.TopRight.Vertices.Z    = 0.0f;


                quad.TopLeft.Colors = quad.TopRight.Colors = quad.BottomLeft.Colors = quad.BottomRight.Colors =
                    new CCColor4B(_displayedColor.R, _displayedColor.G, _displayedColor.B, _displayedOpacity);

                m_pTextureAtlas.UpdateQuad(ref quad, i);
            }
        }
        // pointRect should be in Texture coordinates, not pixel coordinates
        private void InitTexCoordsWithRect(CCRect pointRect)
        {
            // convert to Tex coords

            var rect = new CCRect(
                pointRect.Origin.X * CCMacros.CCContentScaleFactor(),
                pointRect.Origin.Y * CCMacros.CCContentScaleFactor(),
                pointRect.Size.Width * CCMacros.CCContentScaleFactor(),
                pointRect.Size.Height * CCMacros.CCContentScaleFactor());

            float wide = pointRect.Size.Width;
            float high = pointRect.Size.Height;

            if (m_pTexture != null)
            {
                wide = m_pTexture.PixelsWide;
                high = m_pTexture.PixelsHigh;
            }

#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
            float left   = (rect.origin.x * 2 + 1) / (wide * 2);
            float bottom = (rect.origin.y * 2 + 1) / (high * 2);
            float right  = left + (rect.size.width * 2 - 2) / (wide * 2);
            float top    = bottom + (rect.size.height * 2 - 2) / (high * 2);
        #else
            float left   = rect.Origin.X / wide;
            float bottom = rect.Origin.Y / high;
            float right  = left + rect.Size.Width / wide;
            float top    = bottom + rect.Size.Height / high;
#endif
            // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL

            // Important. Texture in cocos2d are inverted, so the Y component should be inverted
            float tmp = top;
            top    = bottom;
            bottom = tmp;

            CCV3F_C4B_T2F_Quad[] quads;
            int start, end;
            if (m_pBatchNode != null)
            {
                quads = m_pBatchNode.TextureAtlas.m_pQuads.Elements;
                m_pBatchNode.TextureAtlas.Dirty = true;
                start = m_uAtlasIndex;
                end   = m_uAtlasIndex + m_uTotalParticles;
            }
            else
            {
                quads = m_pQuads.Elements;
                start = 0;
                end   = m_uTotalParticles;
            }

            for (int i = start; i < end; i++)
            {
                // bottom-left vertex:
                quads[i].BottomLeft.TexCoords.U = left;
                quads[i].BottomLeft.TexCoords.V = bottom;
                // bottom-right vertex:
                quads[i].BottomRight.TexCoords.U = right;
                quads[i].BottomRight.TexCoords.V = bottom;
                // top-left vertex:
                quads[i].TopLeft.TexCoords.U = left;
                quads[i].TopLeft.TexCoords.V = top;
                // top-right vertex:
                quads[i].TopRight.TexCoords.U = right;
                quads[i].TopRight.TexCoords.V = top;
            }
        }
Ejemplo n.º 12
0
        public static void SetDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy)
        {
            Debug.Assert(resolutionPolicy != ResolutionPolicy.UnKnown, "should set resolutionPolicy");

            if (width == 0.0f || height == 0.0f)
            {
                return;
            }

            m_obDesignResolutionSize.Width  = width;
            m_obDesignResolutionSize.Height = height;

            m_fScaleX = m_obScreenSize.Width / m_obDesignResolutionSize.Width;
            m_fScaleY = m_obScreenSize.Height / m_obDesignResolutionSize.Height;

            if (resolutionPolicy == ResolutionPolicy.NoBorder)
            {
                m_fScaleX = m_fScaleY = Math.Max(m_fScaleX, m_fScaleY);
            }

            if (resolutionPolicy == ResolutionPolicy.ShowAll)
            {
                m_fScaleX = m_fScaleY = Math.Min(m_fScaleX, m_fScaleY);
            }

            // calculate the rect of viewport
            float viewPortW = m_obDesignResolutionSize.Width * m_fScaleX;
            float viewPortH = m_obDesignResolutionSize.Height * m_fScaleY;

            m_obViewPortRect = new CCRect((m_obScreenSize.Width - viewPortW) / 2, (m_obScreenSize.Height - viewPortH) / 2, viewPortW, viewPortH);

            m_eResolutionPolicy = resolutionPolicy;

            // reset director's member variables to fit visible rect
            CCDirector.SharedDirector.m_obWinSizeInPoints = Size;
            CCDirector.SharedDirector.m_obWinSizeInPixels = new CCSize(m_obDesignResolutionSize.Width * CCMacros.CCContentScaleFactor(),
                                                                       m_obDesignResolutionSize.Height * CCMacros.CCContentScaleFactor());
            CCDirector.SharedDirector.CreateStatsLabel();
            CCDirector.SharedDirector.SetGlDefaultValues();
        }
Ejemplo n.º 13
0
        private CCBMFontConfiguration InitializeFont(string fontName, float fontSize, string charset)
        {
            if (m_pData == null)
            {
                InitializeTTFAtlas(1024, 1024);
            }

            if (String.IsNullOrEmpty(charset))
            {
                charset = " ";
            }

            var chars = new CCRawList <char>();

            var fontKey = GetFontKey(fontName, fontSize);

            CCBMFontConfiguration fontConfig;

            if (!s_pConfigurations.TryGetValue(fontKey, out fontConfig))
            {
                fontConfig = new CCBMFontConfiguration();
                s_pConfigurations.Add(fontKey, fontConfig);
            }

            for (int i = 0; i < charset.Length; i++)
            {
                var ch = charset[i];
                if (!fontConfig.m_pFontDefDictionary.ContainsKey(ch) && chars.IndexOf(ch) == -1)
                {
                    chars.Add(ch);
                }
            }

            if (chars.Count == 0)
            {
                return(fontConfig);
            }

            CreateFont(fontName, fontSize * CCMacros.CCContentScaleFactor(), chars);

            fontConfig.m_nCommonHeight = (int)Math.Ceiling(GetFontHeight());

            int[] data = null;

            for (int i = 0; i < chars.Count; i++)
            {
                var s = chars[i].ToString();

                var charSize = GetMeasureString(s);

                int w = (int)Math.Ceiling(charSize.Width + 2);
                int h = (int)Math.Ceiling(charSize.Height + 2);

                if (data == null || data.Length < (w * h))
                {
                    data = new int[w * h];
                }

                unsafe
                {
                    int   stride;
                    byte *pBase = GetBitmapData(s, out stride);

                    int minX = w;
                    int maxX = 0;
                    int minY = h;
                    int maxY = 0;

                    for (int y = 0; y < h; y++)
                    {
                        var row = (int *)(pBase + y * stride);

                        for (int x = 0; x < w; x++)
                        {
                            if (row[x] != 0)
                            {
                                minX = Math.Min(minX, x);
                                maxX = Math.Max(maxX, x);
                                minY = Math.Min(minY, y);
                                maxY = Math.Max(maxY, y);
                            }
                        }
                    }

                    w = Math.Max(maxX - minX + 1, 1);
                    h = Math.Max(maxY - minY + 1, 1);

                    //maxX = minX + w;
                    //maxY = minY + h;

                    int index = 0;
                    for (int y = minY; y <= maxY; y++)
                    {
                        var row = (int *)(pBase + y * stride);
                        for (int x = minX; x <= maxX; x++)
                        {
                            data[index] = row[x];
                            index++;
                        }
                    }

                    var region = AllocateRegion(w, h);

                    if (region.x >= 0)
                    {
                        SetRegionData(region, data, w);

                        var info = GetKerningInfo(chars[i]);

                        var fontDef = new CCBMFontConfiguration.CCBMFontDef()
                        {
                            charID   = chars[i],
                            rect     = new CCRect(region.x, region.y, region.width, region.height),
                            xOffset  = minX, // + (int)Math.Ceiling(info.A),
                            yOffset  = minY,
                            xAdvance = (int)Math.Ceiling(info.A + info.B + info.C)
                        };

                        fontConfig.CharacterSet.Add(chars[i]);
                        fontConfig.m_pFontDefDictionary.Add(chars[i], fontDef);
                    }
                    else
                    {
                        CCLog.Log("Texture atlas is full");
                    }
                }
            }

            m_bTextureDirty = true;

            return(fontConfig);
        }
Ejemplo n.º 14
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.º 15
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);
        }