OptionsTabBase.cpp 13 KB

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