Files
zeling_v2/Assets/_Game/Scripts/Editor/Modules/SkillModule.cs

186 lines
6.8 KiB
C#

using System;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
using BaseGames.Skills;
namespace BaseGames.Editor.Modules
{
/// <summary>
/// DataHub 技能模块 —— 管理 FormSkillSO 资产。
/// </summary>
public class SkillModule : IDataModule
{
private const string Folder = "Assets/_Game/Data/Skills";
private const string Prefix = "SKL_";
public string ModuleId => "skill";
public string DisplayName => "技能";
public string IconName => null;
private SoListPane<FormSkillSO> _listPane;
private DetailHeader _header;
private FormSkillSO _selected;
public void Initialize()
{
_listPane = new SoListPane<FormSkillSO>(
Folder, Prefix,
s => s.resourceType.ToString());
}
public void BuildListPane(VisualElement container, Action<UnityEngine.Object> onSelected)
{
_listPane.SelectionChanged = sel =>
{
_selected = sel;
onSelected?.Invoke(sel);
};
container.Add(_listPane);
_listPane.Refresh();
}
public void BuildDetailPane(VisualElement container, UnityEngine.Object selected)
{
_selected = selected as FormSkillSO;
_header = new DetailHeader();
_header.SetAsset(_selected);
_header.RenameRequested += OnRenameRequested;
container.Add(_header);
if (_selected == null) return;
// Stats Card
var card = BuildStatsCard(_selected);
container.Add(card);
// 操作按钮
container.Add(BuildActionBar(_selected));
container.Add(MakeDivider());
// Inspector
var insp = new InspectorElement(_selected); container.Add(insp);
}
public void OnActivated() => _listPane?.Refresh();
// ── 内部 ─────────────────────────────────────────────────────────────
private void OnRenameRequested(string newName)
{
if (_selected == null) return;
var (ok, err) = AssetOperations.Rename(_selected, newName);
if (!ok) EditorUtility.DisplayDialog("重命名失败", err, "确定");
else { _header.SetAsset(_selected); _listPane.Invalidate(); }
}
private static VisualElement BuildStatsCard(FormSkillSO s)
{
var card = MakeCard();
AddChip(card, "效果类型", s.effectType.ToString());
AddChip(card, "资源类型", s.resourceType.ToString());
AddChip(card, "冷却", $"{s.cooldown:F1}s");
AddChip(card, "消耗", s.baseCost.ToString());
if (!string.IsNullOrEmpty(s.skillId))
AddChip(card, "ID", s.skillId);
return card;
}
private VisualElement BuildActionBar(FormSkillSO s)
{
var bar = MakeActionBar();
new Button(() => { EditorGUIUtility.PingObject(s); Selection.activeObject = s; })
{ text = "定位" }.AlsoAddTo(bar);
new Button(() => { var c = AssetOperations.Clone(s, Folder); if (c != null) _listPane.Refresh(c); })
{ text = "克隆..." }.AlsoAddTo(bar);
var del = new Button(() => { if (AssetOperations.Delete(s)) _listPane.Refresh(null); }) { text = "删除" };
del.style.borderLeftColor = new StyleColor(new Color(0.8f, 0.3f, 0.3f, 0.6f));
del.style.borderRightColor = new StyleColor(new Color(0.8f, 0.3f, 0.3f, 0.6f));
del.style.borderTopColor = new StyleColor(new Color(0.8f, 0.3f, 0.3f, 0.6f));
del.style.borderBottomColor = new StyleColor(new Color(0.8f, 0.3f, 0.3f, 0.6f));
del.style.borderLeftWidth = 1;
del.style.borderRightWidth = 1;
del.style.borderTopWidth = 1;
del.style.borderBottomWidth = 1;
del.style.marginLeft = 8;
del.AlsoAddTo(bar);
return bar;
}
// ── 共享构建辅助 ─────────────────────────────────────────────────────
internal static VisualElement MakeCard()
{
var c = new VisualElement();
c.style.flexDirection = FlexDirection.Row;
c.style.flexWrap = Wrap.Wrap;
c.style.paddingLeft = 12;
c.style.paddingRight = 12;
c.style.paddingTop = 8;
c.style.paddingBottom = 8;
c.style.marginBottom = 4;
c.style.backgroundColor = new StyleColor(new Color(0.5f, 0.5f, 0.5f, 0.08f));
c.style.borderBottomWidth = 1;
c.style.borderBottomColor = new StyleColor(new Color(0.5f, 0.5f, 0.5f, 0.2f));
return c;
}
internal static void AddChip(VisualElement parent, string label, string value)
{
var chip = new VisualElement();
chip.style.flexDirection = FlexDirection.Row;
chip.style.alignItems = Align.Center;
chip.style.marginRight = 14;
chip.style.marginBottom = 2;
var l = new Label(label + ":");
l.style.opacity = 0.6f;
l.style.fontSize = 11;
l.style.marginRight = 3;
chip.Add(l);
var v = new Label(value);
v.style.fontSize = 11;
v.style.unityFontStyleAndWeight = UnityEngine.FontStyle.Bold;
chip.Add(v);
parent.Add(chip);
}
internal static VisualElement MakeActionBar()
{
var b = new VisualElement();
b.style.flexDirection = FlexDirection.Row;
b.style.paddingLeft = 12;
b.style.paddingRight = 12;
b.style.paddingTop = 6;
b.style.paddingBottom = 6;
b.style.flexWrap = Wrap.Wrap;
return b;
}
internal static VisualElement MakeDivider()
{
var d = new VisualElement();
d.style.height = 1;
d.style.backgroundColor = new StyleColor(new Color(0.5f, 0.5f, 0.5f, 0.2f));
return d;
}
}
// ── Button 扩展(模块内共用)─────────────────────────────────────────────
internal static class ButtonExtensions
{
public static Button AlsoAddTo(this Button btn, VisualElement parent)
{
parent.Add(btn);
return btn;
}
}
}