Skip to content

Commit da68c0c

Browse files
authored
Improve buttonEx params and deprecate old version (#4818)
* Imporve buttonEx params and deprecate old version * typo fix * swap params order to have simpler abi * try avoid inconsistent abi warning
1 parent c966f22 commit da68c0c

File tree

4 files changed

+53
-32
lines changed

4 files changed

+53
-32
lines changed

source/MRViewer/MRRibbonSceneObjectsListDrawer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ void RibbonSceneObjectsListDrawer::drawObjectLine_( Object& object, const std::s
257257
ImGui::PushStyleColor( ImGuiCol_ButtonHovered, Color::gray().scaledAlpha( 0.2f ).getUInt32() );
258258
UI::ButtonCustomizationParams params;
259259
params.forceImGuiBackground = true;
260-
UI::buttonEx( ( "##SelectBtn_" + object.name() + "_" + uniqueStr ).c_str(), true, Vector2f( -1, cFrameHeight ), ImGuiButtonFlags_AllowOverlap, params );
260+
params.flags = ImGuiButtonFlags_AllowOverlap;
261+
UI::buttonEx( ( "##SelectBtn_" + object.name() + "_" + uniqueStr ).c_str(), Vector2f( -1, cFrameHeight ), params );
261262
if ( ImGui::IsItemHovered( ImGuiHoveredFlags_AllowWhenBlockedByActiveItem ) && needDragDropTarget_() )
262263
{
263264
auto rect = context->LastItemData.Rect;
@@ -325,7 +326,7 @@ void RibbonSceneObjectsListDrawer::drawEyeButton_( Object& object, const std::st
325326
ImGui::PushStyleColor( ImGuiCol_Button, ImVec4( 0, 0, 0, 0 ) );
326327
ImGui::PushStyleColor( ImGuiCol_ButtonHovered, ImVec4( 0, 0, 0, 0 ) );
327328
ImGui::PushStyleColor( ImGuiCol_ButtonActive, ImVec4( 0, 0, 0, 0 ) );
328-
bool changed = UI::buttonEx( ( "##VisibilityBtn_" + object.name() + "_" + uniqueStr ).c_str(), true, ImVec2( -1, cFrameHeight ), 0, params );
329+
bool changed = UI::buttonEx( ( "##VisibilityBtn_" + object.name() + "_" + uniqueStr ).c_str(), ImVec2( -1, cFrameHeight ), params );
329330
ImGui::PopStyleColor( 3 );
330331
ImGui::PopStyleVar();
331332

source/MRViewer/MRUIStyle.cpp

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,11 @@ void init()
181181
textureR->update( data );
182182
}
183183

184-
bool buttonEx( const char* label, bool active, const Vector2f& size_arg /*= Vector2f( 0, 0 )*/,
185-
ImGuiButtonFlags flags /*= ImGuiButtonFlags_None*/, const ButtonCustomizationParams& custmParams )
184+
bool buttonEx( const char* label, const Vector2f& size_arg /*= Vector2f( 0, 0 )*/, const ButtonCustomizationParams& customParams )
186185
{
187-
bool simulateClick = custmParams.enableTestEngine && TestEngine::createButton( label );
188-
assert( ( simulateClick <= active ) && "Trying to programmatically press a button, but it's inactive!" );
189-
if ( !active )
186+
bool simulateClick = customParams.enableTestEngine && TestEngine::createButton( label );
187+
assert( ( simulateClick <= customParams.enabled ) && "Trying to programmatically press a button, but it's inactive!" );
188+
if ( !customParams.enabled )
190189
simulateClick = false;
191190

192191
// copy from ImGui::ButtonEx and replaced visualize part
@@ -199,6 +198,8 @@ bool buttonEx( const char* label, bool active, const Vector2f& size_arg /*= Vect
199198
const ImGuiID id = window->GetID( label );
200199
const ImVec2 label_size = ImGui::CalcTextSize( label, NULL, true );
201200

201+
auto flags = customParams.flags;
202+
202203
ImVec2 pos = window->DC.CursorPos;
203204
if ( ( flags & ImGuiButtonFlags_AlignTextBaseLine ) && style.FramePadding.y < window->DC.CurrLineTextBaseOffset ) // Try to vertically align buttons that are smaller/have no padding so that text baseline matches (bit hacky, since it shouldn't be a flag)
204205
pos.y += window->DC.CurrLineTextBaseOffset - style.FramePadding.y;
@@ -220,38 +221,46 @@ bool buttonEx( const char* label, bool active, const Vector2f& size_arg /*= Vect
220221

221222
// replaced part
222223
// potential fail. need check that customTexture is good
223-
auto texture = ( custmParams.customTexture || custmParams.forceImGuiBackground ) ? custmParams.customTexture : getTexture( TextureType::GradientBtn ).get();
224+
auto texture = ( customParams.customTexture || customParams.forceImGuiBackground ) ? customParams.customTexture : getTexture( TextureType::GradientBtn ).get();
224225
if ( texture )
225226
{
226-
const float textureU = 0.125f + ( !active ? 0.75f : ( held && hovered ) ? 0.5f : hovered ? 0.25f : 0.f );
227+
const float textureU = 0.125f + ( !customParams.enabled ? 0.75f : ( held && hovered ) ? 0.5f : hovered ? 0.25f : 0.f );
227228
window->DrawList->AddImageRounded(
228229
texture->getImTextureId(),
229230
bb.Min, bb.Max,
230231
ImVec2( textureU, 0.25f ), ImVec2( textureU, 0.75f ),
231232
Color::white().getUInt32(), style.FrameRounding );
232-
if ( custmParams.border )
233+
if ( customParams.border )
233234
ImGui::RenderFrameBorder( bb.Min, bb.Max, style.FrameRounding );
234235
}
235236
else
236237
{
237-
const ImGuiCol colIdx = ( !active ? ImGuiCol_TextDisabled : ( held && hovered ) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button );
238+
const ImGuiCol colIdx = ( !customParams.enabled ? ImGuiCol_TextDisabled : ( held && hovered ) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button );
238239
const ImU32 col = ImGui::GetColorU32( colIdx );
239240
ImGui::RenderFrame( bb.Min, bb.Max, col, true, style.FrameRounding );
240241
}
241242

242243
if ( g.LogEnabled )
243244
ImGui::LogSetNextTextDecoration( "[", "]" );
244245
StyleParamHolder sh;
245-
if ( !custmParams.forceImguiTextColor )
246+
if ( !customParams.forceImguiTextColor )
246247
sh.addColor( ImGuiCol_Text, ColorTheme::getRibbonColor( ColorTheme::RibbonColorsType::GradBtnText ) );
247248
ImGui::RenderTextClipped( bb.Min, bb.Max, label, NULL, &label_size, style.ButtonTextAlign, &bb );
248249

249-
if ( custmParams.underlineFirstLetter )
250+
if ( customParams.underlineFirstLetter )
250251
ImGui::RenderTextClipped( bb.Min, bb.Max, "_", NULL, &label_size, style.ButtonTextAlign, &bb);
251252

252253
IMGUI_TEST_ENGINE_ITEM_INFO( id, label, g.LastItemData.StatusFlags );
253254

254-
return ( pressed || simulateClick ) && active;
255+
return ( pressed || simulateClick ) && customParams.enabled;
256+
}
257+
258+
bool buttonEx( const char* label, bool active, const Vector2f& size /*= Vector2f( 0, 0 )*/, ImGuiButtonFlags flags /*= ImGuiButtonFlags_None*/, const ButtonCustomizationParams& customParams /*= {} */ )
259+
{
260+
auto paramscpy = customParams;
261+
paramscpy.enabled = active;
262+
paramscpy.flags = flags;
263+
return buttonEx( label, size, paramscpy );
255264
}
256265

257266
bool button( const char* label, bool active, const Vector2f& size /*= Vector2f( 0, 0 )*/, ImGuiKey key /*= ImGuiKey_None */ )
@@ -263,13 +272,13 @@ bool button( const char* label, bool active, const Vector2f& size /*= Vector2f(
263272
sh.addVar( ImGuiStyleVar_FramePadding, ImVec2( style.FramePadding.x, cGradientButtonFramePadding * scaling ) );
264273

265274
bool sameKey = std::string_view( ImGui::GetKeyName( key ) ) == std::string_view( label, 1 );
266-
return buttonEx( label, active, size, 0, { .underlineFirstLetter = sameKey } ) || ( active && checkKey( key ) );
275+
return buttonEx( label, size, { .enabled = active, .underlineFirstLetter = sameKey } ) || ( active && checkKey( key ) );
267276
}
268277

269278
bool buttonCommonSize( const char* label, const Vector2f& size /*= Vector2f( 0, 0 )*/, ImGuiKey key /*= ImGuiKey_None */ )
270279
{
271280
bool sameKey = std::string_view( ImGui::GetKeyName( key ) ) == std::string_view( label, 1 );
272-
return buttonEx( label, true, size, 0, { .underlineFirstLetter = sameKey } ) || checkKey( key );
281+
return buttonEx( label, size, { .underlineFirstLetter = sameKey } ) || checkKey( key );
273282
}
274283

275284
bool buttonUnique( const char* label, int* value, int ownValue, const Vector2f& size /*= Vector2f( 0, 0 )*/, ImGuiKey key /*= ImGuiKey_None*/ )
@@ -293,7 +302,7 @@ bool buttonUnique( const char* label, int* value, int ownValue, const Vector2f&
293302
params.forceImguiTextColor = true;
294303
params.underlineFirstLetter = std::string_view( ImGui::GetKeyName( key ) ) == std::string_view( label, 1 );
295304

296-
auto res = buttonEx( label, true, ImVec2( size.x, size.y ), 0, params ) || checkKey( key );
305+
auto res = buttonEx( label, ImVec2( size.x, size.y ), params ) || checkKey( key );
297306
if ( res )
298307
value[0] = ownValue;
299308

@@ -420,12 +429,12 @@ bool buttonIconEx(
420429
if ( params.flatBackgroundColor )
421430
{
422431
res = ImGui::Button( buttonText.c_str(), buttonSize );
423-
if( params.enableTestEngine )
432+
if( params.baseParams.enableTestEngine )
424433
res = UI::TestEngine::createButton( buttonText ) || res;
425434
}
426435
else
427436
{
428-
res = UI::buttonEx( buttonText.c_str(), params.active, Vector2f( buttonSize.x, buttonSize.y ), params.flags, params );
437+
res = UI::buttonEx( buttonText.c_str(), Vector2f( buttonSize.x, buttonSize.y ), params.baseParams );
429438
}
430439
ImGui::SameLine();
431440

@@ -539,7 +548,7 @@ bool buttonIconEx(
539548
auto icon = RibbonIcons::findByName( name, maxSize, RibbonIcons::ColorType::White, RibbonIcons::IconType::IndependentIcons );
540549

541550
StyleParamHolder sh;
542-
if ( !params.forceImguiTextColor )
551+
if ( !params.baseParams.forceImguiTextColor )
543552
sh.addColor( ImGuiCol_Text, ColorTheme::getRibbonColor( ColorTheme::RibbonColorsType::GradBtnText ) );
544553

545554
ImVec4 multColor = ImGui::GetStyleColorVec4( ImGuiCol_Text );
@@ -570,7 +579,7 @@ bool buttonIconEx(
570579
color,
571580
detail.start,
572581
detail.end );
573-
if ( numStr == 0 && params.underlineFirstLetter )
582+
if ( numStr == 0 && params.baseParams.underlineFirstLetter )
574583
ImGui::GetWindowDrawList()->AddText( font, cFontSize, screenPos, color, "_" );
575584

576585
numStr++;

source/MRViewer/MRUIStyle.h

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,17 @@ MRVIEWER_API void init();
4040
/// parameters to customize buttonEx
4141
struct ButtonCustomizationParams
4242
{
43+
/// determines if this button is enabled or disabled
44+
bool enabled = true;
45+
46+
/// imgui flags for this button
47+
ImGuiButtonFlags flags = ImGuiButtonFlags_None;
48+
4349
/// gradient texture other than default
4450
/// {start, hover_start, acitve_start, disabled_start,
4551
/// end, hover_end, acitve_end, disabled_end }
4652
ImGuiImage* customTexture = nullptr;
53+
4754
/// force use imgui background if !customTexture
4855
bool forceImGuiBackground = false;
4956

@@ -59,11 +66,11 @@ struct ButtonCustomizationParams
5966
bool enableTestEngine = true;
6067
};
6168

62-
struct ButtonIconCustomizationParams : public ButtonCustomizationParams
69+
struct ButtonIconCustomizationParams
6370
{
64-
ImGuiButtonFlags flags = ImGuiButtonFlags_None;
65-
// flag for buttonEx, which can be disabled
66-
bool active = true;
71+
// basic customization parameters
72+
ButtonCustomizationParams baseParams;
73+
6774
// button without a gradient, always active, configurable by an external style
6875
bool flatBackgroundColor = false;
6976
// if false - text is to the right
@@ -100,8 +107,12 @@ struct PlotAxis
100107
MRVIEWER_API bool checkKey( ImGuiKey passedKey );
101108

102109
/// draw gradient button, which can be disabled (active = false)
103-
MRVIEWER_API bool buttonEx( const char* label, bool active, const Vector2f& size = Vector2f( 0, 0 ),
104-
ImGuiButtonFlags flags = ImGuiButtonFlags_None, const ButtonCustomizationParams& custmParams = {} );
110+
[[deprecated( "Use UI::buttonEx( label, size, params ) instead" )]]
111+
MRVIEWER_API bool buttonEx( const char* label,bool active, const Vector2f& size = Vector2f( 0, 0 ),
112+
ImGuiButtonFlags flags = ImGuiButtonFlags_None, const ButtonCustomizationParams& customParams = {} );
113+
114+
/// draw gradient button, which can be customized
115+
MRVIEWER_API bool buttonEx( const char* label, const Vector2f& size = Vector2f( 0, 0 ), const ButtonCustomizationParams& customParams = {} );
105116
/// draw gradient button, which can be disabled (active = false)
106117
/// returns true if button is clicked in this frame, or key is pressed (optional)
107118
MRVIEWER_API bool button( const char* label, bool active, const Vector2f& size = Vector2f( 0, 0 ), ImGuiKey key = ImGuiKey_None );
@@ -133,7 +144,7 @@ MRVIEWER_API bool buttonIconEx(
133144
inline bool buttonIcon( const std::string& name, const Vector2f& iconSize, const std::string& text, bool active, const ImVec2& buttonSize )
134145
{
135146
ButtonIconCustomizationParams params;
136-
params.active = active;
147+
params.baseParams.enabled = active;
137148
params.flatBackgroundColor = true;
138149
return buttonIconEx(name, iconSize, text, buttonSize, params );
139150
}
@@ -153,9 +164,9 @@ inline bool buttonIconFlatBG(
153164
{
154165
ButtonIconCustomizationParams params;
155166
params.flatBackgroundColor = true;
156-
params.forceImguiTextColor = true;
167+
params.baseParams.forceImguiTextColor = true;
157168
params.textUnderImage = textUnderIcon;
158-
params.underlineFirstLetter = std::string_view( ImGui::GetKeyName( key ) ) == std::string_view( text.c_str(), 1 );
169+
params.baseParams.underlineFirstLetter = std::string_view( ImGui::GetKeyName( key ) ) == std::string_view( text.c_str(), 1 );
159170
return buttonIconEx( name, iconSize, text, buttonSize, params ) || checkKey( key );
160171
}
161172
/// draw button with icon same logic as radioButton

source/MRViewer/MRUIStyle.ipp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,10 @@ bool drag( const char* label, T& v, SpeedType vSpeed, const U& vMin, const U& vM
388388
Vector2f buttonSize( ImGui::GetFrameHeight(), ImGui::GetFrameHeight() );
389389
ImGui::SameLine( 0, ImGui::GetStyle().ItemInnerSpacing.x );
390390
ImGui::SetCursorPosY( dragY ); // Usually redundant, but when the user does something weird, this is sometimes required.
391-
action -= UI::buttonEx( "\xe2\x88\x92", true, buttonSize, 0, { .enableTestEngine = false } );
391+
action -= UI::buttonEx( "\xe2\x88\x92", buttonSize, { .enableTestEngine = false } );
392392
ImGui::SameLine( 0, ImGui::GetStyle().ItemInnerSpacing.x );
393393
ImGui::SetCursorPosY( dragY );
394-
action += UI::buttonEx( "+", true, buttonSize, 0, { .enableTestEngine = false } );
394+
action += UI::buttonEx( "+", buttonSize, { .enableTestEngine = false } );
395395

396396
if ( action )
397397
{

0 commit comments

Comments
 (0)