Browse Source

Implemented CornerRadius for Border

pull/57/head
Nelson Carrillo 11 years ago
parent
commit
6826ef19a5
  1. 14
      Perspex.Controls/Border.cs
  2. 4
      Perspex.SceneGraph/Media/IDrawingContext.cs
  3. 25
      Windows/Perspex.Direct2D1/Media/DrawingContext.cs

14
Perspex.Controls/Border.cs

@ -22,6 +22,9 @@ namespace Perspex.Controls
public static readonly PerspexProperty<double> BorderThicknessProperty = public static readonly PerspexProperty<double> BorderThicknessProperty =
PerspexProperty.Register<Border, double>("BorderThickness"); PerspexProperty.Register<Border, double>("BorderThickness");
public static readonly PerspexProperty<float> CornerRadiusProperty =
PerspexProperty.Register<Border, float>("CornerRadius");
static Border() static Border()
{ {
Control.AffectsRender(Border.BackgroundProperty); Control.AffectsRender(Border.BackgroundProperty);
@ -46,21 +49,28 @@ namespace Perspex.Controls
set { this.SetValue(BorderThicknessProperty, value); } set { this.SetValue(BorderThicknessProperty, value); }
} }
public float CornerRadius
{
get { return this.GetValue(CornerRadiusProperty); }
set { this.SetValue(CornerRadiusProperty, value); }
}
public override void Render(IDrawingContext context) public override void Render(IDrawingContext context)
{ {
Brush background = this.Background; Brush background = this.Background;
Brush borderBrush = this.BorderBrush; Brush borderBrush = this.BorderBrush;
double borderThickness = this.BorderThickness; double borderThickness = this.BorderThickness;
float cornerRadius = this.CornerRadius;
Rect rect = new Rect(this.Bounds.Size).Deflate(this.BorderThickness); Rect rect = new Rect(this.Bounds.Size).Deflate(this.BorderThickness);
if (background != null) if (background != null)
{ {
context.FillRectange(background, rect); context.FillRectange(background, rect, cornerRadius);
} }
if (borderBrush != null && borderThickness > 0) if (borderBrush != null && borderThickness > 0)
{ {
context.DrawRectange(new Pen(borderBrush, borderThickness), rect); context.DrawRectange(new Pen(borderBrush, borderThickness), rect, cornerRadius);
} }
} }

4
Perspex.SceneGraph/Media/IDrawingContext.cs

@ -39,7 +39,7 @@ namespace Perspex.Media
/// </summary> /// </summary>
/// <param name="pen">The pen.</param> /// <param name="pen">The pen.</param>
/// <param name="rect">The rectangle bounds.</param> /// <param name="rect">The rectangle bounds.</param>
void DrawRectange(Pen pen, Rect rect); void DrawRectange(Pen pen, Rect rect, float cornerRadius = 0.0f);
/// <summary> /// <summary>
/// Draws text. /// Draws text.
@ -54,7 +54,7 @@ namespace Perspex.Media
/// </summary> /// </summary>
/// <param name="brush">The brush.</param> /// <param name="brush">The brush.</param>
/// <param name="rect">The rectangle bounds.</param> /// <param name="rect">The rectangle bounds.</param>
void FillRectange(Brush brush, Rect rect); void FillRectange(Brush brush, Rect rect, float cornerRadius = 0.0f);
/// <summary> /// <summary>
/// Pushes a clip rectange. /// Pushes a clip rectange.

25
Windows/Perspex.Direct2D1/Media/DrawingContext.cs

@ -125,13 +125,13 @@ namespace Perspex.Direct2D1.Media
/// </summary> /// </summary>
/// <param name="pen">The pen.</param> /// <param name="pen">The pen.</param>
/// <param name="rect">The rectangle bounds.</param> /// <param name="rect">The rectangle bounds.</param>
public void DrawRectange(Pen pen, Rect rect) public void DrawRectange(Pen pen, Rect rect, float cornerRadius)
{ {
using (var brush = pen.Brush.ToDirect2D(this.renderTarget)) using (var brush = pen.Brush.ToDirect2D(this.renderTarget))
using (var d2dStroke = pen.ToDirect2DStrokeStyle(this.renderTarget)) using (var d2dStroke = pen.ToDirect2DStrokeStyle(this.renderTarget))
{ {
this.renderTarget.DrawRectangle( this.renderTarget.DrawRoundedRectangle(
rect.ToDirect2D(), new RoundedRectangle { Rect = rect.ToDirect2D(), RadiusX = cornerRadius, RadiusY = cornerRadius },
brush, brush,
(float)pen.Thickness, (float)pen.Thickness,
d2dStroke); d2dStroke);
@ -162,16 +162,21 @@ namespace Perspex.Direct2D1.Media
/// </summary> /// </summary>
/// <param name="brush">The brush.</param> /// <param name="brush">The brush.</param>
/// <param name="rect">The rectangle bounds.</param> /// <param name="rect">The rectangle bounds.</param>
public void FillRectange(Perspex.Media.Brush brush, Rect rect) public void FillRectange(Perspex.Media.Brush brush, Rect rect, float cornerRadius)
{ {
using (var b = brush.ToDirect2D(this.renderTarget)) using (var b = brush.ToDirect2D(this.renderTarget))
{ {
this.renderTarget.FillRectangle( this.renderTarget.FillRoundedRectangle(
new RectangleF( new RoundedRectangle
(float)rect.X, {
(float)rect.Y, Rect = new RectangleF(
(float)rect.Width, (float)rect.X,
(float)rect.Height), (float)rect.Y,
(float)rect.Width,
(float)rect.Height),
RadiusX = cornerRadius,
RadiusY = cornerRadius
},
b); b);
} }
} }

Loading…
Cancel
Save