OptionsTabBase.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/MetaString.h"
  20. #include "../../lib/CGeneralTextHandler.h"
  21. OptionsTabBase::OptionsTabBase(const JsonPath & configPath)
  22. {
  23. recActions = 0;
  24. addCallback("setTimerPreset", [&](int index){
  25. if(!variables["timerPresets"].isNull())
  26. {
  27. auto tpreset = variables["timerPresets"].Vector().at(index).Vector();
  28. TurnTimerInfo tinfo;
  29. tinfo.baseTimer = tpreset.at(0).Integer() * 1000;
  30. tinfo.turnTimer = tpreset.at(1).Integer() * 1000;
  31. tinfo.battleTimer = tpreset.at(2).Integer() * 1000;
  32. tinfo.creatureTimer = tpreset.at(3).Integer() * 1000;
  33. CSH->setTurnTimerInfo(tinfo);
  34. }
  35. });
  36. addCallback("setSimturnDuration", [&](int index){
  37. SimturnsInfo info;
  38. info.optionalTurns = index;
  39. CSH->setSimturnsInfo(info);
  40. });
  41. //helper function to parse string containing time to integer reflecting time in seconds
  42. //assumed that input string can be modified by user, function shall support user's intention
  43. // normal: 2:00, 12:30
  44. // adding symbol: 2:005 -> 2:05, 2:305 -> 23:05,
  45. // adding symbol (>60 seconds): 12:095 -> 129:05
  46. // removing symbol: 129:0 -> 12:09, 2:0 -> 0:20, 0:2 -> 0:02
  47. auto parseTimerString = [](const std::string & str) -> int
  48. {
  49. auto sc = str.find(":");
  50. if(sc == std::string::npos)
  51. return str.empty() ? 0 : std::stoi(str);
  52. auto l = str.substr(0, sc);
  53. auto r = str.substr(sc + 1, std::string::npos);
  54. if(r.length() == 3) //symbol added
  55. {
  56. l.push_back(r.front());
  57. r.erase(r.begin());
  58. }
  59. else if(r.length() == 1) //symbol removed
  60. {
  61. r.insert(r.begin(), l.back());
  62. l.pop_back();
  63. }
  64. else if(r.empty())
  65. r = "0";
  66. int sec = std::stoi(r);
  67. if(sec >= 60)
  68. {
  69. if(l.empty()) //9:00 -> 0:09
  70. return sec / 10;
  71. l.push_back(r.front()); //0:090 -> 9:00
  72. r.erase(r.begin());
  73. }
  74. else if(l.empty())
  75. return sec;
  76. return std::stoi(l) * 60 + std::stoi(r);
  77. };
  78. addCallback("parseAndSetTimer_base", [parseTimerString](const std::string & str){
  79. int time = parseTimerString(str) * 1000;
  80. if(time >= 0)
  81. {
  82. TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
  83. tinfo.baseTimer = time;
  84. CSH->setTurnTimerInfo(tinfo);
  85. }
  86. });
  87. addCallback("parseAndSetTimer_turn", [parseTimerString](const std::string & str){
  88. int time = parseTimerString(str) * 1000;
  89. if(time >= 0)
  90. {
  91. TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
  92. tinfo.turnTimer = time;
  93. CSH->setTurnTimerInfo(tinfo);
  94. }
  95. });
  96. addCallback("parseAndSetTimer_battle", [parseTimerString](const std::string & str){
  97. int time = parseTimerString(str) * 1000;
  98. if(time >= 0)
  99. {
  100. TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
  101. tinfo.battleTimer = time;
  102. CSH->setTurnTimerInfo(tinfo);
  103. }
  104. });
  105. addCallback("parseAndSetTimer_creature", [parseTimerString](const std::string & str){
  106. int time = parseTimerString(str) * 1000;
  107. if(time >= 0)
  108. {
  109. TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
  110. tinfo.creatureTimer = time;
  111. CSH->setTurnTimerInfo(tinfo);
  112. }
  113. });
  114. const JsonNode config(configPath);
  115. build(config);
  116. //set timers combo box callbacks
  117. if(auto w = widget<ComboBox>("timerModeSwitch"))
  118. {
  119. w->onConstructItems = [&](std::vector<const void *> & curItems){
  120. if(variables["timers"].isNull())
  121. return;
  122. for(auto & p : variables["timers"].Vector())
  123. {
  124. curItems.push_back(&p);
  125. }
  126. };
  127. w->onSetItem = [&](const void * item){
  128. if(item)
  129. {
  130. if(auto * tObj = reinterpret_cast<const JsonNode *>(item))
  131. {
  132. for(auto wname : (*tObj)["hideWidgets"].Vector())
  133. {
  134. if(auto w = widget<CIntObject>(wname.String()))
  135. w->setEnabled(false);
  136. }
  137. for(auto wname : (*tObj)["showWidgets"].Vector())
  138. {
  139. if(auto w = widget<CIntObject>(wname.String()))
  140. w->setEnabled(true);
  141. }
  142. if((*tObj)["default"].isVector())
  143. {
  144. TurnTimerInfo tinfo;
  145. tinfo.baseTimer = (*tObj)["default"].Vector().at(0).Integer() * 1000;
  146. tinfo.turnTimer = (*tObj)["default"].Vector().at(1).Integer() * 1000;
  147. tinfo.battleTimer = (*tObj)["default"].Vector().at(2).Integer() * 1000;
  148. tinfo.creatureTimer = (*tObj)["default"].Vector().at(3).Integer() * 1000;
  149. CSH->setTurnTimerInfo(tinfo);
  150. }
  151. }
  152. redraw();
  153. }
  154. };
  155. w->getItemText = [this](int idx, const void * item){
  156. if(item)
  157. {
  158. if(auto * tObj = reinterpret_cast<const JsonNode *>(item))
  159. return readText((*tObj)["text"]);
  160. }
  161. return std::string("");
  162. };
  163. w->setItem(0);
  164. }
  165. }
  166. void OptionsTabBase::recreate()
  167. {
  168. //Simultaneous turns
  169. if(auto turnSlider = widget<CSlider>("labelSimturnsDurationValue"))
  170. turnSlider->scrollTo(SEL->getStartInfo()->simturnsInfo.optionalTurns);
  171. if(auto w = widget<CLabel>("labelSimturnsDurationValue"))
  172. {
  173. MetaString message;
  174. message.appendRawString("Simturns: up to %d days");
  175. message.replaceNumber(SEL->getStartInfo()->simturnsInfo.optionalTurns);
  176. w->setText(message.toString());
  177. }
  178. const auto & turnTimerRemote = SEL->getStartInfo()->turnTimerInfo;
  179. //classic timer
  180. if(auto turnSlider = widget<CSlider>("sliderTurnDuration"))
  181. {
  182. if(!variables["timerPresets"].isNull() && !turnTimerRemote.battleTimer && !turnTimerRemote.creatureTimer && !turnTimerRemote.baseTimer)
  183. {
  184. for(int idx = 0; idx < variables["timerPresets"].Vector().size(); ++idx)
  185. {
  186. auto & tpreset = variables["timerPresets"].Vector()[idx];
  187. if(tpreset.Vector().at(1).Integer() == turnTimerRemote.turnTimer / 1000)
  188. {
  189. turnSlider->scrollTo(idx);
  190. if(auto w = widget<CLabel>("labelTurnDurationValue"))
  191. w->setText(CGI->generaltexth->turnDurations[idx]);
  192. }
  193. }
  194. }
  195. }
  196. //chess timer
  197. auto timeToString = [](int time) -> std::string
  198. {
  199. std::stringstream ss;
  200. ss << time / 1000 / 60 << ":" << std::setw(2) << std::setfill('0') << time / 1000 % 60;
  201. return ss.str();
  202. };
  203. if(auto ww = widget<CTextInput>("chessFieldBase"))
  204. ww->setText(timeToString(turnTimerRemote.baseTimer), false);
  205. if(auto ww = widget<CTextInput>("chessFieldTurn"))
  206. ww->setText(timeToString(turnTimerRemote.turnTimer), false);
  207. if(auto ww = widget<CTextInput>("chessFieldBattle"))
  208. ww->setText(timeToString(turnTimerRemote.battleTimer), false);
  209. if(auto ww = widget<CTextInput>("chessFieldCreature"))
  210. ww->setText(timeToString(turnTimerRemote.creatureTimer), false);
  211. if(auto w = widget<ComboBox>("timerModeSwitch"))
  212. {
  213. if(turnTimerRemote.battleTimer || turnTimerRemote.creatureTimer || turnTimerRemote.baseTimer)
  214. {
  215. if(auto turnSlider = widget<CSlider>("sliderTurnDuration"))
  216. if(turnSlider->isActive())
  217. w->setItem(1);
  218. }
  219. }
  220. }