feat: controls for pages, better templates, color changing, clear commenting
This commit is contained in:
@@ -3,24 +3,36 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Sirenix.OdinInspector;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
// ReSharper disable UsageOfDefaultStructEquality, to disable the warnings about IndexOf(currentPage). It's fine.
|
||||
namespace Reset.Core.Tools{
|
||||
public class DebugOverlayDrawer : MonoBehaviour{
|
||||
|
||||
[Header("Asset References")]
|
||||
[BoxGroup("References")] public VisualTreeAsset valueTemplate;
|
||||
[BoxGroup("References")] public VisualTreeAsset pageBaseTemplate;
|
||||
[BoxGroup("References")] public VisualTreeAsset pageButtonTemplate;
|
||||
[Header("Scene References")]
|
||||
[BoxGroup("References")][ShowInInspector] public UIDocument root;
|
||||
[BoxGroup("References")]public GameObject canvasRootGameObject;
|
||||
|
||||
[Space(10)]
|
||||
[ShowInInspector]
|
||||
public Dictionary<GameObject, List<string>> Pages = new(); // Page and value names
|
||||
[Space(5)]
|
||||
[ShowInInspector]
|
||||
public Dictionary<object, Label> values = new(); // Value name and value value (lol)
|
||||
|
||||
[Space]
|
||||
public string pageNamePrefix = "Debug Page: ";
|
||||
|
||||
public VisualTreeAsset template;
|
||||
[ShowInInspector] public UIDocument root;
|
||||
public GameObject canvasRoot;
|
||||
|
||||
public GameObject currentPage;
|
||||
|
||||
private static float defaultLabelAlpha;
|
||||
|
||||
// Private variables
|
||||
[ShowInInspector]
|
||||
public KeyValuePair<GameObject, List<string>> currentPage;
|
||||
|
||||
public enum DebugOverlayAnchor{
|
||||
TopLeft,
|
||||
@@ -37,61 +49,161 @@ namespace Reset.Core.Tools{
|
||||
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
|
||||
void Start(){
|
||||
SetCurrentPageVisible();
|
||||
|
||||
Debug.Log(valueTemplate.Instantiate().resolvedStyle.backgroundColor);
|
||||
|
||||
defaultLabelAlpha = valueTemplate.Instantiate().style.backgroundColor.value.a;
|
||||
}
|
||||
|
||||
public static DebugOverlayDrawer Instance;
|
||||
|
||||
public static void AddOnOverlay(string pageName, string sourceName, Vector3 position){
|
||||
// Make sure the page exists. If it does, use it. If not, create it
|
||||
GameObject thisPage = null;
|
||||
GameObject thisPageObject = null;
|
||||
bool alreadyExisted = false;
|
||||
|
||||
// This checks for a gameobject with the page name. Sets it if it finds it
|
||||
foreach (var page in Instance.Pages) {
|
||||
if (page.Key.name == $"{Instance.pageNamePrefix}{pageName}") {
|
||||
thisPage = page.Key;
|
||||
thisPageObject = page.Key;
|
||||
alreadyExisted = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, this will make a new one and add it
|
||||
if (thisPage == null) {
|
||||
thisPage = new GameObject(name: $"{Instance.pageNamePrefix}{pageName}");
|
||||
Instance.Pages.Add(thisPage, new List<string>());
|
||||
thisPage.transform.SetParent(Instance.canvasRoot.transform);
|
||||
thisPage.AddComponent<UIDocument>();
|
||||
UIDocument thisDocument = null;
|
||||
|
||||
if (thisPageObject == null) {
|
||||
// Create a new object
|
||||
thisPageObject = new GameObject(name: $"{Instance.pageNamePrefix}{pageName}");
|
||||
|
||||
// Add it to the list of page objects and set it's transform
|
||||
KeyValuePair<GameObject, List<string>> newPage = new(thisPageObject, new List<string>());
|
||||
Instance.Pages.Add(newPage.Key, newPage.Value);
|
||||
thisPageObject.transform.SetParent(Instance.canvasRootGameObject.transform);
|
||||
|
||||
// Add a UI Document for it, give it the base template
|
||||
thisDocument = thisPageObject.AddComponent<UIDocument>();
|
||||
thisDocument.visualTreeAsset = Instance.pageBaseTemplate;
|
||||
|
||||
// Then add a button/header to the main container
|
||||
VisualElement newPageButton = Instance.pageButtonTemplate.Instantiate();
|
||||
Instance.root.rootVisualElement.Query<VisualElement>("Pages").First().Add(newPageButton);
|
||||
((Label)newPageButton.Query<VisualElement>("PageLabel").First()).text = pageName;
|
||||
} else {
|
||||
thisDocument = thisPageObject.GetComponent<UIDocument>();
|
||||
}
|
||||
|
||||
// If this is the only page, set it to the current page
|
||||
if (Instance.Pages.Count == 1) {
|
||||
Instance.currentPage = new KeyValuePair<GameObject, List<string>>(thisPageObject, new List<string>());
|
||||
SetCurrentPageVisible();
|
||||
}
|
||||
|
||||
// Warning for the page being null for whatever reason
|
||||
if (thisPage == null) {
|
||||
// Warning for the page being null for whatever reason by this point
|
||||
if (thisPageObject == null) {
|
||||
Debug.LogError(
|
||||
$"Debug Overlay Manager never found a page named: {sourceName}. It failed to create one and aborted.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Now, iterate through this page and see if the variable is already tracked
|
||||
if (alreadyExisted && Instance.Pages[thisPage].Contains(sourceName)) {
|
||||
if (alreadyExisted && Instance.Pages[thisPageObject].Contains(sourceName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Since it doesn't exist, add it to the list of tracked variables
|
||||
Instance.Pages[thisPage].Add(sourceName);
|
||||
Instance.Pages[thisPageObject].Add(sourceName);
|
||||
|
||||
// Then create a new on-screen element for this variable
|
||||
VisualElement thisElement = (Instance.template.Instantiate());
|
||||
thisPage.GetComponent<UIDocument>().rootVisualElement.Add(thisElement);
|
||||
thisElement.style.alignSelf = new StyleEnum<Align>(Align.Center);
|
||||
|
||||
VisualElement thisElement = (Instance.valueTemplate.Instantiate());
|
||||
thisDocument.rootVisualElement.Query<VisualElement>("ValuesContainer").First().Add(thisElement);
|
||||
|
||||
// Set it's label
|
||||
((Label)thisElement.Query<VisualElement>("Label").First()).text = sourceName;
|
||||
|
||||
|
||||
Instance.values.Add($"{pageName}/{sourceName}", (Label)thisElement.Query<VisualElement>("Value"));
|
||||
|
||||
}
|
||||
|
||||
// Publicly accessible method to change the value
|
||||
public static void ChangeValue(string pageName, string sourceName, string newValue){
|
||||
Instance.values[$"{pageName}/{sourceName}"].text = newValue;
|
||||
}
|
||||
}
|
||||
|
||||
// Publicly accessible method to change the color
|
||||
public static void ChangeColor(string pageName, string sourceName, Color newColor){
|
||||
VisualElement labelBG = Instance.values[$"{pageName}/{sourceName}"].parent.parent.Query<VisualElement>("LabelBackground").First();
|
||||
VisualElement valueBG = Instance.values[$"{pageName}/{sourceName}"].parent.parent.Query<VisualElement>("ValueBackground").First();
|
||||
|
||||
labelBG.style.backgroundColor = (newColor * .7f).Alpha(labelBG.resolvedStyle.backgroundColor.a);
|
||||
valueBG.style.backgroundColor = (newColor * .6f).Alpha(valueBG.resolvedStyle.backgroundColor.a);
|
||||
}
|
||||
|
||||
// Back a page
|
||||
public static void PreviousPage(){
|
||||
// Do nothing if there's only one page
|
||||
if (Instance.Pages.Count == 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the index of the current page
|
||||
int currentIndex = Instance.Pages.ToList().IndexOf(Instance.currentPage);
|
||||
|
||||
// Change the page with wrapping
|
||||
if (Instance.Pages.First().Key == Instance.currentPage.Key) {
|
||||
Instance.currentPage = Instance.Pages.Last();
|
||||
} else {
|
||||
Instance.currentPage = Instance.Pages.ElementAt(currentIndex - 1);
|
||||
}
|
||||
|
||||
// Set visibility
|
||||
SetCurrentPageVisible();
|
||||
}
|
||||
|
||||
public static void NextPage(){
|
||||
// Do nothing if there's only one page
|
||||
if (Instance.Pages.Count == 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the index of the current page
|
||||
int currentIndex = Instance.Pages.ToList().IndexOf(Instance.currentPage);
|
||||
|
||||
// Change the page with wrapping
|
||||
if (Instance.Pages.Last().Key == Instance.currentPage.Key) {
|
||||
Instance.currentPage = Instance.Pages.First();
|
||||
} else {
|
||||
Instance.currentPage = Instance.Pages.ElementAt(currentIndex + 1);
|
||||
}
|
||||
|
||||
// Set visibility
|
||||
SetCurrentPageVisible();
|
||||
}
|
||||
|
||||
static void SetCurrentPageVisible(){
|
||||
// Set the display property for the page to enable/disable them
|
||||
foreach (var page in Instance.Pages) {
|
||||
if (page.Key == Instance.currentPage.Key) {
|
||||
page.Key.GetComponent<UIDocument>().rootVisualElement.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.Flex);
|
||||
} else {
|
||||
page.Key.GetComponent<UIDocument>().rootVisualElement.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.None);
|
||||
}
|
||||
}
|
||||
|
||||
// Turn all the page labels into a list
|
||||
var pageButtons = Instance.root.rootVisualElement.Query<Label>("PageLabel").ToList();
|
||||
|
||||
// Iterate through said list and set the opacity of the parent (so everything including the label gets turned down)
|
||||
foreach (Label page in pageButtons) {
|
||||
page.parent.style.opacity = .4f;
|
||||
if (Instance.currentPage.Key.name == $"{Instance.pageNamePrefix}{page.text}") {
|
||||
page.parent.style.opacity = 1f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user