From e3cdb2bea71ed03285a2c1c696534469bdfe93d8 Mon Sep 17 00:00:00 2001 From: twanvl Date: Sat, 10 Jan 2009 01:37:03 +0000 Subject: [PATCH] adding & editing custom pack types now works. git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1328 0fc631ac-6414-0410-93d0-97cfa31319b6 --- data/en.mse-locale/locale | 7 ++++ src/data/action/set.cpp | 31 ++++++++++++++++ src/data/action/set.hpp | 40 ++++++++++++++++++++ src/data/game.cpp | 2 +- src/data/pack.cpp | 14 +++++++ src/data/pack.hpp | 3 ++ src/data/set.cpp | 1 + src/data/set.hpp | 19 ++++++---- src/gui/set/random_pack_panel.cpp | 47 ++++++++++++++++++------ src/gui/set/random_pack_panel.hpp | 3 +- src/resource/common/expected_locale_keys | 8 +++- 11 files changed, 152 insertions(+), 23 deletions(-) diff --git a/data/en.mse-locale/locale b/data/en.mse-locale/locale index 5702e12f..34f656b6 100644 --- a/data/en.mse-locale/locale +++ b/data/en.mse-locale/locale @@ -201,6 +201,8 @@ help: random seed: Different packs will be generated each time. fixed seed: Using the same seed number gives the same 'random' packs. seed: Seed number for the random generator. Using the same seed number gives the same 'random' packs. + edit pack type: Double click to edit pack type + number of packs: The number of %ss to generate # Preferences app language: @@ -426,6 +428,7 @@ label: # Random pack panel pack selection: Pack selection pack totals: Totals + pack name: Pack name seed: Seed total cards: Total @@ -552,6 +555,7 @@ button: generate pack: &Generate Pack random seed: &Random Seed fixed seed: &Fixed Seed + add custom pack: Add &Custom Pack... # Welcome new set: New set @@ -641,6 +645,8 @@ title: select cards export:Select Cards to Export # slice slice image: Slice Image + # pack + custom pack: Custom Pack Type # print print preview: Print Preview # export @@ -824,6 +830,7 @@ type: value: value keyword: keyword keywords: keywords + pack: pack type # Symbol editor shapes shape: shape diff --git a/src/data/action/set.cpp b/src/data/action/set.cpp index e2d6bf3b..fb82fa9a 100644 --- a/src/data/action/set.cpp +++ b/src/data/action/set.cpp @@ -10,11 +10,13 @@ #include #include #include +#include #include #include DECLARE_TYPEOF_COLLECTION(IndexMap); DECLARE_TYPEOF_COLLECTION(CardP); +DECLARE_TYPEOF_COLLECTION(PackTypeP); DECLARE_TYPEOF_COLLECTION(int); // ----------------------------------------------------------------------------- : Add card @@ -135,3 +137,32 @@ void ChangeCardHasStylingAction::perform(bool to_undo) { card->has_styling = !card->has_styling; swap(card->styling_data, styling_data); } + +// ----------------------------------------------------------------------------- : Pack types + +AddPackAction::AddPackAction(AddingOrRemoving ar, Set& set, const PackTypeP& pack) + : PackTypesAction(set) + , action(ar, pack, set.pack_types) +{} + +String AddPackAction::getName(bool to_undo) const { + return action.getName(); +} + +void AddPackAction::perform(bool to_undo) { + action.perform(set.pack_types, to_undo); +} + + +ChangePackAction::ChangePackAction(Set& set, size_t pos, const PackTypeP& pack) + : PackTypesAction(set) + , pos(pos), pack(pack) +{} + +String ChangePackAction::getName(bool to_undo) const { + return _ACTION_1_("change",type_name(pack)); +} + +void ChangePackAction::perform(bool to_undo) { + swap(set.pack_types.at(pos), pack); +} diff --git a/src/data/action/set.hpp b/src/data/action/set.hpp index 1ee75d56..b91d0367 100644 --- a/src/data/action/set.hpp +++ b/src/data/action/set.hpp @@ -23,7 +23,9 @@ DECLARE_POINTER_TYPE(Card); DECLARE_POINTER_TYPE(StyleSheet); DECLARE_POINTER_TYPE(Field); DECLARE_POINTER_TYPE(Value); +DECLARE_POINTER_TYPE(PackType); DECLARE_TYPEOF_COLLECTION(GenericAddAction::Step); +DECLARE_TYPEOF_COLLECTION(GenericAddAction::Step); // ----------------------------------------------------------------------------- : Add card @@ -119,5 +121,43 @@ class ChangeCardHasStylingAction : public DisplayChangeAction { IndexMap styling_data; ///< The old styling of the card }; +// ----------------------------------------------------------------------------- : Pack types + +/// An Action the changes the pack types of a set +class PackTypesAction : public Action { + public: + inline PackTypesAction(Set& set) : set(set) {} + + protected: + Set& set; // the set owns this action, so the set will not be destroyed before this + // therefore we don't need a smart pointer +}; + +/// Adding/removing a pack from a Set +class AddPackAction : public PackTypesAction { + public: + /// Add a newly allocated card + AddPackAction(AddingOrRemoving, Set& set, const PackTypeP& pack); + + virtual String getName(bool to_undo) const; + virtual void perform(bool to_undo); + + const GenericAddAction action; +}; + +/// Updating a pack in a Set +class ChangePackAction : public PackTypesAction { + public: + /// Add a newly allocated card + ChangePackAction(Set& set, size_t pos, const PackTypeP& new_pack); + + virtual String getName(bool to_undo) const; + virtual void perform(bool to_undo); + + private: + PackTypeP pack; + size_t pos; +}; + // ----------------------------------------------------------------------------- : EOF #endif diff --git a/src/data/game.cpp b/src/data/game.cpp index 9d9b4992..522bae34 100644 --- a/src/data/game.cpp +++ b/src/data/game.cpp @@ -55,7 +55,7 @@ IMPLEMENT_REFLECTION(Game) { REFLECT_NO_SCRIPT(statistics_dimensions); REFLECT_NO_SCRIPT(statistics_categories); #if USE_NEW_PACK_SYSTEM - REFLECT_ALIAS(307, "pack item", "pack type"); + REFLECT_ALIAS(308, "pack item", "pack type"); #else REFLECT_NO_SCRIPT(pack_items); #endif diff --git a/src/data/pack.cpp b/src/data/pack.cpp index e128534d..ad111969 100644 --- a/src/data/pack.cpp +++ b/src/data/pack.cpp @@ -454,6 +454,13 @@ PackInstance& PackGenerator::get(const String& name) { if (instance) { return *instance; } else { + FOR_EACH_CONST(type, set->pack_types) { + if (type->name == name) { + instance = PackInstanceP(new PackInstance(*type,*this)); + max_depth = max(max_depth, instance->get_depth()); + return *instance; + } + } FOR_EACH_CONST(type, set->game->pack_types) { if (type->name == name) { instance = PackInstanceP(new PackInstance(*type,*this)); @@ -481,6 +488,13 @@ void PackGenerator::generate(vector& out) { i.generate(&out); } } + // ...and then set file order + FOR_EACH_CONST(type, set->pack_types) { + PackInstance& i = get(type); + if (i.get_depth() == depth) { + i.generate(&out); + } + } } } diff --git a/src/data/pack.hpp b/src/data/pack.hpp index 5883cab0..6db30af8 100644 --- a/src/data/pack.hpp +++ b/src/data/pack.hpp @@ -166,6 +166,9 @@ class PackItem : public IntrusivePtrBase { DECLARE_REFLECTION(); }; +inline String type_name(const PackType&) { + return _TYPE_("pack"); +} // ----------------------------------------------------------------------------- : Generating / counting diff --git a/src/data/set.cpp b/src/data/set.cpp index 35456567..23af30ec 100644 --- a/src/data/set.cpp +++ b/src/data/set.cpp @@ -181,6 +181,7 @@ IMPLEMENT_REFLECTION(Set) { } REFLECT(cards); REFLECT(keywords); + REFLECT(pack_types); } reflect_set_info_get_member(tag,data); REFLECT(apprentice_code); diff --git a/src/data/set.hpp b/src/data/set.hpp index 0b29c30f..0b24e2e4 100644 --- a/src/data/set.hpp +++ b/src/data/set.hpp @@ -25,6 +25,7 @@ DECLARE_POINTER_TYPE(Styling); DECLARE_POINTER_TYPE(Field); DECLARE_POINTER_TYPE(Value); DECLARE_POINTER_TYPE(Keyword); +DECLARE_POINTER_TYPE(PackType); DECLARE_POINTER_TYPE(ScriptValue); class SetScriptManager; class SetScriptContext; @@ -45,19 +46,21 @@ class Set : public Packaged { /// Create a set using the given stylesheet, and its game Set(const StyleSheetP& stylesheet); ~Set(); - - GameP game; ///< The game this set uses - StyleSheetP stylesheet; ///< The default stylesheet + + GameP game; ///< The game this set uses + StyleSheetP stylesheet; ///< The default stylesheet /// The values on the fields of the set /** The indices should correspond to the set_fields in the Game */ IndexMap data; /// Extra values for specitic stylesheets, indexed by stylesheet name DelayedIndexMaps styling_data; - vector cards; ///< The cards in the set - vector keywords; ///< Additional keywords used in this set - String apprentice_code; ///< Code to use for apprentice (Magic only) - ActionStack actions; ///< Actions performed on this set and the cards in it - KeywordDatabase keyword_db; ///< Database for matching keywords, must be cleared when keywords change + vector cards; ///< The cards in the set + vector keywords; ///< Additional keywords used in this set + vector pack_types; ///< Additional/replacement pack types + String apprentice_code; ///< Code to use for apprentice (Magic only) + + ActionStack actions; ///< Actions performed on this set and the cards in it + KeywordDatabase keyword_db; ///< Database for matching keywords, must be cleared when keywords change /// A context for performing scripts /** Should only be used from the main thread! */ diff --git a/src/gui/set/random_pack_panel.cpp b/src/gui/set/random_pack_panel.cpp index ab1291ad..2b749ab8 100644 --- a/src/gui/set/random_pack_panel.cpp +++ b/src/gui/set/random_pack_panel.cpp @@ -12,6 +12,7 @@ #include #include #include // HoverButtonBase +#include #include #include #include @@ -279,9 +280,9 @@ PackAmountPicker::PackAmountPicker(wxWindow* parent, wxFlexGridSizer* sizer, con sizer->Add(label, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL); sizer->Add(value, 0, wxEXPAND | wxALIGN_CENTER); if (active) { - label->SetHelpText(_("Double click to edit.")); + label->SetHelpText(_HELP_("edit pack type")); } - set_help_text(value, _("The number of ") + pack->name + _("s to use.")); + set_help_text(value, _HELP_1_("number of packs", pack->name)); } void PackAmountPicker::destroy(wxFlexGridSizer* sizer) { @@ -434,9 +435,9 @@ void RandomPackPanel::initControls() { wxSizer* s4b = new wxBoxSizer(wxHORIZONTAL); packsSizer = new wxFlexGridSizer(0, 2, 4, 4); packsSizer->AddGrowableCol(0); - s4b->Add(packsSizer, 1, wxEXPAND | wxALL & ~wxTOP & ~wxBOTTOM & ~wxLEFT, 4); + s4b->Add(packsSizer, 1, wxEXPAND | wxALL & ~wxTOP & ~wxLEFT, 4); s4->Add(s4b, 1, wxEXPAND | wxLEFT, 2); - s4->Add(new wxButton(this, ID_CUSTOM_PACK, _BUTTON_("custom pack")), 0, wxEXPAND | wxALL & ~wxTOP, 4); + s4->Add(new wxButton(this, ID_CUSTOM_PACK, _BUTTON_("add custom pack")), 0, wxEXPAND | wxALIGN_TOP | wxALL & ~wxTOP, 4); s3->Add(s4, 1, wxEXPAND, 8); wxSizer* s5 = new wxStaticBoxSizer(wxHORIZONTAL, this, _LABEL_("pack totals")); s5->Add(totals, 1, wxEXPAND | wxALL, 4); @@ -480,15 +481,22 @@ void RandomPackPanel::onChangeSet() { pickers.clear(); // add pack controls + #if USE_NEW_PACK_SYSTEM FOR_EACH(pack, set->game->pack_types) { - #if USE_NEW_PACK_SYSTEM if (pack->selectable) { - #endif - pickers.push_back(PackAmountPicker(this,packsSizer,pack)); - #if USE_NEW_PACK_SYSTEM + pickers.push_back(PackAmountPicker(this,packsSizer,pack,false)); } - #endif } + FOR_EACH(pack, set->pack_types) { + if (pack->selectable) { + pickers.push_back(PackAmountPicker(this,packsSizer,pack,true)); + } + } + #else + FOR_EACH(pack, set->game->pack_types) { + pickers.push_back(PackAmountPicker(this,packsSizer,pack,false)); + } + #endif Layout(); @@ -508,6 +516,14 @@ void RandomPackPanel::onChangeSet() { updateTotals(); } +void RandomPackPanel::onAction(const Action& action, bool undone) { + TYPE_CASE_(action, PackTypesAction) { + // rebuild the list + storeSettings(); + onChangeSet(); + } +} + void RandomPackPanel::storeSettings() { if (!isInitialized()) return; GameSettings& gs = settings.gameSettingsFor(*set->game); @@ -555,19 +571,26 @@ void RandomPackPanel::onCommand(int id) { case ID_CUSTOM_PACK: { CustomPackDialog dlg(this, set, PackTypeP()); if (dlg.ShowModal() == wxID_OK) { - // TODO: add pack + set->actions.addAction( new AddPackAction(ADD,*set,dlg.get()) ); } break; } } } void RandomPackPanel::onPackTypeClick(wxCommandEvent& ev) { - FOR_EACH(pick,pickers) { + for (size_t i = 0 ; i < pickers.size() ; ++i) { + const PackAmountPicker& pick = pickers[i]; if (pick.label == ev.GetEventObject()) { // edit this pack type CustomPackDialog dlg(this, set, pick.pack); if (dlg.ShowModal() == wxID_OK) { - // TODO: update pack + if (dlg.get()) { + // delete pack + set->actions.addAction( new AddPackAction(REMOVE,*set,pick.pack) ); + } else { + // update pack + set->actions.addAction( new ChangePackAction(*set,i,dlg.get()) ); + } } break; } diff --git a/src/gui/set/random_pack_panel.hpp b/src/gui/set/random_pack_panel.hpp index a0003581..839442d8 100644 --- a/src/gui/set/random_pack_panel.hpp +++ b/src/gui/set/random_pack_panel.hpp @@ -30,7 +30,7 @@ struct PackAmountPicker { wxSpinCtrl* value; PackAmountPicker() {} - PackAmountPicker(wxWindow* parent, wxFlexGridSizer* sizer, const PackTypeP& pack, bool active = true); + PackAmountPicker(wxWindow* parent, wxFlexGridSizer* sizer, const PackTypeP& pack, bool interactive); void destroy(wxFlexGridSizer* sizer); }; @@ -46,6 +46,7 @@ class RandomPackPanel : public SetWindowPanel { virtual void onBeforeChangeSet(); virtual void onChangeSet(); + virtual void onAction(const Action&, bool undone); virtual void initUI (wxToolBar* tb, wxMenuBar* mb); virtual void destroyUI(wxToolBar* tb, wxMenuBar* mb); diff --git a/src/resource/common/expected_locale_keys b/src/resource/common/expected_locale_keys index 5726feb1..e0ef63ef 100644 --- a/src/resource/common/expected_locale_keys +++ b/src/resource/common/expected_locale_keys @@ -1,6 +1,6 @@ # This file contains the keys expected to be in MSE locales # It was automatically generated by tools/locale/locale.pl -# Generated on Tue Dec 30 20:45:27 2008 +# Generated on Sat Jan 10 02:30:34 2009 action: add control point: 0 @@ -40,6 +40,7 @@ action: typing: 0 ungroup parts: 0 button: + add custom pack: 0 add item: 0 always: 0 browse: 0 @@ -179,6 +180,7 @@ help: draw rectangle: 0 draw star: 0 duplicate: 0 + edit pack type: 0 ellipse: 0 exit: 0 expand notes: 0 @@ -208,6 +210,7 @@ help: next card: 0 next keyword: 0 no spelling suggestions: 0 + number of packs: 1 open set: 0 open symbol: 0 orientation: 0 @@ -306,6 +309,7 @@ label: no version: 0 original: 0 original size: 0 + pack name: 0 pack selection: 0 pack totals: 0 package action: 0 @@ -443,6 +447,7 @@ title: about: 0 auto replaces: 0 cannot create file: 0 + custom pack: 0 directories: 0 display: 0 export cancelled: 0 @@ -589,6 +594,7 @@ type: locale: optional, 0 nil: 0 object: 0 + pack: 0 package: optional, 0 pentagon: 0 point: 0