ThreeKingdomsSlingshot/Assets/Scripts/OutGame/Costume/CostumeWindow.cs

312 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace TKS.OutGame
{
/// <summary>
/// 외형(코스튬) 창(2026-11-20 개발요청_코스튬 §2 · 목업 영웅정보_img 04 · 05) — 영웅 화면 「외형」 버튼 · 무대(유닛 대기 + 착용 레이어 · 미리보기) ·
/// 부위 칸 4(외형 = 잠김 「추후」) · 고른 부위 꾸미기 칸(벗기 · 보유 ✓ · 미보유 자물쇠 + 얻는 곳 · 추가 예정) · 모두 벗기 · 적용(보유만) · 미보유 두 번 = 상세 창.
/// 미리보기 = 창을 닫으면 버림. 글자 = UI/Costume/costume_text · 값 = 표(C45).
/// </summary>
public static class CostumeWindow
{
public const string Texts = "UI/Costume/costume_text";
const float W = 940f;
const float L = 34f;
const float RW = 872f;
const int GridCols = 4;
const int GridRows = 2;
static string heroId;
static string slotSel;
static Dictionary<string, string> preview;
static string lastTapped;
/// <summary>시험 · 캡처 — 마지막 창(costume · detail) · 지금 미리보기.</summary>
public static string Last { get; private set; }
public static Dictionary<string, string> Preview
{
get { return preview; }
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void ResetStatics()
{
heroId = slotSel = lastTapped = Last = null;
preview = null;
}
static string T(string k)
{
return UiText.T(k).Replace("\\n", "\n");
}
static string F(string k, params string[] a)
{
return UiText.F(k, a).Replace("\\n", "\n");
}
static Text Txt(RectTransform p, string name, string s, int size, Color c, float x, float y, float w, float h, TextAnchor a = TextAnchor.MiddleLeft, bool title = false)
{
var t = UiKit.Label(p, s, size, c, a, new Vector2(0f, 1f), new Vector2(0f, 1f), new Vector2(x, -y), new Vector2(w, h), title);
t.name = name;
if (title) UiKit.Title(t, true);
t.horizontalOverflow = HorizontalWrapMode.Overflow;
return t;
}
static Image Box(RectTransform p, string name, float x, float y, float w, float h, Color c, bool raycast = false)
{
return UiKit.Skin(UiKit.At(p, name, x, y, w, h), "card", c, raycast);
}
static Button Btn(RectTransform p, string name, string skin, string label, int size, Color fg, float x, float y, float w, float h, Action onClick)
{
return UiKit.SkinBtn(p, name, UISkin.Has(skin) ? skin : "button", UiKit.White, label, size, fg, x, y, w, h, onClick);
}
static Color Grade(string rarity)
{
var db = CostumeView.Db;
return UiKit.Col("gear_" + (db != null ? db.RarityGrade(rarity) : "white") + "_bg", UiKit.Dark);
}
/// <summary>영웅 화면 「외형」 — 새로 열면 미리보기 = 지금 착용 · 빨간 점 끔.</summary>
public static void Open(OutGameRoot root, string id)
{
heroId = id;
preview = CostumeRules.Worn(CostumeView.Db, root.Session.Save.Hero(id));
lastTapped = null;
if (root.Session.Save.costumeNew)
{
root.Session.Save.costumeNew = false;
root.Session.Commit();
}
Draw(root);
}
static void Step(OutGameRoot root, int d)
{
Open(root, root.HeroDetail.StepHero(d));
}
static void Draw(OutGameRoot root)
{
UiText.Use(Texts);
var s = root.Session;
var db = CostumeView.Db;
var h = s.Def(heroId);
if (db == null || h == null || h.isSoldier || s.Save.Hero(heroId) == null)
{
root.Toast(T("cos_none"));
return;
}
if (slotSel == null || db.SlotOf(slotSel) == null || !db.SlotOf(slotSel).open) slotSel = CostumeRules.DrawOrder(db).Count > 0 ? db.slots[0].slot : null;
Last = "costume";
var content = root.ModalCustom(T("cos_title"), 1720f);
var panel = (RectTransform)content.parent;
foreach (var dd in new[] { -1, 1 })
{
int k = dd;
var b = Box(panel, dd < 0 ? "Prev" : "Next", W - (dd < 0 ? 300f : 216f), 26f, 72f, 72f, UiKit.Col("chip_dark", UiKit.Dark), true);
b.gameObject.AddComponent<Button>().onClick.AddListener(() => Step(root, k));
UiKit.Icon(b.rectTransform, dd < 0 ? "arrow_prev" : "arrow_next", new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), Vector2.zero, 46f);
}
// 무대 = 유닛 대기 + 착용 레이어(미리보기)
float y = 130f;
var stage = Box(panel, "Stage", L, y, RW, 760f, UiKit.Col("cos_stage", new Color(0.22f, 0.27f, 0.45f, 1f)));
Txt(stage.rectTransform, "Note", T("cos_note"), 24, UiKit.Col("text_soft", UiKit.InkSoft), RW - 520f, 16f, 500f, 70f, TextAnchor.UpperRight);
bool unowned = CostumeRules.CanApply(db, s.Save, preview) == "unowned";
if (unowned)
{
var pb = Box(stage.rectTransform, "PreviewBadge", 16f, 16f, 360f, 54f, UiKit.Col("cos_preview", new Color(0.55f, 0.45f, 0.12f, 1f)));
UiKit.Title(UiKit.FillLabel(pb.transform, T("cos_preview"), 24, UiKit.Col("gold", UiKit.Coin)), true);
}
var shadow = UiKit.At(stage.rectTransform, "Shadow", RW * 0.5f - 300f, 640f, 600f, 90f).gameObject.AddComponent<Image>();
shadow.sprite = UiKit.Circle; // 발밑 타원 그림자
shadow.color = new Color(0f, 0f, 0f, 0.25f);
shadow.raycastTarget = false;
var stand = UiKit.UnitStand("Hero", stage.rectTransform, h, new Vector2(0.5f, 0f), new Vector2(0f, 90f), 560f, 1f);
if (stand != null)
{
var img = stand.GetComponent<Image>();
UiFrameAnim.Attach(img, UnitArt.Load(h.art), "front_idle");
var uc = UiCostume.Attach(img, UnitArt.Load(h.art), heroId);
if (uc != null) uc.Override = preview;
}
y += 780f;
// 부위 칸 4
float cw = (RW - 3 * 16f) / 4f;
for (int i = 0; i < db.slots.Count && i < 4; i++)
{
var sl = db.slots[i];
float x = L + i * (cw + 16f);
bool sel = sl.slot == slotSel;
var c = Box(panel, "Slot_" + sl.slot, x, y, cw, 110f, sel ? UiKit.Col("cos_sel", new Color(0.24f, 0.36f, 0.86f, 1f)) : UiKit.Col("cos_cell", UiKit.Dark), true);
string sk = sl.slot;
c.gameObject.AddComponent<Button>().onClick.AddListener(() =>
{
if (!db.SlotOf(sk).open)
{
root.Toast(T("cos_later_toast"));
return;
}
slotSel = sk;
lastTapped = null;
Draw(root);
});
if (!sl.open)
{
c.color = new Color(c.color.r, c.color.g, c.color.b, 0.5f);
UiKit.Icon(c.rectTransform, "lock", new Vector2(0.5f, 1f), new Vector2(0.5f, 0.5f), new Vector2(0f, -38f), 52f);
Txt(c.rectTransform, "Name", F("cos_slot_later", sl.nameKo), 26, UiKit.Col("text_soft", UiKit.InkSoft), 0f, 66f, cw, 36f, TextAnchor.MiddleCenter, true);
continue;
}
string worn;
preview.TryGetValue(sl.slot, out worn);
var wit = worn != null ? db.ItemOf(worn) : null;
var art = CostumeRules.Art(wit);
if (art != null)
{
var ai = UiKit.At(c.rectTransform, "Art", cw * 0.5f - 30f, 8f, 60f, 60f).gameObject.AddComponent<Image>();
ai.sprite = art;
ai.preserveAspect = true;
ai.raycastTarget = false;
}
Txt(c.rectTransform, "Name", sl.nameKo, 28, UiKit.White, 0f, 66f, cw, 36f, TextAnchor.MiddleCenter, true);
}
y += 126f;
// 고른 부위 꾸미기 칸 = 벗기 · 꾸미기 · 추가 예정
var cells = new List<CostumeDb.Item> { null };
foreach (var it in db.items)
if (it.slot == slotSel) cells.Add(it);
float gh = 200f;
for (int i = 0; i < GridCols * GridRows; i++)
{
float x = L + (i % GridCols) * (cw + 16f), gy = y + (i / GridCols) * (gh + 16f);
if (i >= cells.Count)
{
var e = Box(panel, "Empty" + i, x, gy, cw, gh, UiKit.Col("cos_empty", new Color(0.18f, 0.2f, 0.3f, 0.6f)));
Txt(e.rectTransform, "Q", "?", 56, UiKit.Col("text_soft", UiKit.InkSoft), 0f, 40f, cw, 70f, TextAnchor.MiddleCenter, true);
Txt(e.rectTransform, "T", T("cos_more"), 24, UiKit.Col("text_soft", UiKit.InkSoft), 0f, 120f, cw, 40f, TextAnchor.MiddleCenter, true);
continue;
}
var it = cells[i];
string cur;
preview.TryGetValue(slotSel, out cur);
bool on = it == null ? string.IsNullOrEmpty(cur) : cur == it.id;
bool owned = it == null || CostumeRules.Owns(s.Save, it.id);
var cell = Box(panel, it == null ? "Off" : "Item_" + it.id, x, gy, cw, gh, it == null ? UiKit.Col("cos_cell", UiKit.Dark) : Grade(it.rarity) * new Color(0.55f, 0.55f, 0.6f, 1f), true);
if (on) UiKit.Border(cell, UiKit.Col("gold", UiKit.Coin), 5f);
var item = it;
cell.gameObject.AddComponent<Button>().onClick.AddListener(() => Tap(root, item));
if (it == null)
{
Txt(cell.rectTransform, "Dash", "—", 40, UiKit.Col("text_soft", UiKit.InkSoft), 0f, 50f, cw, 60f, TextAnchor.MiddleCenter, true);
Txt(cell.rectTransform, "T", T("cos_off"), 26, UiKit.White, 0f, 120f, cw, 40f, TextAnchor.MiddleCenter, true);
continue;
}
var ai = UiKit.At(cell.rectTransform, "Art", cw * 0.5f - 60f, 24f, 120f, 110f).gameObject.AddComponent<Image>();
ai.sprite = CostumeRules.Art(it);
ai.preserveAspect = true;
ai.raycastTarget = false;
if (!owned)
{
ai.color = new Color(0.5f, 0.5f, 0.5f, 0.8f);
UiKit.Icon(cell.rectTransform, "lock", new Vector2(0.5f, 1f), new Vector2(0.5f, 0.5f), new Vector2(0f, -60f), 56f);
Txt(cell.rectTransform, "Src", it.sourceKo, 20, UiKit.Col("gold", UiKit.Coin), 0f, 162f, cw, 30f, TextAnchor.MiddleCenter, true);
}
else if (on) UiKit.Icon(cell.rectTransform, "check", new Vector2(1f, 1f), new Vector2(0.5f, 0.5f), new Vector2(-30f, -30f), 44f);
Txt(cell.rectTransform, "Name", it.nameKo, 26, owned ? UiKit.White : UiKit.Col("text_soft", UiKit.InkSoft), 0f, owned ? 140f : 128f, cw, 36f, TextAnchor.MiddleCenter, true);
}
y += GridRows * (gh + 16f) + 8f;
Btn(panel, "OffAll", "btn_s_gray", T("cos_off_all"), 34, UiKit.White, L, y, 280f, 100f, () =>
{
preview = new Dictionary<string, string>();
Draw(root);
});
var why = CostumeRules.CanApply(db, s.Save, preview);
Btn(panel, "Apply", why == null ? "btn_l_yellow" : "btn_s_gray", why == null ? T("cos_apply") : T("cos_apply_lack"), 36, why == null ? UiKit.Col("text_on_yellow", UiKit.Ink) : UiKit.White, L + 300f, y, RW - 300f, 100f, () =>
{
var r = root.Session.CostumeApply(heroId, preview);
if (r != null)
{
root.Toast(T(r == "unowned" ? "cos_apply_lack" : "cos_fail"));
return;
}
root.HeroDetail.Refresh();
root.Toast(T("cos_applied"));
Draw(root);
});
y += 112f;
Txt(panel, "Foot", T("cos_foot"), 22, UiKit.Col("text_soft", UiKit.InkSoft), L, y, RW, 34f, TextAnchor.MiddleCenter);
}
/// <summary>칸 누름 = 바로 미리 입힘(미보유 포함) · 미보유를 두 번 = 상세 창.</summary>
static void Tap(OutGameRoot root, CostumeDb.Item it)
{
if (it == null)
{
preview[slotSel] = "";
lastTapped = null;
Draw(root);
return;
}
bool owned = CostumeRules.Owns(root.Session.Save, it.id);
if (!owned && lastTapped == it.id)
{
OpenDetail(root, it);
return;
}
preview[it.slot] = it.id;
lastTapped = it.id;
Draw(root);
}
/// <summary>미보유 꾸미기 상세(목업 05) — 그림 · 등급 · 부위 · 얻는 곳 · 닫기 / 얻으러 가기.</summary>
public static void OpenDetail(OutGameRoot root, CostumeDb.Item it)
{
UiText.Use(Texts);
var db = CostumeView.Db;
Last = "detail";
var content = root.ModalCustom(it.nameKo, 560f);
var panel = (RectTransform)content.parent;
var head = panel.Find("Head");
if (head != null && head.GetComponent<Image>() != null) head.GetComponent<Image>().color = Grade(it.rarity);
float y = 136f;
var ib = Box(panel, "Icon", L + 10f, y, 220f, 200f, UiKit.Col("cos_cell", UiKit.Dark));
ib.gameObject.AddComponent<RectMask2D>();
// 꾸미기 그림 = 유닛 그림 폭 캔버스(가운데 작은 그림) → 칸에서 크게(임시 그림 기준 2.5 배 · 칸 밖은 잘림)
var ai = UiKit.At(ib.rectTransform, "Art", 110f - 225f, 100f - 225f, 450f, 450f).gameObject.AddComponent<Image>();
ai.sprite = CostumeRules.Art(it);
ai.preserveAspect = true;
float tx = L + 260f;
var gk = db.RarityGrade(it.rarity);
Txt(panel, "Grade", F("cos_d_grade", "<color=#" + ColorUtility.ToHtmlStringRGB(Grade(it.rarity)) + ">" + UiKit.GearGradeKo(gk) + "</color>"), 30, UiKit.Col("text_soft", UiKit.InkSoft), tx, y + 4f, 600f, 44f, TextAnchor.MiddleLeft, true).supportRichText = true;
var sl = db.SlotOf(it.slot);
Txt(panel, "Part", F("cos_d_part", sl != null ? sl.nameKo : it.slot), 30, UiKit.White, tx, y + 52f, 600f, 44f, TextAnchor.MiddleLeft, true);
Txt(panel, "Src", F("cos_d_src", "<color=#" + ColorUtility.ToHtmlStringRGB(UiKit.Col("gold", UiKit.Coin)) + ">" + it.sourceKo + "</color>"), 30, UiKit.White, tx, y + 100f, 600f, 44f, TextAnchor.MiddleLeft, true).supportRichText = true;
Txt(panel, "Hint", T("cos_d_hint"), 24, UiKit.Col("text_soft", UiKit.InkSoft), tx, y + 150f, 600f, 40f);
y += 230f;
Btn(panel, "DetailClose", "btn_s_gray", T("cos_d_close"), 36, UiKit.White, L + 20f, y, 400f, 96f, () => Draw(root));
Btn(panel, "DetailGo", "btn_sky", T("cos_d_go"), 36, UiKit.White, W - L - 420f, y, 400f, 96f, () => GoSource(root, it));
}
/// <summary>얻으러 가기 = s_source 앞 말(mission = 임무 화면 · 그 밖 = 준비 중 안내 · 이벤트 = 준비 중).</summary>
static void GoSource(OutGameRoot root, CostumeDb.Item it)
{
string kind = (it.source ?? "").Split(':')[0];
if (kind == "mission" && root.Missions != null)
{
root.CloseModal();
root.Show(root.Missions);
return;
}
root.Toast(F("cos_d_soon", it.sourceKo));
}
}
}