ThreeKingdomsSlingshot/Assets/Scripts/OutGame/UI/HeroIcons.cs

226 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
using UnityEngine.UI;
namespace TKS.OutGame
{
/// <summary>
/// 영웅 아이콘 2종(PD 2026-11-26 「영웅 조각 = 직소 퍼즐 형태 · 완제품 영웅 아이콘은 조각과 차이가 있도록」 · 목업 선택 = 조각 SB안 · 완제품 HB안).
/// ① 조각(Shard) = 보상 칸(UiKit.ItemSlot · 색 = 태생 ★) 안에 얼굴 가득 + 칸 테두리를 얼굴 위에 다시 + 왼쪽 위 금빛 직소 배지(hi_shard_badge · 왼쪽 홈 없음) + 수량 칩.
/// ② 완제품(Badge) = 팔각 금테(hi_oct_frame) · ★색 바탕(hi_oct_fill × card_star&lt;n&gt;_bg + hi_oct_light × card_star&lt;n&gt;_glow) · 팔각으로 잘린 얼굴 · 왼쪽 위 진영 · 아래 별 받침.
/// 얼굴 = 영웅 그림 f000 의 머리 · 어깨 정사각(표 UI/HeroIcon/hero_icon_crop · Tools/hero_icon_crop.py) · 배치 값 = 표 UI/HeroIcon/hero_icon(C45).
/// 보상 줄에서는 아이콘 id 「hshard:&lt;영웅 id&gt;」 로 부른다(UiKit.RewardTile · UiKit.Icon 이 알아봄 · GoodsUi.Icon 이 영웅 조각에 붙임).
/// </summary>
public static class HeroIcons
{
public const string Table = "UI/HeroIcon/hero_icon";
public const string ShardPrefix = "hshard:";
static Dictionary<string, string> cfg;
static Dictionary<string, float[]> crops;
static HeroDb heroDb;
static string Cfg(string key)
{
if (cfg == null)
{
cfg = new Dictionary<string, string>();
var rows = UiText.Rows(Table);
if (rows == null) Debug.LogWarning("[TKS] 영웅 아이콘 표 없음: " + Table);
else
foreach (var r in rows)
{
string k, v;
if (r.TryGetValue("key", out k) && k.Length > 0 && r.TryGetValue("value", out v)) cfg[k] = v;
}
}
string s;
return cfg.TryGetValue(key, out s) ? s : "";
}
/// <summary>표 값(기준 칸 px) × 지금 크기 ÷ 기준 칸.</summary>
static float V(string key, float size)
{
float v, refSize;
if (!float.TryParse(Cfg(key), NumberStyles.Float, CultureInfo.InvariantCulture, out v)) return 0f;
if (!float.TryParse(Cfg("ref"), NumberStyles.Float, CultureInfo.InvariantCulture, out refSize) || refSize <= 0f) refSize = 132f;
return v * size / refSize;
}
/// <summary>표 값 그대로(비율 등 크기와 무관한 값 · 없으면 d).</summary>
static float R(string key, float d)
{
float v;
return float.TryParse(Cfg(key), NumberStyles.Float, CultureInfo.InvariantCulture, out v) ? v : d;
}
/// <summary>얼굴 자리(그림 px · x · y · side · w · h) — 없으면 null(그림 위쪽 가운데로 대체).</summary>
public static float[] CropOf(string art)
{
if (crops == null)
{
crops = new Dictionary<string, float[]>();
var rows = UiText.Rows(Cfg("crop_table"));
if (rows == null) Debug.LogWarning("[TKS] 영웅 얼굴 자리 표 없음: " + Cfg("crop_table"));
else
foreach (var r in rows)
{
string a;
if (!r.TryGetValue("art", out a) || a.Length == 0) continue;
var f = new float[5];
string[] keys = { "x", "y", "side", "w", "h" };
bool ok = true;
for (int i = 0; i < keys.Length && ok; i++)
{
string s;
ok = r.TryGetValue(keys[i], out s) && float.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out f[i]);
}
if (ok) crops[a] = f;
}
}
float[] c;
return art != null && crops.TryGetValue(art, out c) ? c : null;
}
public static HeroDb.Hero Hero(string id)
{
if (heroDb == null) heroDb = Resources.Load<HeroDb>("OutGame/HeroDb");
return heroDb != null ? heroDb.Get(id) : null;
}
/// <summary>보상 아이콘 id 가 영웅 조각이면 영웅(아니면 null).</summary>
public static HeroDb.Hero ShardHero(string icon)
{
return !string.IsNullOrEmpty(icon) && icon.StartsWith(ShardPrefix) ? Hero(icon.Substring(ShardPrefix.Length)) : null;
}
/// <summary>★ → 보상 칸 색 묶음(영웅 카드 = 1 녹 · 2 파랑 · 3 보라 · 4 주황 · 5 붉음 · PD 2026-11-19).</summary>
public static string SlotPrefix(int star)
{
switch (Mathf.Clamp(star, 1, 5))
{
case 1: return "gear_green_";
case 2: return "gear_blue_";
case 3: return "gear_purple_";
case 4: return "gear_orange_";
}
return "grade_red_";
}
/// <summary>얼굴(box 를 꽉 채우는 머리 · 어깨 · box 에 RectMask2D 또는 Mask 가 있어야 밖이 잘림).</summary>
public static Image Face(RectTransform box, HeroDb.Hero h, Vector2 boxSize)
{
var sp = h != null ? UiKit.ArtSprite(h.art) : null;
if (sp == null) return null;
var rt = UiKit.Rect("Face", box, new Vector2(0f, 1f), new Vector2(0f, 1f), new Vector2(0f, 0f), Vector2.zero);
rt.pivot = new Vector2(0f, 1f);
var img = rt.gameObject.AddComponent<Image>();
img.sprite = sp;
img.raycastTarget = false;
var c = CropOf(h.art);
float sw = sp.rect.width, sh = sp.rect.height;
float x, y, side;
if (c != null)
{
float k = c[3] > 0f ? sw / c[3] : 1f; // 표 그림 크기 ≠ 스프라이트 크기면 비율로
x = c[0] * k;
y = c[1] * k;
side = c[2] * k;
}
else
{
side = sw * 0.75f;
x = (sw - side) * 0.5f;
y = 0f;
}
float s = boxSize.x / Mathf.Max(1f, side);
rt.sizeDelta = new Vector2(sw * s, sh * s);
rt.anchoredPosition = new Vector2(-x * s, y * s);
return img;
}
/// <summary>
/// ① 영웅 조각(SB안) — cell(정사각 · 칸 크기 size) 안에 보상 칸 + 얼굴 + 칸 테두리 + 직소 배지 + 수량(qty 비면 없음).
/// 칸 색 = 태생 ★(조각은 영웅 종류의 것 · 보유 ★ 와 무관).
/// </summary>
public static void Shard(RectTransform cell, HeroDb.Hero h, float size, string qty, bool lit = true)
{
int star = h != null ? Mathf.Max(1, h.rarity) : 1;
UiKit.ItemSlot(cell, SlotPrefix(star), lit);
// 얼굴 = 칸 바탕(item_bg · 9 분할 둥근 모서리)과 같은 모양으로 자름 — 모서리 반경은 칸 크기와 무관하게 같아서 네모 자르기는 작은 칸(90 px)에서 모서리 밖으로 나옴(QA r2 B)
float clip = V("shard_clip", size), inset = V("shard_inset", size);
var cm = UiKit.Fill("FaceClip", cell, clip, clip, clip, clip);
UiKit.Skin(cm, "item_bg", Color.white);
cm.gameObject.AddComponent<Mask>().showMaskGraphic = false;
float d = inset - clip;
var box = UiKit.Fill("FaceBox", cm, d, d, d, d);
Face(box, h, new Vector2(size - inset * 2f, size - inset * 2f));
UiKit.Layer(cell, "Border2", "item_border", UiKit.Col("gear_border", UiKit.Line)); // 얼굴 위에 칸 테두리(모서리 덮기)
float bs = V("shard_badge", size), bo = V("shard_badge_off", size);
var badge = UiKit.Icon(cell, "hi_shard_badge", new Vector2(0f, 1f), new Vector2(0f, 1f), new Vector2(bo, -bo), bs);
if (badge != null) badge.raycastTarget = false;
if (!string.IsNullOrEmpty(qty)) UiKit.QtyChip(cell, qty, size >= 120f ? 30 : 24);
}
/// <summary>
/// ② 완제품 영웅(HB안) — 왼쪽 위 (x, y) 정사각 size · star = 보유 ★(없으면 태생 ★) · 팔각 금테 + ★색 바탕 + 얼굴 + 진영 + 별 받침.
/// </summary>
public static RectTransform Badge(Transform p, string name, float x, float y, float size, HeroDb.Hero h, int star)
{
if (star <= 0) star = h != null ? Mathf.Max(1, h.rarity) : 1;
star = Mathf.Clamp(star, 1, 5);
var root = UiKit.At(p, name, x, y, size, size);
var fill = UiKit.Fill("Fill", root);
var fi = fill.gameObject.AddComponent<Image>();
fi.sprite = UISkin.Icon("hi_oct_fill");
fi.color = UiKit.Col("card_star" + star + "_bg", UiKit.Purple);
fi.raycastTarget = false;
var light = UiKit.Fill("Light", root).gameObject.AddComponent<Image>();
light.sprite = UISkin.Icon("hi_oct_light");
light.color = UiKit.Col("card_star" + star + "_glow", Color.white);
light.raycastTarget = false;
// 얼굴 = 팔각 마스크(hi_oct_fill 모양 · 마스크 그림은 그리지 않음)
float inset = V("oct_inset", size);
var mrt = UiKit.Fill("FaceMask", root);
var mimg = mrt.gameObject.AddComponent<Image>();
mimg.sprite = UISkin.Icon("hi_oct_fill");
mimg.raycastTarget = false;
mrt.gameObject.AddComponent<Mask>().showMaskGraphic = false;
var box = UiKit.Fill("FaceBox", mrt, inset, inset, inset, inset);
Face(box, h, new Vector2(size - inset * 2f, size - inset * 2f));
var frame = UiKit.Fill("Frame", root).gameObject.AddComponent<Image>();
frame.sprite = UISkin.Icon("hi_oct_frame");
frame.raycastTarget = false;
// 왼쪽 위 진영
float fd = V("oct_faction", size), fo = V("oct_faction_off", size);
if (h != null) UiKit.DropShadow(UiKit.FactionBadge(root, h.faction, new Vector2(0f, 1f), new Vector2(0f, 1f), new Vector2(fo, -fo), fd), 2f);
// 오른쪽 위 병종(PD 2026-11-26 「영웅 아이콘 우측 상단에 병종 아이콘도 추가」) — 진영 배지와 같은 원 틀(짙은 바탕) + 영웅 카드와 같은 병종 그림(TypeSymbolId)
float cd = V("oct_class", size), co = V("oct_class_off", size);
if (h != null)
{
var ring = UiKit.Skin(UiKit.Rect("Class", root, new Vector2(1f, 1f), new Vector2(1f, 1f), new Vector2(-co, -co), new Vector2(cd, cd)), "circle_frame", UiKit.Col("chip_dark", UiKit.Dark));
ring.preserveAspect = true;
var inner = UiKit.Rect("In", ring.transform, new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), Vector2.zero, new Vector2(cd * 0.72f, cd * 0.72f)).gameObject.AddComponent<Image>();
inner.sprite = UiKit.Circle;
inner.color = UiKit.Col("chip_dark", UiKit.Dark);
inner.raycastTarget = false;
var sym = UiKit.Icon(ring.transform, UiKit.TypeSymbolId(h.type), new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), Vector2.zero, cd * R("oct_class_icon", 0.76f));
if (sym != null) sym.raycastTarget = false;
else UiKit.Title(UiKit.FillLabel(ring.transform, UnitTypeInfo.Mark(h.type), Mathf.RoundToInt(cd * 0.48f), UiKit.White), true);
UiKit.DropShadow(ring, 2f);
}
// 아래 별 받침
float ss = V("oct_star", size), gap = V("oct_star_gap", size), bh = V("oct_band_h", size);
float bw = star * ss + (star - 1) * gap + bh * 0.5f;
var band = UiKit.Rect("Stars", root, new Vector2(0.5f, 0f), new Vector2(0.5f, 1f), new Vector2(0f, V("oct_band_y", size)), new Vector2(bw, bh));
UiKit.Skin(band, "chip", UiKit.Col("chip_dark", UiKit.Dark));
for (int i = 0; i < star; i++)
{
var st = UiKit.Icon(band, "star_on", new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2((i - (star - 1) * 0.5f) * (ss + gap), 1f), ss);
if (st != null) st.raycastTarget = false;
}
return root;
}
}
}