Added value actions for common value types; drop down list is now correctly aligned

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@92 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2006-11-24 16:36:17 +00:00
parent edfe24a05f
commit 0a916f89bc
23 changed files with 268 additions and 66 deletions
+62
View File
@@ -0,0 +1,62 @@
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <data/action/value.hpp>
#include <data/field.hpp>
#include <data/field/text.hpp>
#include <data/field/choice.hpp>
#include <data/field/multiple_choice.hpp>
#include <data/field/color.hpp>
#include <data/field/image.hpp>
#include <data/field/symbol.hpp>
// ----------------------------------------------------------------------------- : ValueAction
String ValueAction::getName(bool to_undo) const {
return _("Change ") + valueP->fieldP->name;
}
// ----------------------------------------------------------------------------- : Simple
/// A ValueAction that swaps between old and new values
template <typename T, typename T::ValueType T::*member, bool ALLOW_MERGE>
class SimpleValueAction : public ValueAction {
public:
inline SimpleValueAction(const shared_ptr<T>& value, const typename T::ValueType& new_value)
: ValueAction(value), new_value(new_value)
{}
virtual void perform(bool to_undo) {
swap(static_cast<T&>(*valueP).*member, new_value);
}
virtual bool merge(const Action* action) {
if (!ALLOW_MERGE) return false;
if (const SimpleValueAction* sva = dynamic_cast<const SimpleValueAction*>(action)) {
if (sva->valueP == valueP) {
// adjacent actions on the same value, discard the other one,
// because it only keeps an intermediate value
return true;
}
} else {
return false;
}
return false;
}
private:
typename T::ValueType new_value;
};
ValueAction* value_action(const ChoiceValueP& value, const Defaultable<String>& new_value) { return new SimpleValueAction<ChoiceValue, &ChoiceValue::value, true> (value, new_value); }
ValueAction* value_action(const ColorValueP& value, const Defaultable<Color>& new_value) { return new SimpleValueAction<ColorValue, &ColorValue::value, true> (value, new_value); }
ValueAction* value_action(const ImageValueP& value, const FileName& new_value) { return new SimpleValueAction<ImageValue, &ImageValue::filename, false>(value, new_value); }
ValueAction* value_action(const SymbolValueP& value, const FileName& new_value) { return new SimpleValueAction<SymbolValue, &SymbolValue::filename, false>(value, new_value); }
// ----------------------------------------------------------------------------- : Text
+72
View File
@@ -0,0 +1,72 @@
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
#ifndef HEADER_DATA_ACTION_VALUE
#define HEADER_DATA_ACTION_VALUE
/** @file data/action/set.hpp
*
* Actions operating on Values (and derived classes, "*Value")
*/
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <util/action_stack.hpp>
#include <util/defaultable.hpp>
DECLARE_POINTER_TYPE(Value);
DECLARE_POINTER_TYPE(TextValue);
DECLARE_POINTER_TYPE(ChoiceValue);
DECLARE_POINTER_TYPE(ColorValue);
DECLARE_POINTER_TYPE(ImageValue);
DECLARE_POINTER_TYPE(SymbolValue);
// ----------------------------------------------------------------------------- : ValueAction (based class)
/// An Action the changes a Value
class ValueAction : public Action {
public:
inline ValueAction(const ValueP& value) : valueP(value) {}
virtual String getName(bool to_undo) const;
const ValueP valueP; ///< The modified value
};
/// Utility macro for declaring classes derived from ValueAction
#define DECLARE_VALUE_ACTION(Type) \
protected: \
inline Type##Value& value() const { \
return static_cast<Type##Value&>(*valueP); \
} \
public: \
virtual void perform(bool to_undo)
// ----------------------------------------------------------------------------- : Simple
ValueAction* value_action(const ChoiceValueP& value, const Defaultable<String>& new_value);
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);
// ----------------------------------------------------------------------------- : Text
/*
class ColorValueAction : public ValueAction {
public:
ColorValueAction(const ColorValueP& value, const Defaultable<Color>& color);
DECLARE_VALUE_ACTION(Color);
private:
Defaultable<Color> color; ///< The new/old color
};
*/
// ----------------------------------------------------------------------------- : EOF
#endif
+2 -1
View File
@@ -143,7 +143,8 @@ class ChoiceValue : public Value {
{}
DECLARE_HAS_FIELD(Choice)
Defaultable<String> value; /// The name of the selected choice
typedef Defaultable<String> ValueType;
ValueType value; /// The name of the selected choice
virtual String toString() const;
virtual bool update(Context&);
+2 -1
View File
@@ -76,7 +76,8 @@ class ColorValue : public Value {
inline ColorValue(const ColorFieldP& field) : Value(field) {}
DECLARE_HAS_FIELD(Color)
Defaultable<Color> value; ///< The value
typedef Defaultable<Color> ValueType;
ValueType value; ///< The value
virtual String toString() const;
virtual bool update(Context&);
+2 -1
View File
@@ -50,7 +50,8 @@ class ImageValue : public Value {
public:
inline ImageValue(const ImageFieldP& field) : Value(field) {}
FileName filename; ///< Filename of the image (in the current package), or ""
typedef FileName ValueType;
ValueType filename; ///< Filename of the image (in the current package), or ""
virtual String toString() const;
+2 -1
View File
@@ -65,7 +65,8 @@ class SymbolValue : public Value {
inline SymbolValue(const SymbolFieldP& field) : Value(field) {}
DECLARE_HAS_FIELD(Symbol)
FileName filename; ///< Filename of the symbol (in the current package)
typedef FileName ValueType;
ValueType filename; ///< Filename of the symbol (in the current package)
virtual String toString() const;
+2 -1
View File
@@ -80,7 +80,8 @@ class TextValue : public Value {
inline TextValue(const TextFieldP& field) : Value(field) {}
DECLARE_HAS_FIELD(Text)
Defaultable<String> value; ///< The text of this value
typedef Defaultable<String> ValueType;
ValueType value; ///< The text of this value
virtual String toString() const;
virtual bool update(Context&);