/** * 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(CCNode sprite, CCControlState state) { CCSize oldPreferredSize = preferredSize; CCNode previousBackgroundSprite; if (backgroundSpriteDispatchTable.TryGetValue(state, out previousBackgroundSprite)) { RemoveChild(previousBackgroundSprite, true); backgroundSpriteDispatchTable.Remove(state); } backgroundSpriteDispatchTable.Add(state, sprite); sprite.Visible = false; sprite.AnchorPoint = new CCPoint(0.5f, 0.5f); AddChild(sprite); if (preferredSize.Width != 0 || preferredSize.Height != 0 && sprite is CCScale9Sprite) { var scale9 = ((CCScale9Sprite)sprite); if (oldPreferredSize.Equals(preferredSize)) { // Force update of preferred size scale9.PreferredSize = new CCSize(oldPreferredSize.Width + 1, oldPreferredSize.Height + 1); } scale9.PreferredSize = preferredSize; } // If the current state if equal to the given state we update the layout if (State == state) { NeedsLayout(); } }
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); }
void 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; } 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; } float scale = 1f; if (loadedSize != 0) { scale = fontSize / loadedSize * CCSpriteFontCache.FontScale; } if (dimensions.Equals(CCSize.Zero)) { CCVector2 temp = font.MeasureString(text).ToCCVector2(); 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()); nextText.Length = 0; } 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()); nextText.Clear(); } if (dimensions.Height == 0) { dimensions.Height = textList.Count * font.LineSpacing * scale; } //* for render to texture RenderTarget2D renderTarget = CCDrawManager.SharedDrawManager.CreateRenderTarget( (int)dimensions.Width, (int)dimensions.Height, DefaultAlphaPixelFormat, CCRenderTargetUsage.DiscardContents ); CCDrawManager.SharedDrawManager.CurrentRenderTarget = renderTarget; CCDrawManager.SharedDrawManager.Clear(CCColor4B.Transparent); SpriteBatch sb = CCDrawManager.SharedDrawManager.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 CCVector2(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.ToVector2(), Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0); nextY += font.LineSpacing * scale; } sb.End(); CCDrawManager.SharedDrawManager.XnaGraphicsDevice.RasterizerState = RasterizerState.CullNone; CCDrawManager.SharedDrawManager.DepthStencilState = DepthStencilState.Default; CCDrawManager.SharedDrawManager.CurrentRenderTarget = (RenderTarget2D)null; InitWithTexture(renderTarget, (CCSurfaceFormat)renderTarget.Format, true, false); cacheInfo.CacheType = CCTextureCacheType.String; cacheInfo.Data = new CCStringCache() { Dimensions = dimensions, Text = text, FontName = fontName, FontSize = fontSize, HAlignment = hAlignment, VAlignment = vAlignment }; } catch (Exception ex) { CCLog.Log(ex.ToString()); } }
public bool Equals(CCRect rect) { return(Origin.Equals(rect.Origin) && Size.Equals(rect.Size)); }