|
@@ -21,6 +21,7 @@
|
|
|
#include "../render/Graphics.h"
|
|
|
#include "../render/IFont.h"
|
|
|
#include "../widgets/CComponent.h"
|
|
|
+#include "../widgets/ComboBox.h"
|
|
|
#include "../widgets/Buttons.h"
|
|
|
#include "../widgets/Images.h"
|
|
|
#include "../widgets/MiscWidgets.h"
|
|
@@ -43,20 +44,154 @@
|
|
|
OptionsTab::OptionsTab() : humanPlayers(0)
|
|
|
{
|
|
|
recActions = 0;
|
|
|
+
|
|
|
+ addCallback("setTimerPreset", [&](int index){
|
|
|
+ if(!variables["timerPresets"].isNull())
|
|
|
+ {
|
|
|
+ auto tpreset = variables["timerPresets"].Vector().at(index).Vector();
|
|
|
+ TurnTimerInfo tinfo;
|
|
|
+ tinfo.baseTimer = tpreset.at(0).Integer() * 1000;
|
|
|
+ tinfo.turnTimer = tpreset.at(1).Integer() * 1000;
|
|
|
+ tinfo.battleTimer = tpreset.at(2).Integer() * 1000;
|
|
|
+ tinfo.creatureTimer = tpreset.at(3).Integer() * 1000;
|
|
|
+ CSH->setTurnTimerInfo(tinfo);
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
- addCallback("setTurnLength", std::bind(&IServerAPI::setTurnLength, CSH, _1));
|
|
|
+ //helper function to parse string containing time to integer reflecting time in seconds
|
|
|
+ //assumed that input string can be modified by user, function shall support user's intention
|
|
|
+ // normal: 2:00, 12:30
|
|
|
+ // adding symbol: 2:005 -> 2:05, 2:305 -> 23:05,
|
|
|
+ // adding symbol (>60 seconds): 12:095 -> 129:05
|
|
|
+ // removing symbol: 129:0 -> 12:09, 2:0 -> 0:20, 0:2 -> 0:02
|
|
|
+ auto parseTimerString = [](const std::string & str) -> int
|
|
|
+ {
|
|
|
+ auto sc = str.find(":");
|
|
|
+ if(sc == std::string::npos)
|
|
|
+ return str.empty() ? 0 : std::stoi(str);
|
|
|
+
|
|
|
+ auto l = str.substr(0, sc);
|
|
|
+ auto r = str.substr(sc + 1, std::string::npos);
|
|
|
+ if(r.length() == 3) //symbol added
|
|
|
+ {
|
|
|
+ l.push_back(r.front());
|
|
|
+ r.erase(r.begin());
|
|
|
+ }
|
|
|
+ else if(r.length() == 1) //symbol removed
|
|
|
+ {
|
|
|
+ r.insert(r.begin(), l.back());
|
|
|
+ l.pop_back();
|
|
|
+ }
|
|
|
+ else if(r.empty())
|
|
|
+ r = "0";
|
|
|
+
|
|
|
+ int sec = std::stoi(r);
|
|
|
+ if(sec >= 60)
|
|
|
+ {
|
|
|
+ if(l.empty()) //9:00 -> 0:09
|
|
|
+ return sec / 10;
|
|
|
+
|
|
|
+ l.push_back(r.front()); //0:090 -> 9:00
|
|
|
+ r.erase(r.begin());
|
|
|
+ }
|
|
|
+ else if(l.empty())
|
|
|
+ return sec;
|
|
|
+
|
|
|
+ return std::stoi(l) * 60 + std::stoi(r);
|
|
|
+ };
|
|
|
+
|
|
|
+ addCallback("parseAndSetTimer_base", [parseTimerString](const std::string & str){
|
|
|
+ int time = parseTimerString(str) * 1000;
|
|
|
+ if(time >= 0)
|
|
|
+ {
|
|
|
+ TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
|
|
|
+ tinfo.baseTimer = time;
|
|
|
+ CSH->setTurnTimerInfo(tinfo);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ addCallback("parseAndSetTimer_turn", [parseTimerString](const std::string & str){
|
|
|
+ int time = parseTimerString(str) * 1000;
|
|
|
+ if(time >= 0)
|
|
|
+ {
|
|
|
+ TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
|
|
|
+ tinfo.turnTimer = time;
|
|
|
+ CSH->setTurnTimerInfo(tinfo);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ addCallback("parseAndSetTimer_battle", [parseTimerString](const std::string & str){
|
|
|
+ int time = parseTimerString(str) * 1000;
|
|
|
+ if(time >= 0)
|
|
|
+ {
|
|
|
+ TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
|
|
|
+ tinfo.battleTimer = time;
|
|
|
+ CSH->setTurnTimerInfo(tinfo);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ addCallback("parseAndSetTimer_creature", [parseTimerString](const std::string & str){
|
|
|
+ int time = parseTimerString(str) * 1000;
|
|
|
+ if(time >= 0)
|
|
|
+ {
|
|
|
+ TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
|
|
|
+ tinfo.creatureTimer = time;
|
|
|
+ CSH->setTurnTimerInfo(tinfo);
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
const JsonNode config(ResourceID("config/widgets/optionsTab.json"));
|
|
|
build(config);
|
|
|
|
|
|
- if(SEL->screenType == ESelectionScreen::newGame || SEL->screenType == ESelectionScreen::loadGame || SEL->screenType == ESelectionScreen::scenarioInfo)
|
|
|
+ //set timers combo box callbacks
|
|
|
+ if(auto w = widget<ComboBox>("timerModeSwitch"))
|
|
|
{
|
|
|
- if(auto w = widget<CSlider>("sliderTurnDuration"))
|
|
|
- w->deactivate();
|
|
|
- if(auto w = widget<CLabel>("labelPlayerTurnDuration"))
|
|
|
- w->deactivate();
|
|
|
- if(auto w = widget<CLabel>("labelTurnDurationValue"))
|
|
|
- w->deactivate();
|
|
|
+ w->onConstructItems = [&](std::vector<const void *> & curItems){
|
|
|
+ if(variables["timers"].isNull())
|
|
|
+ return;
|
|
|
+
|
|
|
+ for(auto & p : variables["timers"].Vector())
|
|
|
+ {
|
|
|
+ curItems.push_back(&p);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ w->onSetItem = [&](const void * item){
|
|
|
+ if(item)
|
|
|
+ {
|
|
|
+ if(auto * tObj = reinterpret_cast<const JsonNode *>(item))
|
|
|
+ {
|
|
|
+ for(auto wname : (*tObj)["hideWidgets"].Vector())
|
|
|
+ {
|
|
|
+ if(auto w = widget<CIntObject>(wname.String()))
|
|
|
+ w->setEnabled(false);
|
|
|
+ }
|
|
|
+ for(auto wname : (*tObj)["showWidgets"].Vector())
|
|
|
+ {
|
|
|
+ if(auto w = widget<CIntObject>(wname.String()))
|
|
|
+ w->setEnabled(true);
|
|
|
+ }
|
|
|
+ if((*tObj)["default"].isVector())
|
|
|
+ {
|
|
|
+ TurnTimerInfo tinfo;
|
|
|
+ tinfo.baseTimer = (*tObj)["default"].Vector().at(0).Integer() * 1000;
|
|
|
+ tinfo.turnTimer = (*tObj)["default"].Vector().at(1).Integer() * 1000;
|
|
|
+ tinfo.battleTimer = (*tObj)["default"].Vector().at(2).Integer() * 1000;
|
|
|
+ tinfo.creatureTimer = (*tObj)["default"].Vector().at(3).Integer() * 1000;
|
|
|
+ CSH->setTurnTimerInfo(tinfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ redraw();
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ w->getItemText = [this](int idx, const void * item){
|
|
|
+ if(item)
|
|
|
+ {
|
|
|
+ if(auto * tObj = reinterpret_cast<const JsonNode *>(item))
|
|
|
+ return readText((*tObj)["text"]);
|
|
|
+ }
|
|
|
+ return std::string("");
|
|
|
+ };
|
|
|
+
|
|
|
+ w->setItem(0);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -73,12 +208,52 @@ void OptionsTab::recreate()
|
|
|
|
|
|
entries.insert(std::make_pair(pInfo.first, std::make_shared<PlayerOptionsEntry>(pInfo.second, * this)));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ const auto & turnTimerRemote = SEL->getStartInfo()->turnTimerInfo;
|
|
|
+
|
|
|
+ //classic timer
|
|
|
if(auto turnSlider = widget<CSlider>("sliderTurnDuration"))
|
|
|
{
|
|
|
- turnSlider->scrollTo(vstd::find_pos(GameConstants::POSSIBLE_TURNTIME, SEL->getStartInfo()->turnTimerInfo.turnTimer / (60 * 1000)));
|
|
|
- if(auto w = widget<CLabel>("labelTurnDurationValue"))
|
|
|
- w->setText(CGI->generaltexth->turnDurations[turnSlider->getValue()]);
|
|
|
+ if(!variables["timerPresets"].isNull() && !turnTimerRemote.battleTimer && !turnTimerRemote.creatureTimer && !turnTimerRemote.baseTimer)
|
|
|
+ {
|
|
|
+ for(int idx = 0; idx < variables["timerPresets"].Vector().size(); ++idx)
|
|
|
+ {
|
|
|
+ auto & tpreset = variables["timerPresets"].Vector()[idx];
|
|
|
+ if(tpreset.Vector().at(1).Integer() == turnTimerRemote.turnTimer / 1000)
|
|
|
+ {
|
|
|
+ turnSlider->scrollTo(idx);
|
|
|
+ if(auto w = widget<CLabel>("labelTurnDurationValue"))
|
|
|
+ w->setText(CGI->generaltexth->turnDurations[idx]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //chess timer
|
|
|
+ auto timeToString = [](int time) -> std::string
|
|
|
+ {
|
|
|
+ std::stringstream ss;
|
|
|
+ ss << time / 1000 / 60 << ":" << std::setw(2) << std::setfill('0') << time / 1000 % 60;
|
|
|
+ return ss.str();
|
|
|
+ };
|
|
|
+
|
|
|
+ if(auto ww = widget<CTextInput>("chessFieldBase"))
|
|
|
+ ww->setText(timeToString(turnTimerRemote.baseTimer), false);
|
|
|
+ if(auto ww = widget<CTextInput>("chessFieldTurn"))
|
|
|
+ ww->setText(timeToString(turnTimerRemote.turnTimer), false);
|
|
|
+ if(auto ww = widget<CTextInput>("chessFieldBattle"))
|
|
|
+ ww->setText(timeToString(turnTimerRemote.battleTimer), false);
|
|
|
+ if(auto ww = widget<CTextInput>("chessFieldCreature"))
|
|
|
+ ww->setText(timeToString(turnTimerRemote.creatureTimer), false);
|
|
|
+
|
|
|
+ if(auto w = widget<ComboBox>("timerModeSwitch"))
|
|
|
+ {
|
|
|
+ if(turnTimerRemote.battleTimer || turnTimerRemote.creatureTimer || turnTimerRemote.baseTimer)
|
|
|
+ {
|
|
|
+ if(auto turnSlider = widget<CSlider>("sliderTurnDuration"))
|
|
|
+ if(turnSlider->isActive())
|
|
|
+ w->setItem(1);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|