OptionsTabBase.cpp 8.6 KB

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