OptionsTabBase.cpp 12 KB

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