Skip to content

Commit 2232afe

Browse files
authored
Merge pull request #82505 from RenechCDDA/funny_armor_table
Make armor value displays into a table
2 parents 4acd344 + 9dd8f87 commit 2232afe

File tree

4 files changed

+86
-13
lines changed

4 files changed

+86
-13
lines changed

src/item.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3770,7 +3770,7 @@ bool item::armor_full_protection_info( std::vector<iteminfo> &info,
37703770
for( const translation &entry : to_print ) {
37713771
coverage += string_format( _( " The <info>%s</info>." ), entry );
37723772
}
3773-
info.emplace_back( "ARMOR", coverage );
3773+
info.emplace_back( "DESCRIPTION", coverage );
37743774

37753775
// the following function need one representative sub limb from which to query data
37763776
armor_material_info( info, parts, 0, false, *p.sub_coverage.begin() );
@@ -3855,7 +3855,8 @@ void item::armor_protection_info( std::vector<iteminfo> &info, const iteminfo_qu
38553855
layering += string_format( " <stat>%s</stat>.", item::layer_to_string( ll ) );
38563856
}
38573857
//~ Limb-specific coverage (%s = name of limb)
3858-
info.emplace_back( "DESCRIPTION", string_format( _( "<bold>Coverage</bold>:%s" ), layering ) );
3858+
info.emplace_back( "SPECIAL_ARMOR_GRAPH", string_format( _( "<bold>Coverage</bold>:%s" ),
3859+
layering ) );
38593860
//~ Regular/Default coverage
38603861
info.emplace_back( bp_cat, string_format( "%s%s%s", space, _( "Default:" ), space ), "",
38613862
iteminfo::no_flags, get_coverage( sbp ) );
@@ -3896,15 +3897,16 @@ void item::armor_protection_info( std::vector<iteminfo> &info, const iteminfo_qu
38963897
bool display_median = percent_best < 50 && percent_worst < 50;
38973898

38983899
if( display_median ) {
3899-
info.emplace_back( "DESCRIPTION",
3900-
string_format( "<bold>%s</bold>: <bad>%d%%</bad>, <color_c_yellow>Median</color>, <good>%d%%</good>",
3900+
info.emplace_back( "SPECIAL_ARMOR_GRAPH",
3901+
string_format( "<bold>%s</bold>: <bad>%d%%</bad> chance, <color_c_yellow>Median</color> chance, <good>%d%%</good> chance",
39013902
_( "Protection" ), percent_worst, percent_best ) );
39023903
} else if( percent_worst > 0 ) {
3903-
info.emplace_back( "DESCRIPTION",
3904-
string_format( "<bold>%s</bold>: <bad>%d%%</bad>, <good>%d%%</good>", _( "Protection" ),
3904+
info.emplace_back( "SPECIAL_ARMOR_GRAPH",
3905+
string_format( "<bold>%s</bold>: <bad>%d%%</bad> chance, <good>%d%%</good> chance",
3906+
_( "Protection" ),
39053907
percent_worst, percent_best ) );
39063908
} else {
3907-
info.emplace_back( "DESCRIPTION", string_format( "<bold>%s</bold>:", _( "Protection" ) ) );
3909+
info.emplace_back( "SPECIAL_ARMOR_GRAPH", string_format( "<bold>%s</bold>:", _( "Protection" ) ) );
39083910
}
39093911

39103912
for( const damage_info_order &dio : damage_info_order::get_all(
@@ -3976,7 +3978,7 @@ void item::armor_material_info( std::vector<iteminfo> &info, const iteminfo_quer
39763978
if( parts->test( iteminfo_parts::ARMOR_MATERIALS ) ) {
39773979
const std::string space = " ";
39783980
// NOLINTNEXTLINE(cata-translate-string-literal)
3979-
std::string bp_cat = string_format( "ARMOR" );
3981+
std::string bp_cat = string_format( "DESCRIPTION" );
39803982
std::string materials_list;
39813983
std::string units_info = pgettext( "length unit: milimeter", "mm" );
39823984
for( const part_material *mat : armor_made_of( sbp ) ) {

src/output.cpp

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ std::string format_item_info( const std::vector<iteminfo> &vItemDisplay,
11601160
bool bIsNewLine = true;
11611161

11621162
for( const iteminfo &i : vItemDisplay ) {
1163-
if( i.sType == "DESCRIPTION" ) {
1163+
if( i.sType == "DESCRIPTION" || i.sType == "SPECIAL_ARMOR_GRAPH" ) {
11641164
// Always start a new line for sType == "DESCRIPTION"
11651165
if( !bIsNewLine ) {
11661166
buffer += "\n";
@@ -1268,6 +1268,75 @@ static nc_color get_comparison_color( const iteminfo &i,
12681268
return thisColor;
12691269
}
12701270

1271+
static std::vector<std::string> delimit_armor_text( const iteminfo &i )
1272+
{
1273+
const std::string &raw_text = i.sName;
1274+
// The various lines of text that can be passed in from item::armor_protection_info():
1275+
//
1276+
// Protection:
1277+
// Bash: 2.00, 8.00, 20.00
1278+
// Foo: 6.00, 12.00
1279+
//
1280+
// Yes that first one has no numbers. It puts the value into sValue.
1281+
// So we need multiple passes.
1282+
std::vector<std::string> first_pass = string_split( raw_text, ':' );
1283+
if( first_pass.size() == 2 && i.sValue != "-999" ) {
1284+
// Then the value is in sValue and we need to extract it.
1285+
// So just overwrite the second (empty) string that we just split off.
1286+
first_pass[1] = i.sValue;
1287+
}
1288+
const std::string mega_string = string_join( first_pass, ", " );
1289+
const std::vector<std::string> delimited_strings = string_split( mega_string, ',' );
1290+
return delimited_strings;
1291+
}
1292+
1293+
static void draw_armor_table( const std::vector<iteminfo> &vItemDisplay, const iteminfo &i )
1294+
{
1295+
if( ImGui::BeginTable( "##ITEMINFO_ARMOR_TABLE", delimit_armor_text( i ).size(),
1296+
ImGuiTableFlags_BordersH | ImGuiTableFlags_BordersV ) ) {
1297+
auto info_iter = vItemDisplay.begin() + std::distance( vItemDisplay.data(), &i );
1298+
if( info_iter == vItemDisplay.end() ) {
1299+
// PANIC!
1300+
ImGui::EndTable();
1301+
return;
1302+
}
1303+
1304+
const std::vector<std::string> chopped_up = delimit_armor_text( *info_iter );
1305+
for( const std::string &cell_text : chopped_up ) {
1306+
// this prefix prevents imgui from drawing the text. We still have color tags, which imgui won't parse, so we don't want those exposed to the user.
1307+
// But we still want proper column IDs. So we put them in, but we *hide them* with this.
1308+
// This results in a column with an ID of e.g.
1309+
// ##<color_white>Protection:</color>
1310+
//
1311+
// Not great for debugging, but better than having a column with default (randomly generated number) ID!
1312+
const std::string invisible_ID_label = "##" + cell_text;
1313+
ImGui::TableSetupColumn( invisible_ID_label.c_str(), ImGuiTableColumnFlags_WidthStretch );
1314+
}
1315+
ImGui::TableHeadersRow();
1316+
// After putting in the invisible labels in the last for-loop, this writes the actual text. Just the same text without the ## marker, and
1317+
// with our native functions doing the drawing. (So we parse color tags)
1318+
for( size_t i = 0; i < chopped_up.size(); i++ ) {
1319+
ImGui::TableSetColumnIndex( i );
1320+
cataimgui::draw_colored_text( chopped_up[i], c_unset );
1321+
}
1322+
1323+
info_iter++;
1324+
1325+
while( info_iter != vItemDisplay.end() && info_iter->sType == "ARMOR" ) {
1326+
ImGui::TableNextRow();
1327+
const std::vector<std::string> delimited_strings = delimit_armor_text( *info_iter );
1328+
for( const std::string &text : delimited_strings ) {
1329+
ImGui::TableNextColumn();
1330+
cataimgui::draw_colored_text( text, c_unset );
1331+
}
1332+
info_iter++;
1333+
}
1334+
1335+
1336+
ImGui::EndTable();
1337+
}
1338+
}
1339+
12711340
void display_item_info( const std::vector<iteminfo> &vItemDisplay,
12721341
const std::vector<iteminfo> &vItemCompare )
12731342
{
@@ -1276,7 +1345,9 @@ void display_item_info( const std::vector<iteminfo> &vItemDisplay,
12761345
if( i.bIsArt ) {
12771346
cataimgui::PushMonoFont();
12781347
}
1279-
if( i.sType == "DESCRIPTION" ) {
1348+
if( i.sType == "SPECIAL_ARMOR_GRAPH" ) {
1349+
draw_armor_table( vItemDisplay, i );
1350+
} else if( i.sType == "DESCRIPTION" ) {
12801351
if( i.bDrawName ) {
12811352
if( i.sName == "--" ) {
12821353
if( !bAlreadyHasNewLine ) {
@@ -1298,7 +1369,7 @@ void display_item_info( const std::vector<iteminfo> &vItemDisplay,
12981369
bAlreadyHasNewLine = false;
12991370
}
13001371
}
1301-
} else {
1372+
} else if( i.sType != "ARMOR" ) { // ARMOR is handled by draw_armor_table()
13021373
if( i.bDrawName ) {
13031374
cataimgui::TextColoredParagraph( c_light_gray, i.sName );
13041375
bAlreadyHasNewLine = false;

src/ui_iteminfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class iteminfo_window : public cataimgui::window
1212
{
1313
public:
1414
iteminfo_window( item_info_data &info, point pos, int width, int height,
15-
ImGuiWindowFlags flags = ImGuiWindowFlags_None );
15+
ImGuiWindowFlags flags = ImGuiWindowFlags_NoNavInputs );
1616
void execute();
1717

1818
protected:

tests/iteminfo_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,7 @@ TEST_CASE( "armor_protection", "[iteminfo][armor][protection]" )
15421542
"<color_c_white>Coverage</color>: <color_c_light_blue>Close to skin</color>.\n"
15431543
" Default: <color_c_yellow>100</color>\n";
15441544
const std::string protection_str =
1545-
"<color_c_white>Protection</color>: <color_c_red>4%</color>, <color_c_yellow>Median</color>, <color_c_green>4%</color>\n";
1545+
"<color_c_white>Protection</color>: <color_c_red>4%</color> chance, <color_c_yellow>Median</color> chance, <color_c_green>4%</color> chance\n";
15461546
const std::string bash_str =
15471547
" Bash: <color_c_red>1.00</color>, <color_c_yellow>12.00</color>, <color_c_green>23.00</color>\n";
15481548
const std::string cut_str =

0 commit comments

Comments
 (0)