1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Refactoring of main menu related code

We need to make sure it's not stay active during gameplay.
Proper fix would require actually removing menu interface from GH, but then GH needs refactoring too.
This commit is contained in:
Arseniy Shestakov 2018-04-07 18:42:11 +07:00
parent 5c09f751b3
commit 4af288aee4
9 changed files with 27 additions and 35 deletions

View File

@ -1225,7 +1225,6 @@ static void handleEvent(SDL_Event & ev)
case EUserEvent::RETURN_TO_MAIN_MENU:
{
CSH->endGameplay();
GH.curInt = CMainMenu::create();
GH.defActionsDef = 63;
}
break;
@ -1237,7 +1236,6 @@ static void handleEvent(SDL_Event & ev)
case EUserEvent::CAMPAIGN_START_SCENARIO:
{
CSH->endGameplay();
GH.curInt = CMainMenu::create();
auto ourCampaign = std::shared_ptr<CCampaignState>(reinterpret_cast<CCampaignState *>(ev.user.data1));
auto & epilogue = ourCampaign->camp->scenarios[ourCampaign->mapsConquered.back()].epilog;
auto finisher = [=]()
@ -1259,11 +1257,8 @@ static void handleEvent(SDL_Event & ev)
break;
case EUserEvent::RETURN_TO_MENU_LOAD:
CSH->endGameplay();
CMainMenu::create();
GH.defActionsDef = 63;
CMM->update();
CMM->menu->switchToTab(vstd::find_pos(CMM->menu->menuNameToEntry, "load"));
GH.curInt = CMM;
CMM->menu->switchToTab("load");
break;
case EUserEvent::FULLSCREEN_TOGGLED:
fullScreenChanged();

View File

@ -447,6 +447,8 @@ void CServerHandler::sendStartGame(bool allowOnlyAI) const
void CServerHandler::startGameplay()
{
if(CMM)
CMM->disable();
client = new CClient();
switch(si->mode)
@ -477,6 +479,16 @@ void CServerHandler::endGameplay(bool closeConnection)
// Tell the network thread to reach a stable state
CSH->sendClientDisconnecting();
logNetwork->info("Closed connection.");
if(CMM)
{
GH.curInt = CMM;
CMM->enable();
}
else
{
GH.curInt = CMainMenu::create();
}
}
void CServerHandler::startCampaignScenario(std::shared_ptr<CCampaignState> cs)

View File

@ -114,7 +114,7 @@ bool LobbyStartGame::applyOnLobbyHandler(CServerHandler * handler)
void LobbyStartGame::applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler)
{
CMM->showLoadingScreen(std::bind(&CServerHandler::startGameplay, handler));
GH.pushInt(new CLoadingScreen(std::bind(&CServerHandler::startGameplay, handler)));
}
bool LobbyUpdateState::applyOnLobbyHandler(CServerHandler * handler)

View File

@ -433,7 +433,6 @@ void CBonusSelection::startMap()
GH.popInt(this);
LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], [=]()
{
GH.curInt = CMainMenu::create();
showPrologVideo();
}, 0);
}

View File

@ -71,8 +71,6 @@ PlayerInfo ISelectionScreenInfo::getPlayerInfo(int color)
CSelectionBase::CSelectionBase(ESelectionScreen type)
: CWindowObject(BORDERED | SHADOW_DISABLED), ISelectionScreenInfo(type)
{
CMainMenu::create(); //we depend on its graphics
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
IShowActivatable::type = BLOCK_ADV_HOTKEYS;
pos.w = 762;

View File

@ -101,7 +101,7 @@ CMenuScreen::CMenuScreen(const JsonNode & configNode)
std::shared_ptr<CIntObject> CMenuScreen::createTab(size_t index)
{
if(config["items"].Vector().size() == index)
return std::make_shared<CreditsScreen>();
return std::make_shared<CreditsScreen>(this->pos);
else
return std::make_shared<CMenuEntry>(this, config["items"].Vector()[index]);
}
@ -134,6 +134,11 @@ void CMenuScreen::switchToTab(size_t index)
tabs->setActive(index);
}
void CMenuScreen::switchToTab(std::string name)
{
switchToTab(vstd::find_pos(menuNameToEntry, name));
}
//funciton for std::string -> std::function conversion for main menu
static std::function<void()> genCommand(CMenuScreen * menu, std::vector<std::string> menuType, const std::string & string)
{
@ -156,7 +161,7 @@ static std::function<void()> genCommand(CMenuScreen * menu, std::vector<std::str
{
size_t index2 = std::find(menuType.begin(), menuType.end(), commands.front()) - menuType.begin();
if(index2 != menuType.size())
return std::bind(&CMenuScreen::switchToTab, menu, index2);
return std::bind((void(CMenuScreen::*)(size_t))&CMenuScreen::switchToTab, menu, index2);
break;
}
case 1: //open campaign selection window
@ -354,21 +359,6 @@ CMainMenu * CMainMenu::create()
return CMM;
}
void CMainMenu::removeFromGui()
{
//remove everything but main menu and background
GH.popInts(GH.listInt.size() - 2);
GH.popInt(GH.topInt()); //remove main menu
GH.popInt(GH.topInt()); //remove background
}
void CMainMenu::showLoadingScreen(std::function<void()> loader)
{
if(GH.listInt.size() && GH.listInt.front() == CMM)
CMM->removeFromGui();
GH.pushInt(new CLoadingScreen(loader));
}
std::shared_ptr<CPicture> CMainMenu::createPicture(const JsonNode & config)
{
return std::make_shared<CPicture>(config["name"].String(), config["x"].Float(), config["y"].Float());

View File

@ -54,6 +54,7 @@ public:
void deactivate() override;
void switchToTab(size_t index);
void switchToTab(std::string name);
};
class CMenuEntry : public CIntObject
@ -140,8 +141,6 @@ public:
void openCampaignScreen(std::string name);
static CMainMenu * create();
void removeFromGui();
static void showLoadingScreen(std::function<void()> loader);
static std::shared_ptr<CPicture> createPicture(const JsonNode & config);

View File

@ -18,14 +18,13 @@
#include "../../lib/filesystem/Filesystem.h"
CreditsScreen::CreditsScreen()
: positionCounter(0)
CreditsScreen::CreditsScreen(Rect rect)
: CIntObject(LCLICK | RCLICK), positionCounter(0)
{
addUsedEvents(LCLICK | RCLICK);
pos.w = rect.w;
pos.h = rect.h;
type |= REDRAW_PARENT;
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
pos.w = CMM->menu->pos.w;
pos.h = CMM->menu->pos.h;
auto textFile = CResourceHandler::get()->load(ResourceID("DATA/CREDITS.TXT"))->readAll();
std::string text((char *)textFile.first.get(), textFile.second);
size_t firstQuote = text.find('\"') + 1;

View File

@ -20,7 +20,7 @@ class CreditsScreen : public CIntObject
std::shared_ptr<CMultiLineLabel> credits;
public:
CreditsScreen();
CreditsScreen(Rect rect);
void show(SDL_Surface * to) override;
void clickLeft(tribool down, bool previousState) override;
void clickRight(tribool down, bool previousState) override;