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

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