A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

191 lines
6.1 KiB

// -----------------------------------------------------------------------
// <copyright file="Size.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex
{
using System;
using System.Globalization;
/// <summary>
/// Defines a size.
/// </summary>
public struct Size
{
/// <summary>
/// A size representing infinity.
/// </summary>
public static readonly Size Infinity = new Size(double.PositiveInfinity, double.PositiveInfinity);
/// <summary>
/// The width.
/// </summary>
private double width;
/// <summary>
/// The height.
/// </summary>
private double height;
/// <summary>
/// Initializes a new instance of the <see cref="Size"/> structure.
/// </summary>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
public Size(double width, double height)
{
this.width = width;
this.height = height;
}
/// <summary>
/// Gets the width.
/// </summary>
public double Width
{
get { return this.width; }
}
/// <summary>
/// Gets the height.
/// </summary>
public double Height
{
get { return this.height; }
}
/// <summary>
/// Checks for equality between two <see cref="Size"/>s.
/// </summary>
/// <param name="left">The first size.</param>
/// <param name="right">The second size.</param>
/// <returns>True if the sizes are equal; otherwise false.</returns>
public static bool operator ==(Size left, Size right)
{
return left.width == right.width && left.height == right.height;
}
/// <summary>
/// Checks for unequality between two <see cref="Size"/>s.
/// </summary>
/// <param name="left">The first size.</param>
/// <param name="right">The second size.</param>
/// <returns>True if the sizes are unequal; otherwise false.</returns>
public static bool operator !=(Size left, Size right)
{
return !(left == right);
}
/// <summary>
/// Scales a size.
/// </summary>
/// <param name="rect">The size</param>
/// <param name="scale">The scaling factor.</param>
/// <returns>The scaled size.</returns>
public static Size operator *(Size size, Vector scale)
{
return new Size(size.width * scale.X, size.height * scale.Y);
}
/// <summary>
/// Scales a size.
/// </summary>
/// <param name="rect">The size</param>
/// <param name="scale">The scaling factor.</param>
/// <returns>The scaled size.</returns>
public static Size operator /(Size size, Vector scale)
{
return new Size(size.width / scale.X, size.height / scale.Y);
}
/// <summary>
/// Constrains the size.
/// </summary>
/// <param name="constraint">The size to constrain to.</param>
/// <returns>The constrained size.</returns>
public Size Constrain(Size constraint)
{
return new Size(
Math.Min(this.width, constraint.width),
Math.Min(this.height, constraint.height));
}
/// <summary>
/// Deflates the size by a <see cref="Thickness"/>.
/// </summary>
/// <param name="thickness">The thickness.</param>
/// <returns>The deflated size.</returns>
/// <remarks>The deflated size cannot be less than 0.</remarks>
public Size Deflate(Thickness thickness)
{
return new Size(
Math.Max(0, this.width - thickness.Left - thickness.Right),
Math.Max(0, this.height - thickness.Top - thickness.Bottom));
}
public override bool Equals(object obj)
{
if (obj is Size)
{
var other = (Size)obj;
return this.Width == other.Width && this.Height == other.Height;
}
return false;
}
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = (hash * 23) + this.Width.GetHashCode();
hash = (hash * 23) + this.Height.GetHashCode();
return hash;
}
}
/// <summary>
/// Inflates the size by a <see cref="Thickness"/>.
/// </summary>
/// <param name="thickness">The thickness.</param>
/// <returns>The inflated size.</returns>
public Size Inflate(Thickness thickness)
{
return new Size(
this.width + thickness.Left + thickness.Right,
this.height + thickness.Top + thickness.Bottom);
}
/// <summary>
/// Returns a new <see cref="Size"/> with the same height and the specified width.
/// </summary>
/// <param name="width">The width.</param>
/// <returns>The new <see cref="Size"/>.</returns>
public Size WithWidth(double width)
{
return new Size(width, this.height);
}
/// <summary>
/// Returns a new <see cref="Size"/> with the same width and the specified height.
/// </summary>
/// <param name="height">The height.</param>
/// <returns>The new <see cref="Size"/>.</returns>
public Size WithHeight(double height)
{
return new Size(this.width, height);
}
/// <summary>
/// Returns the string representation of the size.
/// </summary>
/// <returns>The string representation of the size</returns>
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "{0}, {1}", this.width, this.height);
}
}
}