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

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