175 lines
5.5 KiB
C#
175 lines
5.5 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Collections.Generic;
|
|
using AlicizaX.Editor;
|
|
using UnityEditor.IMGUI.Controls;
|
|
using UnityEngine;
|
|
using UnityEditor;
|
|
|
|
namespace AlicizaX.Localization.Editor
|
|
{
|
|
public class LanguagesTreeView : TreeView
|
|
{
|
|
private const string k_DeleteCommand = "Delete";
|
|
private const string k_SoftDeleteCommand = "SoftDelete";
|
|
private const string k_NewLanguage = "New Language";
|
|
|
|
public Action<LocalizationTableWindow.WindowSelection> OnLanguageSelect;
|
|
|
|
private readonly LocalizationWindowData windowData;
|
|
private readonly GameLocaizationTable _table;
|
|
|
|
internal class LanguageTreeViewItem : TreeViewItem
|
|
{
|
|
public TempLanguageData language;
|
|
|
|
public LanguageTreeViewItem(int id, int depth, TempLanguageData language) : base(id, depth, language.Entry.LanguageName)
|
|
{
|
|
this.language = language;
|
|
}
|
|
}
|
|
|
|
public LanguagesTreeView(TreeViewState state, LocalizationWindowData data, GameLocaizationTable table) : base(state)
|
|
{
|
|
windowData = data;
|
|
rowHeight = 20f;
|
|
_table = table;
|
|
Reload();
|
|
}
|
|
|
|
|
|
protected override TreeViewItem BuildRoot()
|
|
{
|
|
var root = new TreeViewItem { id = 0, depth = -1, displayName = "Languages" };
|
|
int id = 1;
|
|
|
|
foreach (var lang in windowData.Languages)
|
|
{
|
|
root.AddChild(new LanguageTreeViewItem(id++, 1, lang));
|
|
}
|
|
|
|
if (root.children == null)
|
|
root.children = new List<TreeViewItem>();
|
|
|
|
return root;
|
|
}
|
|
|
|
protected override void RowGUI(RowGUIArgs args)
|
|
{
|
|
var item = args.item;
|
|
var rect = args.rowRect;
|
|
|
|
GUIContent labelIcon = EditorGUIUtility.TrTextContentWithIcon(" " + item.displayName, "BuildSettings.Web.Small");
|
|
Rect labelRect = new(rect.x + 2f, rect.y, rect.width - 2f, rect.height);
|
|
EditorGUI.LabelField(labelRect, labelIcon);
|
|
}
|
|
|
|
public override void OnGUI(Rect rect)
|
|
{
|
|
EditorDrawing.DrawHeaderWithBorder(ref rect, new GUIContent("LANGUAGES"), 20f, false);
|
|
base.OnGUI(rect);
|
|
}
|
|
|
|
private void OnAddNewLanguage()
|
|
{
|
|
windowData.AddLanguage(k_NewLanguage, null);
|
|
}
|
|
|
|
protected override bool CanRename(TreeViewItem item) => false;
|
|
protected override bool CanMultiSelect(TreeViewItem item) => true;
|
|
|
|
protected override void RenameEnded(RenameEndedArgs args)
|
|
{
|
|
if (!args.acceptedRename)
|
|
return;
|
|
|
|
var renamedItem = FindItem(args.itemID, rootItem);
|
|
if (renamedItem == null) return;
|
|
|
|
renamedItem.displayName = args.newName;
|
|
if (renamedItem is LanguageTreeViewItem item)
|
|
item.language.Entry.LanguageName = args.newName;
|
|
}
|
|
|
|
protected override void SingleClickedItem(int id)
|
|
{
|
|
var selectedItem = FindItem(id, rootItem);
|
|
if (selectedItem != null)
|
|
{
|
|
if (selectedItem is LanguageTreeViewItem item)
|
|
{
|
|
OnLanguageSelect?.Invoke(new LocalizationTableWindow.LanguageSelect()
|
|
{
|
|
Language = item.language,
|
|
TreeViewItem = item
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override void SelectionChanged(IList<int> selectedIds)
|
|
{
|
|
if (selectedIds.Count > 1)
|
|
OnLanguageSelect?.Invoke(null);
|
|
}
|
|
|
|
protected override bool CanStartDrag(CanStartDragArgs args)
|
|
{
|
|
var firstItem = FindItem(args.draggedItemIDs[0], rootItem);
|
|
return args.draggedItemIDs.All(id => FindItem(id, rootItem).parent == firstItem.parent);
|
|
}
|
|
|
|
protected override void SetupDragAndDrop(SetupDragAndDropArgs args)
|
|
{
|
|
DragAndDrop.PrepareStartDrag();
|
|
DragAndDrop.SetGenericData("IDs", args.draggedItemIDs.ToArray());
|
|
DragAndDrop.SetGenericData("Type", "Languages");
|
|
DragAndDrop.StartDrag("Languages");
|
|
}
|
|
|
|
|
|
private void HandleCommandEvent(Event uiEvent)
|
|
{
|
|
if (uiEvent.type == EventType.ValidateCommand)
|
|
{
|
|
switch (uiEvent.commandName)
|
|
{
|
|
case k_DeleteCommand:
|
|
case k_SoftDeleteCommand:
|
|
if (HasSelection())
|
|
uiEvent.Use();
|
|
break;
|
|
}
|
|
}
|
|
else if (uiEvent.type == EventType.ExecuteCommand)
|
|
{
|
|
switch (uiEvent.commandName)
|
|
{
|
|
case k_DeleteCommand:
|
|
case k_SoftDeleteCommand:
|
|
DeleteSelected();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DeleteSelected()
|
|
{
|
|
if (!HasFocus())
|
|
return;
|
|
//
|
|
// var toDelete = GetSelection().OrderByDescending(i => i);
|
|
// if (toDelete.Count() <= 0) return;
|
|
//
|
|
// foreach (var index in toDelete)
|
|
// {
|
|
// windowData.Languages.RemoveAt(index - 1);
|
|
// }
|
|
//
|
|
// OnLanguageSelect?.Invoke(null);
|
|
// SetSelection(new int[0]);
|
|
// Reload();
|
|
}
|
|
}
|
|
}
|