0.3.0: Added history list

This commit is contained in:
Anders Ejlersen 2021-02-14 13:49:25 +01:00
parent 1e1736f94a
commit 9b73200c73
38 changed files with 662 additions and 18 deletions

View file

@ -0,0 +1,75 @@
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
namespace Game.NavigationTool.Editor.History
{
internal static class HistoryGUIUtility
{
private static readonly int ENTRY_HASH = "DrawHistoryEntry".GetHashCode();
public static bool isClicked;
public static HistoryList.Entry currentEntry;
public static void DrawEntry(Rect rect, HistoryList.Entry entry, Styles styles, bool ignoreIndentLevel = false)
{
int id = GUIUtility.GetControlID(ENTRY_HASH, FocusType.Passive, rect);
bool on = currentEntry == entry;
bool intersects = rect.Contains(Event.current.mousePosition);
switch (Event.current.type)
{
case EventType.MouseDown:
if (intersects)
{
currentEntry = entry;
Event.current.Use();
}
break;
case EventType.MouseUp:
if (currentEntry == entry)
{
isClicked = intersects;
if (!isClicked)
currentEntry = null;
Event.current.Use();
}
break;
case EventType.Repaint:
Vector2 oldSize = EditorGUIUtility.GetIconSize();
EditorGUIUtility.SetIconSize(new Vector2(rect.height, rect.height));
GUIStyle style = entry.valid ? styles.entry : styles.invalidEntry;
style.Draw(rect, entry.content, id, on, intersects);
EditorGUIUtility.SetIconSize(oldSize);
break;
}
}
public static void Used()
{
isClicked = false;
currentEntry = null;
}
public static Texture2D GetIcon(string path)
{
var texture = AssetDatabase.GetCachedIcon(path) as Texture2D;
if (texture == null)
texture = InternalEditorUtility.GetIconForFile(path);
return texture;
}
public static void Repaint()
{
if (!EditorWindow.HasOpenInstances<EditorHistoryWindow>())
return;
var window = EditorWindow.GetWindow<EditorHistoryWindow>("History", false);
window.Repaint();
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fd4d5beff36f41d40ac4a5a495488f8e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,143 @@
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Game.NavigationTool.Editor.History
{
[Serializable]
internal sealed class HistoryList
{
public static readonly string PREF_ID = "PREF_HISTORY_LIST";
public const int MAX_LENGTH = 32;
public List<Entry> entries;
public HistoryList()
{
entries = new List<Entry>();
string json = EditorPrefs.GetString(PREF_ID);
if (!string.IsNullOrEmpty(json))
EditorJsonUtility.FromJsonOverwrite(json, this);
}
public void Add(string guid)
{
if (InternalAddEntry(new Entry(guid)))
Save();
}
private bool InternalAddEntry(Entry e)
{
e.Refresh();
if (!e.valid)
return false;
entries.Add(e);
if (entries.Count > MAX_LENGTH)
entries.RemoveRange(0, entries.Count - MAX_LENGTH);
return true;
}
public void AddRange(string[] guids)
{
var isDirty = false;
for (var i = 0; i < guids.Length; i++)
{
if (InternalAddEntry(new Entry(guids[i])))
isDirty = true;
}
if (isDirty)
Save();
}
public void AddRangeByPath(string[] paths)
{
var isDirty = false;
for (var i = 0; i < paths.Length; i++)
{
string guid = AssetDatabase.AssetPathToGUID(paths[i]);
if (InternalAddEntry(new Entry(guid)))
isDirty = true;
}
if (isDirty)
Save();
}
public void Remove(Entry e)
{
if (entries.Remove(e))
Save();
}
public void Clear()
{
entries.Clear();
Save();
}
public Object GetObject(Entry entry)
{
string path = AssetDatabase.GUIDToAssetPath(entry.guid);
return AssetDatabase.LoadMainAssetAtPath(path);
}
public void Save()
{
string json = EditorJsonUtility.ToJson(this, false);
EditorPrefs.SetString(PREF_ID, json);
}
/// <summary>
/// Class: Entry
/// </summary>
[Serializable]
public sealed class Entry : ISerializationCallbackReceiver
{
public string guid;
[NonSerialized]
public string name;
[NonSerialized]
public bool valid;
[NonSerialized]
public GUIContent content;
[NonSerialized]
public string lowerName;
public Entry(string guid)
{
this.guid = guid;
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
}
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
Refresh();
}
public void Refresh()
{
string path = AssetDatabase.GUIDToAssetPath(guid);
name = Path.GetFileNameWithoutExtension(path);
lowerName = name.ToLower();
valid = AssetDatabase.LoadMainAssetAtPath(path) != null;
content = new GUIContent(name, HistoryGUIUtility.GetIcon(path), path);
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a7c41592f12a4174885c183470ce5e91
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,46 @@
using UnityEditor;
namespace Game.NavigationTool.Editor.History
{
internal sealed class HistoryListPostProcess : AssetPostprocessor
{
private static bool IGNORE_NEXT_SELECTION_CHANGE;
static HistoryListPostProcess()
{
Selection.selectionChanged += OnSelectionChanged;
}
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
bool isDirty = deletedAssets.Length != 0 ||
movedAssets.Length != 0 ||
movedFromAssetPaths.Length != 0;
if (isDirty && HistoryUtility.IsLoaded())
HistoryUtility.RefreshAll();
}
private static void OnSelectionChanged()
{
if (Selection.assetGUIDs == null)
return;
if (IGNORE_NEXT_SELECTION_CHANGE)
{
IGNORE_NEXT_SELECTION_CHANGE = false;
}
else
{
HistoryList historyList = HistoryUtility.GetHistoryList();
historyList.AddRange(Selection.assetGUIDs);
HistoryGUIUtility.Repaint();
}
}
public static void IgnoreNextSelectionChange()
{
IGNORE_NEXT_SELECTION_CHANGE = true;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2d8bd093d38234a4f9268f2fb4fc6b80
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,37 @@
using System.Collections.Generic;
namespace Game.NavigationTool.Editor.History
{
internal static class HistoryUtility
{
private static HistoryList HISTORY_LIST;
static HistoryUtility()
{
HISTORY_LIST = null;
}
public static HistoryList GetHistoryList()
{
return HISTORY_LIST ?? (HISTORY_LIST = new HistoryList());
}
public static bool IsLoaded()
{
return HISTORY_LIST != null;
}
public static void RefreshAll()
{
if (HISTORY_LIST == null)
return;
List<HistoryList.Entry> entries = HISTORY_LIST.entries;
for (var i = 0; i < entries.Count; i++)
{
entries[i].Refresh();
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4ceaae411c5d2e64b899ea234444354b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: