1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/client/windows/settings/GeneralOptionsTab.cpp

418 lines
13 KiB
C++
Raw Normal View History

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 "CGameInfo.h"
#include "CMusicHandler.h"
#include "CPlayerInterface.h"
2023-01-29 18:36:33 +02:00
#include "CServerHandler.h"
#include "render/IScreenHandler.h"
2023-06-16 11:59:20 +02:00
#include "windows/GUIClasses.h"
#include "../../eventsSDL/InputHandler.h"
#include "../../gui/CGuiHandler.h"
#include "../../gui/WindowHandler.h"
#include "../../widgets/Buttons.h"
#include "../../widgets/Images.h"
#include "../../widgets/Slider.h"
#include "../../widgets/TextControls.h"
2023-01-29 18:36:33 +02:00
2023-06-16 11:59:20 +02:00
#include "../../../lib/CGeneralTextHandler.h"
#include "../../../lib/filesystem/ResourceID.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 entry = settings.write[group][field];
entry->Bool() = value;
2023-01-29 18:36:33 +02:00
}
static std::string scalingToEntryString( int scaling)
{
return std::to_string(scaling) + '%';
}
static std::string scalingToLabelString( int scaling)
{
std::string string = CGI->generaltexth->translate("vcmi.systemOptions.scalingButton.hover");
boost::replace_all(string, "%p", std::to_string(scaling));
return string;
}
2023-06-16 11:59:20 +02:00
static std::string longTouchToEntryString( int duration)
{
std::string string = CGI->generaltexth->translate("vcmi.systemOptions.longTouchMenu.entry");
boost::replace_all(string, "%d", std::to_string(duration));
return string;
}
static std::string longTouchToLabelString( int duration)
{
std::string string = CGI->generaltexth->translate("vcmi.systemOptions.longTouchButton.hover");
boost::replace_all(string, "%d", std::to_string(duration));
return string;
}
2023-03-07 15:27:54 +02:00
static std::string resolutionToEntryString( int w, int h)
2023-01-29 18:36:33 +02:00
{
2023-03-07 15:27:54 +02:00
std::string string = "%wx%h";
boost::replace_all(string, "%w", std::to_string(w));
boost::replace_all(string, "%h", std::to_string(h));
return string;
}
static std::string resolutionToLabelString( int w, int h)
{
std::string string = CGI->generaltexth->translate("vcmi.systemOptions.resolutionButton.hover");
boost::replace_all(string, "%w", std::to_string(w));
boost::replace_all(string, "%h", std::to_string(h));
return string;
2023-01-29 18:36:33 +02:00
}
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;
setRedrawParent(true);
2023-01-29 18:36:33 +02:00
2023-06-16 11:59:20 +02:00
addConditional("touchscreen", GH.input().hasTouchInputDevice());
#ifdef VCMI_MOBILE
addConditional("mobile", true);
addConditional("desktop", false);
#else
addConditional("mobile", false);
addConditional("desktop", true);
#endif
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)
{
setBoolSetting("video", "spellbookAnimation", value);
2023-02-18 18:49:43 +02:00
});
addCallback("setMusic", [this](int value)
{
setIntSetting("general", "music", value);
widget<CSlider>("musicSlider")->redraw();
auto targetLabel = widget<CLabel>("musicValueLabel");
if (targetLabel)
targetLabel->setText(std::to_string(value) + "%");
2023-02-18 18:49:43 +02:00
});
addCallback("setVolume", [this](int value)
{
setIntSetting("general", "sound", value);
widget<CSlider>("soundVolumeSlider")->redraw();
auto targetLabel = widget<CLabel>("soundValueLabel");
if (targetLabel)
targetLabel->setText(std::to_string(value) + "%");
2023-02-18 18:49:43 +02:00
});
//settings that do not belong to base game:
addCallback("fullscreenBorderlessChanged", [this](bool value)
2023-02-18 18:49:43 +02:00
{
setFullscreenMode(value, false);
});
addCallback("fullscreenExclusiveChanged", [this](bool value)
{
setFullscreenMode(value, true);
2023-02-18 18:49:43 +02:00
});
addCallback("setGameResolution", [this](int dummyValue)
{
selectGameResolution();
});
addCallback("setGameScaling", [this](int dummyValue)
{
selectGameScaling();
});
2023-06-16 11:59:20 +02:00
addCallback("setLongTouchDuration", [this](int dummyValue)
{
selectLongTouchDuration();
});
2023-02-18 18:49:43 +02:00
addCallback("framerateChanged", [](bool value)
{
setBoolSetting("video", "showfps", value);
2023-02-18 18:49:43 +02:00
});
2023-07-08 18:47:38 +02:00
addCallback("hapticFeedbackChanged", [](bool value)
{
setBoolSetting("general", "hapticFeedback", 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
addCallback("availableCreaturesAsDwellingChanged", [=](int value)
2023-02-18 18:49:43 +02:00
{
setBoolSetting("gameTweaks", "availableCreaturesAsDwellingLabel", value > 0);
2023-02-18 18:49:43 +02:00
});
2023-02-18 18:49:43 +02:00
addCallback("compactTownCreatureInfoChanged", [](bool value)
{
setBoolSetting("gameTweaks", "compactTownCreatureInfo", value);
2023-02-18 18:49:43 +02:00
});
2023-01-29 18:36:33 +02:00
build(config);
const auto & currentResolution = settings["video"]["resolution"];
std::shared_ptr<CLabel> scalingLabel = widget<CLabel>("scalingLabel");
scalingLabel->setText(scalingToLabelString(currentResolution["scaling"].Integer()));
2023-06-16 11:59:20 +02:00
std::shared_ptr<CLabel> longTouchLabel = widget<CLabel>("longTouchLabel");
if (longTouchLabel)
longTouchLabel->setText(longTouchToLabelString(settings["general"]["longTouchTimeMilliseconds"].Integer()));
2023-06-16 11:59:20 +02:00
2023-01-29 18:36:33 +02:00
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> fullscreenBorderlessCheckbox = widget<CToggleButton>("fullscreenBorderlessCheckbox");
if (fullscreenBorderlessCheckbox)
fullscreenBorderlessCheckbox->setSelected(settings["video"]["fullscreen"].Bool() && !settings["video"]["realFullscreen"].Bool());
std::shared_ptr<CToggleButton> fullscreenExclusiveCheckbox = widget<CToggleButton>("fullscreenExclusiveCheckbox");
if (fullscreenExclusiveCheckbox)
fullscreenExclusiveCheckbox->setSelected(settings["video"]["fullscreen"].Bool() && settings["video"]["realFullscreen"].Bool());
2023-01-29 18:36:33 +02:00
std::shared_ptr<CToggleButton> framerateCheckbox = widget<CToggleButton>("framerateCheckbox");
framerateCheckbox->setSelected(settings["video"]["showfps"].Bool());
2023-01-29 18:36:33 +02:00
2023-07-08 18:47:38 +02:00
std::shared_ptr<CToggleButton> hapticFeedbackCheckbox = widget<CToggleButton>("hapticFeedbackCheckbox");
if (hapticFeedbackCheckbox)
hapticFeedbackCheckbox->setSelected(settings["general"]["hapticFeedback"].Bool());
2023-07-08 18:47:38 +02:00
2023-01-29 18:36:33 +02:00
std::shared_ptr<CSlider> musicSlider = widget<CSlider>("musicSlider");
musicSlider->scrollTo(CCS->musich->getVolume());
2023-01-29 18:36:33 +02:00
std::shared_ptr<CSlider> volumeSlider = widget<CSlider>("soundVolumeSlider");
volumeSlider->scrollTo(CCS->soundh->getVolume());
2023-02-18 17:35:07 +02:00
std::shared_ptr<CToggleGroup> creatureGrowthAsDwellingPicker = widget<CToggleGroup>("availableCreaturesAsDwellingPicker");
creatureGrowthAsDwellingPicker->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());
std::shared_ptr<CLabel> musicVolumeLabel = widget<CLabel>("musicValueLabel");
musicVolumeLabel->setText(std::to_string(CCS->musich->getVolume()) + "%");
std::shared_ptr<CLabel> soundVolumeLabel = widget<CLabel>("soundValueLabel");
soundVolumeLabel->setText(std::to_string(CCS->soundh->getVolume()) + "%");
updateResolutionSelector();
}
void GeneralOptionsTab::updateResolutionSelector()
{
std::shared_ptr<CButton> resolutionButton = widget<CButton>("resolutionButton");
std::shared_ptr<CLabel> resolutionLabel = widget<CLabel>("resolutionLabel");
if (resolutionButton)
{
if (settings["video"]["fullscreen"].Bool() && !settings["video"]["realFullscreen"].Bool())
resolutionButton->disable();
else
resolutionButton->enable();
}
if (resolutionLabel)
{
Point resolution = GH.screenHandler().getRenderResolution();
resolutionLabel->setText(resolutionToLabelString(resolution.x, resolution.y));
}
}
2023-01-29 18:36:33 +02:00
2023-02-12 18:49:41 +02:00
void GeneralOptionsTab::selectGameResolution()
{
supportedResolutions = GH.screenHandler().getSupportedResolutions();
std::vector<std::string> items;
2023-01-29 18:36:33 +02:00
size_t currentResolutionIndex = 0;
size_t i = 0;
for(const auto & it : supportedResolutions)
2023-01-29 18:36:33 +02:00
{
2023-03-07 15:27:54 +02:00
auto resolutionStr = resolutionToEntryString(it.x, it.y);
2023-03-07 15:35:04 +02:00
if(widget<CLabel>("resolutionLabel")->getText() == resolutionToLabelString(it.x, it.y))
2023-01-29 18:36:33 +02:00
currentResolutionIndex = i;
2023-01-29 18:36:33 +02:00
items.push_back(std::move(resolutionStr));
++i;
}
GH.windows().createAndPushWindow<CObjectListWindow>(items, nullptr,
2023-01-29 18:36:33 +02:00
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
{
assert(index >= 0 && index < supportedResolutions.size());
if ( index < 0 || index >= supportedResolutions.size() )
return;
2023-01-29 18:36:33 +02:00
Point resolution = supportedResolutions[index];
2023-01-29 18:36:33 +02:00
Settings gameRes = settings.write["video"]["resolution"];
gameRes["width"].Float() = resolution.x;
gameRes["height"].Float() = resolution.y;
2023-01-29 18:36:33 +02:00
2023-03-07 15:27:54 +02:00
widget<CLabel>("resolutionLabel")->setText(resolutionToLabelString(resolution.x, resolution.y));
GH.dispatchMainThread([](){
boost::unique_lock<boost::recursive_mutex> lock(*CPlayerInterface::pim);
GH.onScreenResize();
});
}
void GeneralOptionsTab::setFullscreenMode(bool on, bool exclusive)
{
if (on == settings["video"]["fullscreen"].Bool() && exclusive == settings["video"]["realFullscreen"].Bool())
return;
setBoolSetting("video", "realFullscreen", exclusive);
setBoolSetting("video", "fullscreen", on);
std::shared_ptr<CToggleButton> fullscreenExclusiveCheckbox = widget<CToggleButton>("fullscreenExclusiveCheckbox");
std::shared_ptr<CToggleButton> fullscreenBorderlessCheckbox = widget<CToggleButton>("fullscreenBorderlessCheckbox");
if (fullscreenBorderlessCheckbox)
fullscreenBorderlessCheckbox->setSelectedSilent(on && !exclusive);
if (fullscreenExclusiveCheckbox)
fullscreenExclusiveCheckbox->setSelectedSilent(on && exclusive);
updateResolutionSelector();
GH.dispatchMainThread([](){
boost::unique_lock<boost::recursive_mutex> lock(*CPlayerInterface::pim);
GH.onScreenResize();
});
}
void GeneralOptionsTab::selectGameScaling()
{
supportedScaling.clear();
// generate list of all possible scaling values, with 10% step
// also add one value over maximum, so if player can use scaling up to 123.456% he will be able to select 130%
// and let screen handler clamp that value to actual maximum
auto [minimalScaling, maximalScaling] = GH.screenHandler().getSupportedScalingRange();
for (int i = 0; i <= maximalScaling + 10 - 1; i += 10)
{
if (i >= minimalScaling)
supportedScaling.push_back(i);
}
std::vector<std::string> items;
size_t currentIndex = 0;
size_t i = 0;
for(const auto & it : supportedScaling)
{
auto resolutionStr = scalingToEntryString(it);
if(widget<CLabel>("scalingLabel")->getText() == scalingToLabelString(it))
currentIndex = i;
items.push_back(std::move(resolutionStr));
++i;
}
GH.windows().createAndPushWindow<CObjectListWindow>(
items,
nullptr,
CGI->generaltexth->translate("vcmi.systemOptions.scalingMenu.hover"),
CGI->generaltexth->translate("vcmi.systemOptions.scalingMenu.help"),
[this](int index)
{
setGameScaling(index);
},
currentIndex
);
}
void GeneralOptionsTab::setGameScaling(int index)
{
assert(index >= 0 && index < supportedScaling.size());
if ( index < 0 || index >= supportedScaling.size() )
return;
int scaling = supportedScaling[index];
Settings gameRes = settings.write["video"]["resolution"];
gameRes["scaling"].Float() = scaling;
widget<CLabel>("scalingLabel")->setText(scalingToLabelString(scaling));
2023-07-13 19:10:10 +02:00
GH.dispatchMainThread([](){
boost::unique_lock<boost::recursive_mutex> lock(*CPlayerInterface::pim);
GH.onScreenResize();
});
}
2023-06-16 11:59:20 +02:00
void GeneralOptionsTab::selectLongTouchDuration()
{
longTouchDurations = { 500, 750, 1000, 1250, 1500, 1750, 2000 };
std::vector<std::string> items;
size_t currentIndex = 0;
size_t i = 0;
for(const auto & it : longTouchDurations)
{
auto resolutionStr = longTouchToEntryString(it);
if(widget<CLabel>("longTouchLabel")->getText() == longTouchToLabelString(it))
currentIndex = i;
items.push_back(std::move(resolutionStr));
++i;
}
GH.windows().createAndPushWindow<CObjectListWindow>(
items,
nullptr,
CGI->generaltexth->translate("vcmi.systemOptions.longTouchMenu.hover"),
CGI->generaltexth->translate("vcmi.systemOptions.longTouchMenu.help"),
[this](int index)
{
setLongTouchDuration(index);
},
currentIndex
);
}
void GeneralOptionsTab::setLongTouchDuration(int index)
{
assert(index >= 0 && index < longTouchDurations.size());
if ( index < 0 || index >= longTouchDurations.size() )
return;
int scaling = longTouchDurations[index];
Settings longTouchTime = settings.write["general"]["longTouchTimeMilliseconds"];
longTouchTime->Float() = scaling;
widget<CLabel>("longTouchLabel")->setText(longTouchToLabelString(scaling));
}