| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- using System;
- using System.Collections;
- using DG.Tweening;
- using UnityEngine.EventSystems;
- using DevilLink2.Core;
- using UnityEngine;
- using UnityEngine.UI;
- namespace DevilLink2.Presentation
- {
- [DisallowMultipleComponent]
- public sealed class TileView : MonoBehaviour, IPointerClickHandler
- {
- [SerializeField] private Image face;
- [SerializeField] private Image border;
- [SerializeField] private Text label;
- [SerializeField] private Sprite impArt = null;
- [SerializeField] private Sprite tridentArt = null;
- [SerializeField] private Sprite potionArt = null;
- [SerializeField] private Sprite piggyArt = null;
- [SerializeField] private Sprite ringArt = null;
- [SerializeField] private Sprite candleArt = null;
- [SerializeField] private Sprite starArt = null;
- [SerializeField] private Sprite aArt = null;
- [SerializeField] private Sprite qArt = null;
- [SerializeField] private Sprite jArt = null;
- [SerializeField] private Sprite tenArt = null;
- [SerializeField] private Sprite nineArt = null;
- private Tween stateTween, removalTween;
- private Action removalCompleted;
- private bool removalNotified;
- private Image artwork;
- public Image Artwork => artwork;
- private bool interactive, removing;
- private Coroutine shake;
- private Vector2 shakeOrigin;
- private bool selected, hinted;
- public bool IsHinted => hinted;
- public bool IsRemoving => removing;
- public Tile Data { get; private set; }
- public event Action<TileView> Clicked;
- public void SetInteractive(bool enabled) { interactive = enabled; if (border) border.raycastTarget = enabled && !removing; }
- public void OnPointerClick(PointerEventData data)
- { if (interactive && !removing && data.button == PointerEventData.InputButton.Left) Clicked?.Invoke(this); }
- public void Shake()
- {
- if (removing || !Application.isPlaying) return;
- if (shake != null) { StopCoroutine(shake); ((RectTransform)transform).anchoredPosition = shakeOrigin; }
- shakeOrigin = ((RectTransform)transform).anchoredPosition; shake = StartCoroutine(ShakeRoutine());
- }
- private IEnumerator ShakeRoutine()
- {
- float elapsed = 0;
- while (elapsed < .32f)
- {
- elapsed += Time.unscaledDeltaTime;
- ((RectTransform)transform).anchoredPosition = shakeOrigin + Vector2.right * Mathf.Sin(elapsed / .32f * Mathf.PI * 6) * 4 * (1 - elapsed / .32f);
- yield return null;
- }
- ((RectTransform)transform).anchoredPosition = shakeOrigin; shake = null;
- }
- public void PlayRemoval(Action completed)
- {
- if (removing) return;
- removing = true; removalNotified = false; removalCompleted = completed; SetInteractive(false);
- KillStateAndShake();
- if (!Application.isPlaying || !isActiveAndEnabled) { CompleteRemoval(); return; }
- var group = GetComponent<CanvasGroup>(); if (!group) group = gameObject.AddComponent<CanvasGroup>();
- float progress = 0;
- removalTween = DOTween.To(() => progress, value => {
- progress = value; group.alpha = 1 - value;
- transform.localScale = Vector3.one * Mathf.Lerp(1, .86f, value);
- }, 1, .22f).SetEase(Ease.OutQuad).SetUpdate(true).OnComplete(CompleteRemoval);
- }
- private void CompleteRemoval()
- {
- if (removalNotified) return;
- removalNotified = true;
- removalTween?.Kill(); removalTween = null;
- var callback = removalCompleted; removalCompleted = null;
- try { callback?.Invoke(); }
- finally
- {
- if (this) { gameObject.SetActive(false); if (Application.isPlaying) Destroy(gameObject); else DestroyImmediate(gameObject); }
- }
- }
- private void KillStateAndShake()
- {
- stateTween?.Kill(); stateTween = null;
- if (shake != null) { StopCoroutine(shake); shake = null; ((RectTransform)transform).anchoredPosition = shakeOrigin; }
- }
- // Pause completes already accepted removals; cancellation on destruction never invokes stale callbacks.
- public void FinishVisuals()
- {
- KillStateAndShake();
- if (removing)
- {
- if (!removalNotified) CompleteRemoval();
- else if (Application.isPlaying) Destroy(gameObject); else DestroyImmediate(gameObject);
- return;
- }
- hinted = false; ApplyVisualState(false);
- }
- private void OnDisable()
- {
- KillStateAndShake(); removalTween?.Kill(); removalTween = null;
- removalCompleted = null; removalNotified = true;
- transform.localScale = Vector3.one; transform.localRotation = Quaternion.identity;
- var group = GetComponent<CanvasGroup>(); if (group) group.alpha = 1;
- }
- public int TileId { get; private set; }
- public string DisplayText => label.text;
- public void Configure(Image background, Image outline, Text text) { face = background; border = outline; label = text; }
- public void Bind(Tile tile)
- {
- if (tile == null) throw new ArgumentNullException(nameof(tile));
- if (!face || !border || !label) throw new InvalidOperationException("TileView prefab is missing visual references.");
- KillStateAndShake(); removalTween?.Kill(); removalTween = null; removalCompleted = null;
- removing = false; removalNotified = false;
- transform.localScale = Vector3.one; transform.localRotation = Quaternion.identity;
- var group = GetComponent<CanvasGroup>(); if (group) group.alpha = 1;
- Data = tile; TileId = tile.Id; label.text = SymbolText(tile.Symbol);
- ApplyArtwork(tile.Symbol);
- face.raycastTarget = border.raycastTarget = label.raycastTarget = false;
- selected = hinted = false; ApplyVisualState(false);
- }
- public void SetHighlighted(bool on)
- {
- if (selected == on) return;
- selected = on; ApplyVisualState();
- }
- public void SetHinted(bool on) { if (hinted == on) return; hinted = on; ApplyVisualState(); }
- private void ApplyArtwork(TileSymbol symbol)
- {
- Sprite sprite = null;
- switch (symbol)
- {
- case TileSymbol.imp: sprite = impArt; break;
- case TileSymbol.trident: sprite = tridentArt; break;
- case TileSymbol.potion: sprite = potionArt; break;
- case TileSymbol.piggy: sprite = piggyArt; break;
- case TileSymbol.ring: sprite = ringArt; break;
- case TileSymbol.candle: sprite = candleArt; break;
- case TileSymbol.star: sprite = starArt; break;
- case TileSymbol.A: sprite = aArt; break;
- case TileSymbol.Q: sprite = qArt; break;
- case TileSymbol.J: sprite = jArt; break;
- case TileSymbol.T10: sprite = tenArt; break;
- case TileSymbol.N9: sprite = nineArt; break;
- }
- if (sprite && !artwork)
- {
- // Keep existing prefab references and cell geometry; art is only an inset visual.
- var rect = (RectTransform)new GameObject("Artwork", typeof(RectTransform)).transform;
- rect.SetParent(face.transform, false);
- rect.anchorMin = Vector2.zero; rect.anchorMax = Vector2.one;
- rect.offsetMin = Vector2.one; rect.offsetMax = -Vector2.one;
- artwork = rect.gameObject.AddComponent<Image>();
- artwork.type = Image.Type.Simple; artwork.preserveAspect = true;
- artwork.raycastTarget = false; artwork.color = Color.white;
- }
- if (artwork) { artwork.sprite = sprite; artwork.enabled = sprite != null; }
- // Keep the Text component and its value for existing BoardView/API compatibility.
- label.enabled = !sprite;
- }
- private void ApplyVisualState(bool animate = true)
- {
- stateTween?.Kill(); stateTween = null;
- if (!face || !border || !label) return;
- Color targetFace = selected ? new Color32(43, 40, 64, 255) : hinted ? new Color32(35, 30, 55, 255) : new Color32(24, 24, 40, 255);
- Color targetBorder = selected ? new Color32(225, 190, 100, 255) : hinted ? new Color32(52, 224, 255, 255) : new Color32(55, 56, 77, 255);
- Color targetText = selected || hinted ? Color.white : (Color)new Color32(220, 230, 247, 255);
- if (!animate || !Application.isPlaying || !isActiveAndEnabled || removing)
- { face.color = targetFace; border.color = targetBorder; label.color = targetText; return; }
- Color fromFace = face.color, fromBorder = border.color, fromText = label.color;
- float progress = 0;
- stateTween = DOTween.To(() => progress, value => {
- progress = value; face.color = Color.Lerp(fromFace, targetFace, value);
- border.color = Color.Lerp(fromBorder, targetBorder, value); label.color = Color.Lerp(fromText, targetText, value);
- }, 1, .12f).SetEase(Ease.OutQuad).SetUpdate(true);
- }
- public static string SymbolText(TileSymbol symbol)
- {
- switch (symbol)
- {
- case TileSymbol.imp: return "IMP"; case TileSymbol.trident: return "TRI";
- case TileSymbol.ring: return "RNG"; case TileSymbol.potion: return "POT";
- case TileSymbol.candle: return "CND"; case TileSymbol.star: return "STR";
- case TileSymbol.piggy: return "PIG"; case TileSymbol.A: return "A";
- case TileSymbol.Q: return "Q"; case TileSymbol.J: return "J";
- case TileSymbol.T10: return "10"; case TileSymbol.N9: return "9";
- default: throw new ArgumentOutOfRangeException(nameof(symbol));
- }
- }
- }
- }
|