Skip to content

Commit

Permalink
Add labels to structured tiny bodygraphs (#77143)
Browse files Browse the repository at this point in the history
* Add labels to structured tiny bodygraphs

* Update tiny bodygraphs to use new label functionality

* Move tiny bodygraph label to torso
  • Loading branch information
sparr authored Oct 21, 2024
1 parent 888e34a commit 7aba755
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
5 changes: 3 additions & 2 deletions data/json/bodypart_graphs/full_body_widget.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"//2": "Note: The code assumes the graph is at most 15x13 (WxH)",
"type": "body_graph",
"id": "tiny_full_body_widget",
"fill_sym": "#",
"fill_sym": " ",
"fill_color": "white",
"rows": [
"1eme1 ",
Expand All @@ -104,7 +104,8 @@
"8": { "body_parts": [ "foot_r" ] },
"9": { "body_parts": [ "leg_l" ] },
"0": { "body_parts": [ "foot_l" ] }
}
},
"label_fill": ""
},
{
"//1": "Only used for displaying as a widget in the sidebar. Not to be included with other bodygraphs.",
Expand Down
27 changes: 23 additions & 4 deletions src/bodygraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ void bodygraph::load( const JsonObject &jo, const std::string_view )
parts.emplace( sym, bpg );
}
}

if( jo.has_string( "label_fill" ) ) {
label_fill = jo.get_string( "label_fill" );
}

}

void bodygraph::finalize()
Expand Down Expand Up @@ -649,7 +654,8 @@ void display_bodygraph( const Character &u, const bodygraph_id &id )
}

std::vector<std::string> get_bodygraph_lines( const Character &u,
const bodygraph_callback &fragment_cb, const bodygraph_id &id, int width, int height )
const bodygraph_callback &fragment_cb, const bodygraph_id &id, int width, int height,
const std::string_view &label )
{
width = ( width <= 0 || width > BPGRAPH_MAXCOLS ) ? BPGRAPH_MAXCOLS : width;
height = ( height <= 0 || height > BPGRAPH_MAXROWS ) ? BPGRAPH_MAXROWS : height;
Expand Down Expand Up @@ -680,9 +686,20 @@ std::vector<std::string> get_bodygraph_lines( const Character &u,
for( int i = 0; static_cast<size_t>( i ) < rid->rows.size() && i < height; i++ ) {
std::string ret_row;
int j = hflip ? rid->rows[i].size() - 1 : 0;
size_t label_i = 0;
for( int x = 0 ; x < width && j < BPGRAPH_MAXCOLS && j >= 0; hflip ? j-- : j++, x++ ) {
std::string sym = id->fill_sym.empty() ? rid->rows[i][j] : id->fill_sym;
auto iter = id->parts.find( rid->rows[i][j] );
std::string sym = " ";
const std::string &r = rid->rows[i][j];
bool label_sym = false;
if( !id->fill_rows.empty() && id->label_fill == id->fill_rows[i][j] ) {
if( label_i < label.length() ) {
label_sym = true;
sym = label[label_i++];
}
} else {
sym = id->fill_sym.empty() ? r : id->fill_sym;
}
auto iter = id->parts.find( r );
const bodygraph_part *bgp = nullptr;
if( iter != id->parts.end() ) {
bgp = &iter->second;
Expand All @@ -697,7 +714,9 @@ std::vector<std::string> get_bodygraph_lines( const Character &u,
missing_section = false;
}
}
sym = missing_section ? " " : ( id->fill_rows.empty() ? iter->second.sym : id->fill_rows[i][j] );
if( !label_sym ) {
sym = missing_section ? " " : ( id->fill_rows.empty() ? iter->second.sym : id->fill_rows[i][j] );
}
}
if( rid->rows[i][j] == " " ) {
sym = " ";
Expand Down
3 changes: 2 additions & 1 deletion src/bodygraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct bodygraph {
std::vector<std::vector<std::string>> rows;
std::vector<std::vector<std::string>> fill_rows;
std::map<std::string, bodygraph_part> parts;
std::string label_fill;
std::string fill_sym;
nc_color fill_color = c_white;
bool was_loaded = false;
Expand Down Expand Up @@ -87,6 +88,6 @@ using bodygraph_callback =
*/
std::vector<std::string> get_bodygraph_lines( const Character &u,
const bodygraph_callback &fragment_cb, const bodygraph_id &id = bodygraph_id::NULL_ID(),
int width = 0, int height = 0 );
int width = 0, int height = 0, const std::string_view &label = "" );

#endif // CATA_SRC_BODYGRAPH_H
5 changes: 4 additions & 1 deletion src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,8 @@ nc_color display::get_bodygraph_bp_color( const Character &u, const bodypart_id
cata_fatal( "Invalid widget_var" );
}

static const std::vector<std::string> bodygraph_var_labels = { "Health", "Temperature", "Encumbrance", "Status", "Wet" };

std::string display::colorized_bodygraph_text( const Character &u, const std::string &graph_id,
const bodygraph_var var, int width, int max_height, int &height )
{
Expand All @@ -1416,7 +1418,8 @@ std::string display::colorized_bodygraph_text( const Character &u, const std::st
return colorize( sym, sym_col.second );
};

std::vector<std::string> rows = get_bodygraph_lines( u, process_sym, graph, width, max_height );
std::vector<std::string> rows = get_bodygraph_lines( u, process_sym, graph, width, max_height,
bodygraph_var_labels[ int( var ) ] );
height = rows.size();

std::string ret;
Expand Down

0 comments on commit 7aba755

Please sign in to comment.