Browse Source

Started implementing Button.

pull/4/head
grokys 12 years ago
parent
commit
8924977a49
  1. 36
      Perspex.Windows/DrawingContext.cs
  2. 8
      Perspex.Windows/Renderer.cs
  3. 25
      Perspex/Controls/Border.cs
  4. 23
      Perspex/Controls/Button.cs
  5. 2
      Perspex/Controls/TextBlock.cs
  6. 8
      Perspex/Media/IDrawingContext.cs
  7. 131
      Perspex/Media/Matrix.cs
  8. 2
      Perspex/Perspex.csproj
  9. 8
      TestApplication/Program.cs

36
Perspex.Windows/DrawingContext.cs

@ -7,10 +7,12 @@
namespace Perspex.Windows
{
using System;
using System.Reactive.Disposables;
using Perspex.Media;
using Perspex.Windows.Media;
using SharpDX;
using SharpDX.Direct2D1;
using Matrix = Perspex.Media.Matrix;
/// <summary>
/// Draws using Direct2D1.
@ -102,6 +104,24 @@ namespace Perspex.Windows
}
}
/// <summary>
/// Pushes a matrix transformation.
/// </summary>
/// <param name="matrix">The matrix</param>
/// <returns>A disposable used to undo the transformation.</returns>
public IDisposable PushTransform(Matrix matrix)
{
Matrix3x2 m3x2 = this.Convert(matrix);
Matrix3x2 transform = this.renderTarget.Transform * m3x2;
this.renderTarget.Transform = transform;
return Disposable.Create(() =>
{
m3x2.Invert();
this.renderTarget.Transform = transform * m3x2;
});
}
/// <summary>
/// Converts a brush to Direct2D.
/// </summary>
@ -139,6 +159,22 @@ namespace Perspex.Windows
(float)(color.A / 255.0));
}
/// <summary>
/// Converts a <see cref="Matrix"/> to a Direct2D <see cref="Matrix3x2"/>
/// </summary>
/// <param name="matrix">The <see cref="Matrix"/>.</param>
/// <returns>The <see cref="Matrix3x2"/>.</returns>
private Matrix3x2 Convert(Matrix matrix)
{
return new Matrix3x2(
(float)matrix.M11,
(float)matrix.M12,
(float)matrix.M21,
(float)matrix.M22,
(float)matrix.OffsetX,
(float)matrix.OffsetY);
}
/// <summary>
/// Converts a <see cref="Rect"/> to a <see cref="RectangleF"/>
/// </summary>

8
Perspex.Windows/Renderer.cs

@ -10,6 +10,7 @@ namespace Perspex.Windows
using SharpDX;
using SharpDX.Direct2D1;
using DwFactory = SharpDX.DirectWrite.Factory;
using Matrix = Perspex.Media.Matrix;
/// <summary>
/// Renders a <see cref="Canvas"/>.
@ -99,7 +100,12 @@ namespace Perspex.Windows
foreach (Visual child in visual.VisualChildren)
{
this.Render(child, context);
Matrix translate = Matrix.Translation(child.Bounds.X, child.Bounds.Y);
using (context.PushTransform(translate))
{
this.Render(child, context);
}
}
}
}

25
Perspex/Controls/Border.cs

@ -8,20 +8,45 @@
public static readonly PerspexProperty<Brush> BackgroundProperty =
PerspexProperty.Register<Border, Brush>("Background");
public static readonly PerspexProperty<Brush> BorderBrushProperty =
PerspexProperty.Register<Border, Brush>("BorderBrush");
public static readonly PerspexProperty<double> BorderThicknessProperty =
PerspexProperty.Register<Border, double>("BorderThickness");
public Brush Background
{
get { return this.GetValue(BackgroundProperty); }
set { this.SetValue(BackgroundProperty, value); }
}
public Brush BorderBrush
{
get { return this.GetValue(BorderBrushProperty); }
set { this.SetValue(BorderBrushProperty, value); }
}
public double BorderThickness
{
get { return this.GetValue(BorderThicknessProperty); }
set { this.SetValue(BorderThicknessProperty, value); }
}
public override void Render(IDrawingContext context)
{
Brush background = this.Background;
Brush borderBrush = this.BorderBrush;
double borderThickness = this.BorderThickness;
if (background != null)
{
context.FillRectange(background, new Rect(this.Bounds.Size));
}
if (borderBrush != null && borderThickness > 0)
{
context.DrawRectange(new Pen(borderBrush, borderThickness), new Rect(this.Bounds.Size));
}
}
}
}

23
Perspex/Controls/Button.cs

@ -0,0 +1,23 @@
// -----------------------------------------------------------------------
// <copyright file="Button.cs" company="Steven Kirk">
// Copyright 2013 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Controls
{
public class Button : ContentControl
{
protected override Visual DefaultTemplate()
{
Border border = new Border();
border.Background = new Perspex.Media.SolidColorBrush(0xff808080);
border.BorderBrush = new Perspex.Media.SolidColorBrush(0xff000000);
border.BorderThickness = 2;
ContentPresenter contentPresenter = new ContentPresenter();
contentPresenter.Bind(ContentPresenter.ContentProperty, this.GetObservable(ContentProperty));
border.Content = contentPresenter;
return border;
}
}
}

2
Perspex/Controls/TextBlock.cs

@ -67,7 +67,7 @@
context.FillRectange(background, this.Bounds);
}
context.DrawText(this.Foreground, this.Bounds, this.FormattedText);
context.DrawText(this.Foreground, new Rect(this.Bounds.Size), this.FormattedText);
}
protected override Size MeasureContent(Size availableSize)

8
Perspex/Media/IDrawingContext.cs

@ -4,6 +4,7 @@
// </copyright>
// -----------------------------------------------------------------------
using System;
namespace Perspex.Media
{
/// <summary>
@ -32,5 +33,12 @@ namespace Perspex.Media
/// <param name="brush">The brush.</param>
/// <param name="rect">The rectangle bounds.</param>
void FillRectange(Brush brush, Rect rect);
/// <summary>
/// Pushes a matrix transformation.
/// </summary>
/// <param name="matrix">The matrix</param>
/// <returns>A disposable used to undo the transformation.</returns>
IDisposable PushTransform(Matrix matrix);
}
}

131
Perspex/Media/Matrix.cs

@ -0,0 +1,131 @@
// -----------------------------------------------------------------------
// <copyright file="Matrix.cs" company="Steven Kirk">
// Copyright 2013 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Media
{
using System;
public struct Matrix
{
private double m11;
private double m12;
private double m21;
private double m22;
private double offsetX;
private double offsetY;
public Matrix(
double m11,
double m12,
double m21,
double m22,
double offsetX,
double offsetY)
{
this.m11 = m11;
this.m12 = m12;
this.m21 = m21;
this.m22 = m22;
this.offsetX = offsetX;
this.offsetY = offsetY;
}
public static Matrix Identity
{
get { return new Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0); }
}
public double Determinant
{
get { return (this.m11 * this.m22) - (this.m12 * this.m21); }
}
public bool HasInverse
{
get { return this.Determinant != 0; }
}
public bool IsIdentity
{
get { return this.Equals(Matrix.Identity); }
}
public double M11
{
get { return this.m11; }
}
public double M12
{
get { return this.m12; }
}
public double M21
{
get { return this.m21; }
}
public double M22
{
get { return this.m22; }
}
public double OffsetX
{
get { return this.offsetX; }
}
public double OffsetY
{
get { return this.offsetY; }
}
public static bool Equals(Matrix matrix1, Matrix matrix2)
{
return matrix1.Equals(matrix2);
}
public static Matrix Translation(double x, double y)
{
return new Matrix(1.0, 0.0, 0.0, 1.0, x, y);
}
public static bool operator ==(Matrix matrix1, Matrix matrix2)
{
return matrix1.Equals(matrix2);
}
public static bool operator !=(Matrix matrix1, Matrix matrix2)
{
return !matrix1.Equals(matrix2);
}
public bool Equals(Matrix value)
{
return this.m11 == value.M11 &&
this.m12 == value.M12 &&
this.m21 == value.M21 &&
this.m22 == value.M22 &&
this.offsetX == value.OffsetX &&
this.offsetY == value.OffsetY;
}
public override bool Equals(object o)
{
if (!(o is Matrix))
{
return false;
}
return this.Equals((Matrix)o);
}
public override int GetHashCode()
{
throw new NotImplementedException();
}
}
}

2
Perspex/Perspex.csproj

@ -69,6 +69,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="Controls\Border.cs" />
<Compile Include="Controls\Button.cs" />
<Compile Include="Controls\ContentPresenter.cs" />
<Compile Include="Controls\Decorator.cs" />
<Compile Include="Controls\ContentControl.cs" />
@ -83,6 +84,7 @@
<Compile Include="Media\Brush.cs" />
<Compile Include="Media\Color.cs" />
<Compile Include="Media\ITextService.cs" />
<Compile Include="Media\Matrix.cs" />
<Compile Include="Media\Pen.cs" />
<Compile Include="Media\SolidColorBrush.cs" />
<Compile Include="PerspexObject.cs" />

8
TestApplication/Program.cs

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using Perspex;
@ -20,14 +21,13 @@ namespace TestApplication
Window window = new Window();
window.Content = new TextBlock
window.Content = new Button
{
Text = "Hello World",
Background = new SolidColorBrush(0xffffffff),
Content = "Hello World",
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};
window.Show();
Dispatcher.Run();
}

Loading…
Cancel
Save