using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class DemonDebugConsole : EditorWindow
{
private SerializedObject serObj;
private static DemonDebugConsole instance;
float currentScrollViewHeight;
bool resize = false;
Rect cursorChangeRect;
VisualElement visualElement;
ListView listView;
DemonDebugMessageBox currentBox = null;
private bool showStack = false;
[MenuItem("Tools/DemonTool/DebugConsole")]
private static void ShowWindow() {
instance = EditorWindow.GetWindow<DemonDebugConsole>();
instance.Show();
}
public void OnEnable() {
DemonDebug.OnLog += GetLogCall;
currentScrollViewHeight = this.position.height*0.55f;
cursorChangeRect = new Rect(0, currentScrollViewHeight+30, this.position.width, 2f);
//SetLogsBox();
}
public void GetLogCall(DemonDebugMessageBox messageBox) {
switch(messageBox.type) {
case LogType.Normal:
break;
case LogType.Error:
break;
case LogType.Warning:
break;
default:
break;
}
Repaint();
//ListViewUpdate();
logScrollViewPosition.y = 50 * DemonDebug.Logs.Count - currentScrollViewHeight+80;
}
Vector2 logScrollViewPosition;
Vector2 toggleScrollViewPosition;
public void OnGUI() {
///Tool Button
GUILayout.BeginHorizontal();
if( GUILayout.Button("Clear Logs",GUILayout.Width(100)) ) {
DemonDebug.CleanUP();
//listView.Rebuild();
}
showStack = GUILayout.Toggle(showStack,"Show Stack", GUILayout.Width(100));
GUILayout.EndHorizontal();
GUI.DrawTexture(new Rect(0, 20, position.width,0.5f), EditorGUIUtility.whiteTexture);
//MessageBox
GUILayout.BeginVertical();
//ShowSelectLogs();
GUILayout.BeginHorizontal();
toggleScrollViewPosition=GUILayout.BeginScrollView(toggleScrollViewPosition,GUILayout.Height(currentScrollViewHeight),GUILayout.Width(100));
foreach(var tag in DemonDebug.Tags) {
DemonDebug.TagsMap[tag] = GUILayout.Toggle(DemonDebug.TagsMap[tag], tag);
}
GUILayout.EndScrollView();
GUI.DrawTexture(new Rect(100,20,1,currentScrollViewHeight+10), EditorGUIUtility.whiteTexture);
logScrollViewPosition = GUILayout.BeginScrollView(logScrollViewPosition, GUILayout.Height(currentScrollViewHeight),GUILayout.Width(position.width-100));
GUIStyle boxStyle = new GUIStyle();
boxStyle.alignment = TextAnchor.UpperLeft;
boxStyle.normal.textColor = Color.white;
boxStyle.wordWrap = true;
boxStyle.normal.background = MakeTex(1, 1, Color.grey);
foreach (var a in DemonDebug.Logs) {
switch (a.type) {
case LogType.Normal:
boxStyle.normal.textColor = Color.white;
break;
case LogType.Error:
boxStyle.normal.textColor = Color.red; break;
case LogType.Warning:
boxStyle.normal.textColor= Color.yellow;
break;
}
bool show = true;
if (a.tags != null) {
foreach (var tag in a.tags) {
if (!DemonDebug.TagsMap[tag]) {
show = false; break;
}
}
}
if (show) {
string tmp = a._time + '\n' + a.owner + ":" + a.message;
if (showStack) tmp = tmp + "\n" + a.stackTrace;
GUILayout.Box(tmp, boxStyle, GUILayout.Width(position.width - 100));
GUILayout.Space(20);
}
}
GUILayout.EndScrollView();
GUILayout.EndHorizontal ();
cursorChangeRect.Set(cursorChangeRect.x, currentScrollViewHeight + 30, position.width, cursorChangeRect.height);
ResizeScrollView();
GUILayout.FlexibleSpace();
if(currentBox!=null) {
EditorGUILayout.LabelField(currentBox._time + '\n' + currentBox.owner + ":" + currentBox.message, GUILayout.Height(position.height - currentScrollViewHeight - 30));
}
GUILayout.EndVertical();
}
private void SetLogsBox() {
// The "makeItem" function will be called as needed
// when the ListView needs more items to render
Func<VisualElement> makeItem = () => new Label();
// As the user scrolls through the list, the ListView object
// will recycle elements created by the "makeItem"
// and invoke the "bindItem" callback to associate
// the element with the matching data item (specified as an index in the list)
Action<VisualElement, int> bindItem = (e, i) => (e as Label).text = DemonDebug.Logs[i]._time + '\n' + DemonDebug.Logs[i].owner + ":" + DemonDebug.Logs[i].message;
listView = new ListView();
listView.makeItem = makeItem;
listView.bindItem = bindItem;
listView.itemsSource = DemonDebug.Logs;
// Callback invoked when the user double clicks an item
//listView.onItemsChosen += Debug.Log;
// Callback invoked when the user changes the selection inside the ListView
//listView.onSelectionChange += ShowSelectLogs;
visualElement = new VisualElement();
rootVisualElement.Add(visualElement);
visualElement.style.position = Position.Relative;
visualElement.style.top = 30;
visualElement.style.left = 10;
visualElement.Add(listView);
}
private void ListViewUpdate() {
listView.Rebuild();
listView.ScrollToItem(listView.childCount - 1);
}
private void ShowSelectLogs() {
currentBox =listView.selectedItem as DemonDebugMessageBox;
}
private void ResizeScrollView() {
GUI.DrawTexture(cursorChangeRect, EditorGUIUtility.whiteTexture);
EditorGUIUtility.AddCursorRect(cursorChangeRect, MouseCursor.ResizeVertical);
//visualElement.style.height = currentScrollViewHeight;
if (Event.current.type == EventType.MouseDown && cursorChangeRect.Contains(Event.current.mousePosition)) {
resize = true;
}
if (resize) {
currentScrollViewHeight = Event.current.mousePosition.y-30;
}
if (Event.current.type == EventType.MouseUp)
resize = false;
}
private Texture2D MakeTex(int width, int height, Color col) {
Color[] pix = new Color[width * height];
for (int i = 0; i < pix.Length; ++i) {
pix[i] = col;
}
Texture2D result = new Texture2D(width, height);
result.SetPixels(pix);
result.Apply();
return result;
}
}