From 5e1f1294e5b3b4212d76a12929284b429d42df28 Mon Sep 17 00:00:00 2001 From: Michael <13953785+Laserlicht@users.noreply.github.com> Date: Tue, 8 Aug 2023 00:38:13 +0200 Subject: [PATCH] rework --- client/lobby/SelectionTab.cpp | 147 ++++++++++++++++++++-------------- client/lobby/SelectionTab.h | 28 ++++--- 2 files changed, 106 insertions(+), 69 deletions(-) diff --git a/client/lobby/SelectionTab.cpp b/client/lobby/SelectionTab.cpp index 3a9374c86..2d14ccd29 100644 --- a/client/lobby/SelectionTab.cpp +++ b/client/lobby/SelectionTab.cpp @@ -41,8 +41,11 @@ #include "../../lib/mapping/MapFormat.h" #include "../../lib/serializer/Connection.h" -bool mapSorter::operator()(const std::shared_ptr aaa, const std::shared_ptr bbb) +bool mapSorter::operator()(const std::shared_ptr aaa, const std::shared_ptr bbb) { + if(aaa->isFolder || bbb->isFolder) + return (aaa->isFolder > bbb->isFolder); + auto a = aaa->mapHeader.get(); auto b = bbb->mapHeader.get(); if(a && b) //if we are sorting scenarios @@ -130,7 +133,7 @@ static ESortBy getSortBySelectionScreen(ESelectionScreen Type) } SelectionTab::SelectionTab(ESelectionScreen Type) - : CIntObject(LCLICK | SHOW_POPUP | KEYBOARD | DOUBLECLICK), callOnSelect(nullptr), tabType(Type), selectionPos(0), sortModeAscending(true), inputNameRect{32, 539, 350, 20} + : CIntObject(LCLICK | SHOW_POPUP | KEYBOARD | DOUBLECLICK), callOnSelect(nullptr), tabType(Type), selectionPos(0), sortModeAscending(true), inputNameRect{32, 539, 350, 20}, curFolder("") { OBJ_CONSTRUCTION; @@ -148,7 +151,7 @@ SelectionTab::SelectionTab(ESelectionScreen Type) int sizes[] = {36, 72, 108, 144, 0}; const char * filterIconNmes[] = {"SCSMBUT.DEF", "SCMDBUT.DEF", "SCLGBUT.DEF", "SCXLBUT.DEF", "SCALBUT.DEF"}; for(int i = 0; i < 5; i++) - buttonsSortBy.push_back(std::make_shared(Point(158 + 47 * i, 46), filterIconNmes[i], CGI->generaltexth->zelp[54 + i], std::bind(&SelectionTab::filter, this, sizes[i], true, ""))); + buttonsSortBy.push_back(std::make_shared(Point(158 + 47 * i, 46), filterIconNmes[i], CGI->generaltexth->zelp[54 + i], std::bind(&SelectionTab::filter, this, sizes[i], true))); int xpos[] = {23, 55, 88, 121, 306, 339}; const char * sortIconNames[] = {"SCBUTT1.DEF", "SCBUTT2.DEF", "SCBUTCP.DEF", "SCBUTT3.DEF", "SCBUTT4.DEF", "SCBUTT5.DEF"}; @@ -214,7 +217,6 @@ void SelectionTab::toggleMode() { allItems.clear(); curItems.clear(); - curFolders.clear(); if(slider) slider->block(true); } @@ -336,15 +338,44 @@ void SelectionTab::showPopupWindow(const Point & cursorPosition) CRClickPopup::createAndPush(text); } +std::tuple SelectionTab::checkSubfolder(std::string path) +{ + std::string folderName = ""; + bool parentExists = false; + + std::string folder = boost::filesystem::path(path).parent_path().string(); + std::vector filetree; + + // delete first element (e.g. 'MAPS') + boost::split(filetree, folder, boost::is_any_of("/")); + filetree.erase(filetree.begin()); + folder = boost::algorithm::join(filetree, "/"); + + if(boost::algorithm::starts_with(folder, curFolder) && curFolder != "") + { + folder = folder.substr(curFolder.size()); + if(boost::algorithm::starts_with(folder, "/")) + folder = folder.substr(1); + + parentExists = true; + } + + if(folder != "") + { + boost::split(filetree, folder, boost::is_any_of("/")); + folderName = filetree[0]; + } + + return {folderName, parentExists}; +} + // A new size filter (Small, Medium, ...) has been selected. Populate // selMaps with the relevant data. -void SelectionTab::filter(int size, bool selectFirst, std::string path) +void SelectionTab::filter(int size, bool selectFirst) { - path = "AA"; - + std::string path = ""; curItems.clear(); - curFolders.clear(); if(tabType == ESelectionScreen::campaignList) { @@ -357,32 +388,23 @@ void SelectionTab::filter(int size, bool selectFirst, std::string path) { if(elem->mapHeader && (!size || elem->mapHeader->width == size)) { - std::string folder = boost::filesystem::path(elem->fileURI).parent_path().string(); - std::vector filetree; + auto [folderName, parentExists] = checkSubfolder(elem->fileURI); - // delete first element (e.g. 'MAPS') - boost::split(filetree, folder, boost::is_any_of("/")); - filetree.erase(filetree.begin()); - folder = boost::algorithm::join(filetree, "/"); - - // remove current dir - if(boost::algorithm::starts_with(folder, path)) + if(parentExists) { - folder = folder.substr(path.size()); - if(boost::algorithm::starts_with(folder, "/")) - folder = folder.substr(1); - - if (std::find(curFolders.begin(), curFolders.end(), "..") == curFolders.end()) { - curFolders.push_back(".."); - } + auto folder = std::make_shared(); + folder->isFolder = true; + folder->folderName = ".."; + if (std::find(curItems.begin(), curItems.end(), folder) == curItems.end()) { + curItems.push_back(folder); + } } - if(folder != "") - { - boost::split(filetree, folder, boost::is_any_of("/")); - if (std::find(curFolders.begin(), curFolders.end(), filetree[0]) == curFolders.end()) { - curFolders.push_back(filetree[0]); - } + auto folder = std::make_shared(); + folder->isFolder = true; + folder->folderName = folderName; + if (std::find(curItems.begin(), curItems.end(), folder) == curItems.end() && folderName != "") { + curItems.push_back(folder); } curItems.push_back(elem); @@ -441,7 +463,7 @@ void SelectionTab::sort() void SelectionTab::select(int position) { - if(!(curFolders.size() + curItems.size())) + if(!curItems.size()) return; // New selection. py is the index in curItems. @@ -458,6 +480,11 @@ void SelectionTab::select(int position) rememberCurrentSelection(); + if(curItems[py]->isFolder) { + return; + //TODO + } + if(inputName && inputName->isActive()) { auto filename = *CResourceHandler::get("local")->getResourceName(ResourceID(curItems[py]->fileURI, EResType::SAVEGAME)); @@ -491,14 +518,9 @@ void SelectionTab::updateListItems() int elemIdx = slider->getValue(); for(auto item : listItems) { - if(elemIdx < curFolders.size()) + if(elemIdx < curItems.size()) { - item->updateItem(curFolders[elemIdx], elemIdx == selectionPos); - elemIdx++; - } - else if(elemIdx - curFolders.size() < curItems.size()) - { - item->updateItem(curItems[elemIdx - curFolders.size()], elemIdx - curFolders.size() == selectionPos); + item->updateItem(curItems[elemIdx], elemIdx == selectionPos); elemIdx++; } else @@ -555,13 +577,16 @@ void SelectionTab::selectFileName(std::string fname) selectAbs(0); } -std::shared_ptr SelectionTab::getSelectedMapInfo() const +std::shared_ptr SelectionTab::getSelectedMapInfo() const { - return curItems.empty() && selectionPos <= curFolders.size() ? nullptr : curItems[selectionPos - curFolders.size()]; + return curItems.empty() ? nullptr : curItems[selectionPos]; } void SelectionTab::rememberCurrentSelection() { + if(getSelectedMapInfo()->isFolder) + return; + // TODO: this can be more elegant if(tabType == ESelectionScreen::newGame) { @@ -624,11 +649,13 @@ void SelectionTab::parseMaps(const std::unordered_set & files) { try { - auto mapInfo = std::make_shared(); + auto mapInfo = std::make_shared(); mapInfo->mapInit(file.getName()); if (isMapSupported(*mapInfo)) + { allItems.push_back(mapInfo); + } } catch(std::exception & e) { @@ -643,7 +670,7 @@ void SelectionTab::parseSaves(const std::unordered_set & files) { try { - auto mapInfo = std::make_shared(); + auto mapInfo = std::make_shared(); mapInfo->saveInit(file); // Filter out other game modes @@ -679,12 +706,13 @@ void SelectionTab::parseCampaigns(const std::unordered_set & files) allItems.reserve(files.size()); for(auto & file : files) { - auto info = std::make_shared(); + auto info = std::make_shared(); //allItems[i].date = std::asctime(std::localtime(&files[i].date)); info->fileURI = file.getName(); info->campaignInit(); - if(info->campaign) + if(info->campaign) { allItems.push_back(info); + } } } @@ -722,22 +750,7 @@ SelectionTab::ListItem::ListItem(Point position, std::shared_ptr ico iconLossCondition = std::make_shared(iconsLoss, 0, 0, 310, -12); } -void SelectionTab::ListItem::updateItem(std::string folderName, bool selected) -{ - labelAmountOfPlayers->disable(); - labelMapSizeLetter->disable(); - iconFormat->disable(); - iconVictoryCondition->disable(); - iconLossCondition->disable(); - labelNumberOfCampaignMaps->disable(); - labelName->enable(); - labelName->setText(folderName); - auto color = selected ? Colors::YELLOW : Colors::WHITE; - labelName->setColor(color); - return; -} - -void SelectionTab::ListItem::updateItem(std::shared_ptr info, bool selected) +void SelectionTab::ListItem::updateItem(std::shared_ptr info, bool selected) { if(!info) { @@ -752,6 +765,20 @@ void SelectionTab::ListItem::updateItem(std::shared_ptr info, bool sel } auto color = selected ? Colors::YELLOW : Colors::WHITE; + if(info->isFolder) + { + labelAmountOfPlayers->disable(); + labelMapSizeLetter->disable(); + iconFormat->disable(); + iconVictoryCondition->disable(); + iconLossCondition->disable(); + labelNumberOfCampaignMaps->disable(); + labelName->enable(); + labelName->setText(info->folderName); + labelName->setColor(color); + return; + } + if(info->campaign) { labelAmountOfPlayers->disable(); diff --git a/client/lobby/SelectionTab.h b/client/lobby/SelectionTab.h index 2ee167af0..f4118508f 100644 --- a/client/lobby/SelectionTab.h +++ b/client/lobby/SelectionTab.h @@ -10,6 +10,7 @@ #pragma once #include "CSelectionBase.h" +#include "../../lib/mapping/CMapInfo.h" class CSlider; class CLabel; @@ -19,12 +20,21 @@ enum ESortBy _playerAm, _size, _format, _name, _viccon, _loscon, _numOfMaps, _fileName }; //_numOfMaps is for campaigns +class ElementInfo : public CMapInfo +{ +public: + ElementInfo() : CMapInfo() { } + ~ElementInfo() { } + std::string folderName = ""; + bool isFolder = false; +}; + /// Class which handles map sorting by different criteria class mapSorter { public: ESortBy sortBy; - bool operator()(const std::shared_ptr aaa, const std::shared_ptr bbb); + bool operator()(const std::shared_ptr aaa, const std::shared_ptr bbb); mapSorter(ESortBy es) : sortBy(es){}; }; @@ -41,8 +51,7 @@ class SelectionTab : public CIntObject std::shared_ptr labelName; ListItem(Point position, std::shared_ptr iconsFormats, std::shared_ptr iconsVictory, std::shared_ptr iconsLoss); - void updateItem(std::string folderName, bool selected = false); - void updateItem(std::shared_ptr info = {}, bool selected = false); + void updateItem(std::shared_ptr info = {}, bool selected = false); }; std::vector> listItems; @@ -52,12 +61,11 @@ class SelectionTab : public CIntObject std::shared_ptr iconsLossCondition; public: - std::vector> allItems; - std::vector> curItems; - std::vector curFolders; + std::vector> allItems; + std::vector> curItems; std::string curFolder; size_t selectionPos; - std::function)> callOnSelect; + std::function)> callOnSelect; ESortBy sortingBy; ESortBy generalSortingBy; @@ -74,7 +82,7 @@ public: void showPopupWindow(const Point & cursorPosition) override; bool receiveEvent(const Point & position, int eventType) const override; - void filter(int size, bool selectFirst = false, std::string path = "MAPS"); //0 - all + void filter(int size, bool selectFirst = false); //0 - all void sortBy(int criteria); void sort(); void select(int position); //position: <0 - positions> position on the screen @@ -84,7 +92,7 @@ public: int getLine() const; int getLine(const Point & position) const; void selectFileName(std::string fname); - std::shared_ptr getSelectedMapInfo() const; + std::shared_ptr getSelectedMapInfo() const; void rememberCurrentSelection(); void restoreLastSelection(); @@ -98,6 +106,8 @@ private: ESelectionScreen tabType; Rect inputNameRect; + std::tuple checkSubfolder(std::string path); + bool isMapSupported(const CMapInfo & info); void parseMaps(const std::unordered_set & files); void parseSaves(const std::unordered_set & files);