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

[EOC] fix somethings and add somethings for #75675 string input #75949

Merged
merged 3 commits into from
Aug 26, 2024
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
7 changes: 4 additions & 3 deletions doc/EFFECT_ON_CONDITION.md
Original file line number Diff line number Diff line change
Expand Up @@ -3050,8 +3050,9 @@ Store string from `set_string_var` in the variable object `target_var`

| Property | Optionality | Type | Description |
| --- | --- | --- | --- |
| "title" | **mandatory** | string, [variable object](##variable-object) | The title of the input popup window, can be localized (e.g., `"title": { "i18n": true, "str": "Input a value:" }`). |
| "description" | **mandatory** | string, [variable object](##variable-object) | The description of the input popup window, can be localized. |
| "title" | optional | string, [variable object](##variable-object) | The title of the input popup window, can be localized (e.g., `"title": { "i18n": true, "str": "Input a value:" }`). |
| "description" | optional | string, [variable object](##variable-object) | The description of the input popup window, can be localized. |
| "default_text" | optional | string, [variable object](##variable-object) | The default text in the input popup window, can be localized. |
| "width" | optional | integer | The character length of the input box. Default is 20. |
| "identifier" | optional | string | Input boxes with the same identifier share input history. Default is `""`. |
| "only_digits" | optional | boolean | Whether the input is purely numeric. Default is false. |
Expand All @@ -3060,7 +3061,7 @@ Store string from `set_string_var` in the variable object `target_var`

| Avatar | Character | NPC | Monster | Furniture | Item |
| ------ | --------- | --------- | ---- | ------- | --- |
| ✔️ | ✔️ | ✔️ | | | |
| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |

##### Examples
Replace value of variable `foo` with value `bar`
Expand Down
7 changes: 7 additions & 0 deletions lang/string_extractor/parsers/effect.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ def parse_effect(effects, origin, comment=""):
comment="String input window's description in {}"
.format(comment)
)
if "default_text" in string_input:
write_translation_or_var(
string_input["default_text"],
origin,
comment="String input window's default_text in {}"
.format(comment)
)
if ("u_spawn_monster" in eff or "npc_spawn_monster" in eff or
"u_spawn_npc" in eff or "npc_spawn_npc" in eff):
if "u_spawn_monster" in eff or "npc_spawn_monster" in eff:
Expand Down
22 changes: 17 additions & 5 deletions src/npctalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4830,19 +4830,31 @@ talk_effect_fun_t::func f_set_string_var( const JsonObject &jo, std::string_view
if( input_params.has_value() ) {
string_input_popup popup;
popup
.title( input_params.value().title.evaluate( d ) )
.description( input_params.value().description.evaluate( d ) )
.title( input_params.value().title ? input_params.value().title->evaluate( d ) : "" )
.description( input_params.value().description ? input_params.value().description->evaluate(
d ) : "" )
.width( input_params.value().width )
.identifier( input_params.value().identifier );
.identifier( input_params.value().identifier ? input_params.value().identifier->evaluate(
d ) : "" );

if( input_params.value().only_digits ) {
int num_temp;
int num_temp = 0;
try {
num_temp = std::stoi( input_params.value().default_text.has_value() ?
input_params.value().default_text->evaluate(
d ) : "0" );
} catch( const std::out_of_range &e ) {
debugmsg( "The number is too large to fit in an int." );
} catch( const std::invalid_argument &e ) {
};
popup.edit( num_temp );
if( !popup.canceled() ) {
str = std::to_string( num_temp );
}
} else {
std::string str_temp;
std::string str_temp = input_params.value().default_text ?
input_params.value().default_text->evaluate(
d ) : "";
popup.edit( str_temp );
if( !popup.canceled() ) {
str = str_temp;
Expand Down
9 changes: 7 additions & 2 deletions src/string_input_popup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,11 +680,16 @@ string_input_params string_input_params::parse_string_input_params( const JsonOb
const JsonValue &jv_description = jo.get_member( "description" );
p.description = get_str_translation_or_var( jv_description, "" );
}
if( jo.has_member( "default_text" ) ) {
const JsonValue &jv_default_text = jo.get_member( "default_text" );
p.default_text = get_str_translation_or_var( jv_default_text, "" );
}
if( jo.has_int( "width" ) ) {
p.width = jo.get_int( "width" );
}
if( jo.has_string( "identifier" ) ) {
p.identifier = jo.get_string( "identifier" );
if( jo.has_member( "identifier" ) ) {
const JsonValue &jv_identifier = jo.get_member( "identifier" );
p.identifier = get_str_or_var( jv_identifier, "" );
}
if( jo.has_bool( "only_digits" ) ) {
p.only_digits = jo.get_bool( "only_digits" );
Expand Down
7 changes: 4 additions & 3 deletions src/string_input_popup.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,11 @@ class string_input_popup // NOLINT(cata-xy)
};

struct string_input_params {
str_translation_or_var title;
str_translation_or_var description;
std::optional<str_translation_or_var> title;
std::optional<str_translation_or_var> description;
std::optional<str_translation_or_var> default_text;
int width = 20;
std::string identifier;
std::optional<str_or_var> identifier;
bool only_digits = false;
static string_input_params parse_string_input_params( const JsonObject &jo );
};
Expand Down
Loading