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.3KB

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