223 lines
4.4 KiB
C#
223 lines
4.4 KiB
C#
using UnityEngine;
|
|
|
|
namespace EnergyBarToolkit
|
|
{
|
|
[RequireComponent(typeof(EnergyBar))]
|
|
[ExecuteInEditMode]
|
|
public class EnergyBarSequenceRenderer : EnergyBarOnGUIBase
|
|
{
|
|
public enum Method
|
|
{
|
|
Grid = 0,
|
|
Sequence = 1
|
|
}
|
|
|
|
public Vector2 position = default(Vector2);
|
|
|
|
public bool positionNormalized;
|
|
|
|
public Vector2 size;
|
|
|
|
public Color color = Color.white;
|
|
|
|
public Method method;
|
|
|
|
public Texture2D gridTexture;
|
|
|
|
public int gridWidth = 2;
|
|
|
|
public int gridHeight = 2;
|
|
|
|
public bool frameCountManual;
|
|
|
|
public int frameCount = 4;
|
|
|
|
public Texture2D[] sequence;
|
|
|
|
[SerializeField]
|
|
private bool sizeNormalized;
|
|
|
|
[SerializeField]
|
|
private bool sizeCalculate = true;
|
|
|
|
private Vector2 PositionPixels
|
|
{
|
|
get
|
|
{
|
|
if (positionNormalized)
|
|
{
|
|
return new Vector2(position.x * (float)Screen.width, position.y * (float)Screen.height);
|
|
}
|
|
return position;
|
|
}
|
|
}
|
|
|
|
public override Vector2 SizePixels
|
|
{
|
|
get
|
|
{
|
|
return GetSizePixels(size);
|
|
}
|
|
set
|
|
{
|
|
SetSizePixels(ref size, value);
|
|
}
|
|
}
|
|
|
|
public override Vector2 TextureSizePixels
|
|
{
|
|
get
|
|
{
|
|
switch (method)
|
|
{
|
|
case Method.Grid:
|
|
if (gridTexture == null)
|
|
{
|
|
return Vector2.one;
|
|
}
|
|
return new Vector2(gridTexture.width / gridWidth, gridTexture.height / gridHeight);
|
|
case Method.Sequence:
|
|
{
|
|
if (sequence == null || sequence.Length == 0 || sequence[0] == null)
|
|
{
|
|
return Vector2.one;
|
|
}
|
|
Texture2D texture2D = sequence[0];
|
|
return new Vector2(texture2D.width, texture2D.height);
|
|
}
|
|
default:
|
|
MadDebug.Assert(false, "unknown method: " + method);
|
|
return Vector2.one;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override Rect DrawAreaRect
|
|
{
|
|
get
|
|
{
|
|
Vector2 vector = Round(SizePixels);
|
|
Vector2 vector2 = RealPosition(Round(PositionPixels), SizePixels);
|
|
return new Rect(vector2.x, vector2.y, vector.x, vector.y);
|
|
}
|
|
}
|
|
|
|
private new void Start()
|
|
{
|
|
base.Start();
|
|
if (version == 169)
|
|
{
|
|
Upgrade_169_171();
|
|
}
|
|
}
|
|
|
|
private void Upgrade_169_171()
|
|
{
|
|
if (sizeNormalized)
|
|
{
|
|
resizeMode = ResizeMode.Stretch;
|
|
}
|
|
else if (!sizeCalculate)
|
|
{
|
|
resizeMode = ResizeMode.Fixed;
|
|
}
|
|
version = 171;
|
|
}
|
|
|
|
protected override void Update()
|
|
{
|
|
base.Update();
|
|
FixValues();
|
|
if (!frameCountManual)
|
|
{
|
|
frameCount = gridWidth * gridHeight;
|
|
}
|
|
}
|
|
|
|
private new void OnGUI()
|
|
{
|
|
base.OnGUI();
|
|
if (RepaintPhase() && IsVisible() && IsValid())
|
|
{
|
|
Rect texCoords;
|
|
Texture2D texture = GetTexture(out texCoords);
|
|
if (texture != null)
|
|
{
|
|
GUIDrawBackground();
|
|
Vector2 bounds = Round(SizePixels);
|
|
Vector2 vector = RealPosition(Round(PositionPixels), bounds);
|
|
DrawTexture(new Rect(vector.x, vector.y, bounds.x, bounds.y), texture, texCoords, color);
|
|
GUIDrawForeground();
|
|
}
|
|
GUIDrawLabel();
|
|
}
|
|
}
|
|
|
|
private void FixValues()
|
|
{
|
|
gridWidth = Mathf.Max(1, gridWidth);
|
|
gridHeight = Mathf.Max(1, gridHeight);
|
|
frameCount = Mathf.Max(1, frameCount);
|
|
}
|
|
|
|
public bool IsValid()
|
|
{
|
|
if (base.energyBar == null)
|
|
{
|
|
return false;
|
|
}
|
|
switch (method)
|
|
{
|
|
case Method.Grid:
|
|
return gridWidth > 0 && gridHeight > 0 && gridTexture != null;
|
|
case Method.Sequence:
|
|
return sequence.Length > 0 && sequence[0] != null;
|
|
default:
|
|
MadDebug.Assert(false, "unknown method: " + method);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public Texture2D GetTexture(out Rect texCoords)
|
|
{
|
|
switch (method)
|
|
{
|
|
case Method.Grid:
|
|
return GetTextureGrid(out texCoords);
|
|
case Method.Sequence:
|
|
return GetTextureSequence(out texCoords);
|
|
default:
|
|
MadDebug.Assert(false, "unknown method: " + method);
|
|
texCoords = default(Rect);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private Texture2D GetTextureGrid(out Rect texCoords)
|
|
{
|
|
int num = frameCount;
|
|
int num2 = Index(num);
|
|
float y = (float)(gridHeight - 1 - num2 / gridWidth) / (float)gridHeight;
|
|
float x = (float)(num2 % gridWidth) / (float)gridWidth;
|
|
float width = 1f / (float)gridWidth;
|
|
float height = 1f / (float)gridHeight;
|
|
texCoords = new Rect(x, y, width, height);
|
|
return gridTexture;
|
|
}
|
|
|
|
private Texture2D GetTextureSequence(out Rect texCoords)
|
|
{
|
|
int num = sequence.Length;
|
|
int num2 = Index(num);
|
|
texCoords = new Rect(0f, 0f, 1f, 1f);
|
|
return sequence[num2];
|
|
}
|
|
|
|
private int Index(int size)
|
|
{
|
|
float valueF = ValueF2;
|
|
return (int)Mathf.Min(Mathf.Floor(valueF * (float)size), size - 1);
|
|
}
|
|
}
|
|
}
|