232 lines
5.2 KiB
C#
232 lines
5.2 KiB
C#
using UnityEngine;
|
|
|
|
namespace EnergyBarToolkit
|
|
{
|
|
[RequireComponent(typeof(EnergyBar))]
|
|
[ExecuteInEditMode]
|
|
public class EnergyBarRepeatRenderer : EnergyBarOnGUIBase
|
|
{
|
|
public enum Effect
|
|
{
|
|
None = 0,
|
|
GrowIn = 1,
|
|
FadeOut = 2,
|
|
Cut = 3
|
|
}
|
|
|
|
public enum CutDirection
|
|
{
|
|
LeftToRight = 0,
|
|
TopToBottom = 1,
|
|
RightToLeft = 2,
|
|
BottomToTop = 3
|
|
}
|
|
|
|
private static readonly Vector2 DefaultPositionDelta = new Vector2(1.2f, 0f);
|
|
|
|
public Texture2D icon;
|
|
|
|
public Color iconColor = Color.white;
|
|
|
|
public Texture2D iconSlot;
|
|
|
|
public Color iconSlotColor = Color.white;
|
|
|
|
public Vector2 iconSize;
|
|
|
|
public Vector2 startPosition = new Vector2(10f, 10f);
|
|
|
|
public bool startPositionNormalized;
|
|
|
|
public int repeatCount = 5;
|
|
|
|
public Vector2 positionDelta = DefaultPositionDelta;
|
|
|
|
public Effect effect = Effect.GrowIn;
|
|
|
|
public CutDirection cutDirection = CutDirection.BottomToTop;
|
|
|
|
[SerializeField]
|
|
private bool iconSizeNormalized;
|
|
|
|
[SerializeField]
|
|
private bool positionDeltaNormalized;
|
|
|
|
[SerializeField]
|
|
private bool iconSizeCalculate = true;
|
|
|
|
public override Vector2 SizePixels
|
|
{
|
|
get
|
|
{
|
|
return GetSizePixels(iconSize);
|
|
}
|
|
set
|
|
{
|
|
SetSizePixels(ref iconSize, value);
|
|
}
|
|
}
|
|
|
|
private Vector2 StartPositionPixels
|
|
{
|
|
get
|
|
{
|
|
if (startPositionNormalized)
|
|
{
|
|
return new Vector2(startPosition.x * (float)Screen.width, startPosition.y * (float)Screen.height);
|
|
}
|
|
return startPosition;
|
|
}
|
|
}
|
|
|
|
private Vector2 PositionDeltaPixels
|
|
{
|
|
get
|
|
{
|
|
Vector2 sizePixels = SizePixels;
|
|
return new Vector2(positionDelta.x * sizePixels.x, positionDelta.y * sizePixels.y);
|
|
}
|
|
}
|
|
|
|
public override Vector2 TextureSizePixels
|
|
{
|
|
get
|
|
{
|
|
if (icon == null)
|
|
{
|
|
return Vector2.one;
|
|
}
|
|
return new Vector2(icon.width, icon.height);
|
|
}
|
|
}
|
|
|
|
public override Rect DrawAreaRect
|
|
{
|
|
get
|
|
{
|
|
Vector2 startPositionPixels = StartPositionPixels;
|
|
Vector2 positionDeltaPixels = PositionDeltaPixels;
|
|
return new Rect(startPositionPixels.x, startPositionPixels.y, positionDeltaPixels.x * (float)repeatCount + iconSize.x, positionDeltaPixels.y * (float)repeatCount + iconSize.y);
|
|
}
|
|
}
|
|
|
|
private new void Start()
|
|
{
|
|
base.Start();
|
|
if (version == 169)
|
|
{
|
|
Upgrade_169_170();
|
|
}
|
|
}
|
|
|
|
private void Upgrade_169_170()
|
|
{
|
|
if (iconSizeNormalized)
|
|
{
|
|
resizeMode = ResizeMode.Stretch;
|
|
}
|
|
else if (!iconSizeCalculate)
|
|
{
|
|
resizeMode = ResizeMode.Fixed;
|
|
}
|
|
if (positionDelta != DefaultPositionDelta)
|
|
{
|
|
if (positionDeltaNormalized)
|
|
{
|
|
positionDelta = new Vector2(positionDelta.x * (float)Screen.width, positionDelta.y * (float)Screen.height);
|
|
}
|
|
positionDelta = new Vector2(positionDelta.x / (float)icon.width, positionDelta.y / (float)icon.height);
|
|
}
|
|
version = 170;
|
|
}
|
|
|
|
protected override void Update()
|
|
{
|
|
base.Update();
|
|
FixValues();
|
|
}
|
|
|
|
private void FixValues()
|
|
{
|
|
repeatCount = Mathf.Clamp(repeatCount, 1, 1000);
|
|
}
|
|
|
|
private new void OnGUI()
|
|
{
|
|
base.OnGUI();
|
|
if (!RepaintPhase() || !IsVisible() || !IsValid())
|
|
{
|
|
return;
|
|
}
|
|
Vector2 vector = RealPosition(Round(StartPositionPixels), SizePixels);
|
|
Vector2 sizePixels = SizePixels;
|
|
Vector2 positionDeltaPixels = PositionDeltaPixels;
|
|
float num = ValueF2 * (float)repeatCount;
|
|
int num2 = (int)Mathf.Floor(num);
|
|
float num3 = num - (float)num2;
|
|
for (int i = 0; i < repeatCount; i++)
|
|
{
|
|
if (iconSlot != null)
|
|
{
|
|
DrawTexture(new Rect(vector.x, vector.y, sizePixels.x, sizePixels.y), iconSlot, iconSlotColor);
|
|
}
|
|
if (i < num2)
|
|
{
|
|
DrawTexture(new Rect(vector.x, vector.y, sizePixels.x, sizePixels.y), icon, iconColor);
|
|
}
|
|
else if (i == num2)
|
|
{
|
|
Rect rect = new Rect(vector.x, vector.y, sizePixels.x, sizePixels.y);
|
|
switch (effect)
|
|
{
|
|
case Effect.FadeOut:
|
|
DrawTexture(tint: new Color(iconColor.r, iconColor.g, iconColor.b, iconColor.a * num3), rect: new Rect(vector.x, vector.y, sizePixels.x, sizePixels.y), texture: icon);
|
|
break;
|
|
case Effect.GrowIn:
|
|
{
|
|
Rect rect2 = Resize(rect, num3);
|
|
DrawTexture(rect2, icon, iconColor);
|
|
break;
|
|
}
|
|
case Effect.Cut:
|
|
switch (cutDirection)
|
|
{
|
|
case CutDirection.BottomToTop:
|
|
DrawTextureVertFill(rect, icon, new Rect(0f, 0f, 1f, 1f), iconColor, false, num3);
|
|
break;
|
|
case CutDirection.TopToBottom:
|
|
DrawTextureVertFill(rect, icon, new Rect(0f, 0f, 1f, 1f), iconColor, true, num3);
|
|
break;
|
|
case CutDirection.LeftToRight:
|
|
DrawTextureHorizFill(rect, icon, new Rect(0f, 0f, 1f, 1f), iconColor, false, num3);
|
|
break;
|
|
case CutDirection.RightToLeft:
|
|
DrawTextureHorizFill(rect, icon, new Rect(0f, 0f, 1f, 1f), iconColor, true, num3);
|
|
break;
|
|
default:
|
|
Debug.LogError("Unknown cut direction: " + cutDirection);
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
vector += Round(positionDeltaPixels);
|
|
}
|
|
GUIDrawLabel();
|
|
}
|
|
|
|
private bool IsValid()
|
|
{
|
|
return icon != null;
|
|
}
|
|
|
|
private Rect Resize(Rect rect, float factor)
|
|
{
|
|
float width = rect.width;
|
|
float height = rect.height;
|
|
Vector2 center = rect.center;
|
|
return new Rect(center.x - width / 2f * factor, center.y - height / 2f * factor, width * factor, height * factor);
|
|
}
|
|
}
|
|
}
|