OptionsTabBase.cpp 11 KB

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