OptionsTabBase.cpp 14 KB

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