Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Console.cpp 6.1KB

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