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

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