Files
MonoGame.Extended/source/MonoGame.Extended/RectangleF.Extensions.cs
T
Christopher Whitley 8850875fb4 Texture Atlas Refactor (#890)
* Moved `TextureRegion2D` and `NinePatchRegion2D` into Graphics namespace
These are used for more than just texture atlas related things and are primarly for working with `Texture2D` instances and creating sub textures from them.  Moving them to Graphics namespace where `Texture2D` lives in MonoGame

* Dropped the `2D` suffix from `TextureRegion2D` and `NinePatchRegion2D`

* Moved `RectanglExtensions` to project root
This moves the extensions for `Rectangle` to the same relative namespace location as `Rectangle` is in MonoGame.

* Renamed to `Rectangle.Extensions.cs`

* Separate `Rectangle` from `RectangleF` extensions

* Added `GetRelativeRectangle` method

* Updated documentation

* Added `GetRelativeRectangle` method

* Updated documentation

* Top-level namespace

* Remove whitespace

* Move properties up

* Top-level namespace

* Don't recreate structs for `Bounds` and Size`
By having the property return a new struct, this allocates on the stack during situations like a loop during draw.

* Size should be `Size` not `SizeF` value
Textures deal in pixels (ints not floats)

* Removed setter for `Texture` property
Texture should not be set after region is created

* Added UV properties

* Refactor constructors

* Added documentation

* Added license header

* Make it an actual extension method

* Added `GetSubRegion` extension methods for `TextureRegion`

* Update documentation about ObjectDisposedException

* Move const values to top

* Resolve errors from`TextureRegion` refactor

* Update unit tests for TextureRegion refactor

* Started NinePatch rework

* Refactored NinePatch

* Update for texture region constructor change

* Remove ninepatch from atlas

* Moved `TextureAtlasExtensions` into `SpriteBatch.Extensions`
These are extension methods for the sprite batch

* Updated to use new `DrawTextureRegion` method after rename

* Renamed to NinePatchJsonConverter

* Removed `GetRegion<T>`
TextureAtlas no longer contains nine patch regions, so this method isn't needed

* Update after `NinePatchRegion2DJsonConverter` name change

* Resolve errors from NinePatch refactor

* TextureAtlas rework

* Renamed `TextureRegion` to `Texture2DRegion`

* Moved ContentReaders to new Content\ContentReaders directory

* Moved TextureAtlasJsonConvert to Serialization directory

* Adjusted name from `Texture*` to `Texture2D*`

* Update runtimereader and runtimettype strings
2024-06-15 01:37:18 -04:00

79 lines
3.6 KiB
C#

using System;
using Microsoft.Xna.Framework;
namespace MonoGame.Extended;
/// <summary>
/// Provides extension methods for the <see cref="RectangleF"/> structure.
/// </summary>
public static class RectangleFExtensions
{
/// <summary>
/// Gets the corners of the rectangle in a clockwise direction starting at the top left.
/// </summary>
/// <param name="rectangle">The rectangle to get the corners of.</param>
/// <returns>An array of <see cref="Vector2"/> elements representing the corners of the rectangle.</returns>
public static Vector2[] GetCorners(this RectangleF rectangle)
{
var corners = new Vector2[4];
corners[0] = new Vector2(rectangle.Left, rectangle.Top);
corners[1] = new Vector2(rectangle.Right, rectangle.Top);
corners[2] = new Vector2(rectangle.Right, rectangle.Bottom);
corners[3] = new Vector2(rectangle.Left, rectangle.Bottom);
return corners;
}
/// <summary>
/// Converts the specified <see cref="RectangleF"/> to a <see cref="Rectangle"/>.
/// </summary>
/// <param name="rectangle">The rectangle to convert.</param>
/// <returns>The converted <see cref="Rectangle"/>.</returns>
public static Rectangle ToRectangle(this RectangleF rectangle)
{
return new Rectangle((int)rectangle.X, (int)rectangle.Y, (int)rectangle.Width, (int)rectangle.Height);
}
/// <summary>
/// Clips the specified rectangle against the specified clipping rectangle.
/// </summary>
/// <param name="rectangle">The rectangle to clip.</param>
/// <param name="clippingRectangle">The rectangle to clip against.</param>
/// <returns>The clipped rectangle, or <see cref="RectangleF.Empty"/> if the rectangles do not intersect.</returns>
public static RectangleF Clip(this RectangleF rectangle, RectangleF clippingRectangle)
{
var clip = clippingRectangle;
rectangle.X = clip.X > rectangle.X ? clip.X : rectangle.X;
rectangle.Y = clip.Y > rectangle.Y ? clip.Y : rectangle.Y;
rectangle.Width = rectangle.Right > clip.Right ? clip.Right - rectangle.X : rectangle.Width;
rectangle.Height = rectangle.Bottom > clip.Bottom ? clip.Bottom - rectangle.Y : rectangle.Height;
if (rectangle.Width <= 0 || rectangle.Height <= 0)
return RectangleF.Empty;
return rectangle;
}
/// <summary>
/// Gets a rectangle that is relative to the specified source rectangle, with the specified offsets and dimensions.
/// </summary>
/// <param name="source">The source rectangle.</param>
/// <param name="x">The x-coordinate of the relative rectangle, relative to the source rectangle.</param>
/// <param name="y">The y-coordinate of the relative rectangle, relative to the source rectangle.</param>
/// <param name="width">The width, in pixels, of the relative rectangle.</param>
/// <param name="height">The height, in pixels, of the relative rectangle.</param>
/// <returns>The relative rectangle, clipped to the source rectangle.</returns>
public static RectangleF GetRelativeRectangle(this RectangleF source, float x, float y, float width, float height)
{
float absoluteX = source.X + x;
float absoluteY = source.Y + y;
RectangleF relative;
relative.X = MathHelper.Clamp(absoluteX, source.Left, source.Right);
relative.Y = MathHelper.Clamp(absoluteY, source.Top, source.Bottom);
relative.Width = Math.Max(Math.Min(absoluteX + width, source.Right) - relative.X, 0);
relative.Height = Math.Max(Math.Min(absoluteY + height, source.Bottom) - relative.Y, 0);
return relative;
}
}