Merge pull request #20 from haganbmj/extra_card_fields

feat: expose extra_card and extra_card_style script accessible variables
This commit is contained in:
Brendan Hagan
2022-07-15 08:32:07 -04:00
committed by GitHub
8 changed files with 28 additions and 60 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ Features:
* Center align the loaded image by default in the Image Slice Window. * Center align the loaded image by default in the Image Slice Window.
* Add Buttons to Align the loaded image in the Image Slice Window. (haganbmj/#18) * Add Buttons to Align the loaded image in the Image Slice Window. (haganbmj/#18)
* Add filter box to Game and Stylesheet selection. (haganbmj/#1) * Add filter box to Game and Stylesheet selection. (haganbmj/#1)
* Add `extra_card("field name")` script function for accessing Extra Card Fields. (haganbmj/#2) * Expose `extra_card` and `extra_card_style` script for accessing Extra Card Fields/Styles. (haganbmj/#20)
* Add Clear button to console panel. (haganbmj/#4) * Add Clear button to console panel. (haganbmj/#4)
* Store images internally with PNG extension for ease of manual tinkering. (haganbmj/#5) * Store images internally with PNG extension for ease of manual tinkering. (haganbmj/#5)
* Add ability to change scale (relative to template) of internally stored images. (haganbmj/#6, haganbmj/#15) * Add ability to change scale (relative to template) of internally stored images. (haganbmj/#6, haganbmj/#15)
-27
View File
@@ -1,27 +0,0 @@
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")) + "!"
-1
View File
@@ -66,7 +66,6 @@ These functions are built into the program, other [[type:function]]s can be defi
| [[fun:process_english_hints]] Process the hints left by english_ functions in a keyword's reminder text. | [[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|forward_editor]] Use one field to edit another.
| [[fun:combined_editor]] Use one field to edit multiple others. | [[fun:combined_editor]] Use one field to edit multiple others.
| [[fun:primary_choice]] Return the top level choice chosen from a choice field. | [[fun:primary_choice]] Return the top level choice chosen from a choice field.
+2
View File
@@ -10,6 +10,8 @@ Aside from the [[fun:index|built in functions]] the following variables are prov
| @card@ [[type:card]] not in @init script@s or when exporting | @card@ [[type:card]] not in @init script@s or when exporting
The current card. The current card.
| @card_style@ [[type:indexmap]] of [[type:style]]s where @card@ is available Style properties for the current card, the same as @stylesheet.card_style@. | @card_style@ [[type:indexmap]] of [[type:style]]s where @card@ is available Style properties for the current card, the same as @stylesheet.card_style@.
| @extra_card@ [[type:indexmap]] of [[type:value]]s field values for the current card as defined by the stylesheet.
| @extra_card_style@ [[type:indexmap]] of [[type:style]] where @card@ is available Style properties for the current card as added by the stylesheet.
| @styling@ [[type:indexmap]] of [[type:value]]s where @card@ is available Styling options for the stylesheet/card. | @styling@ [[type:indexmap]] of [[type:value]]s where @card@ is available Styling options for the stylesheet/card.
| @value@ [[type:value]] when evaluating a [[type:field]]'s @script@ or @default@ script Current value in the field. | @value@ [[type:value]] when evaluating a [[type:field]]'s @script@ or @default@ script Current value in the field.
| @options@ [[type:indexmap]] of [[type:value]]s when exporting Options of the [[type:export template]]. | @options@ [[type:indexmap]] of [[type:value]]s when exporting Options of the [[type:export template]].
-21
View File
@@ -384,26 +384,6 @@ SCRIPT_FUNCTION(count_chosen) {
} }
} }
// ----------------------------------------------------------------------------- : Extra Card Fields
SCRIPT_FUNCTION(extra_data) {
SCRIPT_PARAM_C(String, input);
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 == canonical_field_name) {
SCRIPT_RETURN(valueP);
}
}
return delay_error(ScriptErrorNoMember("extra_data()", input));
}
// ----------------------------------------------------------------------------- : Init // ----------------------------------------------------------------------------- : Init
void init_script_editor_functions(Context& ctx) { void init_script_editor_functions(Context& ctx) {
@@ -416,5 +396,4 @@ void init_script_editor_functions(Context& ctx) {
ctx.setVariable(_("exclusive_choice"), script_exclusive_choice); ctx.setVariable(_("exclusive_choice"), script_exclusive_choice);
ctx.setVariable(_("require_exclusive_choice"), script_require_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);
} }
+2
View File
@@ -74,6 +74,8 @@ void init_script_variables() {
Var(card_style); Var(card_style);
Var(card); Var(card);
Var(styling); Var(styling);
Var(extra_card_style);
Var(extra_card);
Var(value); Var(value);
Var(condition); Var(condition);
Var(language); Var(language);
+2
View File
@@ -142,6 +142,8 @@ enum Variable
, SCRIPT_VAR_card_style , SCRIPT_VAR_card_style
, SCRIPT_VAR_card , SCRIPT_VAR_card
, SCRIPT_VAR_styling , SCRIPT_VAR_styling
, SCRIPT_VAR_extra_card_style
, SCRIPT_VAR_extra_card
, SCRIPT_VAR_value , SCRIPT_VAR_value
, SCRIPT_VAR_condition , SCRIPT_VAR_condition
, SCRIPT_VAR_language , SCRIPT_VAR_language
+12 -1
View File
@@ -40,8 +40,15 @@ Context& SetScriptContext::getContext(const StyleSheetP& stylesheet) {
ctx.setVariable(SCRIPT_VAR_game, to_script(set.game)); ctx.setVariable(SCRIPT_VAR_game, to_script(set.game));
ctx.setVariable(SCRIPT_VAR_stylesheet, to_script(stylesheet)); ctx.setVariable(SCRIPT_VAR_stylesheet, to_script(stylesheet));
ctx.setVariable(SCRIPT_VAR_card_style, to_script(&stylesheet->card_style)); ctx.setVariable(SCRIPT_VAR_card_style, to_script(&stylesheet->card_style));
// I'm not entirely clear on why a "dummy value" is necessary here.
// It doesn't appear that these are getting accessed until a card is found anyways, so they don't trip any errors that I could see.
// Retaining the format just for consistency in case there's something that I missed.
ctx.setVariable(SCRIPT_VAR_card, set.cards.empty() ? script_nil : to_script(set.cards.front())); // dummy value ctx.setVariable(SCRIPT_VAR_card, set.cards.empty() ? script_nil : to_script(set.cards.front())); // dummy value
ctx.setVariable(SCRIPT_VAR_styling, to_script(&set.stylingDataFor(*stylesheet))); ctx.setVariable(SCRIPT_VAR_styling, to_script(&set.stylingDataFor(*stylesheet)));
ctx.setVariable(SCRIPT_VAR_extra_card_style, to_script(&stylesheet->extra_card_style)); // dummy value
ctx.setVariable(SCRIPT_VAR_extra_card, set.cards.empty() ? script_nil : to_script(&set.cards.front()->extraDataFor(*stylesheet))); // dummy value
try { try {
// perform init scripts, don't use a scope, variables stay bound in the context // perform init scripts, don't use a scope, variables stay bound in the context
try { try {
@@ -67,9 +74,13 @@ Context& SetScriptContext::getContext(const CardP& card) {
if (card) { if (card) {
ctx.setVariable(SCRIPT_VAR_card, to_script(card)); ctx.setVariable(SCRIPT_VAR_card, to_script(card));
ctx.setVariable(SCRIPT_VAR_styling, to_script(&set.stylingDataFor(card))); ctx.setVariable(SCRIPT_VAR_styling, to_script(&set.stylingDataFor(card)));
ctx.setVariable(SCRIPT_VAR_extra_card_style, to_script(&stylesheet->extra_card_style));
ctx.setVariable(SCRIPT_VAR_extra_card, to_script(&card->extraDataFor(*stylesheet)));
} else { } else {
ctx.setVariable(SCRIPT_VAR_card, ScriptValueP()); ctx.setVariable(SCRIPT_VAR_card, ScriptValueP());
ctx.setVariable(SCRIPT_VAR_styling, to_script(&set.stylingDataFor(*stylesheet))); ctx.setVariable(SCRIPT_VAR_styling, to_script(&set.stylingDataFor(*stylesheet)));
ctx.setVariable(SCRIPT_VAR_extra_card_style, script_nil);
ctx.setVariable(SCRIPT_VAR_extra_card, script_nil);
} }
return ctx; return ctx;
} }