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

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