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

Bodypart status widget: Symbols + Legend #54531

Merged
merged 7 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 101 additions & 1 deletion data/json/ui/bodypart_status.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
[
{
"id": "bodypart_status_indicator_template",
"type": "widget",
"style": "phrase",
"phrases": [
{ "id": "bitten", "text": "bitten", "sym": "B", "color": "yellow" },
{ "id": "infected", "text": "infected", "sym": "I", "color": "pink" },
{ "id": "broken", "text": "broken", "sym": "%", "color": "magenta" },
{ "id": "splinted", "text": "splinted", "sym": "=", "color": "light_gray" },
{ "id": "bandaged", "text": "bandaged", "sym": "+", "color": "white" },
{ "id": "disinfected", "text": "disinfected", "sym": "$", "color": "light_green" },
{ "id": "bleeding", "text": "bleeding", "value": 0, "sym": "b", "color": "light_red" },
{ "id": "bleeding", "text": "bleeding", "value": 11, "sym": "b", "color": "red" },
{ "id": "bleeding", "text": "bleeding", "value": 21, "sym": "b", "color": "red_red" }
]
},
{
"id": "bodypart_status_text",
"//": "Base widget for showing body part status; needs bodypart field defined in derived widget.",
"type": "widget",
"style": "text",
"var": "bp_status_text"
"var": "bp_status_text",
"copy-from": "bodypart_status_indicator_template"
},
{
"id": "bodypart_status_sym_text",
"//": "Base widget for body part status symbols; needs bodypart field defined in derived widget.",
"type": "widget",
"style": "text",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although the base style is "phrase", the copy has style "text", and it still works OK right?

If so, there may be no need for a separate "phrase" style; we should just use "text" for all of them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, copy-from will overwrite the style with whatever you give it. You can put anything in the style field, really. And the display function can decide how to interpret it if it detects that the widget has a phrases array.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hangon, I think I know why I got confused, and I hope you'll indulge me a lengthy explanation because I just realized it's an important oversight in the widget implementation.

There's something in the widget class that I stuck there as part of my original idea for phrase widgets, but which is basically dead code. It's this widget::text function, invoked by widget::value_string for "text" style widgets.

std::string widget::text( int value, int /* value_max */ )
{
return _strings.at( value ).translated();
}

My thinking back then was to define a field something like "strings": [ "Peckish", "Hungry", "Ravenous" ], and these strings would be mapped to 0, 1, 2 - as returned by some (still mythical) function that yielded a simple numerical progression. It's even defined in the header, and the first thing read in at the top of widget::load:

Cataclysm-DDA/src/widget.h

Lines 159 to 160 in 62a046c

// String values mapped to numeric values or ranges
std::vector<translation> _strings;

void widget::load( const JsonObject &jo, const std::string & )
{
optional( jo, was_loaded, "strings", _strings );

But widget::text is never invoked, because widget::show circumvents color_value_string for any widget that matches uses_text_function - in other words all the *_text widgets I've defined since then, because it was just easier that way.

if( uses_text_function() ) {
// Text functions are a carry-over from before widgets, with existing functions generating
// descriptive colorized text for avatar attributes. The "value" for these is immaterial;

Now, I like the "phrases" data structure a lot better than a naked list of "strings", but this may be the opportunity to hook up that widget::text function for the dynamic rendering it was always destined for. All the dirty work is already done by your widget_phrase class. Would it be feasible to hook into place, you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be feasible to hook into place, you think?

Absolutely. I can connect the dots in a follow-up PR. It will probably pave the way for #54585.

[...] this may be the opportunity to hook up that widget::text function for the dynamic rendering [...]

That's a great idea, and along the lines of what I'd like to do as well. It could work like a convenience function to wrap around the phrase functions.

My thought for widget_phrase was a simple container for fields, which are accessed via static methods:

widget_phrase::get_<field>_for_<condition>( <conditions>, <widget to search> );
widget_phrase::get_text_for_id( const std::string &phrase_id, const widget_id &wgt );

The widget functions could wrap these for convenience. The main reason I'm using static accessors for widget_phrase is because I wanted to avoid passing the widget itself to the display functions.

"var": "bp_status_sym_text",
"copy-from": "bodypart_status_indicator_template"
},
{
"id": "bp_status_head_text",
Expand Down Expand Up @@ -48,6 +73,59 @@
"bodypart": "leg_r",
"copy-from": "bodypart_status_text"
},
{
"id": "bp_status_sym_head_text",
"type": "widget",
"label": "HEAD",
"bodypart": "head",
"copy-from": "bodypart_status_sym_text"
},
{
"id": "bp_status_sym_torso_text",
"type": "widget",
"label": "TORSO",
"bodypart": "torso",
"copy-from": "bodypart_status_sym_text"
},
{
"id": "bp_status_sym_left_arm_text",
"type": "widget",
"label": "L ARM",
"bodypart": "arm_l",
"copy-from": "bodypart_status_sym_text"
},
{
"id": "bp_status_sym_right_arm_text",
"type": "widget",
"label": "R ARM",
"bodypart": "arm_r",
"copy-from": "bodypart_status_sym_text"
},
{
"id": "bp_status_sym_left_leg_text",
"type": "widget",
"label": "L LEG",
"bodypart": "leg_l",
"copy-from": "bodypart_status_sym_text"
},
{
"id": "bp_status_sym_right_leg_text",
"type": "widget",
"label": "R LEG",
"bodypart": "leg_r",
"copy-from": "bodypart_status_sym_text"
},
{
"id": "bp_status_legend_text",
"type": "widget",
"label": "Status Legend",
"style": "text",
"var": "bp_status_legend_text",
"copy-from": "bodypart_status_indicator_template",
"width": 0,
"height": 3,
"flags": [ "W_LABEL_NONE", "W_PAD_NONE", "W_DYNAMIC_HEIGHT" ]
},
{
"id": "bodypart_status_top_layout",
"type": "widget",
Expand All @@ -62,6 +140,20 @@
"arrange": "columns",
"widgets": [ "bp_status_left_leg_text", "bp_status_torso_text", "bp_status_right_leg_text" ]
},
{
"id": "bodypart_status_top_sym_layout",
"type": "widget",
"style": "layout",
"arrange": "columns",
"widgets": [ "bp_status_sym_left_arm_text", "bp_status_sym_head_text", "bp_status_sym_right_arm_text" ]
},
{
"id": "bodypart_status_bottom_sym_layout",
"type": "widget",
"style": "layout",
"arrange": "columns",
"widgets": [ "bp_status_sym_left_leg_text", "bp_status_sym_torso_text", "bp_status_sym_right_leg_text" ]
},
{
"id": "bodypart_status_layout",
"type": "widget",
Expand All @@ -76,5 +168,13 @@
"bp_status_left_leg_text",
"bp_status_right_leg_text"
]
},
{
"id": "bodypart_status_sym_layout",
"type": "widget",
"style": "layout",
"label": "Body Part Status Alt",
"arrange": "rows",
"widgets": [ "bodypart_status_top_sym_layout", "bodypart_status_bottom_sym_layout", "bp_status_legend_text" ]
}
]
2 changes: 2 additions & 0 deletions data/json/ui/compass.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"label": "Compass Legend",
"style": "text",
"var": "compass_legend_text",
"width": 0,
"height": 3,
"flags": [ "W_LABEL_NONE", "W_PAD_NONE" ]
},
Expand All @@ -95,6 +96,7 @@
"label": "Compass Legend",
"style": "text",
"var": "compass_legend_text",
"width": 0,
"height": 5,
"flags": [ "W_LABEL_NONE", "W_PAD_NONE", "W_DYNAMIC_HEIGHT" ]
},
Expand Down
3 changes: 2 additions & 1 deletion data/json/ui/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"light_moon_wind_temp_layout",
"vehicle_azimuth_cruise_fuel_layout",
"vital_numbers_layout",
"bodypart_status_layout"
"bodypart_status_layout",
"bodypart_status_sym_layout"
]
}
]
51 changes: 49 additions & 2 deletions data/mods/TEST_DATA/widgets.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
[
{
"id": "test_bp_status_indicator_template",
"type": "widget",
"style": "phrase",
"phrases": [
{ "id": "bitten", "text": "bitten", "sym": "B", "color": "yellow" },
{ "id": "infected", "text": "infected", "sym": "I", "color": "pink" },
{ "id": "broken", "text": "broken", "sym": "%", "color": "magenta" },
{ "id": "splinted", "text": "splinted", "sym": "=", "color": "light_gray" },
{ "id": "bandaged", "text": "bandaged", "sym": "+", "color": "white" },
{ "id": "disinfected", "text": "disinfected", "sym": "$", "color": "light_green" },
{ "id": "bleeding", "text": "bleeding", "value": 0, "sym": "b", "color": "light_red" },
{ "id": "bleeding", "text": "bleeding", "value": 11, "sym": "b", "color": "red" },
{ "id": "bleeding", "text": "bleeding", "value": 21, "sym": "b", "color": "red_red" }
]
},
{
"id": "test_bucket_graph",
"type": "widget",
Expand Down Expand Up @@ -283,15 +299,46 @@
"label": "TORSO STATUS",
"var": "bp_status_text",
"bodypart": "torso",
"style": "text"
"style": "text",
"copy-from": "test_bp_status_indicator_template"
},
{
"id": "test_status_left_arm_text",
"type": "widget",
"label": "LEFT ARM STATUS",
"var": "bp_status_text",
"bodypart": "arm_l",
"style": "text"
"style": "text",
"copy-from": "test_bp_status_indicator_template"
},
{
"id": "test_status_sym_torso_text",
"type": "widget",
"style": "text",
"var": "bp_status_sym_text",
"label": "TORSO",
"bodypart": "torso",
"copy-from": "test_bp_status_indicator_template"
},
{
"id": "test_status_sym_left_arm_text",
"type": "widget",
"style": "text",
"var": "bp_status_sym_text",
"label": "L ARM",
"bodypart": "arm_l",
"copy-from": "test_bp_status_indicator_template"
},
{
"id": "test_status_legend_text",
"type": "widget",
"label": "Status Legend",
"style": "text",
"var": "bp_status_legend_text",
"copy-from": "test_bp_status_indicator_template",
"width": 0,
"height": 3,
"flags": [ "W_LABEL_NONE", "W_PAD_NONE", "W_DYNAMIC_HEIGHT" ]
},
{
"id": "test_overmap_3x3_text",
Expand Down
5 changes: 5 additions & 0 deletions lang/string_extractor/parsers/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ def parse_widget(json, origin):
for string in json["strings"]:
write_text(string, origin,
comment="Text in UI widget \"{}\"".format(id))
if "phrases" in json:
for phrase in json["phrases"]:
comment = "Text in portion of UI widget \"{}\"".format(id)
if "text" in phrase:
write_text(phrase["text"], origin, comment=comment)
Loading