Commit 0e760ba0 authored by twanvl's avatar twanvl

SimpleValueAction is no longer a template class, and is now used as base for...

SimpleValueAction is no longer a template class, and is now used as base for MultipleChoiceValueAction and TextValueAction.
parent aa451d3a
...@@ -38,73 +38,45 @@ void ValueAction::isOnCard(Card* card) { ...@@ -38,73 +38,45 @@ void ValueAction::isOnCard(Card* card) {
// ----------------------------------------------------------------------------- : Simple // ----------------------------------------------------------------------------- : Simple
/// Swap the value in a Value object with a new one void SimpleValueAction::perform(bool to_undo) {
inline void swap_value(Value& a, ScriptValueP& b) { swap(a.value, b); } ValueAction::perform(to_undo);
swap(valueP->value, new_value);
/// A ValueAction that swaps between old and new values valueP->onAction(*this, to_undo); // notify value
template <typename T, bool ALLOW_MERGE> }
class SimpleValueAction : public ValueAction {
public: bool SimpleValueAction::merge(const Action& action) {
inline SimpleValueAction(const intrusive_ptr<T>& value, const typename T::ValueType& new_value) if (!allow_merge) return false;
: ValueAction(value), new_value(new_value) TYPE_CASE(action, SimpleValueAction) {
{} if (action.valueP == valueP) {
// adjacent actions on the same value, discard the other one,
virtual void perform(bool to_undo) { // because it only keeps an intermediate value
ValueAction::perform(to_undo); return true;
swap_value(static_cast<T&>(*valueP), new_value);
valueP->onAction(*this, to_undo); // notify value
}
virtual bool merge(const Action& action) {
if (!ALLOW_MERGE) return false;
TYPE_CASE(action, SimpleValueAction) {
if (action.valueP == valueP) {
// adjacent actions on the same value, discard the other one,
// because it only keeps an intermediate value
return true;
}
} }
return false;
} }
return false;
private: }
typename T::ValueType new_value;
};
ValueAction* value_action(const ValueP& value, const ScriptValueP& new_value) { return new SimpleValueAction<Value,false>(value, new_value); }
// ----------------------------------------------------------------------------- : MultipleChoice ValueAction* value_action(const ValueP& value, const ScriptValueP& new_value) {
return new SimpleValueAction(value, new_value);
}
ValueAction* value_action(const MultipleChoiceValueP& value, const ScriptValueP& new_value, const String& last_change) { ValueAction* value_action(const MultipleChoiceValueP& value, const ScriptValueP& new_value, const String& last_change) {
return new MultipleChoiceValueAction(value,new_value,last_change); return new MultipleChoiceValueAction(value,new_value,last_change);
} }
// copy paste of SimpleValueAction :(
// TODO: do this in a better way
void MultipleChoiceValueAction::perform(bool to_undo) {
ValueAction::perform(to_undo);
swap_value(static_cast<MultipleChoiceValue&>(*valueP), new_value);
valueP->onAction(*this, to_undo); // notify value
}
// ----------------------------------------------------------------------------- : Text // ----------------------------------------------------------------------------- : Text
TextValueAction::TextValueAction(const TextValueP& value, size_t start, size_t end, size_t new_end, const ScriptValueP& new_value, const String& name) TextValueAction::TextValueAction(const TextValueP& value, size_t start, size_t end, size_t new_end, const ScriptValueP& new_value, const String& name)
: ValueAction(value) : SimpleValueAction(value, new_value)
, selection_start(start), selection_end(end), new_selection_end(new_end) , selection_start(start), selection_end(end), new_selection_end(new_end)
, new_value(new_value)
, name(name) , name(name)
{} {}
String TextValueAction::getName(bool to_undo) const { return name; } String TextValueAction::getName(bool to_undo) const { return name; }
void TextValueAction::perform(bool to_undo) { void TextValueAction::perform(bool to_undo) {
ValueAction::perform(to_undo);
swap_value(value(), new_value);
swap(selection_end, new_selection_end); swap(selection_end, new_selection_end);
valueP->onAction(*this, to_undo); // notify value SimpleValueAction::perform(to_undo);
} }
bool TextValueAction::merge(const Action& action) { bool TextValueAction::merge(const Action& action) {
......
...@@ -52,20 +52,33 @@ class ValueAction : public Action { ...@@ -52,20 +52,33 @@ class ValueAction : public Action {
// ----------------------------------------------------------------------------- : Simple // ----------------------------------------------------------------------------- : Simple
/// A ValueAction that swaps between old and new values
class SimpleValueAction : public ValueAction {
public:
inline SimpleValueAction(const ValueP& value, const ScriptValueP& new_value, bool allow_merge = false)
: ValueAction(value), new_value(new_value), allow_merge(allow_merge)
{}
virtual void perform(bool to_undo);
virtual bool merge(const Action& action);
protected:
ScriptValueP new_value;
bool allow_merge;
};
/// Action that updates a Value to a new value /// Action that updates a Value to a new value
ValueAction* value_action(const ValueP& value, const ScriptValueP& new_value); ValueAction* value_action(const ValueP& value, const ScriptValueP& new_value);
ValueAction* value_action(const MultipleChoiceValueP& value, const ScriptValueP& new_value, const String& last_change); ValueAction* value_action(const MultipleChoiceValueP& value, const ScriptValueP& new_value, const String& last_change);
// ----------------------------------------------------------------------------- : MultipleChoice // ----------------------------------------------------------------------------- : MultipleChoice
class MultipleChoiceValueAction : public ValueAction { class MultipleChoiceValueAction : public SimpleValueAction {
public: public:
inline MultipleChoiceValueAction(const ValueP& value, const ScriptValueP& new_value, const String& changed_choice) inline MultipleChoiceValueAction(const ValueP& value, const ScriptValueP& new_value, const String& changed_choice)
: ValueAction(value), changed_choice(changed_choice), new_value(new_value) : SimpleValueAction(value, new_value), changed_choice(changed_choice)
{} {}
virtual void perform(bool to_undo);
const String changed_choice; ///< What choice was toggled by this action (if any) const String changed_choice; ///< What choice was toggled by this action (if any)
private: private:
ScriptValueP new_value; ScriptValueP new_value;
...@@ -74,7 +87,7 @@ class MultipleChoiceValueAction : public ValueAction { ...@@ -74,7 +87,7 @@ class MultipleChoiceValueAction : public ValueAction {
// ----------------------------------------------------------------------------- : Text // ----------------------------------------------------------------------------- : Text
/// An action that changes a TextValue /// An action that changes a TextValue
class TextValueAction : public ValueAction { class TextValueAction : public SimpleValueAction {
public: public:
TextValueAction(const TextValueP& value, size_t start, size_t end, size_t new_end, const ScriptValueP& new_value, const String& name); TextValueAction(const TextValueP& value, size_t start, size_t end, size_t new_end, const ScriptValueP& new_value, const String& name);
...@@ -90,7 +103,6 @@ class TextValueAction : public ValueAction { ...@@ -90,7 +103,6 @@ class TextValueAction : public ValueAction {
inline TextValue& value() const; inline TextValue& value() const;
size_t new_selection_end; size_t new_selection_end;
ScriptValueP new_value;
String name; String name;
}; };
...@@ -117,28 +129,6 @@ class TextToggleReminderAction : public ValueAction { ...@@ -117,28 +129,6 @@ class TextToggleReminderAction : public ValueAction {
Char old; ///< Old value of the <kw- tag Char old; ///< Old value of the <kw- tag
}; };
// ----------------------------------------------------------------------------- : Replace all
/// A TextValueAction without the start and end stuff
class SimpleTextValueAction : public ValueAction {
public:
SimpleTextValueAction(const Card* card, const TextValueP& value, const Defaultable<String>& new_value);
virtual void perform(bool to_undo);
bool merge(const SimpleTextValueAction& action);
private:
Defaultable<String> new_value;
};
/// An action from "Replace All"; just a bunch of value actions performed in sequence
class ReplaceAllAction : public Action {
public:
~ReplaceAllAction();
virtual String getName(bool to_undo) const;
virtual void perform(bool to_undo);
vector<SimpleTextValueAction> actions;
};
// ----------------------------------------------------------------------------- : Event // ----------------------------------------------------------------------------- : Event
......
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