module-navigation-tool/Editor/History/Utilities/HistoryGUIUtility.cs

98 lines
3.4 KiB
C#

using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
namespace Module.NavigationTool.Editor.History
{
internal static class HistoryGUIUtility
{
private static readonly int ENTRY_HASH = "DrawHistoryEntry".GetHashCode();
public static bool isClicked;
public static bool isDoubleClick;
public static HistoryList.Entry currentEntry;
private static HistoryList.Entry LAST_CLICK_ENTRY;
private static double LAST_CLICK_TIME;
private static int CLICK_COUNT;
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)
{
double dt = EditorApplication.timeSinceStartup - LAST_CLICK_TIME;
if (dt < 0.3 && CLICK_COUNT == 1 && LAST_CLICK_ENTRY == entry)
CLICK_COUNT++;
else
CLICK_COUNT = 1;
LAST_CLICK_ENTRY = entry;
LAST_CLICK_TIME = EditorApplication.timeSinceStartup;
isDoubleClick = CLICK_COUNT == 2;
}
else
{
currentEntry = null;
}
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();
}
}
}