-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevConsole.cs
More file actions
183 lines (166 loc) · 4.85 KB
/
DevConsole.cs
File metadata and controls
183 lines (166 loc) · 4.85 KB
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
using Godot;
using System;
using System.Linq;
using ExtractIntoVoid.Managers;
using System.Globalization;
// ReSharper disable CheckNamespace
// ReSharper disable MemberCanBePrivate.Global
public partial class DevConsole : Window
{
// Called when the node enters the scene tree for the first time.
private TextEdit Output => GetNode<TextEdit>("./Control/Output");
private LineEdit Input => GetNode<LineEdit>("Control/InputArea/InputField");
public override void _Ready()
{
CloseRequested += () =>
{
Close();
};
Open();
//ConsoleManager.Console = this;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
if (Visible && Output.Text != ConsoleManager.Contents)
Output.Text = ConsoleManager.Contents;
}
private void OnInputSubmitted(string input)
{
var split = input.Split(' ');
var command = split.First();
var args = split.Skip(1).ToArray();//TakeLast(input.Split(' ').Length - 1).ToArray();
Input.Text = "";
Print("> " + input);
HandleCommand(command, args);
}
public override void _Input(InputEvent @event)
{
if (@event is InputEventKey eventKey && eventKey.Pressed && eventKey.Keycode == Key.Tab && Input.Text != "")
{
var types = ConsoleManager.GetAll();
if (!types.Keys.Any(s => s.StartsWith(Input.Text)))
{
return;
}
//Print(_cTypes.Keys.FirstOrDefault(s => s.StartsWith(Input.Text)));
var suggestions = new PopupMenu()
{
Size = new Vector2I(0, 0),
Position = new Vector2I((int)Input.GetScreenPosition().X, (int)Input.GetScreenPosition().Y + 24)
};
foreach (var item in types.Where(s => s.Key.StartsWith(Input.Text)))
{
var context = new CommandContext(this, null);
var value = "";
if (item.Value is CVar<string> cVarString)
{
value += " " + cVarString.Get(context);
}
if (item.Value is CVar<bool> cVarBool)
{
value += " " + cVarBool.Get(context);
}
if (item.Value is CVar<int> cVarInt)
{
value += " " + cVarInt.Get(context);
}
if (item.Value is CVar<float> cVarFloat)
{
value += " " + cVarFloat.Get(context);
}
suggestions.AddItem($"{item.Key}{value}");
}
suggestions.IdPressed += delegate (long id)
{
Input.Text = suggestions.GetItemText((int)id).Split(" ").First();
Input.CaretColumn = Input.Text.Length;
};
suggestions.SetFocusedItem(0);
Input.AddChild(suggestions);
suggestions.Popup();
//_cTypes.Keys.Where(s => s.StartsWith(Input.Text));
}
base._Input(@event);
}
public void Close()
{
Visible = false;
Godot.Input.MouseMode = ConsoleManager.CachedMouse;
QueueFree();
}
private void Open()
{
ConsoleManager.CachedMouse = Godot.Input.MouseMode;
Visible = true;
Godot.Input.MouseMode = Godot.Input.MouseModeEnum.Visible;
}
public void HandleCommand(string inputCommand, string[] args)
{
if (ConsoleManager.TryGet(inputCommand, out var command))
{
var context = new CommandContext(this, args);
if (command is CFunc cFunc)
{
cFunc.Call(context);
}
if (command is CVar<string> cVarString)
{
if (args.Length == 0)
{
Print(cVarString.Get(context));
}else {
cVarString.Set(context, string.Join(" ", args));
}
}
if (command is CVar<bool> cVarBool)
{
if (args.Length == 0)
{
Print(cVarBool.Get(context));
}else {
try {
cVarBool.Set(context, Convert.ToBoolean(args[0]));
}
catch (FormatException) {
Print($"Invalid input \"{args[0]}\", must be True or False");
}
}
}
if (command is CVar<int> cVarInt)
{
if (args.Length == 0)
{
Print(cVarInt.Get(context));
}else {
try {
cVarInt.Set(context, Convert.ToInt32(args[0]));
}
catch (FormatException) {
Print($"Invalid input \"{args[0]}\", must be an integer");
}
}
}
if (command is CVar<float> cVarFloat)
{
if (args.Length == 0)
{
Print(cVarFloat.Get(context));
}else {
try {
cVarFloat.Set(context, float.Parse(args[0], CultureInfo.InvariantCulture));
}
catch (FormatException) {
Print($"Invalid input \"{args[0]}\", must be a float");
}
}
}
}
}
public void Print(object what)
{
ConsoleManager.Contents += what;
ConsoleManager.Contents += '\n';
Output.ScrollVertical = Output.GetVScrollBar().MaxValue;
}
}