Last active
June 11, 2024 02:46
-
-
Save mousedoc/d3dc03bcbbf8d0f33fa0a58cffe8bf32 to your computer and use it in GitHub Desktop.
Implement NGUI FillDirection.Diagonal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class UIBasicSprite : UIWidget | |
{ | |
... | |
public enum FillDirection | |
{ | |
Horizontal, | |
Vertical, | |
Radial90, | |
Radial180, | |
Radial360, | |
Diagonal, | |
} | |
... | |
/// <summary> | |
/// Filled sprite fill function. | |
/// </summary> | |
void FilledFill (BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols) | |
{ | |
... | |
if (mFillAmount < 1f) | |
{ | |
... | |
if (mFillDirection == FillDirection.Diagonal) | |
{ | |
if (DiagonalCut(mTempPos, mTempUVs, mFillAmount, mInvert, 0)) | |
{ | |
for (int i = 0; i < 4; ++i) | |
{ | |
verts.Add(mTempPos[i]); | |
uvs.Add(mTempUVs[i]); | |
cols.Add(c); | |
} | |
} | |
if (mFillAmount > 0.5f) | |
{ | |
mTempPos[0] = new Vector2(v.x, v.y); | |
mTempPos[1] = new Vector2(v.x, v.w); | |
mTempPos[2] = new Vector2(v.z, v.w); | |
mTempPos[3] = new Vector2(v.z, v.y); | |
mTempUVs[0] = new Vector2(u.x, u.y); | |
mTempUVs[1] = new Vector2(u.x, u.w); | |
mTempUVs[2] = new Vector2(u.z, u.w); | |
mTempUVs[3] = new Vector2(u.z, u.y); | |
if (DiagonalCut(mTempPos, mTempUVs, mFillAmount, mInvert, 1)) | |
{ | |
for (int i = 0; i < 4; ++i) | |
{ | |
verts.Add(mTempPos[i]); | |
uvs.Add(mTempUVs[i]); | |
cols.Add(c); | |
} | |
} | |
} | |
return; | |
} | |
} | |
... | |
} | |
/// <summary> | |
/// Diagonal filled instead | |
/// </summary> | |
bool DiagonalCut(Vector2[] xy, Vector2[] uv, float fill, bool invert, int corner) | |
{ | |
// Nothing to fill | |
if (fill < 0.001f) | |
{ | |
return false; | |
} | |
// Nothing to adjust | |
if (!invert && fill > 0.999f) | |
{ | |
return true; | |
} | |
DiagonalCut(xy, invert, fill, corner); | |
DiagonalCut(uv, invert, fill, corner); | |
return true; | |
} | |
static void DiagonalCut(Vector2[] xy, bool invert, float fill, int corner) | |
{ | |
if (corner == 0) | |
{ | |
var plusPoint5Fill = fill / 0.5f; | |
if (invert == false) | |
{ | |
xy[0].y = Mathf.Lerp(xy[1].y, xy[3].y, plusPoint5Fill); | |
xy[2].x = Mathf.Lerp(xy[1].x, xy[3].x, plusPoint5Fill); | |
xy[3] = (xy[0] + xy[2]) / 2; | |
} | |
else | |
{ | |
xy[0].x = Mathf.Lerp(xy[3].x, xy[1].x, plusPoint5Fill); | |
xy[2].y = Mathf.Lerp(xy[3].y, xy[1].y, plusPoint5Fill); | |
xy[1] = (xy[0] + xy[2]) / 2; | |
} | |
} | |
else | |
{ | |
var minusPoint5Fill = (fill - 0.5f) / 0.5f; | |
if (invert == false) | |
{ | |
xy[1] = xy[2]; | |
xy[2].y = Mathf.Lerp(xy[2].y, xy[0].y, minusPoint5Fill); | |
xy[3].x = Mathf.Lerp(xy[0].x, xy[2].x, minusPoint5Fill); | |
} | |
else | |
{ | |
xy[3] = xy[2]; | |
xy[1].y = Mathf.Lerp(xy[0].y, xy[2].y, minusPoint5Fill); | |
xy[2].x = Mathf.Lerp(xy[3].x, xy[0].x, minusPoint5Fill); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment