From 2424193ea30187092fc7c3942821650b67388071 Mon Sep 17 00:00:00 2001 From: theastrowander Date: Tue, 9 Aug 2022 19:38:29 +0400 Subject: [PATCH 1/2] add font foe headlines, add GradientButtonValid, fix Separator --- source/MRViewer/ImGuiHelpers.cpp | 2 +- source/MRViewer/MRRibbonButtonDrawer.cpp | 42 ++++++++++++++++++++++++ source/MRViewer/MRRibbonButtonDrawer.h | 1 + source/MRViewer/MRRibbonConstants.h | 1 + source/MRViewer/MRRibbonFontManager.cpp | 31 ++++++++++++++++- source/MRViewer/MRRibbonFontManager.h | 2 ++ 6 files changed, 77 insertions(+), 2 deletions(-) diff --git a/source/MRViewer/ImGuiHelpers.cpp b/source/MRViewer/ImGuiHelpers.cpp index 9cc7d6395e52..9807c93f7d8b 100644 --- a/source/MRViewer/ImGuiHelpers.cpp +++ b/source/MRViewer/ImGuiHelpers.cpp @@ -784,7 +784,7 @@ void Separator( float scaling, const std::string& text ) { ImGui::Separator(); } - else if ( ImGui::BeginTable( "SeparatorTable", 2 ) ) + else if ( ImGui::BeginTable( (std::string("SeparatorTable_") + text).c_str(), 2, ImGuiTableFlags_SizingFixedFit ) ) { ImGui::TableNextColumn(); ImGui::Text( "%s", text.c_str()); diff --git a/source/MRViewer/MRRibbonButtonDrawer.cpp b/source/MRViewer/MRRibbonButtonDrawer.cpp index 25c54a8df439..86291b518956 100644 --- a/source/MRViewer/MRRibbonButtonDrawer.cpp +++ b/source/MRViewer/MRRibbonButtonDrawer.cpp @@ -74,6 +74,48 @@ bool RibbonButtonDrawer::GradientButton( const char* label, const ImVec2& size / return res; } +bool RibbonButtonDrawer::GradientButtonValid( const char* label, bool valid, const ImVec2& size /* = ImVec2(0, 0) */ ) +{ + auto& texture = GetGradientTexture(); + if ( !texture ) + return ImGui::Button( label, size ); + + ImGui::PushStyleColor( ImGuiCol_Button, ImVec4( 0, 0, 0, 0 ) ); + ImGui::PushStyleColor( ImGuiCol_Text, ImVec4( 1, 1, 1, 1 ) ); + + auto window = ImGui::GetCurrentContext()->CurrentWindow; + const ImGuiStyle& style = ImGui::GetStyle(); + const ImVec2 labelSize = ImGui::CalcTextSize( label, NULL, true ); + + int pushedStyleNum = 1; + ImGui::PushStyleVar( ImGuiStyleVar_FrameBorderSize, 0.0f ); + if ( size.y == 0 ) + { + auto framePadding = style.FramePadding; + framePadding.y = cGradientButtonFramePadding; + if ( auto menu = getViewerInstance().getMenuPlugin() ) + framePadding.y *= menu->menu_scaling(); + ImGui::PushStyleVar( ImGuiStyleVar_FramePadding, framePadding ); + ++pushedStyleNum; + } + + ImVec2 pos = window->DC.CursorPos; + ImVec2 realSize = ImGui::CalcItemSize( size, labelSize.x + style.FramePadding.x * 2.0f, labelSize.y + style.FramePadding.y * 2.0f ); + const ImRect bb( pos, ImVec2( pos.x + realSize.x, pos.y + realSize.y ) ); + + ImGui::GetCurrentContext()->CurrentWindow->DrawList->AddImageRounded( + texture->getImTextureId(), + bb.Min, bb.Max, + ImVec2( 0.5f, 0.25f ), ImVec2( 0.5f, 0.75f ), + Color::white().getUInt32(), style.FrameRounding ); + + auto res = ImGui::ButtonValid( label, valid, size ); + + ImGui::PopStyleVar( pushedStyleNum ); + ImGui::PopStyleColor( 2 ); + return res; +} + bool RibbonButtonDrawer::GradientCheckbox( const char* label, bool* value ) { auto& texture = GetGradientTexture(); diff --git a/source/MRViewer/MRRibbonButtonDrawer.h b/source/MRViewer/MRRibbonButtonDrawer.h index 72b00a3b5641..f4ce3eba9594 100644 --- a/source/MRViewer/MRRibbonButtonDrawer.h +++ b/source/MRViewer/MRRibbonButtonDrawer.h @@ -41,6 +41,7 @@ class MRVIEWER_CLASS RibbonButtonDrawer /// draw gradient button MRVIEWER_API static bool GradientButton( const char* label, const ImVec2& size = ImVec2( 0, 0 ) ); + MRVIEWER_API static bool GradientButtonValid( const char* label, bool valid, const ImVec2& size = ImVec2( 0, 0 ) ); /// draw gradient checkbox MRVIEWER_API static bool GradientCheckbox( const char* label, bool* value ); /// draw gradient checkbox diff --git a/source/MRViewer/MRRibbonConstants.h b/source/MRViewer/MRRibbonConstants.h index d59f5b350027..de55c803021c 100644 --- a/source/MRViewer/MRRibbonConstants.h +++ b/source/MRViewer/MRRibbonConstants.h @@ -43,6 +43,7 @@ constexpr float cBigIconSize = 20.0f; constexpr int cSmallFontSize = 11; constexpr int cDefaultFontSize = 13; constexpr int cBigFontSize = 15; +constexpr int cHeadlineFontSize = 24; constexpr float cPaletteDiscretizationScaling = 5.0f / 18.0f; diff --git a/source/MRViewer/MRRibbonFontManager.cpp b/source/MRViewer/MRRibbonFontManager.cpp index 61a22c0ad38b..f77b3c82fc9b 100644 --- a/source/MRViewer/MRRibbonFontManager.cpp +++ b/source/MRViewer/MRRibbonFontManager.cpp @@ -10,7 +10,7 @@ namespace MR void RibbonFontManager::loadAllFonts( ImWchar* charRanges, float scaling ) { - fonts_ = { nullptr,nullptr,nullptr,nullptr }; + fonts_ = {}; const ImWchar iconRanges[] = { 0xe005, 0xf8ff, 0 }; @@ -40,6 +40,11 @@ float RibbonFontManager::getFontSizeByType( FontType type ) const return cSmallFontSize; case MR::RibbonFontManager::FontType::Icons: return cBigIconSize; + case MR::RibbonFontManager::FontType::Headline: + return cHeadlineFontSize; + case MR::RibbonFontManager::FontType::Big: + case MR::RibbonFontManager::FontType::BigSemiBold: + return cBigFontSize; case MR::RibbonFontManager::FontType::Count: default: return 0.f; @@ -108,6 +113,30 @@ void RibbonFontManager::loadFont_( FontType type, const ImWchar* ranges, float s &config, ranges ); fonts_[int( type )] = ImGui::GetIO().Fonts->Fonts.back(); } + else if ( type == FontType::BigSemiBold ) + { + auto fontPath = getMenuFontPath(); + fontPath = fontPath.parent_path() / "NotoSans-SemiBold.ttf"; + ImFontConfig config; + config.FontBuilderFlags = ImGuiFreeTypeBuilderFlags_Bitmap; + config.GlyphOffset = ImVec2( 0, -4 * scaling ); + ImGui::GetIO().Fonts->AddFontFromFileTTF( + utf8string( fontPath ).c_str(), cBigFontSize * scaling, + &config, ranges ); + fonts_[int( type )] = ImGui::GetIO().Fonts->Fonts.back(); + } + else if ( type == FontType::Headline ) + { + auto fontPath = getMenuFontPath(); + fontPath = fontPath.parent_path() / "NotoSans-SemiBold.ttf"; + ImFontConfig config; + config.FontBuilderFlags = ImGuiFreeTypeBuilderFlags_Bitmap; + config.GlyphOffset = ImVec2( 0, -4 * scaling ); + ImGui::GetIO().Fonts->AddFontFromFileTTF( + utf8string( fontPath ).c_str(), cHeadlineFontSize * scaling, + &config, ranges ); + fonts_[int( type )] = ImGui::GetIO().Fonts->Fonts.back(); + } } void RibbonFontManager::loadDefaultFont_( float fontSize, float yOffset ) diff --git a/source/MRViewer/MRRibbonFontManager.h b/source/MRViewer/MRRibbonFontManager.h index d3b3cda94572..4313e0781c5f 100644 --- a/source/MRViewer/MRRibbonFontManager.h +++ b/source/MRViewer/MRRibbonFontManager.h @@ -19,6 +19,8 @@ class MRVIEWER_CLASS RibbonFontManager SemiBold, Icons, Big, + BigSemiBold, + Headline, Count }; From 34aad98c6f9b6b60f2f8d74d7c866f8d089f1518 Mon Sep 17 00:00:00 2001 From: theastrowander Date: Tue, 9 Aug 2022 20:07:22 +0400 Subject: [PATCH 2/2] code review --- source/MRViewer/MRRibbonButtonDrawer.cpp | 2 +- source/MRViewer/MRRibbonButtonDrawer.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/source/MRViewer/MRRibbonButtonDrawer.cpp b/source/MRViewer/MRRibbonButtonDrawer.cpp index 86291b518956..13431af935ae 100644 --- a/source/MRViewer/MRRibbonButtonDrawer.cpp +++ b/source/MRViewer/MRRibbonButtonDrawer.cpp @@ -78,7 +78,7 @@ bool RibbonButtonDrawer::GradientButtonValid( const char* label, bool valid, con { auto& texture = GetGradientTexture(); if ( !texture ) - return ImGui::Button( label, size ); + return ImGui::ButtonValid( label, valid, size ); ImGui::PushStyleColor( ImGuiCol_Button, ImVec4( 0, 0, 0, 0 ) ); ImGui::PushStyleColor( ImGuiCol_Text, ImVec4( 1, 1, 1, 1 ) ); diff --git a/source/MRViewer/MRRibbonButtonDrawer.h b/source/MRViewer/MRRibbonButtonDrawer.h index f4ce3eba9594..6902f96995c8 100644 --- a/source/MRViewer/MRRibbonButtonDrawer.h +++ b/source/MRViewer/MRRibbonButtonDrawer.h @@ -41,6 +41,7 @@ class MRVIEWER_CLASS RibbonButtonDrawer /// draw gradient button MRVIEWER_API static bool GradientButton( const char* label, const ImVec2& size = ImVec2( 0, 0 ) ); + /// draw gradient button, which can be disabled (valid = false) MRVIEWER_API static bool GradientButtonValid( const char* label, bool valid, const ImVec2& size = ImVec2( 0, 0 ) ); /// draw gradient checkbox MRVIEWER_API static bool GradientCheckbox( const char* label, bool* value );