OptionsTabBase.cpp 12 KB

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