Allow multiple cards to be selected in a card list,

Allow card actions to add/remove multiple cards.
not yet: actually use these multicard actions.

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@853 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2008-05-17 01:51:50 +00:00
parent 1b516e781f
commit c666e98645
14 changed files with 145 additions and 149 deletions
+15 -21
View File
@@ -19,40 +19,34 @@ DECLARE_TYPEOF_COLLECTION(KeywordModeP);
// ----------------------------------------------------------------------------- : Add Keyword
AddKeywordAction::AddKeywordAction(Adding, Set& set, const KeywordP& keyword)
: KeywordListAction(set), adding(true), keyword(keyword ? keyword : new_intrusive<Keyword>())
, keyword_id(set.keywords.size())
AddKeywordAction::AddKeywordAction(Set& set)
: KeywordListAction(set)
, action(ADD, new_intrusive<Keyword>(), set.keywords)
{
Keyword& keyword = *action.steps.front().item;
// find default mode
FOR_EACH(mode, set.game->keyword_modes) {
if (mode->is_default) {
this->keyword->mode = mode->name;
keyword.mode = mode->name;
break;
}
}
}
AddKeywordAction::AddKeywordAction(Removing, Set& set, const KeywordP& keyword)
: KeywordListAction(set), adding(false), keyword(keyword)
// find the keyword_id of the keyword we want to remove
, keyword_id(find(set.keywords.begin(), set.keywords.end(), keyword) - set.keywords.begin())
{
if (keyword_id >= set.keywords.size()) {
throw InternalError(_("Keyword to remove not found in set"));
}
}
AddKeywordAction::AddKeywordAction(AddingOrRemoving ar, Set& set, const KeywordP& keyword)
: KeywordListAction(set)
, action(ar, keyword, set.keywords)
{}
/*AddKeywordAction::AddKeywordAction(AddingOrRemoving ar, Set& set, const vector<KeywordP>& keyword)
: KeywordListAction(set)
, action(ar, keywords, set.keywords)
{}*/
String AddKeywordAction::getName(bool to_undo) const {
return adding ? _("Add keyword") : _("Remove keyword");
return action.getName();
}
void AddKeywordAction::perform(bool to_undo) {
if (adding != to_undo) {
assert(keyword_id <= set.keywords.size());
set.keywords.insert(set.keywords.begin() + keyword_id, keyword);
} else {
assert(keyword_id < set.keywords.size());
set.keywords.erase(set.keywords.begin() + keyword_id);
}
action.perform(set.keywords, to_undo);
set.keyword_db.clear();
}
+5 -9
View File
@@ -18,9 +18,11 @@
#include <util/action_stack.hpp>
#include <util/error.hpp>
#include <data/field/text.hpp>
#include <data/action/generic.hpp>
class Set;
DECLARE_POINTER_TYPE(Keyword);
DECLARE_TYPEOF_COLLECTION(GenericAddAction<KeywordP>::Step);
// ----------------------------------------------------------------------------- : Add Keyword
@@ -34,22 +36,16 @@ class KeywordListAction : public Action {
// therefore we don't need a smart pointer
};
enum Adding {ADD};
enum Removing {REMOVE};
/// Adding or removing a keyword from a set
class AddKeywordAction : public KeywordListAction {
public:
AddKeywordAction(Adding, Set& set, const KeywordP& keyword = KeywordP());
AddKeywordAction(Removing, Set& set, const KeywordP& keyword);
AddKeywordAction(Set& set);
AddKeywordAction(AddingOrRemoving, Set& set, const KeywordP& keyword);
virtual String getName(bool to_undo) const;
virtual void perform(bool to_undo);
//private:
const bool adding; ///< Was a keyword added? (as opposed to removed)
const KeywordP keyword; ///< The new/removed keyword
const size_t keyword_id; ///< Position of the keyword in the set
const GenericAddAction<KeywordP> action;
};
// ----------------------------------------------------------------------------- : Changing keywords
+12 -37
View File
@@ -18,51 +18,26 @@ DECLARE_TYPEOF_COLLECTION(IndexMap<FieldP COMMA ValueP>);
// ----------------------------------------------------------------------------- : Add card
AddCardAction::AddCardAction(Set& set)
: CardListAction(set), card(new Card(*set.game))
: CardListAction(set)
, action(ADD, new_intrusive1<Card>(*set.game), set.cards)
{}
AddCardAction::AddCardAction(Set& set, const CardP& card)
: CardListAction(set), card(card)
AddCardAction::AddCardAction(AddingOrRemoving ar, Set& set, const CardP& card)
: CardListAction(set)
, action(ar, card, set.cards)
{}
AddCardAction::AddCardAction(AddingOrRemoving ar, Set& set, const vector<CardP>& cards)
: CardListAction(set)
, action(ar, cards, set.cards)
{}
String AddCardAction::getName(bool to_undo) const {
return _("Add card");
return action.getName();
}
void AddCardAction::perform(bool to_undo) {
if (!to_undo) {
set.cards.push_back(card);
} else {
assert(!set.cards.empty());
set.cards.pop_back();
}
}
// ----------------------------------------------------------------------------- : Remove card
RemoveCardAction::RemoveCardAction(Set& set, const CardP& card)
: CardListAction(set), card(card)
// find the card_id of the card we want to remove
, card_id(find(set.cards.begin(), set.cards.end(), card) - set.cards.begin())
{
if (card_id >= set.cards.size()) {
throw InternalError(_("Card to remove not found in set"));
}
}
String RemoveCardAction::getName(bool to_undo) const {
return _("Remove card");
}
void RemoveCardAction::perform(bool to_undo) {
if (!to_undo) {
assert(card_id < set.cards.size());
set.cards.erase(set.cards.begin() + card_id);
} else {
assert(card_id <= set.cards.size());
set.cards.insert(set.cards.begin() + card_id, card);
}
action.perform(set.cards, to_undo);
}
+6 -18
View File
@@ -16,12 +16,14 @@
#include <util/prec.hpp>
#include <util/action_stack.hpp>
#include <data/action/generic.hpp>
class Set;
DECLARE_POINTER_TYPE(Card);
DECLARE_POINTER_TYPE(StyleSheet);
DECLARE_POINTER_TYPE(Field);
DECLARE_POINTER_TYPE(Value);
DECLARE_TYPEOF_COLLECTION(GenericAddAction<CardP>::Step);
// ----------------------------------------------------------------------------- : Add card
@@ -38,29 +40,15 @@ class CardListAction : public Action {
/// Adding a new card to a set
class AddCardAction : public CardListAction {
public:
/// Add a newly allocated card
AddCardAction(Set& set);
AddCardAction(Set& set, const CardP& card);
AddCardAction(AddingOrRemoving, Set& set, const CardP& card);
AddCardAction(AddingOrRemoving, Set& set, const vector<CardP>& cards);
virtual String getName(bool to_undo) const;
virtual void perform(bool to_undo);
//private:
const CardP card; ///< The new card
};
// ----------------------------------------------------------------------------- : Remove card
/// Removing a card from a set
class RemoveCardAction : public CardListAction {
public:
RemoveCardAction(Set& set, const CardP& card);
virtual String getName(bool to_undo) const;
virtual void perform(bool to_undo);
//private:
const CardP card; ///< The removed card
const size_t card_id; ///< Position of the card in the set
const GenericAddAction<CardP> action;
};
// ----------------------------------------------------------------------------- : Reorder cards