summaryrefslogtreecommitdiffstats
path: root/game/code/console/debugconsolecallback.cpp
blob: 77f014eec80ef47d0139d019ae07058c30affaa2 (plain) (blame)
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
//=============================================================================
// Copyright (C) 2002 Radical Entertainment Ltd.  All rights reserved.
//
// File:        debugconsolecallback.cpp
//
// Description: 
//
// History:     + Created. Thanks to Mirth for this. -- Darwin Chau
//
//=============================================================================

//========================================
// System Includes
//========================================
// Radcore
#include <raddebugconsole.hpp>
// Pure3D
#include <p3d/utility.hpp>


//========================================
// Project Includes
//========================================
#include <console/debugconsolecallback.h>
#include <console/console.h>
#include <console/fbstricmp.h>

#include <string.h>


//******************************************************************************
//
// Global Data, Local Data, Local Classes
//
//******************************************************************************


//******************************************************************************
//
// Public Member Functions
//
//******************************************************************************


//==============================================================================
// DebugConsoleCallback::DebugConsoleCallback
//==============================================================================
//
// Description: 
//
// Parameters:  
//
// Return:      
//
//==============================================================================
DebugConsoleCallback::DebugConsoleCallback()
    :
    mFirstHistoryIndex( 0 ),
    mLastHistoryIndex( 0 ),
    mGetHistoryIndex( 0 ),
    mCursorPosition( 0 )
{
    mConsoleHistory[0][0] = '\0';
    mTabCompleteString  = "";
    mTabCurrentFunction = "";
}


//==============================================================================
// DebugConsoleCallback::~DebugConsoleCallback
//==============================================================================
//
// Description: 
//
// Parameters:  
//
// Return:      
//
//==============================================================================
DebugConsoleCallback::~DebugConsoleCallback()
{
}


//==============================================================================
// DebugConsoleCallback::OnVKey
//==============================================================================
//
// Description: 
//
// Parameters:  
//
// Return:      
//
//==============================================================================
void DebugConsoleCallback::OnVKey
(
    int virtualKey, 
    bool ctrl, 
    bool shift, 
    bool alt
)
{
#ifdef RAD_DEBUG
    //set the bool so that OnChar() will not process the event as well...
    mInputHandled = true;

    //see if the ctrl key was pressed
    if (ctrl)
    {
        switch (virtualKey)
        {
            //cut
            case 'X':
	            p3d::printf("DEBUG cut not yet implemented.");
                break;

            //copy:
            case 'C':
	            p3d::printf("DEBUG copy not yet implemented.");
                break;

            //paste:
            case 'V':
	            p3d::printf("DEBUG paste not yet implemented.");
                break;
        }
    }
    else
    {
        switch (virtualKey)
        {
            case VK_RETURN:
                GetConsole()->Printf("=>%s", mConsoleHistory[0]);
	            GetConsole()->Evaluate(mConsoleHistory[0], "CMDLine");

                //only copy the current console history if we have a non-empty line
                if (mConsoleHistory[0][0] != '\0')
                {
                    //don't duplicate exact history entries...
                    if (mLastHistoryIndex == 0 || smStricmp(mConsoleHistory[0], mConsoleHistory[mLastHistoryIndex]))
                    {
                        //copy the current console history[0] into the next history buffer
                        mLastHistoryIndex++;
                        if (mLastHistoryIndex >= CONSOLE_HISTORY_SIZE)
                            mLastHistoryIndex = 1;
                        strcpy(mConsoleHistory[mLastHistoryIndex], mConsoleHistory[0]);

                        //update the "firstHistory" buffer index
                        if (mFirstHistoryIndex == 0)
                            mFirstHistoryIndex = 1;
                        else if (mLastHistoryIndex == mFirstHistoryIndex)
                            mFirstHistoryIndex++;
                    }
                }

                //reset the "getHistory" index
                mGetHistoryIndex = 0;

                //reset the current buffer
                mConsoleHistory[0][0] = '\0';

                //reset the cursor position
                mCursorPosition = 0;

                //reset the tab completion string
                mTabCompleteString = "";

                break;

            case VK_BACK:
                //delete from the current cursor position
                if (mCursorPosition > 0)
                {
                    //copy from the cursor position into a temp buffer
                    char temp[Console::MAX_STRING_LENGTH];
                    strcpy(temp, &mConsoleHistory[0][mCursorPosition]);

                    //decriment the cursor position, and copy the string back
                    mCursorPosition--;
                    strcpy(&mConsoleHistory[0][mCursorPosition], temp);

                    //reset the tab completion string
                    mTabCompleteString = "";
                }

                break;

            case VK_DELETE:
                //delete the character at the cursor position
                if( mCursorPosition < static_cast<int>(strlen(mConsoleHistory[0])) )
                {
                    //copy from the cursor position + 1 into a temp buffer
                    char temp[Console::MAX_STRING_LENGTH];
                    strcpy(temp, &mConsoleHistory[0][mCursorPosition + 1]);

                    //and copy the string back to the cursor position
                    strcpy(&mConsoleHistory[0][mCursorPosition], temp);

                    //reset the tab completion string
                    mTabCompleteString = "";
                }
                break;

            case VK_UP:
                //retrieve the previous available history
                if (mGetHistoryIndex == 0)
                    mGetHistoryIndex = mLastHistoryIndex;
                else
                {
                    //see if we've run out of history
                    if (mGetHistoryIndex == mFirstHistoryIndex)
                        break;

                    mGetHistoryIndex--;
                    if (mGetHistoryIndex <= 0)
                        mGetHistoryIndex = CONSOLE_HISTORY_SIZE - 1;
                }

                //now copy the history into the "current" entry buffer
                strcpy(mConsoleHistory[0], mConsoleHistory[mGetHistoryIndex]);

                //set the new cursor position
                mCursorPosition = strlen(mConsoleHistory[0]);

                //reset the tab completion string
                mTabCompleteString = "";

                break;

            case VK_DOWN:
                //retrieve the next available history
                if (mGetHistoryIndex == 0 || mGetHistoryIndex == mLastHistoryIndex)
                    break;

                mGetHistoryIndex++;
                if (mGetHistoryIndex >= CONSOLE_HISTORY_SIZE)
                    mGetHistoryIndex = 1;

                //now copy the history into the "current" entry buffer
                strcpy(mConsoleHistory[0], mConsoleHistory[mGetHistoryIndex]);

                //set the new cursor position
                mCursorPosition = strlen(mConsoleHistory[0]);

                //reset the tab completion string
                mTabCompleteString = "";

                break;

            case VK_LEFT:
                if (mCursorPosition > 0)
                    mCursorPosition--;
                break;

            case VK_RIGHT:
                if (mCursorPosition < static_cast<int>(strlen(mConsoleHistory[0])) - 1)
                    mCursorPosition++;
                break;

            case VK_HOME:
                mCursorPosition = 0;
                break;

            case VK_END:
                mCursorPosition = static_cast<int>(strlen(mConsoleHistory[0]));
                break;

            case VK_ESCAPE:
                //reset the "getHistory" index
                mGetHistoryIndex = 0;

                //reset the current buffer
                mConsoleHistory[0][0] = '\0';

                //reset the cursor position
                mCursorPosition = 0;

                //reset the tab completion string
                mTabCompleteString = "";

                break;

            case VK_TAB:
            {
                //make sure we have *something* to tabcomplete
                if (mConsoleHistory[0][0])
                {
                    //initialize the tab search
                    if ( mTabCompleteString.GetUID() != static_cast< tUID >( 0 ) )
                    {
                        mTabCompleteString = mConsoleHistory[ 0 ];
                        mTabCurrentFunction = "";
                    }

                    const tNameInsensitive& tabCompleteFunction = GetConsole()->TabCompleteFunction(mTabCompleteString.GetText(), mTabCurrentFunction.GetText() );
                    if( tabCompleteFunction.GetUID() != static_cast< tUID >( 0 ) )
                    {
                        //copy the tabCompleteFunction
                        mTabCurrentFunction  = tabCompleteFunction;

                        //create the new console entry string
		                char tabConsoleEntry[Console::MAX_STRING_LENGTH];
                        sprintf(tabConsoleEntry, "%s();", tabCompleteFunction.GetText() );
                        strcpy(mConsoleHistory[0], tabConsoleEntry);
                        mCursorPosition = strlen(mConsoleHistory[0]) - 2;
                    }
                }

                break;
            }

            default:
                //set the bool to allow OnChar() to handling this event
                mInputHandled = false;
                break;
        }
    }

    //set the new debug console line
    if (mInputHandled)
    {
        GetConsole()->SetDebugConsoleEntry(mConsoleHistory[0], mCursorPosition);
    }
#endif //RAD_DEBUG
}


//==============================================================================
// DebugConsoleCallback::OnChar
//==============================================================================
//
// Description: 
//
// Parameters:  
//
// Return:      
//
//==============================================================================
void DebugConsoleCallback::OnChar(int asciiKey)
{
#ifdef RAD_DEBUG
    //if the bool is set, OnVKey already handled the event
    if (mInputHandled)
        return;

    //anything not handled by OnVKey will fall through and be handled here...
    int length = strlen(mConsoleHistory[0]);
    if (asciiKey >= 0x20 && length < Console::MAX_STRING_LENGTH - 1)
    {
        //add the character to the console entry at the cursor position
        char temp[Console::MAX_STRING_LENGTH];
        strcpy(temp, &mConsoleHistory[0][mCursorPosition]);

        //insert the character and increment the cursor position
        mConsoleHistory[0][mCursorPosition++] = asciiKey;
        strcpy(&mConsoleHistory[0][mCursorPosition], temp);

        //reset the tab completion string
        mTabCompleteString = "";

        //set the new debug console line
        GetConsole()->SetDebugConsoleEntry(mConsoleHistory[0], mCursorPosition);
    }
#endif //RAD_DEBUG
}


void DebugConsoleCallback::OnButtonClick( int xTextPixels, int yTextPixels, int xScreenPixels, int yScreenPixels, bool ctrl, bool shift, bool alt )
{
}
void DebugConsoleCallback::OnButtonDown( int xTextPixels, int yTextPixels, int xScreenPixels, int yScreenPixels, bool ctrl, bool shift, bool alt )
{
}
void DebugConsoleCallback::OnButtonUp( int xTextPixels, int yTextPixels, int xScreenPixels, int yScreenPixels, bool ctrl, bool shift, bool alt )
{
}
void DebugConsoleCallback::OnButtonMove( int xTextPixels, int yTextPixels, int xScreenPixels, int yScreenPixels, bool ctrl, bool shift, bool alt, bool bottondown )
{
}