2020-10-24 20:18:42 +11:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-11-03 20:59:13 +11:00
|
|
|
|
using UnityExplorer.Unstrip.ColorUtility;
|
2020-10-24 20:18:42 +11:00
|
|
|
|
using TMPro;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
2020-11-08 21:04:41 +11:00
|
|
|
|
using UnityExplorer.Config;
|
2020-10-24 20:18:42 +11:00
|
|
|
|
|
2020-11-05 17:33:04 +11:00
|
|
|
|
namespace UnityExplorer.UI.PageModel
|
2020-10-24 20:18:42 +11:00
|
|
|
|
{
|
|
|
|
|
public class DebugConsole
|
|
|
|
|
{
|
|
|
|
|
public static DebugConsole Instance { get; private set; }
|
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
public static bool LogUnity { get; set; } = ModConfig.Instance.Log_Unity_Debug;
|
2020-10-27 00:54:08 +11:00
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
public static readonly List<string> AllMessages = new List<string>();
|
|
|
|
|
public static readonly List<Text> MessageHolders = new List<Text>();
|
|
|
|
|
|
|
|
|
|
internal static readonly List<string> s_preInitMessages = new List<string>();
|
2020-10-24 20:18:42 +11:00
|
|
|
|
|
|
|
|
|
private TMP_InputField m_textInput;
|
|
|
|
|
|
|
|
|
|
public DebugConsole(GameObject parent)
|
|
|
|
|
{
|
|
|
|
|
Instance = this;
|
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
//AllMessages = new List<string>();
|
|
|
|
|
//MessageHolders = new List<Text>();
|
2020-10-24 20:18:42 +11:00
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
ConstructUI(parent);
|
|
|
|
|
|
|
|
|
|
string preAppend = "";
|
|
|
|
|
for (int i = s_preInitMessages.Count - 1; i >= 0; i--)
|
2020-10-24 20:18:42 +11:00
|
|
|
|
{
|
2020-11-08 21:04:41 +11:00
|
|
|
|
var msg = s_preInitMessages[i];
|
|
|
|
|
if (preAppend != "")
|
|
|
|
|
preAppend += "\r\n";
|
|
|
|
|
preAppend += msg;
|
2020-10-24 20:18:42 +11:00
|
|
|
|
}
|
2020-11-08 21:04:41 +11:00
|
|
|
|
m_textInput.text = preAppend;
|
2020-10-24 20:18:42 +11:00
|
|
|
|
}
|
2020-11-08 21:04:41 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
public static void Log(string message)
|
|
|
|
|
{
|
|
|
|
|
Log(message, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Log(string message, Color color)
|
|
|
|
|
{
|
|
|
|
|
Log(message, color.ToHex());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Log(string message, string hexColor)
|
|
|
|
|
{
|
2020-11-08 21:04:41 +11:00
|
|
|
|
message = $"{AllMessages.Count}: {message}";
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
AllMessages.Add(message);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
if (hexColor != null)
|
|
|
|
|
message = $"<color=#{hexColor}>{message}</color>";
|
|
|
|
|
|
|
|
|
|
if (Instance?.m_textInput)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
Instance.m_textInput.text = $"{message}\n{Instance.m_textInput.text}";
|
2020-11-08 21:04:41 +11:00
|
|
|
|
else
|
|
|
|
|
s_preInitMessages.Add(message);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
}
|
2020-10-24 20:18:42 +11:00
|
|
|
|
|
|
|
|
|
public void ConstructUI(GameObject parent)
|
|
|
|
|
{
|
2020-10-27 00:54:08 +11:00
|
|
|
|
var mainObj = UIFactory.CreateVerticalGroup(parent, new Color(0.1f, 0.1f, 0.1f, 1.0f));
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2020-10-27 00:54:08 +11:00
|
|
|
|
var mainGroup = mainObj.GetComponent<VerticalLayoutGroup>();
|
2020-10-24 20:18:42 +11:00
|
|
|
|
mainGroup.childControlHeight = true;
|
|
|
|
|
mainGroup.childControlWidth = true;
|
|
|
|
|
mainGroup.childForceExpandHeight = true;
|
|
|
|
|
mainGroup.childForceExpandWidth = true;
|
|
|
|
|
|
2020-10-27 00:54:08 +11:00
|
|
|
|
var mainImage = mainObj.GetComponent<Image>();
|
2020-10-24 20:18:42 +11:00
|
|
|
|
mainImage.maskable = true;
|
|
|
|
|
|
2020-10-27 00:54:08 +11:00
|
|
|
|
var mask = mainObj.AddComponent<Mask>();
|
2020-10-24 20:18:42 +11:00
|
|
|
|
mask.showMaskGraphic = true;
|
|
|
|
|
|
2020-10-27 00:54:08 +11:00
|
|
|
|
var mainLayout = mainObj.AddComponent<LayoutElement>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
mainLayout.minHeight = 190;
|
2020-10-24 20:18:42 +11:00
|
|
|
|
mainLayout.flexibleHeight = 0;
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
#region LOG AREA
|
2020-10-27 00:54:08 +11:00
|
|
|
|
var logAreaObj = UIFactory.CreateHorizontalGroup(mainObj);
|
|
|
|
|
var logAreaGroup = logAreaObj.GetComponent<HorizontalLayoutGroup>();
|
|
|
|
|
logAreaGroup.childControlHeight = true;
|
|
|
|
|
logAreaGroup.childControlWidth = true;
|
|
|
|
|
logAreaGroup.childForceExpandHeight = true;
|
|
|
|
|
logAreaGroup.childForceExpandWidth = true;
|
|
|
|
|
|
|
|
|
|
var logAreaLayout = logAreaObj.AddComponent<LayoutElement>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
logAreaLayout.preferredHeight = 190;
|
|
|
|
|
logAreaLayout.flexibleHeight = 0;
|
2020-10-24 20:18:42 +11:00
|
|
|
|
|
2020-10-27 00:54:08 +11:00
|
|
|
|
var inputObj = UIFactory.CreateTMPInput(logAreaObj);
|
|
|
|
|
|
|
|
|
|
var mainInputGroup = inputObj.GetComponent<VerticalLayoutGroup>();
|
|
|
|
|
mainInputGroup.padding.left = 8;
|
|
|
|
|
mainInputGroup.padding.right = 8;
|
|
|
|
|
mainInputGroup.padding.top = 5;
|
|
|
|
|
mainInputGroup.padding.bottom = 5;
|
|
|
|
|
|
|
|
|
|
var inputLayout = inputObj.AddComponent<LayoutElement>();
|
2020-10-24 20:18:42 +11:00
|
|
|
|
inputLayout.preferredWidth = 500;
|
|
|
|
|
inputLayout.flexibleWidth = 9999;
|
|
|
|
|
|
2020-10-27 00:54:08 +11:00
|
|
|
|
var inputImage = inputObj.GetComponent<Image>();
|
|
|
|
|
inputImage.color = new Color(0.05f, 0.05f, 0.05f, 1.0f);
|
|
|
|
|
|
|
|
|
|
var scroll = UIFactory.CreateScrollbar(logAreaObj);
|
2020-10-24 20:18:42 +11:00
|
|
|
|
|
|
|
|
|
var scrollLayout = scroll.AddComponent<LayoutElement>();
|
|
|
|
|
scrollLayout.preferredWidth = 25;
|
|
|
|
|
scrollLayout.flexibleWidth = 0;
|
|
|
|
|
|
|
|
|
|
var scroller = scroll.GetComponent<Scrollbar>();
|
|
|
|
|
scroller.direction = Scrollbar.Direction.TopToBottom;
|
|
|
|
|
var scrollColors = scroller.colors;
|
|
|
|
|
scrollColors.normalColor = new Color(0.5f, 0.5f, 0.5f, 1.0f);
|
|
|
|
|
scroller.colors = scrollColors;
|
|
|
|
|
|
2020-10-27 00:54:08 +11:00
|
|
|
|
var tmpInput = inputObj.GetComponent<TMP_InputField>();
|
2020-10-24 20:18:42 +11:00
|
|
|
|
tmpInput.scrollSensitivity = 15;
|
|
|
|
|
tmpInput.verticalScrollbar = scroller;
|
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
if (UIManager.ConsoleFont != null)
|
|
|
|
|
{
|
|
|
|
|
tmpInput.textComponent.font = UIManager.ConsoleFont;
|
|
|
|
|
#if MONO
|
|
|
|
|
(tmpInput.placeholder as TextMeshProUGUI).font = UIManager.ConsoleFont;
|
|
|
|
|
#else
|
|
|
|
|
tmpInput.placeholder.TryCast<TextMeshProUGUI>().font = UIManager.ConsoleFont;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-27 00:54:08 +11:00
|
|
|
|
tmpInput.readOnly = true;
|
|
|
|
|
|
|
|
|
|
m_textInput = inputObj.GetComponent<TMP_InputField>();
|
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
#endregion
|
2020-10-27 00:54:08 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
#region BOTTOM BAR
|
2020-10-27 00:54:08 +11:00
|
|
|
|
|
|
|
|
|
var bottomBarObj = UIFactory.CreateHorizontalGroup(mainObj);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
LayoutElement topBarLayout = bottomBarObj.AddComponent<LayoutElement>();
|
2020-10-27 00:54:08 +11:00
|
|
|
|
topBarLayout.minHeight = 40;
|
|
|
|
|
topBarLayout.flexibleHeight = 0;
|
|
|
|
|
|
|
|
|
|
var bottomGroup = bottomBarObj.GetComponent<HorizontalLayoutGroup>();
|
|
|
|
|
bottomGroup.padding.left = 10;
|
|
|
|
|
bottomGroup.padding.right = 10;
|
|
|
|
|
bottomGroup.padding.top = 2;
|
|
|
|
|
bottomGroup.padding.bottom = 2;
|
|
|
|
|
bottomGroup.spacing = 10;
|
|
|
|
|
bottomGroup.childForceExpandHeight = true;
|
|
|
|
|
bottomGroup.childForceExpandWidth = false;
|
|
|
|
|
bottomGroup.childControlWidth = true;
|
|
|
|
|
bottomGroup.childControlHeight = true;
|
|
|
|
|
bottomGroup.childAlignment = TextAnchor.MiddleLeft;
|
|
|
|
|
|
|
|
|
|
// Debug Console label
|
|
|
|
|
|
|
|
|
|
var bottomLabel = UIFactory.CreateLabel(bottomBarObj, TextAnchor.MiddleLeft);
|
|
|
|
|
var topBarLabelLayout = bottomLabel.AddComponent<LayoutElement>();
|
|
|
|
|
topBarLabelLayout.minWidth = 100;
|
|
|
|
|
topBarLabelLayout.flexibleWidth = 0;
|
|
|
|
|
var topBarText = bottomLabel.GetComponent<Text>();
|
|
|
|
|
topBarText.fontStyle = FontStyle.Bold;
|
|
|
|
|
topBarText.text = "Debug Console";
|
|
|
|
|
topBarText.fontSize = 14;
|
|
|
|
|
|
|
|
|
|
// Hide button
|
|
|
|
|
|
|
|
|
|
var hideButtonObj = UIFactory.CreateButton(bottomBarObj);
|
|
|
|
|
|
|
|
|
|
var hideBtnText = hideButtonObj.GetComponentInChildren<Text>();
|
|
|
|
|
hideBtnText.text = "Hide";
|
|
|
|
|
|
|
|
|
|
var hideButton = hideButtonObj.GetComponent<Button>();
|
|
|
|
|
#if CPP
|
|
|
|
|
hideButton.onClick.AddListener(new Action(HideCallback));
|
|
|
|
|
#else
|
|
|
|
|
hideButton.onClick.AddListener(HideCallback);
|
|
|
|
|
#endif
|
|
|
|
|
void HideCallback()
|
|
|
|
|
{
|
|
|
|
|
if (logAreaObj.activeSelf)
|
|
|
|
|
{
|
|
|
|
|
logAreaObj.SetActive(false);
|
|
|
|
|
hideBtnText.text = "Show";
|
2020-10-28 06:39:26 +11:00
|
|
|
|
mainLayout.minHeight = 40;
|
2020-10-27 00:54:08 +11:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logAreaObj.SetActive(true);
|
|
|
|
|
hideBtnText.text = "Hide";
|
2020-10-28 06:39:26 +11:00
|
|
|
|
mainLayout.minHeight = 190;
|
2020-10-27 00:54:08 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hideBtnColors = hideButton.colors;
|
|
|
|
|
//hideBtnColors.normalColor = new Color(160f / 255f, 140f / 255f, 40f / 255f);
|
|
|
|
|
hideButton.colors = hideBtnColors;
|
|
|
|
|
|
|
|
|
|
var hideBtnLayout = hideButtonObj.AddComponent<LayoutElement>();
|
|
|
|
|
hideBtnLayout.minWidth = 80;
|
|
|
|
|
hideBtnLayout.flexibleWidth = 0;
|
|
|
|
|
|
|
|
|
|
// Clear button
|
|
|
|
|
|
|
|
|
|
var clearButtonObj = UIFactory.CreateButton(bottomBarObj);
|
|
|
|
|
|
|
|
|
|
var clearBtnText = clearButtonObj.GetComponentInChildren<Text>();
|
|
|
|
|
clearBtnText.text = "Clear";
|
|
|
|
|
|
|
|
|
|
var clearButton = clearButtonObj.GetComponent<Button>();
|
|
|
|
|
#if CPP
|
|
|
|
|
clearButton.onClick.AddListener(new Action(ClearCallback));
|
|
|
|
|
#else
|
|
|
|
|
clearButton.onClick.AddListener(ClearCallback);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
void ClearCallback()
|
|
|
|
|
{
|
|
|
|
|
m_textInput.text = "";
|
2020-11-08 22:53:18 +11:00
|
|
|
|
AllMessages.Clear();
|
2020-10-27 00:54:08 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var clearBtnColors = clearButton.colors;
|
|
|
|
|
//clearBtnColors.normalColor = new Color(160f/255f, 140f/255f, 40f/255f);
|
|
|
|
|
clearButton.colors = clearBtnColors;
|
|
|
|
|
|
|
|
|
|
var clearBtnLayout = clearButtonObj.AddComponent<LayoutElement>();
|
|
|
|
|
clearBtnLayout.minWidth = 80;
|
|
|
|
|
clearBtnLayout.flexibleWidth = 0;
|
|
|
|
|
|
|
|
|
|
// Unity log toggle
|
|
|
|
|
|
|
|
|
|
var unityToggleObj = UIFactory.CreateToggle(bottomBarObj, out Toggle unityToggle, out Text unityToggleText);
|
|
|
|
|
#if CPP
|
2020-11-08 21:04:41 +11:00
|
|
|
|
unityToggle.onValueChanged.AddListener(new Action<bool>(ToggleLogUnity));
|
2020-10-27 00:54:08 +11:00
|
|
|
|
#else
|
2020-11-08 21:04:41 +11:00
|
|
|
|
unityToggle.onValueChanged.AddListener(ToggleLogUnity);
|
2020-10-27 00:54:08 +11:00
|
|
|
|
#endif
|
2020-11-08 21:04:41 +11:00
|
|
|
|
unityToggle.isOn = LogUnity;
|
2020-10-27 00:54:08 +11:00
|
|
|
|
unityToggleText.text = "Print Unity Debug?";
|
|
|
|
|
unityToggleText.alignment = TextAnchor.MiddleLeft;
|
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
void ToggleLogUnity(bool val)
|
|
|
|
|
{
|
|
|
|
|
LogUnity = val;
|
|
|
|
|
ModConfig.Instance.Log_Unity_Debug = val;
|
|
|
|
|
ModConfig.SaveSettings();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-27 00:54:08 +11:00
|
|
|
|
var unityToggleLayout = unityToggleObj.AddComponent<LayoutElement>();
|
|
|
|
|
unityToggleLayout.minWidth = 200;
|
|
|
|
|
unityToggleLayout.flexibleWidth = 0;
|
|
|
|
|
|
|
|
|
|
var unityToggleRect = unityToggleObj.transform.Find("Background").GetComponent<RectTransform>();
|
|
|
|
|
var pos = unityToggleRect.localPosition;
|
|
|
|
|
pos.y = -8;
|
|
|
|
|
unityToggleRect.localPosition = pos;
|
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
#endregion
|
2020-10-24 20:18:42 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|