1
0
mirror of https://github.com/maxartz15/Validator.git synced 2024-11-24 04:55:31 +01:00

Stats, Settings, GUI toolbar, Filtering

This commit is contained in:
max 2021-12-29 14:41:48 +01:00
parent 4b9b5a8c1d
commit 2eee6cb661
2 changed files with 206 additions and 98 deletions

View File

@ -10,14 +10,44 @@ namespace Validator.Editor
{ {
public class ValidatorEditorWindow : EditorWindow public class ValidatorEditorWindow : EditorWindow
{ {
private readonly List<Report> reports = new List<Report>(); private class ValidatorInfo
private readonly List<IValidator> validators = new List<IValidator>(); {
public IValidator validator = null;
public bool isEnabled = true;
public ValidatorInfo(IValidator validator)
{
this.validator = validator;
}
}
private class ReportStats
{
public int TotalCount => infoCount + warningCount + errorCount;
public int infoCount = 0;
public int warningCount = 0;
public int errorCount = 0;
}
private class Settings
{
public bool showInfo = true;
public bool showWarning = true;
public bool showError = true;
public readonly Color lightColor = new Color(0.25f, 0.25f, 0.25f, 1);
public readonly Color darkColor = new Color(0.22f, 0.22f, 0.22f, 1);
}
private static readonly List<ValidatorInfo> validators = new List<ValidatorInfo>();
private static readonly List<Report> reports = new List<Report>();
private static readonly Settings settings = new Settings();
private static ReportStats reportStats = new ReportStats();
private MultiColumnHeaderState multiColumnHeaderState; private MultiColumnHeaderState multiColumnHeaderState;
private MultiColumnHeader multiColumnHeader; private MultiColumnHeader multiColumnHeader;
private MultiColumnHeaderState.Column[] columns; private MultiColumnHeaderState.Column[] columns;
private readonly Color lightColor = Color.white * 0.3f;
private readonly Color darkColor = Color.white * 0.1f;
private Vector2 scrollPosition; private Vector2 scrollPosition;
private float multiColumnHeaderWidth; private float multiColumnHeaderWidth;
private float rows; private float rows;
@ -26,7 +56,7 @@ namespace Validator.Editor
private static void Init() private static void Init()
{ {
ValidatorEditorWindow window = (ValidatorEditorWindow)GetWindow(typeof(ValidatorEditorWindow)); ValidatorEditorWindow window = (ValidatorEditorWindow)GetWindow(typeof(ValidatorEditorWindow));
window.titleContent = new GUIContent("Validator"); window.titleContent = new GUIContent("Validator", EditorGUIUtility.IconContent("d_UnityEditor.ConsoleWindow").image);
window.Show(); window.Show();
window.LoadValidators(); window.LoadValidators();
} }
@ -35,37 +65,7 @@ namespace Validator.Editor
{ {
using (new EditorGUILayout.VerticalScope(GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true))) using (new EditorGUILayout.VerticalScope(GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)))
{ {
using (new EditorGUILayout.HorizontalScope()) DrawMenu();
{
if (GUILayout.Button(EditorGUIUtility.IconContent("Refresh", "Refresh")))
{
LoadValidators();
}
if (GUILayout.Button(EditorGUIUtility.IconContent("PlayButton", "Run all")))
{
reports.Clear();
foreach (IValidator validator in validators)
{
reports.Add(validator.Validate());
}
}
if (GUILayout.Button(EditorGUIUtility.IconContent("d_preAudioLoopOff", "Select validator")))
{
GenericMenu validatorOptionsMenu = new GenericMenu();
foreach (IValidator validator in validators)
{
validatorOptionsMenu.AddItem(new GUIContent($"{validator.GetType().Name}"), false, OnGenericValidate, validator);
}
validatorOptionsMenu.ShowAsContext();
}
GUILayout.FlexibleSpace();
GUILayout.Label($"{reports.Count}/{validators.Count}");
}
GUILayout.Space(EditorGUIUtility.standardVerticalSpacing); GUILayout.Space(EditorGUIUtility.standardVerticalSpacing);
DrawMultiColumnScope(); DrawMultiColumnScope();
} }
@ -134,18 +134,54 @@ namespace Validator.Editor
}, },
}; };
multiColumnHeaderState = new MultiColumnHeaderState(columns: this.columns); multiColumnHeaderState = new MultiColumnHeaderState(columns);
multiColumnHeader = new MultiColumnHeader(state: this.multiColumnHeaderState); multiColumnHeader = new MultiColumnHeader(multiColumnHeaderState);
// When we chagne visibility of the column we resize columns to fit in the window.
//multiColumnHeader.visibleColumnsChanged += (multiColumnHeader) => multiColumnHeader.ResizeToFit();
// Initial resizing of the content.
multiColumnHeader.ResizeToFit(); multiColumnHeader.ResizeToFit();
} }
private void DrawMenu()
{
using (new EditorGUILayout.HorizontalScope(EditorStyles.toolbar))
{
if (GUILayout.Button(EditorGUIUtility.IconContent("Refresh", "Refresh"), EditorStyles.toolbarButton))
{
LoadValidators();
}
if (GUILayout.Button(EditorGUIUtility.IconContent("d_Settings", "Select validator"), EditorStyles.toolbarButton))
{
GenericMenu validatorOptionsMenu = new GenericMenu();
validatorOptionsMenu.AddItem(new GUIContent($"All"), false, OnValidatorInfoVisibilityAllEvent);
validatorOptionsMenu.AddItem(new GUIContent($"None"), false, OnValidatorInfoVisibilityNoneEvent);
validatorOptionsMenu.AddSeparator("");
foreach (ValidatorInfo validatorInfo in validators)
{
validatorOptionsMenu.AddItem(new GUIContent($"{validatorInfo.validator.GetType().Name}"), validatorInfo.isEnabled, OnValidatorInfoVisibilityChangedEvent, validatorInfo);
}
validatorOptionsMenu.ShowAsContext();
}
if (GUILayout.Button(EditorGUIUtility.IconContent("PlayButton", "Run all"), EditorStyles.toolbarButton))
{
RunValidators();
UpdateStats();
}
GUILayout.FlexibleSpace();
if(reportStats != null)
{
GUILayout.Label(new GUIContent($"{reportStats.TotalCount}", EditorGUIUtility.IconContent("d_console.erroricon.inactive.sml").image), EditorStyles.toolbarButton);
settings.showInfo = GUILayout.Toggle(settings.showInfo, new GUIContent($"{reportStats.infoCount}", EditorGUIUtility.IconContent(GetWarningIconName(WarningType.Info)).image), EditorStyles.toolbarButton);
settings.showWarning = GUILayout.Toggle(settings.showWarning, new GUIContent($"{reportStats.warningCount}", EditorGUIUtility.IconContent(GetWarningIconName(WarningType.Warning)).image), EditorStyles.toolbarButton);
settings.showError = GUILayout.Toggle(settings.showError, new GUIContent($"{reportStats.errorCount}", EditorGUIUtility.IconContent(GetWarningIconName(WarningType.Error)).image), EditorStyles.toolbarButton);
}
}
}
private void DrawMultiColumnScope() private void DrawMultiColumnScope()
{ {
GUILayout.FlexibleSpace(); //GUILayout.FlexibleSpace(); // If nothing has been drawn yet, uncomment this because GUILayoutUtility.GetLastRect() needs it.
Rect windowRect = GUILayoutUtility.GetLastRect(); Rect windowRect = GUILayoutUtility.GetLastRect();
windowRect.width = position.width; windowRect.width = position.width;
windowRect.height = position.height; windowRect.height = position.height;
@ -157,7 +193,7 @@ namespace Validator.Editor
float columnHeight = EditorGUIUtility.singleLineHeight * 2; float columnHeight = EditorGUIUtility.singleLineHeight * 2;
Rect headerRect = new Rect(source: windowRect) Rect headerRect = new Rect(windowRect)
{ {
height = EditorGUIUtility.singleLineHeight, height = EditorGUIUtility.singleLineHeight,
}; };
@ -166,20 +202,20 @@ namespace Validator.Editor
scrollViewPositionRect.y += headerRect.height - EditorGUIUtility.standardVerticalSpacing; scrollViewPositionRect.y += headerRect.height - EditorGUIUtility.standardVerticalSpacing;
scrollViewPositionRect.height -= headerRect.height - EditorGUIUtility.standardVerticalSpacing; scrollViewPositionRect.height -= headerRect.height - EditorGUIUtility.standardVerticalSpacing;
Rect scrollViewRect = new Rect(source: windowRect) Rect scrollViewRect = new Rect(windowRect)
{ {
width = multiColumnHeaderState.widthOfAllVisibleColumns, width = multiColumnHeaderState.widthOfAllVisibleColumns,
height = rows * columnHeight height = rows * columnHeight
}; };
Rect rowRect = new Rect(source: windowRect) Rect rowRect = new Rect(windowRect)
{ {
width = multiColumnHeaderWidth, width = multiColumnHeaderWidth,
height = columnHeight, height = columnHeight,
}; };
// Draw column header. // Draw column header.
multiColumnHeader.OnGUI(rect: headerRect, xScroll: scrollPosition.x); multiColumnHeader.OnGUI(headerRect, scrollPosition.x);
// Draw scroll view. // Draw scroll view.
using (GUI.ScrollViewScope scope = new GUI.ScrollViewScope(scrollViewPositionRect, scrollPosition, scrollViewRect, false, false)) using (GUI.ScrollViewScope scope = new GUI.ScrollViewScope(scrollViewPositionRect, scrollPosition, scrollViewRect, false, false))
@ -192,51 +228,69 @@ namespace Validator.Editor
{ {
for (int j = 0; j < reports[i].Reports.Count; j++) for (int j = 0; j < reports[i].Reports.Count; j++)
{ {
// Only draw what is visable within the view. switch (reports[i].Reports[j].WarningType)
{
case WarningType.Info:
if (!settings.showInfo)
continue;
break;
case WarningType.Warning:
if (!settings.showWarning)
continue;
break;
case WarningType.Error:
if (!settings.showError)
continue;
break;
default:
break;
}
// Only draw what is visible within the view.
if (rowRect.yMax > windowRect.y + scrollPosition.y && rowRect.yMin < windowRect.height + scrollPosition.y) if (rowRect.yMax > windowRect.y + scrollPosition.y && rowRect.yMin < windowRect.height + scrollPosition.y)
{ {
if (j % 2 == 0) if (rows % 2 == 0)
{ {
EditorGUI.DrawRect(rect: rowRect, color: darkColor); EditorGUI.DrawRect(rowRect, settings.darkColor);
} }
else else
{ {
EditorGUI.DrawRect(rect: rowRect, color: lightColor); EditorGUI.DrawRect(rowRect, settings.lightColor);
} }
// Warning Field. // Warning Field.
int columnIndex = 0; int columnIndex = 0;
if (multiColumnHeader.IsColumnVisible(columnIndex: columnIndex)) if (multiColumnHeader.IsColumnVisible(columnIndex))
{ {
int visibleColumnIndex = multiColumnHeader.GetVisibleColumnIndex(columnIndex: columnIndex); int visibleColumnIndex = multiColumnHeader.GetVisibleColumnIndex(columnIndex);
Rect columnRect = multiColumnHeader.GetColumnRect(visibleColumnIndex: visibleColumnIndex); Rect columnRect = multiColumnHeader.GetColumnRect(visibleColumnIndex);
columnRect.y = rowRect.y; columnRect.y = rowRect.y;
columnRect.height = rowRect.height; columnRect.height = rowRect.height;
Rect labelFieldRect = multiColumnHeader.GetCellRect(visibleColumnIndex: visibleColumnIndex, columnRect); Rect labelFieldRect = multiColumnHeader.GetCellRect(visibleColumnIndex, columnRect);
labelFieldRect.x += 9; labelFieldRect.x += 9;
labelFieldRect.width -= 9; labelFieldRect.width -= 9;
EditorGUI.LabelField( EditorGUI.LabelField(
position: labelFieldRect, labelFieldRect,
label: EditorGUIUtility.IconContent(GetWarningIconName(reports[i].Reports[j].WarningType), $"{reports[i].Reports[j].WarningType}") EditorGUIUtility.IconContent(GetWarningIconName(reports[i].Reports[j].WarningType), $"{reports[i].Reports[j].WarningType}")
); );
} }
// Target Field. // Target Field.
columnIndex = 1; columnIndex = 1;
if (multiColumnHeader.IsColumnVisible(columnIndex: columnIndex)) if (multiColumnHeader.IsColumnVisible(columnIndex))
{ {
int visibleColumnIndex = multiColumnHeader.GetVisibleColumnIndex(columnIndex: columnIndex); int visibleColumnIndex = multiColumnHeader.GetVisibleColumnIndex(columnIndex);
Rect columnRect = multiColumnHeader.GetColumnRect(visibleColumnIndex: visibleColumnIndex); Rect columnRect = multiColumnHeader.GetColumnRect(visibleColumnIndex);
columnRect.y = rowRect.y; columnRect.y = rowRect.y;
columnRect.height = rowRect.height; columnRect.height = rowRect.height;
if(reports[i].Reports[j].Target != null) if(reports[i].Reports[j].Target != null)
{ {
if (GUI.Button( if (GUI.Button(
position: multiColumnHeader.GetCellRect(visibleColumnIndex: visibleColumnIndex, columnRect), multiColumnHeader.GetCellRect(visibleColumnIndex, columnRect),
content: EditorGUIUtility.IconContent("Animation.FilterBySelection", "Ping Object") EditorGUIUtility.IconContent("Animation.FilterBySelection", "Ping Object")
)) ))
{ {
Selection.objects = new Object[] { reports[i].Reports[j].Target }; Selection.objects = new Object[] { reports[i].Reports[j].Target };
@ -257,58 +311,58 @@ namespace Validator.Editor
// Category Field. // Category Field.
columnIndex = 2; columnIndex = 2;
if (multiColumnHeader.IsColumnVisible(columnIndex: columnIndex)) if (multiColumnHeader.IsColumnVisible(columnIndex))
{ {
int visibleColumnIndex = multiColumnHeader.GetVisibleColumnIndex(columnIndex: columnIndex); int visibleColumnIndex = multiColumnHeader.GetVisibleColumnIndex(columnIndex);
Rect columnRect = multiColumnHeader.GetColumnRect(visibleColumnIndex: visibleColumnIndex); Rect columnRect = multiColumnHeader.GetColumnRect(visibleColumnIndex);
columnRect.y = rowRect.y; columnRect.y = rowRect.y;
columnRect.height = rowRect.height; columnRect.height = rowRect.height;
Rect labelFieldRect = multiColumnHeader.GetCellRect(visibleColumnIndex: visibleColumnIndex, columnRect); Rect labelFieldRect = multiColumnHeader.GetCellRect(visibleColumnIndex, columnRect);
labelFieldRect.x += 7; labelFieldRect.x += 7;
labelFieldRect.width -= 7; labelFieldRect.width -= 7;
EditorGUI.LabelField( EditorGUI.LabelField(
position: labelFieldRect, labelFieldRect,
label: new GUIContent($"{reports[i].Reports[j].Category}") new GUIContent($"{reports[i].Reports[j].Category}")
); );
} }
// Message Field. // Message Field.
columnIndex = 3; columnIndex = 3;
if (multiColumnHeader.IsColumnVisible(columnIndex: columnIndex)) if (multiColumnHeader.IsColumnVisible(columnIndex))
{ {
int visibleColumnIndex = multiColumnHeader.GetVisibleColumnIndex(columnIndex: columnIndex); int visibleColumnIndex = multiColumnHeader.GetVisibleColumnIndex(columnIndex);
Rect columnRect = multiColumnHeader.GetColumnRect(visibleColumnIndex: visibleColumnIndex); Rect columnRect = multiColumnHeader.GetColumnRect(visibleColumnIndex);
columnRect.y = rowRect.y; columnRect.y = rowRect.y;
columnRect.height = rowRect.height; columnRect.height = rowRect.height;
Rect labelFieldRect = multiColumnHeader.GetCellRect(visibleColumnIndex: visibleColumnIndex, columnRect); Rect labelFieldRect = multiColumnHeader.GetCellRect(visibleColumnIndex, columnRect);
labelFieldRect.x += 7; labelFieldRect.x += 7;
labelFieldRect.width -= 7; labelFieldRect.width -= 7;
EditorGUI.LabelField( EditorGUI.LabelField(
position: labelFieldRect, labelFieldRect,
label: new GUIContent($"{reports[i].Reports[j].Message}") new GUIContent($"{reports[i].Reports[j].Message}")
); );
} }
// Solution Field. // Solution Field.
columnIndex = 4; columnIndex = 4;
if (multiColumnHeader.IsColumnVisible(columnIndex: columnIndex)) if (multiColumnHeader.IsColumnVisible(columnIndex))
{ {
int visibleColumnIndex = multiColumnHeader.GetVisibleColumnIndex(columnIndex: columnIndex); int visibleColumnIndex = multiColumnHeader.GetVisibleColumnIndex(columnIndex);
Rect columnRect = multiColumnHeader.GetColumnRect(visibleColumnIndex: visibleColumnIndex); Rect columnRect = multiColumnHeader.GetColumnRect(visibleColumnIndex);
columnRect.y = rowRect.y; columnRect.y = rowRect.y;
columnRect.height = rowRect.height; columnRect.height = rowRect.height;
Rect labelFieldRect = multiColumnHeader.GetCellRect(visibleColumnIndex: visibleColumnIndex, columnRect); Rect labelFieldRect = multiColumnHeader.GetCellRect(visibleColumnIndex, columnRect);
labelFieldRect.x += 7; labelFieldRect.x += 7;
labelFieldRect.width -= 7; labelFieldRect.width -= 7;
EditorGUI.LabelField( EditorGUI.LabelField(
position: labelFieldRect, labelFieldRect,
label: new GUIContent($"{reports[i].Reports[j].Solution}") new GUIContent($"{reports[i].Reports[j].Solution}")
); );
} }
} }
@ -325,7 +379,7 @@ namespace Validator.Editor
{ {
for (int i = validators.Count - 1; i >= 0; i--) for (int i = validators.Count - 1; i >= 0; i--)
{ {
if (validators[i] == null) if (validators[i].validator == null)
{ {
validators.RemoveAt(i); validators.RemoveAt(i);
} }
@ -334,9 +388,9 @@ namespace Validator.Editor
foreach (Type type in GetValidatorTypes()) foreach (Type type in GetValidatorTypes())
{ {
bool hasValidator = false; bool hasValidator = false;
foreach (IValidator validator in validators) foreach (ValidatorInfo validatorInfo in validators)
{ {
if (validator.GetType() == type) if (validatorInfo.validator.GetType() == type)
{ {
hasValidator = true; hasValidator = true;
} }
@ -347,18 +401,72 @@ namespace Validator.Editor
IValidator validator = (IValidator)Activator.CreateInstance(type); IValidator validator = (IValidator)Activator.CreateInstance(type);
if (validator != null) if (validator != null)
{ {
validators.Add(validator); validators.Add(new ValidatorInfo(validator));
} }
} }
} }
} }
private void OnGenericValidate(object validator) private void RunValidators()
{
if (validator is IValidator val)
{ {
reports.Clear(); reports.Clear();
reports.Add(val.Validate());
foreach (ValidatorInfo validatorInfo in validators)
{
if (validatorInfo.isEnabled)
{
reports.Add(validatorInfo.validator.Validate());
}
}
}
private void UpdateStats()
{
reportStats = new ReportStats();
foreach (Report report in reports)
{
for (int i = 0; i < report.Reports.Count; i++)
{
switch (report.Reports[i].WarningType)
{
case WarningType.Info:
reportStats.infoCount++;
break;
case WarningType.Warning:
reportStats.warningCount++;
break;
case WarningType.Error:
reportStats.errorCount++;
break;
default:
break;
}
}
}
}
private void OnValidatorInfoVisibilityChangedEvent(object info)
{
if(info is ValidatorInfo validatorInfo)
{
validatorInfo.isEnabled = !validatorInfo.isEnabled;
}
}
private void OnValidatorInfoVisibilityAllEvent()
{
foreach (ValidatorInfo validatorInfo in validators)
{
validatorInfo.isEnabled = true;
}
}
private void OnValidatorInfoVisibilityNoneEvent()
{
foreach (ValidatorInfo validatorInfo in validators)
{
validatorInfo.isEnabled = false;
} }
} }
@ -371,7 +479,7 @@ namespace Validator.Editor
{ {
return warningType switch return warningType switch
{ {
WarningType.Info => "d_console.warnicon.inactive.sml", WarningType.Info => "d_console.infoicon.sml",
WarningType.Warning => "d_console.warnicon.sml", WarningType.Warning => "d_console.warnicon.sml",
WarningType.Error => "d_console.erroricon.sml", WarningType.Error => "d_console.erroricon.sml",
_ => "d_console.erroricon.inactive.sml", _ => "d_console.erroricon.inactive.sml",

View File

@ -12,7 +12,7 @@ namespace Validator
public class Report public class Report
{ {
public struct ReportMessage public class ReportMessage
{ {
public Object Target => target; public Object Target => target;
public WarningType WarningType => warningType; public WarningType WarningType => warningType;
@ -37,8 +37,8 @@ namespace Validator
} }
public string Name => name; public string Name => name;
public IList<ReportMessage> Reports => reports.AsReadOnly(); public IList<ReportMessage> Reports => reports.AsReadOnly();
private readonly string name; private readonly string name;
private readonly List<ReportMessage> reports = new List<ReportMessage>(); private readonly List<ReportMessage> reports = new List<ReportMessage>();