From 2a45f377f3d8a8cf0f8cf8df83cb22c3fab29dc8 Mon Sep 17 00:00:00 2001 From: mercury233 Date: Wed, 14 Aug 2024 17:26:19 +0800 Subject: [PATCH] add import/export deck code --- gframe/deck_con.cpp | 43 +++++++++++++++++++++++++++++++++++++---- gframe/deck_manager.cpp | 31 ++++++++++++++++++----------- gframe/deck_manager.h | 2 ++ gframe/game.cpp | 12 ++++++++---- gframe/game.h | 4 ++++ strings.conf | 3 +++ 6 files changed, 76 insertions(+), 19 deletions(-) diff --git a/gframe/deck_con.cpp b/gframe/deck_con.cpp index 59121ca03d..7be4e36b82 100644 --- a/gframe/deck_con.cpp +++ b/gframe/deck_con.cpp @@ -369,6 +369,30 @@ bool DeckBuilder::OnEvent(const irr::SEvent& event) { prev_operation = id; break; } + case BUTTON_IMPORT_DECK_CODE: { + time_t currentTime = time(nullptr); + tm* localedtime = localtime(¤tTime); + wchar_t timetext[40]; + wcsftime(timetext, 40, L"%Y-%m-%d %H-%M-%S", localedtime); + mainGame->gMutex.lock(); + mainGame->stDMMessage->setText(dataManager.GetSysString(1471)); + mainGame->ebDMName->setVisible(true); + mainGame->ebDMName->setText(timetext); + mainGame->PopupElement(mainGame->wDMQuery); + mainGame->gMutex.unlock(); + prev_operation = id; + break; + } + case BUTTON_EXPORT_DECK_CODE: { + std::stringstream textStream; + deckManager.SaveDeck(deckManager.current_deck, textStream); + wchar_t text[0x10000]; + BufferIO::DecodeUTF8(textStream.str().c_str(), text); + mainGame->env->getOSOperator()->copyToClipboard(text); + mainGame->stACMessage->setText(dataManager.GetSysString(1480)); + mainGame->PopupElement(mainGame->wACMessage, 20); + break; + } case BUTTON_DM_OK: { switch(prev_operation) { case BUTTON_NEW_CATEGORY: { @@ -445,7 +469,8 @@ bool DeckBuilder::OnEvent(const irr::SEvent& event) { } break; } - case BUTTON_NEW_DECK: { + case BUTTON_NEW_DECK: + case BUTTON_IMPORT_DECK_CODE: { const wchar_t* deckname = mainGame->ebDMName->getText(); wchar_t catepath[256]; deckManager.GetCategoryPath(catepath, mainGame->cbDBCategory->getSelected(), mainGame->cbDBCategory->getText()); @@ -453,9 +478,19 @@ bool DeckBuilder::OnEvent(const irr::SEvent& event) { myswprintf(filepath, L"%ls/%ls.ydk", catepath, deckname); bool res = false; if(!FileSystem::IsFileExists(filepath)) { - deckManager.current_deck.main.clear(); - deckManager.current_deck.extra.clear(); - deckManager.current_deck.side.clear(); + if(prev_operation == BUTTON_NEW_DECK) { + deckManager.current_deck.main.clear(); + deckManager.current_deck.extra.clear(); + deckManager.current_deck.side.clear(); + } else { + const wchar_t* txt = mainGame->env->getOSOperator()->getTextFromClipboard(); + if(txt) { + char text[0x10000]; + BufferIO::EncodeUTF8(txt, text); + std::istringstream textStream(text); + deckManager.LoadCurrentDeck(textStream); + } + } res = deckManager.SaveDeck(deckManager.current_deck, filepath); RefreshDeckList(); ChangeCategory(mainGame->lstCategories->getSelected()); diff --git a/gframe/deck_manager.cpp b/gframe/deck_manager.cpp index 6eacaed325..e2dad9e420 100644 --- a/gframe/deck_manager.cpp +++ b/gframe/deck_manager.cpp @@ -284,6 +284,10 @@ IReadFile* DeckManager::OpenDeckReader(const wchar_t* file) { #endif return reader; } +bool DeckManager::LoadCurrentDeck(std::istringstream& deckStream, bool is_packlist) { + LoadDeck(current_deck, deckStream, is_packlist); + return true; // the above LoadDeck has return value but we ignore it here for now +} bool DeckManager::LoadCurrentDeck(const wchar_t* file, bool is_packlist) { current_deck.clear(); IReadFile* reader = OpenDeckReader(file); @@ -308,8 +312,7 @@ bool DeckManager::LoadCurrentDeck(const wchar_t* file, bool is_packlist) { reader->read(deckBuffer, size); reader->drop(); std::istringstream deckStream(deckBuffer); - LoadDeck(current_deck, deckStream, is_packlist); - return true; // the above LoadDeck has return value but we ignore it here for now + return LoadCurrentDeck(deckStream, is_packlist); } bool DeckManager::LoadCurrentDeck(irr::gui::IGUIComboBox* cbCategory, irr::gui::IGUIComboBox* cbDeck) { wchar_t filepath[256]; @@ -320,21 +323,27 @@ bool DeckManager::LoadCurrentDeck(irr::gui::IGUIComboBox* cbCategory, irr::gui:: mainGame->deckBuilder.RefreshPackListScroll(); return res; } +void DeckManager::SaveDeck(Deck& deck, std::stringstream& deckStream) { + deckStream << "#created by ..." << std::endl; + deckStream << "#main" << std::endl; + for(size_t i = 0; i < deck.main.size(); ++i) + deckStream << deck.main[i]->first << std::endl; + deckStream << "#extra" << std::endl; + for(size_t i = 0; i < deck.extra.size(); ++i) + deckStream << deck.extra[i]->first << std::endl; + deckStream << "!side" << std::endl; + for(size_t i = 0; i < deck.side.size(); ++i) + deckStream << deck.side[i]->first << std::endl; +} bool DeckManager::SaveDeck(Deck& deck, const wchar_t* file) { if(!FileSystem::IsDirExists(L"./deck") && !FileSystem::MakeDir(L"./deck")) return false; FILE* fp = OpenDeckFile(file, "w"); if(!fp) return false; - fprintf(fp, "#created by ...\n#main\n"); - for(size_t i = 0; i < deck.main.size(); ++i) - fprintf(fp, "%d\n", deck.main[i]->first); - fprintf(fp, "#extra\n"); - for(size_t i = 0; i < deck.extra.size(); ++i) - fprintf(fp, "%d\n", deck.extra[i]->first); - fprintf(fp, "!side\n"); - for(size_t i = 0; i < deck.side.size(); ++i) - fprintf(fp, "%d\n", deck.side[i]->first); + std::stringstream deckStream; + SaveDeck(deck, deckStream); + fwrite(deckStream.str().c_str(), 1, deckStream.str().length(), fp); fclose(fp); return true; } diff --git a/gframe/deck_manager.h b/gframe/deck_manager.h index dfb8dc0d6b..8720b2b6c4 100644 --- a/gframe/deck_manager.h +++ b/gframe/deck_manager.h @@ -54,8 +54,10 @@ class DeckManager { void GetDeckFile(wchar_t* ret, irr::gui::IGUIComboBox* cbCategory, irr::gui::IGUIComboBox* cbDeck); FILE* OpenDeckFile(const wchar_t* file, const char* mode); IReadFile* OpenDeckReader(const wchar_t* file); + bool LoadCurrentDeck(std::istringstream& deckStream, bool is_packlist = false); bool LoadCurrentDeck(const wchar_t* file, bool is_packlist = false); bool LoadCurrentDeck(irr::gui::IGUIComboBox* cbCategory, irr::gui::IGUIComboBox* cbDeck); + void SaveDeck(Deck& deck, std::stringstream& deckStream); bool SaveDeck(Deck& deck, const wchar_t* file); bool DeleteDeck(const wchar_t* file); bool CreateCategory(const wchar_t* name); diff --git a/gframe/game.cpp b/gframe/game.cpp index 2ab96904f7..994fa77a4d 100644 --- a/gframe/game.cpp +++ b/gframe/game.cpp @@ -645,10 +645,10 @@ bool Game::Initialize() { wDeckEdit->setVisible(false); btnManageDeck = env->addButton(rect(225, 5, 290, 30), wDeckEdit, BUTTON_MANAGE_DECK, dataManager.GetSysString(1328)); //deck manage - wDeckManage = env->addWindow(rect(310, 135, 800, 465), false, dataManager.GetSysString(1460), 0, WINDOW_DECK_MANAGE); + wDeckManage = env->addWindow(rect(310, 135, 800, 515), false, dataManager.GetSysString(1460), 0, WINDOW_DECK_MANAGE); wDeckManage->setVisible(false); - lstCategories = env->addListBox(rect(10, 30, 140, 320), wDeckManage, LISTBOX_CATEGORIES, true); - lstDecks = env->addListBox(rect(150, 30, 340, 320), wDeckManage, LISTBOX_DECKS, true); + lstCategories = env->addListBox(rect(10, 30, 140, 370), wDeckManage, LISTBOX_CATEGORIES, true); + lstDecks = env->addListBox(rect(150, 30, 340, 370), wDeckManage, LISTBOX_DECKS, true); posY = 30; btnNewCategory = env->addButton(rect(350, posY, 480, posY + 25), wDeckManage, BUTTON_NEW_CATEGORY, dataManager.GetSysString(1461)); posY += 35; @@ -665,6 +665,10 @@ bool Game::Initialize() { btnMoveDeck = env->addButton(rect(350, posY, 480, posY + 25), wDeckManage, BUTTON_MOVE_DECK, dataManager.GetSysString(1467)); posY += 35; btnCopyDeck = env->addButton(rect(350, posY, 480, posY + 25), wDeckManage, BUTTON_COPY_DECK, dataManager.GetSysString(1468)); + posY += 35; + btnImportDeckCode = env->addButton(rect(350, posY, 480, posY + 25), wDeckManage, BUTTON_IMPORT_DECK_CODE, dataManager.GetSysString(1478)); + posY += 35; + btnExportDeckCode = env->addButton(rect(350, posY, 480, posY + 25), wDeckManage, BUTTON_EXPORT_DECK_CODE, dataManager.GetSysString(1479)); //deck manage query wDMQuery = env->addWindow(rect(400, 200, 710, 320), false, dataManager.GetSysString(1460)); wDMQuery->getCloseButton()->setVisible(false); @@ -1874,7 +1878,7 @@ void Game::OnResize() { ebDeckname->setRelativePosition(Resize(80, 65, 220, 90)); cbDBCategory->setRelativePosition(Resize(80, 5, 220, 30)); btnManageDeck->setRelativePosition(Resize(225, 5, 290, 30)); - wDeckManage->setRelativePosition(ResizeWin(310, 135, 800, 465)); + wDeckManage->setRelativePosition(ResizeWin(310, 135, 800, 515)); scrPackCards->setRelativePosition(Resize(775, 161, 795, 629)); wSort->setRelativePosition(Resize(930, 132, 1020, 156)); diff --git a/gframe/game.h b/gframe/game.h index 332a50a577..5b2a56b648 100644 --- a/gframe/game.h +++ b/gframe/game.h @@ -544,6 +544,8 @@ class Game { irr::gui::IGUIButton* btnDMDeleteDeck; irr::gui::IGUIButton* btnMoveDeck; irr::gui::IGUIButton* btnCopyDeck; + irr::gui::IGUIButton* btnImportDeckCode; + irr::gui::IGUIButton* btnExportDeckCode; irr::gui::IGUIWindow* wDMQuery; irr::gui::IGUIStaticText* stDMMessage; irr::gui::IGUIStaticText* stDMMessage2; @@ -797,6 +799,8 @@ extern Game* mainGame; #define LISTBOX_DECKS 340 #define BUTTON_DM_OK 341 #define BUTTON_DM_CANCEL 342 +#define BUTTON_IMPORT_DECK_CODE 343 +#define BUTTON_EXPORT_DECK_CODE 344 #define COMBOBOX_LFLIST 349 #define BUTTON_CLEAR_LOG 350 diff --git a/strings.conf b/strings.conf index 60e0626501..949478c965 100644 --- a/strings.conf +++ b/strings.conf @@ -493,6 +493,9 @@ !system 1475 已存在同名卡组 !system 1476 删除失败 !system 1477 卡片数: +!system 1478 导入卡组码 +!system 1479 导出卡组码 +!system 1480 已导出到剪贴板 !system 1481 OCG !system 1482 TCG !system 1483 简体中文