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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. mLineOffset = 0;
  86. }
  87. getFont().drawText(10, 10, 0.70f, BLUE,
  88. "%s uptime %lus scroll %d%%", VERSION, systemTimerGet() / 1000, scrollIndicator);
  89. // Draw output log
  90. int end = lineCount;
  91. int drawOffset = 0;
  92. int historyOffset = 0;
  93. if (mHistory.size() < lineCount) {
  94. end = mHistory.size();
  95. drawOffset = lineCount - mHistory.size();
  96. } else if (lineCount < mHistory.size()) {
  97. historyOffset = mHistory.size() - lineCount;
  98. }
  99. for (int i = 0; i < end; i++) {
  100. getFont().drawText(10, ((i + drawOffset) * lineSteps) + firstLine,
  101. 0.75f, BLUE, "%s", mHistory[i + historyOffset - mLineOffset]);
  102. }
  103. // Draw current input
  104. if ((mInputBufferPointer > 0) && (mInputBuffer[0] != '\0')) {
  105. getFont().drawText(10, inputLine, 0.75f, BLUE, "> %s", mInputBuffer);
  106. } else {
  107. getFont().drawText(10, inputLine, 0.75f, BLUE, ">");
  108. }
  109. //! \todo display the current mPartialInput. The UTF-8 segfaults SDL-TTF, somehow?
  110. }
  111. }
  112. void Console::handleKeyboard(KeyboardButton key, bool pressed) {
  113. if (pressed && (key == enterKey)) {
  114. // Execute entered command
  115. if ((mInputBufferPointer > 0) && (mInputBuffer[0] != '\0')) {
  116. print("> %s", mInputBuffer);
  117. mCommandHistory.push_back(bufferString("%s", mInputBuffer));
  118. int error = getOpenRaider().command(mInputBuffer);
  119. if (error != 0) {
  120. print("Error Code: %d", error);
  121. }
  122. } else {
  123. print("> ");
  124. }
  125. // Clear partial and input buffer
  126. mInputBufferPointer = 0;
  127. mInputBuffer[0] = '\0';
  128. if (mPartialInput != NULL) {
  129. delete [] mPartialInput;
  130. mPartialInput = NULL;
  131. }
  132. mHistoryPointer = 0;
  133. }
  134. //! \fixme only deleting the last byte is not valid for non-ASCII UTF-8 strings
  135. if (pressed && (key == backspaceKey)) {
  136. if (mInputBufferPointer > 0) {
  137. mInputBufferPointer--;
  138. mInputBuffer[mInputBufferPointer] = '\0';
  139. }
  140. }
  141. if (pressed && ((key == upKey) || (key == downKey))) {
  142. moveInHistory(key == upKey);
  143. }
  144. }
  145. void Console::moveInHistory(bool up) {
  146. if (mCommandHistory.size() == 0)
  147. return;
  148. if (up) {
  149. if (mHistoryPointer < mCommandHistory.size()) {
  150. mHistoryPointer++;
  151. if (mHistoryPointer == 1) {
  152. mUnfinishedInput = bufferString("%s", mInputBuffer);
  153. }
  154. } else {
  155. return;
  156. }
  157. } else {
  158. if (mHistoryPointer > 0)
  159. mHistoryPointer--;
  160. else
  161. return;
  162. }
  163. if ((mHistoryPointer > 0) && (mHistoryPointer <= mCommandHistory.size())) {
  164. strcpy(mInputBuffer, mCommandHistory[mCommandHistory.size() - mHistoryPointer]);
  165. mInputBufferPointer = strlen(mInputBuffer);
  166. } else {
  167. if (mUnfinishedInput != NULL) {
  168. strcpy(mInputBuffer, mUnfinishedInput);
  169. mInputBufferPointer = strlen(mInputBuffer);
  170. delete [] mUnfinishedInput;
  171. mUnfinishedInput = NULL;
  172. } else {
  173. mInputBuffer[0] = '\0';
  174. mInputBufferPointer = 0;
  175. }
  176. }
  177. }
  178. void Console::handleText(char *text, bool notFinished) {
  179. // Always scroll to bottom when text input is received
  180. mLineOffset = 0;
  181. if (!notFinished) {
  182. // Finished entering character
  183. // delete previous partial character, if present
  184. if (mPartialInput != NULL) {
  185. delete [] mPartialInput;
  186. }
  187. //! \fixme Temporary hack filtering the console activation key
  188. if (text[0] == '`')
  189. return;
  190. // Append new input to buffer
  191. size_t length = strlen(text);
  192. if (length > 0) {
  193. if (((INPUT_BUFFER_SIZE - mInputBufferPointer) < length)) {
  194. print("Console input buffer overflowed! (> %d)", INPUT_BUFFER_SIZE);
  195. return;
  196. }
  197. strcpy((mInputBuffer + mInputBufferPointer), text);
  198. mInputBufferPointer += length;
  199. mInputBuffer[mInputBufferPointer] = '\0';
  200. }
  201. } else {
  202. // Partial character received
  203. mPartialInput = bufferString("%s", text);
  204. }
  205. }
  206. void Console::handleMouseScroll(int xrel, int yrel) {
  207. assert((xrel != 0) || (yrel != 0));
  208. LINE_GEOMETRY(getWindow());
  209. if (mHistory.size() > lineCount) {
  210. if (yrel > 0) {
  211. if (mLineOffset < (mHistory.size() - lineCount)) {
  212. mLineOffset++;
  213. }
  214. } else if (yrel < 0) {
  215. if (mLineOffset > 0) {
  216. mLineOffset--;
  217. }
  218. }
  219. }
  220. }