3
0

ImGuiManager.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #ifdef IMGUI_ENABLED
  10. #include <AzFramework/Input/Buses/Requests/InputSystemCursorRequestBus.h>
  11. #include <AzFramework/Input/Events/InputChannelEventListener.h>
  12. #include <AzFramework/Input/Events/InputTextEventListener.h>
  13. #include <AzFramework/Windowing/WindowBus.h>
  14. #include <ISystem.h>
  15. #include <imgui/imgui.h>
  16. #include <ImGuiBus.h>
  17. #include <VertexFormats.h>
  18. #if defined(LOAD_IMGUI_LIB_DYNAMICALLY) && !defined(AZ_MONOLITHIC_BUILD)
  19. # include <AzCore/Module/DynamicModuleHandle.h>
  20. #endif
  21. namespace AZ { class JobCompletion; }
  22. namespace ImGui
  23. {
  24. enum class ImGuiStateBroadcast
  25. {
  26. Broadcast,
  27. NotBroadcast,
  28. };
  29. struct ImGuiBroadcastState
  30. {
  31. ImGuiStateBroadcast m_activationBroadcastStatus = ImGuiStateBroadcast::NotBroadcast;
  32. ImGuiStateBroadcast m_deactivationBroadcastStatus = ImGuiStateBroadcast::NotBroadcast;
  33. };
  34. class ImGuiManager
  35. : public AzFramework::InputChannelEventListener
  36. , public AzFramework::InputTextEventListener
  37. , public ImGuiManagerBus::Handler
  38. , public AzFramework::WindowNotificationBus::Handler
  39. {
  40. public:
  41. void Initialize();
  42. void Shutdown();
  43. // This is called by the ImGuiGem to Register CVARs at the correct time
  44. void RegisterImGuiCVARs();
  45. protected:
  46. // -- ImGuiManagerBus Interface -------------------------------------------------------------------
  47. DisplayState GetDisplayState() const override { return m_clientMenuBarState; }
  48. void SetDisplayState(DisplayState state) override { m_clientMenuBarState = state; }
  49. bool IsControllerSupportModeEnabled(ImGuiControllerModeFlags::FlagType controllerMode) const override;
  50. void EnableControllerSupportMode(ImGuiControllerModeFlags::FlagType controllerMode, bool enable) override;
  51. void SetControllerMouseSensitivity(float sensitivity) override { m_controllerMouseSensitivity = sensitivity; }
  52. float GetControllerMouseSensitivity() const override { return m_controllerMouseSensitivity; }
  53. bool GetEnableDiscreteInputMode() const override { return m_enableDiscreteInputMode; }
  54. void SetEnableDiscreteInputMode(bool enabled) override { m_enableDiscreteInputMode = enabled; }
  55. ImGuiResolutionMode GetResolutionMode() const override { return m_resolutionMode; }
  56. void SetResolutionMode(ImGuiResolutionMode mode) override { m_resolutionMode = mode; }
  57. const ImVec2& GetImGuiRenderResolution() const override { return m_renderResolution; }
  58. void SetImGuiRenderResolution(const ImVec2& res) override { m_renderResolution = res; }
  59. void OverrideRenderWindowSize(uint32_t width, uint32_t height) override;
  60. void RestoreRenderWindowSizeToDefault() override;
  61. void SetDpiScalingFactor(float dpiScalingFactor) override;
  62. float GetDpiScalingFactor() const override;
  63. ImDrawData* GetImguiDrawData() override;
  64. void ToggleThroughImGuiVisibleState() override;
  65. void ToggleToImGuiVisibleState(DisplayState state) override;
  66. // -- ImGuiManagerBus Interface -------------------------------------------------------------------
  67. // -- AzFramework::InputChannelEventListener and AzFramework::InputTextEventListener Interface ------------
  68. bool OnInputChannelEventFiltered(const AzFramework::InputChannel& inputChannel) override;
  69. bool OnInputTextEventFiltered(const AZStd::string& textUTF8) override;
  70. int GetPriority() const override { return AzFramework::InputChannelEventListener::GetPriorityDebugUI(); }
  71. // -- AzFramework::InputChannelEventListener and AzFramework::InputTextEventListener Interface ------------
  72. // AzFramework::WindowNotificationBus::Handler overrides...
  73. void OnResolutionChanged(uint32_t width, uint32_t height) override;
  74. // Sets up initial window size and listens for changes
  75. void InitWindowSize();
  76. private:
  77. void RenderJob();
  78. ImGuiContext* m_imguiContext = nullptr;
  79. DisplayState m_clientMenuBarState = DisplayState::Hidden;
  80. // ImGui Resolution Settings
  81. ImGuiResolutionMode m_resolutionMode = ImGuiResolutionMode::MatchRenderResolution;
  82. ImVec2 m_renderResolution = ImVec2(1920.0f, 1080.0f);
  83. ImVec2 m_lastRenderResolution;
  84. AzFramework::WindowSize m_windowSize = AzFramework::WindowSize(1920, 1080);
  85. bool m_overridingWindowSize = false;
  86. // Rendering buffers
  87. std::vector<SVF_P3F_C4B_T2F> m_vertBuffer;
  88. std::vector<uint16> m_idxBuffer;
  89. //Controller navigation
  90. bool m_button1Pressed, m_button2Pressed, m_menuBarStatusChanged;
  91. bool m_hardwardeMouseConnected = false;
  92. bool m_enableDiscreteInputMode = false;
  93. AzFramework::SystemCursorState m_previousSystemCursorState = AzFramework::SystemCursorState::Unknown;
  94. ImGuiControllerModeFlags::FlagType m_controllerModeFlags = 0;
  95. float m_controllerMouseSensitivity = 4.0f;
  96. float m_controllerMousePosition[2] = { 0.0f, 0.0f };
  97. float m_lastPrimaryTouchPosition[2] = { 0.0f, 0.0f };
  98. bool m_useLastPrimaryTouchPosition = false;
  99. bool m_simulateBackspaceKeyPressed = false;
  100. ImGuiBroadcastState m_imGuiBroadcastState;
  101. AZ::JobCompletion* m_renderJobCompletion = nullptr;
  102. #if defined(LOAD_IMGUI_LIB_DYNAMICALLY) && !defined(AZ_MONOLITHIC_BUILD)
  103. AZStd::unique_ptr<AZ::DynamicModuleHandle> m_imgSharedLib;
  104. #endif // defined(LOAD_IMGUI_LIB_DYNAMICALLY) && !defined(AZ_MONOLITHIC_BUILD)
  105. };
  106. void AddMenuItemHelper(bool& control, const char* show, const char* hide);
  107. }
  108. #endif // ~IMGUI_ENABLED