13 changed files with 480 additions and 50 deletions
@ -0,0 +1,29 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="AlignmentX.cs" company="Steven Kirk">
|
||||
|
// Copyright 2015 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Media |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Describes how content is positioned horizontally in a container.
|
||||
|
/// </summary>
|
||||
|
public enum AlignmentX |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The contents align themselves with the left of the container
|
||||
|
/// </summary>
|
||||
|
Left, |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The contents align themselves with the center of the container
|
||||
|
/// </summary>
|
||||
|
Center, |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The contents align themselves with the right of the container
|
||||
|
/// </summary>
|
||||
|
Right, |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="AlignmentY.cs" company="Steven Kirk">
|
||||
|
// Copyright 2015 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Media |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Describes how content is positioned vertically in a container.
|
||||
|
/// </summary>
|
||||
|
public enum AlignmentY |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The contents align themselves with the top of the container
|
||||
|
/// </summary>
|
||||
|
Top, |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The contents align themselves with the center of the container
|
||||
|
/// </summary>
|
||||
|
Center, |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The contents align themselves with the bottom of the container
|
||||
|
/// </summary>
|
||||
|
Bottom, |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,47 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="MediaExtensions.cs" company="Steven Kirk">
|
||||
|
// Copyright 2015 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Media |
||||
|
{ |
||||
|
using System; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Provides extension methods for Perspex media.
|
||||
|
/// </summary>
|
||||
|
public static class MediaExtensions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Calculates scaling based on a <see cref="Stretch"/> value.
|
||||
|
/// </summary>
|
||||
|
/// <param name="stretch">The stretch mode.</param>
|
||||
|
/// <param name="destinationSize">The size of the destination viewport.</param>
|
||||
|
/// <param name="sourceSize">The size of the source.</param>
|
||||
|
/// <returns>A vector with the X and Y scaling factors.</returns>
|
||||
|
public static Vector CalculateScaling(this Stretch stretch, Size destinationSize, Size sourceSize) |
||||
|
{ |
||||
|
double scaleX = 1; |
||||
|
double scaleY = 1; |
||||
|
|
||||
|
if (stretch != Stretch.None) |
||||
|
{ |
||||
|
scaleX = destinationSize.Width / sourceSize.Width; |
||||
|
scaleY = destinationSize.Height / sourceSize.Height; |
||||
|
|
||||
|
switch (stretch) |
||||
|
{ |
||||
|
case Stretch.Uniform: |
||||
|
scaleX = scaleY = Math.Min(scaleX, scaleY); |
||||
|
break; |
||||
|
case Stretch.UniformToFill: |
||||
|
scaleX = scaleY = Math.Max(scaleX, scaleY); |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return new Vector(scaleX, scaleY); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,90 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="TileBrush.cs" company="Steven Kirk">
|
||||
|
// Copyright 2015 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Media |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Base class for brushes which display repeating images.
|
||||
|
/// </summary>
|
||||
|
public abstract class TileBrush : Brush |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Defines the <see cref="AlignmentX"/> property.
|
||||
|
/// </summary>
|
||||
|
public static readonly PerspexProperty<AlignmentX> AlignmentXProperty = |
||||
|
PerspexProperty.Register<TileBrush, AlignmentX>("ALignmentX", AlignmentX.Center); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Defines the <see cref="AlignmentY"/> property.
|
||||
|
/// </summary>
|
||||
|
public static readonly PerspexProperty<AlignmentY> AlignmentYProperty = |
||||
|
PerspexProperty.Register<TileBrush, AlignmentY>("ALignmentY", AlignmentY.Center); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Defines the <see cref="DestinationRect"/> property.
|
||||
|
/// </summary>
|
||||
|
public static readonly PerspexProperty<RelativeRect> DestinationRectProperty = |
||||
|
PerspexProperty.Register<TileBrush, RelativeRect>("DestinationRect", RelativeRect.Fill); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Defines the <see cref="SourceRect"/> property.
|
||||
|
/// </summary>
|
||||
|
public static readonly PerspexProperty<RelativeRect> SourceRectProperty = |
||||
|
PerspexProperty.Register<TileBrush, RelativeRect>("SourceRect", RelativeRect.Fill); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Defines the <see cref="Stretch"/> property.
|
||||
|
/// </summary>
|
||||
|
public static readonly PerspexProperty<Stretch> StretchProperty = |
||||
|
PerspexProperty.Register<TileBrush, Stretch>(nameof(Stretch), Stretch.Uniform); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets the horizontal alignment of a tile in the destination.
|
||||
|
/// </summary>
|
||||
|
public AlignmentX AlignmentX |
||||
|
{ |
||||
|
get { return this.GetValue(AlignmentXProperty); } |
||||
|
set { this.SetValue(AlignmentXProperty, value); } |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets the horizontal alignment of a tile in the destination.
|
||||
|
/// </summary>
|
||||
|
public AlignmentY AlignmentY |
||||
|
{ |
||||
|
get { return this.GetValue(AlignmentYProperty); } |
||||
|
set { this.SetValue(AlignmentYProperty, value); } |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets the rectangle on the destination in which to paint a tile.
|
||||
|
/// </summary>
|
||||
|
public RelativeRect DestinationRect |
||||
|
{ |
||||
|
get { return this.GetValue(DestinationRectProperty); } |
||||
|
set { this.SetValue(DestinationRectProperty, value); } |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets the rectangle of the source image that will be displayed.
|
||||
|
/// </summary>
|
||||
|
public RelativeRect SourceRect |
||||
|
{ |
||||
|
get { return this.GetValue(SourceRectProperty); } |
||||
|
set { this.SetValue(SourceRectProperty, value); } |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets a value controlling how the source rectangle will be stretched to fill
|
||||
|
/// the destination rect.
|
||||
|
/// </summary>
|
||||
|
public Stretch Stretch |
||||
|
{ |
||||
|
get { return (Stretch)this.GetValue(StretchProperty); } |
||||
|
set { this.SetValue(StretchProperty, value); } |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,105 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="RelativeRect.cs" company="Steven Kirk">
|
||||
|
// Copyright 2015 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Defines a rectangle that may be defined relative to another rectangle.
|
||||
|
/// </summary>
|
||||
|
public struct RelativeRect |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// A rectangle that represents 100% of an area.
|
||||
|
/// </summary>
|
||||
|
public static readonly RelativeRect Fill = new RelativeRect(0, 0, 1, 1, OriginUnit.Percent); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="RelativeRect"/> structure.
|
||||
|
/// </summary>
|
||||
|
/// <param name="x">The X position.</param>
|
||||
|
/// <param name="y">The Y position.</param>
|
||||
|
/// <param name="width">The width.</param>
|
||||
|
/// <param name="height">The height.</param>
|
||||
|
/// <param name="unit">The unit of the rect.</param>
|
||||
|
public RelativeRect(double x, double y, double width, double height, OriginUnit unit) |
||||
|
{ |
||||
|
this.Rect = new Rect(x, y, width, height); |
||||
|
this.Unit = unit; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="RelativeRect"/> structure.
|
||||
|
/// </summary>
|
||||
|
/// <param name="rect">The rectangle.</param>
|
||||
|
/// <param name="unit">The unit of the rect.</param>
|
||||
|
public RelativeRect(Rect rect, OriginUnit unit) |
||||
|
{ |
||||
|
this.Rect = rect; |
||||
|
this.Unit = unit; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="RelativeRect"/> structure.
|
||||
|
/// </summary>
|
||||
|
/// <param name="size">The size of the rectangle.</param>
|
||||
|
/// <param name="unit">The unit of the rect.</param>
|
||||
|
public RelativeRect(Size size, OriginUnit unit) |
||||
|
{ |
||||
|
this.Rect = new Rect(size); |
||||
|
this.Unit = unit; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="RelativeRect"/> structure.
|
||||
|
/// </summary>
|
||||
|
/// <param name="position">The position of the rectangle.</param>
|
||||
|
/// <param name="size">The size of the rectangle.</param>
|
||||
|
/// <param name="unit">The unit of the rect.</param>
|
||||
|
public RelativeRect(Point position, Size size, OriginUnit unit) |
||||
|
{ |
||||
|
this.Rect = new Rect(position, size); |
||||
|
this.Unit = unit; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="RelativeRect"/> structure.
|
||||
|
/// </summary>
|
||||
|
/// <param name="topLeft">The top left position of the rectangle.</param>
|
||||
|
/// <param name="bottomRight">The bottom right position of the rectangle.</param>
|
||||
|
/// <param name="unit">The unit of the rect.</param>
|
||||
|
public RelativeRect(Point topLeft, Point bottomRight, OriginUnit unit) |
||||
|
{ |
||||
|
this.Rect = new Rect(topLeft, bottomRight); |
||||
|
this.Unit = unit; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the unit of the rectangle.
|
||||
|
/// </summary>
|
||||
|
public OriginUnit Unit { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the rectangle.
|
||||
|
/// </summary>
|
||||
|
public Rect Rect { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Converts a <see cref="RelativeRect"/> into pixels.
|
||||
|
/// </summary>
|
||||
|
/// <param name="size">The size of the visual.</param>
|
||||
|
/// <returns>The origin point in pixels.</returns>
|
||||
|
public Rect ToPixels(Size size) |
||||
|
{ |
||||
|
return this.Unit == OriginUnit.Pixels ? |
||||
|
this.Rect : |
||||
|
new Rect( |
||||
|
this.Rect.X * size.Width, |
||||
|
this.Rect.Y * size.Height, |
||||
|
this.Rect.Width * size.Width, |
||||
|
this.Rect.Height * size.Height); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue