using System; using TMPro; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; namespace Fateforge { public sealed class CardView : MonoBehaviour,IPointerClickHandler { public Image symbol,frame,sticker; public UiGlyph outline; public TMP_Text value,mark; public int CardId {get;private set;} public Action Click; public RectTransform Rect => (RectTransform)transform; public Vector2 Target {get;set;} float lastTap=-10,spawnAge=1; public void Bind(Cell cell,Game game,ArtLibrary art,bool animate=false) { CardId=cell.id;symbol.sprite=art.Sprite(cell.symbol);value.text=English.Number(game.CardValue(cell));string tag=game.Sticker(cell);sticker.sprite=art.Sprite(tag);sticker.enabled=sticker.sprite!=null; bool fresh=game.Board.operation=="discard"&&!game.Board.eligible.Contains(cell.id);mark.text=fresh?"NEW":game.Board.selected>=0&&game.Board.cells[game.Board.selected].id==cell.id?"+":""; if(animate)spawnAge=0; } UiGlyph selectedCorners; public bool DiscardFlash {get;private set;} public void ShowDiscardPick(UiTheme theme) { DiscardFlash=true; var matches=GetComponent();if(matches)matches.ResetVisuals(); SetSelected(true,theme); } public void SetSelected(bool active,UiTheme theme) { if(active && !selectedCorners) { selectedCorners=Ui.Glyph(transform,"SelectedCard",UiGlyph.Shape.SelectionCorners,0,0,1,1,theme.gold); Ui.Stretch(selectedCorners.rectTransform); } if(selectedCorners){selectedCorners.gameObject.SetActive(active);if(active)selectedCorners.transform.SetAsLastSibling();} if(active){if(outline)outline.color=theme.gold;frame.color=new Color(1,.76f,.27f,.18f);mark.text="";} } public void Highlight(Color color,float alpha=1){frame.color=new Color(color.r,color.g,color.b,alpha*.08f);if(outline)outline.color=new Color(color.r,color.g,color.b,Mathf.Max(.18f,alpha));} public void OnPointerClick(PointerEventData eventData){bool twice=eventData.clickCount>=2||Time.unscaledTime-lastTap<.3f;lastTap=Time.unscaledTime;Click?.Invoke(CardId,twice);} void Update(){if(spawnAge<1){spawnAge+=Time.unscaledDeltaTime*5;float t=Mathf.Clamp01(spawnAge);float scale=1+Mathf.Sin(t*Mathf.PI)*.13f;transform.localScale=Vector3.one*Mathf.Lerp(.55f,scale,Mathf.Min(1,t*4));}else transform.localScale=Vector3.one;} } }