DragonECS-Unity/src/DragonDocs/Editors/DragonDocsPrefs.cs

156 lines
4.9 KiB
C#
Raw Normal View History

2024-06-10 18:05:06 +08:00
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace DCFApixels.DragonECS.Unity.Docs.Editors
{
[FilePath(EcsConsts.AUTHOR + "/" + EcsConsts.FRAMEWORK_NAME + "/" + nameof(DragonDocsPrefs) + ".prefs", FilePathAttribute.Location.ProjectFolder)]
internal class DragonDocsPrefs : ScriptableSingleton<DragonDocsPrefs>
{
2024-06-13 18:04:47 +08:00
[SerializeField] private DragonDocs m_docs;
[SerializeField] private bool[] m_isExpands;
2024-06-10 18:05:06 +08:00
[NonSerialized] private bool _isInitInfos = false;
[NonSerialized] private MetaGroupInfo[] _infos = null;
public DragonDocs Docs
{
2024-06-13 18:04:47 +08:00
get { return m_docs; }
2024-06-10 18:05:06 +08:00
}
public Span<bool> IsExpands
{
2024-06-13 18:04:47 +08:00
get { return new Span<bool>(m_isExpands, 0, m_docs.Metas.Length); }
2024-06-10 18:05:06 +08:00
}
public ReadOnlySpan<MetaGroupInfo> Infos
{
2024-09-10 19:13:31 +08:00
get
2024-06-10 18:05:06 +08:00
{
InitInfos();
2024-09-10 19:13:31 +08:00
return new ReadOnlySpan<MetaGroupInfo>(_infos);
2024-06-10 18:05:06 +08:00
}
}
private void InitInfos()
{
if (_isInitInfos) { return; }
ReadOnlySpan<DragonDocsMeta> metas;
2024-06-13 18:04:47 +08:00
if (m_docs == null || (metas = m_docs.Metas).IsEmpty)
2024-06-10 18:05:06 +08:00
{
_infos = Array.Empty<MetaGroupInfo>();
_isInitInfos = true;
return;
}
string groupPath = metas[0]._group;
int startIndex = 0;
List<MetaGroupInfo> groups = new List<MetaGroupInfo>(128);
for (int i = 1; i < metas.Length; i++)
{
var meta = metas[i];
if (groupPath != meta._group)
{
if (string.IsNullOrEmpty(groupPath))
{
2024-06-13 18:04:47 +08:00
groups.Add(new MetaGroupInfo("", "<OTHER>", startIndex, i - startIndex, 0));
2024-06-10 18:05:06 +08:00
}
else
{
AddInfo(groups, groupPath, startIndex, i - startIndex);
}
groupPath = meta._group;
startIndex = i;
}
}
AddInfo(groups, groupPath, startIndex, metas.Length - startIndex);
_infos = groups.ToArray();
2024-06-11 02:30:10 +08:00
_isInitInfos = true;
2024-06-10 18:05:06 +08:00
}
private void AddInfo(List<MetaGroupInfo> infos, string path, int startIndex, int length)
{
MetaGroupInfo lastInfo = infos[infos.Count - 1];
2024-06-13 18:04:47 +08:00
//if (lastInfo.Depth == 0) { lastInfo = new MetaGroupInfo("", "", 0, 0, 0); }
int depth = 0;
2024-06-10 18:05:06 +08:00
int lastSeparatorIndex = 0;
int i = 0;
int nameLength = 0;
2024-06-13 18:04:47 +08:00
//if(lastInfo.Path.Length <= path.Length)
2024-06-10 18:05:06 +08:00
{
for (int j = 0, jMax = lastInfo.Path.Length; j < jMax; j++)
{
char lastChr = lastInfo.Path[j];
char chr = path[j];
if (lastChr == chr)
{
i++;
}
else
{
break;
}
if (chr == '/') // || j == jMax - 1
{
depth++;
lastSeparatorIndex = j + 1;
nameLength = 0;
}
else
{
nameLength++;
}
}
}
for (int iMax = path.Length; i < iMax; i++)
{
char chr = path[i];
if (chr == '/')
{
infos.Add(new MetaGroupInfo(path.Substring(0, lastSeparatorIndex + nameLength + 1), path.Substring(lastSeparatorIndex, nameLength), startIndex, i == iMax - 1 ? length : 0, depth));
depth++;
lastSeparatorIndex = i + 1;
nameLength = 0;
}
else
{
nameLength++;
}
}
}
public void Save(DragonDocs docs)
{
2024-06-13 18:04:47 +08:00
m_docs = docs;
2024-09-10 19:13:31 +08:00
if (m_isExpands == null || m_isExpands.Length != docs.Metas.Length)
2024-06-10 18:05:06 +08:00
{
2024-06-13 18:04:47 +08:00
Array.Resize(ref m_isExpands, docs.Metas.Length);
m_isExpands[0] = true;
2024-06-10 18:05:06 +08:00
}
_isInitInfos = false;
_infos = null;
Save(true);
}
}
internal struct MetaGroupInfo
{
public readonly string Path;
public readonly string Name;
public readonly int StartIndex;
public readonly int Length;
public readonly int Depth;
public MetaGroupInfo(string path, string name, int startIndex, int length, int depth)
{
Path = path;
Name = name;
StartIndex = startIndex;
Length = length;
Depth = depth;
}
}
}
#endif