Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added search by author #3837

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/levels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ bool levParse_JSON(const std::string& mountPoint, const std::string& filename, s
abort();
return false;
}

psDataSet->players = 1;
psDataSet->game = -1;
psDataSet->dataDir = pathMode;
Expand Down Expand Up @@ -360,6 +360,7 @@ bool levParse_JSON(const std::string& mountPoint, const std::string& filename, s
}
psDataSet->players = static_cast<SWORD>(levelDetails.value().players);
psDataSet->pName = levelDetails.value().name;
psDataSet->pAuthor = levelDetails.value().author;
auto gamFilePath = mapIO.pathJoin(mapIO.pathDirName(customMountPoint), levelDetails.value().gamFilePath());
psDataSet->apDataFiles[0] = gamFilePath;
psDataSet->game = 0;
Expand Down
1 change: 1 addition & 0 deletions src/levels.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct LEVEL_DATASET
SWORD players = 0; // number of players for the map
SWORD game = 0; // index of WRF/WDG that loads the scenario file
std::string pName; // title for the level
std::string pAuthor; // map author
searchPathMode dataDir = mod_clean; // title for the level
std::array<std::string, LEVEL_MAXFILES> apDataFiles; // the WRF/GAM files for the level
// in load order
Expand Down
38 changes: 32 additions & 6 deletions src/multimenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "lib/widget/multibutform.h"
#include "lib/widget/paneltabbutton.h"
#include "lib/widget/editbox.h"
#include "lib/widget/checkbox.h"

#include "display3d.h"
#include "intdisplay.h"
Expand Down Expand Up @@ -82,6 +83,7 @@ bool MultiMenuUp = false;
static UDWORD current_context = 0;
UDWORD current_numplayers = 4;
static std::string current_searchString;
static bool current_searchByAuthor;

#define MULTIMENU_FORM_Y 25

Expand Down Expand Up @@ -392,7 +394,7 @@ void multiMenuScreenSizeDidChange(unsigned int oldWidth, unsigned int oldHeight,
* \param mode (purpose unknown)
* \param numPlayers (purpose unknown)
*/
void addMultiRequest(const char *searchDir, const char *fileExtension, UDWORD mode, UBYTE numPlayers, std::string const &searchString)
void addMultiRequest(const char *searchDir, const char *fileExtension, UDWORD mode, UBYTE numPlayers, std::string const &searchString, bool searchByAuthor)
{
const size_t extensionLength = strlen(fileExtension);
const unsigned int buttonsX = (mode == MULTIOP_MAP) ? 22 : 17;
Expand All @@ -403,6 +405,7 @@ void addMultiRequest(const char *searchDir, const char *fileExtension, UDWORD mo
// only save these when they select MAP button
current_numplayers = numPlayers;
current_searchString = searchString;
current_searchByAuthor = searchByAuthor;
}

psRScreen = W_SCREEN::make(); ///< move this to intinit or somewhere like that.. (close too.)
Expand Down Expand Up @@ -480,10 +483,31 @@ void addMultiRequest(const char *searchDir, const char *fileExtension, UDWORD mo

if (mode == MULTIOP_MAP)
{
// Add the author filter checkbox
auto authorCheckbox = std::make_shared<WzCheckboxButton>();
requestForm->attach(authorCheckbox);
authorCheckbox->setString(_("Author"));
authorCheckbox->setIsChecked(current_searchByAuthor);
authorCheckbox->setTextColor(WZCOL_TEXT_BRIGHT);
Vector2i authorCbDimentions = authorCheckbox->calculateDesiredDimensions();
authorCheckbox->setGeometry(requestForm->width() - authorCbDimentions.x - 3,
requestForm->height() - MULTIOP_SEARCHBOXH - 3,
authorCbDimentions.x,
authorCbDimentions.y);

authorCheckbox->addOnClickHandler([searchDir, fileExtension, mode, numPlayers](W_BUTTON& widg) {
bool searchByAuthor = static_cast<WzCheckboxButton*>(&widg)->getIsChecked();
closeMultiRequester();
addMultiRequest(searchDir, fileExtension, mode, numPlayers, current_searchString, searchByAuthor);
});

// Add the search edit box
auto searchBox = std::make_shared<W_EDITBOX>();
requestForm->attach(searchBox);
searchBox->setGeometry(3, requestForm->height() - MULTIOP_SEARCHBOXH - 3, requestForm->width() - 6, MULTIOP_SEARCHBOXH);
searchBox->setGeometry(3,
requestForm->height() - MULTIOP_SEARCHBOXH - 3,
requestForm->width() - authorCbDimentions.x - 9,
MULTIOP_SEARCHBOXH);
searchBox->setBoxColours(WZCOL_MENU_BORDER, WZCOL_MENU_BORDER, WZCOL_MENU_BACKGROUND);
searchBox->setPlaceholder(_("Search for map"));
searchBox->setString(WzString::fromUtf8(current_searchString));
Expand All @@ -504,11 +528,13 @@ void addMultiRequest(const char *searchDir, const char *fileExtension, UDWORD mo
for (auto mapData : levels)
{
std::string withoutTechlevel = mapNameWithoutTechlevel(mapData->pName.c_str());
std::string tip = withoutTechlevel +
(mapData->pAuthor.empty() ? "" : "\n" + astringf(_("By %s"), mapData->pAuthor.c_str()));
// add number of players to string.
auto button = std::make_shared<W_BUTTON>();
requestList->attach(button);
button->id = nextButtonId;
button->setTip(withoutTechlevel);
button->setTip(tip);
button->setString(WzString::fromUtf8(withoutTechlevel));
button->displayFunction = displayRequestOption;
button->pUserData = new DisplayRequestOptionData(mapData);
Expand All @@ -517,7 +543,7 @@ void addMultiRequest(const char *searchDir, const char *fileExtension, UDWORD mo
delete static_cast<DisplayRequestOptionData *>(psWidget->pUserData);
psWidget->pUserData = nullptr;
});
buttons.push_back({stringRelevance(mapData->pName, searchString), button});
buttons.push_back({stringRelevance(current_searchByAuthor ? mapData->pAuthor : mapData->pName, searchString), button});

++nextButtonId;
}
Expand Down Expand Up @@ -634,15 +660,15 @@ bool runMultiRequester(UDWORD id, UDWORD *mode, WzString *chosen, LEVEL_DATASET
{
case M_REQUEST_AP:
closeMultiRequester();
addMultiRequest(MultiCustomMapsPath, ".wrf", MULTIOP_MAP, 0, current_searchString);
addMultiRequest(MultiCustomMapsPath, ".wrf", MULTIOP_MAP, 0, current_searchString, current_searchByAuthor);
break;
default:
for (unsigned numPlayers = 2; numPlayers <= MAX_PLAYERS_IN_GUI; ++numPlayers)
{
if (id == M_REQUEST_NP[numPlayers - 2])
{
closeMultiRequester();
addMultiRequest(MultiCustomMapsPath, ".wrf", MULTIOP_MAP, numPlayers, current_searchString);
addMultiRequest(MultiCustomMapsPath, ".wrf", MULTIOP_MAP, numPlayers, current_searchString, current_searchByAuthor);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/multimenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "stringdef.h"

// requester
void addMultiRequest(const char *searchDir, const char *fileExtension, UDWORD id, UBYTE numPlayers, std::string const &searchString = std::string());
void addMultiRequest(const char *searchDir, const char *fileExtension, UDWORD id, UBYTE numPlayers, std::string const &searchString = std::string(), bool searchByAuthor = false);
void closeMultiRequester();

extern bool multiRequestUp;
Expand Down
Loading