Guest User

Untitled

a guest
Mar 2nd, 2014
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. void WinConsole::process()
  2. {
  3. if(winConsoleEnabled)
  4. {
  5. DWORD numEvents;
  6. GetNumberOfConsoleInputEvents(stdIn, &numEvents);
  7. if(numEvents)
  8. {
  9. INPUT_RECORD rec[20];
  10. char outbuf[512];
  11. S32 outpos = 0;
  12.  
  13. ReadConsoleInput(stdIn, rec, 20, &numEvents);
  14. DWORD i;
  15. for(i = 0; i < numEvents; i++)
  16. {
  17. if(rec[i].EventType == KEY_EVENT)
  18. {
  19. KEY_EVENT_RECORD *ke = &(rec[i].Event.KeyEvent);
  20. if(ke->bKeyDown)
  21. {
  22. switch (ke->uChar.AsciiChar)
  23. {
  24. // If no ASCII char, check if it's a handled virtual key
  25. case 0:
  26. switch (ke->wVirtualKeyCode)
  27. {
  28. // UP ARROW
  29. case 0x26 :
  30. // Go to the previous command in the cyclic array
  31. if ((-- iCmdIndex) < 0)
  32. iCmdIndex = MAX_CMDS - 1;
  33.  
  34. // If this command isn't empty ...
  35. if (rgCmds[iCmdIndex][0] != '\0')
  36. {
  37. // Obliterate current displayed text
  38. for (S32 i = outpos = 0; i < inpos; i ++)
  39. {
  40. outbuf[outpos ++] = '\b';
  41. outbuf[outpos ++] = ' ';
  42. outbuf[outpos ++] = '\b';
  43. }
  44.  
  45. // Copy command into command and display buffers
  46. for (inpos = 0; inpos < (S32)strlen(rgCmds[iCmdIndex]); inpos ++, outpos ++)
  47. {
  48. outbuf[outpos] = rgCmds[iCmdIndex][inpos];
  49. inbuf [inpos ] = rgCmds[iCmdIndex][inpos];
  50. }
  51. }
  52. // If previous is empty, stay on current command
  53. else if ((++ iCmdIndex) >= MAX_CMDS)
  54. {
  55. iCmdIndex = 0;
  56. }
  57.  
  58. break;
  59.  
  60. // DOWN ARROW
  61. case 0x28 : {
  62. // Go to the next command in the command array, if
  63. // it isn't empty
  64. if (rgCmds[iCmdIndex][0] != '\0' && (++ iCmdIndex) >= MAX_CMDS)
  65. iCmdIndex = 0;
  66.  
  67. // Obliterate current displayed text
  68. for (S32 i = outpos = 0; i < inpos; i ++)
  69. {
  70. outbuf[outpos ++] = '\b';
  71. outbuf[outpos ++] = ' ';
  72. outbuf[outpos ++] = '\b';
  73. }
  74.  
  75. // Copy command into command and display buffers
  76. for (inpos = 0; inpos < (S32)strlen(rgCmds[iCmdIndex]); inpos ++, outpos ++)
  77. {
  78. outbuf[outpos] = rgCmds[iCmdIndex][inpos];
  79. inbuf [inpos ] = rgCmds[iCmdIndex][inpos];
  80. }
  81. }
  82. break;
  83.  
  84. // LEFT ARROW
  85. case 0x25 :
  86. break;
  87.  
  88. // RIGHT ARROW
  89. case 0x27 :
  90. break;
  91.  
  92. default :
  93. break;
  94. }
  95. break;
  96. case '\b':
  97. if(inpos > 0)
  98. {
  99. outbuf[outpos++] = '\b';
  100. outbuf[outpos++] = ' ';
  101. outbuf[outpos++] = '\b';
  102. inpos--;
  103. }
  104. break;
  105. case '\t':
  106. // In the output buffer, we're going to have to erase the current line (in case
  107. // we're cycling through various completions) and write out the whole input
  108. // buffer, so (inpos * 3) + complen <= 512. Should be OK. The input buffer is
  109. // also 512 chars long so that constraint will also be fine for the input buffer.
  110. {
  111. // Erase the current line.
  112. U32 i;
  113. for (i = 0; i < inpos; i++) {
  114. outbuf[outpos++] = '\b';
  115. outbuf[outpos++] = ' ';
  116. outbuf[outpos++] = '\b';
  117. }
  118. // Modify the input buffer with the completion.
  119. U32 maxlen = 512 - (inpos * 3);
  120. if (ke->dwControlKeyState & SHIFT_PRESSED) {
  121. inpos = Con::tabComplete(inbuf, inpos, maxlen, false);
  122. }
  123. else {
  124. inpos = Con::tabComplete(inbuf, inpos, maxlen, true);
  125. }
  126. // Copy the input buffer to the output.
  127. for (i = 0; i < inpos; i++) {
  128. outbuf[outpos++] = inbuf[i];
  129. }
  130. }
  131. break;
  132. case '\n':
  133. case '\r':
  134. outbuf[outpos++] = '\r';
  135. outbuf[outpos++] = '\n';
  136.  
  137. inbuf[inpos] = 0;
  138. outbuf[outpos] = 0;
  139. printf("%s", outbuf);
  140.  
  141. S32 eventSize;
  142. eventSize = ConsoleEventHeaderSize;
  143.  
  144. dStrcpy(postEvent.data, inbuf);
  145. postEvent.size = eventSize + dStrlen(inbuf) + 1;
  146. Game->postEvent(postEvent);
  147.  
  148. // If we've gone off the end of our array, wrap
  149. // back to the beginning
  150. if (iCmdIndex >= MAX_CMDS)
  151. iCmdIndex %= MAX_CMDS;
  152.  
  153. // Put the new command into the array
  154. strcpy(rgCmds[iCmdIndex ++], inbuf);
  155.  
  156. printf("%s", Con::getVariable("Con::Prompt"));
  157. inpos = outpos = 0;
  158. break;
  159. default:
  160. inbuf[inpos++] = ke->uChar.AsciiChar;
  161. outbuf[outpos++] = ke->uChar.AsciiChar;
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. if(outpos)
  168. {
  169. outbuf[outpos] = 0;
  170. printf("%s", outbuf);
  171. }
  172. }
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment