Skip to content

Commit

Permalink
implement fill patterns for bodygraph widget
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 committed Jul 20, 2022
1 parent ae2ff5c commit fb52ac0
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 13 deletions.
30 changes: 30 additions & 0 deletions data/json/bodypart_graphs/full_body_widget.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,35 @@
"9": { "body_parts": [ "leg_l" ] },
"0": { "body_parts": [ "foot_l" ] }
}
},
{
"//1": "Only used for displaying as a widget in the sidebar. Not to be included with other bodygraphs.",
"//2": "Note: The code assumes the graph is at most 15x13 (WxH)",
"type": "body_graph",
"id": "tiny_full_body_widget",
"fill_sym": "#",
"fill_color": "white",
"rows": [
" 1 ",
"43256 ",
"87 90 "
],
"fill_rows": [
" O ",
"./#\\. ",
"_/ \\_ "
],
"parts": {
"1": { "body_parts": [ "head" ] },
"2": { "body_parts": [ "torso" ] },
"3": { "body_parts": [ "arm_r" ] },
"4": { "body_parts": [ "hand_r" ] },
"5": { "body_parts": [ "arm_l" ] },
"6": { "body_parts": [ "hand_l" ] },
"7": { "body_parts": [ "leg_r" ] },
"8": { "body_parts": [ "foot_r" ] },
"9": { "body_parts": [ "leg_l" ] },
"0": { "body_parts": [ "foot_l" ] }
}
}
]
35 changes: 35 additions & 0 deletions data/json/ui/body_graph.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
"height": 7,
"body_graph": "compact_full_body_widget"
},
{
"id": "tiny_body_graph_template",
"type": "widget",
"copy-from": "body_graph_template",
"height": 3,
"body_graph": "tiny_full_body_widget"
},
{
"id": "body_graph",
"type": "widget",
Expand All @@ -31,6 +38,13 @@
"label": "Body Graph",
"var": "body_graph"
},
{
"id": "tiny_body_graph",
"type": "widget",
"copy-from": "tiny_body_graph_template",
"label": "Body Graph",
"var": "body_graph"
},
{
"id": "body_graph_temp",
"type": "widget",
Expand All @@ -45,6 +59,13 @@
"label": "Body Graph (temperature)",
"var": "body_graph_temp"
},
{
"id": "tiny_body_graph_temp",
"type": "widget",
"copy-from": "tiny_body_graph_template",
"label": "Body Graph (temperature)",
"var": "body_graph_temp"
},
{
"id": "body_graph_encumb",
"type": "widget",
Expand All @@ -59,6 +80,13 @@
"label": "Body Graph (encumbrance)",
"var": "body_graph_encumb"
},
{
"id": "tiny_body_graph_encumb",
"type": "widget",
"copy-from": "tiny_body_graph_template",
"label": "Body Graph (encumbrance)",
"var": "body_graph_encumb"
},
{
"id": "body_graph_status",
"type": "widget",
Expand All @@ -72,5 +100,12 @@
"copy-from": "compact_body_graph_template",
"label": "Body Graph (status)",
"var": "body_graph_status"
},
{
"id": "tiny_body_graph_status",
"type": "widget",
"copy-from": "tiny_body_graph_template",
"label": "Body Graph (status)",
"var": "body_graph_status"
}
]
56 changes: 44 additions & 12 deletions src/bodygraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,23 @@ void bodygraph::load( const JsonObject &jo, const std::string & )
mandatory( jo, false, "mirror", mirror );
} else if( !was_loaded || jo.has_array( "rows" ) ) {
rows.clear();
fill_rows.clear();
for( const JsonValue jval : jo.get_array( "rows" ) ) {
if( !jval.test_string() ) {
jval.throw_error( "\"rows\" array must contain string values." );
} else {
rows.emplace_back( utf8_display_split( jval.get_string() ) );
}
}
if( jo.has_array( "fill_rows" ) ) {
for( const JsonValue jval : jo.get_array( "fill_rows" ) ) {
if( !jval.test_string() ) {
jval.throw_error( "\"rows\" array must contain string values." );
} else {
fill_rows.emplace_back( utf8_display_split( jval.get_string() ) );
}
}
}
}

if( !was_loaded || jo.has_object( "parts" ) ) {
Expand Down Expand Up @@ -108,10 +118,16 @@ void bodygraph::finalize()
debugmsg( "body_graph \"%s\" defines more rows than the maximum (%d).", id.c_str(),
BPGRAPH_MAXROWS );
}

if( !fill_rows.empty() && fill_rows.size() != rows.size() ) {
debugmsg( "body_graph \"%s\" defines a different number of fill_rows than rows (%d vs. %d).", id.c_str(),
fill_rows.size(), rows.size() );
}

int width = -1;
bool w_warned = false;
for( std::vector<std::string> &r : rows ) {
for( size_t i = 0; i < rows.size(); i++ ) {
std::vector<std::string> &r = rows[i];
int w = r.size();
if( width == -1 ) {
width = w;
Expand All @@ -123,20 +139,36 @@ void bodygraph::finalize()
BPGRAPH_MAXCOLS );
w_warned = true;
}

r.insert( r.begin(), ( BPGRAPH_MAXCOLS - w ) / 2, " " );
r.insert( r.end(), BPGRAPH_MAXCOLS - r.size(), " " );

if( ! fill_rows.empty() ) {
std::vector<std::string> &fr = fill_rows[i];
if( fr.size() != r.size() ) {
debugmsg( "body_graph \"%s\" defines a different number of columns in fill_rows than in rows (%d vs. %d).", id.c_str(),
fr.size(), w );
}
fr.insert( fr.begin(), ( BPGRAPH_MAXCOLS - w ) / 2, " " );
fr.insert( fr.end(), BPGRAPH_MAXCOLS - fr.size(), " " );
}

}

for( std::vector<std::vector<std::string>> temp_rows : {rows, fill_rows} ) {
if( !temp_rows.empty() ) {
for( int i = ( BPGRAPH_MAXROWS - temp_rows.size() ) / 2; i > 0; i-- ) {
std::vector<std::string> r;
r.insert( r.begin(), BPGRAPH_MAXCOLS, " " );
temp_rows.insert( temp_rows.begin(), r );
}

for( int i = ( BPGRAPH_MAXROWS - rows.size() ) / 2; i > 0; i-- ) {
std::vector<std::string> r;
r.insert( r.begin(), BPGRAPH_MAXCOLS, " " );
rows.insert( rows.begin(), r );
}

for( int i = rows.size(); i <= BPGRAPH_MAXROWS; i++ ) {
std::vector<std::string> r;
r.insert( r.begin(), BPGRAPH_MAXCOLS, " " );
rows.emplace_back( r );
for( int i = temp_rows.size(); i <= BPGRAPH_MAXROWS; i++ ) {
std::vector<std::string> r;
r.insert( r.begin(), BPGRAPH_MAXCOLS, " " );
temp_rows.emplace_back( r );
}
}
}
}

Expand Down Expand Up @@ -653,7 +685,7 @@ std::vector<std::string> get_bodygraph_lines( const Character &u,
missing_section = false;
}
}
sym = missing_section ? " " : iter->second.sym;
sym = missing_section ? " " : (id->fill_rows.empty() ? iter->second.sym : id->fill_rows[i][j]);
}
if( rid->rows[i][j] == " " ) {
sym = " ";
Expand Down
1 change: 1 addition & 0 deletions src/bodygraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct bodygraph {
cata::optional<bodypart_id> parent_bp;
cata::optional<bodygraph_id> mirror;
std::vector<std::vector<std::string>> rows;
std::vector<std::vector<std::string>> fill_rows;
std::map<std::string, bodygraph_part> parts;
std::string fill_sym;
nc_color fill_color = c_white;
Expand Down
2 changes: 1 addition & 1 deletion src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,7 @@ std::string display::colorized_bodygraph_text( const Character &u, const std::st
return sym;
}
std::pair<std::string, nc_color> sym_col = get_bodygraph_bp_sym_color( u, *bgp, var );
return colorize( sym_col.first, sym_col.second );
return colorize( sym, sym_col.second );
};

std::vector<std::string> rows = get_bodygraph_lines( u, process_sym, graph, width, max_height );
Expand Down

0 comments on commit fb52ac0

Please sign in to comment.