DebugConsole.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/Console/ILogger.h>
  10. #include <AzCore/Math/Color.h>
  11. #include <AzCore/Debug/TraceMessageBus.h>
  12. #include <AzCore/std/containers/deque.h>
  13. #include <AzCore/std/string/string.h>
  14. #include <Atom/RPI.Public/ViewportContextBus.h>
  15. #ifdef IMGUI_ENABLED
  16. # include <imgui/imgui.h>
  17. # include <ImGuiBus.h>
  18. #endif
  19. struct ImGuiInputTextCallbackData;
  20. ////////////////////////////////////////////////////////////////////////////////////////////////////
  21. namespace AZ
  22. {
  23. #if !defined(IMGUI_ENABLED)
  24. class DebugConsole {};
  25. #else
  26. ////////////////////////////////////////////////////////////////////////////////////////////////
  27. //! A debug console used to enter debug console commands and display debug log messages.
  28. //!
  29. //! Toggled using any of the following:
  30. //! - The '~' key on a keyboard.
  31. //! - Both the 'L3+R3' buttons on a gamepad.
  32. //! - The fourth finger press on a touch screen.
  33. class DebugConsole
  34. : public AZ::Debug::TraceMessageBus::Handler
  35. , public ImGui::ImGuiUpdateListenerBus::Handler
  36. {
  37. //! The default maximum number of entries to display in the debug log.
  38. static constexpr int DefaultMaxEntriesToDisplay = 1028;
  39. //! The default maximum number of input history items to retain.
  40. static constexpr int DefaultMaxInputHistorySize = 512;
  41. public:
  42. //! Allocator
  43. AZ_CLASS_ALLOCATOR(DebugConsole, AZ::SystemAllocator);
  44. //! Constructor
  45. //! \param[in] maxEntriesToDisplay The maximum number of entries to display in the debug log.
  46. //! \param[in] maxInputHistorySize The maximum number of text input history items to retain.
  47. DebugConsole(int maxEntriesToDisplay = DefaultMaxEntriesToDisplay,
  48. int maxInputHistorySize = DefaultMaxInputHistorySize);
  49. //! Disable copying
  50. AZ_DISABLE_COPY_MOVE(DebugConsole);
  51. //! Destructor
  52. ~DebugConsole() override;
  53. //! AZ::Debug::TraceMessageBus
  54. bool OnPreError(const char* window, const char* fileName, int line, const char* func, const char* message) override;
  55. bool OnPreWarning(const char* window, const char* fileName, int line, const char* func, const char* message) override;
  56. bool OnPrintf(const char* window, const char* message) override;
  57. //! ImGui::ImGuiUpdateListenerBus overrides
  58. //! @{
  59. void OnImGuiMainMenuUpdate() override;
  60. void OnImGuiUpdate() override;
  61. //! @}
  62. //! Add a string to the debug log display.
  63. //! \param[in] debugLogString The string to add to the debug log display.
  64. //! \param[in] color The color in which to display the above string.
  65. void AddDebugLog(const AZStd::string& debugLogString, const AZ::Color& color = AZ::Colors::White);
  66. //! Clears the debug log display.
  67. void ClearDebugLog();
  68. //! Attempt to auto complete a command using this input text callback data.
  69. //! \param[in] data The input text callback data used try and auto complete.
  70. void AutoCompleteCommand(ImGuiInputTextCallbackData* data);
  71. //! Attempt to browse the input history using this input text callback data.
  72. //! \param[in] data The input text callback data used to try and browse history.
  73. void BrowseInputHistory(ImGuiInputTextCallbackData* data);
  74. //! Called when the user enters text input.
  75. //! \param[in] inputText The text input that was entered.
  76. void OnTextInputEntered(const char* inputText);
  77. private:
  78. void AddDebugLog(const char* window, const char* debugLogString, AZ::LogLevel logLevel);
  79. AZStd::deque<AZStd::pair<AZStd::string, AZ::Color>> m_debugLogEntires; //!< All debug logs.
  80. AZStd::deque<AZStd::string> m_textInputHistory; //!< History of input that has been entered.
  81. char m_inputBuffer[1028] = {}; //!< The character buffer used to accept text input.
  82. int m_currentHistoryIndex = -1; //!< The current index into the input history when browsing.
  83. int m_maxEntriesToDisplay = DefaultMaxEntriesToDisplay; //!< The maximum entries to display.
  84. int m_maxInputHistorySize = DefaultMaxInputHistorySize; //!< The maximum input history size.
  85. bool m_autoScroll = true; //!< Should we auto-scroll as new entries are added?
  86. bool m_forceScroll = false; //!< Do we need to force scroll after input entered?
  87. };
  88. #endif // defined(IMGUI_ENABLED)
  89. } // namespace AZ