Open Source Tomb Raider Engine
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Console.cpp 7.4KB

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