コード例 #1
0
ファイル: CCRect.cs プロジェクト: chengcong/cocos2d-xna
 public static bool CCRectEqualToRect(CCRect rect1, CCRect rect2)
 {
     if (rect1 == null || rect2 == null)
     {
         return(false);
     }
     if (!CCPoint.CCPointEqualToPoint(rect1.origin, rect2.origin))
     {
         return(false);
     }
     return(CCSize.CCSizeEqualToSize(rect1.size, rect2.size));
 }
コード例 #2
0
        public static bool CCRectEqualToRect(CCRect rect1, CCRect rect2)
        {
            Debug.Assert((rect1 != null) && (rect2 != null));

            if ((rect1 == null) || (rect2 == null))
            {
                return(false);
            }
            else
            {
                return(CCPoint.CCPointEqualToPoint(rect1.origin, rect2.origin) &&
                       (CCSize.CCSizeEqualToSize(rect1.size, rect2.size)));
            }
        }
コード例 #3
0
        /// <summary>
        /// changes the string to render
        /// @warning Changing the string is as expensive as creating a new CCLabelTTF. To obtain better performance use CCLabelAtlas
        /// </summary>
        public void setString(string label)
        {
            m_pString = label;

            CCTexture2D texture;

            if (CCSize.CCSizeEqualToSize(m_tDimensions, new CCSize(0, 0)))
            {
                texture = new CCTexture2D();
                texture.initWithString(label, m_pFontName.ToString(), m_fFontSize);
            }
            else
            {
                texture = new CCTexture2D();
                texture.initWithString(label, m_tDimensions, m_eAlignment, m_pFontName.ToString(), m_fFontSize);
            }
            this.Texture = texture;

            CCRect rect = new CCRect(0, 0, 0, 0);

            rect.size = m_pobTexture.getContentSize();
            this.setTextureRect(rect);
        }
コード例 #4
0
        public void setString(string label)
        {
            CCTexture2D cCTexture2D;

            this.m_pString = label;
            if (!CCSize.CCSizeEqualToSize(this.m_tDimensions, new CCSize(0f, 0f)))
            {
                cCTexture2D = new CCTexture2D();
                cCTexture2D.initWithString(label, this.m_tDimensions, this.m_eAlignment, this.m_pFontName.ToString(), this.m_fFontSize, this.m_fgColor, this.m_bgColor);
            }
            else
            {
                cCTexture2D = new CCTexture2D();
                cCTexture2D.initWithString(label, this.m_pFontName.ToString(), this.m_fFontSize, this.m_fgColor, this.m_bgColor);
            }
            this.Texture = cCTexture2D;
            CCRect cCRect = new CCRect(0f, 0f, 0f, 0f)
            {
                size = this.m_pobTexture.getContentSize()
            };

            base.setTextureRect(cCRect);
        }
コード例 #5
0
 public bool initWithString(string text, CCSize dimensions, CCTextAlignment alignment, string fontName, float fontSize, Color fgColor, Color bgColor)
 {
     if ((dimensions.width >= 0f) && (dimensions.height >= 0f))
     {
         Vector2 vector2;
         if (string.IsNullOrEmpty(text))
         {
             return(false);
         }
         SpriteFont spriteFont = null;
         try
         {
             spriteFont = CCApplication.sharedApplication().content.Load <SpriteFont>("fonts/" + fontName);
         }
         catch (Exception)
         {
             if (fontName.EndsWith(".spritefont", StringComparison.OrdinalIgnoreCase))
             {
                 fontName   = fontName.Substring(0, fontName.Length - 11);
                 spriteFont = CCApplication.sharedApplication().content.Load <SpriteFont>("fonts/" + fontName);
             }
         }
         if (CCSize.CCSizeEqualToSize(dimensions, new CCSize()))
         {
             Vector2 vector = spriteFont.MeasureString(text);
             dimensions.width  = vector.X;
             dimensions.height = vector.Y;
         }
         if (CCTextAlignment.CCTextAlignmentRight == alignment)
         {
             vector2 = new Vector2(-(dimensions.width - spriteFont.MeasureString(text).X), 0f);
         }
         else if (CCTextAlignment.CCTextAlignmentCenter == alignment)
         {
             vector2 = new Vector2(-(dimensions.width - spriteFont.MeasureString(text).X) / 2f, 0f);
         }
         else
         {
             vector2 = new Vector2(0f, 0f);
         }
         float scale = 1f;
         try
         {
             CCApplication  application  = CCApplication.sharedApplication();
             RenderTarget2D renderTarget = new RenderTarget2D(application.graphics.GraphicsDevice, (int)dimensions.width, (int)dimensions.height);
             application.graphics.GraphicsDevice.SetRenderTarget(renderTarget);
             application.graphics.GraphicsDevice.Clear(bgColor);
             application.spriteBatch.Begin();
             application.spriteBatch.DrawString(spriteFont, text, new Vector2(0f, 0f), fgColor, 0f, vector2, scale, SpriteEffects.None, 0f);
             application.spriteBatch.End();
             application.graphics.GraphicsDevice.SetRenderTarget(null);
             Color[] data = new Color[renderTarget.Width * renderTarget.Height];
             renderTarget.GetData <Color>(data);
             this.texture2D = new Texture2D(application.GraphicsDevice, renderTarget.Width, renderTarget.Height);
             this.texture2D.SetData <Color>(data);
             return(this.initWithTexture(this.texture2D));
         }
         catch (Exception)
         {
         }
     }
     return(false);
 }
コード例 #6
0
        ///<summary>
        /// Initializes a texture from a string with dimensions, alignment, font name and font size
        /// </summary>
        public bool initWithString(string text, CCSize dimensions, CCTextAlignment alignment, string fontName, float fontSize, Color fgColor, Color bgColor)
        {
            if (dimensions.width < 0 || dimensions.height < 0)
            {
                return(false);
            }

            if (string.IsNullOrEmpty(text))
            {
                return(false);
            }

            SpriteFont font = null;

            try
            {
                font = CCApplication.sharedApplication().content.Load <SpriteFont>(@"fonts/" + fontName);
            }
            catch (Exception)
            {
                if (fontName.EndsWith(".spritefont", StringComparison.OrdinalIgnoreCase))
                {
                    fontName = fontName.Substring(0, fontName.Length - 11);
                    font     = CCApplication.sharedApplication().content.Load <SpriteFont>(@"fonts/" + fontName);
                }
            }
            if (CCSize.CCSizeEqualToSize(dimensions, new CCSize()))
            {
                Vector2 temp = font.MeasureString(text);
                dimensions.width  = temp.X;
                dimensions.height = temp.Y;
            }

            Vector2 origin;

            if (CCTextAlignment.CCTextAlignmentRight == alignment)
            {
                origin = new Vector2(-(dimensions.width - font.MeasureString(text).X), 0);
            }
            else if (CCTextAlignment.CCTextAlignmentCenter == alignment)
            {
                origin = new Vector2(-(dimensions.width - font.MeasureString(text).X) / 2.0f, 0);
            }
            else
            {
                origin = new Vector2(0, 0);
            }

            float scale = 1.0f;//need refer fontSize;

            try
            {
                CCApplication app = CCApplication.sharedApplication();

                //*  for render to texture
                RenderTarget2D renderTarget = new RenderTarget2D(app.graphics.GraphicsDevice, (int)dimensions.width, (int)dimensions.height);
                app.graphics.GraphicsDevice.SetRenderTarget(renderTarget);
                app.graphics.GraphicsDevice.Clear(bgColor);

                app.spriteBatch.Begin();
                app.spriteBatch.DrawString(font, text, new Vector2(0, 0), fgColor, 0.0f, origin, scale, SpriteEffects.None, 0.0f);
                app.spriteBatch.End();

                app.graphics.GraphicsDevice.SetRenderTarget(null);

                // to copy the rendered target data to a plain texture(to the memory)
                Color[] colors1D = new Color[renderTarget.Width * renderTarget.Height];
                renderTarget.GetData(colors1D);
                texture2D = new Texture2D(app.GraphicsDevice, renderTarget.Width, renderTarget.Height);
                texture2D.SetData(colors1D);

                return(initWithTexture(texture2D));
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
            return(false);
        }