214 lines
4.9 KiB
C#
214 lines
4.9 KiB
C#
using UnityEngine;
|
|
|
|
namespace EnergyBarToolkit
|
|
{
|
|
[RequireComponent(typeof(EnergyBar))]
|
|
[ExecuteInEditMode]
|
|
public class SequenceRenderer3D : EnergyBar3DBase
|
|
{
|
|
public enum Method
|
|
{
|
|
Grid = 0,
|
|
Sequence = 1
|
|
}
|
|
|
|
public Method renderingMethod;
|
|
|
|
public Texture2D gridTexture;
|
|
|
|
public string gridAtlasTextureGUID;
|
|
|
|
public int gridWidth = 2;
|
|
|
|
public int gridHeight = 2;
|
|
|
|
public bool gridFrameCountManual;
|
|
|
|
public int gridFrameCount = 4;
|
|
|
|
public Color gridTint = Color.white;
|
|
|
|
public Texture2D[] sequenceTextures = new Texture2D[0];
|
|
|
|
public string[] sequenceAtlasTexturesGUID = new string[0];
|
|
|
|
private MadSprite spriteBar;
|
|
|
|
private int lastRebuildHash;
|
|
|
|
private bool dirty = true;
|
|
|
|
public override Rect DrawAreaRect
|
|
{
|
|
get
|
|
{
|
|
if (spriteBar != null && spriteBar.CanDraw())
|
|
{
|
|
return spriteBar.GetTransformedBounds();
|
|
}
|
|
if (gridTexture != null)
|
|
{
|
|
Vector2 vector = EnergyBar3DBase.PivotOffset(pivot);
|
|
float num = gridTexture.width;
|
|
float num2 = gridTexture.height;
|
|
if (renderingMethod == Method.Grid)
|
|
{
|
|
num /= (float)gridWidth;
|
|
num2 /= (float)gridHeight;
|
|
}
|
|
return new Rect(vector.x * num, (1f - vector.y) * num2, num, num2);
|
|
}
|
|
return default(Rect);
|
|
}
|
|
}
|
|
|
|
protected override void OnEnable()
|
|
{
|
|
base.OnEnable();
|
|
}
|
|
|
|
protected override void Update()
|
|
{
|
|
base.Update();
|
|
if (RebuildNeeded())
|
|
{
|
|
Rebuild();
|
|
}
|
|
if (!(panel == null))
|
|
{
|
|
UpdateColor();
|
|
UpdateProgress();
|
|
if (renderingMethod == Method.Grid && spriteBar.CanDraw())
|
|
{
|
|
spriteBar.size = new Vector2(spriteBar.initialSize.x / (float)gridWidth, spriteBar.initialSize.y / (float)gridHeight);
|
|
}
|
|
spriteBar.pivotPoint = EnergyBar3DBase.Translate(pivot);
|
|
}
|
|
}
|
|
|
|
private void UpdateColor()
|
|
{
|
|
if (!(spriteBar == null))
|
|
{
|
|
spriteBar.tint = ComputeColor(gridTint);
|
|
}
|
|
}
|
|
|
|
private void UpdateProgress()
|
|
{
|
|
switch (renderingMethod)
|
|
{
|
|
case Method.Grid:
|
|
UpdateGridProgress();
|
|
break;
|
|
case Method.Sequence:
|
|
UpdateSequenceProgress();
|
|
break;
|
|
default:
|
|
Debug.Log("Unknown rendering method: " + renderingMethod);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void UpdateGridProgress()
|
|
{
|
|
if (base.useAtlas)
|
|
{
|
|
spriteBar.inputType = MadSprite.InputType.TextureAtlas;
|
|
spriteBar.textureAtlas = atlas;
|
|
spriteBar.textureAtlasSpriteGUID = gridAtlasTextureGUID;
|
|
}
|
|
else
|
|
{
|
|
spriteBar.texture = gridTexture;
|
|
}
|
|
int size = ((!gridFrameCountManual) ? (gridWidth * gridHeight) : gridFrameCount);
|
|
int num = Index(size);
|
|
float y = (float)(gridHeight - 1 - num / gridWidth) / (float)gridHeight;
|
|
float x = (float)(num % gridWidth) / (float)gridWidth;
|
|
spriteBar.textureRepeat = new Vector2(1f / (float)gridWidth, 1f / (float)gridHeight);
|
|
spriteBar.textureOffset = new Vector2(x, y);
|
|
}
|
|
|
|
private void UpdateSequenceProgress()
|
|
{
|
|
if (base.useAtlas)
|
|
{
|
|
if (sequenceAtlasTexturesGUID.Length > 0)
|
|
{
|
|
spriteBar.textureAtlas = atlas;
|
|
spriteBar.textureAtlasSpriteGUID = sequenceAtlasTexturesGUID[Index(sequenceAtlasTexturesGUID.Length)];
|
|
}
|
|
}
|
|
else if (sequenceTextures.Length > 0)
|
|
{
|
|
spriteBar.texture = sequenceTextures[Index(sequenceTextures.Length)];
|
|
}
|
|
}
|
|
|
|
private int Index(int size)
|
|
{
|
|
return (int)Mathf.Min(Mathf.Floor(ValueF2 * (float)size), size - 1);
|
|
}
|
|
|
|
private bool RebuildNeeded()
|
|
{
|
|
if (panel == null)
|
|
{
|
|
return false;
|
|
}
|
|
MadHashCode madHashCode = new MadHashCode();
|
|
madHashCode.Add(textureMode);
|
|
madHashCode.Add(atlas);
|
|
madHashCode.AddEnumerable(texturesBackground);
|
|
madHashCode.AddEnumerable(atlasTexturesBackground);
|
|
madHashCode.AddEnumerable(texturesForeground);
|
|
madHashCode.AddEnumerable(atlasTexturesForeground);
|
|
madHashCode.Add(renderingMethod);
|
|
madHashCode.Add(gridTexture);
|
|
madHashCode.Add(gridAtlasTextureGUID);
|
|
madHashCode.AddEnumerable(sequenceTextures);
|
|
madHashCode.AddEnumerable(sequenceAtlasTexturesGUID);
|
|
madHashCode.Add(gridWidth);
|
|
madHashCode.Add(gridHeight);
|
|
madHashCode.Add(gridFrameCountManual);
|
|
madHashCode.Add(gridFrameCount);
|
|
madHashCode.Add(guiDepth);
|
|
madHashCode.Add(labelEnabled);
|
|
madHashCode.Add(labelFont);
|
|
madHashCode.Add(premultipliedAlpha);
|
|
int hashCode = madHashCode.GetHashCode();
|
|
if (hashCode != lastRebuildHash || dirty)
|
|
{
|
|
lastRebuildHash = hashCode;
|
|
dirty = false;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
protected override void Rebuild()
|
|
{
|
|
base.Rebuild();
|
|
if (spriteBar != null)
|
|
{
|
|
MadGameObject.SafeDestroy(spriteBar.gameObject);
|
|
}
|
|
int depth = guiDepth * 32;
|
|
depth = BuildBackgroundTextures(depth);
|
|
depth = BuildBar(depth);
|
|
depth = BuildForegroundTextures(depth);
|
|
depth = RebuildLabel(depth);
|
|
UpdateContainer();
|
|
}
|
|
|
|
private int BuildBar(int nextDepth)
|
|
{
|
|
spriteBar = CreateHidden<MadSprite>("bar");
|
|
spriteBar.guiDepth = nextDepth++;
|
|
SetTexture(spriteBar, gridTexture, gridAtlasTextureGUID);
|
|
return nextDepth;
|
|
}
|
|
}
|
|
}
|