From 2f5666ac5b1a8e49d165906a09af68d4fa1accaa Mon Sep 17 00:00:00 2001 From: twanvl Date: Sat, 2 Aug 2008 19:16:30 +0000 Subject: [PATCH] dependencies of extra card fields are now also added and updated; this fixes combined_editor in extra fields. git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1036 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/script/dependency.cpp | 9 +++++++-- src/script/functions/basic.cpp | 7 +++++++ src/script/functions/editor.cpp | 17 ++++++++++++++++- src/script/script_manager.cpp | 18 +++++++++++------- src/script/value.cpp | 3 +++ src/script/value.hpp | 3 +++ 6 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/script/dependency.cpp b/src/script/dependency.cpp index 8900202c..ab4038df 100644 --- a/src/script/dependency.cpp +++ b/src/script/dependency.cpp @@ -20,6 +20,8 @@ DECLARE_TYPEOF_COLLECTION(Context::Binding); // ----------------------------------------------------------------------------- : Dummy values +extern ScriptValueP dependency_dummy; + // A dummy type used during dependency analysis, // it simply supresses all error messages. class DependencyDummy : public ScriptIterator { @@ -27,6 +29,7 @@ class DependencyDummy : public ScriptIterator { virtual ScriptType type() const { return SCRIPT_DUMMY; } virtual String typeName() const { return _("dummy"); } virtual ScriptValueP next() { return ScriptValueP(); } + virtual ScriptValueP dependencyName(const ScriptValue&, const Dependency&) const { return dependency_dummy; } }; ScriptValueP dependency_dummy(new DependencyDummy); @@ -55,6 +58,9 @@ class DependencyUnion : public ScriptValue { virtual ScriptValueP dependencyMember(const String& name, const Dependency& dep) const { return unified(a->dependencyMember(name,dep), b->dependencyMember(name,dep)); } + virtual ScriptValueP dependencyName(const ScriptValue& container, const Dependency& dep) const { + return unified(a->dependencyName(container,dep), b->dependencyName(container,dep)); + } private: ScriptValueP a, b; }; @@ -297,8 +303,7 @@ ScriptValueP Context::dependencies(const Dependency& dep, const Script& script) a = rangeIterator(0,0); // values don't matter break; case I_MEMBER: { - String name = *b; - a = a->dependencyMember(name, dep); // dependency on member + a = b->dependencyName(*a, dep); // dependency on member break; } case I_ADD: unify(a, b); // may be function composition diff --git a/src/script/functions/basic.cpp b/src/script/functions/basic.cpp index ae86dac4..b7adb3f8 100644 --- a/src/script/functions/basic.cpp +++ b/src/script/functions/basic.cpp @@ -102,6 +102,13 @@ SCRIPT_FUNCTION(to_int) { } else if (t == SCRIPT_COLOR) { AColor c = (AColor)*input; result = (c.Red() + c.Blue() + c.Green()) / 3; + } else if (t == SCRIPT_STRING) { + long l; + if (input->toString().ToLong(&l)) { + result = l; + } else { + return new_intrusive1(_ERROR_3_("can't convert value", input->toString(), input->typeName(), _TYPE_("integer"))); + } } else { result = (int)*input; } diff --git a/src/script/functions/editor.cpp b/src/script/functions/editor.cpp index 882c5e91..0a9586e8 100644 --- a/src/script/functions/editor.cpp +++ b/src/script/functions/editor.cpp @@ -12,9 +12,12 @@ #include #include #include +#include +#include #include #include #include +#include DECLARE_TYPEOF_COLLECTION(FieldP); DECLARE_TYPEOF_COLLECTION(TextValue*); @@ -81,8 +84,16 @@ SCRIPT_FUNCTION_WITH_DEP(combined_editor) { FOR_EACH_2(v, values, nv, value_parts) { //if (v->value() != nv.first && v->last_update < new_value_update) { if (v->last_update < new_value_update) { + bool changed = v->value() != nv.first; v->value.assign(nv.first); - v->update(ctx); + changed |= v->update(ctx); + v->last_update = new_value_update; + if (changed) { // notify of change + SCRIPT_OPTIONAL_PARAM_(CardP, card); + SCRIPT_PARAM(Set*, set); + ScriptValueEvent change(card.get(), v); + set->actions.tellListeners(change, false); + } } nv.first = v->value(); nv.second = index_to_untagged(nv.first, nv.first.size()) == 0; @@ -153,6 +164,10 @@ SCRIPT_FUNCTION_DEPENDENCIES(combined_editor) { 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 if (dep.type == DEP_EXTRA_CARD_FIELD) { + SCRIPT_PARAM_C(StyleSheetP, stylesheet); + target_field = stylesheet->extra_card_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 diff --git a/src/script/script_manager.cpp b/src/script/script_manager.cpp index d8a538ed..63d79a00 100644 --- a/src/script/script_manager.cpp +++ b/src/script/script_manager.cpp @@ -129,9 +129,9 @@ void SetScriptManager::initDependencies(Context& ctx, StyleSheet& stylesheet) { if (stylesheet.dependencies_initialized) return; stylesheet.dependencies_initialized = true; // find dependencies of extra card fields - /*FOR_EACH(f, stylesheet.extra_card_fields) { + FOR_EACH(f, stylesheet.extra_card_fields) { f->initDependencies(ctx, Dependency(DEP_EXTRA_CARD_FIELD, f->index, &stylesheet)); - }*/ + } // find dependencies of choice images and other style stuff FOR_EACH(s, stylesheet.card_style) { s->initDependencies(ctx, Dependency(DEP_STYLE, s->fieldP->index, &stylesheet)); @@ -229,7 +229,11 @@ void SetScriptManager::updateStyles(const CardP& card, bool only_content_depende // update extra card fields IndexMap& extra_data = card->extraDataFor(stylesheet); FOR_EACH(v, extra_data) { - v->update(ctx); + if (v->update(ctx)) { + // changed, send event + ScriptValueEvent change(card.get(), v.get()); + set.actions.tellListeners(change, false); + } } } // update all styles @@ -379,8 +383,8 @@ void SetScriptManager::alsoUpdate(deque& to_update, const vector(d.data); StyleSheet* stylesheet_card = &set.stylesheetFor(card); @@ -388,8 +392,8 @@ void SetScriptManager::alsoUpdate(deque& to_update, const vectorextra_data.at(d.index); to_update.push_back(ToUpdate(value.get(), card)); } - } - break;*/ + }*/ + break; } case DEP_CARD_COPY_DEP: { // propagate dependencies from another field FieldP f = set.game->card_fields[d.index]; diff --git a/src/script/value.cpp b/src/script/value.cpp index 59268fbd..d1b0ef45 100644 --- a/src/script/value.cpp +++ b/src/script/value.cpp @@ -48,6 +48,9 @@ ScriptValueP ScriptValue::getIndex(int index) const { ScriptValueP ScriptValue::simplifyClosure(ScriptClosure&) const { return ScriptValueP(); } ScriptValueP ScriptValue::dependencyMember(const String& name, const Dependency&) const { return dependency_dummy; } +ScriptValueP ScriptValue::dependencyName(const ScriptValue& container, const Dependency& dep) const { + return container.dependencyMember(toString(),dep); +} ScriptValueP ScriptValue::dependencies(Context&, const Dependency&) const { return dependency_dummy; } bool approx_equal(double a, double b) { diff --git a/src/script/value.hpp b/src/script/value.hpp index 7908a279..3dade3e1 100644 --- a/src/script/value.hpp +++ b/src/script/value.hpp @@ -80,6 +80,9 @@ class ScriptValue : public IntrusivePtrBaseWithDelete { /// Signal that a script depends on a member of this value /** It is the abstract version of getMember*/ virtual ScriptValueP dependencyMember(const String& name, const Dependency&) const; + /// Signal that a script depends on a member of container, with the name of this + /** This function allows for a kind of visitor pattern over dependencyMember */ + virtual ScriptValueP dependencyName(const ScriptValue& container, const Dependency&) const; /// Evaluate this value (if it is a function) virtual ScriptValueP eval(Context&) const;