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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "utf8-cpp/utf8.h"
  12. #include "utils/strings.h"
  13. #include "utils/time.h"
  14. #include "Window.h"
  15. #include "Console.h"
  16. Console::Console() {
  17. mVisible = false;
  18. mHistoryPointer = 0;
  19. mLineOffset = 0;
  20. }
  21. void Console::setVisible(bool visible) {
  22. mVisible = visible;
  23. getWindow().setTextInput(mVisible);
  24. }
  25. bool Console::isVisible() {
  26. return mVisible;
  27. }
  28. template<typename T>
  29. Console &Console::operator<<(T t) {
  30. printBuffer << t;
  31. if (printBuffer.str().back() == '\n') {
  32. mHistory.push_back(printBuffer.str().substr(0, printBuffer.str().length() - 1));
  33. #ifdef DEBUG
  34. std::cout << printBuffer.str().substr(0, printBuffer.str().length() - 1) << std::endl;
  35. #endif
  36. printBuffer.str("");
  37. }
  38. return (*this);
  39. }
  40. // Deprecated!
  41. void Console::print(const char *s, ...) {
  42. va_list args;
  43. va_start(args, s);
  44. char *tmp = bufferString(s, args);
  45. va_end(args);
  46. if (tmp != nullptr)
  47. (*this) << tmp << endl;
  48. delete [] tmp;
  49. }
  50. #define LINE_GEOMETRY(window) \
  51. unsigned int firstLine = 35; \
  52. unsigned int lastLine = (window.getHeight() / 2) - 55; \
  53. unsigned int inputLine = (window.getHeight() / 2) - 30; \
  54. unsigned int lineSteps = 20; \
  55. unsigned int lineCount = (lastLine - firstLine + lineSteps) / lineSteps; \
  56. while (((lineCount * lineSteps) + firstLine) < inputLine) { \
  57. lineSteps++; \
  58. lineCount = (lastLine - firstLine + lineSteps) / lineSteps; \
  59. }
  60. void Console::display() {
  61. if (!mVisible)
  62. return;
  63. // Calculate line drawing geometry
  64. // Depends on window height, so recalculate every time
  65. LINE_GEOMETRY(getWindow());
  66. // Draw half-transparent *overlay*
  67. glColor4f(0.0f, 0.0f, 0.0f, 0.75f);
  68. glDisable(GL_TEXTURE_2D);
  69. glRecti(0, 0, getWindow().getWidth(), getWindow().getHeight() / 2);
  70. glEnable(GL_TEXTURE_2D);
  71. unsigned long scrollIndicator;
  72. if (mHistory.size() > lineCount) {
  73. scrollIndicator = (mHistory.size() - lineCount - mLineOffset) * 100 / (mHistory.size() - lineCount);
  74. } else {
  75. scrollIndicator = 100;
  76. mLineOffset = 0;
  77. }
  78. // Draw status line
  79. getFont().drawText(10, 10, 0.70f, BLUE,
  80. "%s uptime %lus scroll %d%%", VERSION, systemTimerGet() / 1000, scrollIndicator);
  81. // Draw output log
  82. long end = lineCount;
  83. long drawOffset = 0;
  84. long historyOffset = 0;
  85. if (mHistory.size() < lineCount) {
  86. end = mHistory.size();
  87. drawOffset = lineCount - mHistory.size();
  88. } else if (lineCount < mHistory.size()) {
  89. historyOffset = mHistory.size() - lineCount;
  90. }
  91. for (int i = 0; i < end; i++) {
  92. getFont().drawText(10, (unsigned int)((i + drawOffset) * lineSteps) + firstLine,
  93. 0.75f, BLUE, "%s", mHistory[i + historyOffset - mLineOffset].c_str());
  94. }
  95. // Draw current input
  96. getFont().drawText(10, inputLine, 0.75f, BLUE, "> %s", (mInputBuffer + mPartialInput).c_str());
  97. }
  98. void Console::handleKeyboard(KeyboardButton key, bool pressed) {
  99. if (pressed && (key == enterKey)) {
  100. // Execute entered command
  101. if (mInputBuffer.length() > 0) {
  102. print("> %s", mInputBuffer.c_str());
  103. mCommandHistory.push_back(mInputBuffer.c_str());
  104. int error = getOpenRaider().command(mInputBuffer);
  105. if (error != 0) {
  106. print("Error Code: %d", error);
  107. }
  108. } else {
  109. print("> ");
  110. }
  111. // Clear partial and input buffer
  112. mInputBuffer = "";
  113. mPartialInput = "";
  114. mHistoryPointer = 0;
  115. }
  116. // Delete last character
  117. if (pressed && (key == backspaceKey)) {
  118. if ((mPartialInput.length() == 0)
  119. && (mInputBuffer.length() > 0)) {
  120. utf8::iterator<std::string::iterator> it(mInputBuffer.end(), mInputBuffer.begin(), mInputBuffer.end());
  121. mInputBuffer.erase((--it).base(), mInputBuffer.end());
  122. }
  123. }
  124. if (pressed && ((key == upKey) || (key == downKey))) {
  125. moveInHistory(key == upKey);
  126. }
  127. }
  128. void Console::moveInHistory(bool up) {
  129. if (mCommandHistory.size() == 0)
  130. return;
  131. if (up) {
  132. if (mHistoryPointer < mCommandHistory.size()) {
  133. mHistoryPointer++;
  134. if (mHistoryPointer == 1) {
  135. mUnfinishedInput = mInputBuffer;
  136. }
  137. } else {
  138. return;
  139. }
  140. } else {
  141. if (mHistoryPointer > 0)
  142. mHistoryPointer--;
  143. else
  144. return;
  145. }
  146. if ((mHistoryPointer > 0) && (mHistoryPointer <= mCommandHistory.size())) {
  147. mInputBuffer = mCommandHistory[mCommandHistory.size() - mHistoryPointer];
  148. } else {
  149. if (mUnfinishedInput.length() > 0) {
  150. mInputBuffer = mUnfinishedInput;
  151. mUnfinishedInput = "";
  152. } else {
  153. mInputBuffer = "";
  154. }
  155. }
  156. }
  157. void Console::handleText(char *text, bool notFinished) {
  158. // Always scroll to bottom when text input is received
  159. mLineOffset = 0;
  160. if (!notFinished) {
  161. // Finished entering character
  162. // delete previous partial character, if present
  163. mPartialInput = "";
  164. //! \fixme Temporary hack filtering the console activation key
  165. if (text[0] == '`')
  166. return;
  167. // Append new input to buffer
  168. mInputBuffer += text;
  169. } else {
  170. // Partial character received
  171. mPartialInput = text;
  172. }
  173. }
  174. void Console::handleMouseScroll(int xrel, int yrel) {
  175. assert((xrel != 0) || (yrel != 0));
  176. LINE_GEOMETRY(getWindow());
  177. if (mHistory.size() > lineCount) {
  178. if (yrel > 0) {
  179. if (mLineOffset < (mHistory.size() - lineCount)) {
  180. mLineOffset++;
  181. }
  182. } else if (yrel < 0) {
  183. if (mLineOffset > 0) {
  184. mLineOffset--;
  185. }
  186. }
  187. }
  188. }