Open Source Tomb Raider Engine
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Console.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. bool Console::visible = false;
  15. char Console::buffer[bufferLength + 1] = "";
  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 (!visible)
  63. return;
  64. static bool scrollToBottom = false;
  65. if (ImGui::Begin("Console", &visible, ImVec2(600, 400))) {
  66. if (lastLogLength != getLog().size()) {
  67. lastLogLength = getLog().size();
  68. scrollToBottom = true;
  69. }
  70. ImGui::BeginChild("ConsoleText", ImVec2(0, -ImGui::GetTextLineSpacing() * 2));
  71. for (unsigned long i = 0; i < getLog().size(); i++) {
  72. ImGui::TextUnformatted(getLog().get(i).c_str());
  73. }
  74. if (scrollToBottom) {
  75. ImGui::SetScrollPosHere();
  76. scrollToBottom = false;
  77. }
  78. ImGui::EndChild();
  79. bool focusInput = false;
  80. if (ImGui::InputText("Command", buffer, bufferLength,
  81. ImGuiInputTextFlags_EnterReturnsTrue
  82. | ImGuiInputTextFlags_CallbackCompletion
  83. | ImGuiInputTextFlags_CallbackHistory,
  84. &Console::callback)) {
  85. getLog() << "> " << buffer << Log::endl;
  86. if (strlen(buffer) > 0) {
  87. int error = Command::command(buffer);
  88. if (error != 0) {
  89. getLog() << "Error code: " << error << Log::endl;
  90. }
  91. if ((lastCommands.size() == 0) || (lastCommands[lastCommands.size() - 1] != buffer))
  92. lastCommands.push_back(std::string(buffer));
  93. }
  94. lastCommandIndex = -1;
  95. buffer[0] = '\0';
  96. scrollToBottom = true;
  97. focusInput = true;
  98. bufferedCommand.clear();
  99. }
  100. if (ImGui::IsItemHovered() || focusInput) {
  101. ImGui::SetKeyboardFocusHere(-1);
  102. focusInput = false;
  103. }
  104. }
  105. ImGui::End();
  106. }