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

make clause of widget to support displaying global_val #71070

Merged
merged 6 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions doc/WIDGETS.md
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,8 @@ how to display them. These take the form of a nested object containing several o
"clauses": [
{ "id": "bitten", "text": "bitten", "sym": "B", "color": "yellow", "condition": "..." },
{ "id": "infected", "text": "infected", "sym": "I", "color": "pink", "condition": "..." },
{ "id": "bandaged", "text": "bandaged", "sym": "+", "color": "white", "condition": "..." }
{ "id": "bandaged", "text": "bandaged", "sym": "+", "color": "white", "condition": "..." },
{ "id": "some_var", "text": "<color_red>The some_var</color> is <global_val:some_var>", "parse_tags":true , "condition": "..." },
]
}
```
Expand All @@ -905,7 +906,7 @@ which provides text and color definitions for different bodypart status conditio
| `value` | A numeric value for this "clause", which may be interpreted differently based on the context of the parent widget.
| `widgets` | For "layout" style widgets, the child widgets used for this "clause".
| `condition` | A dialogue condition (see [Dialogue conditions](NPCs.md#dialogue-conditions)) that dictates whether this clause will be used or not. If the condition is true (or when no condition is defined), the clause can be used to its text/symbol/color in the widget's value.

| `parse_tags`| default false. If true, parse custom entries in `text` before displaying it. This can be used to display global_val or u_val.(see [Special Custom Entries](NPCs.md#special-custom-entries) for details) You can also use `<color_XXX></color>` to modify the color of your text.

## Conditions

Expand Down
14 changes: 12 additions & 2 deletions src/npctalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2043,7 +2043,7 @@ void parse_tags( std::string &phrase, const Character &u, const Character &me, c
void parse_tags( std::string &phrase, const talker &u, const talker &me, const dialogue &d,
const itype_id &item_type )
{
phrase = SNIPPET.expand( remove_color_tags( phrase ) );
phrase = SNIPPET.expand( phrase );

const Character *u_chr = u.get_character();
const Character *me_chr = me.get_character();
Expand All @@ -2054,6 +2054,16 @@ void parse_tags( std::string &phrase, const talker &u, const talker &me, const d
do {
fa = phrase.find( '<' );
fb = phrase.find( '>' );
// Skip the <color_XXX> tag
if( fa != std::string::npos && phrase.compare( fa + 1, 6, "color_" ) == 0 ) {
fa = phrase.find( '<', fa + 7 );
fb = phrase.find( '>', fa );
}
// Skip the </color> tag
if( fa != std::string::npos && phrase.compare( fa + 1, 7, "/color>" ) == 0 ) {
fa = phrase.find( '<', fa + 8 );
fb = phrase.find( '>', fa );
}
PipeYume marked this conversation as resolved.
Show resolved Hide resolved
if( fa != std::string::npos ) {
size_t nest = 0;
fa_ = phrase.find( '<', fa + 1 );
Expand All @@ -2070,7 +2080,7 @@ void parse_tags( std::string &phrase, const talker &u, const talker &me, const d
if( fa != std::string::npos && fb != std::string::npos ) {
tag = phrase.substr( fa, fb - fa + 1 );
} else {
return;
break;
}

const item_location u_weapon = u_chr ? u_chr->get_wielded_item() : item_location();
Expand Down
19 changes: 16 additions & 3 deletions src/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "json.h"
#include "output.h"
#include "overmapbuffer.h"
#include "npctalk.h"

const static flag_id json_flag_W_DISABLED_BY_DEFAULT( "W_DISABLED_BY_DEFAULT" );
const static flag_id json_flag_W_DISABLED_WHEN_EMPTY( "W_DISABLED_WHEN_EMPTY" );
Expand Down Expand Up @@ -293,6 +294,9 @@ void widget_clause::load( const JsonObject &jo )
read_condition( jo, "condition", condition, false );
has_condition = true;
}
if( jo.has_bool( "parse_tags" ) ) {
should_parse_tags = jo.get_bool( "parse_tags" );
}

optional( jo, false, "widgets", widgets, string_id_reader<::widget> {} );
}
Expand Down Expand Up @@ -1408,8 +1412,13 @@ std::string widget::text_cond( bool no_join, int width )
std::vector<std::string> strings;
strings.reserve( wplist.size() );
for( const widget_clause *wp : wplist ) {
strings.emplace_back( wp->color == c_unset ? wp->text.translated() : colorize(
wp->text.translated(), wp->color ) );
std::string txt = wp->text.translated();
if( wp->should_parse_tags ) {
parse_tags( txt, get_player_character(), get_player_character() );
}
txt = wp->color == c_unset ? txt : colorize(
txt, wp->color );
strings.emplace_back( txt );
}
int h = 0;
std::string ret = format_widget_multiline( strings, _height_max, width, h, !no_join );
Expand Down Expand Up @@ -1457,7 +1466,11 @@ std::string widget::sym_text_cond( bool no_join, int width )
strings.reserve( wplist.size() );
for( const widget_clause *wp : wplist ) {
std::string s = wp->color == c_unset ? wp->sym : colorize( wp->sym, wp->color );
std::string txt = string_format( "%s %s", s, wp->text.translated() );
std::string txt_str = wp->text.translated();
if( wp->should_parse_tags ) {
parse_tags( txt_str, get_player_character(), get_player_character() );
}
std::string txt = string_format( "%s %s", s, txt_str );
strings.emplace_back( txt );
}
int h = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ struct widget_clause {

// Condition for using this clause
bool has_condition = false;
// Whether parse tags in this clause
bool should_parse_tags = false;
std::function<bool( dialogue & )> condition;
bool meets_condition( const std::string &opt_var = "" ) const;
bool meets_condition( const std::set<bodypart_id> &bps ) const;
Expand Down
Loading