From c79e559b59e8b882fac1007bdea544548c18895a Mon Sep 17 00:00:00 2001 From: Johannes Unterguggenberger Date: Sat, 21 Oct 2023 10:59:18 +0200 Subject: [PATCH] Font and style config refactoring --- auto_vk_toolkit/include/imgui_manager.hpp | 38 +++++++++++++------ auto_vk_toolkit/src/imgui_manager.cpp | 10 +++-- examples/model_loader/source/model_loader.cpp | 7 +++- .../source/skinned_meshlets.cpp | 2 +- 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/auto_vk_toolkit/include/imgui_manager.hpp b/auto_vk_toolkit/include/imgui_manager.hpp index 03bb4eeb..4df9a21f 100644 --- a/auto_vk_toolkit/include/imgui_manager.hpp +++ b/auto_vk_toolkit/include/imgui_manager.hpp @@ -10,22 +10,35 @@ namespace avk class imgui_manager : public invokee { public: - enum struct custom_font_mode + /** Enum for configuring which font ImGui shall use + */ + enum struct font_mode { - automatic, // choose font automatically, based on the display scale + /** Automatically select whether to use ImGui's default font or a TTF, based on the UI scale. */ + automatic, + + /** Use ImGui's default font */ use_default_font, - use_custom_font + + /** Use a True Type Font which comes bundled with Auto-Vk-Toolkit */ + use_ttf }; /** Create an ImGui manager element. - * @param aName You *can* give it a name, but you can also leave it at the default name "imgui_manager". - * @param aAlterSettingsBeforeCreation Allows to modify the ImGui style + * @param aName You *can* give it a name, but you can also leave it at the default name "imgui_manager". + * @param aAlterSettingsBeforeInitialization Allows to modify the ImGui style, e.g., like follows: + * [](float uiScale) { + * auto& style = ImGui::GetStyle(); + * style = ImGuiStyle(); // reset to default style (for non-color settings, like rounded corners) + * ImGui::StyleColorsClassic(); // change color theme + * style.ScaleAllSizes(uiScale); // and scale + * } */ - imgui_manager(avk::queue& aQueueToSubmitTo, std::string aName = "imgui_manager", std::optional aRenderpassToUse = {}, std::function aAlterSettingsBeforeCreation = {}) + imgui_manager(avk::queue& aQueueToSubmitTo, std::string aName = "imgui_manager", std::optional aRenderpassToUse = {}, std::function aAlterSettingsBeforeInitialization = {}) : invokee(std::move(aName)) , mQueue { &aQueueToSubmitTo } , mUserInteractionEnabled{ true } - , mAlterSettingsBeforeCreation{ std::move(aAlterSettingsBeforeCreation) } + , mAlterSettingsBeforeInitialization{ std::move(aAlterSettingsBeforeInitialization) } { if (aRenderpassToUse.has_value()) { mRenderpass = std::move(aRenderpassToUse.value()); @@ -98,8 +111,11 @@ namespace avk mUsingSemaphoreInsteadOfFenceForFontUpload = true; } - void set_custom_font_mode(custom_font_mode aMode) { - mCustomFontMode = aMode; + /** Configure imgui_manager which font to use. + * @param aMode Pass font_mode::automatic to let imgui_manager decide, or a different value to decide on your own. + */ + void set_font_mode(font_mode aMode) { + mFontMode = aMode; } /** Indicates whether or not ImGui wants to occupy the mouse. @@ -152,8 +168,8 @@ namespace avk bool mOccupyMouseLastFrame = false; // customization - std::function mAlterSettingsBeforeCreation = {}; - custom_font_mode mCustomFontMode = custom_font_mode::automatic; + std::function mAlterSettingsBeforeInitialization = {}; + font_mode mFontMode = font_mode::automatic; }; } diff --git a/auto_vk_toolkit/src/imgui_manager.cpp b/auto_vk_toolkit/src/imgui_manager.cpp index b8a1966a..d59a4be6 100644 --- a/auto_vk_toolkit/src/imgui_manager.cpp +++ b/auto_vk_toolkit/src/imgui_manager.cpp @@ -55,8 +55,10 @@ namespace avk float uiScale = fontSize / baseFontSize; uint8_t* data = nullptr; - bool use_default_font = (mCustomFontMode == custom_font_mode::automatic) ? (glm::abs(uiScale - 1.f) < 1e-5f) : (mCustomFontMode == custom_font_mode::use_default_font); - if (use_default_font) { + bool useDefaultFont = mCustomFontMode == custom_font_mode::automatic + ? glm::abs(uiScale - 1.f) < 1e-5f + : mCustomFontMode == custom_font_mode::use_default_font; + if (useDefaultFont) { io.Fonts->AddFontDefault(); } else { @@ -88,8 +90,8 @@ namespace avk style.WindowRounding = 2.f; style.ScaleAllSizes(uiScale); - if (mAlterSettingsBeforeCreation) { - mAlterSettingsBeforeCreation(uiScale); // allow the user to change the style + if (mAlterSettingsBeforeInitialization) { + mAlterSettingsBeforeInitialization(uiScale); // allow the user to change the style } // Setup Platform/Renderer bindings diff --git a/examples/model_loader/source/model_loader.cpp b/examples/model_loader/source/model_loader.cpp index d02c6154..58a984dd 100644 --- a/examples/model_loader/source/model_loader.cpp +++ b/examples/model_loader/source/model_loader.cpp @@ -455,7 +455,12 @@ int main() // <== Starting point == // Create an instance of our main avk::element which contains all the functionality: auto app = model_loader_app(singleQueue); // Create another element for drawing the UI with ImGui - auto ui = avk::imgui_manager(singleQueue); + auto ui = avk::imgui_manager(singleQueue, "imgui_manager", {}, [](float uiScale) { + auto& style = ImGui::GetStyle(); + style = ImGuiStyle(); // reset to default style (for non-color settings, like rounded corners) + ImGui::StyleColorsClassic(); // change color theme + style.ScaleAllSizes(uiScale); // and scale + }); // Compile all the configuration parameters and the invokees into a "composition": auto composition = configure_and_compose( diff --git a/examples/skinned_meshlets/source/skinned_meshlets.cpp b/examples/skinned_meshlets/source/skinned_meshlets.cpp index 52c2b276..9109903e 100644 --- a/examples/skinned_meshlets/source/skinned_meshlets.cpp +++ b/examples/skinned_meshlets/source/skinned_meshlets.cpp @@ -421,7 +421,7 @@ class skinned_meshlets_app : public avk::invokee avk::descriptor_binding(3, 4, avk::as_storage_buffers(mIndicesDataBuffers)), #endif avk::descriptor_binding(3, 5, avk::as_uniform_texel_buffer_views(mBoneIndicesBuffers)), - avk::descriptor_binding(3, 6, avk::as_uniform_texel_buffer_views(mBoneWeightsBuffers)), + avk::descriptor_binding(3, 6, avk::as_uniform_texel_buffer_views(mBonhttps://github.com/cg-tuwien/MeshShaderSkinningCulling/tree/master/assetseWeightsBuffers)), avk::descriptor_binding(4, 0, mMeshletsBuffer) ); };