DragonECS-Unity/src/Internal/Utils/RectUtility.cs

84 lines
2.6 KiB
C#
Raw Normal View History

2024-03-04 07:38:38 +08:00
using UnityEngine;
namespace DCFApixels.DragonECS.Unity.Internal
{
internal static class RectUtility
{
2024-05-16 19:03:03 +08:00
public static (Rect, Rect) HorizontalSliceLerp(in this Rect rect, float t)
2024-03-04 07:38:38 +08:00
{
Rect l = rect;
Rect r = rect;
l.xMax -= rect.width * (1f - t);
r.xMin += rect.width * t;
return (l, r);
}
2024-05-16 19:03:03 +08:00
public static (Rect, Rect) HorizontalSliceLeft(in this Rect rect, float with)
2024-03-04 07:38:38 +08:00
{
Rect l = rect;
Rect r = rect;
l.xMax = l.xMin + with;
r.xMin += with;
return (l, r);
}
2024-05-16 19:03:03 +08:00
public static (Rect, Rect) HorizontalSliceRight(in this Rect rect, float with)
2024-03-04 07:38:38 +08:00
{
Rect l = rect;
Rect r = rect;
l.xMax -= with;
r.xMin = r.xMax - with;
return (l, r);
}
2024-05-16 19:03:03 +08:00
public static (Rect, Rect) VerticalSliceTop(in this Rect rect, float height)
2024-03-05 00:46:47 +08:00
{
Rect t = rect;
Rect b = rect;
t.yMax = t.yMin + height;
b.yMin += height;
return (t, b);
}
2024-05-16 19:03:03 +08:00
public static (Rect, Rect) VerticalSliceBottom(in this Rect rect, float height)
2024-03-04 07:38:38 +08:00
{
Rect t = rect;
Rect b = rect;
t.yMax -= height;
b.yMin = b.yMax - height;
return (t, b);
}
2024-03-05 00:46:47 +08:00
2024-05-16 19:03:03 +08:00
public static Rect AddPadding(in this Rect rect, float verticalHorizontal)
2024-03-05 00:46:47 +08:00
{
2024-03-05 03:35:38 +08:00
return AddPadding(rect, verticalHorizontal, verticalHorizontal, verticalHorizontal, verticalHorizontal);
2024-03-05 00:46:47 +08:00
}
2024-05-16 19:03:03 +08:00
public static Rect AddPadding(in this Rect rect, float horizontal, float vertical)
2024-03-05 00:46:47 +08:00
{
2024-03-09 22:36:35 +08:00
return AddPadding(rect, horizontal, horizontal, vertical, vertical);
2024-03-05 03:35:38 +08:00
}
2024-05-16 19:03:03 +08:00
public static Rect AddPadding(in this Rect rect, float left, float right, float top, float bottom)
2024-03-05 03:35:38 +08:00
{
2024-05-16 19:03:03 +08:00
Rect result = rect;
result.xMin += left;
result.xMax -= right;
result.yMin += top;
result.yMax -= bottom;
return result;
2024-03-05 00:46:47 +08:00
}
2024-05-16 19:03:03 +08:00
public static Rect Move(in this Rect rect, Vector2 addVector)
2024-03-09 22:36:35 +08:00
{
2024-05-16 19:03:03 +08:00
Rect result = rect;
result.center += addVector;
2024-05-17 09:09:28 +08:00
return result;
2024-03-09 22:36:35 +08:00
}
2024-06-15 16:53:28 +08:00
public static Rect MoveTo(in this Rect rect, Vector2 center)
{
Rect result = rect;
result.center = center;
return result;
}
2024-05-16 19:03:03 +08:00
public static Rect Move(in this Rect rect, float addX, float addY)
2024-03-09 22:36:35 +08:00
{
return Move(rect, new Vector2(addX, addY));
}
2024-03-04 07:38:38 +08:00
}
}