refactor card copy pasting

This commit is contained in:
GenevensiS
2026-05-04 11:46:11 +02:00
parent 3c2c3ac977
commit 8a86778bfb
3 changed files with 103 additions and 66 deletions
+34 -17
View File
@@ -61,9 +61,11 @@ IMPLEMENT_REFLECTION(WrappedCards) {
}
wxDataFormat CardsDataObject::format(wxString("application/x-mse-cards"));
wxDataFormat CardsDataObject::format(wxString("application/x-mse-cards"));
CardsDataObject::CardsDataObject(const SetP& set, const String id, const vector<CardP>& cards) {
CardsDataObject::CardsDataObject(const SetP& set, const String& id, const vector<CardP>& cards)
: wxCustomDataObject(format)
{
// set the stylesheet, so when deserializing we know whos style options we are reading
vector<bool> has_styling;
for (size_t i = 0 ; i < cards.size() ; ++i) {
@@ -72,9 +74,15 @@ CardsDataObject::CardsDataObject(const SetP& set, const String id, const vector<
cards[i]->stylesheet = set->stylesheet;
}
}
// store as raw bytes
WrappedCards data = { set->game.get(), set->game->name(), id, cards };
SetText(serialize_for_clipboard(*set, data));
// restore cards
String serialized = serialize_for_clipboard(*set, data);
wxScopedCharBuffer utf8 = serialized.utf8_str();
buffer.assign(utf8.data(), utf8.length());
SetData(buffer.size(), buffer.data());
// restore styling
for (size_t i = 0 ; i < cards.size() ; ++i) {
if (has_styling[i]) {
cards[i]->stylesheet = StyleSheetP();
@@ -83,22 +91,31 @@ CardsDataObject::CardsDataObject(const SetP& set, const String id, const vector<
SetFormat(format);
}
CardsDataObject::CardsDataObject() {
SetFormat(format);
}
CardsDataObject::CardsDataObject()
: wxCustomDataObject(format)
{}
bool CardsDataObject::getCards(const SetP& set, const String id, vector<CardP>& out) {
bool CardsDataObject::getCards(const SetP& set, const String& id, vector<CardP>& out) {
size_t size = GetSize();
if (size == 0) return false;
const void* raw = GetData();
if (!raw) return false;
String text(wxString::FromUTF8(static_cast<const char*>(raw), size));
WrappedCards data = { set->game.get(), set->game->name() };
deserialize_from_clipboard(data, *set, GetText());
try {
deserialize_from_clipboard(data, *set, text);
} catch (...) {
queue_message(MESSAGE_ERROR, _("DEBUG: Card deserialization failed"));
return false;
}
if (data.cards.empty()) return false;
if (!id.empty() && data.id == id) return false;
if (data.game_name == set->game->name()) {
// Cards are from the same game
out = data.cards;
return true;
} else {
return false;
}
if (data.game_name != set->game->name()) return false;
// Cards are from the same game
out = data.cards;
return true;
}
// ----------------------------------------------------------------------------- : KeywordDataObject
@@ -139,7 +156,7 @@ KeywordP KeywordDataObject::getKeyword(const SetP& set) {
deserialize_from_clipboard(data, *set, GetText());
if (data.game_name != set->game->name()) return KeywordP(); // Keyword is from a different game
else return keyword;
}
}
// ----------------------------------------------------------------------------- : Card on clipboard
+8 -5
View File
@@ -18,20 +18,23 @@ DECLARE_POINTER_TYPE(Keyword);
// ----------------------------------------------------------------------------- : CardDataObject
/// The data format for cards on the clipboard
class CardsDataObject : public wxTextDataObject {
class CardsDataObject : public wxCustomDataObject {
public:
/// Name of the format of MSE cards
static wxDataFormat format;
CardsDataObject();
/// Store a card
CardsDataObject(const SetP& set, const String id, const vector<CardP>& cards);
CardsDataObject(const SetP& set, const String& id, const vector<CardP>& cards);
/// Retrieve the cards, only if this is made with the same game as set
/// And if this is NOT of the same id as the given one
/** Return true if the cards are correctly retrieved, and there is at least one card */
bool getCards(const SetP& set, const String id, vector<CardP>& out);
};
bool getCards(const SetP& set, const String& id, vector<CardP>& out);
private:
std::string buffer; // keep data alive for wx
};
// ----------------------------------------------------------------------------- : KeywordDataObject
@@ -44,7 +47,7 @@ public:
KeywordDataObject();
/// Store a keyword
KeywordDataObject(const SetP& set, const KeywordP& card);
/// Retrieve a keyword, only if it is made with the same game as set
KeywordP getKeyword(const SetP& set);
};