654 lines
13 KiB
C#
654 lines
13 KiB
C#
using System;
|
|
using EnergyBarToolkit;
|
|
using UnityEngine;
|
|
|
|
public abstract class EnergyBarBase : MonoBehaviour
|
|
{
|
|
public enum Pivot
|
|
{
|
|
TopLeft = 0,
|
|
Top = 1,
|
|
TopRight = 2,
|
|
Right = 3,
|
|
BottomRight = 4,
|
|
Bottom = 5,
|
|
BottomLeft = 6,
|
|
Left = 7,
|
|
Center = 8
|
|
}
|
|
|
|
[Serializable]
|
|
public class Tex : AbstractTex
|
|
{
|
|
public Texture2D texture;
|
|
|
|
public virtual int width
|
|
{
|
|
get
|
|
{
|
|
return texture.width;
|
|
}
|
|
}
|
|
|
|
public virtual int height
|
|
{
|
|
get
|
|
{
|
|
return texture.height;
|
|
}
|
|
}
|
|
|
|
public virtual bool Valid
|
|
{
|
|
get
|
|
{
|
|
return texture != null;
|
|
}
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
int current = 37;
|
|
return HashAddTexture(current, texture);
|
|
}
|
|
}
|
|
|
|
public class AbstractTex
|
|
{
|
|
public Color color = Color.white;
|
|
}
|
|
|
|
public enum GrowDirection
|
|
{
|
|
LeftToRight = 0,
|
|
RightToLeft = 1,
|
|
BottomToTop = 2,
|
|
TopToBottom = 3,
|
|
RadialCW = 4,
|
|
RadialCCW = 5,
|
|
ExpandHorizontal = 6,
|
|
ExpandVertical = 7,
|
|
ColorChange = 8
|
|
}
|
|
|
|
public enum ColorType
|
|
{
|
|
Solid = 0,
|
|
Gradient = 1
|
|
}
|
|
|
|
public enum SmoothDirection
|
|
{
|
|
Both = 0,
|
|
OnlyWhenDecreasing = 1,
|
|
OnlyWhenIncreasing = 2
|
|
}
|
|
|
|
public enum BurnDirection
|
|
{
|
|
Both = 0,
|
|
OnlyWhenDecreasing = 1,
|
|
OnlyWhenIncreasing = 2
|
|
}
|
|
|
|
public abstract class TransformFunction
|
|
{
|
|
public AnimationCurve animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
|
|
}
|
|
|
|
[Serializable]
|
|
public class TranslateFunction : TransformFunction
|
|
{
|
|
public Vector2 startPosition;
|
|
|
|
public Vector2 endPosition;
|
|
|
|
public Vector2 Value(float progress)
|
|
{
|
|
progress = Mathf.Clamp01(progress);
|
|
progress = animationCurve.Evaluate(progress);
|
|
return new Vector2(startPosition.x + (endPosition.x - startPosition.x) * progress, startPosition.y + (endPosition.y - startPosition.y) * progress);
|
|
}
|
|
|
|
public override bool Equals(object obj)
|
|
{
|
|
if (obj == null)
|
|
{
|
|
return false;
|
|
}
|
|
if (!(obj is TranslateFunction))
|
|
{
|
|
return false;
|
|
}
|
|
return GetHashCode() == obj.GetHashCode();
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
int num = 17;
|
|
num = num * 23 + startPosition.GetHashCode();
|
|
return num * 23 + endPosition.GetHashCode();
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class ScaleFunction : TransformFunction
|
|
{
|
|
public Vector2 startScale = Vector3.one;
|
|
|
|
public Vector2 endScale = Vector3.one;
|
|
|
|
public Vector3 Value(float progress)
|
|
{
|
|
progress = Mathf.Clamp01(progress);
|
|
progress = animationCurve.Evaluate(progress);
|
|
Vector2 vector = new Vector2(startScale.x + (endScale.x - startScale.x) * progress, startScale.y + (endScale.y - startScale.y) * progress);
|
|
return new Vector3(vector.x, vector.y, 1f);
|
|
}
|
|
|
|
public override bool Equals(object obj)
|
|
{
|
|
if (obj == null)
|
|
{
|
|
return false;
|
|
}
|
|
if (!(obj is ScaleFunction))
|
|
{
|
|
return false;
|
|
}
|
|
return GetHashCode() == obj.GetHashCode();
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
int num = 17;
|
|
num = num * 23 + startScale.GetHashCode();
|
|
return num * 23 + endScale.GetHashCode();
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class RotateFunction : TransformFunction
|
|
{
|
|
public float startAngle;
|
|
|
|
public float endAngle;
|
|
|
|
public Quaternion Value(float progress)
|
|
{
|
|
progress = Mathf.Clamp01(progress);
|
|
progress = animationCurve.Evaluate(progress);
|
|
float z = startAngle + (endAngle - startAngle) * progress;
|
|
return Quaternion.Euler(0f, 0f, z);
|
|
}
|
|
|
|
public override bool Equals(object obj)
|
|
{
|
|
if (obj == null)
|
|
{
|
|
return false;
|
|
}
|
|
if (!(obj is RotateFunction))
|
|
{
|
|
return false;
|
|
}
|
|
return GetHashCode() == obj.GetHashCode();
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
int num = 17;
|
|
num = num * 23 + startAngle.GetHashCode();
|
|
return num * 23 + endAngle.GetHashCode();
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class Notify
|
|
{
|
|
public MonoBehaviour receiver;
|
|
|
|
public string methodName;
|
|
|
|
public event Action<EnergyBarBase> eventReceiver;
|
|
|
|
public void Execute(EnergyBarBase sender)
|
|
{
|
|
if (receiver != null && !string.IsNullOrEmpty(methodName))
|
|
{
|
|
receiver.SendMessage(methodName, sender);
|
|
}
|
|
if (this.eventReceiver != null)
|
|
{
|
|
this.eventReceiver(sender);
|
|
}
|
|
}
|
|
}
|
|
|
|
[SerializeField]
|
|
protected int version = 169;
|
|
|
|
public bool labelEnabled;
|
|
|
|
public Vector2 labelPosition;
|
|
|
|
public bool labelPositionNormalized = true;
|
|
|
|
public string labelFormat = "{cur}/{max}";
|
|
|
|
public Color labelColor = Color.white;
|
|
|
|
public bool effectSmoothChange;
|
|
|
|
public float effectSmoothChangeSpeed = 0.5f;
|
|
|
|
public SmoothDirection effectSmoothChangeDirection;
|
|
|
|
public Notify effectSmoothChangeFinishedNotify = new Notify();
|
|
|
|
private bool effectSmoothChangeWorking;
|
|
|
|
public bool effectBurn;
|
|
|
|
public Texture2D effectBurnTextureBar;
|
|
|
|
public string atlasEffectBurnTextureBarGUID = string.Empty;
|
|
|
|
public Color effectBurnTextureBarColor = Color.red;
|
|
|
|
public Notify effectBurnFinishedNotify = new Notify();
|
|
|
|
public BurnDirection effectBurnDirection = BurnDirection.OnlyWhenDecreasing;
|
|
|
|
private bool effectBurnWorking;
|
|
|
|
private EnergyBar _energyBar;
|
|
|
|
protected float ValueFBurn;
|
|
|
|
protected float ValueF2;
|
|
|
|
[SerializeField]
|
|
private Color _tint = Color.white;
|
|
|
|
protected EnergyBar energyBar
|
|
{
|
|
get
|
|
{
|
|
if (_energyBar == null)
|
|
{
|
|
_energyBar = GetComponent<EnergyBar>();
|
|
MadDebug.Assert(_energyBar != null, "Cannot access energy bar?!");
|
|
}
|
|
return _energyBar;
|
|
}
|
|
}
|
|
|
|
public abstract Rect DrawAreaRect { get; }
|
|
|
|
public bool isBurning
|
|
{
|
|
get
|
|
{
|
|
return effectBurn && Math.Abs(ValueF2 - ValueFBurn) > 0.001f;
|
|
}
|
|
}
|
|
|
|
protected float ValueF
|
|
{
|
|
get
|
|
{
|
|
return energyBar.ValueF;
|
|
}
|
|
}
|
|
|
|
protected Vector2 LabelPositionPixels
|
|
{
|
|
get
|
|
{
|
|
Rect drawAreaRect = DrawAreaRect;
|
|
return (!labelPositionNormalized) ? new Vector2(drawAreaRect.x + labelPosition.x, drawAreaRect.y + labelPosition.y) : new Vector2(drawAreaRect.x + labelPosition.x * drawAreaRect.width, drawAreaRect.y + labelPosition.y * drawAreaRect.height);
|
|
}
|
|
}
|
|
|
|
public float displayValue
|
|
{
|
|
get
|
|
{
|
|
return ValueF2;
|
|
}
|
|
}
|
|
|
|
public float opacity
|
|
{
|
|
get
|
|
{
|
|
return _tint.a;
|
|
}
|
|
set
|
|
{
|
|
_tint.a = Mathf.Clamp01(value);
|
|
}
|
|
}
|
|
|
|
public Color tint
|
|
{
|
|
get
|
|
{
|
|
return _tint;
|
|
}
|
|
set
|
|
{
|
|
_tint = value;
|
|
}
|
|
}
|
|
|
|
public bool burning
|
|
{
|
|
get
|
|
{
|
|
return effectBurn && ValueF2 != ValueFBurn;
|
|
}
|
|
}
|
|
|
|
public virtual void ResetAnimations()
|
|
{
|
|
ValueF2 = ValueF;
|
|
ValueFBurn = ValueF;
|
|
}
|
|
|
|
protected virtual void OnEnable()
|
|
{
|
|
ValueF2 = ValueF;
|
|
}
|
|
|
|
protected virtual void OnDisable()
|
|
{
|
|
}
|
|
|
|
protected virtual void Start()
|
|
{
|
|
}
|
|
|
|
protected virtual void Update()
|
|
{
|
|
UpdateAnimations();
|
|
}
|
|
|
|
private void UpdateAnimations()
|
|
{
|
|
UpdateBarValue();
|
|
UpdateBurnValue();
|
|
}
|
|
|
|
private void UpdateBurnValue()
|
|
{
|
|
EnergyBarCommons.SmoothDisplayValue(ref ValueFBurn, ValueF2, effectSmoothChangeSpeed);
|
|
ValueFBurn = Mathf.Max(ValueFBurn, ValueF2);
|
|
switch (effectBurnDirection)
|
|
{
|
|
case BurnDirection.Both:
|
|
if (ValueF > ValueF2)
|
|
{
|
|
ValueFBurn = ValueF;
|
|
}
|
|
else if (ValueF < ValueF2)
|
|
{
|
|
EnergyBarCommons.SmoothDisplayValue(ref ValueFBurn, ValueF, effectSmoothChangeSpeed);
|
|
}
|
|
else
|
|
{
|
|
ValueFBurn = Mathf.Max(ValueFBurn, ValueF2);
|
|
}
|
|
break;
|
|
case BurnDirection.OnlyWhenDecreasing:
|
|
if (ValueF < ValueF2)
|
|
{
|
|
EnergyBarCommons.SmoothDisplayValue(ref ValueFBurn, ValueF, effectSmoothChangeSpeed);
|
|
}
|
|
else
|
|
{
|
|
ValueFBurn = Mathf.Max(ValueFBurn, ValueF2);
|
|
}
|
|
break;
|
|
case BurnDirection.OnlyWhenIncreasing:
|
|
if (ValueF > ValueF2)
|
|
{
|
|
ValueFBurn = ValueF;
|
|
}
|
|
else
|
|
{
|
|
ValueFBurn = ValueF2;
|
|
}
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
if (!Mathf.Approximately(ValueFBurn, ValueF2))
|
|
{
|
|
effectBurnWorking = true;
|
|
}
|
|
else if (effectBurnWorking)
|
|
{
|
|
effectBurnFinishedNotify.Execute(this);
|
|
effectBurnWorking = false;
|
|
}
|
|
}
|
|
|
|
private void UpdateBarValue()
|
|
{
|
|
if (effectBurn)
|
|
{
|
|
if (effectSmoothChange)
|
|
{
|
|
bool flag = effectSmoothChangeDirection == SmoothDirection.Both || effectSmoothChangeDirection == SmoothDirection.OnlyWhenIncreasing;
|
|
if (ValueF > ValueF2 && flag)
|
|
{
|
|
EnergyBarCommons.SmoothDisplayValue(ref ValueF2, ValueF, effectSmoothChangeSpeed);
|
|
}
|
|
else
|
|
{
|
|
ValueF2 = energyBar.ValueF;
|
|
}
|
|
if (!Mathf.Approximately(ValueF, ValueF2))
|
|
{
|
|
effectSmoothChangeWorking = true;
|
|
}
|
|
else if (effectSmoothChangeWorking)
|
|
{
|
|
effectSmoothChangeFinishedNotify.Execute(this);
|
|
effectSmoothChangeWorking = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ValueF2 = energyBar.ValueF;
|
|
}
|
|
}
|
|
else if (effectSmoothChange)
|
|
{
|
|
bool flag2 = effectSmoothChangeDirection == SmoothDirection.Both || effectSmoothChangeDirection == SmoothDirection.OnlyWhenIncreasing;
|
|
bool flag3 = effectSmoothChangeDirection == SmoothDirection.Both || effectSmoothChangeDirection == SmoothDirection.OnlyWhenDecreasing;
|
|
if ((ValueF > ValueF2 && flag2) || (ValueF < ValueF2 && flag3))
|
|
{
|
|
EnergyBarCommons.SmoothDisplayValue(ref ValueF2, ValueF, effectSmoothChangeSpeed);
|
|
}
|
|
else
|
|
{
|
|
ValueF2 = energyBar.ValueF;
|
|
}
|
|
if (!Mathf.Approximately(ValueF, ValueF2))
|
|
{
|
|
effectSmoothChangeWorking = true;
|
|
}
|
|
else if (effectSmoothChangeWorking)
|
|
{
|
|
effectSmoothChangeFinishedNotify.Execute(this);
|
|
effectSmoothChangeWorking = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ValueF2 = energyBar.ValueF;
|
|
}
|
|
}
|
|
|
|
protected bool RepaintPhase()
|
|
{
|
|
return Event.current.type == EventType.Repaint;
|
|
}
|
|
|
|
protected string LabelFormatResolve(string format)
|
|
{
|
|
format = format.Replace("{cur}", string.Empty + energyBar.valueCurrent);
|
|
format = format.Replace("{min}", string.Empty + energyBar.valueMin);
|
|
format = format.Replace("{max}", string.Empty + energyBar.valueMax);
|
|
format = format.Replace("{cur%}", string.Format("{0:00}", energyBar.ValueF * 100f));
|
|
format = format.Replace("{cur2%}", string.Format("{0:00.0}", energyBar.ValueF * 100f));
|
|
return format;
|
|
}
|
|
|
|
protected Vector4 ToVector4(Rect r)
|
|
{
|
|
return new Vector4(r.xMin, r.yMax, r.xMax, r.yMin);
|
|
}
|
|
|
|
protected Vector2 Round(Vector2 v)
|
|
{
|
|
return new Vector2(Mathf.Round(v.x), Mathf.Round(v.y));
|
|
}
|
|
|
|
protected virtual bool IsVisible()
|
|
{
|
|
if (opacity == 0f)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
protected Color PremultiplyAlpha(Color c)
|
|
{
|
|
return new Color(c.r * c.a, c.g * c.a, c.b * c.a, c.a);
|
|
}
|
|
|
|
protected virtual Color ComputeColor(Color localColor)
|
|
{
|
|
return Multiply(localColor, tint);
|
|
}
|
|
|
|
protected static Color Multiply(Color c1, Color c2)
|
|
{
|
|
return new Color(c1.r * c2.r, c1.g * c2.g, c1.b * c2.b, c1.a * c2.a);
|
|
}
|
|
|
|
protected static int HashAdd(int current, bool obj)
|
|
{
|
|
return MadHashCode.Add(current, obj);
|
|
}
|
|
|
|
protected static int HashAdd(int current, int obj)
|
|
{
|
|
return MadHashCode.Add(current, obj);
|
|
}
|
|
|
|
protected static int HashAdd(int current, float obj)
|
|
{
|
|
return MadHashCode.Add(current, obj);
|
|
}
|
|
|
|
protected static int HashAdd(int current, UnityEngine.Object obj)
|
|
{
|
|
if (obj != null)
|
|
{
|
|
return MadHashCode.Add(current, obj.GetInstanceID());
|
|
}
|
|
return MadHashCode.Add(current, null);
|
|
}
|
|
|
|
protected static int HashAdd(int current, object obj)
|
|
{
|
|
return MadHashCode.Add(current, obj);
|
|
}
|
|
|
|
protected static int HashAddTexture(int current, Texture texture)
|
|
{
|
|
return MadHashCode.Add(current, texture);
|
|
}
|
|
|
|
protected static int HashAddArray(int current, object[] arr)
|
|
{
|
|
return MadHashCode.AddArray(current, arr);
|
|
}
|
|
|
|
protected static int HashAddTextureArray(int current, Texture[] arr, string name = "")
|
|
{
|
|
return MadHashCode.AddArray(current, arr);
|
|
}
|
|
|
|
protected Rect FindBounds(Texture2D texture)
|
|
{
|
|
int left = -1;
|
|
int top = -1;
|
|
int right = -1;
|
|
int bottom = -1;
|
|
bool condition = false;
|
|
Color32[] pixels;
|
|
try
|
|
{
|
|
pixels = texture.GetPixels32();
|
|
}
|
|
catch (UnityException)
|
|
{
|
|
return default(Rect);
|
|
}
|
|
int width = texture.width;
|
|
int height = texture.height;
|
|
int num = 0;
|
|
int num2 = height - 1;
|
|
for (int i = 0; i < pixels.Length; i++)
|
|
{
|
|
Color32 color = pixels[i];
|
|
if (color.a != 0)
|
|
{
|
|
Expand(num, num2, ref left, ref top, ref right, ref bottom);
|
|
condition = true;
|
|
}
|
|
if (++num == width)
|
|
{
|
|
num2--;
|
|
num = 0;
|
|
}
|
|
}
|
|
MadDebug.Assert(condition, "bar texture has no visible pixels");
|
|
Rect rect = new Rect(left, top, right - left + 1, bottom - top + 1);
|
|
return new Rect(rect.xMin / (float)texture.width, 1f - rect.yMax / (float)texture.height, rect.xMax / (float)texture.width - rect.xMin / (float)texture.width, 1f - rect.yMin / (float)texture.height - (1f - rect.yMax / (float)texture.height));
|
|
}
|
|
|
|
protected void Expand(int x, int y, ref int left, ref int top, ref int right, ref int bottom)
|
|
{
|
|
if (left == -1)
|
|
{
|
|
left = (right = x);
|
|
top = (bottom = y);
|
|
return;
|
|
}
|
|
if (left > x)
|
|
{
|
|
left = x;
|
|
}
|
|
else if (right < x)
|
|
{
|
|
right = x;
|
|
}
|
|
if (top > y)
|
|
{
|
|
top = y;
|
|
}
|
|
else if (bottom == -1 || bottom < y)
|
|
{
|
|
bottom = y;
|
|
}
|
|
}
|
|
}
|