Skip to content

Commit cd588e0

Browse files
committed
Expand widget clauses: JSONify bp_status_text
1 parent d39e15d commit cd588e0

File tree

6 files changed

+167
-52
lines changed

6 files changed

+167
-52
lines changed

data/json/ui/bodypart_status.json

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,81 @@
44
"type": "widget",
55
"style": "phrase",
66
"phrases": [
7-
{ "id": "bitten", "text": "bitten", "sym": "B", "color": "yellow" },
8-
{ "id": "infected", "text": "infected", "sym": "I", "color": "pink" },
9-
{ "id": "broken", "text": "broken", "sym": "%", "color": "magenta" },
10-
{ "id": "splinted", "text": "splinted", "sym": "=", "color": "light_gray" },
11-
{ "id": "bandaged", "text": "bandaged", "sym": "+", "color": "white" },
12-
{ "id": "disinfected", "text": "disinfected", "sym": "$", "color": "light_green" },
13-
{ "id": "bleeding", "text": "bleeding", "value": 0, "sym": "b", "color": "light_red" },
14-
{ "id": "bleeding", "text": "bleeding", "value": 11, "sym": "b", "color": "red" },
15-
{ "id": "bleeding", "text": "bleeding", "value": 21, "sym": "b", "color": "red_red" }
7+
{ "id": "bitten", "text": "bitten", "sym": "B", "color": "yellow", "condition": { "u_has_effect": "bite" } },
8+
{
9+
"id": "infected",
10+
"text": "infected",
11+
"sym": "I",
12+
"color": "pink",
13+
"condition": { "u_has_effect": "infected" }
14+
},
15+
{
16+
"id": "broken",
17+
"text": "broken",
18+
"sym": "%",
19+
"color": "magenta",
20+
"condition": { "compare_int": [ { "u_val": "hp" }, "==", { "const": 0 } ] }
21+
},
22+
{
23+
"id": "splinted",
24+
"text": "splinted",
25+
"sym": "=",
26+
"color": "light_gray",
27+
"condition": { "u_has_worn_with_flag": "SPLINT" }
28+
},
29+
{
30+
"id": "bandaged",
31+
"text": "bandaged",
32+
"sym": "+",
33+
"color": "white",
34+
"condition": { "u_has_effect": "bandaged" }
35+
},
36+
{
37+
"id": "disinfected",
38+
"text": "disinfected",
39+
"sym": "$",
40+
"color": "light_green",
41+
"condition": { "u_has_effect": "disinfected" }
42+
},
43+
{
44+
"id": "bleeding",
45+
"text": "bleeding",
46+
"sym": "b",
47+
"color": "light_red",
48+
"condition": {
49+
"and": [
50+
{ "u_has_effect": "bleed", "intensity": 1 },
51+
{ "compare_int": [ { "u_val": "effect_intensity", "effect": "bleed" }, "<", { "const": 11 } ] }
52+
]
53+
}
54+
},
55+
{
56+
"id": "bleeding",
57+
"text": "bleeding",
58+
"sym": "b",
59+
"color": "red",
60+
"condition": {
61+
"and": [
62+
{ "u_has_effect": "bleed", "intensity": 11 },
63+
{ "compare_int": [ { "u_val": "effect_intensity", "effect": "bleed" }, "<", { "const": 21 } ] }
64+
]
65+
}
66+
},
67+
{
68+
"id": "bleeding",
69+
"text": "bleeding",
70+
"sym": "b",
71+
"color": "red_red",
72+
"condition": { "u_has_effect": "bleed", "intensity": 21 }
73+
}
1674
]
1775
},
1876
{
1977
"id": "bodypart_status_text",
2078
"//": "Base widget for showing body part status; needs bodypart field defined in derived widget.",
2179
"type": "widget",
2280
"style": "text",
23-
"var": "bp_status_text",
81+
"default_phrase": { "text": "--" },
2482
"copy-from": "bodypart_status_indicator_template"
2583
},
2684
{

data/mods/TEST_DATA/widgets.json

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,73 @@
44
"type": "widget",
55
"style": "phrase",
66
"phrases": [
7-
{ "id": "bitten", "text": "bitten", "sym": "B", "color": "yellow" },
8-
{ "id": "infected", "text": "infected", "sym": "I", "color": "pink" },
9-
{ "id": "broken", "text": "broken", "sym": "%", "color": "magenta" },
10-
{ "id": "splinted", "text": "splinted", "sym": "=", "color": "light_gray" },
11-
{ "id": "bandaged", "text": "bandaged", "sym": "+", "color": "white" },
12-
{ "id": "disinfected", "text": "disinfected", "sym": "$", "color": "light_green" },
13-
{ "id": "bleeding", "text": "bleeding", "value": 0, "sym": "b", "color": "light_red" },
14-
{ "id": "bleeding", "text": "bleeding", "value": 11, "sym": "b", "color": "red" },
15-
{ "id": "bleeding", "text": "bleeding", "value": 21, "sym": "b", "color": "red_red" }
7+
{ "id": "bitten", "text": "bitten", "sym": "B", "color": "yellow", "condition": { "u_has_effect": "bite" } },
8+
{
9+
"id": "infected",
10+
"text": "infected",
11+
"sym": "I",
12+
"color": "pink",
13+
"condition": { "u_has_effect": "infected" }
14+
},
15+
{
16+
"id": "broken",
17+
"text": "broken",
18+
"sym": "%",
19+
"color": "magenta",
20+
"condition": { "compare_int": [ { "u_val": "hp" }, "==", { "const": 0 } ] }
21+
},
22+
{
23+
"id": "splinted",
24+
"text": "splinted",
25+
"sym": "=",
26+
"color": "light_gray",
27+
"condition": { "u_has_worn_with_flag": "SPLINT" }
28+
},
29+
{
30+
"id": "bandaged",
31+
"text": "bandaged",
32+
"sym": "+",
33+
"color": "white",
34+
"condition": { "u_has_effect": "bandaged" }
35+
},
36+
{
37+
"id": "disinfected",
38+
"text": "disinfected",
39+
"sym": "$",
40+
"color": "light_green",
41+
"condition": { "u_has_effect": "disinfected" }
42+
},
43+
{
44+
"id": "bleeding",
45+
"text": "bleeding",
46+
"sym": "b",
47+
"color": "light_red",
48+
"condition": {
49+
"and": [
50+
{ "u_has_effect": "bleed", "intensity": 1 },
51+
{ "compare_int": [ { "u_val": "effect_intensity", "effect": "bleed" }, "<", { "const": 11 } ] }
52+
]
53+
}
54+
},
55+
{
56+
"id": "bleeding",
57+
"text": "bleeding",
58+
"sym": "b",
59+
"color": "red",
60+
"condition": {
61+
"and": [
62+
{ "u_has_effect": "bleed", "intensity": 11 },
63+
{ "compare_int": [ { "u_val": "effect_intensity", "effect": "bleed" }, "<", { "const": 21 } ] }
64+
]
65+
}
66+
},
67+
{
68+
"id": "bleeding",
69+
"text": "bleeding",
70+
"sym": "b",
71+
"color": "red_red",
72+
"condition": { "u_has_effect": "bleed", "intensity": 21 }
73+
}
1674
]
1775
},
1876
{
@@ -350,18 +408,18 @@
350408
"id": "test_status_torso_text",
351409
"type": "widget",
352410
"label": "TORSO STATUS",
353-
"var": "bp_status_text",
354411
"bodypart": "torso",
355412
"style": "text",
413+
"default_phrase": { "text": "--" },
356414
"copy-from": "test_bp_status_indicator_template"
357415
},
358416
{
359417
"id": "test_status_left_arm_text",
360418
"type": "widget",
361419
"label": "LEFT ARM STATUS",
362-
"var": "bp_status_text",
363420
"bodypart": "arm_l",
364421
"style": "text",
422+
"default_phrase": { "text": "--" },
365423
"copy-from": "test_bp_status_indicator_template"
366424
},
367425
{

src/display.cpp

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,26 +1072,6 @@ static std::map<bodypart_status, nc_color> bodypart_status_colors( const Charact
10721072
return ret;
10731073
}
10741074

1075-
std::string display::colorized_bodypart_status_text( const Character &u, const bodypart_id &bp,
1076-
const std::string &wgt_id )
1077-
{
1078-
// Colorized strings for each status
1079-
std::vector<std::string> color_strings;
1080-
widget_id wid( wgt_id );
1081-
// Get all status strings and colorize them
1082-
for( const auto &sc : bodypart_status_colors( u, bp, wgt_id ) ) {
1083-
std::string txt = io::enum_to_string( sc.first );
1084-
if( wid.is_valid() ) {
1085-
// Check if there's a phrase defining this status' text
1086-
translation t = widget_phrase::get_text_for_id( txt, wid );
1087-
txt = t.empty() ? txt : t.translated();
1088-
}
1089-
color_strings.emplace_back( colorize( txt, sc.second ) );
1090-
}
1091-
// Join with commas, or return "--" if no statuses
1092-
return color_strings.empty() ? "--" : join( color_strings, ", " );
1093-
}
1094-
10951075
static const std::string &sym_for_bp_status( const bodypart_status &stat )
10961076
{
10971077
static const std::string none = ".";
@@ -1150,7 +1130,36 @@ std::string display::colorized_bodypart_status_legend_text( const Character &u,
11501130
}
11511131
}
11521132
}
1153-
return format_widget_multiline( keys, max_height, width, height );
1133+
// Split legend keys into X lines, where X = height.
1134+
// Lines use the provided width.
1135+
// This effectively limits the text to a 'width'x'height' box.
1136+
std::string ret;
1137+
height = 0;
1138+
const int h_max = max_height == 0 ? INT_MAX : max_height;
1139+
const int nsize = keys.size();
1140+
for( int row = 0, nidx = 0; row < h_max && nidx < nsize; row++ ) {
1141+
int wavail = width;
1142+
int nwidth = utf8_width( keys[nidx], true );
1143+
bool startofline = true;
1144+
while( nidx < nsize && ( wavail > nwidth || startofline ) ) {
1145+
startofline = false;
1146+
wavail -= nwidth;
1147+
ret += keys[nidx];
1148+
nidx++;
1149+
if( nidx < nsize ) {
1150+
nwidth = utf8_width( keys[nidx], true );
1151+
if( wavail > nwidth ) {
1152+
ret += " ";
1153+
wavail -= 2;
1154+
}
1155+
}
1156+
}
1157+
if( row < h_max - 1 ) {
1158+
ret += "\n";
1159+
}
1160+
height++;
1161+
}
1162+
return ret;
11541163
}
11551164

11561165
std::string display::colorized_bodypart_outer_armor( const Character &u, const bodypart_id &bp )

src/display.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ 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 );
105105
// Current body part status (bleeding, bitten, infected) phrase, fully colorized
106-
std::string colorized_bodypart_status_text( const Character &u, const bodypart_id &bp,
107-
const std::string &wgt_id );
108106
std::string colorized_bodypart_status_sym_text( const Character &u, const bodypart_id &bp,
109107
const std::string &wgt_id );
110108
std::string colorized_bodypart_status_legend_text( const Character &u,

src/widget.cpp

Lines changed: 0 additions & 7 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_text:
128-
return "bp_status_text";
129127
case widget_var::bp_status_sym_text:
130128
return "bp_status_sym_text";
131129
case widget_var::bp_status_legend_text:
@@ -841,7 +839,6 @@ bool widget::uses_text_function()
841839
case widget_var::activity_text:
842840
case widget_var::body_temp_text:
843841
case widget_var::bp_armor_outer_text:
844-
case widget_var::bp_status_text:
845842
case widget_var::bp_status_sym_text:
846843
case widget_var::bp_status_legend_text:
847844
case widget_var::compass_text:
@@ -931,10 +928,6 @@ std::string widget::color_text_function_string( const avatar &ava, unsigned int
931928
desc.first = display::colorized_bodypart_outer_armor( ava, only_bp() );
932929
apply_color = false; // Item name already colorized by tname
933930
break;
934-
case widget_var::bp_status_text:
935-
desc.first = display::colorized_bodypart_status_text( ava, only_bp(), id.str() );
936-
apply_color = false; // Has embedded color already
937-
break;
938931
case widget_var::bp_status_sym_text:
939932
desc.first = display::colorized_bodypart_status_sym_text( ava, only_bp(), id.str() );
940933
apply_color = false; // Already colorized

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_text, // Status of bodypart (bleeding, bitten, and/or infected)
5049
bp_status_sym_text, // Status of bodypart (same as above, but shortened to 1 char per status)
5150
bp_status_legend_text, // Legend describing the status indicators from bp_status_sym_text
5251
compass_text, // Compass / visible threats by cardinal direction

0 commit comments

Comments
 (0)