Console singleton, dpi fixes, no linespacing
This commit is contained in:
@@ -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))
|
||||
{
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace Cabrito
|
||||
set
|
||||
{
|
||||
heightMultiplier = value;
|
||||
Height = Screen.Size.Y * HeightMultiplier;
|
||||
UpdateHeight();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,7 +97,9 @@ namespace Cabrito
|
||||
if (font == null)
|
||||
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()
|
||||
@@ -108,14 +110,26 @@ namespace Cabrito
|
||||
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()
|
||||
{
|
||||
if (HeightMultiplier > 0)
|
||||
Height = Screen.Size.Y * HeightMultiplier;
|
||||
UpdateHeight();
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -29,26 +29,6 @@ namespace Cabrito
|
||||
[Limit(0f)]
|
||||
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 Texture BackgroundTexture;
|
||||
@@ -66,8 +46,6 @@ namespace Cabrito
|
||||
{
|
||||
Console.Init();
|
||||
|
||||
|
||||
|
||||
consoleInputEvent = new InputEvent("Console");
|
||||
consoleInputEvent.Triggered += OnConsoleInputEvent;
|
||||
|
||||
@@ -76,7 +54,7 @@ namespace Cabrito
|
||||
|
||||
FontReference fontReference = new FontReference(ConsoleFont, ConsoleFontSize);
|
||||
Font fontRaw = fontReference.GetFont();
|
||||
int fontHeight = fontRaw.Height;
|
||||
int fontHeight = (int)(fontRaw.Height / Platform.DpiScale);
|
||||
|
||||
// root actor which holds all the elements
|
||||
//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.SetAnchorPreset(AnchorPresets.HorizontalStretchTop, false);
|
||||
consoleBox.SetAnchorPreset(AnchorPresets.HorizontalStretchTop, true);
|
||||
//consoleBox.AnchorMax = new Vector2(1.0f, ConsoleHeight);
|
||||
consoleBox.Height = consoleSize.Y - fontHeight;
|
||||
//consoleBox.Height = consoleSize.Y - fontHeight;
|
||||
consoleBox.Font = fontReference;
|
||||
|
||||
//consoleBox.HorizontalAlignment = TextAlignment.Near;
|
||||
@@ -138,7 +116,6 @@ namespace Cabrito
|
||||
consoleBox.BackgroundSelectedFlashSpeed = 0;
|
||||
consoleBox.BorderSelectedColor = Color.Transparent;
|
||||
consoleBox.CaretFlashSpeed = 0;
|
||||
consoleBox.LineSpacing = LineSpacing;
|
||||
}
|
||||
|
||||
var locationFix = consoleBox.Location;
|
||||
@@ -167,7 +144,6 @@ namespace Cabrito
|
||||
consoleNotifyBox.BackgroundSelectedFlashSpeed = 0;
|
||||
consoleNotifyBox.BorderSelectedColor = Color.Transparent;
|
||||
consoleNotifyBox.CaretFlashSpeed = 0;
|
||||
consoleNotifyBox.LineSpacing = LineSpacing;
|
||||
}
|
||||
|
||||
var locationFix2 = consoleNotifyBox.Location;
|
||||
@@ -205,12 +181,6 @@ namespace Cabrito
|
||||
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);
|
||||
RefreshLayout();
|
||||
|
||||
@@ -393,11 +363,23 @@ namespace Cabrito
|
||||
Parent.As<UICanvas>().ReceivesEvents = false;
|
||||
}
|
||||
|
||||
bool firstUpdate = true;
|
||||
public override void OnUpdate()
|
||||
{
|
||||
Profiler.BeginEvent("ConsoleScript_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;
|
||||
if (!Console.IsOpen)
|
||||
targetY = -rootControl.Control.Height;
|
||||
@@ -445,7 +427,6 @@ namespace Cabrito
|
||||
}
|
||||
}
|
||||
}
|
||||
Profiler.EndEvent();
|
||||
}
|
||||
|
||||
public override void OnLineAdded(string text)
|
||||
|
||||
@@ -22,26 +22,6 @@ namespace Cabrito
|
||||
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>
|
||||
/// Gets or sets the font.
|
||||
/// </summary>
|
||||
@@ -128,12 +108,7 @@ namespace Cabrito
|
||||
if (font == null)
|
||||
return (int)Height;
|
||||
|
||||
return (int)Mathf.Round(LineSpacing * font.Height * Scale.Y);
|
||||
}
|
||||
|
||||
private float GetRealLineSpacing()
|
||||
{
|
||||
return GetFontHeight() * (1.0f - LineSpacing);
|
||||
return (int)Mathf.Round(font.Height * Scale.Y);
|
||||
}
|
||||
|
||||
public override void Clear()
|
||||
@@ -147,7 +122,7 @@ namespace Cabrito
|
||||
|
||||
public override void ResetViewOffset()
|
||||
{
|
||||
TargetViewOffset = new Vector2(0, GetRealLineSpacing());
|
||||
TargetViewOffset = new Vector2(0, 0);
|
||||
}
|
||||
|
||||
/*public void ScrollToEnd()
|
||||
@@ -167,12 +142,9 @@ namespace Cabrito
|
||||
Rectangle caretBounds = CaretBounds;
|
||||
|
||||
float maxY = TextSize.Y - Height;
|
||||
float spacing = GetRealLineSpacing();
|
||||
maxY += spacing;
|
||||
|
||||
Vector2 newLocation = CaretBounds.Location;
|
||||
newLocation.Y += spacing;
|
||||
TargetViewOffset = Vector2.Clamp(newLocation, new Vector2(0, spacing), new Vector2(_targetViewOffset.X, maxY));
|
||||
TargetViewOffset = Vector2.Clamp(newLocation, new Vector2(0, 0), new Vector2(_targetViewOffset.X, maxY));
|
||||
}
|
||||
|
||||
/*const bool smoothScrolling = false;
|
||||
@@ -346,7 +318,6 @@ namespace Cabrito
|
||||
Vector2 leftEdge = font.GetCharPosition(text, SelectionLeft + TextPrefix.Length, ref _layout);
|
||||
Vector2 rightEdge = font.GetCharPosition(text, SelectionRight + TextPrefix.Length, ref _layout);
|
||||
float fontHeight = GetFontHeight();
|
||||
float spacing = GetRealLineSpacing();
|
||||
|
||||
// Draw selection background
|
||||
float alpha = Mathf.Min(1.0f, Mathf.Cos(_animateTime * BackgroundSelectedFlashSpeed) * 0.5f + 1.3f);
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user