Commit 3f7b0bae authored by twanvl's avatar twanvl

Added 'symbol_variation' script function

parent e023ad98
......@@ -45,6 +45,19 @@ InputStreamP StyleSheet::openIconFile() {
}
}
StyleP StyleSheet::styleFor(const FieldP& field) {
if (card_style.containsKey(field)) {
return card_style[field];
} else if (set_info_style.containsKey(field)) {
return set_info_style[field];
} else if (styling_style.containsKey(field)) {
return styling_style[field];
} else {
throw InternalError(_("Can not find styling for field '")+field->name+_("'in stylesheet"));
}
}
IMPLEMENT_REFLECTION(StyleSheet) {
// < 0.3.0 didn't use card_ prefix
tag.addAlias(300, _("width"), _("card width"));
......
......@@ -50,6 +50,9 @@ class StyleSheet : public Packaged {
inline RealRect getCardRect() const { return RealRect(0, 0, card_width, card_height); }
/// Return the style for a given field, it is not specified what type of field this is.
StyleP styleFor(const FieldP& field);
/// Load a StyleSheet, given a Game and the name of the StyleSheet
static StyleSheetP byGameAndName(const Game& game, const String& name);
/// name of the package without the game name
......
......@@ -163,6 +163,26 @@ IMPLEMENT_REFLECTION(Symbol) {
REFLECT(parts);
}
// ----------------------------------------------------------------------------- : Default symbol
// A default symbol part, a square, moved by d
SymbolPartP default_symbol_part(double d) {
SymbolPartP part = new_shared<SymbolPart>();
part->points.push_back(new_shared2<ControlPoint>(d + .2, d + .2));
part->points.push_back(new_shared2<ControlPoint>(d + .2, d + .8));
part->points.push_back(new_shared2<ControlPoint>(d + .8, d + .8));
part->points.push_back(new_shared2<ControlPoint>(d + .8, d + .2));
part->name = _("Square");
return part;
}
// A default symbol, a square
SymbolP default_symbol() {
SymbolP symbol = new_shared<Symbol>();
symbol->parts.push_back(default_symbol_part(0));
return symbol;
}
// ----------------------------------------------------------------------------- : SymbolView
SymbolView::SymbolView() {}
......
......@@ -162,6 +162,8 @@ class Symbol {
DECLARE_REFLECTION();
};
/// A default symbol: a square
SymbolP default_symbol();
// ----------------------------------------------------------------------------- : SymbolView
......
......@@ -19,26 +19,6 @@
#include <wx/filename.h>
#include <wx/wfstream.h>
// ------------------------------------------------------------------------------------------------ : Default symbol
// A default symbol part, a square, moved by d
SymbolPartP defaultSymbolPart(double d) {
SymbolPartP part = new_shared<SymbolPart>();
part->points.push_back(new_shared2<ControlPoint>(d + .2, d + .2));
part->points.push_back(new_shared2<ControlPoint>(d + .2, d + .8));
part->points.push_back(new_shared2<ControlPoint>(d + .8, d + .8));
part->points.push_back(new_shared2<ControlPoint>(d + .8, d + .2));
part->name = _("Square");
return part;
}
// A default symbol, a square
SymbolP default_symbol() {
SymbolP symbol = new_shared<Symbol>();
symbol->parts.push_back(defaultSymbolPart(0));
return symbol;
}
// ----------------------------------------------------------------------------- : Constructor
SymbolWindow::SymbolWindow(Window* parent) {
......
......@@ -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);
}
......@@ -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)
......
......@@ -71,7 +71,7 @@ class IndexMap : private vector<Value> {
/// Is a key in the domain of this index map?
inline bool containsKey(const Key& key) const {
assert(key);
return key->index < this.size() && get_key((*this)[key->index]) == key;
return key->index < this->size() && get_key((*this)[key->index]) == key;
}
/// Find a value given the key name, return an iterator
......
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