Skip to content

Commit

Permalink
Font and style config refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesugb committed Oct 21, 2023
1 parent e915d53 commit c79e559
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
38 changes: 27 additions & 11 deletions auto_vk_toolkit/include/imgui_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<avk::renderpass> aRenderpassToUse = {}, std::function<void(float uiScale)> aAlterSettingsBeforeCreation = {})
imgui_manager(avk::queue& aQueueToSubmitTo, std::string aName = "imgui_manager", std::optional<avk::renderpass> aRenderpassToUse = {}, std::function<void(float)> 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());
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -152,8 +168,8 @@ namespace avk
bool mOccupyMouseLastFrame = false;

// customization
std::function<void(float uiScale)> mAlterSettingsBeforeCreation = {};
custom_font_mode mCustomFontMode = custom_font_mode::automatic;
std::function<void(float)> mAlterSettingsBeforeInitialization = {};
font_mode mFontMode = font_mode::automatic;
};

}
10 changes: 6 additions & 4 deletions auto_vk_toolkit/src/imgui_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion examples/model_loader/source/model_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion examples/skinned_meshlets/source/skinned_meshlets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
};
Expand Down

0 comments on commit c79e559

Please sign in to comment.