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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. std::string Console::bufferedCommand;
  20. void Console::callback(ImGuiTextEditCallbackData* data) {
  21. bool update = false;
  22. std::string completion;
  23. switch (data->EventKey) {
  24. case ImGuiKey_Tab:
  25. completion = Command::autoComplete(data->Buf);
  26. if (completion.size() > 0) {
  27. data->DeleteChars(0, data->BufSize);
  28. data->InsertChars(0, completion.c_str());
  29. data->CursorPos = strlen(data->Buf);
  30. }
  31. break;
  32. case ImGuiKey_UpArrow:
  33. update = true;
  34. if (lastCommandIndex < (long)(lastCommands.size() - 1)) {
  35. lastCommandIndex++;
  36. if (lastCommandIndex == 0)
  37. bufferedCommand = data->Buf;
  38. }
  39. break;
  40. case ImGuiKey_DownArrow:
  41. update = true;
  42. if (lastCommandIndex > -1)
  43. lastCommandIndex--;
  44. break;
  45. }
  46. if (update) {
  47. if ((lastCommandIndex >= 0) && (lastCommandIndex < lastCommands.size())) {
  48. if (lastCommands.at(lastCommands.size() - 1 - lastCommandIndex) != buffer) {
  49. data->DeleteChars(0, data->BufSize);
  50. data->InsertChars(0, lastCommands.at(lastCommands.size() - 1 - lastCommandIndex).c_str());
  51. }
  52. } else {
  53. data->DeleteChars(0, data->BufSize);
  54. if (bufferedCommand.size() > 0) {
  55. data->InsertChars(0, bufferedCommand.c_str());
  56. }
  57. }
  58. data->CursorPos = strlen(data->Buf);
  59. }
  60. }
  61. void Console::display() {
  62. if (ImGui::Begin("Console", nullptr, ImVec2(600, 400), -1.0f)) {
  63. if (lastLogLength != getLog().size()) {
  64. lastLogLength = getLog().size();
  65. scrollToBottom = true;
  66. }
  67. ImGui::BeginChild("ConsoleText", ImVec2(ImGui::GetWindowWidth(), ImGui::GetWindowSize().y - 70));
  68. for (unsigned long i = 0; i < getLog().size(); i++) {
  69. ImGui::Text("%s", getLog().get(i).c_str());
  70. }
  71. if (scrollToBottom) {
  72. ImGui::SetScrollPosHere();
  73. scrollToBottom = false;
  74. }
  75. ImGui::EndChild();
  76. if (ImGui::InputText("Command", buffer, bufferLength,
  77. ImGuiInputTextFlags_EnterReturnsTrue
  78. | ImGuiInputTextFlags_CallbackCompletion
  79. | ImGuiInputTextFlags_CallbackHistory,
  80. &Console::callback)) {
  81. getLog() << "> " << buffer << Log::endl;
  82. if (strlen(buffer) > 0) {
  83. int error = Command::command(buffer);
  84. if (error != 0) {
  85. getLog() << "Error code: " << error << Log::endl;
  86. }
  87. if ((lastCommands.size() == 0) || (lastCommands[lastCommands.size() - 1] != buffer))
  88. lastCommands.push_back(std::string(buffer));
  89. }
  90. lastCommandIndex = -1;
  91. buffer[0] = '\0';
  92. scrollToBottom = true;
  93. focusInput = true;
  94. bufferedCommand.clear();
  95. }
  96. if (ImGui::IsItemHovered() || focusInput) {
  97. ImGui::SetKeyboardFocusHere(-1);
  98. focusInput = false;
  99. }
  100. }
  101. ImGui::End();
  102. }