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

View File

@@ -48,7 +48,7 @@ namespace Cabrito
set set
{ {
heightMultiplier = value; heightMultiplier = value;
Height = Screen.Size.Y * HeightMultiplier; UpdateHeight();
} }
} }
@@ -97,7 +97,9 @@ namespace Cabrito
if (font == null) if (font == null)
return (int)Height; return (int)Height;
return (int)Mathf.Round(LineSpacing * (font.Height / Platform.DpiScale) * Scale.Y); int h = (int)Mathf.Round(LineSpacing * (font.Height / Platform.DpiScale) * Scale.Y);
Console.Print("height: " + LineSpacing.ToString());
return h;
} }
private int GetHeightInLines() private int GetHeightInLines()
@@ -108,14 +110,26 @@ namespace Cabrito
return (int)(Height / (font.Height / Platform.DpiScale)); // number of fully visible lines return (int)(Height / (font.Height / Platform.DpiScale)); // number of fully visible lines
} }
protected override void OnParentChangedInternal()
{
base.OnParentChangedInternal();
if (Parent != null)
OnParentResized();
}
public override void OnParentResized() public override void OnParentResized()
{ {
if (HeightMultiplier > 0) UpdateHeight();
Height = Screen.Size.Y * HeightMultiplier;
base.OnParentResized(); base.OnParentResized();
} }
private void UpdateHeight()
{
if (Parent != null && Parent.Parent != null)
Height = (Parent.Parent.Size.Y * HeightMultiplier) - GetFontHeight();
}
private void CalculateVisibleLines(IReadOnlyCollection<ConsoleLine> lines, out int firstVisibleLine, out int lastVisibleLine, out LineInfo[] wrappedLines) private void CalculateVisibleLines(IReadOnlyCollection<ConsoleLine> lines, out int firstVisibleLine, out int lastVisibleLine, out LineInfo[] wrappedLines)
{ {

View File

@@ -29,26 +29,6 @@ namespace Cabrito
[Limit(0f)] [Limit(0f)]
public float ConsoleSpeed = 3500f; public float ConsoleSpeed = 3500f;
[Limit(0.5f, 4f, 0.05f)]
public float LineSpacing
{
get => lineSpacing;
set
{
lineSpacing = value;
if (consoleBox != null)
consoleBox.LineSpacing = value;
if (consoleNotifyBox != null)
consoleNotifyBox.LineSpacing = value;
if (consoleInputBox != null)
consoleInputBox.LineSpacing = value;
RefreshLayout();
}
}
private float lineSpacing;
public FontAsset ConsoleFont; public FontAsset ConsoleFont;
public Texture BackgroundTexture; public Texture BackgroundTexture;
@@ -66,8 +46,6 @@ namespace Cabrito
{ {
Console.Init(); Console.Init();
consoleInputEvent = new InputEvent("Console"); consoleInputEvent = new InputEvent("Console");
consoleInputEvent.Triggered += OnConsoleInputEvent; consoleInputEvent.Triggered += OnConsoleInputEvent;
@@ -76,7 +54,7 @@ namespace Cabrito
FontReference fontReference = new FontReference(ConsoleFont, ConsoleFontSize); FontReference fontReference = new FontReference(ConsoleFont, ConsoleFontSize);
Font fontRaw = fontReference.GetFont(); Font fontRaw = fontReference.GetFont();
int fontHeight = fontRaw.Height; int fontHeight = (int)(fontRaw.Height / Platform.DpiScale);
// root actor which holds all the elements // root actor which holds all the elements
//var rootContainerControl = new ContainerControl(new Rectangle(0, 0, screenSize.X, screenSize.Y)); //var rootContainerControl = new ContainerControl(new Rectangle(0, 0, screenSize.X, screenSize.Y));
@@ -124,9 +102,9 @@ namespace Cabrito
consoleBox = new ConsoleContentTextBox(null, 0, 0, 0, 0); consoleBox = new ConsoleContentTextBox(null, 0, 0, 0, 0);
consoleBox.SetAnchorPreset(AnchorPresets.HorizontalStretchTop, false); consoleBox.SetAnchorPreset(AnchorPresets.HorizontalStretchTop, true);
//consoleBox.AnchorMax = new Vector2(1.0f, ConsoleHeight); //consoleBox.AnchorMax = new Vector2(1.0f, ConsoleHeight);
consoleBox.Height = consoleSize.Y - fontHeight; //consoleBox.Height = consoleSize.Y - fontHeight;
consoleBox.Font = fontReference; consoleBox.Font = fontReference;
//consoleBox.HorizontalAlignment = TextAlignment.Near; //consoleBox.HorizontalAlignment = TextAlignment.Near;
@@ -138,7 +116,6 @@ namespace Cabrito
consoleBox.BackgroundSelectedFlashSpeed = 0; consoleBox.BackgroundSelectedFlashSpeed = 0;
consoleBox.BorderSelectedColor = Color.Transparent; consoleBox.BorderSelectedColor = Color.Transparent;
consoleBox.CaretFlashSpeed = 0; consoleBox.CaretFlashSpeed = 0;
consoleBox.LineSpacing = LineSpacing;
} }
var locationFix = consoleBox.Location; var locationFix = consoleBox.Location;
@@ -167,7 +144,6 @@ namespace Cabrito
consoleNotifyBox.BackgroundSelectedFlashSpeed = 0; consoleNotifyBox.BackgroundSelectedFlashSpeed = 0;
consoleNotifyBox.BorderSelectedColor = Color.Transparent; consoleNotifyBox.BorderSelectedColor = Color.Transparent;
consoleNotifyBox.CaretFlashSpeed = 0; consoleNotifyBox.CaretFlashSpeed = 0;
consoleNotifyBox.LineSpacing = LineSpacing;
} }
var locationFix2 = consoleNotifyBox.Location; var locationFix2 = consoleNotifyBox.Location;
@@ -205,12 +181,6 @@ namespace Cabrito
consoleInputBox.Location = locationFix; // workaround to UIControl.Control overriding the old position consoleInputBox.Location = locationFix; // workaround to UIControl.Control overriding the old position
} }
// close instantly
var rootlocation = rootControl.Control.Location;
rootlocation.Y = -rootControl.Control.Height;
rootControl.Control.Location = rootlocation;
OnConsoleClose();
Console.RegisterConsoleScript(this); Console.RegisterConsoleScript(this);
RefreshLayout(); RefreshLayout();
@@ -393,11 +363,23 @@ namespace Cabrito
Parent.As<UICanvas>().ReceivesEvents = false; Parent.As<UICanvas>().ReceivesEvents = false;
} }
bool firstUpdate = true;
public override void OnUpdate() public override void OnUpdate()
{ {
Profiler.BeginEvent("ConsoleScript_OnUpdate");
base.OnUpdate(); base.OnUpdate();
// HACK: workaround for cursor not getting hidden in editor after entering play mode
if (firstUpdate)
{
firstUpdate = false;
Console.Close();
// hide console by default
var rootlocation = rootControl.Control.Location;
rootlocation.Y = -rootControl.Control.Height;
rootControl.Control.Location = rootlocation;
}
float targetY; float targetY;
if (!Console.IsOpen) if (!Console.IsOpen)
targetY = -rootControl.Control.Height; targetY = -rootControl.Control.Height;
@@ -445,7 +427,6 @@ namespace Cabrito
} }
} }
} }
Profiler.EndEvent();
} }
public override void OnLineAdded(string text) public override void OnLineAdded(string text)

View File

@@ -22,26 +22,6 @@ namespace Cabrito
set => _layout.TextWrapping = value; set => _layout.TextWrapping = value;
} }
[EditorDisplay("Style"), EditorOrder(2000), Tooltip("The line spacing of the text.")]
public float LineSpacing
{
get => _layout.BaseLinesGapScale;
set
{
// Round to nearest pixel in order to avoid uneven line heights
float newValue = value;
var font = Font.GetFont();
if (font != null)
{
float actualHeight = font.Height * Scale.Y;
newValue = Mathf.Round(newValue * actualHeight) / actualHeight;
}
_layout.BaseLinesGapScale = newValue;
OnTextChanged();
}
}
/// <summary> /// <summary>
/// Gets or sets the font. /// Gets or sets the font.
/// </summary> /// </summary>
@@ -128,12 +108,7 @@ namespace Cabrito
if (font == null) if (font == null)
return (int)Height; return (int)Height;
return (int)Mathf.Round(LineSpacing * font.Height * Scale.Y); return (int)Mathf.Round(font.Height * Scale.Y);
}
private float GetRealLineSpacing()
{
return GetFontHeight() * (1.0f - LineSpacing);
} }
public override void Clear() public override void Clear()
@@ -147,7 +122,7 @@ namespace Cabrito
public override void ResetViewOffset() public override void ResetViewOffset()
{ {
TargetViewOffset = new Vector2(0, GetRealLineSpacing()); TargetViewOffset = new Vector2(0, 0);
} }
/*public void ScrollToEnd() /*public void ScrollToEnd()
@@ -167,12 +142,9 @@ namespace Cabrito
Rectangle caretBounds = CaretBounds; Rectangle caretBounds = CaretBounds;
float maxY = TextSize.Y - Height; float maxY = TextSize.Y - Height;
float spacing = GetRealLineSpacing();
maxY += spacing;
Vector2 newLocation = CaretBounds.Location; Vector2 newLocation = CaretBounds.Location;
newLocation.Y += spacing; TargetViewOffset = Vector2.Clamp(newLocation, new Vector2(0, 0), new Vector2(_targetViewOffset.X, maxY));
TargetViewOffset = Vector2.Clamp(newLocation, new Vector2(0, spacing), new Vector2(_targetViewOffset.X, maxY));
} }
/*const bool smoothScrolling = false; /*const bool smoothScrolling = false;
@@ -346,7 +318,6 @@ namespace Cabrito
Vector2 leftEdge = font.GetCharPosition(text, SelectionLeft + TextPrefix.Length, ref _layout); Vector2 leftEdge = font.GetCharPosition(text, SelectionLeft + TextPrefix.Length, ref _layout);
Vector2 rightEdge = font.GetCharPosition(text, SelectionRight + TextPrefix.Length, ref _layout); Vector2 rightEdge = font.GetCharPosition(text, SelectionRight + TextPrefix.Length, ref _layout);
float fontHeight = GetFontHeight(); float fontHeight = GetFontHeight();
float spacing = GetRealLineSpacing();
// Draw selection background // Draw selection background
float alpha = Mathf.Min(1.0f, Mathf.Cos(_animateTime * BackgroundSelectedFlashSpeed) * 0.5f + 1.3f); float alpha = Mathf.Min(1.0f, Mathf.Cos(_animateTime * BackgroundSelectedFlashSpeed) * 0.5f + 1.3f);

View File

@@ -1,29 +0,0 @@
using System;
using System.Collections.Generic;
using FlaxEngine;
namespace Game
{
public class TestScript : Script
{
public override void OnStart()
{
// Here you can add code that needs to be called when script is created, just before the first game update
}
public override void OnEnable()
{
// Here you can add code that needs to be called when script is enabled (eg. register for events)
}
public override void OnDisable()
{
// Here you can add code that needs to be called when script is disabled (eg. unregister from events)
}
public override void OnUpdate()
{
// Here you can add code that needs to be called every frame
}
}
}