From 151a04909a4d894d498fbac586059e359c70dbd7 Mon Sep 17 00:00:00 2001 From: GenevensiS <66968533+G-e-n-e-v-e-n-s-i-S@users.noreply.github.com> Date: Wed, 13 May 2026 18:09:56 +0200 Subject: [PATCH] Add update_cards_scripts --- src/data/action/set.cpp | 25 ++++++++ src/data/action/set.hpp | 12 ++++ src/data/action/value.cpp | 6 +- src/data/add_cards_script.cpp | 14 +++-- src/data/card.cpp | 40 +++++++++++- src/data/card.hpp | 6 +- src/data/game.cpp | 9 ++- src/data/game.hpp | 44 ++++++------- src/data/keyword.cpp | 1 - src/data/set.cpp | 65 +++++++++++++++++--- src/data/set.hpp | 6 +- src/data/stylesheet.cpp | 8 ++- src/data/stylesheet.hpp | 14 +++-- src/data/symbol_font.cpp | 1 - src/data/update_cards_script.cpp | 57 +++++++++++++++++ src/data/update_cards_script.hpp | 39 ++++++++++++ src/gui/control/card_list.cpp | 6 +- src/script/functions/construction.cpp | 14 +++-- src/script/functions/construction_helper.hpp | 16 ++++- src/script/functions/json.cpp | 3 +- src/util/delayed_index_maps.hpp | 15 +++++ src/util/index_map.hpp | 6 +- src/util/io/reader.cpp | 2 +- src/util/io/reader.hpp | 4 +- src/util/version.cpp | 49 +++++++-------- src/util/version.hpp | 49 +++++++++++---- 26 files changed, 403 insertions(+), 108 deletions(-) create mode 100644 src/data/update_cards_script.cpp create mode 100644 src/data/update_cards_script.hpp diff --git a/src/data/action/set.cpp b/src/data/action/set.cpp index 07ab9bf0..28b06de3 100644 --- a/src/data/action/set.cpp +++ b/src/data/action/set.cpp @@ -87,6 +87,31 @@ void AddCardAction::perform(bool to_undo) { set.buildUIDMap(); } +UpdateCardAction::UpdateCardAction(Set& set, const vector& cards_to_add, const vector& cards_to_remove) + : CardListAction(set) + , action_add(ADD, set, cards_to_add) + , action_remove(REMOVE, set, cards_to_remove) +{} + +String UpdateCardAction::getName(bool to_undo) const { + return _ACTION_("update card"); +} + +void UpdateCardAction::perform(bool to_undo) { + if (to_undo) { + action_remove.perform(to_undo); + set.actions.tellListeners(action_remove, to_undo); + action_add.perform(to_undo); + set.actions.tellListeners(action_add, to_undo); + } + else { + action_add.perform(to_undo); + set.actions.tellListeners(action_add, to_undo); + action_remove.perform(to_undo); + set.actions.tellListeners(action_remove, to_undo); + } +} + // ----------------------------------------------------------------------------- : Reorder cards ReorderCardsAction::ReorderCardsAction(Set& set, size_t card_id1, size_t card_id2) diff --git a/src/data/action/set.hpp b/src/data/action/set.hpp index 6d09e84e..88729bcb 100644 --- a/src/data/action/set.hpp +++ b/src/data/action/set.hpp @@ -50,6 +50,18 @@ public: vector card_link_actions; }; +/// Update one or more cards from a set by replacing them with other cards +class UpdateCardAction : public CardListAction { +public: + UpdateCardAction(Set& set, const vector& cards_to_add, const vector& cards_to_remove); + + String getName(bool to_undo) const override; + void perform(bool to_undo) override; + + AddCardAction action_add; + AddCardAction action_remove; +}; + // ----------------------------------------------------------------------------- : Reorder cards /// Change the position of a card in the card list by swapping two cards diff --git a/src/data/action/value.cpp b/src/data/action/value.cpp index 8a3f3e08..d065f0d6 100644 --- a/src/data/action/value.cpp +++ b/src/data/action/value.cpp @@ -400,8 +400,10 @@ String BulkAction::getName(bool to_undo) const { return to_undo ? name_undo : name_do; } -void BulkAction::perform(bool to_undo) { - FOR_EACH(action, actions) { +void BulkAction::perform(bool to_undo) { + size_t size = actions.size(); + for (size_t i = 0 ; i < size ; ++i) { + ActionP& action = actions[to_undo ? size - i - 1 : i]; action->perform(to_undo); set->actions.tellListeners(*action, to_undo); } diff --git a/src/data/add_cards_script.cpp b/src/data/add_cards_script.cpp index 94301c9a..cfd51571 100644 --- a/src/data/add_cards_script.cpp +++ b/src/data/add_cards_script.cpp @@ -25,18 +25,22 @@ IMPLEMENT_REFLECTION_NO_SCRIPT(AddCardsScript) { void AddCardsScript::perform(Set& set, vector& out) { // Perform script - Context& ctx = set.getContext(); + Context& ctx = set.getContext(); + if (enabled.hasBeenRead()) { + enabled.update(ctx); + if (!enabled()) return; + } ScriptValueP result = script.invoke(ctx); // Add cards to out ScriptValueP it = result->makeIterator(); while (ScriptValueP item = it->next()) { - CardP card = from_script(item); + CardP new_card = from_script(item); // is this a new card? - if (contains(set.cards,card) || contains(out,card)) { + if (contains(set.cards,new_card) || contains(out,new_card)) { // make copy - card = make_intrusive(*card); + new_card = make_intrusive(&set, new_card); } - out.push_back(card); + out.push_back(new_card); } } diff --git a/src/data/card.cpp b/src/data/card.cpp index 5ad204d1..ab51a4e2 100644 --- a/src/data/card.cpp +++ b/src/data/card.cpp @@ -42,7 +42,38 @@ Card::Card(Game& game) , has_styling(false) { data.init(game.card_fields); -} +} + +Card::Card(Set* set, const CardP& card) + : game(card->game) + , time_created (wxDateTime::Now()) + , time_modified(wxDateTime::Now()) + , notes(card->notes) + , uid(generate_uid()) + , linked_card_1(card->linked_card_1) + , linked_card_2(card->linked_card_2) + , linked_card_3(card->linked_card_3) + , linked_card_4(card->linked_card_4) + , linked_relation_1(card->linked_relation_1) + , linked_relation_2(card->linked_relation_2) + , linked_relation_3(card->linked_relation_3) + , linked_relation_4(card->linked_relation_4) + , has_styling(card->has_styling) + , stylesheet_version(card->stylesheet_version) + , stylesheet(card->stylesheet) +{ + if (!stylesheet && set) { + stylesheet = set->stylesheetForP(card); + } + if (has_styling) { + styling_data.cloneFrom(card->styling_data); + } + else { + if (stylesheet && set) styling_data.cloneFrom(set->stylingDataFor(*stylesheet)); + } + data.cloneFrom(card->data); + extra_data.cloneFrom(card->extra_data); +} String Card::identification() const { // an identifying field @@ -344,7 +375,12 @@ void reflect_version_check(GetDefaultMember& handler, const Char* key, intrusive IMPLEMENT_REFLECTION(Card) { REFLECT(stylesheet); - reflect_version_check(handler, _("stylesheet_version"), stylesheet); + if (Handler::isReading) { + REFLECT_NO_SCRIPT(stylesheet_version); + } + else { + reflect_version_check(handler, _("stylesheet_version"), stylesheet); + } REFLECT(has_styling); if (has_styling) { if (stylesheet) { diff --git a/src/data/card.hpp b/src/data/card.hpp index 9dde8cb6..19ab0af2 100644 --- a/src/data/card.hpp +++ b/src/data/card.hpp @@ -35,6 +35,8 @@ public: Card(); /// Creates a card using the given game Card(Game& game); + /// Copy constructor, makes a deep copy + Card(Set* set, const CardP& card); /// The game this card is made for Game* game; @@ -60,7 +62,9 @@ public: wxDateTime time_created, time_modified; /// Alternative style to use for this card /** Optional; if not set use the card style from the set */ - StyleSheetP stylesheet; + StyleSheetP stylesheet; + /// What version of the stylesheet was this card using when it was last saved? + Version stylesheet_version; /// Alternative options to use for this card, for this card's stylesheet /** Optional; if not set use the styling data from the set. * If stylesheet is set then contains data for the this->stylesheet, otherwise for set->stylesheet diff --git a/src/data/game.cpp b/src/data/game.cpp index d9bb6883..1c0be291 100644 --- a/src/data/game.cpp +++ b/src/data/game.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include