ImGuiSidebar.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. #include <Utils/ImGuiSidebar.h>
  9. #include <Atom/Feature/ImGui/SystemBus.h>
  10. #include <Automation/ScriptableImGui.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzCore/Serialization/Utils.h>
  13. #include <AzCore/IO/SystemFile.h>
  14. #include <AzCore/IO/FileIO.h>
  15. namespace AtomSampleViewer
  16. {
  17. const float ImGuiSidebar::s_widthMin = 200.0f;
  18. const float ImGuiSidebar::s_widthMax = 1000.0f;
  19. const float ImGuiSidebar::s_widthStepSmall = 25.0f;
  20. const float ImGuiSidebar::s_widthStepBig = 100.0f;
  21. void ImGuiSidebar::Reflect(AZ::ReflectContext* context)
  22. {
  23. ImGuiSidebar::ConfigFile::Reflect(context);
  24. }
  25. void ImGuiSidebar::ConfigFile::Reflect(AZ::ReflectContext* context)
  26. {
  27. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  28. {
  29. serializeContext->Class<ConfigFile>()
  30. ->Version(0)
  31. ->Field("Width", &ConfigFile::m_width)
  32. ;
  33. }
  34. }
  35. ImGuiSidebar::ImGuiSidebar(AZStd::string_view configFilePath)
  36. {
  37. char configFileFullPath[AZ_MAX_PATH_LEN] = {0};
  38. AZ::IO::FileIOBase::GetInstance()->ResolvePath(configFilePath.data(), configFileFullPath, AZ_MAX_PATH_LEN);
  39. m_configFilePath = configFileFullPath;
  40. }
  41. bool ImGuiSidebar::LoadConfigFile()
  42. {
  43. // skip loading config file if the config file is not specified.
  44. if (m_configFilePath.empty())
  45. {
  46. return false;
  47. }
  48. AZStd::unique_ptr<ConfigFile> configFile(AZ::Utils::LoadObjectFromFile<ConfigFile>(m_configFilePath));
  49. if (configFile)
  50. {
  51. m_configFile = *configFile;
  52. return true;
  53. }
  54. else
  55. {
  56. return false;
  57. }
  58. }
  59. void ImGuiSidebar::SaveConfigFile()
  60. {
  61. if (!m_configFilePath.empty())
  62. {
  63. if (!AZ::Utils::SaveObjectToFile(m_configFilePath, AZ::DataStream::ST_XML, &m_configFile))
  64. {
  65. AZ_Error("ImGuiSidebar", false, "Failed to save '%s'", m_configFilePath.c_str());
  66. }
  67. }
  68. }
  69. void ImGuiSidebar::Activate()
  70. {
  71. // only load the config file if it's specified
  72. if ( (!m_configFilePath.empty()) && AZ::IO::SystemFile::Exists(m_configFilePath.c_str()) )
  73. {
  74. if (!LoadConfigFile())
  75. {
  76. AZ_Warning("ImGuiSidebar", false, "Failed to load sidebar config from %s.", m_configFilePath.c_str());
  77. }
  78. }
  79. }
  80. void ImGuiSidebar::Deactivate()
  81. {
  82. if (!m_configFilePath.empty())
  83. {
  84. // We only report this message in Deactivate(), not inside SaveConfigFile(), to avoid spamming
  85. // this message when SaveConfigFile() is called in OnTick()
  86. AZ_TracePrintf("ImGuiSidebar", "Saving settings to '%s'\n", m_configFilePath.c_str());
  87. SaveConfigFile();
  88. }
  89. }
  90. void ImGuiSidebar::SetHideSidebar(bool isHidden)
  91. {
  92. m_hideSidebar = isHidden;
  93. }
  94. AzFramework::WindowSize ImGuiSidebar::BeginFrame()
  95. {
  96. AzFramework::NativeWindowHandle windowHandle = nullptr;
  97. AzFramework::WindowSystemRequestBus::BroadcastResult(
  98. windowHandle,
  99. &AzFramework::WindowSystemRequestBus::Events::GetDefaultWindowHandle);
  100. AzFramework::WindowSize windowSize;
  101. AzFramework::WindowRequestBus::EventResult(
  102. windowSize,
  103. windowHandle, &AzFramework::WindowRequestBus::Events::GetRenderResolution);
  104. ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
  105. return windowSize;
  106. }
  107. void ImGuiSidebar::EndFrame()
  108. {
  109. ImGui::PopStyleVar();
  110. }
  111. bool ImGuiSidebar::Begin()
  112. {
  113. AZ_Assert(!m_isSidebarReady, "End() was not called");
  114. AzFramework::WindowSize windowSize = BeginFrame();
  115. // Apply some offset so we don't collide with the menu bar
  116. const float menuBarOffset = 18.0f;
  117. const float scale = ImGui::GetIO().FontGlobalScale;
  118. const float imguiWindowWidth = m_configFile.m_width * scale;
  119. const float imguiWindowHeight = windowSize.m_height - menuBarOffset;
  120. const float revealWindowWidth = 120.0f * scale;
  121. const float revealWindowHeight = 40.0f * scale;
  122. const ImGuiWindowFlags windowFlags =
  123. ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize |
  124. ImGuiWindowFlags_NoMove;
  125. bool configChanged = false;
  126. // We can't append to the main menu so instead we have a
  127. // smaller window that takes up less space
  128. if (m_hideSidebar)
  129. {
  130. ImGui::SetNextWindowPos(ImVec2(windowSize.m_width - revealWindowWidth, menuBarOffset));
  131. ImGui::SetNextWindowSize(ImVec2(revealWindowWidth, revealWindowHeight));
  132. if (ImGui::Begin("##RevealSidebar", nullptr, windowFlags))
  133. {
  134. if (ScriptableImGui::Button("Reveal Sidebar"))
  135. {
  136. m_hideSidebar = false;
  137. }
  138. ImGui::End();
  139. }
  140. }
  141. else
  142. {
  143. ImGui::SetNextWindowPos(ImVec2(windowSize.m_width - imguiWindowWidth, menuBarOffset));
  144. ImGui::SetNextWindowSize(ImVec2(imguiWindowWidth, imguiWindowHeight));
  145. if (ImGui::Begin("##Sidebar", nullptr, windowFlags))
  146. {
  147. const float itemSpacing = ImGui::GetStyle().ItemSpacing.x;
  148. // Make the "Hide Sidebar" button show up in about the same position as the "Reveal Sidebar" button
  149. const float rightMargin = 16.0f;
  150. const float buttonWidth = revealWindowWidth - rightMargin;
  151. ImGui::SameLine(imguiWindowWidth - revealWindowWidth + itemSpacing);
  152. if (ScriptableImGui::Button("Hide Sidebar", ImVec2(buttonWidth, 0)))
  153. {
  154. m_hideSidebar = true;
  155. }
  156. // Make the sidebar size buttons appear on the same line, aligned to the right, so they don't move.
  157. ImGui::NewLine();
  158. static float resizeButtonWidthSmall = 20.0f;
  159. static float resizeButtonWidthBig = 20.0f;
  160. float pos = resizeButtonWidthBig + itemSpacing;
  161. ImGui::SameLine(imguiWindowWidth - pos);
  162. if (ImGui::Button(" >> "))
  163. {
  164. m_configFile.m_width -= s_widthStepBig;
  165. m_configFile.m_width = AZStd::max(m_configFile.m_width, s_widthMin);
  166. configChanged = true;
  167. }
  168. resizeButtonWidthBig = ImGui::GetItemRectSize().x; // Get the actual width for the next frame
  169. pos += resizeButtonWidthSmall + itemSpacing;
  170. ImGui::SameLine(imguiWindowWidth - pos);
  171. if (ImGui::Button(" > "))
  172. {
  173. m_configFile.m_width -= s_widthStepSmall;
  174. m_configFile.m_width = AZStd::max(m_configFile.m_width, s_widthMin);
  175. configChanged = true;
  176. }
  177. resizeButtonWidthSmall = ImGui::GetItemRectSize().x; // Get the actual width for the next frame
  178. pos += resizeButtonWidthSmall + itemSpacing;
  179. ImGui::SameLine(imguiWindowWidth - pos);
  180. if (ImGui::Button(" < "))
  181. {
  182. m_configFile.m_width += s_widthStepSmall;
  183. m_configFile.m_width = AZStd::min(m_configFile.m_width, s_widthMax);
  184. configChanged = true;
  185. }
  186. pos += resizeButtonWidthBig + itemSpacing;
  187. ImGui::SameLine(imguiWindowWidth - pos);
  188. if (ImGui::Button(" << "))
  189. {
  190. m_configFile.m_width += s_widthStepBig;
  191. m_configFile.m_width = AZStd::min(m_configFile.m_width, s_widthMax);
  192. configChanged = true;
  193. }
  194. ImGui::Spacing();
  195. ImGui::Separator();
  196. ImGui::Spacing();
  197. m_isSidebarReady = true;
  198. }
  199. }
  200. if (configChanged)
  201. {
  202. SaveConfigFile();
  203. }
  204. if (!m_isSidebarReady)
  205. {
  206. EndFrame();
  207. }
  208. return m_isSidebarReady;
  209. }
  210. void ImGuiSidebar::End()
  211. {
  212. AZ_Assert(m_isSidebarReady, "Begin() was not called, or it returned false");
  213. ImGui::End(); // Ends the ImGui::Begin("##Sidebar",...)
  214. m_isSidebarReady = false;
  215. EndFrame();
  216. }
  217. } // namespace AtomSampleViewer