From 7a0ba64d9097cacc917b2d164f7ecd3bdaa8d2f6 Mon Sep 17 00:00:00 2001 From: Daniel Brooks Date: Tue, 10 Sep 2024 20:34:10 -0700 Subject: [PATCH] uilist now draws selected row without color This eliminates problems with contrast between the foreground colors in the text and the background color used to indicate which row is selected. Fixes #75974 --- src/ui.cpp | 43 +++++++++++++++++++++++++++++++++++-------- src/ui.h | 3 +++ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/ui.cpp b/src/ui.cpp index 9c4768909f76e..52f2f9ce4b81e 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -125,9 +125,10 @@ void uilist_impl::draw_controls() parent.need_to_scroll = true; is_selected = parent.clicked = true; } + bool is_hovered = ImGui::IsItemHovered( ImGuiHoveredFlags_NoNavOverride ); bool mouse_moved = ImGui::GetCurrentContext()->HoveredId != ImGui::GetCurrentContext()->HoveredIdPreviousFrame; - if( ImGui::IsItemHovered( ImGuiHoveredFlags_NoNavOverride ) && mouse_moved ) { + if( is_hovered && mouse_moved ) { // this row is hovered and the hover state just changed, show context for it parent.hovered = parent.fentries[ i ]; } @@ -137,16 +138,20 @@ void uilist_impl::draw_controls() is_selected ? parent.hilight_color : parent.hotkey_color ); } + std::string &str1 = entry.txt; + std::string &str2 = entry.ctxt; + nc_color color = entry.enabled || entry.force_color ? entry.text_color : parent.disabled_color; + if( is_selected || is_hovered ) { + str1 = entry._txt_nocolor; + str2 = entry._ctxt_nocolor; + color = parent.hilight_color; + } + ImGui::TableSetColumnIndex( 1 ); - nc_color color = ( is_selected ? - parent.hilight_color : - ( entry.enabled || entry.force_color ? - entry.text_color : - parent.disabled_color ) ); - cataimgui::draw_colored_text( entry.txt, color ); + cataimgui::draw_colored_text( str1, color ); ImGui::TableSetColumnIndex( 2 ); - cataimgui::draw_colored_text( entry.ctxt, color ); + cataimgui::draw_colored_text( str2, color ); ImGui::PopID(); } @@ -212,24 +217,32 @@ uilist_entry::uilist_entry( const std::string &txt ) : retval( -1 ), enabled( true ), hotkey( std::nullopt ), txt( txt ), text_color( c_red_red ) { + _txt_nocolor = remove_color_tags( txt ); + _ctxt_nocolor = remove_color_tags( ctxt ); } uilist_entry::uilist_entry( const std::string &txt, const std::string &desc ) : retval( -1 ), enabled( true ), hotkey( std::nullopt ), txt( txt ), desc( desc ), text_color( c_red_red ) { + _txt_nocolor = remove_color_tags( txt ); + _ctxt_nocolor = remove_color_tags( ctxt ); } uilist_entry::uilist_entry( const std::string &txt, const int key ) : retval( -1 ), enabled( true ), hotkey( hotkey_from_char( key ) ), txt( txt ), text_color( c_red_red ) { + _txt_nocolor = remove_color_tags( txt ); + _ctxt_nocolor = remove_color_tags( ctxt ); } uilist_entry::uilist_entry( const std::string &txt, const std::optional &key ) : retval( -1 ), enabled( true ), hotkey( key ), txt( txt ), text_color( c_red_red ) { + _txt_nocolor = remove_color_tags( txt ); + _ctxt_nocolor = remove_color_tags( ctxt ); } uilist_entry::uilist_entry( const int retval, const bool enabled, const int key, @@ -237,6 +250,8 @@ uilist_entry::uilist_entry( const int retval, const bool enabled, const int key, : retval( retval ), enabled( enabled ), hotkey( hotkey_from_char( key ) ), txt( txt ), text_color( c_red_red ) { + _txt_nocolor = remove_color_tags( txt ); + _ctxt_nocolor = remove_color_tags( ctxt ); } uilist_entry::uilist_entry( const int retval, const bool enabled, @@ -245,6 +260,8 @@ uilist_entry::uilist_entry( const int retval, const bool enabled, : retval( retval ), enabled( enabled ), hotkey( key ), txt( txt ), text_color( c_red_red ) { + _txt_nocolor = remove_color_tags( txt ); + _ctxt_nocolor = remove_color_tags( ctxt ); } uilist_entry::uilist_entry( const int retval, const bool enabled, const int key, @@ -252,6 +269,8 @@ uilist_entry::uilist_entry( const int retval, const bool enabled, const int key, : retval( retval ), enabled( enabled ), hotkey( hotkey_from_char( key ) ), txt( txt ), desc( desc ), text_color( c_red_red ) { + _txt_nocolor = remove_color_tags( txt ); + _ctxt_nocolor = remove_color_tags( ctxt ); } uilist_entry::uilist_entry( const int retval, const bool enabled, @@ -259,6 +278,8 @@ uilist_entry::uilist_entry( const int retval, const bool enabled, : retval( retval ), enabled( enabled ), hotkey( key ), txt( txt ), desc( desc ), text_color( c_red_red ) { + _txt_nocolor = remove_color_tags( txt ); + _ctxt_nocolor = remove_color_tags( ctxt ); } uilist_entry::uilist_entry( const int retval, const bool enabled, const int key, @@ -267,6 +288,8 @@ uilist_entry::uilist_entry( const int retval, const bool enabled, const int key, : retval( retval ), enabled( enabled ), hotkey( hotkey_from_char( key ) ), txt( txt ), desc( desc ), ctxt( column ), text_color( c_red_red ) { + _txt_nocolor = remove_color_tags( txt ); + _ctxt_nocolor = remove_color_tags( ctxt ); } uilist_entry::uilist_entry( const int retval, const bool enabled, @@ -276,6 +299,8 @@ uilist_entry::uilist_entry( const int retval, const bool enabled, : retval( retval ), enabled( enabled ), hotkey( key ), txt( txt ), desc( desc ), ctxt( column ), text_color( c_red_red ) { + _txt_nocolor = remove_color_tags( txt ); + _ctxt_nocolor = remove_color_tags( ctxt ); } uilist_entry::uilist_entry( const int retval, const bool enabled, const int key, @@ -284,6 +309,8 @@ uilist_entry::uilist_entry( const int retval, const bool enabled, const int key, : retval( retval ), enabled( enabled ), hotkey( hotkey_from_char( key ) ), txt( txt ), hotkey_color( keycolor ), text_color( txtcolor ) { + _txt_nocolor = remove_color_tags( txt ); + _ctxt_nocolor = remove_color_tags( ctxt ); } uilist::size_scalar &uilist::size_scalar::operator=( auto_assign ) diff --git a/src/ui.h b/src/ui.h index ce63b0b826ad2..c4d197e4e0abb 100644 --- a/src/ui.h +++ b/src/ui.h @@ -170,6 +170,9 @@ struct uilist_entry { explicit uilist_entry( Enum e, Args && ... args ) : uilist_entry( static_cast( e ), std::forward( args )... ) {} + + std::string _txt_nocolor; // what it says on the tin + std::string _ctxt_nocolor; // second column text }; /**