Reduce coupling between ValueEditors/Viewers and Set and StyleSheet.

- Adding of actions is done with an addAction function
 - Files are read from
     - getStylePackage for styling stuff (this is stylesheet)
     - getLocalPackage for symbol and image values (this was the set)


git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@970 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2008-06-04 00:21:06 +00:00
parent 6b0a0fd098
commit ae14784fd6
50 changed files with 413 additions and 184 deletions
+39 -18
View File
@@ -17,6 +17,7 @@
#include <data/field/symbol.hpp>
#include <data/field/package_choice.hpp>
#include <util/tagged_string.hpp>
#include <data/set.hpp> // for ValueActionPerformer
// ----------------------------------------------------------------------------- : ValueAction
@@ -24,6 +25,10 @@ String ValueAction::getName(bool to_undo) const {
return _ACTION_1_("change", valueP->fieldP->name);
}
void ValueAction::isOnCard(Card* card) {
const_cast<ValueAction*>(this)->card = card;
}
// ----------------------------------------------------------------------------- : Simple
/// Swap the value in a Value object with a new one
@@ -42,8 +47,8 @@ inline void swap_value(MultipleChoiceValue& a, MultipleChoiceValue::ValueType& b
template <typename T, bool ALLOW_MERGE>
class SimpleValueAction : public ValueAction {
public:
inline SimpleValueAction(const Card* card, const intrusive_ptr<T>& value, const typename T::ValueType& new_value)
: ValueAction(card, value), new_value(new_value)
inline SimpleValueAction(const intrusive_ptr<T>& value, const typename T::ValueType& new_value)
: ValueAction(value), new_value(new_value)
{}
virtual void perform(bool to_undo) {
@@ -67,21 +72,21 @@ class SimpleValueAction : public ValueAction {
typename T::ValueType new_value;
};
ValueAction* value_action(const Card* card, const ChoiceValueP& value, const Defaultable<String>& new_value) { return new SimpleValueAction<ChoiceValue, true> (card, value, new_value); }
ValueAction* value_action(const Card* card, const ColorValueP& value, const Defaultable<Color>& new_value) { return new SimpleValueAction<ColorValue, true> (card, value, new_value); }
ValueAction* value_action(const Card* card, const ImageValueP& value, const FileName& new_value) { return new SimpleValueAction<ImageValue, false>(card, value, new_value); }
ValueAction* value_action(const Card* card, const SymbolValueP& value, const FileName& new_value) { return new SimpleValueAction<SymbolValue, false>(card, value, new_value); }
ValueAction* value_action(const Card* card, const PackageChoiceValueP& value, const String& new_value) { return new SimpleValueAction<PackageChoiceValue, false>(card, value, new_value); }
ValueAction* value_action(const Card* card, const MultipleChoiceValueP& value, const Defaultable<String>& new_value, const String& last_change) {
ValueAction* value_action(const ChoiceValueP& value, const Defaultable<String>& new_value) { return new SimpleValueAction<ChoiceValue, true> (value, new_value); }
ValueAction* value_action(const ColorValueP& value, const Defaultable<Color>& new_value) { return new SimpleValueAction<ColorValue, true> (value, new_value); }
ValueAction* value_action(const ImageValueP& value, const FileName& new_value) { return new SimpleValueAction<ImageValue, false>(value, new_value); }
ValueAction* value_action(const SymbolValueP& value, const FileName& new_value) { return new SimpleValueAction<SymbolValue, false>(value, new_value); }
ValueAction* value_action(const PackageChoiceValueP& value, const String& new_value) { return new SimpleValueAction<PackageChoiceValue, false>(value, new_value); }
ValueAction* value_action(const MultipleChoiceValueP& value, const Defaultable<String>& new_value, const String& last_change) {
MultipleChoiceValue::ValueType v = { new_value, last_change };
return new SimpleValueAction<MultipleChoiceValue, false>(card, value, v);
return new SimpleValueAction<MultipleChoiceValue, false>(value, v);
}
// ----------------------------------------------------------------------------- : Text
TextValueAction::TextValueAction(const Card* card, const TextValueP& value, size_t start, size_t end, size_t new_end, const Defaultable<String>& new_value, const String& name)
: ValueAction(card, value)
TextValueAction::TextValueAction(const TextValueP& value, size_t start, size_t end, size_t new_end, const Defaultable<String>& new_value, const String& name)
: ValueAction(value)
, selection_start(start), selection_end(end), new_selection_end(new_end)
, new_value(new_value)
, name(name)
@@ -111,7 +116,7 @@ TextValue& TextValueAction::value() const {
}
TextValueAction* toggle_format_action(const Card* card, const TextValueP& value, const String& tag, size_t start_i, size_t end_i, size_t start, size_t end, const String& action_name) {
TextValueAction* toggle_format_action(const TextValueP& value, const String& tag, size_t start_i, size_t end_i, size_t start, size_t end, const String& action_name) {
if (start > end) {
swap(start, end);
swap(start_i, end_i);
@@ -144,11 +149,11 @@ TextValueAction* toggle_format_action(const Card* card, const TextValueP& value,
if (value->value() == new_value) {
return nullptr; // no changes
} else {
return new TextValueAction(card, value, start, end, end, new_value, action_name);
return new TextValueAction(value, start, end, end, new_value, action_name);
}
}
TextValueAction* typing_action(const Card* card, const TextValueP& value, size_t start_i, size_t end_i, size_t start, size_t end, const String& replacement, const String& action_name) {
TextValueAction* typing_action(const TextValueP& value, size_t start_i, size_t end_i, size_t start, size_t end, const String& replacement, const String& action_name) {
bool reverse = start > end;
if (reverse) {
swap(start, end);
@@ -160,17 +165,17 @@ TextValueAction* typing_action(const Card* card, const TextValueP& value, size_t
return nullptr;
} else {
if (reverse) {
return new TextValueAction(card, value, end, start, start+untag(replacement).size(), new_value, action_name);
return new TextValueAction(value, end, start, start+untag(replacement).size(), new_value, action_name);
} else {
return new TextValueAction(card, value, start, end, start+untag(replacement).size(), new_value, action_name);
return new TextValueAction(value, start, end, start+untag(replacement).size(), new_value, action_name);
}
}
}
// ----------------------------------------------------------------------------- : Reminder text
TextToggleReminderAction::TextToggleReminderAction(const Card* card, const TextValueP& value, size_t pos_in)
: ValueAction(card, value)
TextToggleReminderAction::TextToggleReminderAction(const TextValueP& value, size_t pos_in)
: ValueAction(value)
{
pos = in_tag(value->value(), _("<kw-"), pos_in, pos_in);
if (pos == String::npos) {
@@ -217,3 +222,19 @@ String ScriptStyleEvent::getName(bool) const {
void ScriptStyleEvent::perform(bool) {
assert(false); // this action is just an event, it should not be performed
}
// ----------------------------------------------------------------------------- : Action performer
ValueActionPerformer::ValueActionPerformer(const ValueP& value, Card* card, const SetP& set)
: value(value), card(card), set(set)
{}
ValueActionPerformer::~ValueActionPerformer() {}
void ValueActionPerformer::addAction(ValueAction* action) {
action->isOnCard(card);
set->actions.addAction(action);
}
Package& ValueActionPerformer::getLocalPackage() {
return *set;
}
+34 -11
View File
@@ -20,6 +20,7 @@
class Card;
class StyleSheet;
DECLARE_POINTER_TYPE(Set);
DECLARE_POINTER_TYPE(Value);
DECLARE_POINTER_TYPE(Style);
DECLARE_POINTER_TYPE(TextValue);
@@ -35,10 +36,13 @@ DECLARE_POINTER_TYPE(PackageChoiceValue);
/// An Action the changes a Value
class ValueAction : public Action {
public:
inline ValueAction(const Card* card, const ValueP& value) : valueP(value), card(card) {}
inline ValueAction(const ValueP& value) : valueP(value), card(nullptr) {}
virtual String getName(bool to_undo) const;
/// We know that the value is on the given card, add that information
void isOnCard(Card* card);
const ValueP valueP; ///< The modified value
const Card* card; ///< The card the value is on, or null if it is not a card value
};
@@ -46,19 +50,19 @@ class ValueAction : public Action {
// ----------------------------------------------------------------------------- : Simple
/// Action that updates a Value to a new value
ValueAction* value_action(const Card* card, const ChoiceValueP& value, const Defaultable<String>& new_value);
ValueAction* value_action(const Card* card, const MultipleChoiceValueP& value, const Defaultable<String>& new_value, const String& last_change);
ValueAction* value_action(const Card* card, const ColorValueP& value, const Defaultable<Color>& new_value);
ValueAction* value_action(const Card* card, const ImageValueP& value, const FileName& new_value);
ValueAction* value_action(const Card* card, const SymbolValueP& value, const FileName& new_value);
ValueAction* value_action(const Card* card, const PackageChoiceValueP& value, const String& new_value);
ValueAction* value_action(const ChoiceValueP& value, const Defaultable<String>& new_value);
ValueAction* value_action(const MultipleChoiceValueP& value, const Defaultable<String>& new_value, const String& last_change);
ValueAction* value_action(const ColorValueP& value, const Defaultable<Color>& new_value);
ValueAction* value_action(const ImageValueP& value, const FileName& new_value);
ValueAction* value_action(const SymbolValueP& value, const FileName& new_value);
ValueAction* value_action(const PackageChoiceValueP& value, const String& new_value);
// ----------------------------------------------------------------------------- : Text
/// An action that changes a TextValue
class TextValueAction : public ValueAction {
public:
TextValueAction(const Card* card, const TextValueP& value, size_t start, size_t end, size_t new_end, const Defaultable<String>& new_value, const String& name);
TextValueAction(const TextValueP& value, size_t start, size_t end, size_t new_end, const Defaultable<String>& new_value, const String& name);
virtual String getName(bool to_undo) const;
virtual void perform(bool to_undo);
@@ -77,18 +81,18 @@ class TextValueAction : public ValueAction {
};
/// Action for toggling some formating tag on or off in some range
TextValueAction* toggle_format_action(const Card* card, const TextValueP& value, const String& tag, size_t start_i, size_t end_i, size_t start, size_t end, const String& action_name);
TextValueAction* toggle_format_action(const TextValueP& value, const String& tag, size_t start_i, size_t end_i, size_t start, size_t end, const String& action_name);
/// Typing in a TextValue, replace the selection [start...end) with replacement
/** start and end are cursor positions, start_i and end_i are indices*/
TextValueAction* typing_action(const Card* card, const TextValueP& value, size_t start_i, size_t end_i, size_t start, size_t end, const String& replacement, const String& action_name);
TextValueAction* typing_action(const TextValueP& value, size_t start_i, size_t end_i, size_t start, size_t end, const String& replacement, const String& action_name);
// ----------------------------------------------------------------------------- : Reminder text
/// Toggle reminder text for a keyword on or off
class TextToggleReminderAction : public ValueAction {
public:
TextToggleReminderAction(const Card* card, const TextValueP& value, size_t pos);
TextToggleReminderAction(const TextValueP& value, size_t pos);
virtual String getName(bool to_undo) const;
virtual void perform(bool to_undo);
@@ -150,5 +154,24 @@ class ScriptStyleEvent : public Action {
const Style* style; ///< The modified style
};
// ----------------------------------------------------------------------------- : Action performer
/// A loose object for performing ValueActions on a certain value.
/** Used to reduce coupling */
class ValueActionPerformer {
public:
ValueActionPerformer(const ValueP& value, Card* card, const SetP& set);
~ValueActionPerformer();
/// Perform an action. The performer takes ownerwhip of the action.
void addAction(ValueAction* action);
const ValueP value; ///< The value
Package& getLocalPackage();
private:
Card* card; ///< Card the value is on (if any)
SetP set; ///< Set for the actions
};
// ----------------------------------------------------------------------------- : EOF
#endif
+1
View File
@@ -96,6 +96,7 @@ String tr(LocaleCategory cat, const String& key, DefaultLocaleFun def) {
return loc->tr(subcat, key, def); \
}
IMPLEMENT_TR_TYPE(Package, game_translations) //%% TODO!
IMPLEMENT_TR_TYPE(Game, game_translations)
IMPLEMENT_TR_TYPE(StyleSheet, stylesheet_translations)
IMPLEMENT_TR_TYPE(SymbolFont, symbol_font_translations)
+1 -1
View File
@@ -149,7 +149,7 @@ class SetView : public ActionListener {
~SetView();
/// Get the set that is currently being viewed
inline SetP getSet() { return set; }
//inline SetP getSet() const { return set; }
/// Change the set that is being viewed
void setSet(const SetP& set);