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
608
609
|
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
#include <ctype.h>
#include "SamplePreprocessor.h"
#include "SampleConsole.h"
#include "RendererMemoryMacros.h"
#include "Renderer.h"
#include "PsString.h"
#include "PsUtilities.h"
#include "SampleBaseInputEventIds.h"
#include <SampleUserInputIds.h>
#include "SampleUserInputDefines.h"
using namespace SampleRenderer;
using namespace SampleFramework;
#define NB_LINES 14
// EXIT: a basic command to hide the console
// Usage: exit
void Console::BasicCmdexit(Console* console, const char* text, void* user_data)
{
console->setActive(false);
}
// CLS: a basic command to clear the console
// Usage: cls
void Console::BasicCmdcls(Console* console, const char* text, void* user_data)
{
console->clear();
}
// PROMPT: a basic command to set the prompt
// Usage: prompt [text]
void Console::BasicCmdSetPrompt(Console* console, const char* text, void* user_data)
{
console->setPrompt(text);
}
// CMDLIST: a basic command to display a list of all possible commands
// Usage: cmdlist <= display all possible commands
// cmdlist [command] <= check whether a command exists or not
void Console::BasicCmdcmdlist(Console* console, const char* text, void* user_data)
{
ConsoleCommand* pcmd;
if(!text)
{
for(int i=0;i<256;i++)
{
pcmd = console->mCmds[i];
while(pcmd)
{
console->out(pcmd->fullcmd);
pcmd = pcmd->next;
}
}
}
else
{
int i = text[0];
if( (i >= 'A') && (i<='Z') )
i -= 'A' - 'a';
pcmd = console->mCmds[i];
while(pcmd)
{
if(Ps::strncmp(pcmd->fullcmd, text, strlen(text)) == 0)
console->out(pcmd->fullcmd);
pcmd = pcmd->next;
}
}
}
// CMDHIST: a basic command to display command history
// Usage: cmdhist
void Console::BasicCmdcmdhist(Console* console, const char* text, void* user_data)
{
long index = console->mNewcmd - console->mNumcmdhist;
for(long i=0;i<console->mNumcmdhist;i++)
{
if( index > CONSOLE_MAX_HIST )
index -= CONSOLE_MAX_HIST;
console->out(console->mCmdhist[index] );
index++;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Constructor.
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Console::Console(SampleFramework::SamplePlatform* plt) :
mViewBottom (0),
mNewline (0),
mIsActive (false)
{
strcpy(mLastChar, "-");
strcpy(mPrompt, ">");
resetCol();
mNbCmds = 0;
mNewcmd = 0;
mNumcmdhist = 0;
mCurcmd = 0;
mUserData = 0;
for(PxU32 i=0;i<CONSOLE_MAX_COMMAND_NB;i++)
mCmds[i] = NULL;
// Create console
addCmd("exit", BasicCmdexit);
addCmd("cmdlist", BasicCmdcmdlist);
addCmd("cls", BasicCmdcls);
addCmd("cmdhist", BasicCmdcmdhist);
addCmd("prompt", BasicCmdSetPrompt);
clear();
cmdClear();
out("PhysX Samples console");
out("");
out("Type cmdlist to display all possible commands.");
out("Use PageUp / PageDown to scroll the window.");
out("Use arrow keys to recall old commands.");
out("Use ESC to exit.");
out("");
advance();
strcpy(mBuffer[mNewline].mText, mPrompt);
strcat(mBuffer[mNewline].mText, mLastChar);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Destructor.
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Console::~Console()
{
destroy();
}
void Console::resetCol()
{
mCol = (PxI32)strlen(mPrompt);
}
void Console::setPrompt(const char* text)
{
if(!text)
return;
const PxU32 Length = (PxU32)strlen(text);
if(Length>255)
return;
strcpy(mPrompt, text);
}
// Advance the console one line
void Console::advance()
{
mNewline--;
if(mNewline<0)
mNewline += CONSOLE_MAX_ROW;
mBuffer[mNewline].mText[0] = '\0';
mViewBottom = mNewline;
}
// Clear the console text buffer
void Console::clear()
{
for(PxU32 i=0;i<CONSOLE_MAX_ROW;i++)
{
for(PxU32 j=0;j<CONSOLE_MAX_COL;j++)
mBuffer[i].mText[j] = '\0';
mBuffer[i].mColor = RendererColor(255,255,255);
}
mNewline = 0;
mViewBottom = 0;
}
// Clear the console text buffer
void Console::cmdClear()
{
for(PxU32 i=0;i<CONSOLE_MAX_HIST;i++)
for(PxU32 j=0;j<CONSOLE_MAX_COL;j++)
mCmdhist[i][j] = '\0';
mNewcmd = 0;
mNumcmdhist = 0;
mCurcmd = 0;
}
// Write a string to the console
void Console::out(const char* string)
{
advance();
if(string)
{
size_t Length = strlen(string);
if(Length>=CONSOLE_MAX_COL-1)
{
PX_ASSERT(0);
strcpy(mBuffer[mNewline].mText, "CONSOLE LINE TOO LONG!");
}
else
strcpy(mBuffer[mNewline].mText, string);
}
}
// Process an instruction
// called after the user hits enter
void Console::process()
{
// Discard prompt
char cmd[1024];
long Index = (long)strlen(mPrompt);
strcpy(cmd, &mBuffer[mNewline].mText[Index]);
// Keep track of command in history buffer
strcpy(mCmdhist[mNewcmd], cmd);
mNewcmd = mNewcmd % CONSOLE_MAX_HIST;
mNewcmd++;
mCurcmd = 0;
mNumcmdhist++;
if(mNumcmdhist>CONSOLE_MAX_HIST) mNumcmdhist = CONSOLE_MAX_HIST;
mBuffer[mNewline].mColor = 0xffeeeeee;
// Extract param and execute command
char* cmdparam;
cmdparam = strchr(cmd, ' ');
if(cmdparam)
{
*cmdparam=0;
cmdparam++;
}
if(!execCmd(cmd, cmdparam))
out("Invalid command");
}
// up and down arrow
// for command history
void Console::cmdHistory()
{
if( mCurcmd != -1 )
{
char buf[256];
long cmdnum;
strcpy(buf, mPrompt);
cmdnum = mNewcmd - mCurcmd;
if( cmdnum < 0 )
cmdnum += CONSOLE_MAX_HIST;
strcat(buf, mCmdhist[cmdnum]);
strcat(buf, mLastChar);
strcpy(mBuffer[mNewline].mText, buf);
mCol = (PxI32)strlen(buf)-1;
}
}
bool Console::findBestCommand(char* best_command, const char* text, PxU32& tabIndex) const
{
if(!text || !best_command)
return false;
const size_t length = strlen(text);
if(length>1023)
return false;
char tmp[1024];
strcpy(tmp, text);
Ps::strlwr(tmp);
const unsigned char i = tmp[0];
ConsoleCommand* FirstCommand = NULL;
ConsoleCommand* BestCommand = NULL;
ConsoleCommand* pcmd = mCmds[i];
PxU32 currentIndex = 0;
while(pcmd && !BestCommand)
{
char tmp2[1024];
strcpy(tmp2, pcmd->fullcmd);
Ps::strlwr(tmp2);
if(Ps::strncmp(tmp, tmp2, length)== 0)
{
if(!currentIndex)
FirstCommand = pcmd;
if(currentIndex>=tabIndex)
BestCommand = pcmd;
currentIndex++;
}
pcmd = pcmd->next;
}
if(BestCommand)
{
tabIndex++;
strcpy(best_command, BestCommand->fullcmd);
return true;
}
tabIndex = 0;
if(currentIndex && FirstCommand)
{
tabIndex++;
strcpy(best_command, FirstCommand->fullcmd);
return true;
}
return false;
}
// Try to execute a command
bool Console::execCmd(const char* cmd, const char* param)
{
if(!cmd)
return false;
int HashIndex = cmd[0];
HashIndex = tolower(HashIndex);
ConsoleCommand* pcmd = mCmds[HashIndex];
while(pcmd)
{
if(Ps::stricmp(cmd, pcmd->fullcmd) == 0)
{
pcmd->function(this, param, mUserData);
return true;
}
else pcmd = pcmd->next;
}
return false;
}
// Destroy the console
void Console::destroy()
{
// clean up command list
for(PxU32 i=0;i<256;i++)
{
if(mCmds[i])
{
ConsoleCommand* pcmd = mCmds[i];
while(pcmd)
{
ConsoleCommand* next = pcmd->next;
DELETESINGLE(pcmd);
pcmd = next;
}
}
}
}
// Add a command
void Console::addCmd(const char* full_cmd, void (*function)(Console*, const char *, void*))
{
if(!full_cmd) return; // Command must be defined
if(!function) return; // Function must be defines
if(strlen(full_cmd)>=CONSOLE_MAX_COMMAND_LENGTH) return;
if(mNbCmds==CONSOLE_MAX_COMMAND_NB) return;
mNbCmds++;
int HashIndex = full_cmd[0];
HashIndex = tolower(HashIndex);
ConsoleCommand* pcmd = mCmds[HashIndex];
if(!pcmd)
{
mCmds[HashIndex] = SAMPLE_NEW(ConsoleCommand);
pcmd = mCmds[HashIndex];
}
else
{
while(pcmd->next)
{
pcmd = pcmd->next;
}
pcmd->next = SAMPLE_NEW(ConsoleCommand);
pcmd = pcmd->next;
}
strcpy(pcmd->fullcmd, full_cmd);
pcmd->function = function;
pcmd->next = NULL;
}
bool Console::render(Renderer* rnd)
{
if(!rnd)
return false;
if(!mIsActive)
return true;
const PxU32 NbLines = NB_LINES;
const PxU32 FntHeight = 14;
PxU32 width, height;
rnd->getWindowSize(width, height);
const RendererColor backColor(3, 3, 39);
ScreenQuad sq;
sq.mX0 = 0.0f;
sq.mY0 = 20.0f/float(height);
sq.mX1 = 1.0f;
sq.mY1 = (20.0f + float((NbLines+2)*FntHeight))/float(height);
sq.mLeftUpColor = backColor;
sq.mRightUpColor = backColor;
sq.mLeftDownColor = backColor;
sq.mRightDownColor = backColor;
sq.mAlpha = 0.8f;
rnd->drawScreenQuad(sq);
PxU32 TextY = 24 + NbLines*FntHeight;
const PxReal scale = 0.4f;
const PxReal shadowOffset = 0.0f;
long temp = mViewBottom;
for(PxU32 i=0;i<NbLines;i++)
{
rnd->print(10, TextY, mBuffer[temp].mText, scale, shadowOffset, mBuffer[temp].mColor);
temp = (temp + 1) % CONSOLE_MAX_ROW;
TextY -= FntHeight; //size should come from renderer
}
return true;
}
// Process a single character
static bool gTabMode = false;
static PxU32 gTabIndex = 0;
static char gTabCmd[1024];
void Console::in(PxU32 wparam)
{
if(!mIsActive)
return;
if ((wparam >= 'a' && wparam <= 'z') || (wparam >= 'A' && wparam <= 'Z') || (wparam >= '0' && wparam <= '9') || wparam == ' ' || wparam == '.' || wparam == '-' || wparam == '_')
{
gTabMode = false;
if(mCol >= CONSOLE_MAX_COL-2) // We need 2 extra characters for the cursor and the final 0
return;
mBuffer[mNewline].mText[mCol++] = (char)wparam; // Append new character
mBuffer[mNewline].mText[mCol] = mLastChar[0]; // Append cursor
mBuffer[mNewline].mText[mCol+1] = '\0';
}
}
void Console::collectInputEvents(std::vector<const SampleFramework::InputEvent*>& inputEvents)
{
//digital keyboard events
DIGITAL_INPUT_EVENT_DEF(CONSOLE_OPEN, WKEY_TAB, XKEY_TAB, X1KEY_TAB, PS3KEY_TAB, PS4KEY_TAB, AKEY_UNKNOWN, OSXKEY_TAB, IKEY_UNKNOWN, LINUXKEY_TAB, WIIUKEY_UNKNOWN);
DIGITAL_INPUT_EVENT_DEF(CONSOLE_ESCAPE, WKEY_ESCAPE, XKEY_ESCAPE, X1KEY_ESCAPE, PS3KEY_ESCAPE, PS4KEY_ESCAPE, AKEY_UNKNOWN, OSXKEY_ESCAPE, IKEY_UNKNOWN, LINUXKEY_ESCAPE, WIIUKEY_UNKNOWN);
DIGITAL_INPUT_EVENT_DEF(CONSOLE_ENTER, WKEY_RETURN, XKEY_RETURN, X1KEY_RETURN, PS3KEY_RETURN, PS4KEY_RETURN, AKEY_UNKNOWN, OSXKEY_RETURN, IKEY_UNKNOWN, LINUXKEY_RETURN, WIIUKEY_UNKNOWN);
DIGITAL_INPUT_EVENT_DEF(CONSOLE_BACKSPACE, WKEY_BACKSPACE, XKEY_BACKSPACE, X1KEY_BACKSPACE, PS3KEY_BACKSPACE, PS4KEY_BACKSPACE, AKEY_UNKNOWN, OSXKEY_BACKSPACE, IKEY_UNKNOWN, LINUXKEY_BACKSPACE, WIIUKEY_UNKNOWN);
DIGITAL_INPUT_EVENT_DEF(CONSOLE_LIST_COMMAND_UP, WKEY_UP, XKEY_UP, X1KEY_UP, PS3KEY_UP, PS4KEY_UP, AKEY_UNKNOWN, OSXKEY_UP, IKEY_UNKNOWN, LINUXKEY_UP, WIIUKEY_UNKNOWN);
DIGITAL_INPUT_EVENT_DEF(CONSOLE_LIST_COMMAND_DOWN, WKEY_DOWN, XKEY_DOWN, X1KEY_DOWN, PS3KEY_DOWN, PS4KEY_DOWN, AKEY_UNKNOWN, OSXKEY_DOWN, IKEY_UNKNOWN, LINUXKEY_DOWN, WIIUKEY_UNKNOWN);
DIGITAL_INPUT_EVENT_DEF(CONSOLE_SCROLL_UP, WKEY_PRIOR, XKEY_PRIOR, X1KEY_PRIOR, PS3KEY_PRIOR, PS4KEY_PRIOR, AKEY_UNKNOWN, OSXKEY_PRIOR, IKEY_UNKNOWN, LINUXKEY_PRIOR, WIIUKEY_UNKNOWN);
DIGITAL_INPUT_EVENT_DEF(CONSOLE_SCROLL_DOWN, WKEY_NEXT, XKEY_NEXT, X1KEY_NEXT, PS3KEY_NEXT, PS4KEY_NEXT, AKEY_UNKNOWN, OSXKEY_NEXT, IKEY_UNKNOWN, LINUXKEY_NEXT, WIIUKEY_UNKNOWN);
}
//return true if we processed the key
void Console::onDigitalInputEvent(const SampleFramework::InputEvent& ie, bool val)
{
//if (val)
{
if (!mIsActive)
{
if (ie.m_Id == CONSOLE_OPEN)
{
mIsActive = true;
return;
}
}
else
{
if (!val)
{
switch (ie.m_Id)
{
case CONSOLE_OPEN:
if(!gTabMode)
{
gTabMode = true;
// Discard last character
mBuffer[mNewline].mText[mCol] = '\0';
// Discard prompt
long Index = (long)strlen(mPrompt);
strcpy(gTabCmd, &mBuffer[mNewline].mText[Index]);
}
char BestCmd[1024];
if(findBestCommand(BestCmd, gTabCmd, gTabIndex))
{
strcpy(mBuffer[mNewline].mText, mPrompt);
strcat(mBuffer[mNewline].mText, BestCmd);
strcat(mBuffer[mNewline].mText, mLastChar);
mCol = PxI32(strlen(mPrompt) + strlen(BestCmd));
}
else
{
gTabMode = false;
mBuffer[mNewline].mText[mCol] = mLastChar[0]; // Append cursor
mBuffer[mNewline].mText[mCol+1] = '\0';
}
break;
case CONSOLE_BACKSPACE:
gTabMode = false;
if(mCol>(long)strlen(mPrompt))
{
mBuffer[mNewline].mText[mCol] = '\0';
mBuffer[mNewline].mText[mCol-1] = mLastChar[0];
mCol--;
}
break;
case CONSOLE_ENTER:
gTabMode = false;
mBuffer[mNewline].mText[mCol] = '\0';
process();
advance();
strcpy(mBuffer[mNewline].mText, mPrompt);
strcat(mBuffer[mNewline].mText, mLastChar);
resetCol();
break;
case CONSOLE_ESCAPE:
mIsActive = false;
gTabMode = false;
break;
case CONSOLE_LIST_COMMAND_UP:
mCurcmd++;
if( mCurcmd > mNumcmdhist )
mCurcmd = mNumcmdhist;
cmdHistory();
break;
case CONSOLE_LIST_COMMAND_DOWN:
mCurcmd--;
if( mCurcmd <= 0 )
mCurcmd = 0;
cmdHistory();
break;
case CONSOLE_SCROLL_UP:
mViewBottom++;
if(mViewBottom >= CONSOLE_MAX_ROW) mViewBottom -= CONSOLE_MAX_ROW;
if(mViewBottom == mNewline - NB_LINES) mViewBottom--;
break;
case CONSOLE_SCROLL_DOWN:
mViewBottom--;
if(mViewBottom < 0) mViewBottom += CONSOLE_MAX_ROW;
if(mViewBottom == mNewline-1) mViewBottom = mNewline;
break;
}
}
}
}
}
void Console::onKeyDown(SampleFramework::SampleUserInput::KeyCode keyCode, PxU32 param)
{
//sschirm doesn't compile on snc
//const int keyparam = (int)param;
if (mIsActive)
{
if(param)
in(param);
}
}
|