forked from dzamkov/OpenTKGUI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Terminal.cs
194 lines (166 loc) · 4.64 KB
/
Terminal.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
using System;
using System.Collections.Generic;
namespace OpenTKGUI
{
/// <summary>
/// Console for your debug messages or debug input
/// </summary>
public class Terminal
{
public Terminal()
{
this.Buffer = new Queue<SelectableLabel>();
this.Write("Terminal...");
}
/// <summary>
/// Push a message to the terminal.
/// </summary>
public void Write(string Message)
{
SelectableLabel lbl = new SelectableLabel(Message);
this.Buffer.Enqueue(lbl);
this.Update(lbl);
}
private void Update(SelectableLabel lbl)
{
if(this._Terminal == null)
return;
this._LableItems.AddChild(lbl, this._Style.LableHeight);
this._ScrollContainer.ClientHeight = this.Buffer.Count * this._Style.LableHeight;
}
/// <summary>
/// Shows the terminal.
/// </summary>
public void Show(LayerContainer LayerContainer)
{
if(this._Terminal != null)
return;
this._LableItems = new FlowContainer(Axis.Vertical);
foreach(SelectableLabel lbl in this.Buffer.ToArray())
this._LableItems.AddChild(lbl, this._Style.LableHeight);
this._ScrollContainer = new ScrollContainer(this._LableItems);
this._ScrollContainer.ClientHeight = this.Buffer.Count * this._Style.LableHeight;
FlowContainer flow2 = new FlowContainer(Axis.Vertical);
Textbox command = new Textbox();
command.TextChanged += delegate(string Text)
{
if(this.GetAutoComplete != null)
{
string[] result = this.GetAutoComplete(Text);
PopupContainer pc = new PopupContainer(command);
//pc.Call
MenuItem[] items = new MenuItem[result.Length];
for(int i = 0; i < result.Length; i++)
items[i] = MenuItem.Create(result[i], delegate{});
pc.Items = items;
pc.Call(new Point(0,0));
}
};
command.TextEntered += delegate(string Text)
{
this.Write("> " + Text);
command.Text = "";
LinkedList<string> parsed;
string cmd;
try
{
parsed = Terminal.ParseArguments(Text);
cmd = parsed.First.Value;
parsed.RemoveFirst();
}
catch(ParseArgsException ex)
{
this.Write("Parse error: " + ex.Message);
return;
}
if(this.DoCommand != null)
{
if(!this.DoCommand(cmd, parsed))
{
this.Write("Command \"" + cmd + "\" not found!");
}
}
};
flow2.AddChild(this._ScrollContainer, this._Style.TerminalHeight - this._Style.TextboxHeight);
flow2.AddChild(command, this._Style.TextboxHeight);
this._Terminal = new Form(flow2, "Terminal");
this._Terminal.AddCloseButton();
this._Terminal.ClientSize = new Point(this._Style.TerminalWidth, this._Style.TerminalHeight);
LayerContainer.AddControl(this._Terminal, new Point(200,200));
}
private static Dictionary<char, char> _Literal = null;
private static void LoadDict()
{
_Literal = new Dictionary<char, char>();
_Literal.Add('n', '\n');
_Literal.Add('r', '\r');
_Literal.Add('t', '\t');
}
/// <summary>
/// Convert a string into arguments
/// </summary>
public static LinkedList<string> ParseArguments(string Text)
{
if(_Literal == null)
LoadDict();
LinkedList<string> retval = new LinkedList<string>();
bool literal = false;
bool inQuotes = false;
string currentiteration = "";
foreach(char ch in Text)
{
if(literal)
{
literal = false;
char newchar;
if(!_Literal.TryGetValue(ch, out newchar))
newchar = ch;
currentiteration += newchar;
}
else if(ch == '\\')
literal = true;
else if(ch == '\"')
inQuotes = !inQuotes;
else if(ch == ' ')
{
if(!inQuotes)
{
retval.AddLast(currentiteration);
currentiteration = "";
}else
currentiteration += ch;
}
else
{
currentiteration += ch;
}
}
if(currentiteration != "")
retval.AddLast(currentiteration);
return retval;
}
public event GetAutoCompleteHandeler GetAutoComplete;
public event DoCommandHandeler DoCommand;
private TerminalStyle _Style = new TerminalStyle();
private Form _Terminal;
private ScrollContainer _ScrollContainer;
private FlowContainer _LableItems;
private Queue<SelectableLabel> Buffer;
}
/// <summary>
/// Gives styling options for a message box.
/// </summary>
public class TerminalStyle
{
public double TerminalWidth = 500;
public double TerminalHeight = 300;
public double LableHeight = 20.0;
public double TextboxHeight = 30.0;
}
public class ParseArgsException : Exception
{
public ParseArgsException(string Message) : base(Message)
{
}
}
}