konsoli rewörk

This commit is contained in:
GoaLitiuM
2021-05-18 21:39:05 +03:00
parent 515b9f3bde
commit f3c3225442
5 changed files with 106 additions and 83 deletions

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using FlaxEngine.Assertions;
namespace Cabrito
{
@@ -24,14 +25,22 @@ namespace Cabrito
public static class Console
{
private static ConsoleInstance instance;
public static void Init()
{
if (instance != null)
instance.Dispose();
Destroy();
instance = new ConsoleInstance();
}
private static void Destroy()
{
if (instance != null)
{
instance.Dispose();
instance = null;
FlaxEngine.Debug.Log("Console.Destroy");
}
}
// Returns if Console window open right now.
public static bool IsOpen => instance.IsOpen;
@@ -53,15 +62,16 @@ namespace Cabrito
set { instance.OnClose = value; }
}
// Called when a line of text was printed in Console.
public static Action<string> OnPrint
{
get { return instance.OnPrint; }
set { instance.OnPrint = 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
@@ -82,8 +92,6 @@ namespace Cabrito
// 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);
@@ -91,8 +99,6 @@ namespace Cabrito
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; } }
@@ -100,6 +106,7 @@ namespace Cabrito
public Action OnOpen;
public Action OnClose;
public Action<string> OnPrint;
public bool ShowExecutedLines = true;
public string LinePrefix { get; internal set; } = "]";
@@ -233,33 +240,6 @@ namespace Cabrito
{
}
// Register script for handling the Console frontend.
internal void RegisterConsoleScript(ConsoleScriptBase instance)
{
if (scriptInstance == instance)
return;
scriptInstance = instance;
OnOpen += instance.OnConsoleOpen;
OnClose += instance.OnConsoleClose;
//foreach (var line in consoleLines)
// scriptInstance.OnLineAdded(line);
}
/// Unregister already registered Console script.
internal void UnregisterConsoleScript(ConsoleScriptBase instance)
{
if (scriptInstance != instance)
return;
Close();
scriptInstance = null;
OnOpen -= instance.OnConsoleOpen;
OnClose -= instance.OnConsoleClose;
}
public IReadOnlyCollection<ConsoleLine> Lines
{
get => consoleLines.AsReadOnly();
@@ -270,7 +250,7 @@ namespace Cabrito
{
ConsoleLine line = new ConsoleLine(text);
consoleLines.Add(line);
scriptInstance?.OnLineAdded(text);
OnPrint?.Invoke(text);
}
// Echoes warning text to Console
@@ -278,7 +258,7 @@ namespace Cabrito
{
ConsoleLine line = new ConsoleLine(text);
consoleLines.Add(line);
scriptInstance?.OnLineAdded(text);
OnPrint?.Invoke(text);
}
// Echoes error text to Console
@@ -286,7 +266,7 @@ namespace Cabrito
{
ConsoleLine line = new ConsoleLine(text);
consoleLines.Add(line);
scriptInstance?.OnLineAdded(text);
OnPrint?.Invoke(text);
}
// Echoes developer/debug text to Console
@@ -294,7 +274,7 @@ namespace Cabrito
{
ConsoleLine line = new ConsoleLine(text);
consoleLines.Add(line);
scriptInstance?.OnLineAdded(text);
OnPrint?.Invoke(text);
}
// Opens the Console
@@ -319,11 +299,6 @@ namespace Cabrito
stopwatch.Restart();
}
public void ClearInput()
{
scriptInstance?.SetInput("");
}
public void Execute(string str)
{
str = str.Trim();

View File

@@ -22,7 +22,7 @@ namespace Cabrito
}
public ConsoleInputTextBox(ConsoleContentTextBox contentBox, float x, float y, float width, float height) : base(x, y, width, height)
{
{
this.contentBox = contentBox;
IsMultiline = true; // Not really but behaves better than single-line box
}
@@ -30,6 +30,8 @@ namespace Cabrito
private bool IsConsoleKeyPressed(KeyboardKeys key = KeyboardKeys.None)
{
// Ignore any characters generated by the key which opens the console
string inputTextLower = Input.InputText.ToLowerInvariant();
IEnumerable<ActionConfig> consoleKeyMappings;
if (key == KeyboardKeys.None)
consoleKeyMappings = Input.ActionMappings.Where(x => x.Name == "Console" && x.Key != KeyboardKeys.None);
@@ -37,19 +39,22 @@ namespace Cabrito
consoleKeyMappings = Input.ActionMappings.Where(x => x.Name == "Console" && x.Key == key);
foreach (var mapping in consoleKeyMappings)
{
if (Input.InputText.Length > 0)
if (inputTextLower.Length > 0)
{
if ((mapping.Key == KeyboardKeys.Backslash || mapping.Key == KeyboardKeys.BackQuote) &&
(Input.InputText.ToLowerInvariant().Contains('ö') ||
Input.InputText.ToLowerInvariant().Contains('æ') ||
Input.InputText.ToLowerInvariant().Contains('ø')))
(inputTextLower.Contains('ö') ||
inputTextLower.Contains('æ') ||
inputTextLower.Contains('ø')))
{
continue; // Scandinavian keyboard layouts
}
else if (mapping.Key == KeyboardKeys.BackQuote && Input.InputText.ToLowerInvariant().Contains('\''))
else if (mapping.Key == KeyboardKeys.BackQuote && inputTextLower.Contains('\''))
continue;
else if (mapping.Key == KeyboardKeys.Backslash && (Input.InputText.ToLowerInvariant().Contains('\\') || Input.InputText.ToLowerInvariant().Contains('|')))
else if (mapping.Key == KeyboardKeys.Backslash &&
(inputTextLower.Contains('\\') || inputTextLower.Contains('|')))
{
continue;
}
}
if (Input.GetKey(mapping.Key))

View File

@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using FlaxEngine;
using Console = Cabrito.Console;
namespace Game
{
public class ConsolePlugin : GamePlugin
{
public ConsolePlugin()
{
Console.Init();
Console.Print("ConsolePlugin ctor");
}
public override void Initialize()
{
base.Initialize();
Console.Print("ConsolePlugin initialize");
}
public override void Deinitialize()
{
base.Deinitialize();
FlaxEngine.Debug.Log("ConsolePlugin Deinitialize");
}
public override PluginDescription Description => new PluginDescription()
{
Author = "Ari Vuollet",
Name = "Console",
Description = "Quake-like console",
Version = Version.Parse("0.1.0"),
IsAlpha = true,
};
}
}

View File

@@ -7,15 +7,7 @@ using FlaxEngine.GUI;
namespace Cabrito
{
public abstract class ConsoleScriptBase : Script
{
public abstract void OnConsoleOpen();
public abstract void OnConsoleClose();
public abstract void SetInput(string text);
public abstract void OnLineAdded(string text);
}
public class ConsoleScript : ConsoleScriptBase
public class ConsoleScript : Script
{
[Limit(5, 720, 1)]
public int ConsoleFontSize = 16;
@@ -44,8 +36,7 @@ namespace Cabrito
public override void OnStart()
{
Console.Init();
Console.Print("ConsoleScript OnStart");
consoleInputEvent = new InputEvent("Console");
consoleInputEvent.Triggered += OnConsoleInputEvent;
@@ -90,7 +81,6 @@ namespace Cabrito
};
contentContainer.SetAnchorPreset(AnchorPresets.StretchAll, true);
var contentContainerControl = rootControl.AddChild<UIControl>();
contentContainerControl.Name = "ContentContainer";
contentContainerControl.Control = contentContainer;
@@ -152,7 +142,6 @@ namespace Cabrito
parentControl2.Control = consoleNotifyBox;
consoleNotifyBox.Location = locationFix2; // workaround to UIControl.Control overriding the old position
}
{
if (consoleInputBox == null)
{
@@ -174,14 +163,17 @@ namespace Cabrito
}
containerControl.inputBox = consoleInputBox;
var locationFix = consoleInputBox.Location;
var parentControl = contentContainerControl.AddChild<UIControl>();
parentControl.Name = "ConsoleInput";
parentControl.Control = consoleInputBox;
consoleInputBox.Location = locationFix; // workaround to UIControl.Control overriding the old position
}
Console.RegisterConsoleScript(this);
consoleInputBox.Location = locationFix; // workaround to UIControl.Control overriding the old position
}
#if false
//for (int i = 0; i < 10; i++)
@@ -255,39 +247,44 @@ namespace Cabrito
+ "y long long long line in repeat a very very very 2 long long long line in repeat a very very very 3 long long"
+ " long line in repeat a very very very long long long 4 line in repeat");
Console.Print("another normal line");*/
Debug.Logger.LogHandler.SendLog += OnSendLog;
Debug.Logger.LogHandler.SendExceptionLog += OnSendExceptionLog;
Console.OnOpen += () => { Screen.CursorVisible = true; Screen.CursorLock = CursorLockMode.None; };
Console.OnClose += () => { Screen.CursorVisible = false; Screen.CursorLock = CursorLockMode.Locked; };
Console.OnOpen += OnConsoleOpen;
Console.OnClose += OnConsoleClose;
Console.OnPrint += OnPrint;
// hide console by default, and close it instantly
Console.Close();
var rootlocation = rootControl.Control.Location;
rootlocation.Y = -rootControl.Control.Height;
rootControl.Control.Location = rootlocation;
Console.Print("console open: " + Console.IsOpen.ToString());
}
private void OnSendLog(LogType level, string msg, FlaxEngine.Object obj, string stackTrace)
{
Console.Print("[DEBUG] " + msg);
//Console.Print("[DEBUG] " + msg);
}
private void OnSendExceptionLog(Exception exception, FlaxEngine.Object obj)
{
Console.Print("[EXCEP] " + exception.Message);
//Console.Print("[EXCEP] " + exception.Message);
}
public override void OnDestroy()
{
Console.UnregisterConsoleScript(this);
//consoleInputEvent.Triggered -= OnConsoleInputEvent;
consoleInputEvent?.Dispose();
consoleBox?.Dispose();
consoleNotifyBox?.Dispose();
Console.OnOpen -= OnConsoleOpen;
Console.OnClose -= OnConsoleClose;
Console.OnPrint -= OnPrint;
Debug.Logger.LogHandler.SendLog -= OnSendLog;
Debug.Logger.LogHandler.SendExceptionLog -= OnSendExceptionLog;
}
@@ -323,14 +320,20 @@ namespace Cabrito
Console.Close();
}
public override void OnConsoleOpen()
public void OnConsoleOpen()
{
Screen.CursorVisible = true;
Screen.CursorLock = CursorLockMode.None;
consoleInputBox.Focus();
Parent.As<UICanvas>().ReceivesEvents = true;
}
public override void OnConsoleClose()
public void OnConsoleClose()
{
Screen.CursorVisible = false;
Screen.CursorLock = CursorLockMode.Locked;
consoleInputBox.Defocus();
#if FLAX_EDITOR
Editor.Instance.Windows.GameWin.Focus();
@@ -392,13 +395,13 @@ namespace Cabrito
}
}
public override void OnLineAdded(string text)
public void OnPrint(string text)
{
int fontHeight = (int)(consoleNotifyBox.Font.GetFont().Height / Platform.DpiScale);
consoleNotifyBox.Height = Math.Min(ConsoleNotifyLines, Console.Lines.Count) * fontHeight;
}
public override void SetInput(string text)
public void SetInput(string text)
{
consoleInputBox.Text = text;
}

View File

@@ -64,10 +64,13 @@ namespace Cabrito
_layout.TextWrapping = TextWrapping.NoWrap;
_layout.Bounds = new Rectangle(0, 0, Width, Height);//new Rectangle(DefaultMargin, 1, Width - 2 * DefaultMargin, Height - 2);
#if FLAX_EDITOR
var style = Style.Current;
Font = new FontReference(style.FontMedium);
if (style.FontMedium != null)
Font = new FontReference(style.FontMedium);
TextColor = style.Foreground;
SelectionColor = style.BackgroundSelected;
#endif
}
/*protected override void SetText(string value)