From 6e077b0899505f6cbc7713a9d564342faaf6348c Mon Sep 17 00:00:00 2001 From: Brendan Hagan Date: Tue, 21 Jun 2022 22:28:49 -0400 Subject: [PATCH 1/3] feat: add extra_data() function to expose extra card fields --- src/script/functions/editor.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/script/functions/editor.cpp b/src/script/functions/editor.cpp index 0c9ad735..6deaa3b9 100644 --- a/src/script/functions/editor.cpp +++ b/src/script/functions/editor.cpp @@ -382,6 +382,22 @@ SCRIPT_FUNCTION(count_chosen) { SCRIPT_RETURN(count); } } +} + +// ----------------------------------------------------------------------------- : Extra Card Fields + +SCRIPT_FUNCTION(extra_data) { + SCRIPT_PARAM_C(String, input); + SCRIPT_PARAM_C(CardP, card); + SCRIPT_PARAM_C(StyleSheetP, stylesheet); + + FOR_EACH(valueP, card->extraDataFor(*stylesheet)) { + if (valueP->fieldP->name == input) { + SCRIPT_RETURN(valueP); + } + } + + return delay_error(ScriptErrorNoMember("extra_data()", input)); } // ----------------------------------------------------------------------------- : Init @@ -395,5 +411,6 @@ void init_script_editor_functions(Context& ctx) { ctx.setVariable(_("require_choice"), script_require_choice); ctx.setVariable(_("exclusive_choice"), script_exclusive_choice); ctx.setVariable(_("require_exclusive_choice"), script_require_exclusive_choice); - ctx.setVariable(_("remove_choice"), script_remove_choice); + ctx.setVariable(_("remove_choice"), script_remove_choice); + ctx.setVariable(_("extra_data"), script_extra_data); } From be2635f778f0927fcbffba48fa266055d8155948 Mon Sep 17 00:00:00 2001 From: Brendan Hagan Date: Tue, 21 Jun 2022 23:15:39 -0400 Subject: [PATCH 2/3] misc: apply canonical field name transformation --- CHANGES.md | 1 + src/script/functions/editor.cpp | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 40e7e11f..fea3ca08 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,7 @@ Features: * Add Button to Center the loaded image in the Image Slice Window. * Add "Created At", "Last Modified At" columns to card list. * Add filter box to Game and Stylesheet selection. + * Add extra_card("field name") script function for accessing Extra Card Fields. ------------------------------------------------------------------------------ HEAD: new items added as changes are made diff --git a/src/script/functions/editor.cpp b/src/script/functions/editor.cpp index 6deaa3b9..223829d8 100644 --- a/src/script/functions/editor.cpp +++ b/src/script/functions/editor.cpp @@ -391,8 +391,12 @@ SCRIPT_FUNCTION(extra_data) { SCRIPT_PARAM_C(CardP, card); SCRIPT_PARAM_C(StyleSheetP, stylesheet); + // Transform input to standard field name syntax. + // Other functions are doing lookups for ValuePs, which I assume is doing some of this automatically. + String canonical_field_name = canonical_name_form(input); + FOR_EACH(valueP, card->extraDataFor(*stylesheet)) { - if (valueP->fieldP->name == input) { + if (valueP->fieldP->name == canonical_field_name) { SCRIPT_RETURN(valueP); } } From 4f6c984f48358fdf8dfefcb05c8d8c77b56f57e7 Mon Sep 17 00:00:00 2001 From: Brendan Hagan Date: Fri, 1 Jul 2022 20:44:00 -0400 Subject: [PATCH 3/3] misc: extra_data doc entry --- doc/function/extra_data.txt | 27 +++++++++++++++++++++++++++ doc/function/index.txt | 3 ++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 doc/function/extra_data.txt diff --git a/doc/function/extra_data.txt b/doc/function/extra_data.txt new file mode 100644 index 00000000..1f5b754e --- /dev/null +++ b/doc/function/extra_data.txt @@ -0,0 +1,27 @@ +Function: extra_data + +DOC_MSE_VERSION: since 2.2.0 + +--Usage-- +> extra_data("field_name") + +Access the value of extra card fields. + +Due to lazy initialization of stylesheet specific Extra Card Fields they are not exposed directly from the card using `card.field_name` syntax. +As a workaround this function returns the Value of the specified field name for the active card using an access pattern that supports late resolution of the value for the active stylesheet. + +--Parameters-- +! Parameter Type Description +| @input@ [[type:string]] Field name, with or without underscores. + +--Examples-- + +>extra card field: +> ... +> name: my first extra field +> +>extra card field: +> ... +> name: my second extra field +> script: +>> space_to_comma(extra_data("my first extra field")) + "!" diff --git a/doc/function/index.txt b/doc/function/index.txt index 700fdf7d..254fd71d 100644 --- a/doc/function/index.txt +++ b/doc/function/index.txt @@ -65,7 +65,8 @@ These functions are built into the program, other [[type:function]]s can be defi | [[fun:english_plural|english_singular]] Find the singular of a word, @"cards" -> "card"@. | [[fun:process_english_hints]] Process the hints left by english_ functions in a keyword's reminder text. -! Fields and values <<< +! Fields and values <<< +| [[fun:extra_data]] Access the value of extra card fields. | [[fun:combined_editor|forward_editor]] Use one field to edit another. | [[fun:combined_editor]] Use one field to edit multiple others. | [[fun:primary_choice]] Return the top level choice chosen from a choice field.