OptionsTabBase.cpp 14 KB

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