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

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