unsafe void SetBrushBuffer(Brush.BrushBuffer buffer) { deviceContext.UpdateSubresource(brushBuffer, 0, null, (IntPtr)(&buffer), 0, 0); deviceContext.PixelShader.SetConstantBuffer(1, brushBuffer); }
void SetBrush(Brush brush) { SetBrushBuffer(brush.CreateBuffer()); var textureBrush = brush as TextureBrush; if (textureBrush != null) { SetSamplerState(textureBrush.WrapMode, textureBrush.InterpolationMode == InterpolationMode.Default ? defaultInterpolationMode : textureBrush.InterpolationMode); SetTextureInner(textureBrush.Texture.ResourceView); } }
/// <summary> /// Draws formatted text. /// </summary> /// <param name="text">The text.</param> /// <param name="bounds">The bounds the text will be displayed in.</param> /// <param name="font">The font of the text.</param> /// <param name="size">The font size in points.</param> /// <param name="brush">The brush used to draw the text.</param> /// <param name="format">The format of the text.</param> public void DrawText(string text, Rectangle bounds, Font font, float size, TextFormat format, Brush brush) { var layout = new TextLayout(text, bounds.Size, font, size, format); DrawTextLayout(layout, bounds.Location, brush); }
/// <summary> /// Draws layouted text. /// </summary> /// <param name="layout">The precalculated layout.</param> /// <param name="location">THe location the layout will be displayed at.</param> /// <param name="brush">The brush used to draw the layout.</param> public void DrawTextLayout(TextLayout layout, Vector2 location, Brush brush) { }
/// <summary> /// Draws text. /// </summary> /// <param name="text">The text.</param> /// <param name="location">The location the text will be displayed at.</param> /// <param name="font">The font of the text.</param> /// <param name="size">The font size in points.</param> /// <param name="brush">The brush used to draw the text.</param> public void DrawText(string text, Vector2 location, Font font, float size, Brush brush) { if (string.IsNullOrWhiteSpace(text)) return; float scale = (size * Dpi) / (72 * font.UnitsPerEm); SetBrush(brush); int horizontalPosition = 0; int verticalPosition = font.Ascend; for (int i = 0; i < text.Length; i++) { if (char.IsControl(text[i])) { switch (text[i]) { case '\n': verticalPosition += font.Ascend - font.Descend + font.LineGap; horizontalPosition = 0; break; } } else { Font.Glyph glyph = font.GetGlyph(text[i]); if (glyph.Vertices.Length > 0) { textMatrix = Matrix.Transpose(Matrix.Translation(horizontalPosition + glyph.LeftSideBearing, -verticalPosition, 0) * Matrix.Scaling(scale, -scale, 1) * Matrix.Translation(location.X, location.Y, 0)); UpdateMatrixBuffer(); brush.UpdateVertices(glyph.Vertices); DrawVertices(glyph.Indices, glyph.Vertices); } horizontalPosition += glyph.AdvanceWidth; } } textMatrix = Matrix.Identity; UpdateMatrixBuffer(); }
/// <summary> /// Fills a set of triangles with a brush. /// </summary> /// <param name="indices">The index buffer.</param> /// <param name="vertices">The vertex buffer.</param> /// <param name="brush">The brush used to fill the triangles.</param> public void FillTriangleList(int[] indices, Vector2[] vertices, Brush brush) { SetBrush(brush); var realVertices = new Vertex[vertices.Length]; for (int i = 0; i < vertices.Length; i++) realVertices[i] = new Vertex(vertices[i]); brush.UpdateVertices(realVertices); DrawVertices(indices, realVertices); }
/// <summary> /// Fills a rectangle with a brush. /// </summary> /// <param name="rectangle">The rectangle to fill.</param> /// <param name="brush">The brush used to fill the rectangle.</param> public void FillRectangle(Rectangle rectangle, Brush brush) { SetBrush(brush); Vertex[] vertices = new[] { new Vertex(rectangle.Left, rectangle.Top), new Vertex(rectangle.Right, rectangle.Top), new Vertex(rectangle.Left, rectangle.Bottom), new Vertex(rectangle.Right, rectangle.Bottom), }; int[] indices = { 0, 1, 2, 1, 2, 3 }; brush.UpdateVertices(vertices); DrawVertices(indices, vertices, 0); }