OptionsTabBase.cpp 13 KB

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