From 0425f0ecc2f3e428cc9d48114d86b4463b94025d Mon Sep 17 00:00:00 2001 From: Zhan Avesna <97810032+AudBobb@users.noreply.github.com> Date: Sat, 26 Oct 2024 18:37:00 -0500 Subject: [PATCH] Add a new widget to display weight with the format "##.#/##.#[unit]" (#77212) * Add two new widgets to display weight with the format "##.#/##.# kg" and "##.#/##.# lb", and add the widgets to the legacy labels sidebar * Apply suggestions from code review I don't format goodly Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Remove imperial version of the feature, and modify the metric version to read users mass setting and adjust the display accordingly * Apply suggestions from code review Formatting Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Updated per IdleSol suggestion * I think I forgot to pull before pushing and I don't understand git so I think it ate this space in the merge :( * I'll get it right one day * Update to use functions I didn't know existed, removing a few lines of code. * Update src/display.cpp Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- data/json/ui/sidebar-legacy-labels.json | 27 +++++++++++++++++++++++ src/display.cpp | 29 +++++++++++++++++++++++++ src/display.h | 3 +++ src/widget.cpp | 7 ++++++ src/widget.h | 1 + 5 files changed, 67 insertions(+) diff --git a/data/json/ui/sidebar-legacy-labels.json b/data/json/ui/sidebar-legacy-labels.json index 3ff4b4bbb256e..4a81b0a0ec619 100644 --- a/data/json/ui/sidebar-legacy-labels.json +++ b/data/json/ui/sidebar-legacy-labels.json @@ -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", @@ -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" ] diff --git a/src/display.cpp b/src/display.cpp index c2e5a0c675d6c..ec0713da1d9d3 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -1012,6 +1012,35 @@ std::pair display::carry_weight_text_color( const avatar return std::make_pair( weight_text, weight_color ); } +// Weight carried, formatted as "current/max" in kg +std::pair display::carry_weight_value_color( const avatar &ava ) +{ + float carry_wt = ( convert_weight( ava.weight_carried() ) ); + float max_wt = ( convert_weight( ava.weight_capacity() ) ); + + // 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 display::overmap_note_symbol_color( const std::string_view note_text ) { diff --git a/src/display.h b/src/display.h index 1dec42a2c6b13..dec8b7c3acd06 100644 --- a/src/display.h +++ b/src/display.h @@ -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 carry_weight_value_color( const avatar &ava ); + // Colorized symbol for the overmap tile at the given location std::pair overmap_tile_symbol_color( const avatar &u, const tripoint_abs_omt &omt, bool edge_tile, bool &found_mi ); diff --git a/src/widget.cpp b/src/widget.cpp index 726497b765dae..d81d908d6256e 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -146,6 +146,8 @@ std::string enum_to_string( 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: @@ -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: @@ -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; diff --git a/src/widget.h b/src/widget.h index bdf6fc7dc34dd..491b925d17a6b 100644 --- a/src/widget.h +++ b/src/widget.h @@ -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