Console work

This commit is contained in:
Matt Thorson 2020-06-18 00:13:50 -07:00
parent f95e638e91
commit 9de0160957
5 changed files with 125 additions and 15 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
SampleGame/build/*
SampleGame/BeefSpace_User.toml
build/*

View File

@ -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

View File

@ -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);
}
}

View File

@ -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<String> commandHistory;
static private List<String> messages;
static private Dictionary<String, CommandInfo> commands;
static public void Init()
{
enabled = true;
font = SDL2.SDLTTF.OpenFont("../../../../Strawberry/src/Content/strawberry-seeds.ttf", 8);
entry = new String();
commandHistory = new List<String>();
messages = new List<String>();
commands = new Dictionary<String, CommandInfo>();
//Find commands
for (let type in Type.Types)
{
for (let method in type.GetMethods(.Static | .NonPublic | .Public))
{
let attr = method.GetCustomAttribute<CommandAttribute>();
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;
}
}
}
}

View File

@ -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);