Skip to content

Commit 1205b8f

Browse files
committed
Expand widget clauses: JSONify bp_status_legend_text
1 parent 0854dcb commit 1205b8f

File tree

7 files changed

+38
-188
lines changed

7 files changed

+38
-188
lines changed

data/json/ui/bodypart_status.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@
177177
"id": "bp_status_legend_text",
178178
"type": "widget",
179179
"label": "Status Legend",
180-
"style": "text",
181-
"var": "bp_status_legend_text",
180+
"style": "legend",
181+
"bodyparts": [ "head", "torso", "arm_l", "arm_r", "leg_l", "leg_r" ],
182182
"copy-from": "bodypart_status_indicator_template",
183183
"width": 0,
184184
"height": 3,

data/mods/TEST_DATA/widgets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,8 @@
442442
"id": "test_status_legend_text",
443443
"type": "widget",
444444
"label": "Status Legend",
445-
"style": "text",
446-
"var": "bp_status_legend_text",
445+
"style": "legend",
446+
"bodyparts": [ "head", "torso", "arm_l", "arm_r", "leg_l", "leg_r" ],
447447
"copy-from": "test_bp_status_indicator_template",
448448
"width": 0,
449449
"height": 3,

src/display.cpp

Lines changed: 0 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
#include "vpart_position.h"
1616
#include "weather.h"
1717

18-
static const efftype_id effect_bandaged( "bandaged" );
1918
static const efftype_id effect_bite( "bite" );
2019
static const efftype_id effect_bleed( "bleed" );
21-
static const efftype_id effect_disinfected( "disinfected" );
2220
static const efftype_id effect_got_checked( "got_checked" );
2321
static const efftype_id effect_hunger_blank( "hunger_blank" );
2422
static const efftype_id effect_hunger_engorged( "hunger_engorged" );
@@ -32,7 +30,6 @@ static const efftype_id effect_hunger_very_hungry( "hunger_very_hungry" );
3230
static const efftype_id effect_infected( "infected" );
3331

3432
static const flag_id json_flag_RAD_DETECT( "RAD_DETECT" );
35-
static const flag_id json_flag_SPLINT( "SPLINT" );
3633
static const flag_id json_flag_THERMOMETER( "THERMOMETER" );
3734

3835
static const itype_id fuel_type_muscle( "muscle" );
@@ -1016,135 +1013,6 @@ std::pair<std::string, nc_color> display::vehicle_fuel_percent_text_color( const
10161013
return std::make_pair( fuel_text, fuel_color );
10171014
}
10181015

1019-
// Return status/color pairs for all statuses affecting body part (bleeding, bitten, bandaged, etc.)
1020-
static std::map<bodypart_status, nc_color> bodypart_status_colors( const Character &u,
1021-
const bodypart_id &bp, const std::string &wgt_id )
1022-
{
1023-
// List of active statuses and associated colors
1024-
std::map<bodypart_status, nc_color> ret;
1025-
1026-
// Empty if no bodypart given
1027-
if( bp == bodypart_str_id::NULL_ID() ) {
1028-
return ret;
1029-
}
1030-
1031-
const int bleed_intensity = u.get_effect_int( effect_bleed, bp );
1032-
const bool bleeding = bleed_intensity > 0;
1033-
const bool bitten = u.has_effect( effect_bite, bp.id() );
1034-
const bool infected = u.has_effect( effect_infected, bp.id() );
1035-
const bool broken = u.is_limb_broken( bp ) && bp->is_limb;
1036-
const bool splinted = u.worn_with_flag( json_flag_SPLINT, bp );
1037-
const bool bandaged = u.has_effect( effect_bandaged, bp.id() );
1038-
const bool disinfected = u.has_effect( effect_disinfected, bp.id() );
1039-
1040-
auto get_clr = [&wgt_id]( const bodypart_status & stat, int val ) {
1041-
widget_id id( wgt_id );
1042-
if( id.is_valid() ) {
1043-
return widget_phrase::get_color_for_id( io::enum_to_string( stat ), id, val );
1044-
}
1045-
return c_white;
1046-
};
1047-
1048-
// Ailments
1049-
if( broken ) {
1050-
ret[bodypart_status::BROKEN] = get_clr( bodypart_status::BROKEN, INT_MIN );
1051-
}
1052-
if( bitten ) {
1053-
ret[bodypart_status::BITTEN] = get_clr( bodypart_status::BITTEN, INT_MIN );
1054-
}
1055-
if( bleeding ) {
1056-
ret[bodypart_status::BLEEDING] = get_clr( bodypart_status::BLEEDING, bleed_intensity );
1057-
}
1058-
if( infected ) {
1059-
ret[bodypart_status::INFECTED] = get_clr( bodypart_status::INFECTED, INT_MIN );
1060-
}
1061-
// Treatments
1062-
if( splinted ) {
1063-
ret[bodypart_status::SPLINTED] = get_clr( bodypart_status::SPLINTED, INT_MIN );
1064-
}
1065-
if( bandaged ) {
1066-
ret[bodypart_status::BANDAGED] = get_clr( bodypart_status::BANDAGED, INT_MIN );
1067-
}
1068-
if( disinfected ) {
1069-
ret[bodypart_status::DISINFECTED] = get_clr( bodypart_status::DISINFECTED, INT_MIN );
1070-
}
1071-
1072-
return ret;
1073-
}
1074-
1075-
static const std::string &sym_for_bp_status( const bodypart_status &stat )
1076-
{
1077-
static const std::string none = ".";
1078-
static const std::map<bodypart_status, std::string> symmap {
1079-
{ bodypart_status::BITTEN, "B" },
1080-
{ bodypart_status::INFECTED, "I" },
1081-
{ bodypart_status::BROKEN, "%" },
1082-
{ bodypart_status::BLEEDING, "b" },
1083-
{ bodypart_status::SPLINTED, "=" },
1084-
{ bodypart_status::BANDAGED, "+" },
1085-
{ bodypart_status::DISINFECTED, "$" },
1086-
{ bodypart_status::num_bodypart_status, none }
1087-
};
1088-
auto sym = symmap.find( stat );
1089-
return sym == symmap.end() ? none : sym->second;
1090-
}
1091-
1092-
std::string display::colorized_bodypart_status_legend_text( const Character &u,
1093-
const std::set<bodypart_id> &bplist, const std::string &wgt_id, int width, int max_height,
1094-
int &height )
1095-
{
1096-
std::vector<std::string> keys;
1097-
std::set<bodypart_status> status;
1098-
widget_id wid( wgt_id );
1099-
for( const bodypart_id &bp : bplist ) {
1100-
for( const auto &bpcol : bodypart_status_colors( u, bp, wgt_id ) ) {
1101-
if( status.find( bpcol.first ) == status.end() ) {
1102-
status.emplace( bpcol.first );
1103-
std::string key = io::enum_to_string( bpcol.first );
1104-
std::string sym;
1105-
if( wid.is_valid() ) {
1106-
translation t = widget_phrase::get_text_for_id( key, wid );
1107-
key = t.empty() ? key : t.translated();
1108-
sym = widget_phrase::get_sym_for_id( io::enum_to_string( bpcol.first ), wid );
1109-
}
1110-
sym = sym.empty() ? sym_for_bp_status( bpcol.first ) : sym;
1111-
sym = colorize( sym, bpcol.second );
1112-
keys.emplace_back( string_format( "%s %s", sym, key ) );
1113-
}
1114-
}
1115-
}
1116-
// Split legend keys into X lines, where X = height.
1117-
// Lines use the provided width.
1118-
// This effectively limits the text to a 'width'x'height' box.
1119-
std::string ret;
1120-
height = 0;
1121-
const int h_max = max_height == 0 ? INT_MAX : max_height;
1122-
const int nsize = keys.size();
1123-
for( int row = 0, nidx = 0; row < h_max && nidx < nsize; row++ ) {
1124-
int wavail = width;
1125-
int nwidth = utf8_width( keys[nidx], true );
1126-
bool startofline = true;
1127-
while( nidx < nsize && ( wavail > nwidth || startofline ) ) {
1128-
startofline = false;
1129-
wavail -= nwidth;
1130-
ret += keys[nidx];
1131-
nidx++;
1132-
if( nidx < nsize ) {
1133-
nwidth = utf8_width( keys[nidx], true );
1134-
if( wavail > nwidth ) {
1135-
ret += " ";
1136-
wavail -= 2;
1137-
}
1138-
}
1139-
}
1140-
if( row < h_max - 1 ) {
1141-
ret += "\n";
1142-
}
1143-
height++;
1144-
}
1145-
return ret;
1146-
}
1147-
11481016
std::string display::colorized_bodypart_outer_armor( const Character &u, const bodypart_id &bp )
11491017
{
11501018
for( std::list<item>::const_iterator it = u.worn.end(); it != u.worn.begin(); ) {

src/display.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,6 @@ std::pair<std::string, nc_color> move_mode_letter_color( const Character &u );
102102
std::pair<std::string, nc_color> move_mode_text_color( const Character &u );
103103
// Movement counter and mode letter, like "50(R)" or "100(W)"
104104
std::pair<std::string, nc_color> move_count_and_mode_text_color( const avatar &u );
105-
std::string colorized_bodypart_status_legend_text( const Character &u,
106-
const std::set<bodypart_id> &bplist, const std::string &wgt_id, int width, int max_height,
107-
int &height );
108105
// Item type name (including damage bars) of outermost armor on given body part
109106
std::string colorized_bodypart_outer_armor( const Character &u, const bodypart_id &bp );
110107

src/widget.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ std::string enum_to_string<widget_var>( widget_var data )
124124
return "body_temp_text";
125125
case widget_var::bp_armor_outer_text:
126126
return "bp_armor_outer_text";
127-
case widget_var::bp_status_legend_text:
128-
return "bp_status_legend_text";
129127
case widget_var::date_text:
130128
return "date_text";
131129
case widget_var::env_temp_text:
@@ -837,7 +835,6 @@ bool widget::uses_text_function()
837835
case widget_var::activity_text:
838836
case widget_var::body_temp_text:
839837
case widget_var::bp_armor_outer_text:
840-
case widget_var::bp_status_legend_text:
841838
case widget_var::compass_text:
842839
case widget_var::compass_legend_text:
843840
case widget_var::date_text:
@@ -888,17 +885,6 @@ static void set_height_for_widget( const widget_id &id, int height )
888885
}
889886
}
890887

891-
static std::set<bodypart_id> get_bodyparts_from_status_widgets()
892-
{
893-
std::set<bodypart_id> ret;
894-
for( const widget &w : widget::get_all() ) {
895-
if( !w._bps.empty() ) {
896-
ret.emplace( w.only_bp() );
897-
}
898-
}
899-
return ret;
900-
}
901-
902888
// NOTE: Use max_width to split multi-line widgets across lines
903889
std::string widget::color_text_function_string( const avatar &ava, unsigned int max_width )
904890
{
@@ -925,13 +911,6 @@ std::string widget::color_text_function_string( const avatar &ava, unsigned int
925911
desc.first = display::colorized_bodypart_outer_armor( ava, only_bp() );
926912
apply_color = false; // Item name already colorized by tname
927913
break;
928-
case widget_var::bp_status_legend_text:
929-
desc.first = display::colorized_bodypart_status_legend_text( ava,
930-
get_bodyparts_from_status_widgets(), id.str(),
931-
_width == 0 ? max_width : _width, _height_max, _height );
932-
update_height = true; // Dynamically adjusted height
933-
apply_color = false; // Already colorized
934-
break;
935914
case widget_var::date_text:
936915
desc.first = display::date_string();
937916
break;

src/widget.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ enum class widget_var : int {
4646
activity_text, // Activity level text, color string
4747
body_temp_text, // Felt body temperature, color string
4848
bp_armor_outer_text, // Outermost armor on body part, with color/damage bars
49-
bp_status_legend_text, // Legend describing the status indicators from bp_status_sym_text
5049
compass_text, // Compass / visible threats by cardinal direction
5150
compass_legend_text, // Names of visible creatures that appear on the compass
5251
date_text, // Current date, in terms of day within season

tests/widget_test.cpp

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,13 @@ TEST_CASE( "Widget alignment", "[widget]" )
17441744
SECTION( "Multiline text" ) {
17451745
widget bp_legend = widget_test_status_legend_text.obj();
17461746

1747+
const std::string line1 =
1748+
"<color_c_yellow>B</color> bitten <color_c_pink>I</color> infected <color_c_magenta>%</color> broken";
1749+
const std::string line2 =
1750+
"<color_c_light_gray>=</color> splinted <color_c_white>+</color> bandaged ";
1751+
const std::string line3 =
1752+
"<color_c_light_green>$</color> disinfected <color_c_light_red>b</color> bleeding";
1753+
17471754
ava.add_effect( effect_infected, 1_minutes, torso );
17481755
ava.add_effect( effect_bleed, 1_minutes, torso );
17491756
ava.get_effect( effect_bleed, torso ).set_intensity( 5 );
@@ -1755,72 +1762,72 @@ TEST_CASE( "Widget alignment", "[widget]" )
17551762
bp_legend._text_align = widget_alignment::RIGHT;
17561763

17571764
CHECK( bp_legend.layout( ava, sidebar_width ) ==
1758-
" <color_c_yellow>B</color> bitten <color_c_pink>I</color> infected <color_c_light_red>b</color> bleeding\n"
1759-
" <color_c_magenta>%</color> broken <color_c_light_gray>=</color> splinted <color_c_white>+</color> bandaged\n"
1760-
" <color_c_light_green>$</color> disinfected" );
1765+
" " + line1 + "\n" +
1766+
" " + line2 + "\n" +
1767+
" " + line3 );
17611768

17621769
bp_legend._label_align = widget_alignment::RIGHT;
17631770
bp_legend._text_align = widget_alignment::RIGHT;
17641771

17651772
CHECK( bp_legend.layout( ava, sidebar_width ) ==
1766-
" <color_c_yellow>B</color> bitten <color_c_pink>I</color> infected <color_c_light_red>b</color> bleeding\n"
1767-
" <color_c_magenta>%</color> broken <color_c_light_gray>=</color> splinted <color_c_white>+</color> bandaged\n"
1768-
" <color_c_light_green>$</color> disinfected" );
1773+
" " + line1 + "\n" +
1774+
" " + line2 + "\n" +
1775+
" " + line3 );
17691776

17701777
bp_legend._label_align = widget_alignment::CENTER;
17711778
bp_legend._text_align = widget_alignment::RIGHT;
17721779

17731780
CHECK( bp_legend.layout( ava, sidebar_width ) ==
1774-
" <color_c_yellow>B</color> bitten <color_c_pink>I</color> infected <color_c_light_red>b</color> bleeding\n"
1775-
" <color_c_magenta>%</color> broken <color_c_light_gray>=</color> splinted <color_c_white>+</color> bandaged\n"
1776-
" <color_c_light_green>$</color> disinfected" );
1781+
" " + line1 + "\n" +
1782+
" " + line2 + "\n" +
1783+
" " + line3 );
17771784

17781785
bp_legend._label_align = widget_alignment::LEFT;
17791786
bp_legend._text_align = widget_alignment::LEFT;
17801787

17811788
CHECK( bp_legend.layout( ava, sidebar_width ) ==
1782-
"<color_c_yellow>B</color> bitten <color_c_pink>I</color> infected <color_c_light_red>b</color> bleeding\n"
1783-
"<color_c_magenta>%</color> broken <color_c_light_gray>=</color> splinted <color_c_white>+</color> bandaged\n"
1784-
"<color_c_light_green>$</color> disinfected " );
1789+
line1 + "\n" +
1790+
line2 + "\n" +
1791+
line3 + " " );
17851792

17861793
bp_legend._label_align = widget_alignment::RIGHT;
17871794
bp_legend._text_align = widget_alignment::LEFT;
17881795

17891796
CHECK( bp_legend.layout( ava, sidebar_width ) ==
1790-
"<color_c_yellow>B</color> bitten <color_c_pink>I</color> infected <color_c_light_red>b</color> bleeding\n"
1791-
"<color_c_magenta>%</color> broken <color_c_light_gray>=</color> splinted <color_c_white>+</color> bandaged\n"
1792-
"<color_c_light_green>$</color> disinfected " );
1797+
line1 + "\n" +
1798+
line2 + "\n" +
1799+
line3 + " " );
17931800

17941801
bp_legend._label_align = widget_alignment::CENTER;
17951802
bp_legend._text_align = widget_alignment::LEFT;
17961803

17971804
CHECK( bp_legend.layout( ava, sidebar_width ) ==
1798-
"<color_c_yellow>B</color> bitten <color_c_pink>I</color> infected <color_c_light_red>b</color> bleeding\n"
1799-
"<color_c_magenta>%</color> broken <color_c_light_gray>=</color> splinted <color_c_white>+</color> bandaged\n"
1800-
"<color_c_light_green>$</color> disinfected " );
1805+
line1 + "\n" +
1806+
line2 + "\n" +
1807+
line3 + " " );
18011808

18021809
bp_legend._label_align = widget_alignment::LEFT;
18031810
bp_legend._text_align = widget_alignment::CENTER;
18041811

18051812
CHECK( bp_legend.layout( ava, sidebar_width ) ==
1806-
" <color_c_yellow>B</color> bitten <color_c_pink>I</color> infected <color_c_light_red>b</color> bleeding\n"
1807-
" <color_c_magenta>%</color> broken <color_c_light_gray>=</color> splinted <color_c_white>+</color> bandaged\n"
1808-
" <color_c_light_green>$</color> disinfected " );
1813+
" " + line1 + "\n" +
1814+
" " + line2 + "\n" +
1815+
" " + line3 + " " );
18091816

18101817
bp_legend._label_align = widget_alignment::RIGHT;
18111818
bp_legend._text_align = widget_alignment::CENTER;
18121819

18131820
CHECK( bp_legend.layout( ava, sidebar_width ) ==
1814-
" <color_c_yellow>B</color> bitten <color_c_pink>I</color> infected <color_c_light_red>b</color> bleeding\n"
1815-
" <color_c_magenta>%</color> broken <color_c_light_gray>=</color> splinted <color_c_white>+</color> bandaged\n"
1816-
" <color_c_light_green>$</color> disinfected " );
1821+
" " + line1 + "\n" +
1822+
" " + line2 + "\n" +
1823+
" " + line3 + " " );
18171824

18181825
bp_legend._label_align = widget_alignment::CENTER;
18191826
bp_legend._text_align = widget_alignment::CENTER;
18201827

18211828
CHECK( bp_legend.layout( ava, sidebar_width ) ==
1822-
" <color_c_yellow>B</color> bitten <color_c_pink>I</color> infected <color_c_light_red>b</color> bleeding\n"
1823-
" <color_c_magenta>%</color> broken <color_c_light_gray>=</color> splinted <color_c_white>+</color> bandaged\n"
1824-
" <color_c_light_green>$</color> disinfected " );
1829+
" " + line1 + "\n" +
1830+
" " + line2 + "\n" +
1831+
" " + line3 + " " );
18251832
}
18261833
}

0 commit comments

Comments
 (0)