internal static CCTexture2D CreateNativeLabel(string text, CCSize dimensions, CCTextAlignment hAlignment,
		                                   CCVerticalTextAlignment vAlignment, string fontName,
		                                   float fontSize, CCColor4B textColor)
		{

		    if (string.IsNullOrEmpty(text))
		    {
		        return new CCTexture2D();
		    }

		    var font = CreateFont (fontName, fontSize);

            if (dimensions.Equals(CCSize.Zero))
            {
                CreateBitmap(1, 1);

                var ms = _graphics.MeasureString(text, font);
                
                dimensions.Width = ms.Width;
                dimensions.Height = ms.Height;
            }

            CreateBitmap((int)dimensions.Width, (int)dimensions.Height);

            var stringFormat = new StringFormat();

		    switch (hAlignment)
		    {
		        case CCTextAlignment.Left:
                    stringFormat.Alignment = StringAlignment.Near;
		            break;
		        case CCTextAlignment.Center:
                    stringFormat.Alignment = StringAlignment.Center;
		            break;
		        case CCTextAlignment.Right:
                    stringFormat.Alignment = StringAlignment.Far;
		            break;
		    }

		    switch (vAlignment)
		    {
		        case CCVerticalTextAlignment.Top:
        		    stringFormat.LineAlignment = StringAlignment.Near;
		            break;
		        case CCVerticalTextAlignment.Center:
        		    stringFormat.LineAlignment = StringAlignment.Center;
		            break;
		        case CCVerticalTextAlignment.Bottom:
        		    stringFormat.LineAlignment = StringAlignment.Far;
		            break;
		    }

            _graphics.DrawString(text, font, _brush, new RectangleF(0, 0, dimensions.Width, dimensions.Height), stringFormat);
            _graphics.Flush();

			var texture = new CCTexture2D();
			texture.InitWithStream (SaveToStream(), Microsoft.Xna.Framework.Graphics.SurfaceFormat.Bgra4444);

			return texture;
		}
示例#2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CocosSharp.CCLabel"/> class.
 /// </summary>
 /// <param name="str">Initial text of the label.</param>
 /// <param name="fntFile">Font definition file to use.</param>
 /// <param name="dimensions">Dimensions that the label should use to layout it's text.</param>
 /// <param name="hAlignment">Horizontal alignment of the text.</param>
 /// <param name="vAlignement">Vertical alignement of the text.</param>
 /// <param name="imageOffset">Image offset.</param>
 /// <param name="texture">Texture Atlas to be used.</param>
 public CCLabel(string str, string fntFile, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment,
                CCPoint imageOffset, CCTexture2D texture)
     : this(str, fntFile, dimensions,
            new CCLabelFormat() { Alignment = hAlignment, LineAlignment = vAlignment },
            imageOffset, texture)
 {
 }
示例#3
0
 public CCLabel(string str, string fntFile, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment,
                CCPoint imageOffset, CCTexture2D texture)
     : this(str, fntFile, dimensions, new CCLabelFormat() { Alignment = hAlignment, LineAlignment = vAlignment }, imageOffset, texture)
 {
     // First we try loading BitMapFont
     //InitBMFont(str, fntFile, dimensions, hAlignment, vAlignment, imageOffset, texture);
 }
示例#4
0
 public CCTextFieldTTF(string text, string fontName, float fontSize, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment)
     : base(text, fontName, fontSize, dimensions, new CCLabelFormat( CCLabelFormatFlags.Unknown ) { Alignment = hAlignment,
         LineAlignment = vAlignment})
 {
     EditTitle = "Input";
     EditDescription = "Please provide input";
 }
示例#5
0
 public bool InitWithPlaceHolder(string placeholder, CCSize dimensions, CCTextAlignment alignment, string fontName, float fontSize)
 {
     if (placeholder != null)
     {
         m_pPlaceHolder = placeholder;
     }
     return(cclabelttf.InitWithString(m_pPlaceHolder, fontName, fontSize, dimensions, alignment));
 }
示例#6
0
 public bool initWithPlaceHolder(string placeholder, CCSize dimensions, CCTextAlignment alignment, string fontName, float fontSize)
 {
     if (placeholder != null)
     {
         this.m_pPlaceHolder = placeholder;
     }
     return(this.cclabelttf.initWithString(this.m_pPlaceHolder, dimensions, alignment, fontName, fontSize));
 }
示例#7
0
        public CCLabelTTF ()
        {
            m_hAlignment = CCTextAlignment.Center;
            m_vAlignment = CCVerticalTextAlignment.Top;
            m_pFontName = string.Empty;
            m_fFontSize = 0.0f;

            Init();
        }
示例#8
0
        public CCLabelTTF()
        {
            m_hAlignment = CCTextAlignment.Center;
            m_vAlignment = CCVerticalTextAlignment.Top;
            m_pFontName  = string.Empty;
            m_fFontSize  = 0.0f;

            Init();
        }
        /// <summary>
        /// creates a CCLabelTTF from a fontname, alignment, dimension and font size
        /// </summary>
        public static CCLabelTTF labelWithString(string label, CCSize dimensions, CCTextAlignment alignment, string fontName, float fontSize)
        {
            CCLabelTTF pRet = new CCLabelTTF();
            if (pRet != null && pRet.initWithString(label, dimensions, alignment, fontName, fontSize))
            {
                return pRet;
            }

            return null;
        }
示例#10
0
        public static CCLabelTTF labelWithString(string label, CCSize dimensions, CCTextAlignment alignment, string fontName, float fontSize)
        {
            CCLabelTTF cCLabelTTF = new CCLabelTTF();

            if (cCLabelTTF != null && cCLabelTTF.initWithString(label, dimensions, alignment, fontName, fontSize))
            {
                return(cCLabelTTF);
            }
            return(null);
        }
        //////////////////////////////////////////////////////////////////////////
        // initialize
        //////////////////////////////////////////////////////////////////////////

        /** initializes the CCTextFieldTTF with a font name, alignment, dimension and font size */
        public bool initWithPlaceHolder(string placeholder, CCSize dimensions, CCTextAlignment alignment, string fontName, float fontSize)
        {
            if (placeholder != null)
            {
                //CC_SAFE_DELETE(m_pPlaceHolder);
                m_pPlaceHolder = placeholder;
            }

            return cclabelttf.initWithString(m_pPlaceHolder, dimensions, alignment, fontName, fontSize);
            //throw new NotFiniteNumberException();
        }
示例#12
0
        //////////////////////////////////////////////////////////////////////////
        // initialize
        //////////////////////////////////////////////////////////////////////////

        /** initializes the CCTextFieldTTF with a font name, alignment, dimension and font size */
        public bool initWithPlaceHolder(string placeholder, CCSize dimensions, CCTextAlignment alignment, string fontName, float fontSize)
        {
            if (placeholder != null)
            {
                //CC_SAFE_DELETE(m_pPlaceHolder);
                m_pPlaceHolder = placeholder;
            }

            return(cclabelttf.initWithString(m_pPlaceHolder, dimensions, alignment, fontName, fontSize));
            //throw new NotFiniteNumberException();
        }
示例#13
0
 public bool initWithString(string label, CCSize dimensions, CCTextAlignment alignment, string fontName, float fontSize)
 {
     if (!this.init())
     {
         return(false);
     }
     this.m_tDimensions = new CCSize(dimensions.width * CCDirector.sharedDirector().ContentScaleFactor, dimensions.height * CCDirector.sharedDirector().ContentScaleFactor);
     this.m_eAlignment  = alignment;
     this.m_pFontName   = fontName;
     this.m_fFontSize   = fontSize * CCDirector.sharedDirector().ContentScaleFactor;
     this.setString(label);
     return(true);
 }
示例#14
0
        protected void init(string text, string fontName, float fontSize, Vector2 dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment)
        {
            _opacityModifyRGB = true;
            _flipY            = _flipX = false;
            _anchorPoint      = new Vector2(0.5f, 0.5f);

            _hAlignment = CCTextAlignment.Center;
            _vAlignment = CCVerticalTextAlignment.Center;

            this.dimensions          = dimensions;
            this.horizontalAlignment = hAlignment;
            this.verticalAlignment   = vAlignment;
            this.fontName            = getFontName(fontName);
            this.fontSize            = fontSize;
            this.text = text;
        }
示例#15
0
        /// <summary>
        /// initializes the CCLabelTTF with a font name, alignment, dimension and font size
        /// </summary>
        public bool initWithString(string label, CCSize dimensions, CCTextAlignment alignment, string fontName, float fontSize)
        {
            Debug.Assert(label != null);
            if (init())
            {
                m_tDimensions = new CCSize(dimensions.width * CCDirector.sharedDirector().ContentScaleFactor, dimensions.height * CCDirector.sharedDirector().ContentScaleFactor);
                m_eAlignment = alignment;

                m_pFontName = fontName;

                m_fFontSize = fontSize * CCDirector.sharedDirector().ContentScaleFactor;
                this.setString(label);
                return true;
            }
            return false;
        }
        //char * description();

        //////////////////////////////////////////////////////////////////////////
        // static constructor
        //////////////////////////////////////////////////////////////////////////

        /** creates a CCTextFieldTTF from a fontname, alignment, dimension and font size */
        public static CCTextFieldTTF textFieldWithPlaceHolder(string placeholder, CCSize dimensions, CCTextAlignment alignment, string fontName, float fontSize)
        {
            CCTextFieldTTF pRet = new CCTextFieldTTF();
            if (pRet != null && pRet.initWithPlaceHolder("", dimensions, alignment, fontName, fontSize))
            {
                //pRet->autorelease();
                if (placeholder != null)
                {
                    pRet.PlaceHolder = placeholder;
                }
                return pRet;
            }
            //CC_SAFE_DELETE(pRet);
            return null;
            throw new NotFiniteNumberException();
        }
示例#17
0
        private void InitCCLabelTTF(string text, string fontName, float fontSize,
                                    CCSize dimensions, CCTextAlignment hAlignment,
                                    CCVerticalTextAlignment vAlignment)
        {
            // shader program
            //this->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(SHADER_PROGRAM));

            m_tDimensions = new CCSize(dimensions.Width, dimensions.Height);
            m_hAlignment  = hAlignment;
            m_vAlignment  = vAlignment;
            if (fontName == null)
            {
                fontName = "arial";
            }
            m_pFontName = (!string.IsNullOrEmpty(fontName.Trim())) ? fontName : "arial";
            m_fFontSize = fontSize;

            this.Text = text;
        }
        public override void OnEnter()
        {
            base.OnEnter();

            var s = VisibleBoundsWorldspace.Size;

            menuLeft.Position = new CCPoint(50, s.Height / 2 - 20);
            menuRight.Position = new CCPoint(s.Width - 50, s.Height / 2 - 20);


            alignmentLabel = null;
            horizontalAlign = CCTextAlignment.Left;
            verticalAlign = CCVerticalTextAlignment.Top;


            blockSize = new CCSize(s.Width / 3, s.Height / 2);

            var leftPanel = new AlignmentPanel(blockSize, new CCColor4B(100, 100, 100, 255));
            var centerPanel = new AlignmentPanel(blockSize, new CCColor4B(200, 100, 100, 255));
            var rightPanel = new AlignmentPanel(blockSize, new CCColor4B(100, 100, 200, 255));

            leftPanel.IgnoreAnchorPointForPosition = false;
            centerPanel.IgnoreAnchorPointForPosition = false;
            rightPanel.IgnoreAnchorPointForPosition = false;

            leftPanel.AnchorPoint = CCPoint.AnchorMiddleLeft;
            centerPanel.AnchorPoint = CCPoint.AnchorMiddleLeft;
            rightPanel.AnchorPoint = CCPoint.AnchorMiddleLeft;

            leftPanel.Position = new CCPoint(0, s.Height / 2);
            centerPanel.Position = new CCPoint(blockSize.Width, s.Height / 2);
            rightPanel.Position = new CCPoint(blockSize.Width * 2, s.Height / 2);

            AddChild(leftPanel, -1);
            AddChild(rightPanel, -1);
            AddChild(centerPanel, -1);

            updateAlignment();
        }
示例#19
0
        public bool InitWithString(string text, string fontName, float fontSize,
                                   CCSize dimensions, CCTextAlignment hAlignment,
                                   CCVerticalTextAlignment vAlignment)
        {
            if (base.Init())
            {
                // shader program
                //this->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(SHADER_PROGRAM));

                m_tDimensions = new CCSize(dimensions.Width, dimensions.Height);
                m_hAlignment  = hAlignment;
                m_vAlignment  = vAlignment;
                m_pFontName   = fontName;
                m_fFontSize   = fontSize;

                Label = (text);

                return(true);
            }

            return(false);
        }
        public override void OnEnter()
        {
            base.OnEnter();

            var s = VisibleBoundsWorldspace.Size;

            menuLeft.Position  = new CCPoint(50, s.Height / 2 - 20);
            menuRight.Position = new CCPoint(s.Width - 50, s.Height / 2 - 20);


            alignmentLabel  = null;
            horizontalAlign = CCTextAlignment.Left;
            verticalAlign   = CCVerticalTextAlignment.Top;


            blockSize = new CCSize(s.Width / 3, s.Height / 2);
            //blockSize = new CCSize(50, 50);
            var leftPanel   = new AlignmentPanel(blockSize, new CCColor4B(100, 100, 100, 255));
            var centerPanel = new AlignmentPanel(blockSize, new CCColor4B(200, 100, 100, 255));
            var rightPanel  = new AlignmentPanel(blockSize, new CCColor4B(100, 100, 200, 255));

            leftPanel.IgnoreAnchorPointForPosition   = false;
            centerPanel.IgnoreAnchorPointForPosition = false;
            rightPanel.IgnoreAnchorPointForPosition  = false;

            leftPanel.AnchorPoint   = new CCPoint(0, 0.5f);
            centerPanel.AnchorPoint = new CCPoint(0, 0.5f);
            rightPanel.AnchorPoint  = new CCPoint(0, 0.5f);

            leftPanel.Position   = new CCPoint(0, s.Height / 2);
            centerPanel.Position = new CCPoint(blockSize.Width, s.Height / 2);
            rightPanel.Position  = new CCPoint(blockSize.Width * 2, s.Height / 2);

            AddChild(leftPanel, -1);
            AddChild(rightPanel, -1);
            AddChild(centerPanel, -1);

            updateAlignment();
        }
示例#21
0
        public LabelTTFTest()
        {
            var    blockSize = new CCSize(200, 160);
            CCSize s         = CCDirector.SharedDirector.WinSize;

            CCLayerColor colorLayer = new CCLayerColor(new CCColor4B(100, 100, 100, 255), blockSize.Width, blockSize.Height);

            colorLayer.AnchorPoint = new CCPoint(0, 0);
            colorLayer.Position    = new CCPoint((s.Width - blockSize.Width) / 2, (s.Height - blockSize.Height) / 2);

            AddChild(colorLayer);

            CCMenuItemFont.FontSize = 30;
            CCMenu menu = new CCMenu(
                new CCMenuItemFont("Left", setAlignmentLeft),
                new CCMenuItemFont("Center", setAlignmentCenter),
                new CCMenuItemFont("Right", setAlignmentRight)
                );

            menu.AlignItemsVerticallyWithPadding(4);
            menu.Position = new CCPoint(50, s.Height / 2 - 20);
            AddChild(menu);

            menu = new CCMenu(
                new CCMenuItemFont("Top", setAlignmentTop),
                new CCMenuItemFont("Middle", setAlignmentMiddle),
                new CCMenuItemFont("Bottom", setAlignmentBottom)
                );
            menu.AlignItemsVerticallyWithPadding(4);
            menu.Position = new CCPoint(s.Width - 50, s.Height / 2 - 20);
            AddChild(menu);

            m_plabel      = null;
            m_eHorizAlign = CCTextAlignment.Left;
            m_eVertAlign  = CCVerticalTextAlignment.Top;

            updateAlignment();
        }
示例#22
0
        public LabelTTFTest()
        {
            var blockSize = new CCSize(200, 160);
			var s = Layer.VisibleBoundsWorldspace.Size;

			var colorLayer = new CCLayerColor(new CCColor4B(100, 100, 100, 255));
			colorLayer.AnchorPoint = CCPoint.Zero;
            colorLayer.Position = new CCPoint((s.Width - blockSize.Width) / 2, (s.Height - blockSize.Height) / 2);

            AddChild(colorLayer);

			CCMenuItemFont.FontSize = 32;
			CCMenuItemFont.FontName = "MarkerFelt";

			var menu = new CCMenu(
				new CCMenuItemFont("Left", setAlignmentLeft),
				new CCMenuItemFont("Center", setAlignmentCenter),
				new CCMenuItemFont("Right", setAlignmentRight)
                );
            menu.AlignItemsVertically(4);
            menu.Position = new CCPoint(50, s.Height / 2 - 20);
            AddChild(menu);

            menu = new CCMenu(
				new CCMenuItemFont("Top", setAlignmentTop),
				new CCMenuItemFont("Middle", setAlignmentMiddle),
				new CCMenuItemFont("Bottom", setAlignmentBottom)
                );
            menu.AlignItemsVertically(4);
            menu.Position = new CCPoint(s.Width - 50, s.Height / 2 - 20);
            AddChild(menu);

            m_plabel = null;
            m_eHorizAlign = CCTextAlignment.Left;
            m_eVertAlign = CCVerticalTextAlignment.Top;

            updateAlignment();
        }
示例#23
0
        public LabelTTFTest()
        {
            var blockSize = new CCSize(200, 160);
            CCSize s = CCDirector.SharedDirector.WinSize;

            CCLayerColor colorLayer = new CCLayerColor(new CCColor4B(100, 100, 100, 255), blockSize.Width, blockSize.Height);
            colorLayer.AnchorPoint = new CCPoint(0, 0);
            colorLayer.Position = new CCPoint((s.Width - blockSize.Width) / 2, (s.Height - blockSize.Height) / 2);

            AddChild(colorLayer);

            CCMenuItemFont.FontSize = 30;
            CCMenu menu = new CCMenu(
                new CCMenuItemFont("Left", setAlignmentLeft),
                new CCMenuItemFont("Center", setAlignmentCenter),
                new CCMenuItemFont("Right", setAlignmentRight)
                );
            menu.AlignItemsVerticallyWithPadding(4);
            menu.Position = new CCPoint(50, s.Height / 2 - 20);
            AddChild(menu);

            menu = new CCMenu(
                new CCMenuItemFont("Top", setAlignmentTop),
                new CCMenuItemFont("Middle", setAlignmentMiddle),
                new CCMenuItemFont("Bottom", setAlignmentBottom)
                );
            menu.AlignItemsVerticallyWithPadding(4);
            menu.Position = new CCPoint(s.Width - 50, s.Height / 2 - 20);
            AddChild(menu);

            m_plabel = null;
            m_eHorizAlign = CCTextAlignment.CCTextAlignmentLeft;
            m_eVertAlign = CCVerticalTextAlignment.CCVerticalTextAlignmentTop;

            updateAlignment();
        }
示例#24
0
 public CCLabelTTF (string text, string fontName, float fontSize, CCSize dimensions, CCTextAlignment hAlignment,
                    CCVerticalTextAlignment vAlignment)
 {
     InitWithString(text, fontName, fontSize, dimensions, hAlignment, vAlignment);
 }
示例#25
0
        public bool InitWithString(string text, string fontName, float fontSize,
                                   CCSize dimensions, CCTextAlignment hAlignment,
                                   CCVerticalTextAlignment vAlignment)
        {
            if (base.Init())
            {
                // shader program
                //this->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(SHADER_PROGRAM));

                m_tDimensions = new CCSize(dimensions.Width, dimensions.Height);
                m_hAlignment = hAlignment;
                m_vAlignment = vAlignment;
                m_pFontName = fontName;
                m_fFontSize = fontSize;

                Label = (text);

                return true;
            }

            return false;
        }
示例#26
0
        protected virtual bool InitWithString(string theString, string fntFile, CCSize dimentions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment,
                                              CCPoint imageOffset, CCTexture2D texture)
        {
            Debug.Assert(m_pConfiguration == null, "re-init is no longer supported");
            Debug.Assert((theString == null && fntFile == null) || (theString != null && fntFile != null),
                         "Invalid params for CCLabelBMFont");

            if (!String.IsNullOrEmpty(fntFile))
            {
                CCBMFontConfiguration newConf = FNTConfigLoadFile(fntFile);
                if (newConf == null)
                {
                    CCLog.Log("CCLabelBMFont: Impossible to create font. Please check file: '{0}'", fntFile);
                    return false;
                }

                m_pConfiguration = newConf;

                m_sFntFile = fntFile;

                if (texture == null)
                {
                    try
                    {
                        texture = CCTextureCache.SharedTextureCache.AddImage(m_pConfiguration.AtlasName);
                    }
                    catch (Exception)
                    {
                        // Try the 'images' ref location just in case.
                        try
                        {
                            texture =
                                CCTextureCache.SharedTextureCache.AddImage(System.IO.Path.Combine("images",
                                                                                                  m_pConfiguration
                                                                                                      .AtlasName));
                        }
                        catch (Exception)
                        {
                            // Lastly, try <font_path>/images/<font_name>
                            string dir = System.IO.Path.GetDirectoryName(m_pConfiguration.AtlasName);
                            string fname = System.IO.Path.GetFileName(m_pConfiguration.AtlasName);
                            string newName = System.IO.Path.Combine(System.IO.Path.Combine(dir, "images"), fname);
                            texture = CCTextureCache.SharedTextureCache.AddImage(newName);
                        }
                    }
                }
            }
            else
            {
                texture = new CCTexture2D();
            }

            if (String.IsNullOrEmpty(theString))
            {
                theString = String.Empty;
            }

            if (base.InitWithTexture(texture, theString.Length))
            {
                m_tDimensions = dimentions;

                m_pHAlignment = hAlignment;
                m_pVAlignment = vAlignment;

                m_cDisplayedOpacity = m_cRealOpacity = 255;
                m_tDisplayedColor = m_tRealColor = CCTypes.CCWhite;
                m_bCascadeOpacityEnabled = true;
                m_bCascadeColorEnabled = true;

                m_obContentSize = CCSize.Zero;

                m_bIsOpacityModifyRGB = m_pobTextureAtlas.Texture.HasPremultipliedAlpha;
                AnchorPoint = new CCPoint(0.5f, 0.5f);

                m_tImageOffset = imageOffset;

                m_pReusedChar = new CCSprite();
                m_pReusedChar.InitWithTexture(m_pobTextureAtlas.Texture, CCRect.Zero, false);
                m_pReusedChar.BatchNode = this;

                SetString(theString, true);

                return true;
            }
            return false;
        }
示例#27
0
 public CCLabelBMFont(string str, string fntFile, float width, CCTextAlignment alignment)
     : this(str, fntFile, width, alignment, CCPoint.Zero)
 {
 }
 public CCLabelTTF(string text, string fontName, float fontSize, Vector2 dimensions,
                   CCTextAlignment hAlignment         = CCTextAlignment.Left,
                   CCVerticalTextAlignment vAlignment = CCVerticalTextAlignment.Top)
 {
     init(text, fontName, fontSize, dimensions, hAlignment, vAlignment);
 }
示例#29
0
        protected void InitCCLabelBMFont(string theString, string fntFile, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment,
                                         CCPoint imageOffset, CCTexture2D texture)
        {
            Debug.Assert(FontConfiguration == null, "re-init is no longer supported");
            Debug.Assert((theString == null && fntFile == null) || (theString != null && fntFile != null),
                         "Invalid params for CCLabelBMFont");

            if (!String.IsNullOrEmpty(fntFile))
            {
                CCBMFontConfiguration newConf = FNTConfigLoadFile(fntFile);
                if (newConf == null)
                {
                    CCLog.Log("CCLabelBMFont: Impossible to create font. Please check file: '{0}'", fntFile);
                    return;
                }

                FontConfiguration = newConf;

                fntConfigFile = fntFile;

                if (texture == null)
                {
                    try
                    {
                        texture = CCTextureCache.SharedTextureCache.AddImage(FontConfiguration.AtlasName);
                    }
                    catch (Exception)
                    {
                        // Try the 'images' ref location just in case.
                        try
                        {
                            texture =
                                CCTextureCache.SharedTextureCache.AddImage(System.IO.Path.Combine("images",
                                                                                                  FontConfiguration
                                                                                                  .AtlasName));
                        }
                        catch (Exception)
                        {
                            // Lastly, try <font_path>/images/<font_name>
                            string dir     = System.IO.Path.GetDirectoryName(FontConfiguration.AtlasName);
                            string fname   = System.IO.Path.GetFileName(FontConfiguration.AtlasName);
                            string newName = System.IO.Path.Combine(System.IO.Path.Combine(dir, "images"), fname);
                            texture = CCTextureCache.SharedTextureCache.AddImage(newName);
                        }
                    }
                }
            }
            else
            {
                texture = new CCTexture2D();
            }

            if (String.IsNullOrEmpty(theString))
            {
                theString = String.Empty;
            }

            base.InitCCSpriteBatchNode(texture, theString.Length);

            this.labelDimensions = dimensions;

            horzAlignment = hAlignment;
            vertAlignment = vAlignment;

            IsOpacityCascaded = true;

            ContentSize = CCSize.Zero;

            IsColorModifiedByOpacity = TextureAtlas.Texture.HasPremultipliedAlpha;
            AnchorPoint = CCPoint.AnchorMiddle;

            ImageOffset = imageOffset;

            SetString(theString, true);
        }
示例#30
0
 public CCLabelTTF(string text, string fontName, float fontSize, CCSize dimensions, CCTextAlignment hAlignment) :
     this(text, fontName, fontSize, dimensions, hAlignment, CCVerticalTextAlignment.CCVerticalTextAlignmentTop)
 {
 }
示例#31
0
        internal static CCTexture2D CreateNativeLabel(string text, CCSize dimensions, CCTextAlignment hAlignment,
                                                      CCVerticalTextAlignment vAlignment, string fontName,
                                                      float fontSize, CCColor4B textColor)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(new CCTexture2D());
            }

            var font = CreateFont(fontName, fontSize);

            if (dimensions.Equals(CCSize.Zero))
            {
                CreateBitmap(1, 1);

                var ms = _graphics.MeasureString(text, font);

                dimensions.Width  = ms.Width;
                dimensions.Height = ms.Height;
            }

            CreateBitmap((int)dimensions.Width, (int)dimensions.Height);

            var stringFormat = new StringFormat();

            switch (hAlignment)
            {
            case CCTextAlignment.Left:
                stringFormat.Alignment = StringAlignment.Near;
                break;

            case CCTextAlignment.Center:
                stringFormat.Alignment = StringAlignment.Center;
                break;

            case CCTextAlignment.Right:
                stringFormat.Alignment = StringAlignment.Far;
                break;
            }

            switch (vAlignment)
            {
            case CCVerticalTextAlignment.Top:
                stringFormat.LineAlignment = StringAlignment.Near;
                break;

            case CCVerticalTextAlignment.Center:
                stringFormat.LineAlignment = StringAlignment.Center;
                break;

            case CCVerticalTextAlignment.Bottom:
                stringFormat.LineAlignment = StringAlignment.Far;
                break;
            }

            _graphics.DrawString(text, font, _brush, new RectangleF(0, 0, dimensions.Width, dimensions.Height), stringFormat);
            _graphics.Flush();

            var texture = new CCTexture2D(SaveToStream(), CCSurfaceFormat.Bgra4444);

            return(texture);
        }
示例#32
0
        protected void InitSpriteFont(string theString, string fntFile, float fontSize, CCSize dimensions, CCLabelFormat labelFormat, 
            CCPoint imageOffset, CCTexture2D texture)
        {
            Debug.Assert((theString == null && fntFile == null) || (theString != null && fntFile != null),
                "Invalid params for CCLabel SpriteFont");

            if (!String.IsNullOrEmpty(fntFile))
            {
                try
                {
                    FontAtlas = CCFontAtlasCache.GetFontAtlasSpriteFont(fntFile, fontSize, imageOffset);
                    Scale = FontAtlas.Font.FontScale;
                }
                catch {}

                if (FontAtlas == null)
                {
                    CCLog.Log("SpriteFont CCLabel: Impossible to create font. Please check file: '{0}'", fntFile);
                    return;
                }

            }

            AnchorPoint = CCPoint.AnchorMiddle;

            LabelType = CCLabelType.SpriteFont;

            if (String.IsNullOrEmpty(theString))
            {
                theString = String.Empty;
            }

            // Initialize the TextureAtlas along with children.
            var capacity = theString.Length;

            BlendFunc = CCBlendFunc.AlphaBlend;

            if (capacity == 0)
            {
                capacity = defaultSpriteBatchCapacity;
            }

            UpdateBlendFunc();

            // no lazy alloc in this node
            Children = new CCRawList<CCNode>(capacity);
            Descendants = new CCRawList<CCSprite>(capacity);

            this.labelDimensions = dimensions;

            horzAlignment = labelFormat.Alignment;
            vertAlignment = labelFormat.LineAlignment;

            IsOpacityCascaded = true;

            ContentSize = CCSize.Zero;

            IsColorModifiedByOpacity = TextureAtlas.Texture.HasPremultipliedAlpha;
            AnchorPoint = CCPoint.AnchorMiddle;

            ImageOffset = imageOffset;

            Text = theString;
        }
示例#33
0
        protected void InitBMFont(string theString, string fntFile, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, 
            CCPoint imageOffset, CCTexture2D texture)
        {
            Debug.Assert(FontConfiguration == null, "re-init is no longer supported");
            Debug.Assert((theString == null && fntFile == null) || (theString != null && fntFile != null),
                "Invalid params for CCLabelBMFont");

            if (!String.IsNullOrEmpty(fntFile))
            {
                try
                {
                    FontAtlas = CCFontAtlasCache.GetFontAtlasFNT(fntFile, imageOffset);
                }
                catch {}

                if (FontAtlas == null)
                {
                    CCLog.Log("Bitmap Font CCLabel: Impossible to create font. Please check file: '{0}'", fntFile);
                    return;
                }

            }

            AnchorPoint = CCPoint.AnchorMiddle;

            FontConfiguration = CCBMFontConfiguration.FontConfigurationWithFile(fntFile);

            LabelType = CCLabelType.BitMapFont;

            if (String.IsNullOrEmpty(theString))
            {
                theString = String.Empty;
            }

            // Initialize the TextureAtlas along with children.
            var capacity = theString.Length;

            BlendFunc = CCBlendFunc.AlphaBlend;

            if (capacity == 0)
            {
                capacity = defaultSpriteBatchCapacity;
            }

            UpdateBlendFunc();

            // no lazy alloc in this node
            Children = new CCRawList<CCNode>(capacity);
            Descendants = new CCRawList<CCSprite>(capacity);

            this.labelDimensions = dimensions;

            horzAlignment = hAlignment;
            vertAlignment = vAlignment;

            IsOpacityCascaded = true;

            // We use base here so we do not trigger an update internally.
            base.ContentSize = CCSize.Zero;

            IsColorModifiedByOpacity = TextureAtlas.Texture.HasPremultipliedAlpha;
            AnchorPoint = CCPoint.AnchorMiddle;

            ImageOffset = imageOffset;

            Text = theString;
        }
示例#34
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CocosSharp.CCLabel"/> class.
        /// </summary>
        /// <param name="str">Initial text of the label.</param>
        /// <param name="fntFile">Font definition file to use.</param>
        /// <param name="size">Font point size.</param>
        /// <param name="dimensions">Dimensions that the label should use to layout it's text.</param>
        /// <param name="labelFormat">Label format <see cref="CocosSharp.CCLabelFormat"/>.</param>
        /// <param name="imageOffset">Image offset.</param>
        /// <param name="texture">Texture atlas to be used.</param>
        public CCLabel(CCFontFNT fntFontConfig, string str, CCSize dimensions, CCLabelFormat labelFormat)
        {
            quadCommand = new CCQuadCommand(str.Length);

            labelFormat.FormatFlags = CCLabelFormatFlags.BitmapFont;
            AnchorPoint = CCPoint.AnchorMiddle;

            try
            {
                FontAtlas = CCFontAtlasCache.GetFontAtlasFNT(fntFontConfig);
            }
            catch { }

            if (FontAtlas == null)
            {
                CCLog.Log("Bitmap Font CCLabel: Impossible to create font. Please check CCFontFNT file: ");
                return;
            }

            LabelType = CCLabelType.BitMapFont;
            this.labelFormat = labelFormat;

            if (String.IsNullOrEmpty(str))
            {
                str = String.Empty;
            }

            // Initialize the TextureAtlas along with children.
            var capacity = str.Length;

            BlendFunc = CCBlendFunc.AlphaBlend;

            if (capacity == 0)
            {
                capacity = defaultSpriteBatchCapacity;
            }

            UpdateBlendFunc();

            // no lazy alloc in this node
            Children = new CCRawList<CCNode>(capacity);
            Descendants = new CCRawList<CCSprite>(capacity);

            this.labelDimensions = dimensions;

            horzAlignment = labelFormat.Alignment;
            vertAlignment = labelFormat.LineAlignment;

            IsOpacityCascaded = true;

            // We use base here so we do not trigger an update internally.
            base.ContentSize = CCSize.Zero;

            IsColorModifiedByOpacity = TextureAtlas.Texture.HasPremultipliedAlpha;
            AnchorPoint = CCPoint.AnchorMiddle;

            ImageOffset = CCPoint.Zero;

            Text = str;
        }
示例#35
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CocosSharp.CCLabel"/> class.
 /// </summary>
 /// <param name="str">Initial text of the label.</param>
 /// <param name="fntFile">Font definition file to use.</param>
 /// <param name="dimensions">Dimensions that the label should use to layout it's text.</param>
 /// <param name="hAlignment">Horizontal alignment of the text.</param>
 /// <param name="vAlignement">Vertical alignement of the text.</param>
 /// <param name="imageOffset">Image offset.</param>
 /// <param name="texture">Texture Atlas to be used.</param>
 public CCLabel(string str, string fntFile, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, 
     CCPoint imageOffset, CCTexture2D texture)
     : this (str, fntFile, dimensions, 
         new CCLabelFormat() { Alignment = hAlignment, LineAlignment = vAlignment}, 
         imageOffset, texture)
 {   }
示例#36
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CocosSharp.CCLabel"/> class.
 /// </summary>
 /// <param name="str">Initial text of the label.</param>
 /// <param name="fntFile">Font definition file to use.</param>
 /// <param name="dimensions">Dimensions that the label should use to layout it's text.</param>
 /// <param name="hAlignment">Horizontal alignment of the text.</param>
 /// <param name="vAlignement">Vertical alignement of the text.</param>
 public CCLabel(string str, string fntFile, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignement)
     : this (str, fntFile, dimensions, hAlignment, vAlignement, CCPoint.Zero, null)
 {   }
示例#37
0
        public bool InitWithString(string text, CCSize dimensions, CCTextAlignment hAlignment,
                                   CCVerticalTextAlignment vAlignment, string fontName,
                                   float fontSize)
        {
            try
            {
                Debug.Assert(dimensions.Width >= 0 || dimensions.Height >= 0);

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

                float loadedSize = fontSize;

                SpriteFont font = CCSpriteFontCache.SharedInstance.TryLoadFont(fontName, fontSize, out loadedSize);

                if (font == null)
                {
                    CCLog.Log("Failed to load default font. No font supported.");
                    return(false);
                }

                float scale = 1f;

                if (loadedSize != 0)
                {
                    scale = fontSize / loadedSize;
                }

                if (dimensions.Equals(CCSize.Zero))
                {
                    Vector2 temp = font.MeasureString(text);
                    dimensions.Width  = temp.X * scale;
                    dimensions.Height = temp.Y * scale;
                }

                var textList = new List <String>();
                var nextText = new StringBuilder();

                string[] lineList = text.Split('\n');

                float spaceWidth = font.MeasureString(" ").X *scale;

                for (int j = 0; j < lineList.Length; ++j)
                {
                    string[] wordList = lineList[j].Split(' ');

                    float lineWidth = 0;
                    bool  firstWord = true;

                    for (int i = 0; i < wordList.Length; ++i)
                    {
                        float wordWidth = font.MeasureString(wordList[i]).X *scale;

                        if ((lineWidth + wordWidth) > dimensions.Width)
                        {
                            lineWidth = wordWidth;

                            if (nextText.Length > 0)
                            {
                                firstWord = true;
                                textList.Add(nextText.ToString());
#if XBOX || XBOX360
                                nextText.Length = 0;
#else
                                nextText.Clear();
#endif
                            }
                            else
                            {
                                lineWidth += wordWidth;
                                firstWord  = false;
                                textList.Add(wordList[i]);
                                continue;
                            }
                        }
                        else
                        {
                            lineWidth += wordWidth;
                        }
                        if (!firstWord)
                        {
                            nextText.Append(' ');
                            lineWidth += spaceWidth;
                        }

                        nextText.Append(wordList[i]);
                        firstWord = false;
                    }

                    textList.Add(nextText.ToString());
#if XBOX || XBOX360
                    nextText.Length = 0;
#else
                    nextText.Clear();
#endif
                }

                if (dimensions.Height == 0)
                {
                    dimensions.Height = textList.Count * font.LineSpacing * scale;
                }

                //*  for render to texture
                RenderTarget2D renderTarget = CCDrawManager.CreateRenderTarget(
                    (int)dimensions.Width, (int)dimensions.Height,
                    DefaultAlphaPixelFormat, RenderTargetUsage.DiscardContents
                    );

                CCDrawManager.SetRenderTarget(renderTarget);
                CCDrawManager.Clear(Color.Transparent);

                SpriteBatch sb = CCDrawManager.spriteBatch;
                sb.Begin();

                float textHeight = textList.Count * font.LineSpacing * scale;
                float nextY      = 0;

                if (vAlignment == CCVerticalTextAlignment.Bottom)
                {
                    nextY = dimensions.Height - textHeight;
                }
                else if (vAlignment == CCVerticalTextAlignment.Center)
                {
                    nextY = (dimensions.Height - textHeight) / 2.0f;
                }

                for (int j = 0; j < textList.Count; ++j)
                {
                    string line = textList[j];

                    var position = new Vector2(0, nextY);

                    if (hAlignment == CCTextAlignment.Right)
                    {
                        position.X = dimensions.Width - font.MeasureString(line).X *scale;
                    }
                    else if (hAlignment == CCTextAlignment.Center)
                    {
                        position.X = (dimensions.Width - font.MeasureString(line).X *scale) / 2.0f;
                    }

                    sb.DrawString(font, line, position, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0);

                    nextY += font.LineSpacing * scale;
                }

                sb.End();

                CCDrawManager.graphicsDevice.RasterizerState   = RasterizerState.CullNone;
                CCDrawManager.graphicsDevice.DepthStencilState = DepthStencilState.Default;

                CCDrawManager.SetRenderTarget((RenderTarget2D)null);

                if (InitWithTexture(renderTarget, renderTarget.Format, true, false))
                {
                    m_CacheInfo.CacheType = CCTextureCacheType.String;
                    m_CacheInfo.Data      = new CCStringCache()
                    {
                        Dimensions = dimensions,
                        Text       = text,
                        FontName   = fontName,
                        FontSize   = fontSize,
                        HAlignment = hAlignment,
                        VAlignment = vAlignment
                    };

                    return(true);
                }
            }
            catch (Exception ex)
            {
                CCLog.Log(ex.ToString());
            }
            return(false);
        }
示例#38
0
 private void setAlignmentCenter(object pSender)
 {
     m_eHorizAlign = CCTextAlignment.CCTextAlignmentCenter;
     updateAlignment();
 }
示例#39
0
 public bool InitWithString(string label, string fontName, float fontSize, CCSize dimensions, CCTextAlignment alignment)
 {
     return(InitWithString(label, fontName, fontSize, dimensions, alignment, CCVerticalTextAlignment.CCVerticalTextAlignmentTop));
 }
示例#40
0
        public static CCTextFieldTTF textFieldWithPlaceHolder(string placeholder, CCSize dimensions, CCTextAlignment alignment, string fontName, float fontSize)
        {
            CCTextFieldTTF cCTextFieldTTF = new CCTextFieldTTF();

            if (cCTextFieldTTF == null || !cCTextFieldTTF.initWithPlaceHolder("", dimensions, alignment, fontName, fontSize))
            {
                return(null);
            }
            if (placeholder != null)
            {
                cCTextFieldTTF.PlaceHolder = placeholder;
            }
            return(cCTextFieldTTF);
        }
示例#41
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);
 }
示例#42
0
 public CCTextField(string text, string fontName, float fontSize, CCSize dimensions, CCTextAlignment hAlignment, CCLabelFormat labelFormat)
     : this(text, fontName, fontSize, dimensions, hAlignment, CCVerticalTextAlignment.Top, labelFormat)
 {
 }
示例#43
0
        protected void InitSpriteFont(string theString, string fntFile, float fontSize, CCSize dimensions, CCLabelFormat labelFormat,
                                      CCPoint imageOffset, CCTexture2D texture)
        {
            Debug.Assert((theString == null && fntFile == null) || (theString != null && fntFile != null),
                         "Invalid params for CCLabel SpriteFont");

            if (!String.IsNullOrEmpty(fntFile))
            {
                try
                {
                    FontAtlas = CCFontAtlasCache.GetFontAtlasSpriteFont(fntFile, fontSize, imageOffset);
                }
                catch {}

                if (FontAtlas == null)
                {
                    CCLog.Log("SpriteFont CCLabel: Impossible to create font. Please check file: '{0}'", fntFile);
                    return;
                }
            }

            AnchorPoint = CCPoint.AnchorMiddle;

            currentLabelType = CCLabelType.SpriteFont;

            if (String.IsNullOrEmpty(theString))
            {
                theString = String.Empty;
            }

            // Initialize the TextureAtlas along with children.
            var capacity = theString.Length;

            BlendFunc = CCBlendFunc.AlphaBlend;

            if (capacity == 0)
            {
                capacity = defaultSpriteBatchCapacity;
            }

            UpdateBlendFunc();

            // no lazy alloc in this node
            Children    = new CCRawList <CCNode>(capacity);
            Descendants = new CCRawList <CCSprite>(capacity);

            this.labelDimensions = dimensions;

            horzAlignment = labelFormat.Alignment;
            vertAlignment = labelFormat.LineAlignment;

            IsOpacityCascaded = true;

            ContentSize = CCSize.Zero;

            IsColorModifiedByOpacity = TextureAtlas.Texture.HasPremultipliedAlpha;
            AnchorPoint = CCPoint.AnchorMiddle;

            ImageOffset = imageOffset;

            Text = theString;
        }
示例#44
0
 public CCTextField(string text, string fontName, float fontSize, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment)
     : this(text, fontName, fontSize, dimensions, hAlignment, vAlignment, new CCLabelFormat(CCLabelFormatFlags.Unknown))
 { }
示例#45
0
 public CCLabelBMFont(string str, string fntFile, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment,
                      CCPoint imageOffset, CCTexture2D texture)
 {
     InitCCLabelBMFont(str, fntFile, dimensions, hAlignment, vAlignment, imageOffset, texture);
 }
示例#46
0
        public CCTextField(string text, string fontName, float fontSize, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, CCLabelFormat labelFormat)
            : base(text, fontName, fontSize, dimensions, labelFormat)
        {
            this.HorizontalAlignment = hAlignment;
            this.VerticalAlignment = vAlignment;
            AutoRepeat = true;
            placeHolderText = text;
            updateColors();
            TextFieldIMEImplementation = IMEKeyboardImpl.SharedInstance;

        }
示例#47
0
 public CCLabelBMFont(string str, string fntFile, float width, CCTextAlignment alignment, CCPoint imageOffset)
 {
     InitWithString(str, fntFile, new CCSize(width, 0), alignment, CCVerticalTextAlignment.Top, imageOffset, null);
 }
示例#48
0
        /**
        Extensions to make it easy to create a CCTexture2D object from a string of text.
        Note that the generated textures are of type A8 - use the blending mode (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA).
        */

        public bool initWithString(string text, CCSize dimensions, CCTextAlignment alignment, string fontName, float fontSize)
        {
            return (initWithString(text, dimensions, alignment, fontName, fontSize, Color.YellowGreen, g_MyBlack));
        }
示例#49
0
 public bool InitWithString(string label, string fontName, float fontSize, CCSize dimensions, CCTextAlignment alignment)
 {
     return InitWithString(label, fontName, fontSize, dimensions, alignment, CCVerticalTextAlignment.Top);
 }
示例#50
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);
        }
示例#51
0
 public CCLabelTTF (string text, string fontName, float fontSize, CCSize dimensions, CCTextAlignment hAlignment) :
     this (text, fontName, fontSize, dimensions, hAlignment, CCVerticalTextAlignment.Top)
 { }
示例#52
0
 private void setAlignmentLeft(object pSender)
 {
     m_eHorizAlign = CCTextAlignment.Left;
     updateAlignment();
 }
示例#53
0
 public CCLabel(string str, string fntFile, float width, CCTextAlignment alignment)
     : this(str, fntFile, width, alignment, CCPoint.Zero)
 {
 }
示例#54
0
 public CCLabel(string str, string fntFile, float width, CCTextAlignment alignment, CCPoint imageOffset)
     : this(str, fntFile, width, alignment, imageOffset, null)
 {
 }
示例#55
0
 private void setAlignmentRight(object pSender)
 {
     m_eHorizAlign = CCTextAlignment.CCTextAlignmentRight;
     updateAlignment();
 }
示例#56
0
 public CCLabel(string str, string fntFile, float width, CCTextAlignment alignment, CCPoint imageOffset, CCTexture2D texture)
     : this(str, fntFile, width, alignment, CCVerticalTextAlignment.Top, imageOffset, null)
 {
 }
示例#57
0
        public bool InitWithString(string text, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, string fontName,
                                   float fontSize)
        {
            try
            {
                m_CallParams = new object[] { text, dimensions, hAlignment, vAlignment, fontName, fontSize };

                // CCLog.Log("InitWithString: text={0}", text);

                Debug.Assert(dimensions.Width >= 0 || dimensions.Height >= 0);

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

                SpriteFont font = m_spriteFont;

                if (font == null)
                {
                    font = CCSpriteFontCache.SharedInstance.GetFont(fontName, fontSize);
                    if (font == null)
                    {
                        CCLog.Log("Can't find {0}, use system default ({1})", fontName, CCDrawManager.DefaultFont);
                        font = CCSpriteFontCache.SharedInstance.GetFont(CCDrawManager.DefaultFont, fontSize);
                        if (font == null)
                        {
                            CCLog.Log("Failed to load default font. No font supported.");
                        }
                    }
                    // m_spriteFont = font;
                }

                if (font == null)
                {
                    return(false);
                }

                // m_spriteFont = font;

                if (dimensions.Equals(CCSize.Zero))
                {
                    Microsoft.Xna.Framework.Vector2 temp = font.MeasureString(text);
                    dimensions.Width  = temp.X;
                    dimensions.Height = temp.Y;
                }

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

                var      textList = new List <String>();
                var      nextText = new StringBuilder();
                string[] lineList = text.Split('\n');

                float spaceWidth = font.MeasureString(" ").X;

                for (int j = 0; j < lineList.Length; ++j)
                {
                    string[] wordList = lineList[j].Split(' ');

                    float lineWidth = 0;
                    bool  firstWord = true;
                    for (int i = 0; i < wordList.Length; ++i)
                    {
                        lineWidth += font.MeasureString(wordList[i]).X;

                        if (lineWidth > dimensions.Width)
                        {
                            lineWidth = 0;

                            if (nextText.Length > 0)
                            {
                                firstWord = true;
                                textList.Add(nextText.ToString());
#if XBOX || XBOX360
                                nextText.Length = 0;
#else
                                nextText.Clear();
#endif
                            }
                            else
                            {
                                firstWord = false;
                                textList.Add(wordList[i]);
                                continue;
                            }
                        }

                        if (!firstWord)
                        {
                            nextText.Append(' ');
                            lineWidth += spaceWidth;
                        }

                        nextText.Append(wordList[i]);
                        firstWord = false;
                    }

                    textList.Add(nextText.ToString());
#if XBOX || XBOX360
                    nextText.Length = 0;
#else
                    nextText.Clear();
#endif
                }

                if (dimensions.Height == 0)
                {
                    dimensions.Height = textList.Count * font.LineSpacing;
                }

                //*  for render to texture
                RenderTarget2D renderTarget = CCDrawManager.CreateRenderTarget((int)dimensions.Width, (int)dimensions.Height,
                                                                               RenderTargetUsage.PreserveContents);
                CCDrawManager.SetRenderTarget(renderTarget);

                CCDrawManager.Clear(Color.Transparent);

                SpriteBatch spriteBatch = CCDrawManager.spriteBatch;

                spriteBatch.Begin();

                int   textHeight = textList.Count * font.LineSpacing;
                float nextY      = 0;

                if (vAlignment == CCVerticalTextAlignment.CCVerticalTextAlignmentBottom)
                {
                    nextY = dimensions.Height - textHeight;
                }
                else if (vAlignment == CCVerticalTextAlignment.CCVerticalTextAlignmentCenter)
                {
                    nextY = (dimensions.Height - textHeight) / 2.0f;
                }

                for (int j = 0; j < textList.Count; ++j)
                {
                    string line = textList[j];

                    var position = new Microsoft.Xna.Framework.Vector2(0, nextY);

                    if (hAlignment == CCTextAlignment.CCTextAlignmentRight)
                    {
                        position.X = dimensions.Width - font.MeasureString(line).X;
                    }
                    else if (hAlignment == CCTextAlignment.CCTextAlignmentCenter)
                    {
                        position.X = (dimensions.Width - font.MeasureString(line).X) / 2.0f;
                    }

#if MONOMAC
                    // It seems that MonoGame has an initialization problem with MONOMAC
                    // what we are doing here is a HACK and no doubt about it.  We can take this
                    // work around out when the issue is addressed in MonoGame.
                    // The issue is that if we do not re-initialize the font for some reason it is
                    // not drawing the next label if it is the same font and size.
                    spriteBatch.DrawString(hackFont, " ", position, Color.White);
#endif
                    spriteBatch.DrawString(font, line, position, Color.White);
                    nextY += font.LineSpacing;
                }
                spriteBatch.End();

                CCDrawManager.graphicsDevice.RasterizerState   = RasterizerState.CullNone;
                CCDrawManager.graphicsDevice.DepthStencilState = DepthStencilState.Default;

                CCDrawManager.SetRenderTarget((RenderTarget2D)null);

                // to copy the rendered target data to a plain texture(to the memory)
                //            texture2D = CCDrawManager.CreateTexture2D(renderTarget.Width, renderTarget.Height);
                // This is the old 3.1 way of doing things. 4.0 does not need this and it causes compatibility problems.

                //            var colors1D = new Color[renderTarget.Width * renderTarget.Height];
                //            renderTarget.GetData(colors1D);
                //            texture2D.SetData(colors1D);
                return(InitWithTexture(renderTarget));
            }
            catch (Exception ex)
            {
                CCLog.Log(ex.ToString());
            }
            return(false);
        }
示例#58
0
 public CCLabel(string str, string fntFile, float width, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment,
                CCPoint imageOffset, CCTexture2D texture)
     : this(str, fntFile, new CCSize(width, 0), hAlignment, vAlignment, imageOffset, texture)
 {
 }
示例#59
0
 public CCLabelTTF(string text, string fontName, float fontSize, CCSize dimensions, CCTextAlignment hAlignment,
                   CCVerticalTextAlignment vAlignment)
 {
     InitWithString(text, fontName, fontSize, dimensions, hAlignment, vAlignment);
 }
示例#60
0
 public bool initWithString(string text, CCSize dimensions, CCTextAlignment alignment, string fontName, float fontSize)
 {
     return(this.initWithString(text, dimensions, alignment, fontName, fontSize, Color.YellowGreen, g_MyBlack));
 }