Add vertical mode for ImageSliced3.

This commit is contained in:
2023-11-07 16:17:41 +08:00
parent 5854801a53
commit b56722b2d1

View File

@@ -56,38 +56,28 @@ namespace Cryville.Common.Unity.UI {
set { m_compact = value; }
}
[SerializeField]
[Tooltip("Whether the sprite is vertical.")]
private bool m_isVertical;
/// <summary>
/// Whether the sprite is vertical.
/// </summary>
public bool IsVertical {
get { return m_isVertical; }
set { m_isVertical = value; }
}
public override Texture mainTexture {
get { return Sprite == null ? s_WhiteTexture : Sprite.texture; }
}
void GetCornerLength(int axis, int corner, Vector4 uv, out float length, out float uvLength) {
float border = Sprite.border[(corner << 1) | axis];
float sizeRatio = border / (axis == 0 ? Sprite.rect.height : Sprite.rect.width);
length = sizeRatio * (axis == 0 ? rectTransform.rect.height : rectTransform.rect.width);
uvLength = (uv[(axis ^ 1) | 2] - uv[axis ^ 1]) * (border / (axis == 0 ? Sprite.rect.width : Sprite.rect.height));
}
protected override void OnPopulateMesh(VertexHelper vh) {
float actualWidth = rectTransform.rect.width;
Vector4 uv = DataUtility.GetOuterUV(Sprite);
float cornerLeftSizeRatio = Sprite.border.x / Sprite.rect.height;
float actualLeftCornerWidth = cornerLeftSizeRatio * rectTransform.rect.height;
float actualLeftUVWidth = (uv.w - uv.y) * (Sprite.border.x / Sprite.rect.width);
float cornerRightSizeRatio = Sprite.border.z / Sprite.rect.height;
float actualRightCornerWidth = cornerRightSizeRatio * rectTransform.rect.height;
float actualRightUVWidth = (uv.w - uv.y) * (Sprite.border.z / Sprite.rect.width);
float actualTotalCornerWidth = actualLeftCornerWidth + actualRightCornerWidth;
Vector2 corner1 = new Vector2(0f, 0f);
Vector2 corner2 = new Vector2(1f, 1f);
corner1.x -= rectTransform.pivot.x;
corner1.y -= rectTransform.pivot.y;
corner2.x -= rectTransform.pivot.x;
corner2.y -= rectTransform.pivot.y;
corner1.x *= actualWidth;
corner1.y *= rectTransform.rect.height;
corner2.x *= actualWidth;
corner2.y *= rectTransform.rect.height;
if (Sprite == null) {
throw new UnityException("No sprite");
}
@@ -95,51 +85,87 @@ namespace Cryville.Common.Unity.UI {
throw new UnityException("No sprite border");
}
int axis = IsVertical ? 1 : 0;
float actualLength = axis == 0 ? rectTransform.rect.width : rectTransform.rect.height;
Vector4 uv = DataUtility.GetOuterUV(Sprite);
GetCornerLength(axis, 0, uv, out var actualStartCornerLength, out var actualStartUVWidth);
GetCornerLength(axis, 1, uv, out var actualEndCornerLength, out var actualEndUVLength);
float actualTotalCornerLength = actualStartCornerLength + actualEndCornerLength;
float w3, w4, w5, w6;
if (actualTotalCornerWidth > actualWidth) {
if (actualTotalCornerLength > actualLength) {
switch (Compact) {
case CompactMode.SqueezeBoth:
w3 = w4 = actualLeftCornerWidth / actualTotalCornerWidth * actualWidth;
w5 = w6 = actualRightCornerWidth / actualTotalCornerWidth * actualWidth;
w3 = w4 = actualStartCornerLength / actualTotalCornerLength * actualLength;
w5 = w6 = actualEndCornerLength / actualTotalCornerLength * actualLength;
break;
case CompactMode.SqueezeLeft:
w3 = w4 = actualWidth - actualRightCornerWidth;
w5 = w6 = actualRightCornerWidth;
w3 = w4 = actualLength - actualEndCornerLength;
w5 = w6 = actualEndCornerLength;
break;
case CompactMode.SqueezeRight:
w3 = w4 = actualLeftCornerWidth;
w5 = w6 = actualWidth - actualLeftCornerWidth;
w3 = w4 = actualStartCornerLength;
w5 = w6 = actualLength - actualStartCornerLength;
break;
case CompactMode.DiagonalLeft:
w3 = actualLeftCornerWidth;
w4 = actualWidth - actualRightCornerWidth;
w5 = actualWidth - actualLeftCornerWidth;
w6 = actualRightCornerWidth;
w3 = actualStartCornerLength;
w4 = actualLength - actualEndCornerLength;
w5 = actualLength - actualStartCornerLength;
w6 = actualEndCornerLength;
break;
case CompactMode.DiagonalRight:
w3 = actualWidth - actualRightCornerWidth;
w4 = actualLeftCornerWidth;
w5 = actualRightCornerWidth;
w6 = actualWidth - actualLeftCornerWidth;
w3 = actualLength - actualEndCornerLength;
w4 = actualStartCornerLength;
w5 = actualEndCornerLength;
w6 = actualLength - actualStartCornerLength;
break;
default:
throw new ArgumentOutOfRangeException("Compact");
}
}
else {
w3 = w4 = actualLeftCornerWidth;
w5 = w6 = actualRightCornerWidth;
w3 = w4 = actualStartCornerLength;
w5 = w6 = actualEndCornerLength;
}
Vector2 corner1 = Vector2.zero;
Vector2 corner2 = Vector2.one;
corner1.x -= rectTransform.pivot[axis];
corner1.y -= rectTransform.pivot[axis ^ 1];
corner2.x -= rectTransform.pivot[axis];
corner2.y -= rectTransform.pivot[axis ^ 1];
corner1.x *= actualLength;
corner1.y *= axis == 0 ? rectTransform.rect.height : rectTransform.rect.width;
corner2.x *= actualLength;
corner2.y *= axis == 0 ? rectTransform.rect.height : rectTransform.rect.width;
vh.Clear();
vh.AddVert(new Vector3(corner1.x, corner2.y), color, new Vector2(uv.x, uv.w));
vh.AddVert(new Vector3(corner1.x, corner1.y), color, new Vector2(uv.x, uv.y));
vh.AddVert(new Vector3(corner1.x + w3, corner2.y), color, new Vector2(uv.x + actualLeftUVWidth, uv.w));
vh.AddVert(new Vector3(corner1.x + w4, corner1.y), color, new Vector2(uv.x + actualLeftUVWidth, uv.y));
vh.AddVert(new Vector3(corner2.x - w5, corner2.y), color, new Vector2(uv.z - actualRightUVWidth, uv.w));
vh.AddVert(new Vector3(corner2.x - w6, corner1.y), color, new Vector2(uv.z - actualRightUVWidth, uv.y));
vh.AddVert(new Vector3(corner2.x, corner2.y), color, new Vector2(uv.z, uv.w));
vh.AddVert(new Vector3(corner2.x, corner1.y), color, new Vector2(uv.z, uv.y));
if (axis == 0) {
vh.AddVert(new Vector3(corner1.x, corner2.y), color, new Vector2(uv.x, uv.w));
vh.AddVert(new Vector3(corner1.x, corner1.y), color, new Vector2(uv.x, uv.y));
vh.AddVert(new Vector3(corner1.x + w3, corner2.y), color, new Vector2(uv.x + actualStartUVWidth, uv.w));
vh.AddVert(new Vector3(corner1.x + w4, corner1.y), color, new Vector2(uv.x + actualStartUVWidth, uv.y));
vh.AddVert(new Vector3(corner2.x - w5, corner2.y), color, new Vector2(uv.z - actualEndUVLength, uv.w));
vh.AddVert(new Vector3(corner2.x - w6, corner1.y), color, new Vector2(uv.z - actualEndUVLength, uv.y));
vh.AddVert(new Vector3(corner2.x, corner2.y), color, new Vector2(uv.z, uv.w));
vh.AddVert(new Vector3(corner2.x, corner1.y), color, new Vector2(uv.z, uv.y));
}
else {
vh.AddVert(new Vector3(corner1.y, corner1.x), color, new Vector2(uv.x, uv.w));
vh.AddVert(new Vector3(corner2.y, corner1.x), color, new Vector2(uv.z, uv.w));
vh.AddVert(new Vector3(corner1.y, corner1.x + w4), color, new Vector2(uv.x, uv.w - actualStartUVWidth));
vh.AddVert(new Vector3(corner2.y, corner1.x + w3), color, new Vector2(uv.z, uv.w - actualStartUVWidth));
vh.AddVert(new Vector3(corner1.y, corner2.x - w6), color, new Vector2(uv.x, uv.y + actualEndUVLength));
vh.AddVert(new Vector3(corner2.y, corner2.x - w5), color, new Vector2(uv.z, uv.y + actualEndUVLength));
vh.AddVert(new Vector3(corner1.y, corner2.x), color, new Vector2(uv.x, uv.y));
vh.AddVert(new Vector3(corner2.y, corner2.x), color, new Vector2(uv.z, uv.y));
}
if (((int)Compact & 0x1) == 0) {
vh.AddTriangle(2, 1, 0);