OptionsTabBase.cpp 12 KB

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