244 lines
5.1 KiB
C#
244 lines
5.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace EnergyBarToolkit
|
|
{
|
|
[RequireComponent(typeof(EnergyBar))]
|
|
[ExecuteInEditMode]
|
|
public class SequenceRendererUGUI : EnergyBarUGUIBase
|
|
{
|
|
public enum RenderMethod
|
|
{
|
|
Grid = 0,
|
|
Sequence = 1
|
|
}
|
|
|
|
public RenderMethod renderMethod;
|
|
|
|
public SpriteTex gridSprite = new SpriteTex();
|
|
|
|
public int gridWidth = 3;
|
|
|
|
public int gridHeight = 3;
|
|
|
|
public bool frameCountAuto = true;
|
|
|
|
public int frameCount;
|
|
|
|
public List<SpriteTex> sequenceSprites = new List<SpriteTex>();
|
|
|
|
public Color sequenceTint = Color.white;
|
|
|
|
[SerializeField]
|
|
private int lastRebuildHash;
|
|
|
|
private bool dirty;
|
|
|
|
[SerializeField]
|
|
private Image2 barImage;
|
|
|
|
public override void SetNativeSize()
|
|
{
|
|
switch (renderMethod)
|
|
{
|
|
case RenderMethod.Grid:
|
|
SetNativeSizeGrid();
|
|
break;
|
|
case RenderMethod.Sequence:
|
|
SetNativeSizeSequence();
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
|
|
private void SetNativeSizeGrid()
|
|
{
|
|
if (gridSprite.sprite == null)
|
|
{
|
|
Rebuild();
|
|
if (gridSprite.sprite == null)
|
|
{
|
|
Debug.LogWarning("Cannot resize bar that has not been created yet");
|
|
return;
|
|
}
|
|
}
|
|
Rect rect = gridSprite.sprite.rect;
|
|
float w = rect.width / (float)gridWidth;
|
|
float h = rect.height / (float)gridHeight;
|
|
SetSize(rectTransform, w, h);
|
|
}
|
|
|
|
private void SetNativeSizeSequence()
|
|
{
|
|
if (sequenceSprites.Count == 0)
|
|
{
|
|
Rebuild();
|
|
if (sequenceSprites.Count == 0)
|
|
{
|
|
Debug.LogWarning("Cannot resize bar that has not been created yet");
|
|
return;
|
|
}
|
|
}
|
|
SpriteTex spriteTex = sequenceSprites[0];
|
|
if (spriteTex != null && !(spriteTex.sprite == null))
|
|
{
|
|
Rect rect = spriteTex.sprite.rect;
|
|
SetSize(rectTransform, rect.width, rect.height);
|
|
}
|
|
}
|
|
|
|
protected override void Update()
|
|
{
|
|
base.Update();
|
|
if (RebuildNeeded())
|
|
{
|
|
Rebuild();
|
|
}
|
|
UpdateNonIntrusive();
|
|
}
|
|
|
|
private void UpdateNonIntrusive()
|
|
{
|
|
UpdateSize();
|
|
UpdateValue();
|
|
UpdateColor();
|
|
}
|
|
|
|
private void UpdateSize()
|
|
{
|
|
RectTransform component = GetComponent<RectTransform>();
|
|
for (int i = 0; i < createdChildren.Count; i++)
|
|
{
|
|
GameObject gameObject = createdChildren[i];
|
|
RectTransform component2 = gameObject.GetComponent<RectTransform>();
|
|
SetSize(component2, component.rect.size);
|
|
}
|
|
}
|
|
|
|
private void UpdateValue()
|
|
{
|
|
switch (renderMethod)
|
|
{
|
|
case RenderMethod.Grid:
|
|
UpdateValueGrid();
|
|
break;
|
|
case RenderMethod.Sequence:
|
|
UpdateValueSequence();
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
|
|
private void UpdateValueGrid()
|
|
{
|
|
float valueF = ValueF2;
|
|
int frameIndex = GetFrameIndex(valueF);
|
|
barImage.uvTiling = GetTilling();
|
|
barImage.uvOffset = GetOffset(frameIndex);
|
|
barImage.sprite = gridSprite.sprite;
|
|
barImage.color = gridSprite.color;
|
|
barImage.material = gridSprite.material;
|
|
barImage.SetAllDirty();
|
|
}
|
|
|
|
private void UpdateColor()
|
|
{
|
|
switch (renderMethod)
|
|
{
|
|
case RenderMethod.Grid:
|
|
barImage.color = ComputeColor(gridSprite.color);
|
|
break;
|
|
case RenderMethod.Sequence:
|
|
barImage.color = ComputeColor(sequenceTint);
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
|
|
private Vector2 GetOffset(int frameIndex)
|
|
{
|
|
Vector2 tilling = GetTilling();
|
|
int num = frameIndex / gridWidth;
|
|
int num2 = frameIndex - num * gridWidth;
|
|
return new Vector2(tilling.x * (float)num2, 1f - tilling.y * (float)num - tilling.y);
|
|
}
|
|
|
|
private Vector2 GetTilling()
|
|
{
|
|
return new Vector2(1f / (float)gridWidth, 1f / (float)gridHeight);
|
|
}
|
|
|
|
private void UpdateValueSequence()
|
|
{
|
|
if (GetFrameCount() != 0)
|
|
{
|
|
float valueF = ValueF2;
|
|
int frameIndex = GetFrameIndex(valueF);
|
|
SpriteTex spriteTex = sequenceSprites[frameIndex];
|
|
barImage.sprite = spriteTex.sprite;
|
|
barImage.color = spriteTex.color;
|
|
barImage.material = spriteTex.material;
|
|
barImage.uvOffset = Vector2.zero;
|
|
barImage.uvTiling = Vector2.one;
|
|
barImage.SetAllDirty();
|
|
}
|
|
}
|
|
|
|
private int GetFrameIndex(float progress)
|
|
{
|
|
int num = GetFrameCount();
|
|
return Mathf.FloorToInt((float)(num - 1) * progress);
|
|
}
|
|
|
|
private int GetFrameCount()
|
|
{
|
|
switch (renderMethod)
|
|
{
|
|
case RenderMethod.Grid:
|
|
if (frameCountAuto)
|
|
{
|
|
return gridWidth * gridHeight;
|
|
}
|
|
return frameCount;
|
|
case RenderMethod.Sequence:
|
|
return sequenceSprites.Count;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
|
|
private bool RebuildNeeded()
|
|
{
|
|
int currentHash = 37;
|
|
currentHash = MadHashCode.AddList(currentHash, spritesBackground);
|
|
currentHash = MadHashCode.AddList(currentHash, spritesForeground);
|
|
if (currentHash != lastRebuildHash || dirty)
|
|
{
|
|
lastRebuildHash = currentHash;
|
|
dirty = false;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void Rebuild()
|
|
{
|
|
RemoveCreatedChildren();
|
|
BuildBackgroundImages();
|
|
BuildBar();
|
|
BuildForegroundImages();
|
|
UpdateSize();
|
|
MoveLabelToTop();
|
|
}
|
|
|
|
private void BuildBar()
|
|
{
|
|
barImage = CreateChild<Image2>("bar");
|
|
}
|
|
}
|
|
}
|