From 9de0160957c94d14212bd3471a11eb633ff913a6 Mon Sep 17 00:00:00 2001 From: Matt Thorson Date: Thu, 18 Jun 2020 00:13:50 -0700 Subject: [PATCH] Console work --- .gitignore | 1 + src/Core/Game.bf | 4 +- src/Static/Calc.bf | 12 +++-- src/Static/Console.bf | 121 +++++++++++++++++++++++++++++++++++++++--- src/Static/Draw.bf | 2 +- 5 files changed, 125 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 81ebe94..85dfe18 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ SampleGame/build/* SampleGame/BeefSpace_User.toml +build/* diff --git a/src/Core/Game.bf b/src/Core/Game.bf index 5980f62..673a4bc 100644 --- a/src/Core/Game.bf +++ b/src/Core/Game.bf @@ -181,7 +181,7 @@ namespace Strawberry Time.Elapsed += Time.Delta; } - Strawberry.Console.Update(); + Strawberry.Console.[Friend]Update(); } private void Render() @@ -203,7 +203,7 @@ namespace Strawberry } if (Console.Enabled) - Strawberry.Console.Draw(); + Strawberry.Console.[Friend]Draw(); } public Scene Scene diff --git a/src/Static/Calc.bf b/src/Static/Calc.bf index e3c9e02..e9c905c 100644 --- a/src/Static/Calc.bf +++ b/src/Static/Calc.bf @@ -88,10 +88,8 @@ namespace Strawberry Debug.WriteLine(string); } - static public void Log(StringView str, params Object[] args) + static private String StringArgs(String str, Object[] args) { - String string = scope String(str); - for (let i < args.Count) { String arg = scope String(); @@ -101,9 +99,15 @@ namespace Strawberry arg.Clear(); args[i].ToString(arg); - string.Replace(num, arg); + str.Replace(num, arg); } + return str; + } + + static public void Log(StringView str, params Object[] args) + { + let string = StringArgs(scope String(str), args); Debug.WriteLine(string); } } diff --git a/src/Static/Console.bf b/src/Static/Console.bf index 735044c..1115aa5 100644 --- a/src/Static/Console.bf +++ b/src/Static/Console.bf @@ -1,7 +1,23 @@ using System; +using System.Collections; +using System.Reflection; +using Strawberry; namespace Strawberry { + public struct CommandAttribute : Attribute + { + public String Name; + public String Help; + + public this(String name, String help = "") + { + Name = name; + Help = help; + } + } + + [Reflect] static public class Console { static public bool Open; @@ -9,18 +25,39 @@ namespace Strawberry static private bool enabled; static private SDL2.SDLTTF.Font* font; static private String entry; + static private List commandHistory; + static private List messages; + static private Dictionary commands; static public void Init() { enabled = true; font = SDL2.SDLTTF.OpenFont("../../../../Strawberry/src/Content/strawberry-seeds.ttf", 8); entry = new String(); + commandHistory = new List(); + messages = new List(); + commands = new Dictionary(); + + //Find commands + for (let type in Type.Types) + { + for (let method in type.GetMethods(.Static | .NonPublic | .Public)) + { + let attr = method.GetCustomAttribute(); + if (attr != .Err) + commands.Add(attr.Value.Name, new CommandInfo(method, attr.Value)); + } + } + Calc.Log(commands.Count); } static public void Dispose() { SDL2.SDLTTF.CloseFont(font); delete entry; + DeleteContainerAndItems!(commandHistory); + DeleteContainerAndItems!(messages); + DeleteDictionaryAndKeysAndItems!(commands); } static public bool Enabled @@ -34,7 +71,31 @@ namespace Strawberry } } - static public void Update() + static public void Log(StringView str) + { + messages.Add(new String(str)); + while (messages.Count > MessageRows) + { + delete messages[0]; + messages.RemoveAt(0); + } + } + + static public void Log(StringView str, params Object[] args) + { + let string = Calc.[Friend]StringArgs(scope String(str), args); + Log(string); + } + + [AlwaysInclude] + [Reflect] + [Command("clear", "Clears the console window")] + static public void Clear() + { + DeleteAndClearItems!(messages); + } + + static private void Update() { if (enabled) { @@ -46,22 +107,52 @@ namespace Strawberry Input.KeystrokesIntoString(entry, 0.25f, 0.05f); if (Input.KeyPressed(.Delete)) entry.Clear(); + if (Input.KeyPressed(.Return) && entry.Length > 0 && !String.IsNullOrWhiteSpace(entry)) + SubmitEntry(); } } } - static public void Draw() + static private void SubmitEntry() + { + let line = new String(entry); + commandHistory.Add(line); + Log(line); + + //Parse command and arguments + { + let list = String.StackSplit!(line, ' '); + list.RemoveAll(scope => String.IsNullOrWhiteSpace); + Calc.Log("hey {0}"); + + if (commands.ContainsKey(list[0])) + { + Log(line); + //Do it + } + else + Log("Command '{0}' not recognized.", list[0]); + } + + entry.Clear(); + } + + static private void Draw() { if (enabled && Open) { Draw.Rect(0, 0, Game.Width, Game.Height, .Black * 0.6f); - Draw.Rect(0, Game.Height - 12, Game.Width, 14, .Black * 0.6f); + Draw.Rect(0, Game.Height - 14, Game.Width, 14, .Black * 0.6f); - Text(">", 0, 0, .White); + //Entry if (entry.Length > 0) - Text(entry, 0, 1, .White); - if (Time.BetweenInterval(0.3f)) - Text("_", 0, entry.Length + 1, .White); + Text(entry, 0, 0, .White); + if (Time.BetweenInterval(0.25f)) + Text("_", 0, entry.Length, .White); + + //Messages + for (int i = messages.Count - 1; i >= 0; i--) + Text(messages[i], messages.Count - i, 0, .White); } } @@ -73,6 +164,20 @@ namespace Strawberry Draw.Text(font, str, pos + .Down, .Black); Draw.Text(font, str, pos, color); - } + } + + static private int MessageRows => (int)Math.Ceiling((Game.Height - 14) / 10f); + + private class CommandInfo + { + public delegate void(String[]) Action ~ delete _; + public String Help; + public String Usage ~ delete _; + + public this(MethodInfo method, CommandAttribute attr) + { + Help = attr.Help; + } + } } } diff --git a/src/Static/Draw.bf b/src/Static/Draw.bf index 4d3e10c..47a83a6 100644 --- a/src/Static/Draw.bf +++ b/src/Static/Draw.bf @@ -103,7 +103,7 @@ namespace Strawberry static public void Text(SDL2.SDLTTF.Font* font, String text, Point position, Color color) { SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A); - let surface = SDLTTF.RenderUTF8_Blended(font, text, color); + let surface = SDLTTF.RenderUTF8_Solid(font, text, color); let texture = SDL.CreateTextureFromSurface(Game.Renderer, surface); SDL.Rect srcRect = .(0, 0, surface.w, surface.h);