mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
made dependency analysis work without errors for magic-new (except for a few script functions); implemented the rest of the ScriptManager
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@71 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -99,6 +99,14 @@ template <> StyleP read_new<Style>(Reader&) {
|
||||
throw InternalError(_("IndexMap contains nullptr StyleP the application should have crashed already"));
|
||||
}
|
||||
|
||||
bool Style::update(Context& ctx) {
|
||||
return left .update(ctx)
|
||||
| top .update(ctx)
|
||||
| width .update(ctx)
|
||||
| height .update(ctx)
|
||||
| visible.update(ctx);
|
||||
}
|
||||
|
||||
void Style::initDependencies(Context& ctx, const Dependency& dep) const {
|
||||
left .initDependencies(ctx,dep);
|
||||
top .initDependencies(ctx,dep);
|
||||
|
||||
+8
-2
@@ -12,6 +12,7 @@
|
||||
#include <util/prec.hpp>
|
||||
#include <util/reflect.hpp>
|
||||
#include <util/alignment.hpp>
|
||||
#include <util/age.hpp>
|
||||
#include <script/scriptable.hpp>
|
||||
#include <script/dependency.hpp>
|
||||
|
||||
@@ -96,6 +97,8 @@ class Style {
|
||||
/** thisP is a smart pointer to this */
|
||||
virtual ValueEditorP makeEditor(DataEditor& parent, const StyleP& thisP) = 0;
|
||||
|
||||
/// Update scripted values of this style, return true if anything has changed
|
||||
virtual bool update(Context&);
|
||||
/// Add the given dependency to the dependet_scripts list for the variables this style depends on
|
||||
virtual void initDependencies(Context&, const Dependency&) const;
|
||||
|
||||
@@ -116,10 +119,13 @@ class Value {
|
||||
inline Value(const FieldP& field) : fieldP(field) {}
|
||||
virtual ~Value();
|
||||
|
||||
const FieldP fieldP; ///< Field this value is for, should have the right type!
|
||||
|
||||
const FieldP fieldP; ///< Field this value is for, should have the right type!
|
||||
Age last_script_update; ///< When where the scripts last updated? (by calling update)
|
||||
|
||||
/// Convert this value to a string for use in tables
|
||||
virtual String toString() const = 0;
|
||||
/// Apply scripts to this value, return true if the value has changed
|
||||
virtual bool update(Context&) { last_script_update.update(); return false; }
|
||||
|
||||
private:
|
||||
DECLARE_REFLECTION_VIRTUAL();
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
#include <data/field/choice.hpp>
|
||||
|
||||
DECLARE_TYPEOF_COLLECTION(ChoiceField::ChoiceP);
|
||||
typedef map<String,ScriptableImage> map_String_ScriptableImage;
|
||||
DECLARE_TYPEOF(map_String_ScriptableImage);
|
||||
|
||||
// ----------------------------------------------------------------------------- : ChoiceField
|
||||
|
||||
@@ -23,6 +25,12 @@ String ChoiceField::typeName() const {
|
||||
return _("choice");
|
||||
}
|
||||
|
||||
void ChoiceField::initDependencies(Context& ctx, const Dependency& dep) const {
|
||||
Field ::initDependencies(ctx, dep);
|
||||
script .initDependencies(ctx, dep);
|
||||
default_script.initDependencies(ctx, dep);
|
||||
}
|
||||
|
||||
IMPLEMENT_REFLECTION(ChoiceField) {
|
||||
REFLECT_BASE(Field);
|
||||
REFLECT_N("choices", choices->choices);
|
||||
@@ -156,6 +164,24 @@ ChoiceStyle::ChoiceStyle(const ChoiceFieldP& field)
|
||||
, colors_card_list(false)
|
||||
{}
|
||||
|
||||
// TODO
|
||||
/*
|
||||
void ChoiceStyle::invalidate() {
|
||||
// rebuild choice images
|
||||
}
|
||||
}
|
||||
*/
|
||||
bool ChoiceStyle::update(Context& ctx) {
|
||||
// Don't update the choice images, leave that to invalidate()
|
||||
return Style::update(ctx);
|
||||
}
|
||||
void ChoiceStyle::initDependencies(Context& ctx, const Dependency& dep) const {
|
||||
Style::initDependencies(ctx, dep);
|
||||
FOR_EACH_CONST(ci, choice_images) {
|
||||
ci.second.initDependencies(ctx, dep);
|
||||
}
|
||||
}
|
||||
|
||||
IMPLEMENT_REFLECTION_ENUM(ChoicePopupStyle) {
|
||||
VALUE_N("dropdown", POPUP_DROPDOWN);
|
||||
VALUE_N("menu", POPUP_MENU);
|
||||
@@ -190,6 +216,11 @@ IMPLEMENT_REFLECTION(ChoiceStyle) {
|
||||
String ChoiceValue::toString() const {
|
||||
return value();
|
||||
}
|
||||
bool ChoiceValue::update(Context& ctx) {
|
||||
Value::update(ctx);
|
||||
return field().default_script.invokeOnDefault(ctx, value)
|
||||
| field(). script.invokeOn(ctx, value);
|
||||
}
|
||||
|
||||
IMPLEMENT_REFLECTION_NAMELESS(ChoiceValue) {
|
||||
REFLECT_NAMELESS(value);
|
||||
|
||||
@@ -36,7 +36,9 @@ class ChoiceField : public Field {
|
||||
OptionalScript default_script; ///< Script that generates the default value
|
||||
String initial; ///< Initial choice of a new value, or ""
|
||||
String default_name; ///< Name of "default" value
|
||||
|
||||
|
||||
virtual void initDependencies(Context&, const Dependency&) const;
|
||||
|
||||
private:
|
||||
DECLARE_REFLECTION();
|
||||
};
|
||||
@@ -123,6 +125,9 @@ class ChoiceStyle : public Style {
|
||||
ImageCombine combine; ///< Combining mode for drawing the images
|
||||
Alignment alignment; ///< Alignment of images
|
||||
|
||||
virtual bool update(Context&);
|
||||
virtual void initDependencies(Context&, const Dependency&) const;
|
||||
|
||||
private:
|
||||
DECLARE_REFLECTION();
|
||||
};
|
||||
@@ -138,6 +143,7 @@ class ChoiceValue : public Value {
|
||||
Defaultable<String> value; /// The name of the selected choice
|
||||
|
||||
virtual String toString() const;
|
||||
virtual bool update(Context&);
|
||||
|
||||
private:
|
||||
DECLARE_REFLECTION();
|
||||
|
||||
@@ -46,6 +46,11 @@ Context& Set::getContext(const Card& card) {
|
||||
return script_manager->getContext(card.stylesheet ? card.stylesheet : stylesheet);
|
||||
}
|
||||
|
||||
StyleSheetP Set::stylesheetFor(const CardP& card) {
|
||||
if (card && card->stylesheet) return card->stylesheet;
|
||||
else return stylesheet;
|
||||
}
|
||||
|
||||
String Set::typeName() const { return _("set"); }
|
||||
|
||||
// fix values for versions < 0.2.7
|
||||
|
||||
+3
-1
@@ -54,11 +54,13 @@ class Set : public Packaged {
|
||||
/// A context for performing scripts
|
||||
/** Should only be used from the main thread! */
|
||||
Context& getContext();
|
||||
|
||||
/// A context for performing scripts on a particular card
|
||||
/** Should only be used from the main thread! */
|
||||
Context& getContext(const Card& card);
|
||||
|
||||
/// Stylesheet to use for a particular card
|
||||
StyleSheetP stylesheetFor(const CardP& card);
|
||||
|
||||
protected:
|
||||
virtual String typeName() const;
|
||||
virtual void validate(Version);
|
||||
|
||||
@@ -97,6 +97,9 @@ IMPLEMENT_REFLECTION(GradientSymbolFilter) {
|
||||
|
||||
// ----------------------------------------------------------------------------- : LinearGradientSymbolFilter
|
||||
|
||||
// TODO: move to some general util header
|
||||
inline double sqr(double x) { return x * x; }
|
||||
|
||||
String LinearGradientSymbolFilter::fillType() const { return _("linear gradient"); }
|
||||
|
||||
LinearGradientSymbolFilter::LinearGradientSymbolFilter()
|
||||
@@ -105,12 +108,14 @@ LinearGradientSymbolFilter::LinearGradientSymbolFilter()
|
||||
{}
|
||||
|
||||
AColor LinearGradientSymbolFilter::color(double x, double y, SymbolSet point) const {
|
||||
len = sqr(end_x - center_x) + sqr(end_y - center_y);
|
||||
if (len == 0) len = 1; // prevent div by 0
|
||||
return GradientSymbolFilter::color(x,y,point,this);
|
||||
}
|
||||
|
||||
double LinearGradientSymbolFilter::t(double x, double y) const {
|
||||
//return abs( int(x - center_x) * dirX + int(y - centerY) * dirY) * scale;
|
||||
return 0; // todo
|
||||
double t= abs( (x - center_x) * (end_x - center_x) + (y - center_y) * (end_y - center_y)) / len;
|
||||
return min(1.,max(0.,t));
|
||||
}
|
||||
|
||||
IMPLEMENT_REFLECTION(LinearGradientSymbolFilter) {
|
||||
@@ -127,9 +132,6 @@ AColor RadialGradientSymbolFilter::color(double x, double y, SymbolSet point) co
|
||||
return GradientSymbolFilter::color(x,y,point,this);
|
||||
}
|
||||
|
||||
// TODO: move to some general util header
|
||||
inline double sqr(double x) { return x * x; }
|
||||
|
||||
double RadialGradientSymbolFilter::t(double x, double y) const {
|
||||
return sqrt( (sqr(x - 0.5) + sqr(y - 0.5)) * 2);
|
||||
}
|
||||
|
||||
@@ -94,6 +94,7 @@ class LinearGradientSymbolFilter : public GradientSymbolFilter {
|
||||
private:
|
||||
double center_x, center_y;
|
||||
double end_x, end_y;
|
||||
mutable double len;
|
||||
DECLARE_REFLECTION();
|
||||
};
|
||||
|
||||
|
||||
@@ -25,9 +25,11 @@ class DependencyDummy : public ScriptIterator {
|
||||
virtual ScriptType type() const { return SCRIPT_DUMMY; }
|
||||
virtual String typeName() const { return _("dummy"); }
|
||||
virtual ScriptValueP next() { return ScriptValueP(); }
|
||||
virtual ScriptValueP eval(Context&) const { return dependency_dummy; } // dummy() == dummy
|
||||
virtual ScriptValueP getMember(const String&) const { return dependency_dummy; } // dummy.* = dummy
|
||||
};
|
||||
|
||||
ScriptValueP dependencyDummy(new DependencyDummy);
|
||||
ScriptValueP dependency_dummy(new DependencyDummy);
|
||||
|
||||
ScriptValueP unified(const ScriptValueP& a, const ScriptValueP& b);
|
||||
|
||||
@@ -220,7 +222,7 @@ ScriptValueP Context::dependencies(const Dependency& dep, const Script& script)
|
||||
assert(dynamic_pointer_cast<ScriptIterator>(it)); // top of stack must be an iterator
|
||||
ScriptValueP val = static_pointer_cast<ScriptIterator>(it)->next();
|
||||
if (val) {
|
||||
it = dependencyDummy; // invalidate iterator, so we loop only once
|
||||
it = dependency_dummy; // invalidate iterator, so we loop only once
|
||||
stack.push_back(val);
|
||||
} else {
|
||||
stack.erase(stack.end() - 2); // remove iterator
|
||||
@@ -259,7 +261,9 @@ ScriptValueP Context::dependencies(const Dependency& dep, const Script& script)
|
||||
// Get a variable (almost as normal)
|
||||
case I_GET_VAR: {
|
||||
ScriptValueP value = variables[i.data].value;
|
||||
if (!value) value = new_intrusive1<ScriptMissingVariable>(variable_to_string(i.data)); // no errors here
|
||||
if (!value) {
|
||||
value = new_intrusive1<ScriptMissingVariable>(variable_to_string(i.data)); // no errors here
|
||||
}
|
||||
stack.push_back(value);
|
||||
break;
|
||||
}
|
||||
@@ -277,7 +281,7 @@ ScriptValueP Context::dependencies(const Dependency& dep, const Script& script)
|
||||
a = a->makeIterator(); // as normal
|
||||
break;
|
||||
default:
|
||||
a = dependencyDummy;
|
||||
a = dependency_dummy;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -298,7 +302,7 @@ ScriptValueP Context::dependencies(const Dependency& dep, const Script& script)
|
||||
unify(a, b); // may be function composition
|
||||
break;
|
||||
default:
|
||||
a = dependencyDummy;
|
||||
a = dependency_dummy;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -307,7 +311,7 @@ ScriptValueP Context::dependencies(const Dependency& dep, const Script& script)
|
||||
ScriptValueP c = stack.back(); stack.pop_back();
|
||||
ScriptValueP b = stack.back(); stack.pop_back();
|
||||
ScriptValueP& a = stack.back();
|
||||
a = dependencyDummy;
|
||||
a = dependency_dummy;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,9 +11,9 @@
|
||||
|
||||
#include <util/prec.hpp>
|
||||
#include <util/age.hpp>
|
||||
#include <util/dynamic_arg.hpp>
|
||||
#include <script/scriptable.hpp>
|
||||
#include <gfx/gfx.hpp>
|
||||
#include <util/dynamic_arg.hpp>
|
||||
|
||||
class Package;
|
||||
DECLARE_INTRUSIVE_POINTER_TYPE(ScriptImage);
|
||||
@@ -64,6 +64,10 @@ class ScriptableImage {
|
||||
/// Is the cached image up to date?
|
||||
bool upToDate(Context& ctx, Age age) const;
|
||||
|
||||
inline void initDependencies(Context& ctx, const Dependency& dep) const {
|
||||
script.initDependencies(ctx, dep);
|
||||
}
|
||||
|
||||
private:
|
||||
OptionalScript script; ///< The script, not really optional
|
||||
ScriptImageP cache; ///< The cached image
|
||||
|
||||
@@ -10,14 +10,19 @@
|
||||
#include <data/set.hpp>
|
||||
#include <data/stylesheet.hpp>
|
||||
#include <data/game.hpp>
|
||||
#include <data/card.hpp>
|
||||
#include <data/field.hpp>
|
||||
#include <util/error.hpp>
|
||||
|
||||
typedef map<const StyleSheet*,Context*> Contexts;
|
||||
typedef IndexMap<FieldP,StyleP> IndexMap_FieldP_StyleP;
|
||||
typedef IndexMap<FieldP,ValueP> IndexMap_FieldP_ValueP;
|
||||
DECLARE_TYPEOF(Contexts);
|
||||
DECLARE_TYPEOF_COLLECTION(CardP);
|
||||
DECLARE_TYPEOF_COLLECTION(FieldP);
|
||||
DECLARE_TYPEOF_COLLECTION(Dependency);
|
||||
DECLARE_TYPEOF_NO_REV(IndexMap_FieldP_StyleP);
|
||||
DECLARE_TYPEOF_NO_REV(IndexMap_FieldP_ValueP);
|
||||
|
||||
// initialize functions, from functions.cpp
|
||||
void init_script_functions(Context& ctx);
|
||||
@@ -58,9 +63,9 @@ Context& ScriptManager::getContext(const StyleSheetP& stylesheet) {
|
||||
ctx->setVariable(_("card"), set.cards.empty() ? script_nil : toScript(set.cards.front())); // dummy value
|
||||
//ctx->setVariable(_("styling"), toScript(set->extraStyleData(style)));
|
||||
try {
|
||||
// perform init scripts
|
||||
set.game ->init_script.invoke(*ctx);
|
||||
stylesheet->init_script.invoke(*ctx);
|
||||
// perform init scripts, don't use a scope, variables stay bound in the context
|
||||
set.game ->init_script.invoke(*ctx, false);
|
||||
stylesheet->init_script.invoke(*ctx, false);
|
||||
// find script dependencies
|
||||
initDependencies(*ctx, *set.game);
|
||||
initDependencies(*ctx, *stylesheet);
|
||||
@@ -97,12 +102,140 @@ void ScriptManager::initDependencies(Context& ctx, StyleSheet& stylesheet) {
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : ScriptManager : dependency handling
|
||||
// ----------------------------------------------------------------------------- : ScriptManager : updating
|
||||
|
||||
void ScriptManager::onAction(const Action& action, bool undone) {
|
||||
// TODO
|
||||
// TYPE_CASE(action, ValueAction) {
|
||||
// }
|
||||
// TYPE_CASE(action, CardListAction) {
|
||||
// }
|
||||
}
|
||||
|
||||
void ScriptManager::updateStyles(const CardP& card) {
|
||||
// lastUpdatedCard = card;
|
||||
StyleSheetP stylesheet = set.stylesheetFor(card);
|
||||
Context& ctx = getContext(stylesheet);
|
||||
// update all styles
|
||||
FOR_EACH(s, stylesheet->card_style) {
|
||||
if (s->update(ctx)) {
|
||||
// style has changed, tell listeners
|
||||
// ScriptStyleEvent change(s.get());
|
||||
// set->actions.tellListeners(change);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptManager::updateValue(Value& value, const CardP& card) {
|
||||
Age starting_age; // the start of the update process
|
||||
deque<ToUpdate> to_update;
|
||||
// execute script for initial changed value
|
||||
value.update(getContext(set.stylesheetFor(card)));
|
||||
// update dependent scripts
|
||||
alsoUpdate(to_update, value.fieldP->dependent_scripts, card);
|
||||
updateRecursive(to_update, starting_age);
|
||||
}
|
||||
|
||||
void ScriptManager::updateAll() {
|
||||
// TODO
|
||||
// update set data
|
||||
Context& ctx = getContext(set.stylesheet);
|
||||
FOR_EACH(v, set.data) {
|
||||
v->update(ctx);
|
||||
}
|
||||
// update card data of all cards
|
||||
FOR_EACH(card, set.cards) {
|
||||
Context& ctx = getContext(set.stylesheetFor(card));
|
||||
FOR_EACH(v, card->data) {
|
||||
v->update(ctx);
|
||||
}
|
||||
}
|
||||
// update things that depend on the card list
|
||||
updateAllDependend(set.game->dependent_scripts_cards);
|
||||
}
|
||||
|
||||
void ScriptManager::updateAllDependend(const vector<Dependency>& dependent_scripts) {
|
||||
deque<ToUpdate> to_update;
|
||||
Age starting_age;
|
||||
alsoUpdate(to_update, dependent_scripts, CardP());
|
||||
updateRecursive(to_update, starting_age);
|
||||
}
|
||||
|
||||
void ScriptManager::updateRecursive(deque<ToUpdate>& to_update, Age starting_age) {
|
||||
// set->order_cache.clear(); // clear caches before evaluating a round of scripts
|
||||
while (!to_update.empty()) {
|
||||
updateToUpdate(to_update.front(), to_update, starting_age);
|
||||
to_update.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptManager::updateToUpdate(const ToUpdate& u, deque<ToUpdate>& to_update, Age starting_age) {
|
||||
Age age = u.value->last_script_update;
|
||||
if (starting_age < age) return; // this value was already updated
|
||||
Context& ctx = getContext(set.stylesheetFor(u.card));
|
||||
if (u.value->update(ctx)) {
|
||||
// changed, send event
|
||||
// ScriptValueEvent change(&*u.card, u.value);
|
||||
// set.actions.tellListeners(change);
|
||||
// u.value has changed, also update values with a dependency on u.value
|
||||
alsoUpdate(to_update, u.value->fieldP->dependent_scripts, u.card);
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptManager::alsoUpdate(deque<ToUpdate>& to_update, const vector<Dependency>& deps, const CardP& card) {
|
||||
FOR_EACH_CONST(d, deps) {
|
||||
switch (d.type) {
|
||||
case DEP_SET_FIELD: {
|
||||
break;
|
||||
} case DEP_CARD_FIELD: {
|
||||
break;
|
||||
} case DEP_CARDS_FIELD: {
|
||||
break;
|
||||
} case DEP_STYLE: {
|
||||
break;
|
||||
} case DEP_CARD_COPY_DEP: {
|
||||
break;
|
||||
} case DEP_SET_COPY_DEP: {
|
||||
break;
|
||||
} default:
|
||||
assert(false);
|
||||
}
|
||||
/*
|
||||
if (d.type == DependendScript.setField) {
|
||||
// from set data
|
||||
ValueP value = set->data.at(ds.index);
|
||||
toUpdate.push_back(ToUpdate(&*value));
|
||||
} else if (ds.type == DependendScript.cardField) {
|
||||
// from the same card's data
|
||||
assert(card);
|
||||
ValueP value = card->data.at(ds.index);
|
||||
toUpdate.push_back(ToUpdate(&*value, card));
|
||||
} else if (ds.type == DependendScript.cardsField) {
|
||||
// something invalidates a card value for all cards, so all cards need updating
|
||||
FOR_EACH(card, set)->cards {
|
||||
ValueP value = card->data.at(ds.index);
|
||||
toUpdate.push_back(ToUpdate(&*value, card));
|
||||
}
|
||||
} else if (ds.type >= DependendScript.choiceImage) {
|
||||
// a generated image has become invalid, there is not much we can do
|
||||
// because the index is not exact enough, it only gives the field
|
||||
// TODO : Indicate what style
|
||||
//CardStyleP style = set->styleOf(card) // WRONG?
|
||||
CardStyle* style = CardStyle.getByIndex(ds.type - DependendScript.choiceImage);
|
||||
StyleP s = style->cardStyle.at(ds.index);
|
||||
s->invalidate();
|
||||
// something changed, send event
|
||||
ScriptStyleEvent change(&*s);
|
||||
set->actions.tellListeners(change);
|
||||
} else if (ds.type == DependendScript.cardCopyDep) {
|
||||
// propagate dependencies from another field
|
||||
FieldP f = game->cardFields#(ds.index);
|
||||
alsoUpdate(toUpdate, f->dependendScripts, card);
|
||||
} else if (ds.type == DependendScript.setCopyDep) {
|
||||
// propagate dependencies from another field
|
||||
FieldP f = game->setFields#(ds.index);
|
||||
alsoUpdate(toUpdate, f->dependendScripts, card);
|
||||
} else {
|
||||
assert(false); // only setField, cardField and cardsField should be possible
|
||||
}*/
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,10 @@
|
||||
|
||||
#include <util/prec.hpp>
|
||||
#include <util/action_stack.hpp>
|
||||
#include <util/age.hpp>
|
||||
#include <script/context.hpp>
|
||||
#include <script/dependency.hpp>
|
||||
#include <queue>
|
||||
|
||||
class Set;
|
||||
class Value;
|
||||
@@ -37,10 +39,13 @@ class ScriptManager : public ActionListener {
|
||||
public:
|
||||
ScriptManager(Set& set);
|
||||
~ScriptManager();
|
||||
|
||||
|
||||
/// Get a context to use for the set, for a given stylesheet
|
||||
Context& getContext(const StyleSheetP& s);
|
||||
|
||||
// Update all styles for a particular card
|
||||
void updateStyles(const CardP& card);
|
||||
|
||||
private:
|
||||
Set& set; ///< Set for which we are managing scripts
|
||||
map<const StyleSheet*,Context*> contexts; ///< Context for evaluating scripts that use a given stylesheet
|
||||
@@ -48,18 +53,29 @@ class ScriptManager : public ActionListener {
|
||||
void initDependencies(Context&, Game&);
|
||||
void initDependencies(Context&, StyleSheet&);
|
||||
|
||||
// Update all styles for a particular card
|
||||
void updateStyles(const CardP& card);
|
||||
/// Updates scripts, starting at some value
|
||||
/** if the value changes any dependend values are updated as well */
|
||||
void updateValue(Value* value, const CardP& card);
|
||||
void updateValue(Value& value, const CardP& card);
|
||||
/// Update all fields of all cards
|
||||
/** Update all set info fields
|
||||
* Doesn't update styles
|
||||
*/
|
||||
void updateAll();
|
||||
// Update all values with a specific dependency
|
||||
void updateAllDependend(const vector<Dependency>& dependendScripts);
|
||||
void updateAllDependend(const vector<Dependency>& dependent_scripts);
|
||||
|
||||
// Something that needs to be updated
|
||||
struct ToUpdate {
|
||||
Value* value; // value to update
|
||||
CardP card; // card the value is in, or 0 if it is not a card field
|
||||
};
|
||||
/// Update all things in to_update, and things that depent on them, etc.
|
||||
/** Only update things that are older than starting_age. */
|
||||
void updateRecursive(deque<ToUpdate>& to_update, Age starting_age);
|
||||
/// Update a value given by a ToUpdate object, and add things depending on it to to_update
|
||||
void updateToUpdate(const ToUpdate& u, deque<ToUpdate>& to_update, Age starting_age);
|
||||
/// Schedule all things in deps to be updated by adding them to to_update
|
||||
void alsoUpdate(deque<ToUpdate>& to_update, const vector<Dependency>& deps, const CardP& card);
|
||||
|
||||
protected:
|
||||
/// Respond to actions by updating scripts
|
||||
|
||||
@@ -24,9 +24,9 @@ void store(const ScriptValueP& val, Defaultable<String>& var) { var.assign(*val)
|
||||
|
||||
OptionalScript::~OptionalScript() {}
|
||||
|
||||
ScriptValueP OptionalScript::invoke(Context& ctx) const {
|
||||
ScriptValueP OptionalScript::invoke(Context& ctx, bool open_scope) const {
|
||||
if (script) {
|
||||
return ctx.eval(*script);
|
||||
return ctx.eval(*script, open_scope);
|
||||
} else {
|
||||
return script_nil;
|
||||
}
|
||||
|
||||
@@ -13,10 +13,10 @@
|
||||
#include <util/reflect.hpp>
|
||||
#include <util/defaultable.hpp>
|
||||
#include <script/script.hpp>
|
||||
#include <script/context.hpp>
|
||||
#include <script/parser.hpp>
|
||||
|
||||
DECLARE_INTRUSIVE_POINTER_TYPE(Script);
|
||||
class Context;
|
||||
|
||||
// ----------------------------------------------------------------------------- : Store
|
||||
|
||||
@@ -37,7 +37,7 @@ class OptionalScript {
|
||||
inline operator bool() const { return !!script; }
|
||||
|
||||
/// Invoke the script, return the result, or script_nil if there is no script
|
||||
ScriptValueP invoke(Context& ctx) const;
|
||||
ScriptValueP invoke(Context& ctx, bool open_scope = true) const;
|
||||
|
||||
/// Invoke the script on a value
|
||||
/** Assigns the result to value if it has changed.
|
||||
@@ -47,7 +47,8 @@ class OptionalScript {
|
||||
bool invokeOn(Context& ctx, T& value) const {
|
||||
if (script) {
|
||||
T new_value;
|
||||
store(new_value, script->invoke(ctx));
|
||||
ctx.setVariable(_("value"), toScript(value));
|
||||
store(ctx.eval(*script), new_value);
|
||||
if (value != new_value) {
|
||||
value = new_value;
|
||||
return true;
|
||||
@@ -55,6 +56,16 @@ class OptionalScript {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/// Invoke the script on a value if it is in the default state
|
||||
template <typename T>
|
||||
bool invokeOnDefault(Context& ctx, Defaultable<T>& value) const {
|
||||
if (value.isDefault() && invokeOn(ctx, value)) {
|
||||
value.setDefault(); // restore defaultness
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize things this script depends on by adding dep to their list of dependent scripts
|
||||
void initDependencies(Context&, const Dependency& dep) const;
|
||||
@@ -68,6 +79,9 @@ class OptionalScript {
|
||||
template <typename T> friend class Scriptable;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline ScriptValueP toScript(const Defaultable<T>& v) { return toScript(v.get()); }
|
||||
|
||||
// ----------------------------------------------------------------------------- : Scriptable
|
||||
|
||||
/// A script that defines a calculation to find a value
|
||||
|
||||
+10
-12
@@ -13,20 +13,18 @@
|
||||
// ----------------------------------------------------------------------------- : ScriptValue
|
||||
// Base cases
|
||||
|
||||
ScriptValue::operator String() const { return _("[[") + typeName() + _("]]"); }
|
||||
ScriptValue::operator int() const { throw ScriptError(_("Can't convert from ")+typeName()+_(" to integer number")); }
|
||||
ScriptValue::operator double() const { throw ScriptError(_("Can't convert from ")+typeName()+_(" to real number" )); }
|
||||
ScriptValue::operator Color() const { throw ScriptError(_("Can't convert from ")+typeName()+_(" to color" )); }
|
||||
ScriptValueP ScriptValue::eval(Context&) const
|
||||
{ throw ScriptError(_("Can't convert from ")+typeName()+_(" to function" )); }
|
||||
ScriptValueP ScriptValue::getMember(const String& name) const
|
||||
{ throw ScriptError(typeName() + _(" has no member '") + name + _("'")); }
|
||||
ScriptValueP ScriptValue::next() { throw InternalError(_("Can't convert from ")+typeName()+_(" to iterator")); }
|
||||
ScriptValueP ScriptValue::makeIterator() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to collection")); }
|
||||
int ScriptValue::itemCount() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to collection")); }
|
||||
ScriptValue::operator String() const { return _("[[") + typeName() + _("]]"); }
|
||||
ScriptValue::operator int() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to integer number")); }
|
||||
ScriptValue::operator double() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to real number" )); }
|
||||
ScriptValue::operator Color() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to color" )); }
|
||||
ScriptValueP ScriptValue::eval(Context&) const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to function" )); }
|
||||
ScriptValueP ScriptValue::getMember(const String& name) const { throw ScriptError(typeName() + _(" has no member '") + name + _("'")); }
|
||||
ScriptValueP ScriptValue::next() { throw InternalError(_("Can't convert from ")+typeName()+_(" to iterator")); }
|
||||
ScriptValueP ScriptValue::makeIterator() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to collection")); }
|
||||
int ScriptValue::itemCount() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to collection")); }
|
||||
|
||||
void ScriptValue::signalDependent(Context&, const Dependency&, const String& name) {}
|
||||
ScriptValueP ScriptValue::dependencies( Context&, const Dependency&) const { return script_nil; }
|
||||
ScriptValueP ScriptValue::dependencies(Context&, const Dependency&) const { return dependency_dummy; }
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------- : Iterators
|
||||
|
||||
@@ -114,7 +114,7 @@ class ScriptValue {
|
||||
extern ScriptValueP script_nil; ///< The preallocated nil value
|
||||
extern ScriptValueP script_true; ///< The preallocated true value
|
||||
extern ScriptValueP script_false; ///< The preallocated false value
|
||||
|
||||
extern ScriptValueP dependency_dummy; ///< Dummy value used during dependency analysis
|
||||
|
||||
// ----------------------------------------------------------------------------- : Iterators
|
||||
|
||||
|
||||
@@ -35,6 +35,12 @@ class Defaultable {
|
||||
|
||||
/// Is this value in the default state?
|
||||
inline bool isDefault() const { return is_default; }
|
||||
/// Set the defaultness to true
|
||||
inline void setDefault() { is_default = true; }
|
||||
|
||||
/// Compare the values, ignore defaultness
|
||||
/** used by scriptable to check for changes */
|
||||
inline bool operator != (const Defaultable& that) const { return value != that.value; }
|
||||
|
||||
private:
|
||||
/// Is this value in the default state?
|
||||
@@ -46,7 +52,6 @@ class Defaultable {
|
||||
friend class Writer;
|
||||
};
|
||||
|
||||
|
||||
// we need some custom io, because the behaviour is different for each of Reader/Writer/GetMember
|
||||
|
||||
template <typename T>
|
||||
|
||||
Reference in New Issue
Block a user