OptionsTabBase.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * OptionsTabBase.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "OptionsTabBase.h"
  12. #include "CSelectionBase.h"
  13. #include "TurnOptionsTab.h"
  14. #include "CLobbyScreen.h"
  15. #include "../widgets/ComboBox.h"
  16. #include "../widgets/CTextInput.h"
  17. #include "../widgets/Images.h"
  18. #include "../widgets/Slider.h"
  19. #include "../widgets/TextControls.h"
  20. #include "../CServerHandler.h"
  21. #include "../GameInstance.h"
  22. #include "../../lib/StartInfo.h"
  23. #include "../../lib/texts/CGeneralTextHandler.h"
  24. #include "../../lib/texts/Languages.h"
  25. #include "../../lib/texts/MetaString.h"
  26. #include "../../lib/CConfigHandler.h"
  27. #include "../../lib/GameLibrary.h"
  28. static std::string timeToString(int time)
  29. {
  30. std::stringstream ss;
  31. ss << time / 1000 / 60 << ":" << std::setw(2) << std::setfill('0') << time / 1000 % 60;
  32. return ss.str();
  33. };
  34. std::vector<TurnTimerInfo> OptionsTabBase::getTimerPresets() const
  35. {
  36. std::vector<TurnTimerInfo> result;
  37. for (auto const & tpreset : variables["timerPresets"].Vector())
  38. {
  39. TurnTimerInfo tinfo;
  40. tinfo.baseTimer = tpreset[0].Integer() * 1000;
  41. tinfo.turnTimer = tpreset[1].Integer() * 1000;
  42. tinfo.battleTimer = tpreset[2].Integer() * 1000;
  43. tinfo.unitTimer = tpreset[3].Integer() * 1000;
  44. tinfo.accumulatingTurnTimer = tpreset[4].Bool();
  45. tinfo.accumulatingUnitTimer = tpreset[5].Bool();
  46. result.push_back(tinfo);
  47. }
  48. return result;
  49. }
  50. std::vector<SimturnsInfo> OptionsTabBase::getSimturnsPresets() const
  51. {
  52. std::vector<SimturnsInfo> result;
  53. for (auto const & tpreset : variables["simturnsPresets"].Vector())
  54. {
  55. SimturnsInfo tinfo;
  56. tinfo.optionalTurns = tpreset[0].Integer();
  57. tinfo.requiredTurns = tpreset[1].Integer();
  58. tinfo.allowHumanWithAI = tpreset[2].Bool();
  59. result.push_back(tinfo);
  60. }
  61. return result;
  62. }
  63. OptionsTabBase::OptionsTabBase(const JsonPath & configPath)
  64. {
  65. recActions = 0;
  66. auto setTimerPresetCallback = [this](int index){
  67. GAME->server().setTurnTimerInfo(getTimerPresets().at(index));
  68. };
  69. auto setSimturnsPresetCallback = [this](int index){
  70. GAME->server().setSimturnsInfo(getSimturnsPresets().at(index));
  71. };
  72. addCallback("tabTurnOptions", [&](int)
  73. {
  74. auto lobby = (static_cast<CLobbyScreen *>(parent));
  75. lobby->toggleTab(lobby->tabTurnOptions);
  76. });
  77. addCallback("setTimerPreset", setTimerPresetCallback);
  78. addCallback("setSimturnPreset", setSimturnsPresetCallback);
  79. addCallback("setSimturnDurationMin", [&](int index){
  80. SimturnsInfo info = SEL->getStartInfo()->simturnsInfo;
  81. info.requiredTurns = index;
  82. info.optionalTurns = std::max(info.optionalTurns, index);
  83. GAME->server().setSimturnsInfo(info);
  84. });
  85. addCallback("setSimturnDurationMax", [&](int index){
  86. SimturnsInfo info = SEL->getStartInfo()->simturnsInfo;
  87. info.optionalTurns = index;
  88. info.requiredTurns = std::min(info.requiredTurns, index);
  89. GAME->server().setSimturnsInfo(info);
  90. });
  91. addCallback("setSimturnAI", [&](int index){
  92. SimturnsInfo info = SEL->getStartInfo()->simturnsInfo;
  93. info.allowHumanWithAI = index;
  94. GAME->server().setSimturnsInfo(info);
  95. });
  96. addCallback("setCheatAllowed", [&](int index){
  97. bool isMultiplayer = GAME->server().loadMode == ELoadMode::MULTI;
  98. Settings entry = persistentStorage.write["startExtraOptions"][isMultiplayer ? "multiPlayer" : "singlePlayer"][isMultiplayer ? "cheatsAllowed" : "cheatsNotAllowed"];
  99. entry->Bool() = isMultiplayer ? index : !index;
  100. ExtraOptionsInfo info = SEL->getStartInfo()->extraOptionsInfo;
  101. info.cheatsAllowed = index;
  102. GAME->server().setExtraOptionsInfo(info);
  103. });
  104. addCallback("setUnlimitedReplay", [&](int index){
  105. bool isMultiplayer = GAME->server().loadMode == ELoadMode::MULTI;
  106. Settings entry = persistentStorage.write["startExtraOptions"][isMultiplayer ? "multiPlayer" : "singlePlayer"]["unlimitedReplay"];
  107. entry->Bool() = index;
  108. ExtraOptionsInfo info = SEL->getStartInfo()->extraOptionsInfo;
  109. info.unlimitedReplay = index;
  110. GAME->server().setExtraOptionsInfo(info);
  111. });
  112. addCallback("setTurnTimerAccumulate", [&](int index){
  113. TurnTimerInfo info = SEL->getStartInfo()->turnTimerInfo;
  114. info.accumulatingTurnTimer = index;
  115. GAME->server().setTurnTimerInfo(info);
  116. });
  117. addCallback("setUnitTimerAccumulate", [&](int index){
  118. TurnTimerInfo info = SEL->getStartInfo()->turnTimerInfo;
  119. info.accumulatingUnitTimer = index;
  120. GAME->server().setTurnTimerInfo(info);
  121. });
  122. //helper function to parse string containing time to integer reflecting time in seconds
  123. //assumed that input string can be modified by user, function shall support user's intention
  124. // normal: 2:00, 12:30
  125. // adding symbol: 2:005 -> 2:05, 2:305 -> 23:05,
  126. // adding symbol (>60 seconds): 12:095 -> 129:05
  127. // removing symbol: 129:0 -> 12:09, 2:0 -> 0:20, 0:2 -> 0:02
  128. auto parseTimerString = [](const std::string & str) -> int
  129. {
  130. auto sc = str.find(":");
  131. if(sc == std::string::npos)
  132. return str.empty() ? 0 : std::stoi(str);
  133. auto l = str.substr(0, sc);
  134. auto r = str.substr(sc + 1, std::string::npos);
  135. if(r.length() == 3) //symbol added
  136. {
  137. l.push_back(r.front());
  138. r.erase(r.begin());
  139. }
  140. else if(r.length() == 1) //symbol removed
  141. {
  142. r.insert(r.begin(), l.back());
  143. l.pop_back();
  144. }
  145. else if(r.empty())
  146. r = "0";
  147. int sec = std::stoi(r);
  148. if(sec >= 60)
  149. {
  150. if(l.empty()) //9:00 -> 0:09
  151. return sec / 10;
  152. l.push_back(r.front()); //0:090 -> 9:00
  153. r.erase(r.begin());
  154. }
  155. else if(l.empty())
  156. return sec;
  157. return std::min(24*60, std::stoi(l)) * 60 + std::stoi(r);
  158. };
  159. addCallback("parseAndSetTimer_base", [this, parseTimerString](const std::string & str){
  160. int time = parseTimerString(str) * 1000;
  161. if(time >= 0)
  162. {
  163. TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
  164. tinfo.baseTimer = time;
  165. GAME->server().setTurnTimerInfo(tinfo);
  166. if(auto ww = widget<CTextInput>("chessFieldBase"))
  167. ww->setText(timeToString(time));
  168. }
  169. });
  170. addCallback("parseAndSetTimer_turn", [this, parseTimerString](const std::string & str){
  171. int time = parseTimerString(str) * 1000;
  172. if(time >= 0)
  173. {
  174. TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
  175. tinfo.turnTimer = time;
  176. GAME->server().setTurnTimerInfo(tinfo);
  177. if(auto ww = widget<CTextInput>("chessFieldTurn"))
  178. ww->setText(timeToString(time));
  179. }
  180. });
  181. addCallback("parseAndSetTimer_battle", [this, parseTimerString](const std::string & str){
  182. int time = parseTimerString(str) * 1000;
  183. if(time >= 0)
  184. {
  185. TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
  186. tinfo.battleTimer = time;
  187. GAME->server().setTurnTimerInfo(tinfo);
  188. if(auto ww = widget<CTextInput>("chessFieldBattle"))
  189. ww->setText(timeToString(time));
  190. }
  191. });
  192. addCallback("parseAndSetTimer_unit", [this, parseTimerString](const std::string & str){
  193. int time = parseTimerString(str) * 1000;
  194. if(time >= 0)
  195. {
  196. TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
  197. tinfo.unitTimer = time;
  198. GAME->server().setTurnTimerInfo(tinfo);
  199. if(auto ww = widget<CTextInput>("chessFieldUnit"))
  200. ww->setText(timeToString(time));
  201. }
  202. });
  203. const JsonNode config(configPath);
  204. build(config);
  205. //set timers combo box callbacks
  206. if(auto w = widget<ComboBox>("timerModeSwitch"))
  207. {
  208. w->onConstructItems = [&](std::vector<const void *> & curItems){
  209. if(variables["timers"].isNull())
  210. return;
  211. for(auto & p : variables["timers"].Vector())
  212. {
  213. curItems.push_back(&p);
  214. }
  215. };
  216. w->onSetItem = [&](const void * item){
  217. if(item)
  218. {
  219. if(auto * tObj = reinterpret_cast<const JsonNode *>(item))
  220. {
  221. for(auto wname : (*tObj)["hideWidgets"].Vector())
  222. {
  223. if(auto w = widget<CIntObject>(wname.String()))
  224. w->setEnabled(false);
  225. }
  226. for(auto wname : (*tObj)["showWidgets"].Vector())
  227. {
  228. if(auto w = widget<CIntObject>(wname.String()))
  229. w->setEnabled(true);
  230. }
  231. if((*tObj)["default"].isVector())
  232. {
  233. TurnTimerInfo tinfo;
  234. tinfo.baseTimer = (*tObj)["default"].Vector().at(0).Integer() * 1000;
  235. tinfo.turnTimer = (*tObj)["default"].Vector().at(1).Integer() * 1000;
  236. tinfo.battleTimer = (*tObj)["default"].Vector().at(2).Integer() * 1000;
  237. tinfo.unitTimer = (*tObj)["default"].Vector().at(3).Integer() * 1000;
  238. GAME->server().setTurnTimerInfo(tinfo);
  239. }
  240. }
  241. redraw();
  242. }
  243. };
  244. w->getItemText = [this](int idx, const void * item){
  245. if(item)
  246. {
  247. if(auto * tObj = reinterpret_cast<const JsonNode *>(item))
  248. return readText((*tObj)["text"]);
  249. }
  250. return std::string("");
  251. };
  252. w->setItem(0);
  253. }
  254. if(auto w = widget<ComboBox>("simturnsPresetSelector"))
  255. {
  256. w->onConstructItems = [this](std::vector<const void *> & curItems)
  257. {
  258. for (size_t i = 0; i < variables["simturnsPresets"].Vector().size(); ++i)
  259. curItems.push_back((void*)i);
  260. };
  261. w->onSetItem = [setSimturnsPresetCallback](const void * item){
  262. size_t itemIndex = (size_t)item;
  263. setSimturnsPresetCallback(itemIndex);
  264. };
  265. }
  266. if(auto w = widget<ComboBox>("timerPresetSelector"))
  267. {
  268. w->onConstructItems = [this](std::vector<const void *> & curItems)
  269. {
  270. for (size_t i = 0; i < variables["timerPresets"].Vector().size(); ++i)
  271. curItems.push_back((void*)i);
  272. };
  273. w->onSetItem = [setTimerPresetCallback](const void * item){
  274. size_t itemIndex = (size_t)item;
  275. setTimerPresetCallback(itemIndex);
  276. };
  277. }
  278. }
  279. void OptionsTabBase::recreate(bool campaign)
  280. {
  281. auto const & generateSimturnsDurationText = [](int days) -> std::string
  282. {
  283. if (days == 0)
  284. return LIBRARY->generaltexth->translate("core.genrltxt.523");
  285. if (days >= 1000000) // Not "unlimited" but close enough
  286. return LIBRARY->generaltexth->translate("core.turndur.10");
  287. bool canUseMonth = days % 28 == 0 && days >= 28*2;
  288. bool canUseWeek = days % 7 == 0 && days >= 7*2;
  289. int value = days;
  290. std::string text = "vcmi.optionsTab.simturns.days";
  291. if (canUseWeek && !canUseMonth)
  292. {
  293. value = days / 7;
  294. text = "vcmi.optionsTab.simturns.weeks";
  295. }
  296. if (canUseMonth)
  297. {
  298. value = days / 28;
  299. text = "vcmi.optionsTab.simturns.months";
  300. }
  301. MetaString message;
  302. message.appendTextID(Languages::getPluralFormTextID( LIBRARY->generaltexth->getPreferredLanguage(), value, text));
  303. message.replaceNumber(value);
  304. return message.toString();
  305. };
  306. //Simultaneous turns
  307. if(auto turnSlider = widget<CSlider>("simturnsDurationMin"))
  308. turnSlider->setValue(SEL->getStartInfo()->simturnsInfo.requiredTurns, false);
  309. if(auto turnSlider = widget<CSlider>("simturnsDurationMax"))
  310. turnSlider->setValue(SEL->getStartInfo()->simturnsInfo.optionalTurns, false);
  311. if(auto w = widget<CLabel>("labelSimturnsDurationValueMin"))
  312. w->setText(generateSimturnsDurationText(SEL->getStartInfo()->simturnsInfo.requiredTurns));
  313. if(auto w = widget<CLabel>("labelSimturnsDurationValueMax"))
  314. w->setText(generateSimturnsDurationText(SEL->getStartInfo()->simturnsInfo.optionalTurns));
  315. if(auto buttonSimturnsAI = widget<CToggleButton>("buttonSimturnsAI"))
  316. buttonSimturnsAI->setSelectedSilent(SEL->getStartInfo()->simturnsInfo.allowHumanWithAI);
  317. if(auto buttonTurnTimerAccumulate = widget<CToggleButton>("buttonTurnTimerAccumulate"))
  318. buttonTurnTimerAccumulate->setSelectedSilent(SEL->getStartInfo()->turnTimerInfo.accumulatingTurnTimer);
  319. if(auto chessFieldTurnLabel = widget<CLabel>("chessFieldTurnLabel"))
  320. {
  321. if (SEL->getStartInfo()->turnTimerInfo.accumulatingTurnTimer)
  322. chessFieldTurnLabel->setText(LIBRARY->generaltexth->translate("vcmi.optionsTab.chessFieldTurnAccumulate.help"));
  323. else
  324. chessFieldTurnLabel->setText(LIBRARY->generaltexth->translate("vcmi.optionsTab.chessFieldTurnDiscard.help"));
  325. }
  326. if(auto chessFieldUnitLabel = widget<CLabel>("chessFieldUnitLabel"))
  327. {
  328. if (SEL->getStartInfo()->turnTimerInfo.accumulatingUnitTimer)
  329. chessFieldUnitLabel->setText(LIBRARY->generaltexth->translate("vcmi.optionsTab.chessFieldUnitAccumulate.help"));
  330. else
  331. chessFieldUnitLabel->setText(LIBRARY->generaltexth->translate("vcmi.optionsTab.chessFieldUnitDiscard.help"));
  332. }
  333. if(auto buttonUnitTimerAccumulate = widget<CToggleButton>("buttonUnitTimerAccumulate"))
  334. buttonUnitTimerAccumulate->setSelectedSilent(SEL->getStartInfo()->turnTimerInfo.accumulatingUnitTimer);
  335. const auto & turnTimerRemote = SEL->getStartInfo()->turnTimerInfo;
  336. //classic timer
  337. if(auto turnSlider = widget<CSlider>("sliderTurnDuration"))
  338. {
  339. if(!variables["timerPresets"].isNull() && !turnTimerRemote.battleTimer && !turnTimerRemote.unitTimer && !turnTimerRemote.baseTimer)
  340. {
  341. for(int idx = 0; idx < variables["timerPresets"].Vector().size(); ++idx)
  342. {
  343. auto & tpreset = variables["timerPresets"].Vector()[idx];
  344. if(tpreset.Vector().at(1).Integer() == turnTimerRemote.turnTimer / 1000)
  345. {
  346. turnSlider->scrollTo(idx, false);
  347. if(auto w = widget<CLabel>("labelTurnDurationValue"))
  348. w->setText(LIBRARY->generaltexth->turnDurations[idx]);
  349. }
  350. }
  351. }
  352. }
  353. if(auto ww = widget<CTextInput>("chessFieldBase"))
  354. ww->setText(timeToString(turnTimerRemote.baseTimer));
  355. if(auto ww = widget<CTextInput>("chessFieldTurn"))
  356. ww->setText(timeToString(turnTimerRemote.turnTimer));
  357. if(auto ww = widget<CTextInput>("chessFieldBattle"))
  358. ww->setText(timeToString(turnTimerRemote.battleTimer));
  359. if(auto ww = widget<CTextInput>("chessFieldUnit"))
  360. ww->setText(timeToString(turnTimerRemote.unitTimer));
  361. if(auto w = widget<ComboBox>("timerModeSwitch"))
  362. {
  363. if(turnTimerRemote.battleTimer || turnTimerRemote.unitTimer || turnTimerRemote.baseTimer)
  364. {
  365. if(auto turnSlider = widget<CSlider>("sliderTurnDuration"))
  366. if(turnSlider->isActive())
  367. w->setItem(1);
  368. }
  369. }
  370. if(auto buttonCheatAllowed = widget<CToggleButton>("buttonCheatAllowed"))
  371. {
  372. buttonCheatAllowed->setSelectedSilent(SEL->getStartInfo()->extraOptionsInfo.cheatsAllowed);
  373. buttonCheatAllowed->block(GAME->server().isGuest());
  374. }
  375. if(auto buttonUnlimitedReplay = widget<CToggleButton>("buttonUnlimitedReplay"))
  376. {
  377. buttonUnlimitedReplay->setSelectedSilent(SEL->getStartInfo()->extraOptionsInfo.unlimitedReplay);
  378. buttonUnlimitedReplay->block(GAME->server().isGuest());
  379. }
  380. if(auto buttonTurnOptions = widget<CButton>("buttonTurnOptions"))
  381. {
  382. buttonTurnOptions->block(GAME->server().isGuest() || campaign);
  383. }
  384. if(auto textureCampaignOverdraw = widget<CFilledTexture>("textureCampaignOverdraw"))
  385. textureCampaignOverdraw->setEnabled(campaign);
  386. }