Add some CSConsole interactions, some UI Cleanups

This commit is contained in:
Sinai 2021-05-14 06:14:25 +10:00
parent 12fe19ba8e
commit 1c216c0d86
8 changed files with 96 additions and 68 deletions

View File

@ -13,7 +13,12 @@ namespace UnityExplorer
public static string EnsureValidDirectory(string path) public static string EnsureValidDirectory(string path)
{ {
return string.Concat(path.Split(invalidDirectoryCharacters)); path = string.Concat(path.Split(invalidDirectoryCharacters));
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
return path;
} }
public static string EnsureValidFilename(string filename) public static string EnsureValidFilename(string filename)

View File

@ -227,8 +227,8 @@ namespace UnityExplorer.UI.CSConsole
if (ScriptEvaluator._reportPrinter.ErrorsCount > 0) if (ScriptEvaluator._reportPrinter.ErrorsCount > 0)
throw new FormatException($"Unable to compile the code. Evaluator's last output was:\r\n{output}"); throw new FormatException($"Unable to compile the code. Evaluator's last output was:\r\n{output}");
if (!supressLog) //if (!supressLog)
ExplorerCore.Log("Code executed successfully."); // ExplorerCore.Log("Code executed successfully.");
} }
catch (FormatException fex) catch (FormatException fex)
{ {

View File

@ -5,33 +5,34 @@ using UnityEngine;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using UnityExplorer.Core.Runtime; using UnityExplorer.Core.Runtime;
using System.Text;
namespace UnityExplorer.UI.CSConsole namespace UnityExplorer.UI.CSConsole
{ {
public class ScriptInteraction : InteractiveBase public class ScriptInteraction : InteractiveBase
{ {
internal const string STARTUP_TEXT = @"Welcome to the UnityExplorer C# Console. internal const string STARTUP_TEXT = @"<color=#5d8556>// Compile a using directive to add it to the console (until Reset)</color>
using SomeNamespace;
Use this console to declare temporary C# classes, or evaluate standalone expressions as though you were executing a method body. "+ <color=#5d8556>// Compile a C# class and it will exist until Reset</color>
@"Use the <b>Reset</b> button to clear all classes and variables, and restore the default using directives. <color=#5a728c>public class</color> SomeClass {
<color=#5a728c>public static void</color> SomeMethod() {
}
}
* <color=#add490>using SomeNamespace;</color> to add a namespace to the Console. This is not required if you use full namespaces to access classes. <color=#5d8556>// If not compiling any usings or classes, the code will run immediately (REPL).
// Variables you define in REPL mode will also exist until Reset.
When evaluating standalone expressions, these helpers are available: // In REPL context, the following helpers are available:</color>
* System.Object <color=#96b570>CurrentTarget</color> - the target of the active Inspector tab
* System.Object[] <color=#96b570>AllTargets</color> - an array containing the targets of all Inspector tabs
* void <color=#add490>Log(""message"")</color> - logs a message to the console log
* System.Object <color=#add490>CurrentTarget</color> - the target of the active Inspector tab
* System.Object[] <color=#add490>AllTargets</color> - an array containing the targets of all Inspector tabs
* void <color=#add490>Log(""message"")</color> - prints a message to the console log
* void <color=#add490>Inspect(someObject)</color> - inspect an instance, eg. Inspect(Camera.main); * void <color=#add490>Inspect(someObject)</color> - inspect an instance, eg. Inspect(Camera.main);
* void <color=#add490>Inspect(typeof(SomeClass))</color> - inspect a Class with static reflection * void <color=#add490>Inspect(typeof(SomeClass))</color> - inspect a Class with static reflection
* void <color=#add490>StartCoroutine(ienumerator)</color> - start the IEnumerator as a Coroutine * void <color=#add490>StartCoroutine(ienumerator)</color> - start the IEnumerator as a Coroutine
* void <color=#add490>GetUsing()</color> - prints the current using directives to the console log
"; * void <color=#add490>GetVars()</color> - prints the variables you have defined and their current values
* void <color=#add490>GetClasses()</color> - prints the names of the classes you have defined, and their members";
public static void Log(object message) public static void Log(object message)
{ {
@ -56,5 +57,36 @@ When evaluating standalone expressions, these helpers are available:
{ {
RuntimeProvider.Instance.StartCoroutine(ienumerator); RuntimeProvider.Instance.StartCoroutine(ienumerator);
} }
public static void GetUsing()
{
Log(Evaluator.GetUsing());
}
public static void GetVars()
{
Log(Evaluator.GetVars());
}
public static void GetClasses()
{
if (ReflectionUtility.GetFieldInfo(typeof(Evaluator), "source_file")
.GetValue(Evaluator) is CompilationSourceFile sourceFile
&& sourceFile.Containers.Any())
{
var sb = new StringBuilder();
sb.Append($"There are {sourceFile.Containers.Count} defined classes:");
foreach (TypeDefinition type in sourceFile.Containers.Where(it => it is TypeDefinition))
{
sb.Append($"\n\n{type.MemberName.Name}:");
foreach (var member in type.Members)
sb.Append($"\n\t- {member.AttributeTargets}: \"{member.MemberName.Name}\" ({member.ModFlags})");
}
Log(sb.ToString());
}
else
ExplorerCore.LogWarning("No classes seem to be defined.");
}
} }
} }

View File

@ -155,12 +155,6 @@ namespace UnityExplorer.UI.IValues
var horiGroup = UIFactory.CreateHorizontalGroup(UIRoot, "ColorEditor", false, false, true, true, 5, var horiGroup = UIFactory.CreateHorizontalGroup(UIRoot, "ColorEditor", false, false, true, true, 5,
default, new Color(1, 1, 1, 0), TextAnchor.MiddleLeft); default, new Color(1, 1, 1, 0), TextAnchor.MiddleLeft);
// apply button
m_applyButton = UIFactory.CreateButton(horiGroup, "ApplyButton", "Apply", new Color(0.2f, 0.26f, 0.2f));
UIFactory.SetLayoutElement(m_applyButton.Component.gameObject, minHeight: 25, minWidth: 90);
m_applyButton.OnClick += SetValueToOwner;
// sliders / inputs // sliders / inputs
var grid = UIFactory.CreateGridGroup(horiGroup, "Grid", new Vector2(140, 25), new Vector2(2, 2), new Color(1, 1, 1, 0)); var grid = UIFactory.CreateGridGroup(horiGroup, "Grid", new Vector2(140, 25), new Vector2(2, 2), new Color(1, 1, 1, 0));
@ -169,6 +163,12 @@ namespace UnityExplorer.UI.IValues
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
AddEditorRow(i, grid); AddEditorRow(i, grid);
// apply button
m_applyButton = UIFactory.CreateButton(horiGroup, "ApplyButton", "Apply", new Color(0.2f, 0.26f, 0.2f));
UIFactory.SetLayoutElement(m_applyButton.Component.gameObject, minHeight: 25, minWidth: 90);
m_applyButton.OnClick += SetValueToOwner;
// image of color // image of color
var imgObj = UIFactory.CreateUIObject("ColorImageHelper", horiGroup); var imgObj = UIFactory.CreateUIObject("ColorImageHelper", horiGroup);

View File

@ -15,7 +15,7 @@ namespace UnityExplorer.UI.Panels
{ {
public override string Name => "C# Console"; public override string Name => "C# Console";
public override UIManager.Panels PanelType => UIManager.Panels.CSConsole; public override UIManager.Panels PanelType => UIManager.Panels.CSConsole;
public override int MinWidth => 400; public override int MinWidth => 750;
public override int MinHeight => 300; public override int MinHeight => 300;
public InputFieldScroller InputScroll { get; private set; } public InputFieldScroller InputScroll { get; private set; }
@ -62,36 +62,29 @@ namespace UnityExplorer.UI.Panels
{ {
mainPanelRect.localPosition = Vector2.zero; mainPanelRect.localPosition = Vector2.zero;
mainPanelRect.pivot = new Vector2(0f, 1f); mainPanelRect.pivot = new Vector2(0f, 1f);
mainPanelRect.anchorMin = new Vector2(0.4f, 0.1f); mainPanelRect.anchorMin = new Vector2(0.4f, 0.175f);
mainPanelRect.anchorMax = new Vector2(0.9f, 0.85f); mainPanelRect.anchorMax = new Vector2(0.85f, 0.925f);
} }
public override void ConstructPanelContent() public override void ConstructPanelContent()
{ {
#region TOP BAR // Tools Row
// Main group object var toolsRow = UIFactory.CreateHorizontalGroup(this.content, "ToggleRow", false, false, true, true, 5, new Vector4(8, 8, 10, 5),
default, TextAnchor.MiddleLeft);
var topBarObj = UIFactory.CreateHorizontalGroup(this.content, "TopBar", true, true, true, true, 10, new Vector4(8, 8, 30, 30), UIFactory.SetLayoutElement(toolsRow, minHeight: 25, flexibleHeight: 0, flexibleWidth: 9999);
default, TextAnchor.LowerCenter);
UIFactory.SetLayoutElement(topBarObj, minHeight: 50, flexibleHeight: 0);
//// Top label
//var topBarLabel = UIFactory.CreateLabel(topBarObj, "TopLabel", "C# Console", TextAnchor.MiddleLeft, default, true, 25);
//UIFactory.SetLayoutElement(topBarLabel.gameObject, preferredWidth: 150, flexibleWidth: 5000);
// Enable Ctrl+R toggle // Enable Ctrl+R toggle
var ctrlRToggleObj = UIFactory.CreateToggle(topBarObj, "CtrlRToggle", out var CtrlRToggle, out Text ctrlRToggleText); var ctrlRToggleObj = UIFactory.CreateToggle(toolsRow, "CtrlRToggle", out var CtrlRToggle, out Text ctrlRToggleText);
UIFactory.SetLayoutElement(ctrlRToggleObj, minWidth: 140, flexibleWidth: 0, minHeight: 25); UIFactory.SetLayoutElement(ctrlRToggleObj, minWidth: 150, flexibleWidth: 0, minHeight: 25);
ctrlRToggleText.alignment = TextAnchor.UpperLeft; ctrlRToggleText.alignment = TextAnchor.UpperLeft;
ctrlRToggleText.text = "Run on Ctrl+R"; ctrlRToggleText.text = "Compile on Ctrl+R";
CtrlRToggle.onValueChanged.AddListener((bool val) => { OnCtrlRToggled?.Invoke(val); }); CtrlRToggle.onValueChanged.AddListener((bool val) => { OnCtrlRToggled?.Invoke(val); });
// Enable Suggestions toggle // Enable Suggestions toggle
var suggestToggleObj = UIFactory.CreateToggle(topBarObj, "SuggestionToggle", out var SuggestionsToggle, out Text suggestToggleText); var suggestToggleObj = UIFactory.CreateToggle(toolsRow, "SuggestionToggle", out var SuggestionsToggle, out Text suggestToggleText);
UIFactory.SetLayoutElement(suggestToggleObj, minWidth: 120, flexibleWidth: 0, minHeight: 25); UIFactory.SetLayoutElement(suggestToggleObj, minWidth: 120, flexibleWidth: 0, minHeight: 25);
suggestToggleText.alignment = TextAnchor.UpperLeft; suggestToggleText.alignment = TextAnchor.UpperLeft;
suggestToggleText.text = "Suggestions"; suggestToggleText.text = "Suggestions";
@ -99,15 +92,25 @@ namespace UnityExplorer.UI.Panels
// Enable Auto-indent toggle // Enable Auto-indent toggle
var autoIndentToggleObj = UIFactory.CreateToggle(topBarObj, "IndentToggle", out var AutoIndentToggle, out Text autoIndentToggleText); var autoIndentToggleObj = UIFactory.CreateToggle(toolsRow, "IndentToggle", out var AutoIndentToggle, out Text autoIndentToggleText);
UIFactory.SetLayoutElement(autoIndentToggleObj, minWidth: 180, flexibleWidth: 0, minHeight: 25); UIFactory.SetLayoutElement(autoIndentToggleObj, minWidth: 180, flexibleWidth: 0, minHeight: 25);
autoIndentToggleText.alignment = TextAnchor.UpperLeft; autoIndentToggleText.alignment = TextAnchor.UpperLeft;
autoIndentToggleText.text = "Auto-indent"; autoIndentToggleText.text = "Auto-indent";
AutoIndentToggle.onValueChanged.AddListener((bool val) => { OnAutoIndentToggled?.Invoke(val); }); AutoIndentToggle.onValueChanged.AddListener((bool val) => { OnAutoIndentToggled?.Invoke(val); });
#endregion // Buttons
#region CONSOLE INPUT var resetButton = UIFactory.CreateButton(toolsRow, "ResetButton", "Reset", new Color(0.33f, 0.33f, 0.33f));
UIFactory.SetLayoutElement(resetButton.Component.gameObject, minHeight: 28, minWidth: 80, flexibleHeight: 0);
resetButton.ButtonText.fontSize = 15;
resetButton.OnClick += OnResetClicked;
var compileButton = UIFactory.CreateButton(toolsRow, "CompileButton", "Compile", new Color(0.33f, 0.5f, 0.33f));
UIFactory.SetLayoutElement(compileButton.Component.gameObject, minHeight: 28, minWidth: 130, flexibleHeight: 0);
compileButton.ButtonText.fontSize = 15;
compileButton.OnClick += OnCompileClicked;
// Console Input
int fontSize = 16; int fontSize = 16;
@ -140,24 +143,6 @@ namespace UnityExplorer.UI.Panels
Input.PlaceholderText.font = UIManager.ConsoleFont; Input.PlaceholderText.font = UIManager.ConsoleFont;
HighlightText.font = UIManager.ConsoleFont; HighlightText.font = UIManager.ConsoleFont;
#endregion
#region COMPILE BUTTON BAR
var horozGroupObj = UIFactory.CreateHorizontalGroup(this.content, "BigButtons", true, true, true, true, 0, new Vector4(2, 2, 2, 2),
new Color(1, 1, 1, 0));
var resetButton = UIFactory.CreateButton(horozGroupObj, "ResetButton", "Reset", new Color(0.33f, 0.33f, 0.33f));
UIFactory.SetLayoutElement(resetButton.Component.gameObject, minHeight: 45, minWidth: 80, flexibleHeight: 0);
resetButton.ButtonText.fontSize = 18;
resetButton.OnClick += OnResetClicked;
var compileButton = UIFactory.CreateButton(horozGroupObj, "CompileButton", "Compile", new Color(0.33f, 0.5f, 0.33f));
UIFactory.SetLayoutElement(compileButton.Component.gameObject, minHeight: 45, minWidth: 80, flexibleHeight: 0);
compileButton.ButtonText.fontSize = 18;
compileButton.OnClick += OnCompileClicked;
#endregion
} }
} }

View File

@ -19,7 +19,7 @@ namespace UnityExplorer.UI.Panels
public override string Name => "Inspector"; public override string Name => "Inspector";
public override UIManager.Panels PanelType => UIManager.Panels.Inspector; public override UIManager.Panels PanelType => UIManager.Panels.Inspector;
public override bool ShouldSaveActiveState => false; public override bool ShouldSaveActiveState => false;
public override int MinWidth => 625; public override int MinWidth => 800;
public override int MinHeight => 350; public override int MinHeight => 350;
public GameObject NavbarHolder; public GameObject NavbarHolder;

View File

@ -11,6 +11,8 @@ using UnityExplorer.UI.Widgets;
namespace UnityExplorer.UI.Panels namespace UnityExplorer.UI.Panels
{ {
// TODO move the logic out of this class into a LogUtil class (also move ExplorerCore.Log into that)
public class LogPanel : UIPanel, ICellPoolDataSource<ConsoleLogCell> public class LogPanel : UIPanel, ICellPoolDataSource<ConsoleLogCell>
{ {
public struct LogInfo public struct LogInfo
@ -29,6 +31,8 @@ namespace UnityExplorer.UI.Panels
public override int MinWidth => 300; public override int MinWidth => 300;
public override int MinHeight => 75; public override int MinHeight => 75;
public override bool ShouldSaveActiveState => true;
public override bool ShowByDefault => true;
public int ItemCount => Logs.Count; public int ItemCount => Logs.Count;
@ -156,8 +160,8 @@ namespace UnityExplorer.UI.Panels
{ {
mainPanelRect.localPosition = Vector2.zero; mainPanelRect.localPosition = Vector2.zero;
mainPanelRect.pivot = new Vector2(0f, 1f); mainPanelRect.pivot = new Vector2(0f, 1f);
mainPanelRect.anchorMin = new Vector2(0.5f, 0.1f); mainPanelRect.anchorMin = new Vector2(0.5f, 0.03f);
mainPanelRect.anchorMax = new Vector2(0.9f, 0.25f); mainPanelRect.anchorMax = new Vector2(0.9f, 0.2f);
} }
// UI Construction // UI Construction

View File

@ -11,12 +11,14 @@ using UnityExplorer.UI.Widgets;
namespace UnityExplorer.UI.Panels namespace UnityExplorer.UI.Panels
{ {
// TODO move the logic out of this class into ConfigManager
public class OptionsPanel : UIPanel, ICacheObjectController, ICellPoolDataSource<ConfigEntryCell> public class OptionsPanel : UIPanel, ICacheObjectController, ICellPoolDataSource<ConfigEntryCell>
{ {
public override string Name => "Options"; public override string Name => "Options";
public override UIManager.Panels PanelType => UIManager.Panels.Options; public override UIManager.Panels PanelType => UIManager.Panels.Options;
public override int MinWidth => 550; public override int MinWidth => 600;
public override int MinHeight => 200; public override int MinHeight => 200;
public override bool ShouldSaveActiveState => false; public override bool ShouldSaveActiveState => false;