Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Console.cpp 7.5KB

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