Added 'symbol_variation' script function

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@101 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2006-12-09 16:31:22 +00:00
parent 86328aaeec
commit c130916465
8 changed files with 110 additions and 37 deletions
+46 -6
View File
@@ -10,8 +10,16 @@
#include <script/context.hpp>
#include <util/dynamic_arg.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);
// image generating functions have two modes
// if last_update_age > 0 they return whether the image is still up to date
// if last_update_age == 0 they generate the image
@@ -226,6 +234,37 @@ SCRIPT_FUNCTION(set_combine) {
}
}
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(true);
}
}
SCRIPT_FUNCTION(buildin_image) {
if (last_update_age() == 0) {
SCRIPT_PARAM(String, input);
@@ -233,14 +272,15 @@ SCRIPT_FUNCTION(buildin_image) {
if (!img.Ok()) throw ScriptError(_("There is no build in image '") + input + _("'"));
return new_intrusive1<ScriptImage>(img);
} else {
SCRIPT_RETURN(true);
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(_("buildin image"), script_buildin_image);
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);
}
+25 -10
View File
@@ -257,6 +257,8 @@ class ScriptObject : public ScriptValue {
int i = item_count(*value);
return i >= 0 ? i : ScriptValue::itemCount();
}
/// Get access to the value
inline T getValue() { return value; }
private:
T value; ///< The object
ScriptValueP getDefault() const {
@@ -332,9 +334,15 @@ inline ScriptValueP toScript(const shared_ptr<T>& v) { return new_intrusive1<Scr
Type name = getParam<Type>(ctx.getVariable(_(#name)))
template <typename T>
inline T getParam (const ScriptValueP& value) { return *value; }
template <>
inline ScriptValueP getParam<ScriptValueP>(const ScriptValueP& value) { return value; }
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; }
/// Retrieve an optional parameter
/** Usage:
@@ -347,17 +355,24 @@ inline ScriptValueP getParam<ScriptValueP>(const ScriptValueP& value) { return v
* }
* @endcode
*/
#define SCRIPT_OPTIONAL_PARAM(Type, name) SCRIPT_OPTIONAL_PARAM_N(Type, name, #name)
#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##_ ? *name##_ : Type(); \
#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) \
ScriptValueP name##_ = ctx.getVariableOpt(_(#name)); \
Type name = name##_ ? *name##_ : def
#define SCRIPT_PARAM_DEFAULT(Type, name, def) \
ScriptValueP name##_ = ctx.getVariableOpt(_(#name)); \
Type name = name##_ ? getParam<Type>(name##_) : def
/// Return a value from a SCRIPT_FUNCTION
#define SCRIPT_RETURN(value) return toScript(value)