2018-01-05 19:21:07 +02:00
|
|
|
/*
|
|
|
|
* SelectionTab.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
|
|
|
|
#include "SelectionTab.h"
|
|
|
|
#include "CSelectionBase.h"
|
|
|
|
#include "CLobbyScreen.h"
|
|
|
|
|
|
|
|
#include "../CGameInfo.h"
|
|
|
|
#include "../CPlayerInterface.h"
|
|
|
|
#include "../CServerHandler.h"
|
|
|
|
#include "../gui/CGuiHandler.h"
|
2023-04-27 19:21:06 +02:00
|
|
|
#include "../gui/Shortcut.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../widgets/CComponent.h"
|
|
|
|
#include "../widgets/Buttons.h"
|
|
|
|
#include "../widgets/MiscWidgets.h"
|
|
|
|
#include "../widgets/ObjectLists.h"
|
2023-05-30 16:31:45 +02:00
|
|
|
#include "../widgets/Slider.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../widgets/TextControls.h"
|
|
|
|
#include "../windows/GUIClasses.h"
|
|
|
|
#include "../windows/InfoWindows.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../render/CAnimation.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
#include "../../CCallback.h"
|
|
|
|
|
|
|
|
#include "../../lib/NetPacksLobby.h"
|
|
|
|
#include "../../lib/CGeneralTextHandler.h"
|
2023-04-30 00:03:50 +02:00
|
|
|
#include "../../lib/CConfigHandler.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../../lib/CModHandler.h"
|
2023-03-15 21:34:29 +02:00
|
|
|
#include "../../lib/GameSettings.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../../lib/filesystem/Filesystem.h"
|
2023-06-25 21:28:24 +02:00
|
|
|
#include "../../lib/campaign/CampaignState.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../../lib/mapping/CMapInfo.h"
|
2023-05-24 01:05:59 +02:00
|
|
|
#include "../../lib/mapping/CMapHeader.h"
|
2023-05-24 00:14:06 +02:00
|
|
|
#include "../../lib/mapping/MapFormat.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../../lib/serializer/Connection.h"
|
|
|
|
|
|
|
|
bool mapSorter::operator()(const std::shared_ptr<CMapInfo> aaa, const std::shared_ptr<CMapInfo> bbb)
|
|
|
|
{
|
|
|
|
auto a = aaa->mapHeader.get();
|
|
|
|
auto b = bbb->mapHeader.get();
|
|
|
|
if(a && b) //if we are sorting scenarios
|
|
|
|
{
|
|
|
|
switch(sortBy)
|
|
|
|
{
|
|
|
|
case _format: //by map format (RoE, WoG, etc)
|
|
|
|
return (a->version < b->version);
|
|
|
|
break;
|
|
|
|
case _loscon: //by loss conditions
|
2023-06-18 12:51:11 +02:00
|
|
|
return (a->defeatIconIndex < b->defeatIconIndex);
|
2018-01-05 19:21:07 +02:00
|
|
|
break;
|
|
|
|
case _playerAm: //by player amount
|
|
|
|
int playerAmntB, humenPlayersB, playerAmntA, humenPlayersA;
|
|
|
|
playerAmntB = humenPlayersB = playerAmntA = humenPlayersA = 0;
|
|
|
|
for(int i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
if(a->players[i].canHumanPlay)
|
|
|
|
{
|
|
|
|
playerAmntA++;
|
|
|
|
humenPlayersA++;
|
|
|
|
}
|
|
|
|
else if(a->players[i].canComputerPlay)
|
|
|
|
{
|
|
|
|
playerAmntA++;
|
|
|
|
}
|
|
|
|
if(b->players[i].canHumanPlay)
|
|
|
|
{
|
|
|
|
playerAmntB++;
|
|
|
|
humenPlayersB++;
|
|
|
|
}
|
|
|
|
else if(b->players[i].canComputerPlay)
|
|
|
|
{
|
|
|
|
playerAmntB++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(playerAmntB != playerAmntA)
|
|
|
|
return (playerAmntA < playerAmntB);
|
|
|
|
else
|
|
|
|
return (humenPlayersA < humenPlayersB);
|
|
|
|
break;
|
|
|
|
case _size: //by size of map
|
|
|
|
return (a->width < b->width);
|
|
|
|
break;
|
|
|
|
case _viccon: //by victory conditions
|
2023-06-18 12:51:11 +02:00
|
|
|
return (a->victoryIconIndex < b->victoryIconIndex);
|
2018-01-05 19:21:07 +02:00
|
|
|
break;
|
|
|
|
case _name: //by name
|
|
|
|
return boost::ilexicographical_compare(a->name, b->name);
|
|
|
|
case _fileName: //by filename
|
|
|
|
return boost::ilexicographical_compare(aaa->fileURI, bbb->fileURI);
|
|
|
|
default:
|
|
|
|
return boost::ilexicographical_compare(a->name, b->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else //if we are sorting campaigns
|
|
|
|
{
|
|
|
|
switch(sortBy)
|
|
|
|
{
|
|
|
|
case _numOfMaps: //by number of maps in campaign
|
2023-04-17 02:26:35 +02:00
|
|
|
return aaa->campaignHeader->numberOfScenarios <
|
|
|
|
bbb->campaignHeader->numberOfScenarios;
|
2018-01-05 19:21:07 +02:00
|
|
|
break;
|
|
|
|
case _name: //by name
|
|
|
|
return boost::ilexicographical_compare(aaa->campaignHeader->name, bbb->campaignHeader->name);
|
|
|
|
default:
|
|
|
|
return boost::ilexicographical_compare(aaa->campaignHeader->name, bbb->campaignHeader->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-08 11:04:05 +02:00
|
|
|
// pick sorting order based on selection
|
|
|
|
static ESortBy getSortBySelectionScreen(ESelectionScreen Type)
|
|
|
|
{
|
|
|
|
switch(Type)
|
|
|
|
{
|
|
|
|
case ESelectionScreen::newGame:
|
|
|
|
return ESortBy::_name;
|
|
|
|
case ESelectionScreen::loadGame:
|
|
|
|
case ESelectionScreen::saveGame:
|
|
|
|
return ESortBy::_fileName;
|
|
|
|
case ESelectionScreen::campaignList:
|
|
|
|
return ESortBy::_name;
|
|
|
|
}
|
|
|
|
// Should not reach here. But let's not crash the game.
|
|
|
|
return ESortBy::_name;
|
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
SelectionTab::SelectionTab(ESelectionScreen Type)
|
2023-05-29 18:02:23 +02:00
|
|
|
: CIntObject(LCLICK | KEYBOARD | DOUBLECLICK), callOnSelect(nullptr), tabType(Type), selectionPos(0), sortModeAscending(true), inputNameRect{32, 539, 350, 20}
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
OBJ_CONSTRUCTION;
|
2021-05-08 11:04:05 +02:00
|
|
|
|
|
|
|
generalSortingBy = getSortBySelectionScreen(tabType);
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
if(tabType != ESelectionScreen::campaignList)
|
|
|
|
{
|
|
|
|
sortingBy = _format;
|
|
|
|
background = std::make_shared<CPicture>("SCSELBCK.bmp", 0, 6);
|
|
|
|
pos = background->pos;
|
2021-03-24 20:02:12 +02:00
|
|
|
inputName = std::make_shared<CTextInput>(inputNameRect, Point(-32, -25), "GSSTRIP.bmp", 0);
|
2018-01-05 19:21:07 +02:00
|
|
|
inputName->filters += CTextInput::filenameFilter;
|
2022-11-26 23:12:20 +02:00
|
|
|
labelMapSizes = std::make_shared<CLabel>(87, 62, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[510]);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
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<CButton>(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"};
|
|
|
|
for(int i = 0; i < 6; i++)
|
|
|
|
{
|
|
|
|
ESortBy criteria = (ESortBy)i;
|
|
|
|
if(criteria == _name)
|
|
|
|
criteria = generalSortingBy;
|
|
|
|
|
|
|
|
buttonsSortBy.push_back(std::make_shared<CButton>(Point(xpos[i], 86), sortIconNames[i], CGI->generaltexth->zelp[107 + i], std::bind(&SelectionTab::sortBy, this, criteria)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int positionsToShow = 18;
|
|
|
|
std::string tabTitle;
|
|
|
|
switch(tabType)
|
|
|
|
{
|
|
|
|
case ESelectionScreen::newGame:
|
|
|
|
tabTitle = CGI->generaltexth->arraytxt[229];
|
|
|
|
break;
|
|
|
|
case ESelectionScreen::loadGame:
|
|
|
|
tabTitle = CGI->generaltexth->arraytxt[230];
|
|
|
|
break;
|
|
|
|
case ESelectionScreen::saveGame:
|
|
|
|
positionsToShow = 16;
|
|
|
|
tabTitle = CGI->generaltexth->arraytxt[231];
|
|
|
|
break;
|
|
|
|
case ESelectionScreen::campaignList:
|
|
|
|
tabTitle = CGI->generaltexth->allTexts[726];
|
|
|
|
type |= REDRAW_PARENT; // we use parent background so we need to make sure it's will be redrawn too
|
|
|
|
pos.w = parent->pos.w;
|
|
|
|
pos.h = parent->pos.h;
|
|
|
|
pos.x += 3;
|
|
|
|
pos.y += 6;
|
|
|
|
|
|
|
|
buttonsSortBy.push_back(std::make_shared<CButton>(Point(23, 86), "CamCusM.DEF", CButton::tooltip(), std::bind(&SelectionTab::sortBy, this, _numOfMaps)));
|
|
|
|
buttonsSortBy.push_back(std::make_shared<CButton>(Point(55, 86), "CamCusL.DEF", CButton::tooltip(), std::bind(&SelectionTab::sortBy, this, _name)));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
iconsMapFormats = std::make_shared<CAnimation>("SCSELC.DEF");
|
|
|
|
iconsMapFormats->preload();
|
|
|
|
iconsVictoryCondition = std::make_shared<CAnimation>("SCNRVICT.DEF");
|
|
|
|
iconsVictoryCondition->preload();
|
|
|
|
iconsLossCondition = std::make_shared<CAnimation>("SCNRLOSS.DEF");
|
|
|
|
iconsLossCondition->preload();
|
|
|
|
for(int i = 0; i < positionsToShow; i++)
|
|
|
|
listItems.push_back(std::make_shared<ListItem>(Point(30, 129 + i * 25), iconsMapFormats, iconsVictoryCondition, iconsLossCondition));
|
|
|
|
|
2022-11-26 23:12:20 +02:00
|
|
|
labelTabTitle = std::make_shared<CLabel>(205, 28, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, tabTitle);
|
2023-06-22 22:49:38 +02:00
|
|
|
slider = std::make_shared<CSlider>(Point(372, 86), tabType != ESelectionScreen::saveGame ? 480 : 430, std::bind(&SelectionTab::sliderMove, this, _1), positionsToShow, (int)curItems.size(), 0, Orientation::VERTICAL, CSlider::BLUE);
|
2023-05-29 12:08:08 +02:00
|
|
|
slider->setPanningStep(24);
|
2023-06-22 22:49:38 +02:00
|
|
|
|
|
|
|
// create scroll bounds that encompass all area in this UI element to the left of slider (including area of slider itself)
|
|
|
|
// entire screen can't be used in here since map description might also have slider
|
|
|
|
slider->setScrollBounds(Rect(pos.x - slider->pos.x, 0, slider->pos.x + slider->pos.w - pos.x, slider->pos.h ));
|
2018-01-05 19:21:07 +02:00
|
|
|
filter(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTab::toggleMode()
|
|
|
|
{
|
|
|
|
if(CSH->isGuest())
|
|
|
|
{
|
|
|
|
allItems.clear();
|
|
|
|
curItems.clear();
|
|
|
|
if(slider)
|
|
|
|
slider->block(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch(tabType)
|
|
|
|
{
|
|
|
|
case ESelectionScreen::newGame:
|
|
|
|
inputName->disable();
|
|
|
|
parseMaps(getFiles("Maps/", EResType::MAP));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ESelectionScreen::loadGame:
|
|
|
|
inputName->disable();
|
2023-03-13 11:10:44 +02:00
|
|
|
parseSaves(getFiles("Saves/", EResType::SERVER_SAVEGAME));
|
2018-01-05 19:21:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ESelectionScreen::saveGame:
|
2023-03-13 11:10:44 +02:00
|
|
|
parseSaves(getFiles("Saves/", EResType::SERVER_SAVEGAME));
|
2018-01-05 19:21:07 +02:00
|
|
|
inputName->enable();
|
|
|
|
restoreLastSelection();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ESelectionScreen::campaignList:
|
|
|
|
parseCampaigns(getFiles("Maps/", EResType::CAMPAIGN));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(slider)
|
|
|
|
{
|
|
|
|
slider->block(false);
|
|
|
|
filter(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(CSH->campaignStateToSend)
|
|
|
|
{
|
|
|
|
CSH->setCampaignState(CSH->campaignStateToSend);
|
|
|
|
CSH->campaignStateToSend.reset();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
restoreLastSelection();
|
|
|
|
}
|
|
|
|
}
|
2020-10-01 10:38:06 +02:00
|
|
|
slider->setAmount((int)curItems.size());
|
2018-01-05 19:21:07 +02:00
|
|
|
updateListItems();
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTab::clickLeft(tribool down, bool previousState)
|
|
|
|
{
|
|
|
|
if(down)
|
|
|
|
{
|
|
|
|
int line = getLine();
|
2022-05-07 11:56:34 +02:00
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
if(line != -1)
|
2022-05-07 11:56:34 +02:00
|
|
|
{
|
2018-01-05 19:21:07 +02:00
|
|
|
select(line);
|
2022-05-07 11:56:34 +02:00
|
|
|
}
|
2021-03-24 20:02:12 +02:00
|
|
|
#ifdef VCMI_IOS
|
2023-01-27 00:27:06 +02:00
|
|
|
// focus input field if clicked inside it
|
2023-05-19 17:56:20 +02:00
|
|
|
else if(inputName && inputName->isActive() && inputNameRect.isInside(GH.getCursorPosition()))
|
2023-01-27 00:27:06 +02:00
|
|
|
inputName->giveFocus();
|
2021-03-24 20:02:12 +02:00
|
|
|
#endif
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-07 11:56:34 +02:00
|
|
|
|
2023-04-27 19:21:06 +02:00
|
|
|
void SelectionTab::keyPressed(EShortcut key)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
int moveBy = 0;
|
2023-02-02 18:02:25 +02:00
|
|
|
switch(key)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-04-28 13:22:03 +02:00
|
|
|
case EShortcut::MOVE_UP:
|
2018-01-05 19:21:07 +02:00
|
|
|
moveBy = -1;
|
|
|
|
break;
|
2023-04-28 13:22:03 +02:00
|
|
|
case EShortcut::MOVE_DOWN:
|
2018-01-05 19:21:07 +02:00
|
|
|
moveBy = +1;
|
|
|
|
break;
|
2023-04-28 13:22:03 +02:00
|
|
|
case EShortcut::MOVE_PAGE_UP:
|
2020-10-01 10:38:06 +02:00
|
|
|
moveBy = -(int)listItems.size() + 1;
|
2018-01-05 19:21:07 +02:00
|
|
|
break;
|
2023-04-28 13:22:03 +02:00
|
|
|
case EShortcut::MOVE_PAGE_DOWN:
|
2020-10-01 10:38:06 +02:00
|
|
|
moveBy = +(int)listItems.size() - 1;
|
2018-01-05 19:21:07 +02:00
|
|
|
break;
|
2023-04-28 13:22:03 +02:00
|
|
|
case EShortcut::MOVE_FIRST:
|
2018-01-05 19:21:07 +02:00
|
|
|
select(-slider->getValue());
|
|
|
|
return;
|
2023-04-28 13:22:03 +02:00
|
|
|
case EShortcut::MOVE_LAST:
|
2020-10-01 10:38:06 +02:00
|
|
|
select((int)curItems.size() - slider->getValue());
|
2018-01-05 19:21:07 +02:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2020-10-01 10:38:06 +02:00
|
|
|
select((int)selectionPos - slider->getValue() + moveBy);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-05-26 20:46:09 +02:00
|
|
|
void SelectionTab::clickDouble()
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
if(getLine() != -1) //double clicked scenarios list
|
|
|
|
{
|
2023-05-26 14:55:58 +02:00
|
|
|
(static_cast<CLobbyScreen *>(parent))->buttonStart->clickLeft(true, false);
|
2018-01-05 19:21:07 +02:00
|
|
|
(static_cast<CLobbyScreen *>(parent))->buttonStart->clickLeft(false, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A new size filter (Small, Medium, ...) has been selected. Populate
|
|
|
|
// selMaps with the relevant data.
|
|
|
|
void SelectionTab::filter(int size, bool selectFirst)
|
|
|
|
{
|
|
|
|
curItems.clear();
|
|
|
|
|
|
|
|
if(tabType == ESelectionScreen::campaignList)
|
|
|
|
{
|
|
|
|
for(auto elem : allItems)
|
|
|
|
curItems.push_back(elem);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(auto elem : allItems)
|
|
|
|
{
|
2023-04-02 18:56:10 +02:00
|
|
|
if(elem->mapHeader && (!size || elem->mapHeader->width == size))
|
2018-01-05 19:21:07 +02:00
|
|
|
curItems.push_back(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(curItems.size())
|
|
|
|
{
|
|
|
|
slider->block(false);
|
2020-10-01 10:38:06 +02:00
|
|
|
slider->setAmount((int)curItems.size());
|
2018-01-05 19:21:07 +02:00
|
|
|
sort();
|
|
|
|
if(selectFirst)
|
|
|
|
{
|
2023-05-30 17:10:22 +02:00
|
|
|
slider->scrollTo(0);
|
2018-01-05 19:21:07 +02:00
|
|
|
callOnSelect(curItems[0]);
|
|
|
|
selectAbs(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
slider->block(true);
|
|
|
|
if(callOnSelect)
|
|
|
|
callOnSelect(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTab::sortBy(int criteria)
|
|
|
|
{
|
|
|
|
if(criteria == sortingBy)
|
|
|
|
{
|
|
|
|
sortModeAscending = !sortModeAscending;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sortingBy = (ESortBy)criteria;
|
|
|
|
sortModeAscending = true;
|
|
|
|
}
|
|
|
|
sort();
|
|
|
|
|
|
|
|
selectAbs(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTab::sort()
|
|
|
|
{
|
|
|
|
if(sortingBy != generalSortingBy)
|
|
|
|
std::stable_sort(curItems.begin(), curItems.end(), mapSorter(generalSortingBy));
|
|
|
|
std::stable_sort(curItems.begin(), curItems.end(), mapSorter(sortingBy));
|
|
|
|
|
|
|
|
if(!sortModeAscending)
|
|
|
|
std::reverse(curItems.begin(), curItems.end());
|
|
|
|
|
|
|
|
updateListItems();
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTab::select(int position)
|
|
|
|
{
|
|
|
|
if(!curItems.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// New selection. py is the index in curItems.
|
|
|
|
int py = position + slider->getValue();
|
|
|
|
vstd::amax(py, 0);
|
|
|
|
vstd::amin(py, curItems.size() - 1);
|
|
|
|
|
|
|
|
selectionPos = py;
|
|
|
|
|
|
|
|
if(position < 0)
|
2023-05-30 17:10:22 +02:00
|
|
|
slider->scrollBy(position);
|
2018-01-05 19:21:07 +02:00
|
|
|
else if(position >= listItems.size())
|
2023-05-30 17:10:22 +02:00
|
|
|
slider->scrollBy(position - (int)listItems.size() + 1);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
rememberCurrentSelection();
|
|
|
|
|
2023-05-17 22:22:45 +02:00
|
|
|
if(inputName && inputName->isActive())
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
auto filename = *CResourceHandler::get("local")->getResourceName(ResourceID(curItems[py]->fileURI, EResType::CLIENT_SAVEGAME));
|
|
|
|
inputName->setText(filename.stem().string());
|
|
|
|
}
|
|
|
|
updateListItems();
|
|
|
|
if(callOnSelect)
|
|
|
|
callOnSelect(curItems[py]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTab::selectAbs(int position)
|
|
|
|
{
|
|
|
|
select(position - slider->getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTab::sliderMove(int slidPos)
|
|
|
|
{
|
|
|
|
if(!slider)
|
|
|
|
return; // ignore spurious call when slider is being created
|
|
|
|
updateListItems();
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTab::updateListItems()
|
|
|
|
{
|
|
|
|
// elemIdx is the index of the maps or saved game to display on line 0
|
|
|
|
// slider->capacity contains the number of available screen lines
|
|
|
|
// slider->positionsAmnt is the number of elements after filtering
|
|
|
|
int elemIdx = slider->getValue();
|
|
|
|
for(auto item : listItems)
|
|
|
|
{
|
|
|
|
if(elemIdx < curItems.size())
|
|
|
|
{
|
|
|
|
item->updateItem(curItems[elemIdx], elemIdx == selectionPos);
|
|
|
|
elemIdx++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item->updateItem();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int SelectionTab::getLine()
|
|
|
|
{
|
|
|
|
int line = -1;
|
2023-01-27 00:27:06 +02:00
|
|
|
Point clickPos = GH.getCursorPosition() - pos.topLeft();
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
// Ignore clicks on save name area
|
|
|
|
int maxPosY;
|
|
|
|
if(tabType == ESelectionScreen::saveGame)
|
|
|
|
maxPosY = 516;
|
|
|
|
else
|
|
|
|
maxPosY = 564;
|
|
|
|
|
|
|
|
if(clickPos.y > 115 && clickPos.y < maxPosY && clickPos.x > 22 && clickPos.x < 371)
|
|
|
|
{
|
|
|
|
line = (clickPos.y - 115) / 25; //which line
|
|
|
|
}
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTab::selectFileName(std::string fname)
|
|
|
|
{
|
|
|
|
boost::to_upper(fname);
|
2020-10-01 10:38:06 +02:00
|
|
|
for(int i = (int)curItems.size() - 1; i >= 0; i--)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
if(curItems[i]->fileURI == fname)
|
|
|
|
{
|
2023-05-30 17:10:22 +02:00
|
|
|
slider->scrollTo(i);
|
2018-01-05 19:21:07 +02:00
|
|
|
selectAbs(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
selectAbs(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CMapInfo> SelectionTab::getSelectedMapInfo() const
|
|
|
|
{
|
|
|
|
return curItems.empty() ? nullptr : curItems[selectionPos];
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTab::rememberCurrentSelection()
|
|
|
|
{
|
|
|
|
// TODO: this can be more elegant
|
|
|
|
if(tabType == ESelectionScreen::newGame)
|
|
|
|
{
|
|
|
|
Settings lastMap = settings.write["general"]["lastMap"];
|
|
|
|
lastMap->String() = getSelectedMapInfo()->fileURI;
|
|
|
|
}
|
|
|
|
else if(tabType == ESelectionScreen::loadGame)
|
|
|
|
{
|
|
|
|
Settings lastSave = settings.write["general"]["lastSave"];
|
|
|
|
lastSave->String() = getSelectedMapInfo()->fileURI;
|
|
|
|
}
|
|
|
|
else if(tabType == ESelectionScreen::campaignList)
|
|
|
|
{
|
|
|
|
Settings lastCampaign = settings.write["general"]["lastCampaign"];
|
|
|
|
lastCampaign->String() = getSelectedMapInfo()->fileURI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTab::restoreLastSelection()
|
|
|
|
{
|
|
|
|
switch(tabType)
|
|
|
|
{
|
|
|
|
case ESelectionScreen::newGame:
|
|
|
|
selectFileName(settings["general"]["lastMap"].String());
|
|
|
|
break;
|
|
|
|
case ESelectionScreen::campaignList:
|
|
|
|
selectFileName(settings["general"]["lastCampaign"].String());
|
|
|
|
break;
|
|
|
|
case ESelectionScreen::loadGame:
|
|
|
|
case ESelectionScreen::saveGame:
|
|
|
|
selectFileName(settings["general"]["lastSave"].String());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-20 14:46:40 +02:00
|
|
|
bool SelectionTab::isMapSupported(const CMapInfo & info)
|
|
|
|
{
|
|
|
|
switch (info.mapHeader->version)
|
|
|
|
{
|
|
|
|
case EMapFormat::ROE:
|
|
|
|
return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_RESTORATION_OF_ERATHIA)["supported"].Bool();
|
|
|
|
case EMapFormat::AB:
|
|
|
|
return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_ARMAGEDDONS_BLADE)["supported"].Bool();
|
|
|
|
case EMapFormat::SOD:
|
|
|
|
return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_SHADOW_OF_DEATH)["supported"].Bool();
|
|
|
|
case EMapFormat::WOG:
|
|
|
|
return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_IN_THE_WAKE_OF_GODS)["supported"].Bool();
|
|
|
|
case EMapFormat::HOTA:
|
|
|
|
return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_HORN_OF_THE_ABYSS)["supported"].Bool();
|
|
|
|
case EMapFormat::VCMI:
|
2023-05-20 16:34:39 +02:00
|
|
|
return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_JSON_VCMI)["supported"].Bool();
|
2023-05-20 14:46:40 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
void SelectionTab::parseMaps(const std::unordered_set<ResourceID> & files)
|
|
|
|
{
|
|
|
|
logGlobal->debug("Parsing %d maps", files.size());
|
|
|
|
allItems.clear();
|
|
|
|
for(auto & file : files)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto mapInfo = std::make_shared<CMapInfo>();
|
|
|
|
mapInfo->mapInit(file.getName());
|
|
|
|
|
2023-05-20 14:46:40 +02:00
|
|
|
if (isMapSupported(*mapInfo))
|
2018-01-05 19:21:07 +02:00
|
|
|
allItems.push_back(mapInfo);
|
|
|
|
}
|
|
|
|
catch(std::exception & e)
|
|
|
|
{
|
|
|
|
logGlobal->error("Map %s is invalid. Message: %s", file.getName(), e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTab::parseSaves(const std::unordered_set<ResourceID> & files)
|
|
|
|
{
|
|
|
|
for(auto & file : files)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto mapInfo = std::make_shared<CMapInfo>();
|
|
|
|
mapInfo->saveInit(file);
|
|
|
|
|
|
|
|
// Filter out other game modes
|
|
|
|
bool isCampaign = mapInfo->scenarioOptionsOfSave->mode == StartInfo::CAMPAIGN;
|
|
|
|
bool isMultiplayer = mapInfo->amountOfHumanPlayersInSave > 1;
|
|
|
|
switch(CSH->getLoadMode())
|
|
|
|
{
|
|
|
|
case ELoadMode::SINGLE:
|
|
|
|
if(isMultiplayer || isCampaign)
|
|
|
|
mapInfo->mapHeader.reset();
|
|
|
|
break;
|
|
|
|
case ELoadMode::CAMPAIGN:
|
|
|
|
if(!isCampaign)
|
|
|
|
mapInfo->mapHeader.reset();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if(!isMultiplayer)
|
|
|
|
mapInfo->mapHeader.reset();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
allItems.push_back(mapInfo);
|
|
|
|
}
|
|
|
|
catch(const std::exception & e)
|
|
|
|
{
|
|
|
|
logGlobal->error("Error: Failed to process %s: %s", file.getName(), e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTab::parseCampaigns(const std::unordered_set<ResourceID> & files)
|
|
|
|
{
|
|
|
|
allItems.reserve(files.size());
|
|
|
|
for(auto & file : files)
|
|
|
|
{
|
|
|
|
auto info = std::make_shared<CMapInfo>();
|
|
|
|
//allItems[i].date = std::asctime(std::localtime(&files[i].date));
|
|
|
|
info->fileURI = file.getName();
|
|
|
|
info->campaignInit();
|
2023-04-09 01:24:40 +02:00
|
|
|
if(info->campaignHeader)
|
|
|
|
allItems.push_back(info);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unordered_set<ResourceID> SelectionTab::getFiles(std::string dirURI, int resType)
|
|
|
|
{
|
|
|
|
boost::to_upper(dirURI);
|
|
|
|
CResourceHandler::get()->updateFilteredFiles([&](const std::string & mount)
|
|
|
|
{
|
|
|
|
return boost::algorithm::starts_with(mount, dirURI);
|
|
|
|
});
|
|
|
|
|
|
|
|
std::unordered_set<ResourceID> ret = CResourceHandler::get()->getFilteredFiles([&](const ResourceID & ident)
|
|
|
|
{
|
|
|
|
return ident.getType() == resType && boost::algorithm::starts_with(ident.getName(), dirURI);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelectionTab::ListItem::ListItem(Point position, std::shared_ptr<CAnimation> iconsFormats, std::shared_ptr<CAnimation> iconsVictory, std::shared_ptr<CAnimation> iconsLoss)
|
|
|
|
: CIntObject(LCLICK, position)
|
|
|
|
{
|
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
|
2022-11-26 23:12:20 +02:00
|
|
|
labelName = std::make_shared<CLabel>(184, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
|
2018-01-05 19:21:07 +02:00
|
|
|
labelName->setAutoRedraw(false);
|
2022-11-26 23:12:20 +02:00
|
|
|
labelAmountOfPlayers = std::make_shared<CLabel>(8, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
|
2018-01-05 19:21:07 +02:00
|
|
|
labelAmountOfPlayers->setAutoRedraw(false);
|
2022-11-26 23:12:20 +02:00
|
|
|
labelNumberOfCampaignMaps = std::make_shared<CLabel>(8, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
|
2018-01-05 19:21:07 +02:00
|
|
|
labelNumberOfCampaignMaps->setAutoRedraw(false);
|
2022-11-26 23:12:20 +02:00
|
|
|
labelMapSizeLetter = std::make_shared<CLabel>(41, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
|
2018-01-05 19:21:07 +02:00
|
|
|
labelMapSizeLetter->setAutoRedraw(false);
|
|
|
|
// FIXME: This -12 should not be needed, but for some reason CAnimImage displaced otherwise
|
|
|
|
iconFormat = std::make_shared<CAnimImage>(iconsFormats, 0, 0, 59, -12);
|
|
|
|
iconVictoryCondition = std::make_shared<CAnimImage>(iconsVictory, 0, 0, 277, -12);
|
|
|
|
iconLossCondition = std::make_shared<CAnimImage>(iconsLoss, 0, 0, 310, -12);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTab::ListItem::updateItem(std::shared_ptr<CMapInfo> info, bool selected)
|
|
|
|
{
|
|
|
|
if(!info)
|
|
|
|
{
|
|
|
|
labelAmountOfPlayers->disable();
|
|
|
|
labelMapSizeLetter->disable();
|
|
|
|
iconFormat->disable();
|
|
|
|
iconVictoryCondition->disable();
|
|
|
|
iconLossCondition->disable();
|
|
|
|
labelNumberOfCampaignMaps->disable();
|
|
|
|
labelName->disable();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto color = selected ? Colors::YELLOW : Colors::WHITE;
|
|
|
|
if(info->campaignHeader)
|
|
|
|
{
|
|
|
|
labelAmountOfPlayers->disable();
|
|
|
|
labelMapSizeLetter->disable();
|
|
|
|
iconFormat->disable();
|
|
|
|
iconVictoryCondition->disable();
|
|
|
|
iconLossCondition->disable();
|
|
|
|
labelNumberOfCampaignMaps->enable();
|
|
|
|
std::ostringstream ostr(std::ostringstream::out);
|
2023-04-17 02:26:35 +02:00
|
|
|
ostr << info->campaignHeader->numberOfScenarios;
|
2018-01-05 19:21:07 +02:00
|
|
|
labelNumberOfCampaignMaps->setText(ostr.str());
|
|
|
|
labelNumberOfCampaignMaps->setColor(color);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
labelNumberOfCampaignMaps->disable();
|
|
|
|
std::ostringstream ostr(std::ostringstream::out);
|
|
|
|
ostr << info->amountOfPlayersOnMap << "/" << info->amountOfHumanControllablePlayers;
|
|
|
|
labelAmountOfPlayers->enable();
|
|
|
|
labelAmountOfPlayers->setText(ostr.str());
|
|
|
|
labelAmountOfPlayers->setColor(color);
|
|
|
|
labelMapSizeLetter->enable();
|
|
|
|
labelMapSizeLetter->setText(info->getMapSizeName());
|
|
|
|
labelMapSizeLetter->setColor(color);
|
|
|
|
iconFormat->enable();
|
2023-05-20 16:34:39 +02:00
|
|
|
iconFormat->setFrame(info->getMapSizeFormatIconId());
|
2018-01-05 19:21:07 +02:00
|
|
|
iconVictoryCondition->enable();
|
|
|
|
iconVictoryCondition->setFrame(info->mapHeader->victoryIconIndex, 0);
|
|
|
|
iconLossCondition->enable();
|
|
|
|
iconLossCondition->setFrame(info->mapHeader->defeatIconIndex, 0);
|
|
|
|
}
|
|
|
|
labelName->enable();
|
|
|
|
labelName->setText(info->getNameForList());
|
|
|
|
labelName->setColor(color);
|
|
|
|
}
|