2023-01-29 18:36:33 +02:00
|
|
|
/*
|
2023-02-12 18:49:41 +02:00
|
|
|
* GeneralOptionsTab.cpp, part of VCMI engine
|
2023-01-29 18:36:33 +02:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2023-02-12 18:49:41 +02:00
|
|
|
#include "GeneralOptionsTab.h"
|
2023-01-29 18:36:33 +02:00
|
|
|
|
2023-02-12 18:29:23 +02:00
|
|
|
#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 "windows/GUIClasses.h"
|
2023-01-29 18:36:33 +02:00
|
|
|
#include "CServerHandler.h"
|
2023-02-12 12:55:21 +02:00
|
|
|
#include "renderSDL/SDL_Extensions.h"
|
2023-01-29 18:36:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-02-12 18:49:41 +02:00
|
|
|
GeneralOptionsTab::GeneralOptionsTab()
|
2023-01-29 18:36:33 +02:00
|
|
|
: InterfaceObjectConfigurable(),
|
|
|
|
onFullscreenChanged(settings.listen["video"]["fullscreen"])
|
|
|
|
{
|
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
|
|
|
|
|
2023-02-12 18:49:41 +02:00
|
|
|
const JsonNode config(ResourceID("config/widgets/settings/generalOptionsTab.json"));
|
2023-02-18 18:49:43 +02:00
|
|
|
addCallback("spellbookAnimationChanged", [](bool value)
|
|
|
|
{
|
|
|
|
return setBoolSetting("video", "spellbookAnimation", value);
|
|
|
|
});
|
|
|
|
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();
|
|
|
|
});
|
2023-02-14 00:53:28 +02:00
|
|
|
//settings that do not belong to base game:
|
2023-02-18 18:49:43 +02:00
|
|
|
addCallback("fullscreenChanged", [this](bool value)
|
|
|
|
{
|
|
|
|
setFullscreenMode(value);
|
|
|
|
});
|
|
|
|
addCallback("setGameResolution", [this](int dummyValue)
|
|
|
|
{
|
|
|
|
selectGameResolution();
|
|
|
|
});
|
|
|
|
addCallback("framerateChanged", [](bool value)
|
|
|
|
{
|
|
|
|
setBoolSetting("general", "showfps", value);
|
|
|
|
});
|
2023-02-18 17:35:07 +02:00
|
|
|
//moved from "other" tab that is disabled for now to avoid excessible tabs with barely any content
|
2023-02-18 18:49:43 +02:00
|
|
|
addCallback("availableCreaturesAsDwellingLabelChanged", [](bool value)
|
|
|
|
{
|
|
|
|
setBoolSetting("gameTweaks", "availableCreaturesAsDwellingLabel", value);
|
|
|
|
});
|
|
|
|
addCallback("compactTownCreatureInfoChanged", [](bool value)
|
|
|
|
{
|
|
|
|
return setBoolSetting("gameTweaks", "compactTownCreatureInfo", value);
|
|
|
|
});
|
2023-01-29 18:36:33 +02:00
|
|
|
build(config);
|
|
|
|
|
|
|
|
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<CToggleButton> spellbookAnimationCheckbox = widget<CToggleButton>("spellbookAnimationCheckbox");
|
2023-02-18 18:49:43 +02:00
|
|
|
spellbookAnimationCheckbox->setSelected(settings["video"]["spellbookAnimation"].Bool());
|
2023-01-29 18:36:33 +02:00
|
|
|
|
|
|
|
std::shared_ptr<CToggleButton> fullscreenCheckbox = widget<CToggleButton>("fullscreenCheckbox");
|
2023-02-18 18:49:43 +02:00
|
|
|
fullscreenCheckbox->setSelected(settings["video"]["fullscreen"].Bool());
|
2023-01-29 18:36:33 +02:00
|
|
|
onFullscreenChanged([&](const JsonNode &newState) //used when pressing F4 etc. to change fullscreen checkbox state
|
2023-01-31 00:26:33 +02:00
|
|
|
{
|
|
|
|
widget<CToggleButton>("fullscreenCheckbox")->setSelected(newState.Bool());
|
|
|
|
});
|
2023-01-29 18:36:33 +02:00
|
|
|
|
2023-02-14 00:53:28 +02:00
|
|
|
std::shared_ptr<CToggleButton> framerateCheckbox = widget<CToggleButton>("framerateCheckbox");
|
2023-02-18 18:49:43 +02:00
|
|
|
framerateCheckbox->setSelected(settings["general"]["showfps"].Bool());
|
2023-01-29 18:36:33 +02:00
|
|
|
|
|
|
|
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());
|
2023-02-18 17:35:07 +02:00
|
|
|
|
|
|
|
std::shared_ptr<CToggleButton> availableCreaturesAsDwellingLabelCheckbox = widget<CToggleButton>("availableCreaturesAsDwellingLabelCheckbox");
|
2023-02-18 18:49:43 +02:00
|
|
|
availableCreaturesAsDwellingLabelCheckbox->setSelected(settings["gameTweaks"]["availableCreaturesAsDwellingLabel"].Bool());
|
2023-02-18 17:35:07 +02:00
|
|
|
|
|
|
|
std::shared_ptr<CToggleButton> compactTownCreatureInfo = widget<CToggleButton>("compactTownCreatureInfoCheckbox");
|
2023-02-18 18:49:43 +02:00
|
|
|
compactTownCreatureInfo->setSelected(settings["gameTweaks"]["compactTownCreatureInfo"].Bool());
|
2023-01-29 18:36:33 +02:00
|
|
|
}
|
|
|
|
|
2023-01-31 00:26:33 +02:00
|
|
|
|
2023-02-12 18:49:41 +02:00
|
|
|
bool GeneralOptionsTab::isResolutionSupported(const Point & resolution)
|
2023-02-12 16:03:33 +02:00
|
|
|
{
|
|
|
|
return isResolutionSupported( resolution, settings["video"]["fullscreen"].Bool());
|
|
|
|
}
|
2023-01-29 18:36:33 +02:00
|
|
|
|
2023-02-12 18:49:41 +02:00
|
|
|
bool GeneralOptionsTab::isResolutionSupported(const Point & resolution, bool fullscreen)
|
2023-01-29 18:36:33 +02:00
|
|
|
{
|
2023-02-12 16:03:33 +02:00
|
|
|
if (!fullscreen)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto supportedList = CSDL_Ext::getSupportedResolutions();
|
2023-01-29 18:36:33 +02:00
|
|
|
|
2023-02-12 16:03:33 +02:00
|
|
|
return CSDL_Ext::isResolutionSupported(supportedList, resolution);
|
|
|
|
}
|
2023-01-29 18:36:33 +02:00
|
|
|
|
2023-02-12 18:49:41 +02:00
|
|
|
void GeneralOptionsTab::selectGameResolution()
|
2023-02-12 16:03:33 +02:00
|
|
|
{
|
|
|
|
fillSelectableResolutions();
|
|
|
|
|
|
|
|
std::vector<std::string> items;
|
2023-01-29 18:36:33 +02:00
|
|
|
size_t currentResolutionIndex = 0;
|
|
|
|
size_t i = 0;
|
2023-02-12 16:03:33 +02:00
|
|
|
for(const auto & it : selectableResolutions)
|
2023-01-29 18:36:33 +02:00
|
|
|
{
|
2023-02-12 16:03:33 +02:00
|
|
|
auto resolutionStr = resolutionToString(it.x, it.y);
|
2023-01-29 18:36:33 +02:00
|
|
|
if(widget<CLabel>("resolutionLabel")->getText() == resolutionStr)
|
|
|
|
currentResolutionIndex = i;
|
2023-02-12 16:03:33 +02:00
|
|
|
|
2023-01-29 18:36:33 +02:00
|
|
|
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"),
|
2023-02-18 18:49:43 +02:00
|
|
|
[this](int index)
|
|
|
|
{
|
|
|
|
setGameResolution(index);
|
|
|
|
},
|
2023-01-29 18:36:33 +02:00
|
|
|
currentResolutionIndex);
|
|
|
|
}
|
|
|
|
|
2023-02-12 18:49:41 +02:00
|
|
|
void GeneralOptionsTab::setGameResolution(int index)
|
2023-01-29 18:36:33 +02:00
|
|
|
{
|
2023-02-12 16:03:33 +02:00
|
|
|
assert(index >= 0 && index < selectableResolutions.size());
|
|
|
|
|
|
|
|
if ( index < 0 || index >= selectableResolutions.size() )
|
|
|
|
return;
|
2023-01-29 18:36:33 +02:00
|
|
|
|
2023-02-12 16:03:33 +02:00
|
|
|
Point resolution = selectableResolutions[index];
|
2023-01-29 18:36:33 +02:00
|
|
|
|
|
|
|
Settings gameRes = settings.write["video"]["screenRes"];
|
2023-02-12 16:03:33 +02:00
|
|
|
gameRes["width"].Float() = resolution.x;
|
|
|
|
gameRes["height"].Float() = resolution.y;
|
2023-01-29 18:36:33 +02:00
|
|
|
|
|
|
|
std::string resText;
|
2023-02-12 16:03:33 +02:00
|
|
|
resText += std::to_string(resolution.x);
|
2023-01-29 18:36:33 +02:00
|
|
|
resText += "x";
|
2023-02-12 16:03:33 +02:00
|
|
|
resText += std::to_string(resolution.y);
|
2023-01-29 18:36:33 +02:00
|
|
|
widget<CLabel>("resolutionLabel")->setText(resText);
|
2023-02-12 16:03:33 +02:00
|
|
|
}
|
|
|
|
|
2023-02-12 18:49:41 +02:00
|
|
|
void GeneralOptionsTab::setFullscreenMode(bool on)
|
2023-02-12 16:03:33 +02:00
|
|
|
{
|
|
|
|
fillSelectableResolutions();
|
|
|
|
|
|
|
|
const auto & screenRes = settings["video"]["screenRes"];
|
|
|
|
const Point desiredResolution(screenRes["width"].Integer(), screenRes["height"].Integer());
|
2023-02-18 17:35:07 +02:00
|
|
|
const Point currentResolution = GH.screenDimensions();
|
2023-02-12 16:03:33 +02:00
|
|
|
|
|
|
|
if (!isResolutionSupported(currentResolution, on))
|
|
|
|
{
|
|
|
|
widget<CToggleButton>("fullscreenCheckbox")->setSelected(!on);
|
|
|
|
LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.systemOptions.fullscreenFailed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setBoolSetting("video", "fullscreen", on);
|
|
|
|
|
|
|
|
if (!isResolutionSupported(desiredResolution, on))
|
|
|
|
{
|
|
|
|
// user changed his desired resolution and switched to fullscreen
|
|
|
|
// however resolution he selected before is not available in fullscreen
|
|
|
|
// so reset it back to currect resolution which is confirmed to be supported earlier
|
|
|
|
Settings gameRes = settings.write["video"]["screenRes"];
|
|
|
|
gameRes["width"].Float() = currentResolution.x;
|
|
|
|
gameRes["height"].Float() = currentResolution.y;
|
|
|
|
|
|
|
|
widget<CLabel>("resolutionLabel")->setText(resolutionToString(currentResolution.x, currentResolution.y));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 18:49:41 +02:00
|
|
|
void GeneralOptionsTab::fillSelectableResolutions()
|
2023-02-12 16:03:33 +02:00
|
|
|
{
|
|
|
|
selectableResolutions.clear();
|
|
|
|
|
|
|
|
for(const auto & it : conf.guiOptions)
|
|
|
|
{
|
|
|
|
const Point dimensions(it.first.first, it.first.second);
|
|
|
|
|
|
|
|
if(isResolutionSupported(dimensions))
|
|
|
|
selectableResolutions.push_back(dimensions);
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::range::sort(selectableResolutions, [](const auto & left, const auto & right)
|
|
|
|
{
|
|
|
|
return left.x * left.y < right.x * right.y;
|
|
|
|
});
|
2023-01-29 18:36:33 +02:00
|
|
|
}
|