public static Texture2D FromText(string text, SpriteFont font, Color color, Size size, bool multiLine, int lineStart, GraphicsDevice device) { string[] drawAbleText = multiLine ? text.Split(new string[1] { "\n" }, StringSplitOptions.None) : new string[1] { text }; RenderTarget2D target = new RenderTarget2D(device, size.Width, size.Height); SpriteBatch sb = new SpriteBatch(device); device.SetRenderTarget(target); device.Clear(Color.Transparent); sb.Begin(); for (int i = lineStart; i < drawAbleText.Length; i++) { float y = 1 + (i - lineStart) * font.GetHeight(); sb.DrawString(font, drawAbleText[i], new Vector2(1, y), color, 0f, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f); } sb.End(); device.SetRenderTarget(null); Texture2D texture = new Texture2D(device, size.Width, size.Height); Color[] colorData = target.GetColorData(); texture.SetData(colorData); target.Dispose(); sb.Dispose(); return texture; }
public static Texture2D FromLabels(KeyValuePair<string, Color>[] labels, SpriteFont font, Size size, GraphicsDevice device) { RenderTarget2D target = new RenderTarget2D(device, size.Width, size.Height); SpriteBatch sb = new SpriteBatch(device); device.SetRenderTarget(target); device.Clear(Color.Transparent); sb.Begin(); for (int i = 0; i < labels.Length; i++) { string prevString = string.Join(" ", labels.Select(l => l.Key).ToArray(), 0, i); if (i > 0) prevString += " "; float x = 1 + font.MeasureString(prevString).X; sb.DrawString(font, labels[i].Key, new Vector2(x, 1), labels[i].Value, 0f, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f); } sb.End(); device.SetRenderTarget(null); Texture2D texture = new Texture2D(device, size.Width, size.Height); Color[] colorData = target.GetColorData(); texture.SetData(colorData); target.Dispose(); return texture; }
public Box(Size baseSize, Vector2 position, Vector2 scale, float rotation = 0, Vector2? origin = null) { _size = baseSize.Half; _position = position; _scale = scale; _rotation = new Rotation(rotation); _origin = origin ?? Vector2.Zero; Transform(); }
public static Texture2D FromColor(Color color, Size size, Rectangle destinationRectangle, GraphicsDevice device) { Texture2D texture = new Texture2D(device, size.Width, size.Height); Color[] data = new Color[size.Width * size.Height]; for (int i = 0; i < data.Length; i++) { Vector2 pos = new Vector2(i % size.Width, (i - (i % size.Width)) / size.Width); if (destinationRectangle.Contains(pos.ToPoint())) data[i] = color; else data[i] = Color.Transparent; } texture.SetData(data); return texture; }
public static Texture2D FromColor(Color color, Size size, GraphicsDevice device) { Texture2D texture = new Texture2D(device, size.Width, size.Height); Color[] data = new Color[size.Width * size.Height]; for (int i = 0; i < data.Length; i++) { data[i] = color; } texture.SetData(data); return texture; }
/// <summary> /// Checks if current size is equal to other size /// </summary> /// <param name="other">Other size</param> /// <returns>True if equals, otherwise false</returns> public bool Equals(Size other) { return Math.Abs(_width - other.Width) <= float.Epsilon && Math.Abs(_height - other.Height) <= float.Epsilon; }
public TextBox(GraphicsDevice device, Rectangle bounds, SpriteFont font) : base(device, bounds, font) { inputHandler = new KeyInputHandler(); FlickerStyle = FlickerStyle.Normal; MinimumSize = DefaultMinimumSize; MaximumSize = device.Viewport.Bounds.Size(); FocusChanged += OnFocusChanged; }
public static Texture2D Stretch(this Texture2D texture, Size size) { RenderTarget2D target = new RenderTarget2D(texture.GraphicsDevice, size.Width, size.Height); SpriteBatch sb = new SpriteBatch(texture.GraphicsDevice); texture.GraphicsDevice.SetRenderTarget(target); texture.GraphicsDevice.Clear(Color.Transparent); sb.Begin(); sb.Draw(texture, new Rectangle(0, 0, size.Width, size.Height), Color.White); sb.End(); texture.GraphicsDevice.SetRenderTarget(null); Texture2D result = new Texture2D(texture.GraphicsDevice, size.Width, size.Height); result.SetData(target.GetColorData()); target.Dispose(); sb.Dispose(); return result; }
public static Texture2D RenderOnto(this Texture2D texture, Size size, Vector2 position = default(Vector2), float rotation = 0, Vector2 scale = default(Vector2)) { RenderTarget2D target = new RenderTarget2D(texture.GraphicsDevice, size.Width, size.Height); SpriteBatch sb = new SpriteBatch(texture.GraphicsDevice); texture.GraphicsDevice.SetRenderTarget(target); texture.GraphicsDevice.Clear(Color.Transparent); sb.Begin(); sb.Draw(texture, position, null, Color.White, rotation, Vector2.Zero, scale == default(Vector2) ? Vector2.One : scale, SpriteEffects.None, 1f); sb.End(); texture.GraphicsDevice.SetRenderTarget(null); Texture2D result = new Texture2D(texture.GraphicsDevice, size.Width, size.Height); result.SetData(target.GetColorData()); target.Dispose(); sb.Dispose(); return result; }