/
console.cpp
357 lines (322 loc) · 11.8 KB
/
console.cpp
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <TGUI/TGUI.hpp>
#include "../app.hpp"
#include <SFML/OpenGL.hpp>
#include "console.hpp"
#include "consolecommands.hpp"
#include <cstdlib>
#include <sstream>
#include <algorithm>
ConsoleManager::ConsoleManager(sf::Window *Parent):
d_parent_window(Parent)
{
d_console_window.create(sf::VideoMode(400,200),
"Console",
sf::Style::Default,
sf::ContextSettings(32, 8, 0, 3, 3)
);
gui.setWindow(d_console_window);
gui.setGlobalFont("tgui/fonts/TerminalVector.ttf");
//to line console up with main window, divide parent size by some constant(2.35)
d_console_window.setPosition( sf::Vector2i(
(int)(Parent->getPosition().x - Parent->getSize().x/2.35),
Parent->getPosition().y));
}
void ConsoleManager::bindParentWindow(sf::Window *Parent)
{
d_parent_window = Parent;
}
void ConsoleManager::bindParticleManager(ParticleManager *Particlemanager)
{
d_particle_manager = Particlemanager;
}
void ConsoleManager::init()
{
//here we initialize the gui objects. now TGUI, or any other
// SFML gui for that matter does offer any quick solutions to
// creating a console, so here we are making a very ugly console
// that will make the user think it is a fully functional window.
// we place an edit box at the bottom, and simply use a chat box
// to display previous commands, everything blends so the user will
// think this is all one window!
d_console_edit_box = tgui::EditBox::Ptr(gui);
d_console_edit_box->load("tgui/widgets/Black.conf");
d_console_edit_box->setSize(400,20);
d_console_edit_box->setPosition(0,180);
d_console_edit_box->setTextSize(12);
d_console_edit_box->setMaximumCharacters(32);
d_console_edit_box->setText("> ");
d_console_command_list = tgui::ChatBox::Ptr(gui);
d_console_command_list->load("tgui/widgets/Black.conf");
d_console_command_list->setSize(400,185);
d_console_command_list->setPosition(0,0);
d_console_command_list->removeScrollbar();
d_console_command_list->setLinesStartFromBottom(true);
d_console_command_list->setLineLimit(COMMAND_COUNT+1);
d_console_command_list->setTextSize(12);
d_console_command_list->setTextColor(d_console_edit_box->getTextColor());
//set input box in focus so user does not have to click
d_console_edit_box->focus();
}
void ConsoleManager::handleEvent(sf::Event& event, bool& run)
{
while(d_console_window.pollEvent(event))
{
if(event.type == sf::Event::Closed || (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape))
run = false;
if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Return) {
handleCommand();
}
//pass event to all widgets
gui.handleEvent(event);
}
}
void ConsoleManager::handleCommand()
{
//get current text inside console
std::string command = d_console_edit_box->getText().toAnsiString();
//determine distance from '>' character to end of first command
std::size_t dist_command_sub_string =
std::distance(
command.begin()+2,
std::find(command.begin()+2, command.end(), ' ')
);
//get substring holding just the command
std::string command_sub_string = command.substr(
2,
dist_command_sub_string
);
//if there is more after the command, set command to JUST that
if(dist_command_sub_string+2 != command.length()) {
command = command.substr(dist_command_sub_string+3,
std::distance(command.begin()+dist_command_sub_string+3, command.end())
);
}else{
command = "";
}
consolecommands::Key command_key;
if(!consolecommands::isValidCommandKey(command_sub_string, command_key)) {
d_console_command_list->
addLine(d_console_edit_box->getText().getData());
printToConsole("Invalid Command");
d_console_edit_box->setText("> ");
}else{
translateCommandsUp();
switch(command_key) {
case consolecommands::GET:
handleGetCommand(command);
break;
case consolecommands::SET:
handleSetCommand(command);
break;
case consolecommands::EXIT:
App::procClose();
break;
case consolecommands::HELP:
printToConsole("list of available commands:% get% set% reset% exit%%type help <command> for additional information");
handleHelpCommand(command);
break;
case consolecommands::RESET:
d_particle_manager->resetParticles();
break;
}
}
}
void ConsoleManager::render()
{
d_console_window.clear();
d_console_window.pushGLStates();
gui.draw();
d_console_window.display();
d_console_window.popGLStates();
}
void ConsoleManager::translateCommandsUp()
{
d_console_command_list->
addLine(d_console_edit_box->getText().getData());
d_console_edit_box->setText("> ");
}
void ConsoleManager::printToConsole(const std::string& text)
{
//a very weird way parsing the input and adding new lines to the console when a '%' is found
auto Iter = std::find(text.begin(), text.end(), '%');
std::string line = text.substr(0,std::distance(text.begin(), Iter));
if(Iter != text.end())
Iter++;
d_console_command_list->addLine(sf::String(line));
if(Iter == text.end())
return;
while(1) {
std::size_t start = std::distance(text.begin(),Iter);
auto IterEnd = std::find(text.begin()+start, text.end(), '%');
if(IterEnd == text.end()) {
std::string sline = text.substr(std::distance(text.begin(), text.begin()+start), std::distance(text.begin()+start, IterEnd));
d_console_command_list->addLine(sf::String(sline));
return;
}
std::string sline = text.substr(std::distance(text.begin(),text.begin()+start), std::distance(text.begin()+start,IterEnd));
Iter = IterEnd;
++Iter;
d_console_command_list->addLine(sf::String(sline));
}
}
void ConsoleManager::handleSetCommand(const std::string& str)
{
//determine distance from '>' character to end of first command
std::size_t dist_str_sub_string =
std::distance(
str.begin(),
std::find(str.begin(), str.end(), ' ')
);
//get substring holding just the str
std::string str_sub_string = str.substr(
0,
dist_str_sub_string
);
std::string value;
if(dist_str_sub_string != str.length()) {
//get substring holding just the command
value = str.substr(
dist_str_sub_string,
str.length() - dist_str_sub_string
);
}else
value = "";
consolecommands::VarKey key;
if(!consolecommands::isValidCommandVariable(str_sub_string, key)) {
if(str_sub_string.empty())
printToConsole("no variable specififed");
else {
std::stringstream ss;
ss << "variable " << str_sub_string << " not found";
printToConsole(ss.str());
}
}else{
std::stringstream ss;
if(value.empty()){
printToConsole("no value specified");
}else{
ss << value;
float numeric_value;
ss >> numeric_value;
if(numeric_value < 0) {
printToConsole("value cannot be negative wtf");
return;
}
switch(key) {
case consolecommands::DRAG:
d_particle_manager->setDrag(numeric_value);
break;
case consolecommands::MASS:
d_particle_manager->setMass(numeric_value);
break;
case consolecommands::MOUSEFORCE:
d_particle_manager->setMouseForce(numeric_value);
break;
case consolecommands::PARTICLECOUNT:
printToConsole("command not yet supported, sorry!");
break;
case consolecommands::COLOR_R:
d_particle_manager->setColorR(numeric_value);
break;
case consolecommands::COLOR_G:
d_particle_manager->setColorG(numeric_value);
break;
case consolecommands::COLOR_B:
d_particle_manager->setColorB(numeric_value);
break;
case consolecommands::COLOR_A:
d_particle_manager->setColorA(numeric_value);
break;
}
}
}
}
void ConsoleManager::handleGetCommand(const std::string& str)
{
//get substring holding just the str
std::string str_sub_string = str.substr(
0,
std::distance(
str.begin(),
std::find(str.begin(), str.end(), ' ')
)
);
consolecommands::VarKey key;
if(!consolecommands::isValidCommandVariable(str_sub_string, key)) {
if(str_sub_string.empty())
printToConsole("no variable specififed");
else {
std::stringstream ss;
ss << "variable " << str_sub_string << " not found";
printToConsole(ss.str());
}
}else{
std::stringstream ss;
ss.precision(2);
switch(key) {
case consolecommands::DRAG:
ss << std::fixed << d_particle_manager->getDrag() << "f";
break;
case consolecommands::MASS:
ss << std::fixed << d_particle_manager->getMass() << "f";
break;
case consolecommands::MOUSEFORCE:
ss << std::fixed << d_particle_manager->getMouseForce() << "f";
break;
case consolecommands::PARTICLECOUNT:
ss << d_particle_manager->getParticleCount();
break;
case consolecommands::COLOR_R:
ss << d_particle_manager->getColorR();
break;
case consolecommands::COLOR_G:
ss << d_particle_manager->getColorG();
break;
case consolecommands::COLOR_B:
ss << d_particle_manager->getColorB();
break;
case consolecommands::COLOR_A:
ss << d_particle_manager->getColorA();
break;
}
printToConsole(ss.str());
}
}
void ConsoleManager::handleHelpCommand(const std::string& str)
{
//get substring holding just the str
std::string str_sub_string = str.substr(
0,
std::distance(
str.begin(),
std::find(str.begin(), str.end(), ' ')
)
);
if(str_sub_string.empty())
return;
consolecommands::Key key;
if(!consolecommands::isValidCommandKey(str_sub_string, key)) {
if(str_sub_string.empty())
printToConsole("no variable specififed");
else {
std::stringstream ss;
ss << "command " << str_sub_string << " not found";
printToConsole(ss.str());
}
}else{
switch(key) {
case consolecommands::GET:
printToConsole("get:% retrieve variable values, list of var's:% mass,drag,mouseforce% particlecount,color_r/g/b/a");
break;
case consolecommands::SET:
printToConsole("set:% set value of variable, list of var's allowed:% mass,drag,mouseforce% color_r/g/b/a");
break;
case consolecommands::RESET:
printToConsole("reset:% remove all acting force on particles and% position them at the center of the screen");
break;
case consolecommands::EXIT:
printToConsole("exit:% exit the program, esc will also exit the% program");
break;
}
}
}