1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-15 20:03:15 +02:00

Extract options stuff to separate file

This commit is contained in:
Dydzio
2023-01-29 17:36:33 +01:00
parent 0fdfd8bdcb
commit e3578d0bc8
6 changed files with 265 additions and 223 deletions

View File

@@ -50,6 +50,7 @@ set(client_SRCS
windows/GUIClasses.cpp
windows/InfoWindows.cpp
windows/QuickRecruitmentWindow.cpp
windows/SystemOptionsWindow.cpp
mainmenu/CMainMenu.cpp
mainmenu/CCampaignScreen.cpp
@@ -138,6 +139,7 @@ set(client_HEADERS
windows/GUIClasses.h
windows/InfoWindows.h
windows/QuickRecruitmentWindow.h
windows/SystemOptionsWindow.h
mainmenu/CMainMenu.h
mainmenu/CCampaignScreen.h

View File

@@ -36,6 +36,7 @@
#include "../gui/CGuiHandler.h"
#include "../gui/SDL_Extensions.h"
#include "../widgets/MiscWidgets.h"
#include "../windows/SystemOptionsWindow.h"
#include "../../CCallback.h"
@@ -817,7 +818,7 @@ void CAdvMapInt::fadventureOPtions()
void CAdvMapInt::fsystemOptions()
{
GH.pushIntT<CSystemOptionsWindow>();
GH.pushIntT<SystemOptionsWindow>();
}
void CAdvMapInt::fnextHero()

View File

@@ -425,205 +425,6 @@ CLevelWindow::~CLevelWindow()
LOCPLINT->showingDialog->setn(false);
}
static void setIntSetting(std::string group, std::string field, int value)
{
Settings entry = settings.write[group][field];
entry->Float() = value;
}
static void setBoolSetting(std::string group, std::string field, bool value)
{
Settings fullscreen = settings.write[group][field];
fullscreen->Bool() = value;
}
static std::string resolutionToString(int w, int h)
{
return std::to_string(w) + 'x' + std::to_string(h);
}
CSystemOptionsWindow::CSystemOptionsWindow()
: InterfaceObjectConfigurable(),
onFullscreenChanged(settings.listen["video"]["fullscreen"])
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
const JsonNode config(ResourceID("config/widgets/optionsMenu.json"));
addCallback("loadGame", [this](int) { loadGameButtonCallback(); });
addCallback("saveGame", [this](int) { saveGameButtonCallback(); });
addCallback("restartGame", [this](int) { restartGameButtonCallback(); });
addCallback("quitGame", [this](int) { quitGameButtonCallback(); });
addCallback("returnToMainMenu", [this](int) { mainMenuButtonCallback(); });
addCallback("closeWindow", [this](int) { backButtonCallback(); });
addCallback("playerHeroSpeedChanged", std::bind(&setIntSetting, "adventure", "heroSpeed", _1));
addCallback("enemyHeroSpeedChanged", std::bind(&setIntSetting, "adventure", "enemySpeed", _1));
addCallback("mapScrollSpeedChanged", std::bind(&setIntSetting, "adventure", "scrollSpeed", _1));
addCallback("heroReminderChanged", std::bind(&setBoolSetting, "adventure", "heroReminder", _1));
addCallback("quickCombatChanged", std::bind(&setBoolSetting, "adventure", "quickCombat", _1));
addCallback("spellbookAnimationChanged", std::bind(&setBoolSetting, "video", "spellbookAnimation", _1));
addCallback("fullscreenChanged", std::bind(&setBoolSetting, "video", "fullscreen", _1));
addCallback("setGameResolution", std::bind(&CSystemOptionsWindow::selectGameResolution, this));
addCallback("setMusic", [this](int value) { setIntSetting("general", "music", value); widget<CSlider>("musicSlider")->redraw(); });
addCallback("setVolume", [this](int value) { setIntSetting("general", "sound", value); widget<CSlider>("soundVolumeSlider")->redraw(); });
build(config);
std::shared_ptr<CPicture> background = widget<CPicture>("background");
pos.w = background->pos.w;
pos.h = background->pos.h;
pos = center();
std::shared_ptr<CLabel> resolutionLabel = widget<CLabel>("resolutionLabel");
const auto & currentResolution = settings["video"]["screenRes"];
resolutionLabel->setText(resolutionToString(currentResolution["width"].Integer(), currentResolution["height"].Integer()));
std::shared_ptr<CToggleGroup> playerHeroSpeedToggle = widget<CToggleGroup>("heroMovementSpeedPicker");
playerHeroSpeedToggle->setSelected((int)settings["adventure"]["heroSpeed"].Float());
std::shared_ptr<CToggleGroup> enemyHeroSpeedToggle = widget<CToggleGroup>("enemyMovementSpeedPicker");
enemyHeroSpeedToggle->setSelected((int)settings["adventure"]["enemySpeed"].Float());
std::shared_ptr<CToggleGroup> mapScrollSpeedToggle = widget<CToggleGroup>("mapScrollSpeedPicker");
mapScrollSpeedToggle->setSelected((int)settings["adventure"]["scrollSpeed"].Float());
std::shared_ptr<CToggleButton> heroReminderCheckbox = widget<CToggleButton>("heroReminderCheckbox");
heroReminderCheckbox->setSelected((bool)settings["adventure"]["heroReminder"].Bool());
std::shared_ptr<CToggleButton> quickCombatCheckbox = widget<CToggleButton>("quickCombatCheckbox");
quickCombatCheckbox->setSelected((bool)settings["adventure"]["quickCombat"].Bool());
std::shared_ptr<CToggleButton> spellbookAnimationCheckbox = widget<CToggleButton>("spellbookAnimationCheckbox");
spellbookAnimationCheckbox->setSelected((bool)settings["video"]["spellbookAnimation"].Bool());
std::shared_ptr<CToggleButton> fullscreenCheckbox = widget<CToggleButton>("fullscreenCheckbox");
fullscreenCheckbox->setSelected((bool)settings["video"]["fullscreen"].Bool());
onFullscreenChanged([&](const JsonNode &newState) //used when pressing F4 etc. to change fullscreen checkbox state
{
widget<CToggleButton>("fullscreenCheckbox")->setSelected(newState.Bool());
});
std::shared_ptr<CSlider> musicSlider = widget<CSlider>("musicSlider");
musicSlider->moveTo(CCS->musich->getVolume());
std::shared_ptr<CSlider> volumeSlider = widget<CSlider>("soundVolumeSlider");
volumeSlider->moveTo(CCS->soundh->getVolume());
std::shared_ptr<CButton> loadButton = widget<CButton>("loadButton");
assert(loadButton);
std::shared_ptr<CButton> saveButton = widget<CButton>("saveButton");
assert(saveButton);
std::shared_ptr<CButton> restartButton = widget<CButton>("restartButton");
assert(restartButton);
if(CSH->isGuest())
{
loadButton->block(true);
saveButton->block(true);
restartButton->block(true);
}
}
void CSystemOptionsWindow::close()
{
if(GH.topInt().get() != this)
logGlobal->error("Only top interface must be closed");
GH.popInts(1);
}
void CSystemOptionsWindow::selectGameResolution()
{
std::vector<std::string> items;
#ifndef VCMI_IOS
SDL_Rect displayBounds;
SDL_GetDisplayBounds(std::max(0, SDL_GetWindowDisplayIndex(mainWindow)), &displayBounds);
#endif
size_t currentResolutionIndex = 0;
size_t i = 0;
for(const auto & it : conf.guiOptions)
{
const auto & resolution = it.first;
#ifndef VCMI_IOS
if(displayBounds.w < resolution.first || displayBounds.h < resolution.second)
continue;
#endif
auto resolutionStr = resolutionToString(resolution.first, resolution.second);
if(widget<CLabel>("resolutionLabel")->getText() == resolutionStr)
currentResolutionIndex = i;
items.push_back(std::move(resolutionStr));
++i;
}
GH.pushIntT<CObjectListWindow>(items, nullptr,
CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.hover"),
CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.help"),
std::bind(&CSystemOptionsWindow::setGameResolution, this, _1),
currentResolutionIndex);
}
void CSystemOptionsWindow::setGameResolution(int index)
{
auto iter = conf.guiOptions.begin();
std::advance(iter, index);
//do not set resolution to illegal one (0x0)
assert(iter!=conf.guiOptions.end() && iter->first.first > 0 && iter->first.second > 0);
Settings gameRes = settings.write["video"]["screenRes"];
gameRes["width"].Float() = iter->first.first;
gameRes["height"].Float() = iter->first.second;
std::string resText;
resText += boost::lexical_cast<std::string>(iter->first.first);
resText += "x";
resText += boost::lexical_cast<std::string>(iter->first.second);
widget<CLabel>("resolutionLabel")->setText(resText);
}
void CSystemOptionsWindow::quitGameButtonCallback()
{
LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this](){ closeAndPushEvent(SDL_USEREVENT, EUserEvent::FORCE_QUIT); }, 0);
}
void CSystemOptionsWindow::backButtonCallback()
{
close();
}
void CSystemOptionsWindow::mainMenuButtonCallback()
{
LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this](){ closeAndPushEvent(SDL_USEREVENT, EUserEvent::RETURN_TO_MAIN_MENU); }, 0);
}
void CSystemOptionsWindow::loadGameButtonCallback()
{
close();
LOCPLINT->proposeLoadingGame();
}
void CSystemOptionsWindow::saveGameButtonCallback()
{
close();
GH.pushIntT<CSavingScreen>();
}
void CSystemOptionsWindow::restartGameButtonCallback()
{
LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], [this](){ closeAndPushEvent(SDL_USEREVENT, EUserEvent::RESTART_GAME); }, 0);
}
void CSystemOptionsWindow::closeAndPushEvent(int eventType, int code)
{
close();
GH.pushSDLEvent(eventType, code);
}
CTavernWindow::CTavernWindow(const CGObjectInstance * TavernObj)
: CStatusbarWindow(PLAYER_COLORED, "TPTAVERN"),

View File

@@ -17,7 +17,6 @@
#include "../widgets/CArtifactHolder.h"
#include "../widgets/CGarrisonInt.h"
#include "../widgets/Images.h"
#include "gui/InterfaceObjectConfigurable.h"
VCMI_LIB_NAMESPACE_BEGIN
@@ -197,28 +196,6 @@ public:
void keyPressed (const SDL_KeyboardEvent & key) override;
};
class CSystemOptionsWindow : public InterfaceObjectConfigurable
{
private:
SettingsListener onFullscreenChanged;
//functions bound to buttons
void loadGameButtonCallback(); //load game
void saveGameButtonCallback(); //save game
void quitGameButtonCallback(); //quit game
void backButtonCallback(); //return to game
void restartGameButtonCallback(); //restart game
void mainMenuButtonCallback(); //return to main menu
void close(); //TODO: copypaste of WindowBase::close(), consider changing Windowbase to IWindowbase with default close() implementation and changing WindowBase inheritance to CIntObject + IWindowBase
void selectGameResolution();
void setGameResolution(int index);
void closeAndPushEvent(int eventType, int code = 0);
public:
CSystemOptionsWindow();
};
class CTavernWindow : public CStatusbarWindow
{
public:

View File

@@ -0,0 +1,226 @@
/*
* SystemOptionsWindow.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 "SystemOptionsWindow.h"
#include "../lib/CGeneralTextHandler.h"
#include "../lib/filesystem/ResourceID.h"
#include "../gui/CGuiHandler.h"
#include "../widgets/Buttons.h"
#include "../widgets/TextControls.h"
#include "../widgets/Images.h"
#include "../CGameInfo.h"
#include "../CMusicHandler.h"
#include "../CPlayerInterface.h"
#include "GUIClasses.h"
#include "CServerHandler.h"
#include "lobby/CSavingScreen.h"
static void setIntSetting(std::string group, std::string field, int value)
{
Settings entry = settings.write[group][field];
entry->Float() = value;
}
static void setBoolSetting(std::string group, std::string field, bool value)
{
Settings fullscreen = settings.write[group][field];
fullscreen->Bool() = value;
}
static std::string resolutionToString(int w, int h)
{
return std::to_string(w) + 'x' + std::to_string(h);
}
SystemOptionsWindow::SystemOptionsWindow()
: InterfaceObjectConfigurable(),
onFullscreenChanged(settings.listen["video"]["fullscreen"])
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
const JsonNode config(ResourceID("config/widgets/optionsMenu.json"));
addCallback("loadGame", [this](int) { loadGameButtonCallback(); });
addCallback("saveGame", [this](int) { saveGameButtonCallback(); });
addCallback("restartGame", [this](int) { restartGameButtonCallback(); });
addCallback("quitGame", [this](int) { quitGameButtonCallback(); });
addCallback("returnToMainMenu", [this](int) { mainMenuButtonCallback(); });
addCallback("closeWindow", [this](int) { backButtonCallback(); });
addCallback("playerHeroSpeedChanged", std::bind(&setIntSetting, "adventure", "heroSpeed", _1));
addCallback("enemyHeroSpeedChanged", std::bind(&setIntSetting, "adventure", "enemySpeed", _1));
addCallback("mapScrollSpeedChanged", std::bind(&setIntSetting, "adventure", "scrollSpeed", _1));
addCallback("heroReminderChanged", std::bind(&setBoolSetting, "adventure", "heroReminder", _1));
addCallback("quickCombatChanged", std::bind(&setBoolSetting, "adventure", "quickCombat", _1));
addCallback("spellbookAnimationChanged", std::bind(&setBoolSetting, "video", "spellbookAnimation", _1));
addCallback("fullscreenChanged", std::bind(&setBoolSetting, "video", "fullscreen", _1));
addCallback("setGameResolution", std::bind(&SystemOptionsWindow::selectGameResolution, this));
addCallback("setMusic", [this](int value) { setIntSetting("general", "music", value); widget<CSlider>("musicSlider")->redraw(); });
addCallback("setVolume", [this](int value) { setIntSetting("general", "sound", value); widget<CSlider>("soundVolumeSlider")->redraw(); });
build(config);
std::shared_ptr<CPicture> background = widget<CPicture>("background");
pos.w = background->pos.w;
pos.h = background->pos.h;
pos = center();
std::shared_ptr<CLabel> resolutionLabel = widget<CLabel>("resolutionLabel");
const auto & currentResolution = settings["video"]["screenRes"];
resolutionLabel->setText(resolutionToString(currentResolution["width"].Integer(), currentResolution["height"].Integer()));
std::shared_ptr<CToggleGroup> playerHeroSpeedToggle = widget<CToggleGroup>("heroMovementSpeedPicker");
playerHeroSpeedToggle->setSelected((int)settings["adventure"]["heroSpeed"].Float());
std::shared_ptr<CToggleGroup> enemyHeroSpeedToggle = widget<CToggleGroup>("enemyMovementSpeedPicker");
enemyHeroSpeedToggle->setSelected((int)settings["adventure"]["enemySpeed"].Float());
std::shared_ptr<CToggleGroup> mapScrollSpeedToggle = widget<CToggleGroup>("mapScrollSpeedPicker");
mapScrollSpeedToggle->setSelected((int)settings["adventure"]["scrollSpeed"].Float());
std::shared_ptr<CToggleButton> heroReminderCheckbox = widget<CToggleButton>("heroReminderCheckbox");
heroReminderCheckbox->setSelected((bool)settings["adventure"]["heroReminder"].Bool());
std::shared_ptr<CToggleButton> quickCombatCheckbox = widget<CToggleButton>("quickCombatCheckbox");
quickCombatCheckbox->setSelected((bool)settings["adventure"]["quickCombat"].Bool());
std::shared_ptr<CToggleButton> spellbookAnimationCheckbox = widget<CToggleButton>("spellbookAnimationCheckbox");
spellbookAnimationCheckbox->setSelected((bool)settings["video"]["spellbookAnimation"].Bool());
std::shared_ptr<CToggleButton> fullscreenCheckbox = widget<CToggleButton>("fullscreenCheckbox");
fullscreenCheckbox->setSelected((bool)settings["video"]["fullscreen"].Bool());
onFullscreenChanged([&](const JsonNode &newState) //used when pressing F4 etc. to change fullscreen checkbox state
{
widget<CToggleButton>("fullscreenCheckbox")->setSelected(newState.Bool());
});
std::shared_ptr<CSlider> musicSlider = widget<CSlider>("musicSlider");
musicSlider->moveTo(CCS->musich->getVolume());
std::shared_ptr<CSlider> volumeSlider = widget<CSlider>("soundVolumeSlider");
volumeSlider->moveTo(CCS->soundh->getVolume());
std::shared_ptr<CButton> loadButton = widget<CButton>("loadButton");
assert(loadButton);
std::shared_ptr<CButton> saveButton = widget<CButton>("saveButton");
assert(saveButton);
std::shared_ptr<CButton> restartButton = widget<CButton>("restartButton");
assert(restartButton);
if(CSH->isGuest())
{
loadButton->block(true);
saveButton->block(true);
restartButton->block(true);
}
}
void SystemOptionsWindow::close()
{
if(GH.topInt().get() != this)
logGlobal->error("Only top interface must be closed");
GH.popInts(1);
}
void SystemOptionsWindow::selectGameResolution()
{
std::vector<std::string> items;
#ifndef VCMI_IOS
SDL_Rect displayBounds;
SDL_GetDisplayBounds(std::max(0, SDL_GetWindowDisplayIndex(mainWindow)), &displayBounds);
#endif
size_t currentResolutionIndex = 0;
size_t i = 0;
for(const auto & it : conf.guiOptions)
{
const auto & resolution = it.first;
#ifndef VCMI_IOS
if(displayBounds.w < resolution.first || displayBounds.h < resolution.second)
continue;
#endif
auto resolutionStr = resolutionToString(resolution.first, resolution.second);
if(widget<CLabel>("resolutionLabel")->getText() == resolutionStr)
currentResolutionIndex = i;
items.push_back(std::move(resolutionStr));
++i;
}
GH.pushIntT<CObjectListWindow>(items, nullptr,
CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.hover"),
CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.help"),
std::bind(&SystemOptionsWindow::setGameResolution, this, _1),
currentResolutionIndex);
}
void SystemOptionsWindow::setGameResolution(int index)
{
auto iter = conf.guiOptions.begin();
std::advance(iter, index);
//do not set resolution to illegal one (0x0)
assert(iter!=conf.guiOptions.end() && iter->first.first > 0 && iter->first.second > 0);
Settings gameRes = settings.write["video"]["screenRes"];
gameRes["width"].Float() = iter->first.first;
gameRes["height"].Float() = iter->first.second;
std::string resText;
resText += boost::lexical_cast<std::string>(iter->first.first);
resText += "x";
resText += boost::lexical_cast<std::string>(iter->first.second);
widget<CLabel>("resolutionLabel")->setText(resText);
}
void SystemOptionsWindow::quitGameButtonCallback()
{
LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this](){ closeAndPushEvent(SDL_USEREVENT, EUserEvent::FORCE_QUIT); }, 0);
}
void SystemOptionsWindow::backButtonCallback()
{
close();
}
void SystemOptionsWindow::mainMenuButtonCallback()
{
LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this](){ closeAndPushEvent(SDL_USEREVENT, EUserEvent::RETURN_TO_MAIN_MENU); }, 0);
}
void SystemOptionsWindow::loadGameButtonCallback()
{
close();
LOCPLINT->proposeLoadingGame();
}
void SystemOptionsWindow::saveGameButtonCallback()
{
close();
GH.pushIntT<CSavingScreen>();
}
void SystemOptionsWindow::restartGameButtonCallback()
{
LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], [this](){ closeAndPushEvent(SDL_USEREVENT, EUserEvent::RESTART_GAME); }, 0);
}
void SystemOptionsWindow::closeAndPushEvent(int eventType, int code)
{
close();
GH.pushSDLEvent(eventType, code);
}

View File

@@ -0,0 +1,35 @@
/*
* SystemOptionsWindow.h, 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
*
*/
#pragma once
#include "../lib/CConfigHandler.h"
#include "gui/InterfaceObjectConfigurable.h"
class SystemOptionsWindow : public InterfaceObjectConfigurable
{
private:
SettingsListener onFullscreenChanged;
//functions bound to buttons
void loadGameButtonCallback(); //load game
void saveGameButtonCallback(); //save game
void quitGameButtonCallback(); //quit game
void backButtonCallback(); //return to game
void restartGameButtonCallback(); //restart game
void mainMenuButtonCallback(); //return to main menu
void close(); //TODO: copypaste of WindowBase::close(), consider changing Windowbase to IWindowbase with default close() implementation and changing WindowBase inheritance to CIntObject + IWindowBase
void selectGameResolution();
void setGameResolution(int index);
void closeAndPushEvent(int eventType, int code = 0);
public:
SystemOptionsWindow();
};