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

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