OptionsTabBase.cpp 13 KB

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