-
-
Notifications
You must be signed in to change notification settings - Fork 453
/
Copy pathCConsole.cpp
607 lines (500 loc) · 17.1 KB
/
CConsole.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: core/CConsole.cpp
* PURPOSE: In-game console implementation
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#include "StdInc.h"
using SharedUtil::CalcMTASAPath;
using std::string;
#define CONSOLE_INPUT_HISTORY_LENGTH 128
#define CONSOLE_SIZE 4096
#define MAX_CONSOLE_COMMAND_LENGTH 255
#define NATIVE_RES_X 1152.0f
#define NATIVE_RES_Y 864.0f
CConsole::CConsole(CGUI* pManager, CGUIElement* pParent)
{
// Store the GUI manager
m_pManager = pManager;
// Create our history
m_pConsoleHistory = new CEntryHistory(CONSOLE_INPUT_HISTORY_LENGTH);
m_iHistoryIndex = -1;
m_iAutoCompleteIndex = -1;
m_bIsEnabled = true;
m_fWindowSpacer = 9.0f;
m_fWindowSpacerTop = 23.0f;
m_fWindowX = 550.0f;
m_fWindowY = 360.0f;
m_fInputHeight = 29.0f;
// Create GUI elements
CreateElements();
// Add the eventhandlers we use
m_pWindow->SetCloseClickHandler(GUI_CALLBACK(&CConsole::OnCloseButtonClick, this));
m_pWindow->SetSizedHandler(GUI_CALLBACK(&CConsole::OnWindowSize, this));
m_pInput->SetTextAcceptedHandler(GUI_CALLBACK(&CConsole::Edit_OnTextAccepted, this));
m_pInput->SetMaxLength(MAX_CONSOLE_COMMAND_LENGTH);
m_pHistory->SetTextChangedHandler(GUI_CALLBACK(&CConsole::History_OnTextChanged, this));
// Load the console history from a file
m_pConsoleHistory->LoadFromFile(MTA_CONSOLE_INPUT_LOG_PATH);
}
CConsole::~CConsole()
{
// Delete the GUI elements
DestroyElements();
// Delete our history
delete m_pConsoleHistory;
}
void CConsole::Echo(const char* text)
{
// Add to add buffer
m_strPendingAdd += text;
if (!m_strPendingAdd.EndsWith("\n"))
m_strPendingAdd += "\n";
// Trim add buffer
if (m_strPendingAdd.length() > CONSOLE_SIZE)
{
m_strPendingAdd = m_strPendingAdd.Right(CONSOLE_SIZE);
m_strPendingAdd = m_strPendingAdd.SplitRight("\n");
m_pHistory->SetText("");
}
// Flush add buffer is window is visible
if (m_pWindow->IsVisible())
FlushPendingAdd();
CConsoleLogger::GetSingleton().LinePrintf("[Output] : %s", text);
}
void CConsole::Echo(const std::string& text)
{
// Add to add buffer
m_strPendingAdd += text;
if (!m_strPendingAdd.EndsWith("\n"))
m_strPendingAdd += "\n";
// Trim add buffer
if (m_strPendingAdd.length() > CONSOLE_SIZE)
{
m_strPendingAdd = m_strPendingAdd.Right(CONSOLE_SIZE);
m_strPendingAdd = m_strPendingAdd.SplitRight("\n");
m_pHistory->SetText("");
}
// Flush add buffer is window is visible
if (m_pWindow->IsVisible())
FlushPendingAdd();
CConsoleLogger::GetSingleton().WriteLine("[Output] : " + text);
}
void CConsole::Print(const char* text)
{
Echo(text);
}
void CConsole::Printf(const char* format, ...)
{
// Parse the formatted string to a string we can echo
char szBuffer[1024];
va_list ap;
va_start(ap, format);
VSNPRINTF(szBuffer, 1024, format, ap);
va_end(ap);
// Echo it
Echo(szBuffer);
}
void CConsole::Print(const std::string& text)
{
Echo(text);
}
void CConsole::Clear()
{
// Clear the history buffer.
// This crashes if there is more than one line in the console
if (m_pHistory)
{
m_pHistory->SetText("");
m_strPendingAdd.clear();
m_strSavedInputText.clear();
}
}
bool CConsole::IsEnabled()
{
return m_bIsEnabled;
}
void CConsole::SetEnabled(bool bEnabled)
{
// Hide it if neccessary
if (!bEnabled && m_pWindow->IsVisible())
{
SetVisible(false);
}
// Set the enabled bool
m_bIsEnabled = bEnabled;
if (bEnabled)
{
m_pWindow->Activate();
m_pWindow->SetEnabled(true);
m_pWindow->SetAlwaysOnTop(true);
}
}
bool CConsole::IsVisible()
{
return m_pWindow->IsVisible();
}
void CConsole::SetVisible(bool bVisible)
{
// It must be enabled
if (m_bIsEnabled)
{
CMultiplayer* pMultiplayer = CCore::GetSingleton().GetMultiplayer();
pMultiplayer->DisablePadHandler(bVisible);
m_pWindow->SetVisible(bVisible);
// Focus the editbox if we show it, de-focus if we hide
if (bVisible)
{
m_pInput->Activate();
// Flush pending stuff if becoming visible
FlushPendingAdd();
}
else
{
m_pInput->Deactivate();
}
}
}
void CConsole::Show()
{
SetVisible(true);
}
void CConsole::Hide()
{
SetVisible(false);
}
bool CConsole::IsInputActive()
{
return IsVisible() && m_pInput->IsActive();
}
void CConsole::ActivateInput()
{
m_pInput->Activate();
}
bool CConsole::OnCloseButtonClick(CGUIElement* pElement)
{
SetVisible(false);
return true;
}
bool CConsole::History_OnTextChanged(CGUIElement* pElement)
{
// Set the carat to the end of the text
// TODO: Only do this if the console is scrolled to the bottom
m_pHistory->EnsureCaratIsVisible();
// Roger that
return true;
}
bool CConsole::Edit_OnTextAccepted(CGUIElement* pElement)
{
string strInput;
string strHistory;
string strCmd;
string strCmdLine;
// Get the text object from our input window.
strInput = m_pInput->GetText();
// If the input isn't empty and isn't identical to the previous entry in history, add it to the history
if (!strInput.empty() && (m_pConsoleHistory->Empty() || m_pConsoleHistory->GetLast() != strInput))
m_pConsoleHistory->Add(strInput.c_str());
// Clear the input text.
m_pInput->SetText("");
ResetHistoryChanges();
// Add the text to the history buffer.
// Echo ( strInput.c_str () );
// Write to console log
CConsoleLogger::GetSingleton().LinePrintf("[Input] : %s", strInput.c_str());
// Parse out the command name and command line.
GetCommandInfo(strInput.c_str(), strCmd, strCmdLine);
// Execute the command.
CCommands::GetSingleton().Execute(strCmd.c_str(), strCmdLine.c_str());
// Success
return true;
}
void CConsole::GetCommandInfo(const string& strIn, string& strCmdOut, string& strCmdLineOut)
{
int pos;
pos = strIn.find(' ');
if (pos > 0)
{
strCmdOut = strIn.substr(0, pos).c_str();
strCmdLineOut = strIn.substr(pos + 1, strIn.size() - 1).c_str();
}
else
{
strCmdOut = strIn.c_str();
strCmdLineOut.clear();
}
}
void CConsole::GracefullySetEditboxText(const char* szText)
{
m_pInput->SetText(szText);
// Reset caret so input is scrolled back if it had a long text before
m_pInput->SetCaretAtStart();
// Caret can't be set back to end yet because GUI will not detect any
// state change and thus will not redraw anything. Just wait for next
// rendering phase instead and then move the caret back.
//
// There is a minimal caret flickering, it can't be prevented without
// some sort of custom rendering logic.
m_pInput->SetRenderingEndedHandler(GUI_CALLBACK(&CConsole::GracefullyMoveEditboxCaret, this));
}
bool CConsole::GracefullyMoveEditboxCaret(CGUIElement* pElement)
{
m_pInput->SetCaretAtEnd();
// Done, unhook itself
m_pInput->SetRenderingEndedHandler(nullptr);
return true;
}
void CConsole::ResetHistoryChanges()
{
// Reset history selection, any history changes and our saved input
m_iHistoryIndex = -1;
m_pConsoleHistory->ResetChanges();
m_strSavedInputText.clear();
}
void CConsole::SelectHistoryEntry(int iEntry)
{
int iPreviousHistoryIndex = m_iHistoryIndex;
// Check if we're in bounds, otherwise clear selection
if (!m_pConsoleHistory->Empty() && iEntry >= 0 && iEntry < m_pConsoleHistory->Size())
m_iHistoryIndex = iEntry;
else
m_iHistoryIndex = -1;
SString strInputText = m_pInput->GetText();
// Save current input as a temporary input value
if (iPreviousHistoryIndex == -1)
m_strSavedInputText = strInputText;
else
m_pConsoleHistory->Get(iPreviousHistoryIndex)->temp = strInputText;
// If we haven't selected any history entry, use our saved input text
if (m_iHistoryIndex == -1)
GracefullySetEditboxText(m_strSavedInputText.c_str());
else
{
SString& strSelectedInputHistoryEntry = m_pConsoleHistory->Get(m_iHistoryIndex)->temp;
GracefullySetEditboxText(strSelectedInputHistoryEntry.c_str());
}
}
bool CConsole::SetNextHistoryText()
{
// If we can't take input or we're at the end of the list, stop here
if (!m_pInput->IsActive() || m_iHistoryIndex >= m_pConsoleHistory->Size() - 1)
return false;
// Select the previous entry
SelectHistoryEntry(m_iHistoryIndex + 1);
return true;
}
bool CConsole::SetPreviousHistoryText()
{
// If we can't take input, stop here
if (!m_pInput->IsActive())
return false;
// Select the next entry, or the default entry
if (m_pConsoleHistory->Size() > 0 && m_iHistoryIndex > 0)
SelectHistoryEntry(m_iHistoryIndex - 1);
else
SelectHistoryEntry(-1);
return true;
}
void CConsole::ResetAutoCompleteMatch()
{
m_iAutoCompleteIndex = -1;
}
void CConsole::SetNextAutoCompleteMatch()
{
// Update match list if required
if (m_iAutoCompleteIndex == -1)
{
m_AutoCompleteList.clear();
// Get current input
string strInput = m_pInput->GetText();
if (strInput.length() > 0)
{
// Step through the history
int iIndex = -1;
while (CEntryHistoryItem* pEntryHistoryItem = m_pConsoleHistory->Get(++iIndex))
{
const char* szItem = *pEntryHistoryItem;
if (strlen(szItem) < 3) // Skip very short lines
continue;
// Save the index of any matches
if (strnicmp(szItem, strInput.c_str(), strInput.length()) == 0)
{
if (m_AutoCompleteList.size()) // Dont add duplicates of the previously added line
{
CEntryHistoryItem* pPrevEntryHistoryItem = m_pConsoleHistory->Get(m_AutoCompleteList.at(m_AutoCompleteList.size() - 1));
if (!pPrevEntryHistoryItem)
continue;
const char* szPrevItem = *pPrevEntryHistoryItem;
if (strcmp(szItem, szPrevItem) == 0)
continue;
}
m_AutoCompleteList.push_back(iIndex);
}
}
}
}
// Stop if no matches
if (m_AutoCompleteList.size() == 0)
return;
// Step to next match
m_iAutoCompleteIndex = (m_iAutoCompleteIndex + 1) % m_AutoCompleteList.size();
// Grab the item and set the input text to it
CEntryHistoryItem* pHistoryEntryItem = m_pConsoleHistory->Get(m_AutoCompleteList.at(m_iAutoCompleteIndex));
if (!pHistoryEntryItem)
return;
const char* szItem = *pHistoryEntryItem;
if (szItem)
GracefullySetEditboxText(szItem);
}
void CConsole::CreateElements(CGUIElement* pParent)
{
// Adjust window size to resolution
CVector2D ScreenSize = m_pManager->GetResolution();
m_fWindowX *= ScreenSize.fX / NATIVE_RES_X;
m_fWindowY *= ScreenSize.fY / NATIVE_RES_Y;
// Create window
m_pWindow = reinterpret_cast<CGUIWindow*>(m_pManager->CreateWnd(pParent, _("CONSOLE")));
m_pWindow->SetAlwaysOnTop(true);
CVector2D resolution = CCore::GetSingleton().GetGUI()->GetResolution();
float yoff = resolution.fY > 600 ? resolution.fY / 12 : 0.0f;
m_pWindow->SetPosition(CVector2D(resolution.fX / 2 - m_fWindowX / 2, resolution.fY / 2 - m_fWindowY / 2 + yoff), false);
// m_pWindow->SetPosition ( CVector2D ( 0.20f, 0.20f ), true );
m_pWindow->SetSize(CVector2D(m_fWindowX, m_fWindowY));
m_pWindow->SetMinimumSize(CVector2D(m_fWindowX, m_fWindowY));
m_pWindow->SetSizingEnabled(true);
/** History widget
size x: SPACER - [WINDOW WIDTH] - SPACER
size y: SPACER (TOP) - [WINDOW HEIGHT] - SPACER/2 - INPUT HEIGHT - SPACER
*/
CVector2D HistorySize = CVector2D(m_fWindowX - m_fWindowSpacer * 2.0f, m_fWindowY - m_fWindowSpacer * 1.5f - m_fWindowSpacerTop - m_fInputHeight);
m_pHistory = reinterpret_cast<CGUIMemo*>(m_pManager->CreateMemo(m_pWindow));
m_pHistory->SetPosition(CVector2D(m_fWindowSpacer, m_fWindowSpacerTop));
CVector2D RelHistorySize = m_pWindow->AbsoluteToRelative(HistorySize);
m_pHistory->SetSize(HistorySize);
m_pHistory->SetReadOnly(true);
/** Input widget
pos x: SPACER
pos y: SPACER (TOP) + HISTORY HEIGHT + SPACER
*/
m_pInput = reinterpret_cast<CGUIEdit*>(m_pManager->CreateEdit(m_pWindow));
m_pInput->SetPosition(CVector2D(m_fWindowSpacer, HistorySize.fY + m_fWindowSpacer * 0.5f + m_fWindowSpacerTop));
m_pInput->SetWidth(HistorySize.fX);
m_pInput->SetHeight(m_fInputHeight);
}
void CConsole::DestroyElements()
{
if (m_pWindow)
delete m_pWindow;
}
CVector2D CConsole::GetPosition()
{
if (m_pWindow)
{
return m_pWindow->GetPosition();
}
return CVector2D();
}
void CConsole::SetPosition(CVector2D& vecPosition)
{
if (m_pWindow)
{
m_pWindow->SetPosition(vecPosition);
}
}
CVector2D CConsole::GetSize()
{
if (m_pWindow)
{
return m_pWindow->GetSize();
}
return CVector2D();
}
void CConsole::SetSize(CVector2D& vecSize)
{
if (m_pWindow)
{
// OnWindowSize should do the rest
m_pWindow->SetSize(vecSize);
}
}
bool CConsole::OnWindowSize(CGUIElement* pElement)
{
CVector2D vecSize;
m_pWindow->GetSize(vecSize);
m_fWindowX = vecSize.fX;
m_fWindowY = vecSize.fY;
CVector2D HistorySize = CVector2D(m_fWindowX - m_fWindowSpacer * 2.0f, m_fWindowY - m_fWindowSpacer * 1.5f - m_fWindowSpacerTop - m_fInputHeight);
m_pHistory->SetPosition(CVector2D(m_fWindowSpacer, m_fWindowSpacerTop));
CVector2D RelHistorySize = m_pWindow->AbsoluteToRelative(HistorySize);
m_pHistory->SetSize(HistorySize);
m_pInput->SetPosition(CVector2D(m_fWindowSpacer, HistorySize.fY + m_fWindowSpacer * 0.5f + m_fWindowSpacerTop));
m_pInput->SetWidth(HistorySize.fX);
m_pInput->SetHeight(m_fInputHeight);
return true;
}
// Send saved console adds to the actual gui window
void CConsole::FlushPendingAdd()
{
if (!m_strPendingAdd.empty())
{
// Grab the scroll and the max scroll
float fScroll = m_pHistory->GetVerticalScrollPosition();
float fMaxScroll = m_pHistory->GetScrollbarDocumentSize() - m_pHistory->GetScrollbarPageSize();
// Grab selection
uint uiSelectionStart = m_pHistory->GetSelectionStart();
uint uiSelectionEnd = m_pHistory->GetSelectionEnd();
uint uiSelectionLength = m_pHistory->GetSelectionLength();
// Make new buffer
SString strBuffer = m_pHistory->GetText();
strBuffer += m_strPendingAdd;
m_strPendingAdd.clear();
// Trim new buffer
uint uiBufferLength = strBuffer.length();
if (uiBufferLength > CONSOLE_SIZE)
{
strBuffer = strBuffer.Right(CONSOLE_SIZE);
strBuffer = strBuffer.SplitRight("\n");
// Fix text selection coords after trimming
if (uiSelectionLength > 0)
{
uint uiBufferLengthDiff = uiBufferLength - strBuffer.length();
// Beware of underflows, all cases must be properly handled
if (uiSelectionEnd < uiBufferLengthDiff)
{
// Whole selection would be out of the screen now, so technically
// it does not exist anymore
uiSelectionStart = 0;
uiSelectionEnd = 0;
uiSelectionLength = 0;
}
else if (uiSelectionStart < uiBufferLengthDiff)
{
// Start of selection would be out of the screen now,
// so it must be shortened
uiSelectionLength -= uiBufferLengthDiff - uiSelectionStart;
uiSelectionStart = 0;
uiSelectionEnd -= uiBufferLengthDiff;
}
else // Both start and end of selection are greater than length difference
{
// Whole selection would still be visible on the screen,
// just a simple movement is needed
uiSelectionStart -= uiBufferLengthDiff;
uiSelectionEnd -= uiBufferLengthDiff;
}
}
}
// Set new buffer
m_pHistory->SetText(strBuffer);
// If not at the end, keep the scrollbar position
if (fScroll < fMaxScroll)
m_pHistory->SetVerticalScrollPosition(fScroll);
// Restore text selection if any
if (uiSelectionLength > 0)
m_pHistory->SetSelection(uiSelectionStart, uiSelectionEnd);
}
}