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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*!
  2. * \file src/Console.cpp
  3. * \brief Console class
  4. *
  5. * \author xythobuz
  6. */
  7. #include <cstring>
  8. #include "global.h"
  9. #include "Log.h"
  10. #include "UI.h"
  11. #include "commands/Command.h"
  12. #include "Console.h"
  13. char Console::buffer[bufferLength + 1] = "";
  14. bool Console::scrollToBottom = false;
  15. bool Console::focusInput = false;
  16. unsigned long Console::lastLogLength = 0;
  17. std::vector<std::string> Console::lastCommands;
  18. long Console::lastCommandIndex = -1;
  19. void Console::display() {
  20. if (ImGui::Begin("Console", NULL, ImVec2(600, 400), -1.0f)) {
  21. if (lastLogLength != getLog().size()) {
  22. lastLogLength = getLog().size();
  23. scrollToBottom = true;
  24. }
  25. ImGui::BeginChild("ConsoleText", ImVec2(ImGui::GetWindowWidth(), ImGui::GetWindowSize().y - 70));
  26. for (unsigned long i = 0; i < getLog().size(); i++) {
  27. ImGui::Text("%s", getLog().get(i).c_str());
  28. }
  29. if (scrollToBottom) {
  30. ImGui::SetScrollPosHere();
  31. scrollToBottom = false;
  32. }
  33. ImGui::EndChild();
  34. if (focusInput) {
  35. ImGui::SetKeyboardFocusHere();
  36. focusInput = false;
  37. }
  38. if (ImGui::GetWindowIsFocused()) {
  39. ImGuiIO& io = ImGui::GetIO();
  40. static bool hold = false;
  41. bool update = false;
  42. if ((!hold) && (io.KeysDown[upKey] || io.KeysDown[downKey]))
  43. update = hold = true;
  44. if (hold && (!(io.KeysDown[upKey] || io.KeysDown[downKey])))
  45. hold = false;
  46. if (update) {
  47. if (io.KeysDown[upKey]) {
  48. if (lastCommandIndex < static_cast<long>(lastCommands.size() - 1))
  49. lastCommandIndex++;
  50. }
  51. if (io.KeysDown[downKey]) {
  52. if (lastCommandIndex > -1)
  53. lastCommandIndex--;
  54. }
  55. if ((lastCommandIndex >= 0) && (lastCommandIndex < lastCommands.size())) {
  56. strncpy(buffer,
  57. lastCommands.at(lastCommands.size() - 1 - lastCommandIndex).c_str(),
  58. bufferLength);
  59. buffer[bufferLength] = '\0';
  60. } else {
  61. buffer[0] = '\0';
  62. }
  63. update = false;
  64. }
  65. }
  66. if (ImGui::InputText("Command", buffer, bufferLength,
  67. ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) {
  68. getLog() << "> " << buffer << Log::endl;
  69. int error = Command::command(buffer);
  70. if (error != 0) {
  71. getLog() << "Error code: " << error << Log::endl;
  72. }
  73. if ((lastCommands.size() == 0) || (lastCommands[lastCommands.size() - 1] != buffer))
  74. lastCommands.push_back(std::string(buffer));
  75. lastCommandIndex = -1;
  76. buffer[0] = '\0';
  77. scrollToBottom = true;
  78. focusInput = true;
  79. }
  80. }
  81. ImGui::End();
  82. }