Improve Rectangle/RECT marshalling, add utility extensions
このコミットが含まれているのは:
1 親
ea354eade6
コミット
f78d377ebf
4ファイル変更
+116
-16
検索条件に一致するファイルはありません
@@ -10,6 +10,7 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics.CodeAnalysis\DoesNotReturnAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Math.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeHelpers.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)..\ComputeSharp.NetStandard\System\Runtime\InteropServices\NativeMemory.cs" Link="System\Runtime\InteropServices\NativeMemory.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)..\ComputeSharp.NetStandard\System\Runtime\InteropServices\UnmanagedCallersOnlyAttribute.cs" Link="System\Runtime\InteropServices\UnmanagedCallersOnlyAttribute.cs" />
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace ComputeSharp.D2D1.NetStandard.System;
|
||||
|
||||
/// <summary>
|
||||
/// A polyfill type that mirrors some methods from <see cref="global::System.Math"/> on .NET 6.
|
||||
/// </summary>
|
||||
internal static class Math
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns <paramref name="value"/> clamped to the inclusive range of <paramref name="min"/> and <paramref name="max"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to be clamped.</param>
|
||||
/// <param name="min">The lower bound of the result.</param>
|
||||
/// <param name="max">The upper bound of the result.</param>
|
||||
/// <returns>The input value clamped to the inclusive range of <paramref name="min"/> and <paramref name="max"/>.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int Clamp(int value, int min, int max)
|
||||
{
|
||||
if (value < min)
|
||||
{
|
||||
return min;
|
||||
}
|
||||
|
||||
if (value > max)
|
||||
{
|
||||
return max;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="global::System.Math.Min(int, int)"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int Min(int val1, int val2)
|
||||
{
|
||||
return global::System.Math.Min(val1, val2);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="global::System.Math.Max(int, int)"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int Max(int val1, int val2)
|
||||
{
|
||||
return global::System.Math.Max(val1, val2);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,16 @@
|
||||
using System.Drawing;
|
||||
using System.Runtime.CompilerServices;
|
||||
using TerraFX.Interop.Windows;
|
||||
#if NET6_0_OR_GREATER
|
||||
using Math = System.Math;
|
||||
#else
|
||||
using Math = ComputeSharp.D2D1.NetStandard.System.Math;
|
||||
#endif
|
||||
|
||||
namespace ComputeSharp.D2D1.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Helper methods for <see cref="RECT"/>.
|
||||
/// Helper methods for <see cref="RECT"/> and <see cref="Rectangle"/>.
|
||||
/// </summary>
|
||||
internal static class RECTExtensions
|
||||
{
|
||||
@@ -17,6 +22,44 @@ internal static class RECTExtensions
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Rectangle ToRectangle(this in RECT rect)
|
||||
{
|
||||
return Rectangle.FromLTRB(rect.left, rect.top, rect.right, rect.bottom);
|
||||
int left = Math.Clamp(rect.left, int.MinValue / 2, int.MaxValue / 2);
|
||||
int top = Math.Clamp(rect.top, int.MinValue / 2, int.MaxValue / 2);
|
||||
int right = Math.Clamp(rect.right, int.MinValue / 2, int.MaxValue / 2);
|
||||
int bottom = Math.Clamp(rect.bottom, int.MinValue / 2, int.MaxValue / 2);
|
||||
|
||||
int minX = Math.Min(left, right);
|
||||
int minY = Math.Min(top, bottom);
|
||||
int maxX = Math.Max(left, right);
|
||||
int maxY = Math.Max(top, bottom);
|
||||
|
||||
return new(minX, minY, maxX - minX, maxY - minY);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="RECT"/> from a <see cref="Rectangle"/> value.
|
||||
/// </summary>
|
||||
/// <param name="rectangle">The input <see cref="Rectangle"/> value.</param>
|
||||
/// <returns>The resulting <see cref="RECT"/> value.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static RECT ToRECT(this in Rectangle rectangle)
|
||||
{
|
||||
if (rectangle.IsD2D1Infinite())
|
||||
{
|
||||
return new()
|
||||
{
|
||||
left = int.MinValue,
|
||||
top = int.MinValue,
|
||||
right = int.MaxValue,
|
||||
bottom = int.MaxValue
|
||||
};
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
left = rectangle.Left,
|
||||
top = rectangle.Top,
|
||||
right = rectangle.Right,
|
||||
bottom = rectangle.Bottom
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,29 +1,39 @@
|
||||
using System.Drawing;
|
||||
using System.Runtime.CompilerServices;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
namespace ComputeSharp.D2D1.Extensions;
|
||||
namespace ComputeSharp.D2D1;
|
||||
|
||||
/// <summary>
|
||||
/// Helper methods for <see cref="Rectangle"/>.
|
||||
/// </summary>
|
||||
internal static class RectangleExtensions
|
||||
public static class RectangleExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a <see cref="RECT"/> from a <see cref="Rectangle"/> value.
|
||||
/// Modifies a target <see cref="Rectangle"/> instance to be a D2D1 infinite rectangle.
|
||||
/// </summary>
|
||||
/// <param name="rectangle">The input <see cref="Rectangle"/> value.</param>
|
||||
/// <returns>The resulting <see cref="RECT"/> value.</returns>
|
||||
/// <param name="rectangle">The target <see cref="Rectangle"/> instance to modify.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static RECT ToRECT(this in Rectangle rectangle)
|
||||
public static void ToD2D1Infinite(this ref Rectangle rectangle)
|
||||
{
|
||||
RECT result;
|
||||
rectangle = new Rectangle(
|
||||
int.MinValue / 2,
|
||||
int.MinValue / 2,
|
||||
int.MaxValue,
|
||||
int.MaxValue);
|
||||
}
|
||||
|
||||
result.left = rectangle.Left;
|
||||
result.top = rectangle.Top;
|
||||
result.right = rectangle.Right;
|
||||
result.bottom = rectangle.Bottom;
|
||||
|
||||
return result;
|
||||
/// <summary>
|
||||
/// Checks whether a given <see cref="Rectangle"/> instance represents a D2D1 infinite rectangle.
|
||||
/// </summary>
|
||||
/// <param name="rectangle">The input <see cref="Rectangle"/> instance to check.</param>
|
||||
/// <returns>Whether or not <paramref name="rectangle"/> represents a D2D1 infinite rectangle.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool IsD2D1Infinite(this in Rectangle rectangle)
|
||||
{
|
||||
return rectangle == new Rectangle(
|
||||
int.MinValue / 2,
|
||||
int.MinValue / 2,
|
||||
int.MaxValue,
|
||||
int.MaxValue);
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする