Skip to content

Commit d75768a

Browse files
authored
Improve keybind UI on debug wish menus (#82437)
2 parents 67ae374 + 85306e8 commit d75768a

File tree

6 files changed

+89
-24
lines changed

6 files changed

+89
-24
lines changed

src/cata_imgui.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static ImVec4 compute_color( uint8_t index )
7272
}
7373
}
7474

75-
ImVec4 cataimgui::imvec4_from_color( nc_color &color )
75+
ImVec4 cataimgui::imvec4_from_color( const nc_color &color )
7676
{
7777
int pair_id = color.get_index();
7878
pairs &pair = colorpairs[pair_id];
@@ -239,7 +239,7 @@ RGBTuple color_loader<RGBTuple>::from_rgb( const int r, const int g, const int b
239239
#include <imgui/imgui_impl_sdl2.h>
240240
#include <imgui/imgui_impl_sdlrenderer2.h>
241241

242-
ImVec4 cataimgui::imvec4_from_color( nc_color &color )
242+
ImVec4 cataimgui::imvec4_from_color( const nc_color &color )
243243
{
244244
SDL_Color c = curses_color_to_SDL( color );
245245
return { static_cast<float>( c.r / 255. ),
@@ -1252,3 +1252,28 @@ void cataimgui::init_colors()
12521252
style_path.generic_u8string(), err.what() );
12531253
}
12541254
}
1255+
1256+
void cataimgui::TextKeybinding( const input_context &ctxt,
1257+
const char *action, const char *description, bool active,
1258+
int max_limit, const nc_color &default_color )
1259+
{
1260+
const nc_color color = active ? ACTIVE_HOTKEY_COLOR : default_color;
1261+
ImGui::TextColored( default_color, "[" );
1262+
ImGui::SameLine( 0, 0 );
1263+
ImGui::TextColored( color, "%s",
1264+
// strlen(action) <= 1 // for non-action explicit keys
1265+
( *action == 0 || *( action + 1 ) == 0 ) ?
1266+
action :
1267+
ctxt.get_desc( action, max_limit ).c_str() );
1268+
ImGui::SameLine( 0, 0 );
1269+
ImGui::TextColored( default_color, "] " );
1270+
ImGui::SameLine( 0, 0 );
1271+
ImGui::TextColored( color, "%s", description );
1272+
}
1273+
1274+
void cataimgui::TextListSeparator( const nc_color &color )
1275+
{
1276+
ImGui::SameLine( 0, 0 );
1277+
ImGui::TextColored( color, ", " );
1278+
ImGui::SameLine( 0, 0 );
1279+
}

src/cata_imgui.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <vector>
66
#include <unordered_map>
77

8+
#include "color.h"
89
#include "font_loader.h"
910

1011
class nc_color;
@@ -99,7 +100,7 @@ class client
99100
void point_to_imvec2( point *src, ImVec2 *dest );
100101
void imvec2_to_point( ImVec2 *src, point *dest );
101102

102-
ImVec4 imvec4_from_color( nc_color &color );
103+
ImVec4 imvec4_from_color( const nc_color &color );
103104

104105
void set_scroll( scroll &s );
105106

@@ -169,4 +170,25 @@ void EndRightAlign();
169170
// This loads the settings from `config/imgui_style.json` and - optionally - falls back to base colors
170171
// for elements not explicitly specified.
171172
void init_colors();
173+
174+
/**
175+
* Print out key(s) and description for a keybinding
176+
* e.g. "[/] Filter"
177+
* @param ctxt input context
178+
* @param action action name, e.g. "FILTER", "QUIT"
179+
* @param description action description, e.g. "Do thing", "Whatever else"
180+
* @param active whether the action should be highlighted
181+
* @param max_limit no more than this many keys will be printed
182+
* @param default_color the color to use for non-highlighted actions
183+
*/
184+
void TextKeybinding( const input_context &ctxt,
185+
const char *action, const char *description, bool active,
186+
int max_limit = 0, const nc_color &default_color = c_light_gray );
187+
188+
/**
189+
* Print out a list separator, ", " in English
190+
* The previous and next outputs will appear on the same line.
191+
*/
192+
void TextListSeparator( const nc_color &color = c_light_gray );
193+
172194
} // namespace cataimgui

src/color.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "uilist.h"
3131
#include "cata_imgui.h"
3232

33-
nc_color::operator ImVec4()
33+
nc_color::operator ImVec4() const
3434
{
3535
return cataimgui::imvec4_from_color( *this );
3636
}

src/color.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ class nc_color
368368
public:
369369
nc_color() : attribute_value( 0 ), index( 0 ) { }
370370

371-
operator ImVec4(); // NOLINT(google-explicit-constructor): the conversion is not expensive
371+
operator ImVec4() const; // NOLINT(google-explicit-constructor): the conversion is not expensive
372372

373373
// Most of the functions here are implemented in ncurses_def.cpp
374374
// (for ncurses builds) *and* in cursesport.cpp (for other builds).

src/uilist.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ class uilist // NOLINT(cata-xy)
433433
std::optional<cataimgui::bounds> desired_bounds;
434434
bool desc_enabled = false;
435435

436+
std::string filter;
436437
bool filtering = false;
437438
bool filtering_nocase = false;
438439

@@ -477,7 +478,6 @@ class uilist // NOLINT(cata-xy)
477478
weak_ptr_fast<uilist_impl> ui;
478479

479480
std::unique_ptr<string_input_popup_imgui> filter_popup;
480-
std::string filter;
481481

482482
int max_entry_len = 0;
483483
int max_column_len = 0;

src/wish.cpp

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,15 @@ class wish_mutate_callback: public uilist_callback
243243
ImGui::TextColored( c_green, "%s", msg.c_str() );
244244
msg.clear();
245245
input_context ctxt( menu->input_category, keyboard_mode::keycode );
246-
ImGui::Text( _( "[%s] find, [%s] quit, [t] toggle base trait" ),
247-
ctxt.get_desc( "FILTER" ).c_str(), ctxt.get_desc( "QUIT" ).c_str() );
248246

249-
if( only_active ) {
250-
ImGui::TextColored( c_green, "%s", _( "[a] show active traits (active)" ) );
251-
} else {
252-
ImGui::TextColored( c_white, "%s", _( "[a] show active traits" ) );
253-
}
247+
cataimgui::TextKeybinding( ctxt, "FILTER", _( "Find" ),
248+
menu->filtering && ( !menu->filter.empty() ) );
249+
cataimgui::TextListSeparator();
250+
cataimgui::TextKeybinding( ctxt, "t", _( "Toggle base trait" ), false );
251+
cataimgui::TextListSeparator();
252+
cataimgui::TextKeybinding( ctxt, "a", _( "Show active traits" ), only_active );
253+
cataimgui::TextListSeparator();
254+
cataimgui::TextKeybinding( ctxt, "QUIT", _( "Quit" ), false );
254255
}
255256

256257
~wish_mutate_callback() override = default;
@@ -725,9 +726,20 @@ class wish_monster_callback: public uilist_callback
725726
ImGui::TextColored( c_green, "%s", msg.c_str() );
726727
msg.clear();
727728
input_context ctxt( menu->input_category, keyboard_mode::keycode );
728-
ImGui::Text(
729-
_( "[%s] find, [f]riendly, [h]allucination, [i]ncrease group, [d]ecrease group, [%s] quit" ),
730-
ctxt.get_desc( "FILTER" ).c_str(), ctxt.get_desc( "QUIT" ).c_str() );
729+
730+
cataimgui::TextKeybinding( ctxt, "FILTER", _( "Find" ),
731+
menu->filtering && ( !menu->filter.empty() ) );
732+
cataimgui::TextListSeparator();
733+
cataimgui::TextKeybinding( ctxt, "f", _( "Friendly" ), friendly );
734+
cataimgui::TextListSeparator();
735+
cataimgui::TextKeybinding( ctxt, "h", _( "Hallucination" ), hallucination );
736+
cataimgui::TextListSeparator();
737+
cataimgui::TextKeybinding( ctxt, "i", _( "Increase Group" ), false );
738+
cataimgui::TextListSeparator();
739+
cataimgui::TextKeybinding( ctxt, "d", _( "Decrease Group" ), false );
740+
cataimgui::TextListSeparator();
741+
cataimgui::TextKeybinding( ctxt, "QUIT", _( "Quit" ), false );
742+
731743
}
732744
ImGui::EndChild();
733745
}
@@ -1062,13 +1074,19 @@ class wish_item_callback: public uilist_callback
10621074

10631075
ImGui::TextColored( c_green, "%s", msg.c_str() );
10641076
input_context ctxt( menu->input_category, keyboard_mode::keycode );
1065-
ImGui::Text( _( "[%s] find, [%s] container, [%s] flag, [%s] everything, [%s] snippet, [%s] quit" ),
1066-
ctxt.get_desc( "FILTER" ).c_str(),
1067-
ctxt.get_desc( "CONTAINER" ).c_str(),
1068-
ctxt.get_desc( "FLAG" ).c_str(),
1069-
ctxt.get_desc( "EVERYTHING" ).c_str(),
1070-
ctxt.get_desc( "SNIPPET" ).c_str(),
1071-
ctxt.get_desc( "QUIT" ).c_str() );
1077+
1078+
cataimgui::TextKeybinding( ctxt, "FILTER", _( "Find" ),
1079+
menu->filtering && ( !menu->filter.empty() ) );
1080+
cataimgui::TextListSeparator();
1081+
cataimgui::TextKeybinding( ctxt, "CONTAINER", _( "Container" ), incontainer );
1082+
cataimgui::TextListSeparator();
1083+
cataimgui::TextKeybinding( ctxt, "FLAG", _( "Flag" ), !flags.empty() );
1084+
cataimgui::TextListSeparator();
1085+
cataimgui::TextKeybinding( ctxt, "EVERYTHING", _( "Everything" ), spawn_everything );
1086+
cataimgui::TextListSeparator();
1087+
cataimgui::TextKeybinding( ctxt, "SNIPPET", _( "Snippet" ), chosen_snippet_id.first != -1 );
1088+
cataimgui::TextListSeparator();
1089+
cataimgui::TextKeybinding( ctxt, "QUIT", _( "Quit" ), false );
10721090
}
10731091
};
10741092

@@ -1169,7 +1187,7 @@ void debug_menu::wishitem( Character *you, const tripoint_bub_ms &pos )
11691187
popup
11701188
.title( _( "How many?" ) )
11711189
.width( 20 )
1172-
.description( granted.tname() )
1190+
.description( cb.spawn_everything ? _( "Everything" ) : granted.tname() )
11731191
.edit( amount );
11741192
canceled = popup.canceled();
11751193
}

0 commit comments

Comments
 (0)