From 26893a8f57343968512d29de0481a4265e9961ce Mon Sep 17 00:00:00 2001 From: GoaLitiuM Date: Sun, 14 Mar 2021 01:00:08 +0200 Subject: [PATCH] console notify box --- Content/Scenes/MainScene.scene | 6 +- Source/Game/Cabrito/Console/Console.cs | 44 +++++++--- .../Cabrito/Console/ConsoleContentTextBox.cs | 14 ++-- .../Cabrito/Console/ConsoleInputTextBox.cs | 7 +- Source/Game/Cabrito/Console/ConsoleScript.cs | 84 ++++++++++++++++--- .../Cabrito/Console/ConsoleTextBoxBase.cs | 8 +- 6 files changed, 123 insertions(+), 40 deletions(-) diff --git a/Content/Scenes/MainScene.scene b/Content/Scenes/MainScene.scene index aaa2f0b..d043b34 100644 --- a/Content/Scenes/MainScene.scene +++ b/Content/Scenes/MainScene.scene @@ -40,7 +40,7 @@ "V": { "Order": -999999999, "Size": { - "X": 1456.0, + "X": 1401.0, "Y": 813.0 } } @@ -138,7 +138,7 @@ }, "V": { "Size": { - "X": 1456.0, + "X": 1401.0, "Y": 813.0 } } @@ -165,7 +165,7 @@ }, "Control": "FlaxEngine.GUI.Label", "Data": { - "Text": "FPS: 15\nrFPS: 11\nCon: NaNms\nDirectX11\nGC memory: 13.52053MB", + "Text": "FPS: 767\nrFPS: 767\nCon: NaNms\nDirectX11\nGC memory: 7.908752MB", "TextColor": { "R": 1.0, "G": 1.0, diff --git a/Source/Game/Cabrito/Console/Console.cs b/Source/Game/Cabrito/Console/Console.cs index 3cdfece..e7cb3e7 100644 --- a/Source/Game/Cabrito/Console/Console.cs +++ b/Source/Game/Cabrito/Console/Console.cs @@ -6,6 +6,21 @@ using System.Reflection; namespace Cabrito { + public class ConsoleLine + { + public string content; + + internal ConsoleLine(string line) + { + content = line; + } + + public static implicit operator string(ConsoleLine line) => line.content; + public static explicit operator ConsoleLine(string line) => new ConsoleLine(line); + + public override string ToString() => content.ToString(); + } + public static class Console { private static ConsoleScriptBase scriptInstance = null; @@ -28,7 +43,8 @@ namespace Cabrito public static string LinePrefix { get; internal set; } = "]"; - private static List consoleLines = new List(); + //private static List consoleLines = new List(); + private static List consoleLines = new List(); private static Dictionary consoleCommands = new Dictionary(); private static Dictionary consoleVariables = new Dictionary(); @@ -166,8 +182,8 @@ namespace Cabrito OnOpen += instance.OnConsoleOpen; OnClose += instance.OnConsoleClose; - foreach (var line in consoleLines) - scriptInstance.AddLine(line); + //foreach (var line in consoleLines) + // scriptInstance.OnLineAdded(line); } /// Unregister already registered Console script. @@ -183,7 +199,7 @@ namespace Cabrito OnClose -= instance.OnConsoleClose; } - public static IReadOnlyCollection Lines + public static IReadOnlyCollection Lines { get => consoleLines.AsReadOnly(); } @@ -191,29 +207,33 @@ namespace Cabrito // Echoes text to Console. public static void Print(string text) { - consoleLines.Add(text); - scriptInstance?.AddLine(text); + ConsoleLine line = new ConsoleLine(text); + consoleLines.Add(line); + scriptInstance?.OnLineAdded(text); } // Echoes warning text to Console. public static void PrintWarning(string text) { - consoleLines.Add(text); - scriptInstance?.AddLine(text); + ConsoleLine line = new ConsoleLine(text); + consoleLines.Add(line); + scriptInstance?.OnLineAdded(text); } // Echoes error text to Console. public static void PrintError(string text) { - consoleLines.Add(text); - scriptInstance?.AddLine(text); + ConsoleLine line = new ConsoleLine(text); + consoleLines.Add(line); + scriptInstance?.OnLineAdded(text); } // Echoes developer/debug text to Console. public static void PrintDebug(string text) { - consoleLines.Add(text); - scriptInstance?.AddLine(text); + ConsoleLine line = new ConsoleLine(text); + consoleLines.Add(line); + scriptInstance?.OnLineAdded(text); } // Opens the Console. diff --git a/Source/Game/Cabrito/Console/ConsoleContentTextBox.cs b/Source/Game/Cabrito/Console/ConsoleContentTextBox.cs index 8493050..2cfebb6 100644 --- a/Source/Game/Cabrito/Console/ConsoleContentTextBox.cs +++ b/Source/Game/Cabrito/Console/ConsoleContentTextBox.cs @@ -36,6 +36,7 @@ namespace Cabrito public float CaretFlashSpeed = 0; public Color BorderColor; public Color TextColor = Color.White; + public int DefaultMargin = 1; public int ScrollOffset = 0; public int ScrollMouseLines = 3; @@ -110,12 +111,13 @@ namespace Cabrito public override void OnParentResized() { - Height = Screen.Size.Y * HeightMultiplier; + if (HeightMultiplier > 0) + Height = Screen.Size.Y * HeightMultiplier; base.OnParentResized(); } - private void CalculateVisibleLines(IReadOnlyCollection lines, out int firstVisibleLine, out int lastVisibleLine, out LineInfo[] wrappedLines) + private void CalculateVisibleLines(IReadOnlyCollection lines, out int firstVisibleLine, out int lastVisibleLine, out LineInfo[] wrappedLines) { wrappedLines = null; firstVisibleLine = 0; @@ -309,7 +311,7 @@ namespace Cabrito foreach (LineInfo li in wrappedLines) { var lineIndex = li.lineIndex; - string line = lines.ElementAt(lineIndex).Substring(li.lineOffset, li.lineLength); + string line = lines.ElementAt(lineIndex).content.Substring(li.lineOffset, li.lineLength); Render2D.DrawText(font, line, TextColor, ref layout); layout.Bounds.Y += lineHeight; } @@ -383,7 +385,7 @@ namespace Cabrito return false; hitLine = wrappedLines[hitWrappedLine].lineIndex; - string line = lines.ElementAt(hitLine).Substring(wrappedLines[hitWrappedLine].lineOffset, wrappedLines[hitWrappedLine].lineLength); + string line = lines.ElementAt(hitLine).content.Substring(wrappedLines[hitWrappedLine].lineOffset, wrappedLines[hitWrappedLine].lineLength); layout.Bounds.Y = top + ((hitWrappedLine) * lineHeight); layout.Bounds.Height = top + 9999;//(visibleHeight / Platform.DpiScale); @@ -474,7 +476,7 @@ namespace Cabrito selectionStartChar = hitChar; selectionEndLine = hitLine; selectionEndChar = hitChar; - FlaxEngine.Debug.Log(string.Format("start line {0} char {1}", hitLine, hitChar)); + //FlaxEngine.Debug.Log(string.Format("start line {0} char {1}", hitLine, hitChar)); } else throw new Exception("no???"); @@ -507,7 +509,7 @@ namespace Cabrito { selectionEndLine = hitLine; selectionEndChar = hitChar; - FlaxEngine.Debug.Log(string.Format("end line {0} char {1}", hitLine, hitChar)); + //FlaxEngine.Debug.Log(string.Format("end line {0} char {1}", hitLine, hitChar)); } } } diff --git a/Source/Game/Cabrito/Console/ConsoleInputTextBox.cs b/Source/Game/Cabrito/Console/ConsoleInputTextBox.cs index 4a75ec5..0c72d86 100644 --- a/Source/Game/Cabrito/Console/ConsoleInputTextBox.cs +++ b/Source/Game/Cabrito/Console/ConsoleInputTextBox.cs @@ -13,13 +13,16 @@ namespace Cabrito private ConsoleContentTextBox contentBox; + protected override Rectangle TextRectangle => new Rectangle(0, 0, Width, Height); + protected override Rectangle TextClipRectangle => new Rectangle(0, 0, Width, Height); + public ConsoleInputTextBox() : base() { } 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 } @@ -41,7 +44,7 @@ namespace Cabrito Input.InputText.ToLowerInvariant().Contains('æ') || Input.InputText.ToLowerInvariant().Contains('ø'))) { - continue; // Scandinavian keyboards layouts + continue; // Scandinavian keyboard layouts } else if (mapping.Key == KeyboardKeys.BackQuote && Input.InputText.ToLowerInvariant().Contains('\'')) continue; diff --git a/Source/Game/Cabrito/Console/ConsoleScript.cs b/Source/Game/Cabrito/Console/ConsoleScript.cs index 5c0491e..2feb10f 100644 --- a/Source/Game/Cabrito/Console/ConsoleScript.cs +++ b/Source/Game/Cabrito/Console/ConsoleScript.cs @@ -12,7 +12,7 @@ namespace Cabrito public abstract void OnConsoleOpen(); public abstract void OnConsoleClose(); public abstract void SetInput(string text); - public abstract void AddLine(string text); + public abstract void OnLineAdded(string text); } public class ConsoleScript : ConsoleScriptBase @@ -23,6 +23,12 @@ namespace Cabrito [Limit(0.05f, 1.0f, 1)] public float ConsoleHeight = 0.65f; + [Limit(0)] + public int ConsoleNotifyLines = 3; + + [Limit(0f)] + public float ConsoleSpeed = 3500f; + [Limit(0.5f, 4f, 0.05f)] public float LineSpacing { @@ -33,6 +39,8 @@ namespace Cabrito if (consoleBox != null) consoleBox.LineSpacing = value; + if (consoleNotifyBox != null) + consoleNotifyBox.LineSpacing = value; if (consoleInputBox != null) consoleInputBox.LineSpacing = value; @@ -50,6 +58,7 @@ namespace Cabrito private UIControl rootControl; private ConsoleContentTextBox consoleBox; private ConsoleInputTextBox consoleInputBox; + private ConsoleContentTextBox consoleNotifyBox; internal InputEvent consoleInputEvent; @@ -137,6 +146,35 @@ namespace Cabrito parentControl.Name = "ConsoleContent"; parentControl.Control = consoleBox; consoleBox.Location = locationFix; // workaround to UIControl.Control overriding the old position + + if (consoleNotifyBox == null) + { + //consoleBox = new ConsoleContentTextBox(null, 0, 0, consoleSize.X, consoleSize.Y - fontHeight); + consoleNotifyBox = new ConsoleContentTextBox(null, 0, 0, 0, 0); + consoleNotifyBox.HeightMultiplier = 0; + consoleNotifyBox.Height = ConsoleNotifyLines * fontHeight; + consoleNotifyBox.SetAnchorPreset(AnchorPresets.HorizontalStretchTop, true); + //consoleBox.AnchorMax = new Vector2(1.0f, ConsoleHeight); + + consoleNotifyBox.Font = fontReference; + + //consoleBox.HorizontalAlignment = TextAlignment.Near; + //consoleBox.VerticalAlignment = TextAlignment.Near; + //consoleNotifyBox.HeightMultiplier = ConsoleHeight; + consoleNotifyBox.Wrapping = TextWrapping.WrapWords; + consoleNotifyBox.BackgroundColor = Color.Transparent; + consoleNotifyBox.BackgroundSelectedColor = Color.Transparent; + consoleNotifyBox.BackgroundSelectedFlashSpeed = 0; + consoleNotifyBox.BorderSelectedColor = Color.Transparent; + consoleNotifyBox.CaretFlashSpeed = 0; + consoleNotifyBox.LineSpacing = LineSpacing; + } + + var locationFix2 = consoleNotifyBox.Location; + var parentControl2 = Actor.AddChild(); + parentControl2.Name = "ConsoleNotifyContent"; + parentControl2.Control = consoleNotifyBox; + consoleNotifyBox.Location = locationFix2; // workaround to UIControl.Control overriding the old position } { @@ -176,7 +214,7 @@ namespace Cabrito Console.RegisterConsoleScript(this); RefreshLayout(); -#if true +#if false //for (int i = 0; i < 10; i++) { string[] teststr = { @@ -259,12 +297,12 @@ namespace Cabrito 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() @@ -273,6 +311,7 @@ namespace Cabrito //consoleInputEvent.Triggered -= OnConsoleInputEvent; consoleInputEvent?.Dispose(); consoleBox?.Dispose(); + consoleNotifyBox?.Dispose(); Debug.Logger.LogHandler.SendLog -= OnSendLog; Debug.Logger.LogHandler.SendExceptionLog -= OnSendExceptionLog; @@ -359,8 +398,6 @@ namespace Cabrito Profiler.BeginEvent("ConsoleScript_OnUpdate"); base.OnUpdate(); - const float consoleSpeed = 3500f; - float targetY; if (!Console.IsOpen) targetY = -rootControl.Control.Height; @@ -372,28 +409,49 @@ namespace Cabrito { if (location.Y > targetY) { - location.Y -= Time.UnscaledDeltaTime * consoleSpeed; + // closing + location.Y -= Time.UnscaledDeltaTime * ConsoleSpeed; if (location.Y < targetY) location.Y = targetY; + + if (location.Y < targetY * ConsoleHeight) + location.Y = targetY; } else if (location.Y < targetY) { - location.Y += Time.UnscaledDeltaTime * consoleSpeed; + // opening + if (location.Y < -rootControl.Control.Height * ConsoleHeight) + location.Y = -rootControl.Control.Height * ConsoleHeight; + + location.Y += Time.UnscaledDeltaTime * ConsoleSpeed; if (location.Y > targetY) location.Y = targetY; } rootControl.Control.Location = location; + + if (Console.IsOpen) + { + consoleNotifyBox.Visible = false; + consoleInputBox.Visible = true; + } + else if (!Console.IsOpen) + { + int fontHeight = consoleNotifyBox.Font.GetFont().Height; + if (location.Y < (-rootControl.Control.Height * ConsoleHeight) + fontHeight) + { + consoleNotifyBox.Visible = true; + consoleInputBox.Visible = false; + } + } } Profiler.EndEvent(); } - public override void AddLine(string text) + public override void OnLineAdded(string text) { - /*if (string.IsNullOrEmpty(consoleBox.Text)) - consoleBox.Text += text; - else - consoleBox.Text += "\n" + text;*/ + int fontHeight = consoleNotifyBox.Font.GetFont().Height; + consoleNotifyBox.Height = Math.Min(ConsoleNotifyLines, Console.Lines.Count) * fontHeight; } public override void SetInput(string text) diff --git a/Source/Game/Cabrito/Console/ConsoleTextBoxBase.cs b/Source/Game/Cabrito/Console/ConsoleTextBoxBase.cs index 71e9545..2a7b5a3 100644 --- a/Source/Game/Cabrito/Console/ConsoleTextBoxBase.cs +++ b/Source/Game/Cabrito/Console/ConsoleTextBoxBase.cs @@ -150,14 +150,14 @@ namespace Cabrito TargetViewOffset = new Vector2(0, GetRealLineSpacing()); } - public void ScrollToEnd() + /*public void ScrollToEnd() { float maxY = TextSize.Y - Height; float spacing = GetRealLineSpacing(); maxY += spacing; TargetViewOffset = new Vector2(0, Math.Max(0, maxY)); - } + }*/ public override void ScrollToCaret() { @@ -175,7 +175,7 @@ namespace Cabrito TargetViewOffset = Vector2.Clamp(newLocation, new Vector2(0, spacing), new Vector2(_targetViewOffset.X, maxY)); } - const bool smoothScrolling = false; + /*const bool smoothScrolling = false; public override bool OnMouseWheel(Vector2 location, float delta) { @@ -192,7 +192,7 @@ namespace Cabrito maxY += offset; TargetViewOffset = Vector2.Clamp(_targetViewOffset - new Vector2(0, delta), new Vector2(0, offset), new Vector2(_targetViewOffset.X, maxY)); return true; - } + }*/ public override Vector2 GetTextSize() {