Update LocalizationTableWindow.cs

This commit is contained in:
陈思海 2026-04-17 15:34:48 +08:00
parent a9dc53e068
commit 9d75547998

View File

@ -11,6 +11,13 @@ namespace AlicizaX.Localization.Editor
{ {
public class LocalizationTableWindow : EditorWindow public class LocalizationTableWindow : EditorWindow
{ {
private enum SearchTarget
{
Key,
Value
}
private static readonly string[] SearchTargetOptions = { "Key", "Value" };
private List<GameLocaizationTable> allTables = new List<GameLocaizationTable>(); // 存储所有找到的GameLocaizationTable private List<GameLocaizationTable> allTables = new List<GameLocaizationTable>(); // 存储所有找到的GameLocaizationTable
private string[] tableDisplayNames; // 用于下拉框显示的名称数组 private string[] tableDisplayNames; // 用于下拉框显示的名称数组
private int selectedTableIndex = 0; // 当前选中的索引 private int selectedTableIndex = 0; // 当前选中的索引
@ -59,6 +66,7 @@ namespace AlicizaX.Localization.Editor
private SearchField searchField; private SearchField searchField;
private string searchString; private string searchString;
[SerializeField] private SearchTarget searchTarget = SearchTarget.Key;
private Vector2 scrollPosition; private Vector2 scrollPosition;
[SerializeField] private TreeViewState languagesTreeViewState; [SerializeField] private TreeViewState languagesTreeViewState;
@ -72,7 +80,7 @@ namespace AlicizaX.Localization.Editor
private void CreateGUI() private void CreateGUI()
{ {
searchField = new SearchField(); EnsureSearchField();
} }
private void OnDestroy() private void OnDestroy()
@ -82,9 +90,18 @@ namespace AlicizaX.Localization.Editor
private void OnEnable() private void OnEnable()
{ {
EnsureSearchField();
RefreshTableList(); RefreshTableList();
} }
private void EnsureSearchField()
{
if (searchField == null)
{
searchField = new SearchField();
}
}
private void OnDisable() private void OnDisable()
{ {
SaveSelection(); SaveSelection();
@ -191,12 +208,14 @@ namespace AlicizaX.Localization.Editor
private void OnGUI() private void OnGUI()
{ {
EnsureSearchField();
// 顶部工具栏 // 顶部工具栏
Rect toolbarRect = new(0, 0, position.width, 20f); Rect toolbarRect = new(0, 0, position.width, 20f);
GUI.Box(toolbarRect, GUIContent.none, EditorStyles.toolbar); GUI.Box(toolbarRect, GUIContent.none, EditorStyles.toolbar);
float buttonWidth = 100f; float buttonWidth = 100f;
float spacing = 5f; float spacing = 5f;
float searchTypeWidth = 65f;
Rect leftTitle = new(toolbarRect.xMin, 0, 40, 20f); Rect leftTitle = new(toolbarRect.xMin, 0, 40, 20f);
Rect leftPop = new(leftTitle.xMin + 40, 0, 200, 20f); Rect leftPop = new(leftTitle.xMin + 40, 0, 200, 20f);
@ -204,6 +223,8 @@ namespace AlicizaX.Localization.Editor
Rect genBtn = new(saveBtn.xMin - buttonWidth - spacing, 0, buttonWidth, 20f); Rect genBtn = new(saveBtn.xMin - buttonWidth - spacing, 0, buttonWidth, 20f);
Rect importBtn = new(genBtn.xMin - buttonWidth - spacing, 0, buttonWidth, 20f); Rect importBtn = new(genBtn.xMin - buttonWidth - spacing, 0, buttonWidth, 20f);
Rect exportBtn = new(importBtn.xMin - buttonWidth - spacing, 0, buttonWidth, 20f); Rect exportBtn = new(importBtn.xMin - buttonWidth - spacing, 0, buttonWidth, 20f);
Rect searchTypePopup = new(leftPop.xMax + spacing, 0, searchTypeWidth, 20f);
Rect searchRect = new(searchTypePopup.xMax + spacing, 0, Mathf.Max(80f, exportBtn.xMin - spacing - (searchTypePopup.xMax + spacing)), 20f);
EditorGUI.LabelField(leftTitle, "Table", EditorStyles.boldLabel); EditorGUI.LabelField(leftTitle, "Table", EditorStyles.boldLabel);
EditorDrawing.DrawStringSelectPopup(leftPop, tableDisplayNames, selectString, (e) => EditorDrawing.DrawStringSelectPopup(leftPop, tableDisplayNames, selectString, (e) =>
@ -213,6 +234,12 @@ namespace AlicizaX.Localization.Editor
UpdateCurrentTable(); UpdateCurrentTable();
}); });
using (new EditorGUI.DisabledGroupScope(currentTable == null || !(selection is LanguageSelect)))
{
searchTarget = (SearchTarget)EditorGUI.Popup(searchTypePopup, (int)searchTarget, SearchTargetOptions, EditorStyles.toolbarPopup);
searchString = searchField.OnToolbarGUI(searchRect, searchString);
}
if (currentTable == null) return; if (currentTable == null) return;
if (GUI.Button(exportBtn, "Export CSV", EditorStyles.toolbarButton)) if (GUI.Button(exportBtn, "Export CSV", EditorStyles.toolbarButton))
@ -402,27 +429,22 @@ namespace AlicizaX.Localization.Editor
{ {
var language = selection.Language; var language = selection.Language;
var entry = language.Entry; var entry = language.Entry;
var treeView = selection.TreeViewItem;
using (new EditorGUI.DisabledGroupScope(entry.Asset == null)) using (new EditorGUI.DisabledGroupScope(entry.Asset == null))
{ {
// Draw search field
EditorGUILayout.Space(); EditorGUILayout.Space();
GUIContent expandText = new GUIContent("Expand"); GUIContent expandText = new GUIContent("Expand");
float expandWidth = miniLabelButton.CalcSize(expandText).x; float expandWidth = miniLabelButton.CalcSize(expandText).x;
var searchRect = EditorGUILayout.GetControlRect(); using (new EditorGUILayout.HorizontalScope())
searchRect.xMax -= (expandWidth + 2f); {
searchString = searchField.OnGUI(searchRect, searchString); GUILayout.FlexibleSpace();
Rect expandRect = new Rect(searchRect.xMax + 2f, searchRect.y, expandWidth, searchRect.height);
expandRect.y -= 1f;
using (new EditorDrawing.BackgroundColorScope("#F7E987")) using (new EditorDrawing.BackgroundColorScope("#F7E987"))
{ {
if (GUI.Button(expandRect, expandText, miniLabelButton)) if (GUILayout.Button(expandText, miniLabelButton, GUILayout.Width(expandWidth)))
{ {
globalExpanded = !globalExpanded; globalExpanded = !globalExpanded;
foreach (var section in language.TableSheet) foreach (var section in language.TableSheet)
@ -431,15 +453,18 @@ namespace AlicizaX.Localization.Editor
} }
} }
} }
}
if (entry.Asset != null) if (entry.Asset != null)
{ {
bool forceExpanded = !string.IsNullOrWhiteSpace(searchString);
// Draw localization data // Draw localization data
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
{ {
foreach (var section in GetSearchResult(language, searchString)) foreach (var section in GetSearchResult(language, searchString, searchTarget))
{ {
DrawLocalizationKey(section); DrawLocalizationKey(section, forceExpanded);
} }
} }
EditorGUILayout.EndScrollView(); EditorGUILayout.EndScrollView();
@ -451,7 +476,7 @@ namespace AlicizaX.Localization.Editor
} }
} }
private void DrawLocalizationKey(TempSheetSection section) private void DrawLocalizationKey(TempSheetSection section, bool forceExpanded = false)
{ {
if (section.Items == null || section.Items.Count == 0) if (section.Items == null || section.Items.Count == 0)
return; return;
@ -459,15 +484,20 @@ namespace AlicizaX.Localization.Editor
using (new EditorDrawing.BorderBoxScope(false)) using (new EditorDrawing.BorderBoxScope(false))
{ {
string sectionName = section.Name.Replace(" ", ""); string sectionName = section.Name.Replace(" ", "");
section.Reference.IsExpanded = EditorGUILayout.Foldout(section.Reference.IsExpanded, new GUIContent(sectionName), true, EditorDrawing.Styles.miniBoldLabelFoldout); bool isExpanded = forceExpanded || section.Reference.IsExpanded;
bool nextExpanded = EditorGUILayout.Foldout(isExpanded, new GUIContent(sectionName), true, EditorDrawing.Styles.miniBoldLabelFoldout);
if (!forceExpanded)
{
section.Reference.IsExpanded = nextExpanded;
}
// Show section keys when expanded // Show section keys when expanded
if (section.Reference.IsExpanded) if (forceExpanded || nextExpanded)
{ {
foreach (var item in section.Items) foreach (var item in section.Items)
{ {
string keyName = item.Key.Replace(" ", ""); string key = GetItemDisplayKey(section, item);
string key = sectionName + "." + keyName;
if (IsMultiline(item.Value)) if (IsMultiline(item.Value))
key += " (Multiline)"; key += " (Multiline)";
@ -503,32 +533,33 @@ namespace AlicizaX.Localization.Editor
EditorGUILayout.Space(1f); EditorGUILayout.Space(1f);
} }
private IEnumerable<TempSheetSection> GetSearchResult(TempLanguageData languageData, string search) private IEnumerable<TempSheetSection> GetSearchResult(TempLanguageData languageData, string search, SearchTarget target)
{ {
if (!string.IsNullOrEmpty(search)) if (!string.IsNullOrWhiteSpace(search))
{ {
List<TempSheetSection> searchResult = new(); List<TempSheetSection> searchResult = new();
foreach (var section in languageData.TableSheet) foreach (var section in languageData.TableSheet)
{ {
List<TempSheetItem> sectionItems = new(); List<TempSheetItem> sectionItems = new();
string sectionName = section.Name.Replace(" ", "");
foreach (var item in section.Items) foreach (var item in section.Items)
{ {
string keyName = item.Key.Replace(" ", ""); string source = target == SearchTarget.Value ? item.Value : GetItemDisplayKey(section, item);
string key = sectionName + "." + keyName;
if (key.Contains(search)) if (ContainsSearch(source, search))
sectionItems.Add(item); sectionItems.Add(item);
} }
if (sectionItems.Count > 0)
{
searchResult.Add(new TempSheetSection() searchResult.Add(new TempSheetSection()
{ {
Items = sectionItems, Items = sectionItems,
Reference = section.Reference Reference = section.Reference
}); });
} }
}
return searchResult; return searchResult;
} }
@ -538,7 +569,19 @@ namespace AlicizaX.Localization.Editor
private bool IsMultiline(string text) private bool IsMultiline(string text)
{ {
return text.Contains("\n") || text.Contains("\r"); return !string.IsNullOrEmpty(text) && (text.Contains("\n") || text.Contains("\r"));
}
private string GetItemDisplayKey(TempSheetSection section, TempSheetItem item)
{
string sectionName = section.Name.Replace(" ", "");
string keyName = item.Key.Replace(" ", "");
return sectionName + "." + keyName;
}
private bool ContainsSearch(string source, string search)
{
return !string.IsNullOrEmpty(source) && source.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0;
} }
private void BuildLocalizationTable() private void BuildLocalizationTable()