93 lines
2.9 KiB
C#
93 lines
2.9 KiB
C#
using Cysharp.Threading.Tasks;
|
|
using SepCore.AsyncTask;
|
|
using SepCore.Definition;
|
|
using SepCore.Event;
|
|
using UnityEngine;
|
|
|
|
namespace SepCore.UI
|
|
{
|
|
public partial class ItemBasicTooltipForm
|
|
{
|
|
[SerializeField] private bool _autoResizeHeight = true;
|
|
[SerializeField] private float _fixedTopHeight = 150f;
|
|
[SerializeField] private float _fixedBottomHeight = 250f;
|
|
[SerializeField] private float _fixedWidth = 600f;
|
|
|
|
private ItemBasicTooltipContext _context;
|
|
private bool _listenersBound;
|
|
|
|
public RectTransform RectTransform => View.contentRectTransform;
|
|
|
|
public void RefreshUI(ItemBasicTooltipContext context)
|
|
{
|
|
EnsureListeners();
|
|
_context = context;
|
|
|
|
View.titleText.text = _context.Title;
|
|
View.typeText.text = _context.TypeName;
|
|
View.infoText.text = _context.Description;
|
|
View.textFormatText.Set(_context.Price);
|
|
View.recycleButton.gameObject.SetActive(_context.ShowRecycleButton);
|
|
|
|
ResizeToFitContent();
|
|
|
|
if (!string.IsNullOrEmpty(_context.IconAssetName))
|
|
{
|
|
LoadIconAsync(_context.IconAssetName, _context.Rarity).Forget();
|
|
}
|
|
}
|
|
|
|
private async UniTaskVoid LoadIconAsync(string iconAssetName, Rarity rarity)
|
|
{
|
|
var sprite = await SpriteLoader.LoadSpriteByKeyAsync(iconAssetName);
|
|
if (_context != null && _context.IconAssetName == iconAssetName)
|
|
{
|
|
View.iconArea.OnInit(sprite, rarity);
|
|
}
|
|
}
|
|
|
|
public void OnRecycleButtonClick()
|
|
{
|
|
if (_context != null)
|
|
{
|
|
GameEntry.Event.Fire(this, ItemTooltipRecycleEventArgs.Create(_context.Index, _context.Price));
|
|
}
|
|
}
|
|
|
|
public void OnCancelButtonClick()
|
|
{
|
|
GameEntry.Event.Fire(this, ItemTooltipHideEventArgs.Create());
|
|
}
|
|
|
|
private void EnsureListeners()
|
|
{
|
|
if (_listenersBound)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_listenersBound = true;
|
|
View.recycleButton.onClick.AddListener(OnRecycleButtonClick);
|
|
View.cancelButton.onClick.AddListener(OnCancelButtonClick);
|
|
}
|
|
|
|
public void ResizeToFitContent()
|
|
{
|
|
if (!_autoResizeHeight)
|
|
{
|
|
return;
|
|
}
|
|
|
|
View.infoText.ForceMeshUpdate();
|
|
var descriptionSize = View.infoText.rectTransform.sizeDelta;
|
|
descriptionSize.y = View.infoText.preferredHeight;
|
|
View.infoText.rectTransform.sizeDelta = descriptionSize;
|
|
float descriptionHeight = Mathf.Max(0f, View.infoText.preferredHeight);
|
|
float targetHeight = _fixedTopHeight + descriptionHeight + _fixedBottomHeight;
|
|
|
|
Vector2 size = new Vector2(_fixedWidth, targetHeight);
|
|
View.contentRectTransform.sizeDelta = size;
|
|
}
|
|
}
|
|
}
|