diff --git a/client/CServerHandler.cpp b/client/CServerHandler.cpp index 8a4051fed..ddf40406d 100644 --- a/client/CServerHandler.cpp +++ b/client/CServerHandler.cpp @@ -39,6 +39,7 @@ #include "../lib/CThreadHelper.h" #include "../lib/NetPackVisitor.h" #include "../lib/StartInfo.h" +#include "../lib/TurnTimerInfo.h" #include "../lib/VCMIDirs.h" #include "../lib/campaign/CampaignState.h" #include "../lib/mapping/CMapInfo.h" @@ -475,11 +476,10 @@ void CServerHandler::setDifficulty(int to) const sendLobbyPack(lsd); } -void CServerHandler::setTurnLength(int npos) const +void CServerHandler::setTurnTimerInfo(const TurnTimerInfo & info) const { - vstd::amin(npos, GameConstants::POSSIBLE_TURNTIME.size() - 1); LobbySetTurnTime lstt; - lstt.turnTimerInfo.turnTimer = GameConstants::POSSIBLE_TURNTIME[npos] * 60 * 1000; + lstt.turnTimerInfo = info; sendLobbyPack(lstt); } diff --git a/client/CServerHandler.h b/client/CServerHandler.h index 777cbc29e..edecd3a64 100644 --- a/client/CServerHandler.h +++ b/client/CServerHandler.h @@ -19,6 +19,7 @@ VCMI_LIB_NAMESPACE_BEGIN class CConnection; class PlayerColor; struct StartInfo; +struct TurnTimerInfo; class CMapInfo; class CGameState; @@ -64,7 +65,7 @@ public: virtual void setPlayer(PlayerColor color) const = 0; virtual void setPlayerOption(ui8 what, int32_t value, PlayerColor player) const = 0; virtual void setDifficulty(int to) const = 0; - virtual void setTurnLength(int npos) const = 0; + virtual void setTurnTimerInfo(const TurnTimerInfo &) const = 0; virtual void sendMessage(const std::string & txt) const = 0; virtual void sendGuiAction(ui8 action) const = 0; // TODO: possibly get rid of it? virtual void sendStartGame(bool allowOnlyAI = false) const = 0; @@ -146,7 +147,7 @@ public: void setPlayer(PlayerColor color) const override; void setPlayerOption(ui8 what, int32_t value, PlayerColor player) const override; void setDifficulty(int to) const override; - void setTurnLength(int npos) const override; + void setTurnTimerInfo(const TurnTimerInfo &) const override; void sendMessage(const std::string & txt) const override; void sendGuiAction(ui8 action) const override; void sendRestartGame() const override; diff --git a/client/gui/InterfaceObjectConfigurable.cpp b/client/gui/InterfaceObjectConfigurable.cpp index 2fff4284c..60bdacbda 100644 --- a/client/gui/InterfaceObjectConfigurable.cpp +++ b/client/gui/InterfaceObjectConfigurable.cpp @@ -54,6 +54,7 @@ InterfaceObjectConfigurable::InterfaceObjectConfigurable(int used, Point offset) REGISTER_BUILDER("slider", &InterfaceObjectConfigurable::buildSlider); REGISTER_BUILDER("layout", &InterfaceObjectConfigurable::buildLayout); REGISTER_BUILDER("comboBox", &InterfaceObjectConfigurable::buildComboBox); + REGISTER_BUILDER("textInput", &InterfaceObjectConfigurable::buildTextInput); } void InterfaceObjectConfigurable::registerBuilder(const std::string & type, BuilderFunction f) @@ -63,9 +64,15 @@ void InterfaceObjectConfigurable::registerBuilder(const std::string & type, Buil void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function callback) { - callbacks[callbackName] = callback; + callbacks_int[callbackName] = callback; } +void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function callback) +{ + callbacks_string[callbackName] = callback; +} + + void InterfaceObjectConfigurable::deleteWidget(const std::string & name) { auto iter = widgets.find(name); @@ -340,7 +347,7 @@ std::shared_ptr InterfaceObjectConfigurable::buildToggleGroup(cons if(!config["selected"].isNull()) group->setSelected(config["selected"].Integer()); if(!config["callback"].isNull()) - group->addCallback(callbacks.at(config["callback"].String())); + group->addCallback(callbacks_int.at(config["callback"].String())); return group; } @@ -413,8 +420,8 @@ void InterfaceObjectConfigurable::loadToggleButtonCallback(std::shared_ptr 0) - button->addCallback(callbacks.at(callbackName)); + if (callbacks_int.count(callbackName) > 0) + button->addCallback(callbacks_int.at(callbackName)); else logGlobal->error("Invalid callback '%s' in widget", callbackName ); } @@ -426,8 +433,8 @@ void InterfaceObjectConfigurable::loadButtonCallback(std::shared_ptr bu std::string callbackName = config.String(); - if (callbacks.count(callbackName) > 0) - button->addCallback(std::bind(callbacks.at(callbackName), 0)); + if (callbacks_int.count(callbackName) > 0) + button->addCallback(std::bind(callbacks_int.at(callbackName), 0)); else logGlobal->error("Invalid callback '%s' in widget", callbackName ); } @@ -483,7 +490,7 @@ std::shared_ptr InterfaceObjectConfigurable::buildSlider(const JsonNode auto value = config["selected"].Integer(); bool horizontal = config["orientation"].String() == "horizontal"; const auto & result = - std::make_shared(position, length, callbacks.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal ? Orientation::HORIZONTAL : Orientation::VERTICAL, style); + std::make_shared(position, length, callbacks_int.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal ? Orientation::HORIZONTAL : Orientation::VERTICAL, style); if(!config["scrollBounds"].isNull()) { @@ -541,6 +548,26 @@ std::shared_ptr InterfaceObjectConfigurable::buildComboBox(const JsonN return result; } +std::shared_ptr InterfaceObjectConfigurable::buildTextInput(const JsonNode & config) const +{ + logGlobal->debug("Building widget CTextInput"); + auto rect = readRect(config["rect"]); + auto offset = readPosition(config["backgroundOffset"]); + auto bgName = config["background"].String(); + auto result = std::make_shared(rect, offset, bgName, 0); + if(!config["alignment"].isNull()) + result->alignment = readTextAlignment(config["alignment"]); + if(!config["font"].isNull()) + result->font = readFont(config["font"]); + if(!config["color"].isNull()) + result->setColor(readColor(config["color"])); + if(!config["text"].isNull()) + result->setText(readText(config["text"])); + if(!config["callback"].isNull()) + result->cb += callbacks_string.at(config["callback"].String()); + return result; +} + /// Small helper class that provides ownership for shared_ptr's of child elements class InterfaceLayoutWidget : public CIntObject { @@ -625,7 +652,7 @@ std::shared_ptr InterfaceObjectConfigurable::buildAnimation(const if(!config["alpha"].isNull()) anim->setAlpha(config["alpha"].Integer()); if(!config["callback"].isNull()) - anim->callback = std::bind(callbacks.at(config["callback"].String()), 0); + anim->callback = std::bind(callbacks_int.at(config["callback"].String()), 0); if(!config["frames"].isNull()) { auto b = config["frames"]["start"].Integer(); diff --git a/client/gui/InterfaceObjectConfigurable.h b/client/gui/InterfaceObjectConfigurable.h index 784220198..b2558655c 100644 --- a/client/gui/InterfaceObjectConfigurable.h +++ b/client/gui/InterfaceObjectConfigurable.h @@ -28,6 +28,7 @@ class CAnimImage; class CShowableAnim; class CFilledTexture; class ComboBox; +class CTextInput; #define REGISTER_BUILDER(type, method) registerBuilder(type, std::bind(method, this, std::placeholders::_1)) @@ -59,6 +60,7 @@ protected: void addWidget(const std::string & name, std::shared_ptr widget); void addCallback(const std::string & callbackName, std::function callback); + void addCallback(const std::string & callbackName, std::function callback); JsonNode variables; template @@ -101,6 +103,7 @@ protected: std::shared_ptr buildTexture(const JsonNode &) const; std::shared_ptr buildLayout(const JsonNode &); std::shared_ptr buildComboBox(const JsonNode &); + std::shared_ptr buildTextInput(const JsonNode &) const; //composite widgets std::shared_ptr buildWidget(JsonNode config) const; @@ -116,7 +119,8 @@ private: int unnamedObjectId = 0; std::map builders; std::map> widgets; - std::map> callbacks; + std::map> callbacks_int; + std::map> callbacks_string; std::map conditionals; std::map shortcuts; }; diff --git a/client/lobby/OptionsTab.cpp b/client/lobby/OptionsTab.cpp index bbbd25ca5..922c37df2 100644 --- a/client/lobby/OptionsTab.cpp +++ b/client/lobby/OptionsTab.cpp @@ -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" @@ -44,19 +45,118 @@ OptionsTab::OptionsTab() : humanPlayers(0) { recActions = 0; - addCallback("setTurnLength", std::bind(&IServerAPI::setTurnLength, CSH, _1)); + //addCallback("timerFieldChangedBase", <#std::function callback#>) + + 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); + } + }); + + auto parseTimerString = [](const std::string & str){ + std::stringstream sstrm; + int a, b; + sstrm << str; + sstrm >> a; + char c = sstrm.get(); + if(c == ':') + { + sstrm >> b; + return a * 60 + b; + } + return -1; + }; + + addCallback("parseAndSetTimer_base", [parseTimerString](const std::string & str){ + int time = parseTimerString(str) * 1000; + if(time >= 0) + { + TurnTimerInfo tinfo; + tinfo.baseTimer = time; + CSH->setTurnTimerInfo(tinfo); + } + }); + addCallback("parseAndSetTimer_turn", [parseTimerString](const std::string & str){ + int time = parseTimerString(str) * 1000; + if(time >= 0) + { + TurnTimerInfo tinfo; + tinfo.turnTimer = time; + CSH->setTurnTimerInfo(tinfo); + } + }); + addCallback("parseAndSetTimer_battle", [parseTimerString](const std::string & str){ + int time = parseTimerString(str) * 1000; + if(time >= 0) + { + TurnTimerInfo tinfo; + tinfo.battleTimer = time; + CSH->setTurnTimerInfo(tinfo); + } + }); + addCallback("parseAndSetTimer_creature", [parseTimerString](const std::string & str){ + int time = parseTimerString(str) * 1000; + if(time >= 0) + { + TurnTimerInfo tinfo; + 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("timerModeSwitch")) { - if(auto w = widget("sliderTurnDuration")) - w->deactivate(); - if(auto w = widget("labelPlayerTurnDuration")) - w->deactivate(); - if(auto w = widget("labelTurnDurationValue")) - w->deactivate(); + w->onConstructItems = [&](std::vector & 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(item)) + { + for(auto wname : (*tObj)["hideWidgets"].Vector()) + if(auto w = widget(wname.String())) + { + w->setEnabled(false); + } + for(auto wname : (*tObj)["showWidgets"].Vector()) + if(auto w = widget(wname.String())) + { + w->setEnabled(true); + } + } + redraw(); + } + }; + + w->getItemText = [this](int idx, const void * item){ + if(item) + { + if(auto * tObj = reinterpret_cast(item)) + return readText((*tObj)["text"]); + } + return std::string(""); + }; + + w->setItem(0); } } @@ -76,9 +176,19 @@ void OptionsTab::recreate() if(auto turnSlider = widget("sliderTurnDuration")) { - turnSlider->scrollTo(vstd::find_pos(GameConstants::POSSIBLE_TURNTIME, SEL->getStartInfo()->turnTimerInfo.turnTimer / (60 * 1000))); - if(auto w = widget("labelTurnDurationValue")) - w->setText(CGI->generaltexth->turnDurations[turnSlider->getValue()]); + if(!variables["timerPresets"].isNull()) + { + for(int idx = 0; idx < variables["timerPresets"].Vector().size(); ++idx) + { + auto & tpreset = variables["timerPresets"].Vector()[idx]; + if(tpreset.Vector().at(1).Integer() == SEL->getStartInfo()->turnTimerInfo.turnTimer / 1000) + { + turnSlider->scrollTo(idx); + if(auto w = widget("labelTurnDurationValue")) + w->setText(CGI->generaltexth->turnDurations[idx]); + } + } + } } } diff --git a/client/widgets/ComboBox.cpp b/client/widgets/ComboBox.cpp index 349307b5d..29ac42d0c 100644 --- a/client/widgets/ComboBox.cpp +++ b/client/widgets/ComboBox.cpp @@ -172,3 +172,10 @@ void ComboBox::setItem(const void * item) if(onSetItem) onSetItem(item); } + +void ComboBox::setItem(int id) +{ + std::vector tempItems; + onConstructItems(tempItems); + setItem(tempItems.at(id)); +} diff --git a/client/widgets/ComboBox.h b/client/widgets/ComboBox.h index 960e717b2..ced39987d 100644 --- a/client/widgets/ComboBox.h +++ b/client/widgets/ComboBox.h @@ -64,4 +64,6 @@ public: //return text value from item data std::function getItemText; + + void setItem(int id); }; diff --git a/config/widgets/optionsTab.json b/config/widgets/optionsTab.json index 8e9072d78..ca3c936f2 100644 --- a/config/widgets/optionsTab.json +++ b/config/widgets/optionsTab.json @@ -75,7 +75,6 @@ // timer { - "name": "labelPlayerTurnDuration", "type": "label", "font": "small", "alignment": "center", @@ -100,7 +99,7 @@ "orientation": "horizontal", "position": {"x": 55, "y": 557}, "size": 194, - "callback": "setTurnLength", + "callback": "setTimerPreset", "itemsVisible": 1, "itemsTotal": 11, "selected": 11, @@ -108,5 +107,23 @@ "scrollBounds": {"x": -3, "y": -25, "w": 337, "h": 43}, "panningStep": 20 }, - ] + ], + + "variables": + { + "timerPresets" : + [ + [0, 60, 0, 0], + [0, 120, 0, 0], + [0, 240, 0, 0], + [0, 360, 0, 0], + [0, 480, 0, 0], + [0, 600, 0, 0], + [0, 900, 0, 0], + [0, 1200, 0, 0], + [0, 1500, 0, 0], + [0, 1800, 0, 0], + [0, 0, 0, 0], + ] + } }