Handbuch Tabs: Bauen und Index (fertig)
UI_Prefabs HandbuchDataAssets
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
using System.Collections;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace DepthsOfGreed.HandbookUI
|
||||
{
|
||||
public sealed class TowerCardView : MonoBehaviour, IPointerClickHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
|
||||
{
|
||||
public HandbookTowerEntry Entry { get; private set; }
|
||||
private BuildMenuController menu;
|
||||
private GameObject front, back;
|
||||
private TMP_Text nameText, costText, statsText, infoText;
|
||||
private Coroutine animation;
|
||||
private Vector3 originalScale;
|
||||
private bool showingBack, dragged;
|
||||
private Image icon;
|
||||
private Sprite placeholderSprite;
|
||||
private Color placeholderColor;
|
||||
|
||||
public void Initialize(BuildMenuController owner)
|
||||
{
|
||||
menu = owner;
|
||||
front = transform.Find("Front").gameObject;
|
||||
back = transform.Find("Back").gameObject;
|
||||
nameText = front.transform.Find("NameText").GetComponent<TMP_Text>();
|
||||
costText = front.transform.Find("CostText").GetComponent<TMP_Text>();
|
||||
statsText = front.transform.Find("StatsText").GetComponent<TMP_Text>();
|
||||
infoText = back.transform.Find("InfoText").GetComponent<TMP_Text>();
|
||||
icon = front.transform.Find("TowerIcon").GetComponent<Image>();
|
||||
placeholderSprite = icon.sprite;
|
||||
placeholderColor = icon.color;
|
||||
originalScale = transform.localScale;
|
||||
foreach (var text in GetComponentsInChildren<TMP_Text>(true))
|
||||
{
|
||||
text.raycastTarget = false;
|
||||
text.color = Color.black;
|
||||
text.enableAutoSizing = true;
|
||||
text.fontSizeMin = 14;
|
||||
text.fontSizeMax = text.fontSize;
|
||||
}
|
||||
SetBack(false, true);
|
||||
}
|
||||
|
||||
public void Bind(HandbookTowerEntry entry)
|
||||
{
|
||||
Entry = entry;
|
||||
nameText.text = entry.displayName;
|
||||
costText.text = "Kosten: " + entry.energyCost + " Energie (Test)";
|
||||
statsText.text = entry.summary;
|
||||
infoText.text = entry.description;
|
||||
icon.sprite = entry.icon ? entry.icon : placeholderSprite;
|
||||
icon.color = entry.icon ? Color.white : placeholderColor;
|
||||
icon.preserveAspect = true;
|
||||
}
|
||||
|
||||
public void OnPointerClick(PointerEventData data)
|
||||
{
|
||||
if (data.button == PointerEventData.InputButton.Left && !data.dragging && !dragged)
|
||||
menu.ToggleCard(this);
|
||||
dragged = false;
|
||||
}
|
||||
|
||||
public void OnBeginDrag(PointerEventData data)
|
||||
{
|
||||
dragged = true;
|
||||
if (data.button == PointerEventData.InputButton.Left && !showingBack && menu.Placement)
|
||||
menu.Placement.Begin(Entry, data.position);
|
||||
}
|
||||
public void OnDrag(PointerEventData data)
|
||||
{
|
||||
if (menu.Placement) menu.Placement.Move(data.position);
|
||||
}
|
||||
public void OnEndDrag(PointerEventData data)
|
||||
{
|
||||
if (menu.Placement) menu.Placement.End(data.position);
|
||||
dragged = false;
|
||||
}
|
||||
|
||||
public void SetBack(bool showBack, bool instant = false)
|
||||
{
|
||||
showingBack = showBack;
|
||||
if (animation != null) StopCoroutine(animation);
|
||||
animation = null;
|
||||
if (instant || !isActiveAndEnabled)
|
||||
{
|
||||
transform.localScale = originalScale;
|
||||
front.SetActive(!showBack);
|
||||
back.SetActive(showBack);
|
||||
}
|
||||
else animation = StartCoroutine(Flip(showBack));
|
||||
}
|
||||
|
||||
private IEnumerator Flip(bool showBack)
|
||||
{
|
||||
float startingY = transform.localScale.y;
|
||||
const float halfDuration = 0.09f;
|
||||
for (float elapsed = 0; elapsed < halfDuration; elapsed += Time.unscaledDeltaTime)
|
||||
{
|
||||
transform.localScale = new Vector3(originalScale.x, Mathf.Lerp(startingY, 0.02f, elapsed / halfDuration), originalScale.z);
|
||||
yield return null;
|
||||
}
|
||||
front.SetActive(!showBack);
|
||||
back.SetActive(showBack);
|
||||
for (float elapsed = 0; elapsed < halfDuration; elapsed += Time.unscaledDeltaTime)
|
||||
{
|
||||
transform.localScale = new Vector3(originalScale.x, Mathf.Lerp(0.02f, originalScale.y, elapsed / halfDuration), originalScale.z);
|
||||
yield return null;
|
||||
}
|
||||
transform.localScale = originalScale;
|
||||
animation = null;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (animation != null) StopCoroutine(animation);
|
||||
animation = null;
|
||||
transform.localScale = originalScale;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user