Commit 4e21ca7c authored by twanvl's avatar twanvl

Split script/functions.cpp into multiple files in new script/functions/ directory.

parent 195956e7
...@@ -89,13 +89,17 @@ magicseteditor_SOURCES += ./src/gui/drop_down_list.cpp ...@@ -89,13 +89,17 @@ magicseteditor_SOURCES += ./src/gui/drop_down_list.cpp
magicseteditor_SOURCES += ./src/gui/image_slice_window.cpp magicseteditor_SOURCES += ./src/gui/image_slice_window.cpp
magicseteditor_SOURCES += ./src/script/script_manager.cpp magicseteditor_SOURCES += ./src/script/script_manager.cpp
magicseteditor_SOURCES += ./src/script/script.cpp magicseteditor_SOURCES += ./src/script/script.cpp
magicseteditor_SOURCES += ./src/script/functions.cpp
magicseteditor_SOURCES += ./src/script/value.cpp magicseteditor_SOURCES += ./src/script/value.cpp
magicseteditor_SOURCES += ./src/script/dependency.cpp magicseteditor_SOURCES += ./src/script/dependency.cpp
magicseteditor_SOURCES += ./src/script/image.cpp magicseteditor_SOURCES += ./src/script/image.cpp
magicseteditor_SOURCES += ./src/script/context.cpp magicseteditor_SOURCES += ./src/script/context.cpp
magicseteditor_SOURCES += ./src/script/scriptable.cpp magicseteditor_SOURCES += ./src/script/scriptable.cpp
magicseteditor_SOURCES += ./src/script/parser.cpp magicseteditor_SOURCES += ./src/script/parser.cpp
magicseteditor_SOURCES += ./src/script/functions/basic.cpp
magicseteditor_SOURCES += ./src/script/functions/image.cpp
magicseteditor_SOURCES += ./src/script/functions/editor.cpp
magicseteditor_SOURCES += ./src/script/functions/export.cpp
magicseteditor_SOURCES += ./src/script/functions/english.cpp
magicseteditor_SOURCES += ./src/data/field/color.cpp magicseteditor_SOURCES += ./src/data/field/color.cpp
magicseteditor_SOURCES += ./src/data/field/boolean.cpp magicseteditor_SOURCES += ./src/data/field/boolean.cpp
magicseteditor_SOURCES += ./src/data/field/image.cpp magicseteditor_SOURCES += ./src/data/field/image.cpp
......
...@@ -92,7 +92,7 @@ void export_image(const SetP& set, const CardP& card, const String& filename); ...@@ -92,7 +92,7 @@ void export_image(const SetP& set, const CardP& card, const String& filename);
Bitmap export_bitmap(const SetP& set, const CardP& card); Bitmap export_bitmap(const SetP& set, const CardP& card);
/// Export a set to Magic Workstation format /// Export a set to Magic Workstation format
void export_mws(const SetP& set); void export_mws(Window* parent, const SetP& set);
// ----------------------------------------------------------------------------- : EOF // ----------------------------------------------------------------------------- : EOF
#endif #endif
...@@ -52,7 +52,7 @@ String card_rarity_code(const String& rarity) { ...@@ -52,7 +52,7 @@ String card_rarity_code(const String& rarity) {
// ----------------------------------------------------------------------------- : export_mws // ----------------------------------------------------------------------------- : export_mws
void export_mws(const SetP& set) { void export_mws(Window* parent, const SetP& set) {
if (!set->game->isMagic()) { if (!set->game->isMagic()) {
throw Error(_("Can only export Magic sets to Magic Workstation")); throw Error(_("Can only export Magic sets to Magic Workstation"));
} }
...@@ -60,7 +60,7 @@ void export_mws(const SetP& set) { ...@@ -60,7 +60,7 @@ void export_mws(const SetP& set) {
// Select filename // Select filename
String name = wxFileSelector(_("Export to file"),_(""),_(""),_(""), String name = wxFileSelector(_("Export to file"),_(""),_(""),_(""),
_("Text files (*.txt)|*.txt|All Files|*.*"), _("Text files (*.txt)|*.txt|All Files|*.*"),
wxSAVE | wxOVERWRITE_PROMPT); wxSAVE | wxOVERWRITE_PROMPT, parent);
if (name.empty()) return; if (name.empty()) return;
wxBusyCursor busy; wxBusyCursor busy;
// Open file // Open file
......
...@@ -389,17 +389,17 @@ String KeywordDatabase::expand(const String& text, ...@@ -389,17 +389,17 @@ String KeywordDatabase::expand(const String& text,
part = part + param; // keep tags part = part + param; // keep tags
} else if (kw->parameters[j/2-1]->script) { } else if (kw->parameters[j/2-1]->script) {
// apply parameter script // apply parameter script
ctx.setVariable(_("input"), toScript(part)); ctx.setVariable(_("input"), to_script(part));
part = kw->parameters[j/2-1]->script.invoke(ctx)->toString(); part = kw->parameters[j/2-1]->script.invoke(ctx)->toString();
ctx.setVariable(_("input"), toScript(part)); ctx.setVariable(_("input"), to_script(part));
param = kw->parameters[j/2-1]->script.invoke(ctx)->toString(); param = kw->parameters[j/2-1]->script.invoke(ctx)->toString();
} }
ctx.setVariable(String(_("param")) << (int)(j/2), toScript(param)); ctx.setVariable(String(_("param")) << (int)(j/2), to_script(param));
} }
total += part; total += part;
start = part_end; start = part_end;
} }
ctx.setVariable(_("mode"), toScript(kw->mode)); ctx.setVariable(_("mode"), to_script(kw->mode));
// Show reminder text? // Show reminder text?
bool expand = expand_type == _('1'); bool expand = expand_type == _('1');
...@@ -412,8 +412,8 @@ String KeywordDatabase::expand(const String& text, ...@@ -412,8 +412,8 @@ String KeywordDatabase::expand(const String& text,
// Combine keyword & reminder with result // Combine keyword & reminder with result
if (expand) { if (expand) {
String reminder = kw->reminder.invoke(ctx)->toString(); String reminder = kw->reminder.invoke(ctx)->toString();
ctx.setVariable(_("keyword"), toScript(total)); ctx.setVariable(_("keyword"), to_script(total));
ctx.setVariable(_("reminder"), toScript(reminder)); ctx.setVariable(_("reminder"), to_script(reminder));
result += _("<kw-"); result += expand_type; result += _(">"); result += _("<kw-"); result += expand_type; result += _(">");
result += combine_script->eval(ctx)->toString(); result += combine_script->eval(ctx)->toString();
result += _("</kw-"); result += expand_type; result += _(">"); result += _("</kw-"); result += expand_type; result += _(">");
......
...@@ -33,7 +33,7 @@ ColumnSettings::ColumnSettings() ...@@ -33,7 +33,7 @@ ColumnSettings::ColumnSettings()
{} {}
// dummy for ColumnSettings reflection // dummy for ColumnSettings reflection
ScriptValueP toScript(const ColumnSettings&) { return script_nil; } ScriptValueP to_script(const ColumnSettings&) { return script_nil; }
IMPLEMENT_REFLECTION(ColumnSettings) { IMPLEMENT_REFLECTION(ColumnSettings) {
REFLECT(width); REFLECT(width);
......
...@@ -457,7 +457,7 @@ void SetWindow::onFileExportApprentice(wxCommandEvent&) { ...@@ -457,7 +457,7 @@ void SetWindow::onFileExportApprentice(wxCommandEvent&) {
} }
void SetWindow::onFileExportMWS(wxCommandEvent&) { void SetWindow::onFileExportMWS(wxCommandEvent&) {
export_mws(set); export_mws(this, set);
} }
void SetWindow::onFilePrint(wxCommandEvent&) { void SetWindow::onFilePrint(wxCommandEvent&) {
......
...@@ -2120,9 +2120,6 @@ ...@@ -2120,9 +2120,6 @@
<File <File
RelativePath=".\script\dependency.hpp"> RelativePath=".\script\dependency.hpp">
</File> </File>
<File
RelativePath=".\script\functions.cpp">
</File>
<File <File
RelativePath=".\script\image.cpp"> RelativePath=".\script\image.cpp">
<FileConfiguration <FileConfiguration
...@@ -2198,6 +2195,103 @@ ...@@ -2198,6 +2195,103 @@
<File <File
RelativePath=".\script\value.hpp"> RelativePath=".\script\value.hpp">
</File> </File>
<Filter
Name="functions"
Filter="">
<File
RelativePath=".\script\functions\basic.cpp">
</File>
<File
RelativePath=".\script\functions\editor.cpp">
<FileConfiguration
Name="Debug|Win32">
<Tool
Name="VCCLCompilerTool"
ObjectFile="$(IntDir)/$(InputName)2.obj"/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32">
<Tool
Name="VCCLCompilerTool"
ObjectFile="$(IntDir)/$(InputName)2.obj"/>
</FileConfiguration>
<FileConfiguration
Name="Debug Unicode|Win32">
<Tool
Name="VCCLCompilerTool"
ObjectFile="$(IntDir)/$(InputName)2.obj"/>
</FileConfiguration>
<FileConfiguration
Name="Release Unicode|Win32">
<Tool
Name="VCCLCompilerTool"
ObjectFile="$(IntDir)/$(InputName)2.obj"/>
</FileConfiguration>
<FileConfiguration
Name="Release Profile Unicode|Win32">
<Tool
Name="VCCLCompilerTool"
ObjectFile="$(IntDir)/$(InputName)2.obj"/>
</FileConfiguration>
<FileConfiguration
Name="Release Unicode fast build|Win32">
<Tool
Name="VCCLCompilerTool"
ObjectFile="$(IntDir)/$(InputName)2.obj"/>
</FileConfiguration>
</File>
<File
RelativePath=".\script\functions\english.cpp">
</File>
<File
RelativePath=".\script\functions\export.cpp">
</File>
<File
RelativePath=".\script\functions\functions.hpp">
</File>
<File
RelativePath=".\script\functions\image.cpp">
<FileConfiguration
Name="Debug|Win32">
<Tool
Name="VCCLCompilerTool"
ObjectFile="$(IntDir)/$(InputName)5.obj"/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32">
<Tool
Name="VCCLCompilerTool"
ObjectFile="$(IntDir)/$(InputName)5.obj"/>
</FileConfiguration>
<FileConfiguration
Name="Debug Unicode|Win32">
<Tool
Name="VCCLCompilerTool"
ObjectFile="$(IntDir)/$(InputName)5.obj"/>
</FileConfiguration>
<FileConfiguration
Name="Release Unicode|Win32">
<Tool
Name="VCCLCompilerTool"
ObjectFile="$(IntDir)/$(InputName)5.obj"/>
</FileConfiguration>
<FileConfiguration
Name="Release Profile Unicode|Win32">
<Tool
Name="VCCLCompilerTool"
ObjectFile="$(IntDir)/$(InputName)5.obj"/>
</FileConfiguration>
<FileConfiguration
Name="Release Unicode fast build|Win32">
<Tool
Name="VCCLCompilerTool"
ObjectFile="$(IntDir)/$(InputName)5.obj"/>
</FileConfiguration>
</File>
<File
RelativePath=".\script\functions\util.hpp">
</File>
</Filter>
</Filter> </Filter>
<Filter <Filter
Name="render" Name="render"
......
...@@ -212,10 +212,10 @@ void instrUnary (UnaryInstructionType i, ScriptValueP& a) { ...@@ -212,10 +212,10 @@ void instrUnary (UnaryInstructionType i, ScriptValueP& a) {
a = a->makeIterator(); a = a->makeIterator();
break; break;
case I_NEGATE: case I_NEGATE:
a = toScript(-(int)*a); a = to_script(-(int)*a);
break; break;
case I_NOT: case I_NOT:
a = toScript(!(bool)*a); a = to_script(!(bool)*a);
break; break;
} }
} }
...@@ -224,26 +224,26 @@ void instrUnary (UnaryInstructionType i, ScriptValueP& a) { ...@@ -224,26 +224,26 @@ void instrUnary (UnaryInstructionType i, ScriptValueP& a) {
// operator on ints // operator on ints
#define OPERATOR_I(OP) \ #define OPERATOR_I(OP) \
a = toScript((int)*a OP (int)*b); \ a = to_script((int)*a OP (int)*b); \
break break
// operator on doubles or ints // operator on doubles or ints
#define OPERATOR_DI(OP) \ #define OPERATOR_DI(OP) \
if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) { \ if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) { \
a = toScript((double)*a OP (double)*b); \ a = to_script((double)*a OP (double)*b); \
} else { \ } else { \
a = toScript((int)*a OP (int)*b); \ a = to_script((int)*a OP (int)*b); \
} \ } \
break break
// operator on strings or doubles or ints // operator on strings or doubles or ints
#define OPERATOR_SDI(OP) \ #define OPERATOR_SDI(OP) \
if (at == SCRIPT_STRING || bt == SCRIPT_STRING) { \ if (at == SCRIPT_STRING || bt == SCRIPT_STRING) { \
a = toScript(a->toString() OP b->toString()); \ a = to_script(a->toString() OP b->toString()); \
} else if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) { \ } else if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) { \
a = toScript((double)*a OP (double)*b); \ a = to_script((double)*a OP (double)*b); \
} else { \ } else { \
a = toScript((int)*a OP (int)*b); \ a = to_script((int)*a OP (int)*b); \
} \ } \
break break
...@@ -283,13 +283,13 @@ void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP& ...@@ -283,13 +283,13 @@ void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP&
} else if (at == SCRIPT_FUNCTION && bt == SCRIPT_FUNCTION) { } else if (at == SCRIPT_FUNCTION && bt == SCRIPT_FUNCTION) {
a = new_intrusive2<ScriptCompose>(a, b); a = new_intrusive2<ScriptCompose>(a, b);
} else if (at == SCRIPT_STRING || bt == SCRIPT_STRING) { } else if (at == SCRIPT_STRING || bt == SCRIPT_STRING) {
a = toScript(a->toString() + b->toString()); a = to_script(a->toString() + b->toString());
} else if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) { } else if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) {
a = toScript((double)*a + (double)*b); a = to_script((double)*a + (double)*b);
} else if (at == SCRIPT_INT || bt == SCRIPT_INT) { } else if (at == SCRIPT_INT || bt == SCRIPT_INT) {
a = toScript((int)*a + (int)*b); a = to_script((int)*a + (int)*b);
} else { } else {
a = toScript(a->toString() + b->toString()); a = to_script(a->toString() + b->toString());
} }
break; break;
case I_SUB: OPERATOR_DI(-); case I_SUB: OPERATOR_DI(-);
...@@ -297,9 +297,9 @@ void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP& ...@@ -297,9 +297,9 @@ void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP&
case I_DIV: OPERATOR_DI(/); case I_DIV: OPERATOR_DI(/);
case I_MOD: case I_MOD:
if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) { if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) {
a = toScript(fmod((double)*a, (double)*b)); a = to_script(fmod((double)*a, (double)*b));
} else { } else {
a = toScript((int)*a % (int)*b); a = to_script((int)*a % (int)*b);
} }
break; break;
case I_AND: OPERATOR_I(&&); case I_AND: OPERATOR_I(&&);
...@@ -318,7 +318,7 @@ void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP& ...@@ -318,7 +318,7 @@ void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP&
void instrTernary(TernaryInstructionType i, ScriptValueP& a, const ScriptValueP& b, const ScriptValueP& c) { void instrTernary(TernaryInstructionType i, ScriptValueP& a, const ScriptValueP& b, const ScriptValueP& c) {
switch (i) { switch (i) {
case I_RGB: case I_RGB:
a = toScript(Color((int)*a, (int)*b, (int)*c)); a = to_script(Color((int)*a, (int)*b, (int)*c));
break; break;
} }
} }
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <script/functions/functions.hpp>
#include <script/functions/util.hpp>
#include <util/tagged_string.hpp>
#include <data/set.hpp>
#include <data/game.hpp>
#include <data/field/text.hpp>
DECLARE_TYPEOF_COLLECTION(FieldP);
DECLARE_TYPEOF_COLLECTION(TextValue*);
DECLARE_TYPEOF_COLLECTION(String);
// ----------------------------------------------------------------------------- : Combined editor
// Combining multiple (text) values into a single one
// The combined value is value1 <sep>something</sep> value2 <sep>something</sep> value3
//
SCRIPT_FUNCTION_WITH_DEP(combined_editor) {
// read 'field#' arguments
vector<TextValue*> values;
for (int i = 0 ; ; ++i) {
String name = _("field"); if (i > 0) name = name << i;
SCRIPT_OPTIONAL_PARAM_N(ValueP, name, value) {
TextValue* text_value = dynamic_cast<TextValue*>(value.get());
if (!text_value) throw ScriptError(_("Argument '")+name+_("' should be a text field"));
values.push_back(text_value);
} else if (i > 0) break;
}
if (values.empty()) {
throw ScriptError(_("No fields specified for combined_editor"));
}
// read 'separator#' arguments
vector<String> separators;
for (int i = 0 ; ; ++i) {
String name = _("separator"); if (i > 0) name = name << i;
SCRIPT_OPTIONAL_PARAM_N(String, name, separator) {
separators.push_back(separator);
} else if (i > 0) break;
}
if (separators.size() < values.size() - 1) {
throw ScriptError(String::Format(_("Not enough separators for combine_editor, expected %d"), values.size()-1));
}
// split the value
SCRIPT_PARAM(String, value);
vector<String> value_parts;
size_t pos = value.find(_("<sep"));
while (pos != String::npos) {
value_parts.push_back(value.substr(0, pos));
value = value.substr(min(match_close_tag_end(value,pos), value.size()));
pos = value.find(_("<sep"));
}
value_parts.push_back(value);
value_parts.resize(values.size()); // TODO: what if there are more value_parts than values?
// update the values if our input value is newer?
Age new_value_update = last_update_age();
FOR_EACH_2(v, values, nv, value_parts) {
if (v->value() != nv && v->last_update < new_value_update) {
// TODO : type over
v->value.assign(nv);
v->update(ctx);
}
nv = v->value();
}
// options
SCRIPT_PARAM_DEFAULT_N(bool, _("hide when empty"), hide_when_empty, false);
SCRIPT_PARAM_DEFAULT_N(bool, _("soft before empty"), soft_before_empty, false);
// recombine the parts
String new_value = value_parts.front();
for (size_t i = 1 ; i < value_parts.size() ; ++i) {
if (value_parts[i].empty() && new_value.empty() && hide_when_empty) {
// no separator
} else if (value_parts[i].empty() && soft_before_empty) {
// soft separator
new_value += _("<sep-soft>") + separators[i - 1] + _("</sep-soft>");
} else {
// normal separator
new_value += _("<sep>") + separators[i - 1] + _("</sep>");
new_value += value_parts[i];
}
}
SCRIPT_RETURN(new_value);
}
SCRIPT_FUNCTION_DEPENDENCIES(combined_editor) {
// read 'field#' arguments
vector<FieldP> fields;
for (int i = 0 ; ; ++i) {
String name = _("field"); if (i > 0) name = name << i;
SCRIPT_OPTIONAL_PARAM_N(ValueP, name, value) {
fields.push_back(value->fieldP);
} else if (i > 0) break;
}
// Find the target field
SCRIPT_PARAM(Set*, set);
GameP game = set->game;
FieldP target_field;
if (dep.type == DEP_CARD_FIELD) target_field = game->card_fields[dep.index];
else if (dep.type == DEP_SET_FIELD) target_field = game->set_fields[dep.index];
else throw InternalError(_("Finding dependencies of combined error for non card/set field"));
// Add dependencies, from target_field on field#
// For card fields
size_t j = 0;
FOR_EACH(f, game->card_fields) {
Dependency dep(DEP_CARD_COPY_DEP, j++);
FOR_EACH(fn, fields) {
if (f == fn) {
target_field->dependent_scripts.add(dep);
break;
}
}
}
// For set fields
j = 0;
FOR_EACH(f, game->set_fields) {
Dependency dep(DEP_SET_COPY_DEP, j++);
FOR_EACH(fn, fields) {
if (f == fn) {
target_field->dependent_scripts.add(dep);
break;
}
}
}
return dependency_dummy;
}
// ----------------------------------------------------------------------------- : Init
void init_script_editor_functions(Context& ctx) {
ctx.setVariable(_("forward editor"), script_combined_editor); // combatability
ctx.setVariable(_("combined editor"), script_combined_editor);
}
\ No newline at end of file
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <script/functions/functions.hpp>
// ----------------------------------------------------------------------------- : Numbers
// ----------------------------------------------------------------------------- : Init
void init_script_english_functions(Context& ctx) {
}
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <script/functions/functions.hpp>
// ----------------------------------------------------------------------------- :
// ----------------------------------------------------------------------------- : Init
void init_script_export_functions(Context& ctx) {
}
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
#ifndef HEADER_SCRIPT_FUNCTIONS_FUNCTIONS
#define HEADER_SCRIPT_FUNCTIONS_FUNCTIONS
/** @file script/functions/functions.cpp
*
* @brief Header for buildin script functions.
*/
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
class Context;
// ----------------------------------------------------------------------------- : Script functions
void init_script_basic_functions(Context& ctx);
void init_script_image_functions(Context& ctx);
void init_script_editor_functions(Context& ctx);
void init_script_export_functions(Context& ctx);
void init_script_english_functions(Context& ctx);
/// Initialize all build in functions for a context
inline void init_script_functions(Context& ctx) {
init_script_basic_functions(ctx);
init_script_image_functions(ctx);
init_script_editor_functions(ctx);
init_script_export_functions(ctx);
init_script_english_functions(ctx);
}
// ----------------------------------------------------------------------------- : EOF
#endif
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <script/functions/functions.hpp>
#include <script/functions/util.hpp>
#include <script/image.hpp>
// used by the functions
#include <data/set.hpp>
#include <data/stylesheet.hpp>
#include <data/symbol.hpp>
#include <data/field/symbol.hpp>
#include <render/symbol/filter.hpp>
#include <gui/util.hpp> // load_resource_image
DECLARE_TYPEOF_COLLECTION(SymbolStyle::VariationP);
// ----------------------------------------------------------------------------- : Macros
#define SCRIPT_IMAGE_FUNCTION(name) \
SCRIPT_FUNCTION(name) { \
if (last_update_age() == 0)
#define SCRIPT_IMAGE_FUNCTION_UP_TO_DATE }
template <> inline ScriptImageP from_script<ScriptImageP>(const ScriptValueP& value) {
return to_script_image(value);
}
#define SCRIPT_IMAGE_PARAM_UP_TO_DATE(name) script_image_up_to_date(ctx.getVariable(_(#name)))
// ----------------------------------------------------------------------------- : Image functions
SCRIPT_IMAGE_FUNCTION(linear_blend) {
SCRIPT_PARAM(ScriptImageP, image1);
SCRIPT_PARAM(ScriptImageP, image2);
SCRIPT_PARAM(double, x1); SCRIPT_PARAM(double, y1);
SCRIPT_PARAM(double, x2); SCRIPT_PARAM(double, y2);
linear_blend(image1->image, image2->image, x1, y1, x2, y2);
return image1;
SCRIPT_IMAGE_FUNCTION_UP_TO_DATE
SCRIPT_RETURN(
SCRIPT_IMAGE_PARAM_UP_TO_DATE(image1) &&
SCRIPT_IMAGE_PARAM_UP_TO_DATE(image2)
);
}
SCRIPT_IMAGE_FUNCTION(masked_blend) {
SCRIPT_PARAM(ScriptImageP, light);
SCRIPT_PARAM(ScriptImageP, dark);
SCRIPT_PARAM(ScriptImageP, mask);
mask_blend(light->image, dark->image, mask->image);
return light;
SCRIPT_IMAGE_FUNCTION_UP_TO_DATE
SCRIPT_RETURN(
SCRIPT_IMAGE_PARAM_UP_TO_DATE(light) &&
SCRIPT_IMAGE_PARAM_UP_TO_DATE(dark) &&
SCRIPT_IMAGE_PARAM_UP_TO_DATE(mask)
);
}
SCRIPT_IMAGE_FUNCTION(set_mask) {
SCRIPT_PARAM(ScriptImageP, image);
SCRIPT_PARAM(ScriptImageP, mask);
set_alpha(image->image, mask->image);
return image;
SCRIPT_IMAGE_FUNCTION_UP_TO_DATE
SCRIPT_RETURN(
SCRIPT_IMAGE_PARAM_UP_TO_DATE(image) &&
SCRIPT_IMAGE_PARAM_UP_TO_DATE(mask)
);
}
bool parse_enum(const String&, ImageCombine& out);
SCRIPT_IMAGE_FUNCTION(set_combine) {
SCRIPT_PARAM(String, combine);
SCRIPT_PARAM(ScriptImageP, input);
// parse and set combine
if (!parse_enum(combine, input->combine)) {
throw ScriptError(_("Not a valid combine mode: '") + combine + _("'"));
}
return input;
SCRIPT_IMAGE_FUNCTION_UP_TO_DATE
SCRIPT_RETURN(
SCRIPT_IMAGE_PARAM_UP_TO_DATE(input)
);
}
SCRIPT_IMAGE_FUNCTION(symbol_variation) {
SCRIPT_PARAM(ValueP, symbol);
SymbolValueP value = dynamic_pointer_cast<SymbolValue>(symbol);
SCRIPT_PARAM(String, variation);
// find set & style
SCRIPT_PARAM(Set*, set);
SCRIPT_OPTIONAL_PARAM_(CardP, card);
SymbolStyleP style = dynamic_pointer_cast<SymbolStyle>(set->stylesheetFor(card)->styleFor(value->fieldP));
if (!style) throw InternalError(_("Symbol value has a style of the wrong type"));
// load symbol
SymbolP the_symbol;
if (value->filename.empty()) {
the_symbol = default_symbol();
} else {
the_symbol = set->readFile<SymbolP>(value->filename);
}
// determine filter & render
FOR_EACH(v, style->variations) {
if (v->name == variation) {
// render & filter
return new_intrusive1<ScriptImage>(render_symbol(the_symbol, *v->filter, v->border_radius));
}
}
throw ScriptError(_("Variation of symbol not found ('") + variation + _("')"));
SCRIPT_IMAGE_FUNCTION_UP_TO_DATE
// SCRIPT_RETURN(last_update_age() >= value->filename.last_update_age);
SCRIPT_RETURN(last_update_age() > 1); // the symbol was created/loaded after program start,
// don't use cached images
}
SCRIPT_IMAGE_FUNCTION(buildin_image) {
SCRIPT_PARAM(String, input);
Image img = load_resource_image(input);
if (!img.Ok()) {
throw ScriptError(_("There is no build in image '") + input + _("'"));
}
return new_intrusive1<ScriptImage>(img);
SCRIPT_IMAGE_FUNCTION_UP_TO_DATE
SCRIPT_RETURN(true); // always up to date
}
// ----------------------------------------------------------------------------- : Init
void init_script_image_functions(Context& ctx) {
ctx.setVariable(_("linear blend"), script_linear_blend);
ctx.setVariable(_("masked blend"), script_masked_blend);
ctx.setVariable(_("set mask"), script_set_mask);
ctx.setVariable(_("set combine"), script_set_combine);
ctx.setVariable(_("symbol variation"), script_symbol_variation);
ctx.setVariable(_("buildin image"), script_buildin_image);
}
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
#ifndef HEADER_SCRIPT_FUNCTIONS_UTIL
#define HEADER_SCRIPT_FUNCTIONS_UTIL
/** @file script/functions/util.cpp
*
* @brief Utility macros for defining script functions.
*/
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <util/error.hpp>
#include <script/to_value.hpp>
// ----------------------------------------------------------------------------- : Functions
/// Macro to declare a new script function
/** Usage:
* @code
* SCRIPT_FUNCTION(my_function) {
* // function code goes here
* }
* @endcode
* This declares a value 'script_my_function' which can be added as a variable to a context
* using:
* @code
* extern ScriptValueP script_my_function;
* context.setVariable("my_function", script_my_function);
* @endcode
*/
#define SCRIPT_FUNCTION(name) SCRIPT_FUNCTION_AUX(name,;)
/// Macro to declare a new script function with custom dependency handling
#define SCRIPT_FUNCTION_WITH_DEP(name) \
SCRIPT_FUNCTION_AUX(name, virtual ScriptValueP dependencies(Context&, const Dependency&) const;)
#define SCRIPT_FUNCTION_DEPENDENCIES(name) \
ScriptValueP ScriptBuildin_##name::dependencies(Context& ctx, const Dependency& dep) const
// helper for SCRIPT_FUNCTION and SCRIPT_FUNCTION_DEP
#define SCRIPT_FUNCTION_AUX(name,dep) \
class ScriptBuildin_##name : public ScriptValue { \
dep \
virtual ScriptType type() const \
{ return SCRIPT_FUNCTION; } \
virtual String typeName() const \
{ return _("build in function '") _(#name) _("'"); } \
virtual ScriptValueP eval(Context&) const; \
}; \
ScriptValueP script_##name(new ScriptBuildin_##name); \
ScriptValueP ScriptBuildin_##name::eval(Context& ctx) const
/// Return a value from a SCRIPT_FUNCTION
#define SCRIPT_RETURN(value) return to_script(value)
// ----------------------------------------------------------------------------- : Parameters
/// Retrieve a parameter to a SCRIPT_FUNCTION with the given name and type
/** Usage:
* @code
* SCRIPT_FUNCTION(my_function) {
* SCRIPT_PARAM(String, my_string_param);
* ... my_string_param ...
* }
* @endcode
* Throws an error if the parameter is not found.
*/
#define SCRIPT_PARAM(Type, name) \
SCRIPT_PARAM_N(Type, _(#name), name)
#define SCRIPT_PARAM_N(Type, str, name) \
Type name = from_script<Type>(ctx.getVariable(str))
/// Retrieve an optional parameter
/** Usage:
* @code
* SCRIPT_FUNCTION(my_function) {
* SCRIPT_OPTIONAL_PARAM(String, my_string_param) {
* ... my_string_param ...
* }
* ...
* }
* @endcode
*/
#define SCRIPT_OPTIONAL_PARAM(Type, name) \
SCRIPT_OPTIONAL_PARAM_N(Type, _(#name), name)
/// Retrieve a named optional parameter
#define SCRIPT_OPTIONAL_PARAM_N(Type, str, name) \
ScriptValueP name##_ = ctx.getVariableOpt(str); \
Type name = name##_ ? from_script<Type>(name##_) : Type(); \
if (name##_)
/// Retrieve an optional parameter, can't be used as an if statement
#define SCRIPT_OPTIONAL_PARAM_(Type, name) \
SCRIPT_OPTIONAL_PARAM_N_(Type, _(#name), name)
/// Retrieve a named optional parameter, can't be used as an if statement
#define SCRIPT_OPTIONAL_PARAM_N_(Type, str, name) \
ScriptValueP name##_ = ctx.getVariableOpt(str); \
Type name = name##_ ? from_script<Type>(name##_) : Type();
/// Retrieve an optional parameter with a default value
#define SCRIPT_PARAM_DEFAULT(Type, name, def) \
SCRIPT_PARAM_DEFAULT_N(Type, _(#name), name, def)
/// Retrieve a named optional parameter with a default value
#define SCRIPT_PARAM_DEFAULT_N(Type, str, name, def) \
ScriptValueP name##_ = ctx.getVariableOpt(str); \
Type name = name##_ ? from_script<Type>(name##_) : def
// ----------------------------------------------------------------------------- : Rules
/// Utility for defining a script rule with a single parameter
#define SCRIPT_RULE_1(funname, type1, name1) \
SCRIPT_RULE_1_N(funname, type1, _(#name1), name1)
/// Utility for defining a script rule with a single named parameter
#define SCRIPT_RULE_1_N(funname, type1, str1, name1) \
class ScriptRule_##funname: public ScriptValue { \
public: \
inline ScriptRule_##funname(const type1& name1) : name1(name1) {} \
virtual ScriptType type() const { return SCRIPT_FUNCTION; } \
virtual String typeName() const { return _(#funname)_("_rule"); } \
virtual ScriptValueP eval(Context& ctx) const; \
private: \
type1 name1; \
}; \
SCRIPT_FUNCTION(funname##_rule) { \
SCRIPT_PARAM_N(type1, str1, name1); \
return new_intrusive1<ScriptRule_##funname>(name1); \
} \
SCRIPT_FUNCTION(funname) { \
SCRIPT_PARAM_N(type1, str1, name1); \
return ScriptRule_##funname(name1).eval(ctx); \
} \
ScriptValueP ScriptRule_##funname::eval(Context& ctx) const
/// Utility for defining a script rule with two parameters
#define SCRIPT_RULE_2(funname, type1, name1, type2, name2) \
SCRIPT_RULE_2_N(funname, type1, _(#name1), name1, type2, _(#name2), name2)
/// Utility for defining a script rule with two named parameters
#define SCRIPT_RULE_2_N(funname, type1, str1, name1, type2, str2, name2) \
class ScriptRule_##funname: public ScriptValue { \
public: \
inline ScriptRule_##funname(const type1& name1, const type2& name2) \
: name1(name1), name2(name2) {} \
virtual ScriptType type() const { return SCRIPT_FUNCTION; } \
virtual String typeName() const { return _(#funname)_("_rule"); } \
virtual ScriptValueP eval(Context& ctx) const; \
private: \
type1 name1; \
type2 name2; \
}; \
SCRIPT_FUNCTION(funname##_rule) { \
SCRIPT_PARAM_N(type1, str1, name1); \
SCRIPT_PARAM_N(type2, str2, name2); \
return new_intrusive2<ScriptRule_##funname>(name1, name2); \
} \
SCRIPT_FUNCTION(funname) { \
SCRIPT_PARAM_N(type1, str1, name1); \
SCRIPT_PARAM_N(type2, str2, name2); \
return ScriptRule_##funname(name1, name2).eval(ctx); \
} \
ScriptValueP ScriptRule_##funname::eval(Context& ctx) const
// ----------------------------------------------------------------------------- : EOF
#endif
...@@ -11,15 +11,6 @@ ...@@ -11,15 +11,6 @@
#include <script/to_value.hpp> #include <script/to_value.hpp>
#include <util/dynamic_arg.hpp> #include <util/dynamic_arg.hpp>
#include <util/io/package.hpp> #include <util/io/package.hpp>
// for functions:
#include <data/set.hpp>
#include <data/stylesheet.hpp>
#include <data/symbol.hpp>
#include <data/field/symbol.hpp>
#include <render/symbol/filter.hpp>
#include <gui/util.hpp> // load_resource_image
DECLARE_TYPEOF_COLLECTION(SymbolStyle::VariationP);
IMPLEMENT_DYNAMIC_ARG(Package*, load_images_from, nullptr); IMPLEMENT_DYNAMIC_ARG(Package*, load_images_from, nullptr);
...@@ -169,123 +160,3 @@ template <> void Writer::handle(const ScriptableImage& s) { ...@@ -169,123 +160,3 @@ template <> void Writer::handle(const ScriptableImage& s) {
template <> void GetDefaultMember::handle(const ScriptableImage& s) { template <> void GetDefaultMember::handle(const ScriptableImage& s) {
handle(s.script.unparsed); handle(s.script.unparsed);
} }
// ----------------------------------------------------------------------------- : Functions
SCRIPT_FUNCTION(linear_blend) {
if (last_update_age() == 0) {
ScriptImageP image1 = to_script_image(ctx.getVariable(_("image1")));
ScriptImageP image2 = to_script_image(ctx.getVariable(_("image2")));
SCRIPT_PARAM(double, x1); SCRIPT_PARAM(double, y1);
SCRIPT_PARAM(double, x2); SCRIPT_PARAM(double, y2);
linear_blend(image1->image, image2->image, x1, y1, x2, y2);
return image1;
} else {
SCRIPT_RETURN(
script_image_up_to_date(ctx.getVariable(_("image1"))) &&
script_image_up_to_date(ctx.getVariable(_("image2")))
);
}
}
SCRIPT_FUNCTION(masked_blend) {
if (last_update_age() == 0) {
ScriptImageP light = to_script_image(ctx.getVariable(_("light")));
ScriptImageP dark = to_script_image(ctx.getVariable(_("dark")));
ScriptImageP mask = to_script_image(ctx.getVariable(_("mask")));
mask_blend(light->image, dark->image, mask->image);
return light;
} else {
SCRIPT_RETURN(
script_image_up_to_date(ctx.getVariable(_("light"))) &&
script_image_up_to_date(ctx.getVariable(_("dark" ))) &&
script_image_up_to_date(ctx.getVariable(_("mask" )))
);
}
}
SCRIPT_FUNCTION(set_mask) {
if (last_update_age() == 0) {
ScriptImageP image = to_script_image(ctx.getVariable(_("image")));
ScriptImageP mask = to_script_image(ctx.getVariable(_("mask")));
set_alpha(image->image, mask->image);
return image;
} else {
SCRIPT_RETURN(
script_image_up_to_date(ctx.getVariable(_("image"))) &&
script_image_up_to_date(ctx.getVariable(_("mask")))
);
}
}
bool parse_enum(const String&, ImageCombine& out);
SCRIPT_FUNCTION(set_combine) {
if (last_update_age() == 0) {
SCRIPT_PARAM(String, combine);
ScriptImageP image = to_script_image(ctx.getVariable(_("input")));
// parse and set combine
if (!parse_enum(combine, image->combine)) {
throw ScriptError(_("Not a valid combine mode: '") + combine + _("'"));
}
return image;
} else {
SCRIPT_RETURN(
script_image_up_to_date(ctx.getVariable(_("input")))
);
}
}
SCRIPT_FUNCTION(symbol_variation) {
SCRIPT_PARAM(ValueP, symbol);
SymbolValueP value = dynamic_pointer_cast<SymbolValue>(symbol);
if (last_update_age() == 0) {
SCRIPT_PARAM(String, variation);
// find set & style
SCRIPT_PARAM(Set*, set);
SCRIPT_OPTIONAL_PARAM_(CardP, card);
SymbolStyleP style = dynamic_pointer_cast<SymbolStyle>(set->stylesheetFor(card)->styleFor(value->fieldP));
if (!style) throw InternalError(_("Symbol value has a style of the wrong type"));
// load symbol
SymbolP symbol;
if (value->filename.empty()) {
symbol = default_symbol();
} else {
symbol = set->readFile<SymbolP>(value->filename);
}
// determine filter & render
FOR_EACH(v, style->variations) {
if (v->name == variation) {
// render & filter
return new_intrusive1<ScriptImage>(render_symbol(symbol, *v->filter, v->border_radius));
}
}
throw ScriptError(_("Variation of symbol not found ('") + variation + _("')"));
} else {
// SCRIPT_RETURN(last_update_age() >= value->filename.last_update_age);
SCRIPT_RETURN(last_update_age() > 1); // the symbol was created/loaded after program start,
// don't use cached images
// SCRIPT_RETURN(true);
}
}
SCRIPT_FUNCTION(buildin_image) {
if (last_update_age() == 0) {
SCRIPT_PARAM(String, input);
Image img = load_resource_image(input);
if (!img.Ok()) throw ScriptError(_("There is no build in image '") + input + _("'"));
return new_intrusive1<ScriptImage>(img);
} else {
SCRIPT_RETURN(true); // always up to date
}
}
void init_script_image_functions(Context& ctx) {
ctx.setVariable(_("linear blend"), script_linear_blend);
ctx.setVariable(_("masked blend"), script_masked_blend);
ctx.setVariable(_("set mask"), script_set_mask);
ctx.setVariable(_("set combine"), script_set_combine);
ctx.setVariable(_("symbol variation"), script_symbol_variation);
ctx.setVariable(_("buildin image"), script_buildin_image);
}
...@@ -85,7 +85,13 @@ class ScriptableImage { ...@@ -85,7 +85,13 @@ class ScriptableImage {
}; };
/// Missing for now /// Missing for now
inline ScriptValueP toScript(const ScriptableImage&) { return script_nil; } inline ScriptValueP to_script(const ScriptableImage&) { return script_nil; }
/// Convert a script value to an image
ScriptImageP to_script_image(const ScriptValueP& value);
/// Is the given image up to date?
bool script_image_up_to_date(const ScriptValueP& value);
// ----------------------------------------------------------------------------- : EOF // ----------------------------------------------------------------------------- : EOF
#endif #endif
...@@ -419,14 +419,14 @@ void parseExpr(TokenIterator& input, Script& script, Precedence minPrec) { ...@@ -419,14 +419,14 @@ void parseExpr(TokenIterator& input, Script& script, Precedence minPrec) {
long l = 0; long l = 0;
//l = lexical_cast<long>(token.value); //l = lexical_cast<long>(token.value);
token.value.ToLong(&l); token.value.ToLong(&l);
script.addInstruction(I_PUSH_CONST, toScript(l)); script.addInstruction(I_PUSH_CONST, to_script(l));
} else if (token == TOK_DOUBLE) { } else if (token == TOK_DOUBLE) {
double d = 0; double d = 0;
//d = lexical_cast<double>(token.value); //d = lexical_cast<double>(token.value);
token.value.ToDouble(&d); token.value.ToDouble(&d);
script.addInstruction(I_PUSH_CONST, toScript(d)); script.addInstruction(I_PUSH_CONST, to_script(d));
} else if (token == TOK_STRING) { } else if (token == TOK_STRING) {
script.addInstruction(I_PUSH_CONST, toScript(token.value)); script.addInstruction(I_PUSH_CONST, to_script(token.value));
} else { } else {
throw ScriptParseError(_("Unexpected token '") + token.value + _("'")); throw ScriptParseError(_("Unexpected token '") + token.value + _("'"));
} }
......
...@@ -79,7 +79,7 @@ void Script::addInstruction(InstructionType t, const ScriptValueP& c) { ...@@ -79,7 +79,7 @@ void Script::addInstruction(InstructionType t, const ScriptValueP& c) {
instructions.push_back(i); instructions.push_back(i);
} }
void Script::addInstruction(InstructionType t, const String& s) { void Script::addInstruction(InstructionType t, const String& s) {
constants.push_back(toScript(s)); constants.push_back(to_script(s));
Instruction i = {t, {(unsigned int)constants.size() - 1}}; Instruction i = {t, {(unsigned int)constants.size() - 1}};
instructions.push_back(i); instructions.push_back(i);
} }
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <script/script_manager.hpp> #include <script/script_manager.hpp>
#include <script/to_value.hpp> #include <script/to_value.hpp>
#include <script/functions/functions.hpp>
#include <data/set.hpp> #include <data/set.hpp>
#include <data/stylesheet.hpp> #include <data/stylesheet.hpp>
#include <data/game.hpp> #include <data/game.hpp>
...@@ -25,10 +26,6 @@ DECLARE_TYPEOF_COLLECTION(Dependency); ...@@ -25,10 +26,6 @@ DECLARE_TYPEOF_COLLECTION(Dependency);
DECLARE_TYPEOF_NO_REV(IndexMap<FieldP COMMA StyleP>); DECLARE_TYPEOF_NO_REV(IndexMap<FieldP COMMA StyleP>);
DECLARE_TYPEOF_NO_REV(IndexMap<FieldP COMMA ValueP>); DECLARE_TYPEOF_NO_REV(IndexMap<FieldP COMMA ValueP>);
// initialize functions, from functions.cpp
void init_script_functions(Context& ctx);
void init_script_image_functions(Context& ctx);
//#define LOG_UPDATES //#define LOG_UPDATES
// ----------------------------------------------------------------------------- : SetScriptContext : initialization // ----------------------------------------------------------------------------- : SetScriptContext : initialization
...@@ -56,12 +53,11 @@ Context& SetScriptContext::getContext(const StyleSheetP& stylesheet) { ...@@ -56,12 +53,11 @@ Context& SetScriptContext::getContext(const StyleSheetP& stylesheet) {
// NOTE: do not use a smart pointer for the pointer to the set, because the set owns this // NOTE: do not use a smart pointer for the pointer to the set, because the set owns this
// which would lead to a reference cycle. // which would lead to a reference cycle.
init_script_functions(*ctx); init_script_functions(*ctx);
init_script_image_functions(*ctx);
ctx->setVariable(_("set"), new_intrusive1<ScriptObject<Set*> >(&set)); ctx->setVariable(_("set"), new_intrusive1<ScriptObject<Set*> >(&set));
ctx->setVariable(_("game"), toScript(set.game)); ctx->setVariable(_("game"), to_script(set.game));
ctx->setVariable(_("stylesheet"), toScript(stylesheet)); ctx->setVariable(_("stylesheet"), to_script(stylesheet));
ctx->setVariable(_("card"), set.cards.empty() ? script_nil : toScript(set.cards.front())); // dummy value ctx->setVariable(_("card"), set.cards.empty() ? script_nil : to_script(set.cards.front())); // dummy value
ctx->setVariable(_("styling"), toScript(&set.stylingDataFor(*stylesheet))); ctx->setVariable(_("styling"), to_script(&set.stylingDataFor(*stylesheet)));
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
set.game ->init_script.invoke(*ctx, false); set.game ->init_script.invoke(*ctx, false);
...@@ -76,7 +72,7 @@ Context& SetScriptContext::getContext(const StyleSheetP& stylesheet) { ...@@ -76,7 +72,7 @@ Context& SetScriptContext::getContext(const StyleSheetP& stylesheet) {
Context& SetScriptContext::getContext(const CardP& card) { Context& SetScriptContext::getContext(const CardP& card) {
Context& ctx = getContext(set.stylesheetFor(card)); Context& ctx = getContext(set.stylesheetFor(card));
if (card) { if (card) {
ctx.setVariable(_("card"), toScript(card)); ctx.setVariable(_("card"), to_script(card));
} else { } else {
ctx.setVariable(_("card"), script_nil); ctx.setVariable(_("card"), script_nil);
} }
......
...@@ -52,7 +52,7 @@ class OptionalScript { ...@@ -52,7 +52,7 @@ class OptionalScript {
bool invokeOn(Context& ctx, T& value) const { bool invokeOn(Context& ctx, T& value) const {
if (script) { if (script) {
T new_value; T new_value;
ctx.setVariable(_("value"), toScript(value)); ctx.setVariable(_("value"), to_script(value));
store(ctx.eval(*script), new_value); store(ctx.eval(*script), new_value);
if (value != new_value) { if (value != new_value) {
value = new_value; value = new_value;
......
...@@ -37,7 +37,7 @@ class ScriptCollectionIterator : public ScriptIterator { ...@@ -37,7 +37,7 @@ class ScriptCollectionIterator : public ScriptIterator {
ScriptCollectionIterator(const Collection* col) : pos(0), col(col) {} ScriptCollectionIterator(const Collection* col) : pos(0), col(col) {}
virtual ScriptValueP next() { virtual ScriptValueP next() {
if (pos < col->size()) { if (pos < col->size()) {
return toScript(col->at(pos++)); return to_script(col->at(pos++));
} else { } else {
return ScriptValueP(); return ScriptValueP();
} }
...@@ -57,7 +57,7 @@ class ScriptCollection : public ScriptValue { ...@@ -57,7 +57,7 @@ class ScriptCollection : public ScriptValue {
virtual ScriptValueP getMember(const String& name) const { virtual ScriptValueP getMember(const String& name) const {
long index; long index;
if (name.ToLong(&index) && index >= 0 && (size_t)index < value->size()) { if (name.ToLong(&index) && index >= 0 && (size_t)index < value->size()) {
return toScript(value->at(index)); return to_script(value->at(index));
} else { } else {
return ScriptValue::getMember(name); return ScriptValue::getMember(name);
} }
...@@ -66,6 +66,8 @@ class ScriptCollection : public ScriptValue { ...@@ -66,6 +66,8 @@ class ScriptCollection : public ScriptValue {
return new_intrusive1<ScriptCollectionIterator<Collection> >(value); return new_intrusive1<ScriptCollectionIterator<Collection> >(value);
} }
virtual int itemCount() const { return (int)value->size(); } virtual int itemCount() const { return (int)value->size(); }
/// Collections can be compared by comparing pointers
virtual const void* comparePointer() const { return value; }
private: private:
/// Store a pointer to a collection, collections are only ever used for structures owned outside the script /// Store a pointer to a collection, collections are only ever used for structures owned outside the script
const Collection* value; const Collection* value;
...@@ -77,7 +79,7 @@ template <typename V> ...@@ -77,7 +79,7 @@ template <typename V>
ScriptValueP get_member(const map<String,V>& m, const String& name) { ScriptValueP get_member(const map<String,V>& m, const String& name) {
typename map<String,V>::const_iterator it = m.find(name); typename map<String,V>::const_iterator it = m.find(name);
if (it != m.end()) { if (it != m.end()) {
return toScript(it->second); return to_script(it->second);
} else { } else {
throw ScriptError(_ERROR_2_("has no member", _TYPE_("collection"), name)); throw ScriptError(_ERROR_2_("has no member", _TYPE_("collection"), name));
} }
...@@ -87,7 +89,7 @@ template <typename K, typename V> ...@@ -87,7 +89,7 @@ template <typename K, typename V>
ScriptValueP get_member(const IndexMap<K,V>& m, const String& name) { ScriptValueP get_member(const IndexMap<K,V>& m, const String& name) {
typename IndexMap<K,V>::const_iterator it = m.find(name); typename IndexMap<K,V>::const_iterator it = m.find(name);
if (it != m.end()) { if (it != m.end()) {
return toScript(*it); return to_script(*it);
} else { } else {
throw ScriptError(_ERROR_2_("has no member", _TYPE_("collection"), name)); throw ScriptError(_ERROR_2_("has no member", _TYPE_("collection"), name));
} }
...@@ -104,6 +106,8 @@ class ScriptMap : public ScriptValue { ...@@ -104,6 +106,8 @@ class ScriptMap : public ScriptValue {
return get_member(*value, name); return get_member(*value, name);
} }
virtual int itemCount() const { return (int)value->size(); } virtual int itemCount() const { return (int)value->size(); }
/// Collections can be compared by comparing pointers
virtual const void* comparePointer() const { return value; }
private: private:
/// Store a pointer to a collection, collections are only ever used for structures owned outside the script /// Store a pointer to a collection, collections are only ever used for structures owned outside the script
const Collection* value; const Collection* value;
...@@ -163,6 +167,8 @@ class ScriptObject : public ScriptValue { ...@@ -163,6 +167,8 @@ class ScriptObject : public ScriptValue {
int i = item_count(*value); int i = item_count(*value);
return i >= 0 ? i : ScriptValue::itemCount(); return i >= 0 ? i : ScriptValue::itemCount();
} }
/// Objects can be compared by comparing pointers
virtual const void* comparePointer() const { return &*value; }
/// Get access to the value /// Get access to the value
inline T getValue() const { return value; } inline T getValue() const { return value; }
private: private:
...@@ -177,118 +183,38 @@ class ScriptObject : public ScriptValue { ...@@ -177,118 +183,38 @@ class ScriptObject : public ScriptValue {
// ----------------------------------------------------------------------------- : Creating // ----------------------------------------------------------------------------- : Creating
/// Convert a value to a script value /// Convert a value to a script value
ScriptValueP toScript(int v); ScriptValueP to_script(int v);
inline ScriptValueP toScript(long v) { return toScript((int) v); } inline ScriptValueP to_script(long v) { return to_script((int) v); }
ScriptValueP toScript(double v); ScriptValueP to_script(double v);
ScriptValueP toScript(const String& v); ScriptValueP to_script(const String& v);
ScriptValueP toScript(const Color& v); ScriptValueP to_script(const Color& v);
inline ScriptValueP toScript(bool v) { return v ? script_true : script_false; } inline ScriptValueP to_script(bool v) { return v ? script_true : script_false; }
template <typename T> template <typename T>
inline ScriptValueP toScript(const vector<T>* v) { return new_intrusive1<ScriptCollection<vector<T> > >(v); } inline ScriptValueP to_script(const vector<T>* v) { return new_intrusive1<ScriptCollection<vector<T> > >(v); }
template <typename K, typename V> template <typename K, typename V>
inline ScriptValueP toScript(const map<K,V>* v) { return new_intrusive1<ScriptMap<map<K,V> > >(v); } inline ScriptValueP to_script(const map<K,V>* v) { return new_intrusive1<ScriptMap<map<K,V> > >(v); }
template <typename K, typename V> template <typename K, typename V>
inline ScriptValueP toScript(const IndexMap<K,V>* v) { return new_intrusive1<ScriptMap<IndexMap<K,V> > >(v); } inline ScriptValueP to_script(const IndexMap<K,V>* v) { return new_intrusive1<ScriptMap<IndexMap<K,V> > >(v); }
template <typename T> template <typename T>
inline ScriptValueP toScript(const shared_ptr<T>& v) { return new_intrusive1<ScriptObject<shared_ptr<T> > >(v); } inline ScriptValueP to_script(const shared_ptr<T>& v) { return new_intrusive1<ScriptObject<shared_ptr<T> > >(v); }
template <typename T> template <typename T>
inline ScriptValueP toScript(const Defaultable<T>& v) { return toScript(v()); } inline ScriptValueP to_script(const Defaultable<T>& v) { return to_script(v()); }
// ----------------------------------------------------------------------------- : Buildin functions // ----------------------------------------------------------------------------- : Destructing
/// Macro to declare a new script function /// Convert a value from a script value to a normal value
/** Usage: template <typename T> inline T from_script (const ScriptValueP& value) {
* @code ScriptObject<T>* o = dynamic_cast<ScriptObject<T>*>(value.get());
* SCRIPT_FUNCTION(my_function) { if (!o) {
* // function code goes here throw ScriptError(_ERROR_2_("can't convert", value->typeName(), _TYPE_("object" )));
* } }
* @endcode return o->getValue();
* This declares a value 'script_my_function' which can be added as a variable to a context }
* using: template <> inline ScriptValueP from_script<ScriptValueP>(const ScriptValueP& value) { return value; }
* @code template <> inline String from_script<String> (const ScriptValueP& value) { return *value; }
* extern ScriptValueP script_my_function; template <> inline int from_script<int> (const ScriptValueP& value) { return *value; }
* context.setVariable("my_function", script_my_function); template <> inline double from_script<double> (const ScriptValueP& value) { return *value; }
* @endcode template <> inline bool from_script<bool> (const ScriptValueP& value) { return *value; }
*/
#define SCRIPT_FUNCTION(name) SCRIPT_FUNCTION_AUX(name,;)
/// Macro to declare a new script function with custom dependency handling
#define SCRIPT_FUNCTION_DEP(name) SCRIPT_FUNCTION_AUX(name, virtual ScriptValueP dependencies(Context&, const Dependency&) const;)
// helper for SCRIPT_FUNCTION and SCRIPT_FUNCTION_DEP
#define SCRIPT_FUNCTION_AUX(name,dep) \
class ScriptBuildin_##name : public ScriptValue { \
dep \
virtual ScriptType type() const \
{ return SCRIPT_FUNCTION; } \
virtual String typeName() const \
{ return _("build in function '") _(#name) _("'"); } \
virtual ScriptValueP eval(Context&) const; \
}; \
ScriptValueP script_##name(new ScriptBuildin_##name); \
ScriptValueP ScriptBuildin_##name::eval(Context& ctx) const
/// Retrieve a parameter to a SCRIPT_FUNCTION with the given name and type
/** Usage:
* @code
* SCRIPT_FUNCTION(my_function) {
* SCRIPT_PARAM(String, my_string_param);
* ... my_string_param ...
* }
* @endcode
* Throws an error if the parameter is not found.
*/
#define SCRIPT_PARAM(Type, name) \
SCRIPT_PARAM_N(Type, _(#name), name)
#define SCRIPT_PARAM_N(Type, str, name) \
Type name = getParam<Type>(ctx.getVariable(str))
template <typename T>
inline T getParam (const ScriptValueP& value) {
ScriptObject<T>* o = dynamic_cast<ScriptObject<T>*>(value.get());
if (!o) throw ScriptError(_("Can't convert from ")+value->typeName()+_(" to object"));
return o->getValue();
}
template <> inline ScriptValueP getParam<ScriptValueP>(const ScriptValueP& value) { return value; }
template <> inline String getParam<String> (const ScriptValueP& value) { return *value; }
template <> inline int getParam<int> (const ScriptValueP& value) { return *value; }
template <> inline double getParam<double> (const ScriptValueP& value) { return *value; }
template <> inline bool getParam<bool> (const ScriptValueP& value) { return (int)*value; }
/// Retrieve an optional parameter
/** Usage:
* @code
* SCRIPT_FUNCTION(my_function) {
* SCRIPT_OPTIONAL_PARAM(String, my_string_param) {
* ... my_string_param ...
* }
* ...
* }
* @endcode
*/
#define SCRIPT_OPTIONAL_PARAM(Type, name) SCRIPT_OPTIONAL_PARAM_N(Type, _(#name), name)
#define SCRIPT_OPTIONAL_PARAM_N(Type, str, name) \
ScriptValueP name##_ = ctx.getVariableOpt(str); \
Type name = name##_ ? getParam<Type>(name##_) : Type(); \
if (name##_)
/// Retrieve an optional parameter, can't be used as an if statement
#define SCRIPT_OPTIONAL_PARAM_(Type, name) SCRIPT_OPTIONAL_PARAM_N_(Type, _(#name), name)
#define SCRIPT_OPTIONAL_PARAM_N_(Type, str, name) \
ScriptValueP name##_ = ctx.getVariableOpt(str); \
Type name = name##_ ? getParam<Type>(name##_) : Type();
/// Retrieve an optional parameter with a default value
#define SCRIPT_PARAM_DEFAULT(Type, name, def) SCRIPT_PARAM_DEFAULT_N(Type, _(#name), name, def)
/// Retrieve an optional parameter with a default value
#define SCRIPT_PARAM_DEFAULT_N(Type, str, name, def) \
ScriptValueP name##_ = ctx.getVariableOpt(str); \
Type name = name##_ ? getParam<Type>(name##_) : def
/// Return a value from a SCRIPT_FUNCTION
#define SCRIPT_RETURN(value) return toScript(value)
// ----------------------------------------------------------------------------- : EOF // ----------------------------------------------------------------------------- : EOF
#endif #endif
...@@ -23,6 +23,7 @@ ScriptValueP ScriptValue::getMember(const String& name) const { throw ScriptErro ...@@ -23,6 +23,7 @@ ScriptValueP ScriptValue::getMember(const String& name) const { throw ScriptErro
ScriptValueP ScriptValue::next() { throw InternalError(_("Can't convert from ")+typeName()+_(" to iterator")); } ScriptValueP ScriptValue::next() { throw InternalError(_("Can't convert from ")+typeName()+_(" to iterator")); }
ScriptValueP ScriptValue::makeIterator() const { throw ScriptError(_ERROR_2_("can't convert", typeName(), _TYPE_("collection"))); } ScriptValueP ScriptValue::makeIterator() const { throw ScriptError(_ERROR_2_("can't convert", typeName(), _TYPE_("collection"))); }
int ScriptValue::itemCount() const { throw ScriptError(_ERROR_2_("can't convert", typeName(), _TYPE_("collection"))); } int ScriptValue::itemCount() const { throw ScriptError(_ERROR_2_("can't convert", typeName(), _TYPE_("collection"))); }
const void* ScriptValue::comparePointer() const { return nullptr; }
ScriptValueP ScriptValue::dependencyMember(const String& name, const Dependency&) const { return dependency_dummy; } ScriptValueP ScriptValue::dependencyMember(const String& name, const Dependency&) const { return dependency_dummy; }
ScriptValueP ScriptValue::dependencies(Context&, const Dependency&) const { return dependency_dummy; } ScriptValueP ScriptValue::dependencies(Context&, const Dependency&) const { return dependency_dummy; }
...@@ -41,7 +42,7 @@ class ScriptRangeIterator : public ScriptIterator { ...@@ -41,7 +42,7 @@ class ScriptRangeIterator : public ScriptIterator {
: pos(start), end(end) {} : pos(start), end(end) {}
virtual ScriptValueP next() { virtual ScriptValueP next() {
if (pos <= end) { if (pos <= end) {
return toScript(pos++); return to_script(pos++);
} else { } else {
return ScriptValueP(); return ScriptValueP();
} }
...@@ -84,7 +85,7 @@ class ScriptInt : public ScriptValue { ...@@ -84,7 +85,7 @@ class ScriptInt : public ScriptValue {
} }
#endif #endif
ScriptValueP toScript(int v) { ScriptValueP to_script(int v) {
#ifdef USE_POOL_ALLOCATOR #ifdef USE_POOL_ALLOCATOR
#ifdef USE_INTRUSIVE_PTR #ifdef USE_INTRUSIVE_PTR
return ScriptValueP( return ScriptValueP(
...@@ -138,7 +139,7 @@ class ScriptDouble : public ScriptValue { ...@@ -138,7 +139,7 @@ class ScriptDouble : public ScriptValue {
double value; double value;
}; };
ScriptValueP toScript(double v) { ScriptValueP to_script(double v) {
return new_intrusive1<ScriptDouble>(v); return new_intrusive1<ScriptDouble>(v);
} }
...@@ -184,7 +185,7 @@ class ScriptString : public ScriptValue { ...@@ -184,7 +185,7 @@ class ScriptString : public ScriptValue {
// get member returns characters // get member returns characters
long index; long index;
if (name.ToLong(&index) && index >= 0 && (size_t)index < value.size()) { if (name.ToLong(&index) && index >= 0 && (size_t)index < value.size()) {
return toScript(String(1,value[index])); return to_script(String(1,value[index]));
} else { } else {
throw ScriptError(_ERROR_2_("has no member value", value, name)); throw ScriptError(_ERROR_2_("has no member value", value, name));
} }
...@@ -193,7 +194,7 @@ class ScriptString : public ScriptValue { ...@@ -193,7 +194,7 @@ class ScriptString : public ScriptValue {
String value; String value;
}; };
ScriptValueP toScript(const String& v) { ScriptValueP to_script(const String& v) {
return new_intrusive1<ScriptString>(v); return new_intrusive1<ScriptString>(v);
} }
...@@ -214,7 +215,7 @@ class ScriptColor : public ScriptValue { ...@@ -214,7 +215,7 @@ class ScriptColor : public ScriptValue {
Color value; Color value;
}; };
ScriptValueP toScript(const Color& v) { ScriptValueP to_script(const Color& v) {
return new_intrusive1<ScriptColor>(v); return new_intrusive1<ScriptColor>(v);
} }
......
...@@ -41,6 +41,9 @@ class ScriptValue : public IntrusivePtrBase { ...@@ -41,6 +41,9 @@ class ScriptValue : public IntrusivePtrBase {
virtual ScriptType type() const = 0; virtual ScriptType type() const = 0;
/// Name of the type of value /// Name of the type of value
virtual String typeName() const = 0; virtual String typeName() const = 0;
/// A pointer that uniquely identifies the value, used for comparing.
/** If implementation is not possible, should return nullptr. */
virtual const void* comparePointer() const;
/// Convert this value to a string /// Convert this value to a string
virtual operator String() const; virtual operator String() const;
......
...@@ -13,15 +13,15 @@ ...@@ -13,15 +13,15 @@
// ---------------------------------------------------------------------------- : GetDefaultMember // ---------------------------------------------------------------------------- : GetDefaultMember
void GetDefaultMember::handle(const Char* v) { value = toScript(v); } void GetDefaultMember::handle(const Char* v) { value = to_script(v); }
template <> void GetDefaultMember::handle(const String& v) { value = toScript(v); } template <> void GetDefaultMember::handle(const String& v) { value = to_script(v); }
template <> void GetDefaultMember::handle(const FileName& v) { value = toScript(v); } template <> void GetDefaultMember::handle(const FileName& v) { value = to_script(v); }
template <> void GetDefaultMember::handle(const int& v) { value = toScript(v); } template <> void GetDefaultMember::handle(const int& v) { value = to_script(v); }
template <> void GetDefaultMember::handle(const unsigned int& v) { value = toScript((int)v); } template <> void GetDefaultMember::handle(const unsigned int& v) { value = to_script((int)v); }
template <> void GetDefaultMember::handle(const double& v) { value = toScript(v); } template <> void GetDefaultMember::handle(const double& v) { value = to_script(v); }
template <> void GetDefaultMember::handle(const bool& v) { value = toScript(v); } template <> void GetDefaultMember::handle(const bool& v) { value = to_script(v); }
template <> void GetDefaultMember::handle(const Vector2D& v) { value = toScript(String::Format(_("(%.10lf,%.10lf)"), v.x, v.y)); } template <> void GetDefaultMember::handle(const Vector2D& v) { value = to_script(String::Format(_("(%.10lf,%.10lf)"), v.x, v.y)); }
template <> void GetDefaultMember::handle(const Color& v) { value = toScript(v); } template <> void GetDefaultMember::handle(const Color& v) { value = to_script(v); }
void GetDefaultMember::handle(const ScriptValueP& v) { value = v; } void GetDefaultMember::handle(const ScriptValueP& v) { value = v; }
void GetDefaultMember::handle(const ScriptP& v) { value = v; } void GetDefaultMember::handle(const ScriptP& v) { value = v; }
......
...@@ -47,10 +47,10 @@ class GetDefaultMember { ...@@ -47,10 +47,10 @@ class GetDefaultMember {
/// Handle a Defaultable: investigate children /// Handle a Defaultable: investigate children
template <typename T> void handle(const Defaultable<T>&); template <typename T> void handle(const Defaultable<T>&);
template <typename T> void handle(const Scriptable<T>& ); template <typename T> void handle(const Scriptable<T>& );
template <typename T> void handle(const vector<T>& c) { value = toScript(&c); } template <typename T> void handle(const vector<T>& c) { value = to_script(&c); }
template <typename K, typename V> void handle(const map<K,V>& c) { value = toScript(&c); } template <typename K, typename V> void handle(const map<K,V>& c) { value = to_script(&c); }
template <typename K, typename V> void handle(const IndexMap<K,V>& c) { value = toScript(&c); } template <typename K, typename V> void handle(const IndexMap<K,V>& c) { value = to_script(&c); }
template <typename T> void handle(const shared_ptr<T>& p) { value = toScript(p); } template <typename T> void handle(const shared_ptr<T>& p) { value = to_script(p); }
void handle(const ScriptValueP&); void handle(const ScriptValueP&);
void handle(const ScriptP&); void handle(const ScriptP&);
private: private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment