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 6.0KB

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