Ejemplo n.º 1
1
        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;
		}
Ejemplo n.º 2
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";
 }
Ejemplo n.º 3
0
        public CCLabelTTF()
        {
            m_hAlignment = CCTextAlignment.Center;
            m_vAlignment = CCVerticalTextAlignment.Top;
            m_pFontName  = string.Empty;
            m_fFontSize  = 0.0f;

            Init();
        }
Ejemplo n.º 4
0
        public CCLabelTTF ()
        {
            m_hAlignment = CCTextAlignment.Center;
            m_vAlignment = CCVerticalTextAlignment.Top;
            m_pFontName = string.Empty;
            m_fFontSize = 0.0f;

            Init();
        }
Ejemplo n.º 5
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;
        }
Ejemplo n.º 6
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();
        }
Ejemplo n.º 8
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();
        }
Ejemplo n.º 10
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();
        }
Ejemplo n.º 11
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();
        }
Ejemplo n.º 12
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();
        }
Ejemplo n.º 13
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)
 {   }
Ejemplo n.º 14
0
 public CCTexture2D(string text, SizeF dimensions, UITextAlignment alignment, CCVerticalTextAlignment vertAlignment, string fontName, float fontSize)
     : this(text, fontName, fontSize, dimensions, alignment, vertAlignment)
 {
 }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
0
 private void setAlignmentTop(object pSender)
 {
     m_eVertAlign = CCVerticalTextAlignment.Top;
     updateAlignment();
 }
Ejemplo n.º 17
0
 public CCLabelTTF (string text, string fontName, float fontSize, CCSize dimensions, CCTextAlignment hAlignment,
                    CCVerticalTextAlignment vAlignment)
 {
     InitWithString(text, fontName, fontSize, dimensions, hAlignment, vAlignment);
 }
Ejemplo n.º 18
0
 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);
 }
Ejemplo n.º 19
0
 private void setAlignmentTop(object pSender)
 {
     m_eVertAlign = CCVerticalTextAlignment.Top;
     updateAlignment();
 }
Ejemplo n.º 20
0
        protected virtual bool InitWithString(string text, string fontFile, CCSize dimentions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, CCPoint imageOffset, CCTexture2D texture)
        {
            if (string.IsNullOrEmpty(fontFile) == false)
            {
                CCBMFontConfiguration newConfig = _fontConfigLoad(fontFile);
                if (newConfig == null)
                    return false;

                this._fontConfig = newConfig;
                //this._fontFile = fontFile;

                if (texture == null)
                {
                    //base.Texture = CCTextureCache.SharedTextureCache.AddImage(this._configuration.AtlasName);
                    texture = Factory.CreateTexture(this._fontConfig.AtlasName);
                }
            }
            else
            {
                texture = new CCTexture2D();
            }

            if (text == null) text = "";
            if (base.InitWithTexture(texture, text.Length))
            {
                this._dimensions = dimentions;
                this._horizontalAlignment = hAlignment;
                this._verticalAlignment = vAlignment;

                this._displayedOpacity = this._realOpacity = 255;
                this._displayedColor = this._realColor = CCTypes.CCWhite;
                this._isCascadeOpacityEnabled = true;
                this._isCascadeColorEnabled = true;
                base.ContentSize = CCSize.Zero;
                this._isOpacityModifyRGB = base.TextureAtlas.Texture.HasPremultipliedAlpha;
                this.AnchorPoint = new CCPoint(0.5f, 0.5f);
                this._imageOffset = imageOffset;
                this._reusedChar = new CCSprite();
                this._reusedChar.InitWithTexture(base.TextureAtlas.Texture, CCRect.Zero, false);
                this._reusedChar.BatchNode = this;
                this.SetString(text, true);
                return true;
            }
            return false;
        }
Ejemplo n.º 21
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 * CCSpriteFontCache.FontScale;
                }

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

                StringBuilder next = new StringBuilder();
                string        last = null;
                for (int j = 0; j < lineList.Length; ++j)
                {
                    string[] wordList = lineList[j].Split(' ');
                    for (int i = 0; i < wordList.Length;)
                    {
                        // Run through the list of words to create a sentence that fits in the dimensions.Width space
                        while (i < wordList.Length)
                        {
                            if ((font.MeasureString(next.ToString()).X *scale) > dimensions.Width)
                            {
                                i--;
                                break;
                            }
                            last = next.ToString();
                            if (next.Length > 0)
                            {
                                next.Append(' ');
                            }
                            next.Append(wordList[i]);
                            i++;
                        }
                        if (i == wordList.Length || i == -1) // -1 means the default width was too small for the string.
                        {
                            string nstr = next.ToString();
                            if ((font.MeasureString(nstr).X *scale) > dimensions.Width)
                            {
                                // Last line could have bleed into the margin
                                if (last != null && last.Length > 0)
                                {
                                    textList.Add(last);                      // Single word label has a null last which can cause problems
                                }
                                textList.Add(wordList[wordList.Length - 1]); // last word bleeds
                            }
                            else if (nstr.Length > 0)
                            {
                                textList.Add(nstr);
                            }
                        }
                        else if (last.Length > 0)
                        {
                            textList.Add(last);
                        }
                        last        = null;
                        next.Length = 0;
                    }

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

                if (textList.Count == 0 && text.Length > 0)
                {
                    textList.Add(text);
                }

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

                try
                {
                    CCDrawManager.SetRenderTarget(renderTarget);
                }
                catch (Exception)
                {
                    CCTextureCache.SharedTextureCache.RemoveUnusedTextures();
                    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);
        }
Ejemplo n.º 22
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);
			_quadColor = new Color32 (255, 255, 255, 255);

			_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;
		}
Ejemplo n.º 23
0
		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);
		}
Ejemplo n.º 24
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);
        }
Ejemplo n.º 25
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;
        }
Ejemplo n.º 26
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;
        }
Ejemplo n.º 27
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))
 { }
Ejemplo n.º 28
0
 public bool InitWithString(string text, string fontName, float fontSize, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment)
 {
     InitializeFont(fontName, fontSize, text);
     m_FontName = fontName;
     m_FontSize = fontSize;
     return(base.InitWithString(text, GetFontKey(fontName, fontSize), dimensions.PointsToPixels(), hAlignment, vAlignment, CCPoint.Zero, m_pTexture));
 }
Ejemplo n.º 29
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);
 }
Ejemplo n.º 30
0
 public CCLabel(string text, string fontName, float fontSize, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment) :
     this(text, fontName, fontSize, CCSize.Zero, hAlignment, vAlignment)
 {
 }
Ejemplo n.º 31
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;
        }
Ejemplo n.º 32
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";
 }
Ejemplo n.º 33
0
 private void setAlignmentMiddle(object pSender)
 {
     m_eVertAlign = CCVerticalTextAlignment.CCVerticalTextAlignmentCenter;
     updateAlignment();
 }
Ejemplo n.º 34
0
 public CCLabelBMFont(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)
 {
 }
Ejemplo n.º 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)
 {
 }
Ejemplo n.º 36
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);
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 38
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;
            }

            TextureAtlas = new CCTextureAtlas(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);
        }
Ejemplo n.º 39
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)
 {   }
Ejemplo n.º 40
0
 public CCTexture2D(string text, CCSize dimensions, CCTextAlignment hAlignment,
                    CCVerticalTextAlignment vAlignment, string fontName, float fontSize)
     : this()
 {
     InitWithString(text, dimensions, hAlignment, vAlignment, fontName, fontSize);
 }
Ejemplo n.º 41
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;
        }
Ejemplo n.º 42
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, DrawManager.DefaultFont);
                        font = CCSpriteFontCache.SharedInstance.GetFont(DrawManager.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 = DrawManager.CreateRenderTarget((int)dimensions.Width, (int)dimensions.Height,
                                                                              RenderTargetUsage.PreserveContents);
                DrawManager.SetRenderTarget(renderTarget);

                DrawManager.Clear(Color.Transparent);

                SpriteBatch spriteBatch = DrawManager.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();

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

                DrawManager.SetRenderTarget((RenderTarget2D)null);

                // to copy the rendered target data to a plain texture(to the memory)
                //            texture2D = DrawManager.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);
        }
Ejemplo n.º 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);
                    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;
        }
Ejemplo n.º 44
0
 public CCLabel(string text, string fontName, float fontSize, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment)
 {
     // Can't call base(text, ...), becasue we have to initialize font first
     InitializeFont(fontName, fontSize, text);
     this.fontName = fontName;
     this.fontSize = fontSize;
     base.InitCCLabelBMFont(text, GetFontKey(fontName, fontSize), dimensions, hAlignment, vAlignment, CCPoint.Zero, labelTexture);
 }
Ejemplo n.º 45
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;

        }
Ejemplo n.º 46
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));

            this.dimensions = new CCSize(dimensions.Width, dimensions.Height);
            horzTextAlignment = hAlignment;
            vertTextAlignment = vAlignment;
			if (fontName == null)
				fontName = "arial";
			this.fontName = (!string.IsNullOrEmpty(fontName.Trim())) ? fontName : "arial";
            this.fontSize = fontSize;

            this.Text = text;          
        }
Ejemplo n.º 47
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)
 {
 }
Ejemplo n.º 48
0
 public CCTexture2D(string text, CGSize dimensions, UITextAlignment alignment, CCVerticalTextAlignment vertAlignment, string fontName, float fontSize) : this(text, fontName, fontSize, dimensions, alignment, vertAlignment)
 {
 }
Ejemplo n.º 49
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;
        }
Ejemplo n.º 50
0
 private void setAlignmentBottom(object pSender)
 {
     m_eVertAlign = CCVerticalTextAlignment.Bottom;
     updateAlignment();
 }
Ejemplo n.º 51
0
 private void setAlignmentMiddle(object pSender)
 {
     m_eVertAlign = CCVerticalTextAlignment.Center;
     updateAlignment();
 }
Ejemplo n.º 52
0
 public CCLabel(string text, string fontName, float fontSize, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment) :
     this(text, fontName, fontSize, CCSize.Zero, hAlignment, vAlignment)
 { }
Ejemplo n.º 53
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;
        }
Ejemplo n.º 54
0
 public CCLabel(string text, string fontName, float fontSize, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment) 
 {
     // Can't call base(text, ...), becasue we have to initialize font first
     InitializeFont(fontName, fontSize, text);
     m_FontName = fontName;
     m_FontSize = fontSize;
     base.InitCCLabelBMFont(text, GetFontKey(fontName, fontSize), dimensions, hAlignment, vAlignment, CCPoint.Zero, m_pTexture);
 }
Ejemplo n.º 55
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);
        }
Ejemplo n.º 56
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);
 }
Ejemplo n.º 57
0
 private void setAlignmentBottom(object pSender)
 {
     m_eVertAlign = CCVerticalTextAlignment.CCVerticalTextAlignmentBottom;
     updateAlignment();
 }
Ejemplo n.º 58
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);
        }
Ejemplo n.º 59
0
 public CCLabelTTF(string text, string fontName, float fontSize, CCSize dimensions, CCTextAlignment hAlignment,
                   CCVerticalTextAlignment vAlignment)
 {
     InitWithString(text, fontName, fontSize, dimensions, hAlignment, vAlignment);
 }
Ejemplo n.º 60
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)
 {
 }