From b3d04fe19217fbc395fb50e403e0b4a004a2c5f0 Mon Sep 17 00:00:00 2001 From: twanvl Date: Wed, 4 Jul 2007 17:15:08 +0000 Subject: [PATCH] Scripts depending on content_something are re-updating after updating the content properties git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@480 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/data/set.cpp | 4 ++-- src/data/set.hpp | 2 +- src/render/card/viewer.cpp | 42 ++++++++++++++++++++++------------- src/render/card/viewer.hpp | 2 ++ src/render/text/viewer.cpp | 5 ++++- src/render/text/viewer.hpp | 3 ++- src/render/value/text.cpp | 3 ++- src/render/value/text.hpp | 2 +- src/render/value/viewer.hpp | 5 +++-- src/script/script_manager.cpp | 25 ++++++++++++++------- src/script/script_manager.hpp | 4 ++-- 11 files changed, 63 insertions(+), 34 deletions(-) diff --git a/src/data/set.cpp b/src/data/set.cpp index 2d7c99e7..814eda3f 100644 --- a/src/data/set.cpp +++ b/src/data/set.cpp @@ -53,8 +53,8 @@ Context& Set::getContext(const CardP& card) { assert(wxThread::IsMain()); return script_manager->getContext(card); } -void Set::updateStyles(const CardP& card) { - script_manager->updateStyles(card); +void Set::updateStyles(const CardP& card, bool only_content_dependent) { + script_manager->updateStyles(card, only_content_dependent); } void Set::updateDelayed() { script_manager->updateDelayed(); diff --git a/src/data/set.hpp b/src/data/set.hpp index 7ba794ff..a4e6087c 100644 --- a/src/data/set.hpp +++ b/src/data/set.hpp @@ -66,7 +66,7 @@ class Set : public Packaged { /** Should only be used from the main thread! */ Context& getContext(const CardP& card); /// Update styles and extra_card_fields for a card - void updateStyles(const CardP& card); + void updateStyles(const CardP& card, bool only_content_dependent); /// Update scripts that were delayed void updateDelayed(); /// A context for performing scripts diff --git a/src/render/card/viewer.cpp b/src/render/card/viewer.cpp index d51389df..2c8370c1 100644 --- a/src/render/card/viewer.cpp +++ b/src/render/card/viewer.cpp @@ -40,30 +40,23 @@ void DataViewer::draw(RotatedDC& dc, const Color& background) { // fill with background color clearDC(dc.getDC(), background); // update style scripts - try { - if (card) { - set->updateStyles(card); - } else { - Context& ctx = getContext(); - FOR_EACH(v, viewers) { - if (v->getStyle()->update(ctx)) { - v->getStyle()->tellListeners(); - } - } - } - } catch (const Error& e) { - handle_error(e, false, false); - } + updateStyles(false); // prepare viewers + bool changed_content_properties = false; FOR_EACH(v, viewers) { // draw low z index fields first if (v->getStyle()->visible) { try { - v->prepare(dc); + if (v->prepare(dc)) { + changed_content_properties = true; + } } catch (const Error& e) { handle_error(e, false, false); } } } + if (changed_content_properties) { + updateStyles(true); + } // draw viewers FOR_EACH(v, viewers) { // draw low z index fields first if (v->getStyle()->visible) {// visible @@ -80,6 +73,25 @@ void DataViewer::drawViewer(RotatedDC& dc, ValueViewer& v) { v.draw(dc); } +void DataViewer::updateStyles(bool only_content_dependent) { + try { + if (card) { + set->updateStyles(card, only_content_dependent); + } else { + Context& ctx = getContext(); + FOR_EACH(v, viewers) { + Style& s = *v->getStyle(); + if (only_content_dependent && !s.content_dependent) continue; + if (s.update(ctx)) { + s.tellListeners(); + } + } + } + } catch (const Error& e) { + handle_error(e, false, false); + } +} + // ----------------------------------------------------------------------------- : Utility for ValueViewers bool DataViewer::nativeLook() const { return false; } diff --git a/src/render/card/viewer.hpp b/src/render/card/viewer.hpp index 7c36f0a3..8997b3f9 100644 --- a/src/render/card/viewer.hpp +++ b/src/render/card/viewer.hpp @@ -75,6 +75,8 @@ class DataViewer : public SetView { private: /// Create some viewers for the given styles void addStyles(IndexMap& styles); + /// Update style scripts + void updateStyles(bool only_content_dependent); protected: /// Set the styles for the data to be shown, recreating the viewers void setStyles(const StyleSheetP& stylesheet, IndexMap& styles, IndexMap* extra_styles = nullptr); diff --git a/src/render/text/viewer.cpp b/src/render/text/viewer.cpp index 6b279c1e..ec5eda3b 100644 --- a/src/render/text/viewer.cpp +++ b/src/render/text/viewer.cpp @@ -131,12 +131,15 @@ void TextViewer::drawSeparators(RotatedDC& dc) { } } -void TextViewer::prepare(RotatedDC& dc, const String& text, TextStyle& style, Context& ctx) { +bool TextViewer::prepare(RotatedDC& dc, const String& text, TextStyle& style, Context& ctx) { if (lines.empty()) { // not prepared yet Rotater r(dc, style.getRotation()); prepareElements(text, style, ctx); prepareLines(dc, text, style, ctx); + return true; + } else { + return false; } } void TextViewer::reset() { diff --git a/src/render/text/viewer.hpp b/src/render/text/viewer.hpp index e3d55e01..03aa4ca6 100644 --- a/src/render/text/viewer.hpp +++ b/src/render/text/viewer.hpp @@ -54,7 +54,8 @@ class TextViewer { void drawSeparators(RotatedDC& dc); /// Prepare the text for drawing, if it is not already prepared - void prepare(RotatedDC& dc, const String& text, TextStyle& style, Context&); + /** Returns true if something has been done */ + bool prepare(RotatedDC& dc, const String& text, TextStyle& style, Context&); /// Reset the cached data, at a new call to draw it will be recalculated void reset(); diff --git a/src/render/value/text.cpp b/src/render/value/text.cpp index cad21eef..5c25e251 100644 --- a/src/render/value/text.cpp +++ b/src/render/value/text.cpp @@ -12,7 +12,7 @@ // ----------------------------------------------------------------------------- : TextValueViewer -void TextValueViewer::prepare(RotatedDC& dc) { +bool TextValueViewer::prepare(RotatedDC& dc) { if (!style().mask_filename.empty() && !style().mask.ok()) { // load contour mask Image image; @@ -22,6 +22,7 @@ void TextValueViewer::prepare(RotatedDC& dc) { } } v.prepare(dc, value().value(), style(), viewer.getContext()); + return true; } void TextValueViewer::draw(RotatedDC& dc) { diff --git a/src/render/value/text.hpp b/src/render/value/text.hpp index 91b9b0ac..4d8e7ce6 100644 --- a/src/render/value/text.hpp +++ b/src/render/value/text.hpp @@ -21,7 +21,7 @@ class TextValueViewer : public ValueViewer { public: DECLARE_VALUE_VIEWER(Text) : ValueViewer(parent,style) {} - virtual void prepare(RotatedDC& dc); + virtual bool prepare(RotatedDC& dc); virtual void draw(RotatedDC& dc); virtual void onValueChange(); virtual void onStyleChange(); diff --git a/src/render/value/viewer.hpp b/src/render/value/viewer.hpp index 34e31697..a001dc44 100644 --- a/src/render/value/viewer.hpp +++ b/src/render/value/viewer.hpp @@ -40,8 +40,9 @@ class ValueViewer : public StyleListener { inline const ValueP& getValue() const { return valueP; } /// Prepare before drawing. - /** Scripts are updated after preparing, allowing */ - virtual void prepare(RotatedDC& dc) {}; + /** Should return true if a content property has changed + * Scripts are re-updated after preparing if they depend on content properties. */ + virtual bool prepare(RotatedDC& dc) { return false; }; /// Draw this value virtual void draw(RotatedDC& dc) = 0; diff --git a/src/script/script_manager.cpp b/src/script/script_manager.cpp index 973eac54..4ed0f048 100644 --- a/src/script/script_manager.cpp +++ b/src/script/script_manager.cpp @@ -139,6 +139,12 @@ void SetScriptManager::initDependencies(Context& ctx, StyleSheet& stylesheet) { s->checkContentDependencies(ctx, test); if (test.index) s->content_dependent = true; } + FOR_EACH(s, stylesheet.extra_card_style) { + // are there dependencies of this style on other style properties? + Dependency test(DEP_DUMMY, false); + s->checkContentDependencies(ctx, test); + if (test.index) s->content_dependent = true; + } } // ----------------------------------------------------------------------------- : ScriptManager : updating @@ -199,21 +205,24 @@ void SetScriptManager::onAction(const Action& action, bool undone) { } } -void SetScriptManager::updateStyles(const CardP& card) { +void SetScriptManager::updateStyles(const CardP& card, bool only_content_dependent) { assert(card); const StyleSheet& stylesheet = set.stylesheetFor(card); Context& ctx = getContext(card); - // update extra card fields - IndexMap& extra_data = card->extraDataFor(stylesheet); - FOR_EACH(v, extra_data) { - v->update(ctx); + if (!only_content_dependent) { + // update extra card fields + IndexMap& extra_data = card->extraDataFor(stylesheet); + FOR_EACH(v, extra_data) { + v->update(ctx); + } } // update all styles - updateStyles(ctx, stylesheet.card_style); - updateStyles(ctx, stylesheet.extra_card_style); + updateStyles(ctx, stylesheet.card_style, only_content_dependent); + updateStyles(ctx, stylesheet.extra_card_style, only_content_dependent); } -void SetScriptManager::updateStyles(Context& ctx, const IndexMap& styles) { +void SetScriptManager::updateStyles(Context& ctx, const IndexMap& styles, bool only_content_dependent) { FOR_EACH_CONST(s, styles) { + if (only_content_dependent && !s->content_dependent) continue; try { if (s->update(ctx)) { // style has changed, tell listeners diff --git a/src/script/script_manager.hpp b/src/script/script_manager.hpp index 4825215f..26a03f1e 100644 --- a/src/script/script_manager.hpp +++ b/src/script/script_manager.hpp @@ -61,7 +61,7 @@ class SetScriptManager : public SetScriptContext, public ActionListener { ~SetScriptManager(); /// Update all styles for a particular card - void updateStyles(const CardP& card); + void updateStyles(const CardP& card, bool only_content_dependent); /// Update expensive things that were previously delayed void updateDelayed(); @@ -79,7 +79,7 @@ class SetScriptManager : public SetScriptContext, public ActionListener { void initDependencies(Context&, StyleSheet&); /// Update a map of styles - void updateStyles(Context& ctx, const IndexMap& styles); + void updateStyles(Context& ctx, const IndexMap& styles, bool only_content_dependent); /// Updates scripts, starting at some value /** if the value changes any dependend values are updated as well */ void updateValue(Value& value, const CardP& card);