Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Console.cpp 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*!
  2. * \file src/Console.cpp
  3. * \brief Console 'overlay'
  4. *
  5. * \author xythobuz
  6. */
  7. #include <iostream>
  8. #include "global.h"
  9. #include "Font.h"
  10. #include "OpenRaider.h"
  11. #include "utils/strings.h"
  12. #include "utils/time.h"
  13. #include "Window.h"
  14. #include "Console.h"
  15. #define INPUT_BUFFER_SIZE 255
  16. Console::Console() {
  17. mVisible = false;
  18. mInputBuffer = new char[INPUT_BUFFER_SIZE + 1];
  19. mInputBuffer[INPUT_BUFFER_SIZE] = '\0';
  20. mInputBufferPointer = 0;
  21. mPartialInput = NULL;
  22. mHistoryPointer = 0;
  23. mUnfinishedInput = NULL;
  24. mLineOffset = 0;
  25. }
  26. Console::~Console() {
  27. delete [] mInputBuffer;
  28. mInputBuffer = NULL;
  29. delete [] mPartialInput;
  30. mPartialInput = NULL;
  31. delete [] mUnfinishedInput;
  32. mUnfinishedInput = NULL;
  33. while (mHistory.size() > 0) {
  34. delete [] mHistory.back();
  35. mHistory.pop_back();
  36. }
  37. while (mCommandHistory.size() > 0) {
  38. delete [] mCommandHistory.back();
  39. mCommandHistory.pop_back();
  40. }
  41. }
  42. void Console::setVisible(bool visible) {
  43. mVisible = visible;
  44. getWindow().setTextInput(mVisible);
  45. }
  46. bool Console::isVisible() {
  47. return mVisible;
  48. }
  49. void Console::print(const char *s, ...) {
  50. va_list args;
  51. va_start(args, s);
  52. char *tmp = bufferString(s, args);
  53. va_end(args);
  54. if (tmp != NULL) {
  55. mHistory.push_back(tmp);
  56. #ifdef DEBUG
  57. std::cout << tmp << std::endl;
  58. #endif
  59. }
  60. }
  61. #define LINE_GEOMETRY(window) unsigned int firstLine = 35; \
  62. unsigned int lastLine = (window.getHeight() / 2) - 55; \
  63. unsigned int inputLine = (window.getHeight() / 2) - 30; \
  64. unsigned int lineSteps = 20; \
  65. unsigned int lineCount = (lastLine - firstLine + lineSteps) / lineSteps; \
  66. while (((lineCount * lineSteps) + firstLine) < inputLine) { \
  67. lineSteps++; \
  68. lineCount = (lastLine - firstLine + lineSteps) / lineSteps; \
  69. }
  70. void Console::display() {
  71. if (mVisible) {
  72. // Calculate line drawing geometry
  73. // Depends on window height, so recalculate every time
  74. LINE_GEOMETRY(getWindow());
  75. // Draw half-transparent *overlay*
  76. glColor4f(0.0f, 0.0f, 0.0f, 0.75f);
  77. glDisable(GL_TEXTURE_2D);
  78. glRecti(0, 0, getWindow().getWidth(), getWindow().getHeight() / 2);
  79. glEnable(GL_TEXTURE_2D);
  80. int scrollIndicator;
  81. if (mHistory.size() > lineCount) {
  82. scrollIndicator = (mHistory.size() - lineCount - mLineOffset) * 100 / (mHistory.size() - lineCount);
  83. } else {
  84. scrollIndicator = 100;
  85. }
  86. getFont().drawText(10, 10, 0.70f, BLUE,
  87. "%s uptime %lus scroll %d%%", VERSION, systemTimerGet() / 1000, scrollIndicator);
  88. // Draw output log
  89. int end = lineCount;
  90. int drawOffset = 0;
  91. int historyOffset = 0;
  92. if (mHistory.size() < lineCount) {
  93. end = mHistory.size();
  94. drawOffset = lineCount - mHistory.size();
  95. } else if (lineCount < mHistory.size()) {
  96. historyOffset = mHistory.size() - lineCount;
  97. }
  98. for (int i = 0; i < end; i++) {
  99. getFont().drawText(10, ((i + drawOffset) * lineSteps) + firstLine,
  100. 0.75f, BLUE, "%s", mHistory[i + historyOffset - mLineOffset]);
  101. }
  102. // Draw current input
  103. if ((mInputBufferPointer > 0) && (mInputBuffer[0] != '\0')) {
  104. getFont().drawText(10, inputLine, 0.75f, BLUE, "> %s", mInputBuffer);
  105. } else {
  106. getFont().drawText(10, inputLine, 0.75f, BLUE, ">");
  107. }
  108. //! \todo display the current mPartialInput. The UTF-8 segfaults SDL-TTF, somehow?
  109. }
  110. }
  111. void Console::handleKeyboard(KeyboardButton key, bool pressed) {
  112. if (pressed && (key == enterKey)) {
  113. // Execute entered command
  114. if ((mInputBufferPointer > 0) && (mInputBuffer[0] != '\0')) {
  115. print("> %s", mInputBuffer);
  116. mCommandHistory.push_back(bufferString("%s", mInputBuffer));
  117. int error = getOpenRaider().command(mInputBuffer);
  118. if (error != 0) {
  119. print("Error Code: %d", error);
  120. }
  121. } else {
  122. print("> ");
  123. }
  124. // Clear partial and input buffer
  125. mInputBufferPointer = 0;
  126. mInputBuffer[0] = '\0';
  127. if (mPartialInput != NULL) {
  128. delete [] mPartialInput;
  129. mPartialInput = NULL;
  130. }
  131. mHistoryPointer = 0;
  132. }
  133. //! \fixme only deleting the last byte is not valid for non-ASCII UTF-8 strings
  134. if (pressed && (key == backspaceKey)) {
  135. if (mInputBufferPointer > 0) {
  136. mInputBufferPointer--;
  137. mInputBuffer[mInputBufferPointer] = '\0';
  138. }
  139. }
  140. if (pressed && ((key == upKey) || (key == downKey))) {
  141. moveInHistory(key == upKey);
  142. }
  143. }
  144. void Console::moveInHistory(bool up) {
  145. if (mCommandHistory.size() == 0)
  146. return;
  147. if (up) {
  148. if (mHistoryPointer < mCommandHistory.size()) {
  149. mHistoryPointer++;
  150. if (mHistoryPointer == 1) {
  151. mUnfinishedInput = bufferString("%s", mInputBuffer);
  152. }
  153. } else {
  154. return;
  155. }
  156. } else {
  157. if (mHistoryPointer > 0)
  158. mHistoryPointer--;
  159. else
  160. return;
  161. }
  162. if ((mHistoryPointer > 0) && (mHistoryPointer <= mCommandHistory.size())) {
  163. strcpy(mInputBuffer, mCommandHistory[mCommandHistory.size() - mHistoryPointer]);
  164. mInputBufferPointer = strlen(mInputBuffer);
  165. } else {
  166. if (mUnfinishedInput != NULL) {
  167. strcpy(mInputBuffer, mUnfinishedInput);
  168. mInputBufferPointer = strlen(mInputBuffer);
  169. delete [] mUnfinishedInput;
  170. mUnfinishedInput = NULL;
  171. } else {
  172. mInputBuffer[0] = '\0';
  173. mInputBufferPointer = 0;
  174. }
  175. }
  176. }
  177. void Console::handleText(char *text, bool notFinished) {
  178. // Always scroll to bottom when text input is received
  179. mLineOffset = 0;
  180. if (!notFinished) {
  181. // Finished entering character
  182. // delete previous partial character, if present
  183. if (mPartialInput != NULL) {
  184. delete [] mPartialInput;
  185. }
  186. //! \fixme Temporary hack filtering the console activation key
  187. if (text[0] == '`')
  188. return;
  189. // Append new input to buffer
  190. size_t length = strlen(text);
  191. if (length > 0) {
  192. if (((INPUT_BUFFER_SIZE - mInputBufferPointer) < length)) {
  193. print("Console input buffer overflowed! (> %d)", INPUT_BUFFER_SIZE);
  194. return;
  195. }
  196. strcpy((mInputBuffer + mInputBufferPointer), text);
  197. mInputBufferPointer += length;
  198. mInputBuffer[mInputBufferPointer] = '\0';
  199. }
  200. } else {
  201. // Partial character received
  202. mPartialInput = bufferString("%s", text);
  203. }
  204. }
  205. void Console::handleMouseScroll(int xrel, int yrel) {
  206. assert((xrel != 0) || (yrel != 0));
  207. LINE_GEOMETRY(getWindow());
  208. if (mHistory.size() > lineCount) {
  209. if (yrel > 0) {
  210. if (mLineOffset < (mHistory.size() - lineCount)) {
  211. mLineOffset++;
  212. }
  213. } else if (yrel < 0) {
  214. if (mLineOffset > 0) {
  215. mLineOffset--;
  216. }
  217. }
  218. }
  219. }