-
Notifications
You must be signed in to change notification settings - Fork 1
/
Console.cs
237 lines (214 loc) · 7.03 KB
/
Console.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Kafe
{
public static class Console
{
private static List<string> backLog = new List<string>();
private static StreamWriter fileLog;
public static TildeConsole TildeConsole;
public static void Prepare()
{
fileLog = new StreamWriter("console.txt");
fileLog.AutoFlush = true;
}
public static void Shutdown()
{
fileLog.Flush();
fileLog.Close();
}
public static void WriteLine(string text)
{
if (TildeConsole != null)
{
if (backLog != null)
{
foreach (var line in backLog)
TildeConsole.WriteLine(line);
backLog = null;
}
TildeConsole.WriteLine(text);
}
else if (backLog != null)
backLog.Add(text);
fileLog.WriteLine(text);
}
public static void WriteLine(string format, params object[] arg)
{
WriteLine(string.Format(format, arg));
}
}
public class ConsoleEventArgs : EventArgs
{
public string Command { get; private set; }
public ConsoleEventArgs(string command)
{
Command = command;
}
}
public class TildeConsole : DrawableGameComponent
{
private SpriteBatch batch;
private Texture2D background;
//private Texture2D edge;
private List<string> buffer;
private string command;
private int width, height;
public int ScrollOffset { get; set; }
public delegate void OnCommandEventHandler(object sender, ConsoleEventArgs e);
public event OnCommandEventHandler OnCommand;
public void RaiseOnCommandEvent(string command)
{
if (this.OnCommand != null)
OnCommand(this, new ConsoleEventArgs(command));
}
#region Keymaps
private static char[] lowers = new char[]
{
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // 0
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // 1
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // 2
'0','1','2','3','4','5','6','7','8','9',' ',' ',' ',' ',' ',' ', // 3
' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o', // 4
'p','q','r','s','t','u','v','w','x','y','z',' ',' ',' ',' ',' ', // 5
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','.',' ', // 6
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // 7
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // 8
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // 9
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // A
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',';','=',',','-','.','/', // B
'`',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // C
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','[','\\',']','\'',' ',//D
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // E
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // F
};
private static char[] uppers = new char[]
{
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // 0
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // 1
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // 2
')','!','@','#','$','%','^','&','*','(',' ',' ',' ',' ',' ',' ', // 3
' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O', // 4
'P','Q','R','S','T','U','V','W','X','Y','Z',' ',' ',' ',' ',' ', // 5
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','.',' ', // 6
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // 7
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // 8
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // 9
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // A
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',':','+','<','_','>','?', // B
'~',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // C
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','{','|','}','"',' ', //D
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // E
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', // F
};
#endregion
public TildeConsole(Game game) : base(game)
{
buffer = new List<string>();
command = string.Empty;
}
public override void Initialize()
{
base.Initialize();
Console.TildeConsole = this;
Console.WriteLine("Console initialized.");
}
protected override void LoadContent()
{
batch = new SpriteBatch(GraphicsDevice);
background = Mix.GetTexture("menu_back.png");
//edge = Mix.GetTexture("Console edge");
width = GraphicsDevice.PresentationParameters.BackBufferWidth;
height = GraphicsDevice.PresentationParameters.BackBufferHeight / 2;
base.LoadContent();
Console.WriteLine("Console content loaded.");
}
public override void Update(GameTime gameTime)
{
if (!Visible)
return;
if (Input.WasJustPressed(Keys.PageUp) && ScrollOffset < buffer.Count - 1)
ScrollOffset++;
else if (Input.WasJustPressed(Keys.PageDown) && ScrollOffset > 0)
ScrollOffset--;
else if (Input.WasJustPressed(Keys.Back) && command.Length > 0)
command = command.Remove(command.Length - 1);
else if (Input.WasJustPressed(Keys.Home))
ScrollOffset = buffer.Count - 1;
else if (Input.WasJustPressed(Keys.End))
ScrollOffset = 0;
else if (Input.WasJustReleased(Keys.OemTilde))
{
command = string.Empty;
Visible = false;
Input.Flush();
}
else if (Input.WasJustPressed(Keys.Enter))
{
WriteLine("|c2|" + command);
RaiseOnCommandEvent(command);
command = "";
}
else
{
var ch = GetCharacter();
if (ch != 0)
command += ch;
}
if (command.StartsWith("`"))
command = command.Substring(1);
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
batch.Begin();
batch.Draw(background, new Rectangle(0, 0, width, height - 16), Color.White);
//batch.Draw(edge, new Rectangle(0, height - 16, width, 32), Color.Black);
var y = height - 26;
Text.Draw(batch, 0, ">" + command + (gameTime.TotalGameTime.Milliseconds % 500 < 300 ? "_" : ""), 2, y, Color.Yellow);
int start = buffer.Count - ScrollOffset;
y -= 4;
for (int i = 0; i < 26; i++)
{
y -= 9;
start--;
if (start < 0)
break;
Text.Draw(batch, 0, buffer[start], 6, y, Color.White);
}
batch.End();
base.Draw(gameTime);
}
public void WriteLine(string text)
{
buffer.Add(text.Trim());
if (buffer.Count > 255)
buffer.RemoveAt(0);
}
public void WriteLine(string format, params object[] parms)
{
WriteLine(string.Format(format, parms));
}
private char GetCharacter()
{
for (int a = 0; a < lowers.Length; a++)
{
var key = (Keys)a;
if (Input.WasJustReleased(key) && (lowers[a] != ' ' || a == 0x20))
{
if (Input.IsHeld(Keys.LeftShift) || Input.IsHeld(Keys.RightShift))
return uppers[a];
else
return lowers[a];
}
}
return (char)0;
}
}
}