OptionsTabBase.cpp 13 KB

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