106 lines
2.8 KiB
C#
106 lines
2.8 KiB
C#
using GeometryTD.Definition;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace GeometryTD.UI
|
|
{
|
|
public class IconArea : MonoBehaviour
|
|
{
|
|
[SerializeField] private Image _board;
|
|
|
|
[SerializeField] private Image _icon;
|
|
|
|
private IconAreaContext _context;
|
|
|
|
public virtual Sprite CurrentIconSprite => _icon != null ? _icon.sprite : null;
|
|
|
|
public virtual Color CurrentIconColor => _icon != null ? _icon.color : Color.white;
|
|
|
|
public virtual Vector2 CurrentIconSize => _icon != null ? _icon.rectTransform.rect.size : Vector2.zero;
|
|
|
|
public virtual Material CurrentIconMaterial => _icon != null ? _icon.material : null;
|
|
|
|
public virtual RectTransform IconRectTransform => _icon != null ? _icon.rectTransform : null;
|
|
|
|
public virtual void OnInit(IconAreaContext context)
|
|
{
|
|
if (context == null)
|
|
{
|
|
Log.Error("Icon area need IconAreaContext.");
|
|
return;
|
|
}
|
|
|
|
_context = context;
|
|
|
|
switch (context.ComponentSlotType)
|
|
{
|
|
case TowerCompSlotType.Base:
|
|
case TowerCompSlotType.Bearing:
|
|
case TowerCompSlotType.Muzzle:
|
|
GameEntry.SpriteCache.GetSprite(context.ComponentSlotType.ToString(), SetIcon);
|
|
break;
|
|
default:
|
|
SetIcon(_context.Icon);
|
|
break;
|
|
}
|
|
|
|
SetRarity(_context.Rarity);
|
|
SetIconColor(_context.Color);
|
|
SetIconVisible(true);
|
|
}
|
|
|
|
public virtual void SetIcon(Sprite sprite)
|
|
{
|
|
if (_icon != null)
|
|
{
|
|
_icon.sprite = sprite;
|
|
}
|
|
}
|
|
|
|
public virtual void SetRarity(RarityType rarity)
|
|
{
|
|
if (_board == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_board.color = rarity switch
|
|
{
|
|
RarityType.White => Color.white,
|
|
RarityType.Green => Color.green,
|
|
RarityType.Blue => Color.blue,
|
|
RarityType.Purple => Color.magenta,
|
|
RarityType.Red => Color.red,
|
|
_ => Color.clear,
|
|
};
|
|
}
|
|
|
|
public virtual void SetIconColor(Color color)
|
|
{
|
|
if (_icon == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_icon.color = color;
|
|
}
|
|
|
|
public virtual void SetIconVisible(bool visible)
|
|
{
|
|
if (_icon != null)
|
|
{
|
|
_icon.enabled = visible;
|
|
}
|
|
}
|
|
|
|
public virtual void OnReset()
|
|
{
|
|
SetIcon(null);
|
|
SetRarity(RarityType.None);
|
|
SetIconColor(Color.clear);
|
|
SetIconVisible(true);
|
|
}
|
|
}
|
|
}
|