OptionsTabBase.cpp 12 KB

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