Console singleton, dpi fixes, no linespacing

This commit is contained in:
GoaLitiuM
2021-04-03 03:17:25 +03:00
parent dc7fc99b03
commit ae8983a90e
5 changed files with 133 additions and 135 deletions

View File

@@ -23,38 +23,95 @@ namespace Cabrito
public static class Console
{
private static ConsoleScriptBase scriptInstance = null;
private static ConsoleInstance instance;
public static void Init()
{
if (instance != null)
instance.Dispose();
instance = new ConsoleInstance();
}
// Returns if Console window open right now.
public static bool IsOpen { get; internal set; }
public static bool IsOpen => instance.IsOpen;
// For debugging only: Returns true when Console was not closed during the same frame.
// Needed when Escape-key both closes the console and exits the game.
public static bool IsSafeToQuit { get { return stopwatch.Elapsed.TotalSeconds > 0.1; } }
private static Stopwatch stopwatch = Stopwatch.StartNew();
public static bool IsSafeToQuit => instance.IsSafeToQuit;
// Called when Console is opened.
public static Action OnOpen;
public static Action OnOpen
{
get { return instance.OnOpen; }
set { instance.OnOpen = value; }
}
// Called when Console is closed.
public static Action OnClose;
public static bool ShowExecutedLines = true;
public static string LinePrefix { get; internal set; } = "]";
public static Action OnClose
{
get { return instance.OnClose; }
set { instance.OnClose = value; }
}
public static bool ShowExecutedLines => instance.ShowExecutedLines;
public static string LinePrefix => instance.LinePrefix;
// Register script for handling the Console frontend.
internal static void RegisterConsoleScript(ConsoleScriptBase instance) => Console.instance.RegisterConsoleScript(instance);
/// Unregister already registered Console script.
internal static void UnregisterConsoleScript(ConsoleScriptBase instance) => Console.instance.UnregisterConsoleScript(instance);
public static IReadOnlyCollection<ConsoleLine> Lines => instance.Lines;
// Echoes text to Console
public static void Print(string text) => instance.Print(text);
// Echoes warning text to Console
public static void PrintWarning(string text) => instance.PrintWarning(text);
// Echoes error text to Console
public static void PrintError(string text) => instance.PrintError(text);
// Echoes developer/debug text to Console
public static void PrintDebug(string text) => instance.PrintDebug(text);
// Opens the Console
public static void Open() => instance.Open();
// Closes the Console
public static void Close() => instance.Close();
public static void ClearInput() => instance.ClearInput();
public static void Execute(string str) => instance.Execute(str);
public static string GetVariable(string variableName) => instance.GetVariable(variableName);
}
public class ConsoleInstance : IDisposable
{
private ConsoleScriptBase scriptInstance = null;
public bool IsOpen { get; internal set; } = true;
public bool IsSafeToQuit { get { return stopwatch.Elapsed.TotalSeconds > 0.1; } }
private Stopwatch stopwatch = Stopwatch.StartNew();
public Action OnOpen;
public Action OnClose;
public bool ShowExecutedLines = true;
public string LinePrefix { get; internal set; } = "]";
//private static List<string> consoleLines = new List<string>();
private static List<ConsoleLine> consoleLines = new List<ConsoleLine>();
private static Dictionary<string, ConsoleCommand> consoleCommands = new Dictionary<string, ConsoleCommand>();
private static Dictionary<string, ConsoleVariable> consoleVariables = new Dictionary<string, ConsoleVariable>();
private List<ConsoleLine> consoleLines = new List<ConsoleLine>();
private Dictionary<string, ConsoleCommand> consoleCommands = new Dictionary<string, ConsoleCommand>();
private Dictionary<string, ConsoleVariable> consoleVariables = new Dictionary<string, ConsoleVariable>();
// Initializes the Console system.
public static void Init()
public ConsoleInstance()
{
consoleLines.Clear();
consoleCommands.Clear();
consoleVariables.Clear();
AppDomain currentDomain = AppDomain.CurrentDomain;
Assembly[] assemblies = currentDomain.GetAssemblies();
@@ -172,8 +229,12 @@ namespace Cabrito
}
}
public void Dispose()
{
}
// Register script for handling the Console frontend.
internal static void RegisterConsoleScript(ConsoleScriptBase instance)
internal void RegisterConsoleScript(ConsoleScriptBase instance)
{
if (scriptInstance == instance)
return;
@@ -187,7 +248,7 @@ namespace Cabrito
}
/// Unregister already registered Console script.
internal static void UnregisterConsoleScript(ConsoleScriptBase instance)
internal void UnregisterConsoleScript(ConsoleScriptBase instance)
{
if (scriptInstance != instance)
return;
@@ -199,45 +260,45 @@ namespace Cabrito
OnClose -= instance.OnConsoleClose;
}
public static IReadOnlyCollection<ConsoleLine> Lines
public IReadOnlyCollection<ConsoleLine> Lines
{
get => consoleLines.AsReadOnly();
}
// Echoes text to Console.
public static void Print(string text)
// Echoes text to Console
public void Print(string text)
{
ConsoleLine line = new ConsoleLine(text);
consoleLines.Add(line);
scriptInstance?.OnLineAdded(text);
}
// Echoes warning text to Console.
public static void PrintWarning(string text)
// Echoes warning text to Console
public void PrintWarning(string text)
{
ConsoleLine line = new ConsoleLine(text);
consoleLines.Add(line);
scriptInstance?.OnLineAdded(text);
}
// Echoes error text to Console.
public static void PrintError(string text)
// Echoes error text to Console
public void PrintError(string text)
{
ConsoleLine line = new ConsoleLine(text);
consoleLines.Add(line);
scriptInstance?.OnLineAdded(text);
}
// Echoes developer/debug text to Console.
public static void PrintDebug(string text)
// Echoes developer/debug text to Console
public void PrintDebug(string text)
{
ConsoleLine line = new ConsoleLine(text);
consoleLines.Add(line);
scriptInstance?.OnLineAdded(text);
}
// Opens the Console.
public static void Open()
// Opens the Console
public void Open()
{
if (IsOpen)
return;
@@ -246,8 +307,8 @@ namespace Cabrito
OnOpen?.Invoke();
}
// Closes the Console;
public static void Close()
// Closes the Console
public void Close()
{
if (!IsOpen)
return;
@@ -258,12 +319,12 @@ namespace Cabrito
stopwatch.Restart();
}
public static void ClearInput()
public void ClearInput()
{
scriptInstance?.SetInput("");
}
public static void Execute(string str)
public void Execute(string str)
{
str = str.Trim();
@@ -297,7 +358,7 @@ namespace Cabrito
Console.Print("Unknown command '" + execute + "'");
}
public static string GetVariable(string variableName)
public string GetVariable(string variableName)
{
if (consoleVariables.TryGetValue(variableName, out ConsoleVariable cvar))
{