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; }
/** * Sets the background sprite to use for the specified button state. * * @param sprite The background sprite to use for the specified state. * @param state The state that uses the specified image. The values are described * in "CCControlState". */ public virtual void SetBackgroundSpriteForState(CCScale9Sprite sprite, CCControlState state) { CCSize oldPreferredSize = m_preferredSize; CCScale9Sprite previousBackgroundSprite; if (m_backgroundSpriteDispatchTable.TryGetValue(state, out previousBackgroundSprite)) { RemoveChild(previousBackgroundSprite, true); m_backgroundSpriteDispatchTable.Remove(state); } m_backgroundSpriteDispatchTable.Add(state, sprite); sprite.Visible = false; sprite.AnchorPoint = new CCPoint(0.5f, 0.5f); AddChild(sprite); if (m_preferredSize.Width != 0 || m_preferredSize.Height != 0) { if (oldPreferredSize.Equals(m_preferredSize)) { // Force update of preferred size sprite.PreferredSize = new CCSize(oldPreferredSize.Width + 1, oldPreferredSize.Height + 1); } sprite.PreferredSize = m_preferredSize; } // If the current state if equal to the given state we update the layout if (State == state) { NeedsLayout(); } }
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); }
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); }
public bool Equals(CCRect rect) { return(Origin.Equals(rect.Origin) && Size.Equals(rect.Size)); }
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); }
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; } var 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.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 Microsoft.Xna.Framework.Vector2(0, nextY); if (hAlignment == CCTextAlignment.Right) { position.X = dimensions.Width - font.MeasureString(line).X; } else if (hAlignment == CCTextAlignment.Center) { position.X = (dimensions.Width - font.MeasureString(line).X) / 2.0f; } 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); }
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); }