OptionsTabBase.cpp 14 KB

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