From 902a85f113450ba389cc651927a40e988daa2c4f Mon Sep 17 00:00:00 2001 From: twanvl Date: Thu, 5 Jun 2008 20:36:20 +0000 Subject: [PATCH] Allow symbol_variation script function to load images from the stylesheet by filename git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@977 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/gfx/generated_image.cpp | 12 +++++++----- src/gfx/generated_image.hpp | 7 ++++--- src/script/functions/image.cpp | 21 ++++++++++++++++----- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/gfx/generated_image.cpp b/src/gfx/generated_image.cpp index 5fa0a46c..16dc9fbf 100644 --- a/src/gfx/generated_image.cpp +++ b/src/gfx/generated_image.cpp @@ -385,19 +385,20 @@ bool BuiltInImage::operator == (const GeneratedImage& that) const { // ----------------------------------------------------------------------------- : SymbolToImage -SymbolToImage::SymbolToImage(const String& filename, Age age, const SymbolVariationP& variation) - : filename(filename), age(age), variation(variation) +SymbolToImage::SymbolToImage(bool is_local, const String& filename, Age age, const SymbolVariationP& variation) + : is_local(is_local), filename(filename), age(age), variation(variation) {} SymbolToImage::~SymbolToImage() {} Image SymbolToImage::generate(const Options& opt) const { // TODO : use opt.width and opt.height? - if (!opt.local_package) throw ScriptError(_("Can only load images in a context where an image is expected")); + Package* package = is_local ? opt.local_package : opt.package; + if (!package) throw ScriptError(_("Can only load images in a context where an image is expected")); SymbolP the_symbol; if (filename.empty()) { the_symbol = default_symbol(); } else { - the_symbol = opt.local_package->readFile(filename); + the_symbol = package->readFile(filename); } int size = max(100, 3*max(opt.width,opt.height)); if (opt.width <= 1 || opt.height <= 1) { @@ -410,7 +411,8 @@ Image SymbolToImage::generate(const Options& opt) const { } bool SymbolToImage::operator == (const GeneratedImage& that) const { const SymbolToImage* that2 = dynamic_cast(&that); - return that2 && filename == that2->filename + return that2 && is_local == that2->is_local + && filename == that2->filename && age == that2->age && (variation == that2->variation || *variation == *that2->variation // custom variation diff --git a/src/gfx/generated_image.hpp b/src/gfx/generated_image.hpp index c2ca4c5e..52edb7d1 100644 --- a/src/gfx/generated_image.hpp +++ b/src/gfx/generated_image.hpp @@ -289,19 +289,20 @@ class BuiltInImage : public GeneratedImage { /// Use a symbol as an image class SymbolToImage : public GeneratedImage { public: - SymbolToImage(const String& filename, Age age, const SymbolVariationP& variation); + SymbolToImage(bool is_local, const String& filename, Age age, const SymbolVariationP& variation); ~SymbolToImage(); virtual Image generate(const Options& opt) const; virtual bool operator == (const GeneratedImage& that) const; - virtual bool local() const { return true; } + virtual bool local() const { return is_local; } #ifdef __WXGTK__ virtual bool threadSafe() const { return false; } #endif private: SymbolToImage(const SymbolToImage&); // copy ctor + bool is_local; ///< Use local package? String filename; - Age age; ///< Age the symbol was last updated + Age age; ///< Age the symbol was last updated SymbolVariationP variation; }; diff --git a/src/script/functions/image.cpp b/src/script/functions/image.cpp index a1f289af..0435906d 100644 --- a/src/script/functions/image.cpp +++ b/src/script/functions/image.cpp @@ -112,9 +112,20 @@ SCRIPT_FUNCTION(drop_shadow) { SCRIPT_FUNCTION(symbol_variation) { // find symbol - SCRIPT_PARAM(ValueP, symbol); - SymbolValueP value = dynamic_pointer_cast(symbol); - SCRIPT_OPTIONAL_PARAM(String, variation) { + SCRIPT_PARAM(ScriptValueP, symbol); // TODO: change to input? + ScriptObject* valueO = dynamic_cast*>(symbol.get()); + SymbolValue* value = valueO ? dynamic_cast(valueO->getValue().get()) : nullptr; + String filename; + if (value) { + filename = value->filename; + } else if (valueO) { + throw ScriptError(_ERROR_2_("can't convert", valueO->typeName(), _TYPE_("symbol" ))); + } else { + filename = from_script(symbol); + } + // known variation? + SCRIPT_OPTIONAL_PARAM_(String, variation) + if (value && variation_) { // find style SCRIPT_PARAM(Set*, set); SCRIPT_OPTIONAL_PARAM_(CardP, card); @@ -124,7 +135,7 @@ SCRIPT_FUNCTION(symbol_variation) { FOR_EACH(v, style->variations) { if (v->name == variation) { // found it - return new_intrusive3(value->filename, value->last_update, v); + return new_intrusive4(value, filename, value->last_update, v); } } throw ScriptError(_("Variation of symbol not found ('") + variation + _("')")); @@ -158,7 +169,7 @@ SCRIPT_FUNCTION(symbol_variation) { } else { throw ScriptError(_("Unknown fill type for symbol_variation: ") + fill_type); } - return new_intrusive3(value->filename, value->last_update, var); + return new_intrusive4(value, filename, value ? value->last_update : Age(), var); } }