Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new widget to display weight with the format "##.#/##.#[unit]" #77212

Merged
merged 11 commits into from
Oct 26, 2024
27 changes: 27 additions & 0 deletions data/json/ui/sidebar-legacy-labels.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,32 @@
"arrange": "minimum_columns",
"widgets": [ "ll_place_info", "ll_place_overmap" ]
},
{
"id": "ll_carry_weight_label",
"width": 8,
"style": "symbol",
"text_align": "left",
"colors": [ "c_dark_gray" ],
"string": "Weight :",
"type": "widget"
},
{
"id": "ll_carry_weight_value",
"width": 12,
"style": "text",
"text_align": "left",
"var": "carry_weight_value",
"type": "widget"
},
{
"id": "ll_weight_carried_value",
"width": 13,
"style": "layout",
"label": "Weight Carried",
"arrange": "minimum_columns",
"widgets": [ "ll_carry_weight_label", "ll_carry_weight_value" ],
"type": "widget"
},
{
"id": "legacy_labels_sidebar",
"type": "widget",
Expand All @@ -194,6 +220,7 @@
"weapon_style_layout",
"vehicle_acf_label_layout",
"compass_all_danger_layout",
"ll_weight_carried_value",
"rad_badge_desc",
"sundial_label_layout"
]
Expand Down
29 changes: 29 additions & 0 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,35 @@
return std::make_pair( weight_text, weight_color );
}

// Weight carried, formatted as "current/max" in kg
std::pair<std::string, nc_color> display::carry_weight_value_color( const avatar &ava )
{
float carry_wt = ( convert_weight( ava.weight_carried() ) );

Check failure on line 1018 in src/display.cpp

View workflow job for this annotation

GitHub Actions / build (src)

Redundant parentheses. [cata-redundant-parentheses,-warnings-as-errors]
float max_wt = ( convert_weight( ava.weight_capacity() ) );

Check failure on line 1019 in src/display.cpp

View workflow job for this annotation

GitHub Actions / build (src)

Redundant parentheses. [cata-redundant-parentheses,-warnings-as-errors]

// Create a string showing "current_weight / max_weight"
std::string weight_text = string_format( "%.1f/%.1f %s", carry_wt, max_wt, weight_units() );

// Set the color based on carry weight
nc_color weight_color = c_green; // Default color

if( max_wt > 0 ) {
if( carry_wt > max_wt ) {
weight_color = c_red; // Exceeds capacity
} else if( carry_wt > 0.75 * max_wt ) {
weight_color = c_light_red; // Approaching capacity (75%)
} else if( carry_wt > 0.5 * max_wt ) {
weight_color = c_yellow; // At half capacity (50%)
} else if( carry_wt > 0.25 * max_wt ) {
weight_color = c_light_green; // Below half capacity (25%)
} else {
weight_color = c_green; // Light load
}
}

return std::make_pair( weight_text, weight_color );
}

std::pair<std::string, nc_color> display::overmap_note_symbol_color( const std::string_view
note_text )
{
Expand Down
3 changes: 3 additions & 0 deletions src/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ nc_color limb_color( const Character &u, const bodypart_id &bp, bool bleed, bool
// Color for displaying the given encumbrance level
nc_color encumb_color( int level );

// Weight carried, formatted as "current/max"
std::pair<std::string, nc_color> carry_weight_value_color( const avatar &ava );

// Colorized symbol for the overmap tile at the given location
std::pair<std::string, nc_color> overmap_tile_symbol_color( const avatar &u,
const tripoint_abs_omt &omt, bool edge_tile, bool &found_mi );
Expand Down
7 changes: 7 additions & 0 deletions src/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ std::string enum_to_string<widget_var>( widget_var data )
return "bp_armor_outer_text";
case widget_var::carry_weight_text:
return "carry_weight_text";
case widget_var::carry_weight_value:
return "carry_weight_value";
case widget_var::date_text:
return "date_text";
case widget_var::env_temp_text:
Expand Down Expand Up @@ -1046,6 +1048,7 @@ bool widget::uses_text_function() const
case widget_var::body_graph_wet:
case widget_var::bp_armor_outer_text:
case widget_var::carry_weight_text:
case widget_var::carry_weight_value:
case widget_var::compass_text:
case widget_var::compass_legend_text:
case widget_var::date_text:
Expand Down Expand Up @@ -1149,6 +1152,10 @@ std::string widget::color_text_function_string( const avatar &ava, unsigned int
case widget_var::carry_weight_text:
desc = display::carry_weight_text_color( ava );
break;
case widget_var::carry_weight_value:
desc = display::carry_weight_value_color( ava );
break;
break;
case widget_var::date_text:
desc.first = display::date_string();
break;
Expand Down
1 change: 1 addition & 0 deletions src/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ enum class widget_var : int {
body_graph_wet, // Body graph showing color-coded body part wetness
bp_armor_outer_text, // Outermost armor on body part, with color/damage bars
carry_weight_text, // Weight carried, relative to capacity, in %
carry_weight_value, // Weight carried, formatted as "current/max"
compass_text, // Compass / visible threats by cardinal direction
compass_legend_text, // Names of visible creatures that appear on the compass
date_text, // Current date, in terms of day within season
Expand Down
Loading