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