BattleOptionsTab.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * BattleOptionsTab.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 "BattleOptionsTab.h"
  12. #include "CConfigHandler.h"
  13. #include "../../battle/BattleInterface.h"
  14. #include "../../battle/BattleActionsController.h"
  15. #include "../../gui/CGuiHandler.h"
  16. #include "../../../lib/filesystem/ResourceID.h"
  17. #include "../../../lib/CGeneralTextHandler.h"
  18. #include "../../widgets/Buttons.h"
  19. #include "../../widgets/TextControls.h"
  20. BattleOptionsTab::BattleOptionsTab(BattleInterface * owner)
  21. {
  22. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  23. type |= REDRAW_PARENT;
  24. const JsonNode config(ResourceID("config/widgets/settings/battleOptionsTab.json"));
  25. addCallback("viewGridChanged", [this, owner](bool value)
  26. {
  27. viewGridChangedCallback(value, owner);
  28. });
  29. addCallback("movementShadowChanged", [this, owner](bool value)
  30. {
  31. movementShadowChangedCallback(value, owner);
  32. });
  33. addCallback("movementHighlightOnHoverChanged", [this, owner](bool value)
  34. {
  35. movementHighlightOnHoverChangedCallback(value, owner);
  36. });
  37. addCallback("mouseShadowChanged", [this](bool value)
  38. {
  39. mouseShadowChangedCallback(value);
  40. });
  41. addCallback("animationSpeedChanged", [this](int value)
  42. {
  43. animationSpeedChangedCallback(value);
  44. });
  45. addCallback("showQueueChanged", [this, owner](bool value)
  46. {
  47. showQueueChangedCallback(value, owner);
  48. });
  49. addCallback("queueSizeChanged", [this, owner](int value)
  50. {
  51. queueSizeChangedCallback(value, owner);
  52. });
  53. addCallback("skipBattleIntroMusicChanged", [this](bool value)
  54. {
  55. skipBattleIntroMusicChangedCallback(value);
  56. });
  57. addCallback("touchscreenModeChanged", [this, owner](bool value)
  58. {
  59. touchscreenModeChangedCallback(value, owner);
  60. });
  61. build(config);
  62. std::shared_ptr<CToggleGroup> animationSpeedToggle = widget<CToggleGroup>("animationSpeedPicker");
  63. animationSpeedToggle->setSelected(getAnimSpeed());
  64. std::shared_ptr<CToggleGroup> queueSizeToggle = widget<CToggleGroup>("queueSizePicker");
  65. queueSizeToggle->setSelected(getQueueSizeId());
  66. std::shared_ptr<CToggleButton> viewGridCheckbox = widget<CToggleButton>("viewGridCheckbox");
  67. viewGridCheckbox->setSelected(settings["battle"]["cellBorders"].Bool());
  68. std::shared_ptr<CToggleButton> movementShadowCheckbox = widget<CToggleButton>("movementShadowCheckbox");
  69. movementShadowCheckbox->setSelected(settings["battle"]["stackRange"].Bool());
  70. std::shared_ptr<CToggleButton> movementHighlightOnHoverCheckbox = widget<CToggleButton>("movementHighlightOnHoverCheckbox");
  71. movementHighlightOnHoverCheckbox->setSelected(settings["battle"]["movementHighlightOnHover"].Bool());
  72. std::shared_ptr<CToggleButton> mouseShadowCheckbox = widget<CToggleButton>("mouseShadowCheckbox");
  73. mouseShadowCheckbox->setSelected(settings["battle"]["mouseShadow"].Bool());
  74. std::shared_ptr<CToggleButton> touchscreenModeCheckbox = widget<CToggleButton>("touchscreenModeCheckbox");
  75. touchscreenModeCheckbox->setSelected(settings["battle"]["touchscreenMode"].Bool());
  76. std::shared_ptr<CToggleButton> skipBattleIntroMusicCheckbox = widget<CToggleButton>("skipBattleIntroMusicCheckbox");
  77. skipBattleIntroMusicCheckbox->setSelected(settings["gameTweaks"]["skipBattleIntroMusic"].Bool());
  78. }
  79. int BattleOptionsTab::getAnimSpeed() const
  80. {
  81. if(settings["session"]["spectate"].Bool() && !settings["session"]["spectate-battle-speed"].isNull())
  82. return static_cast<int>(std::round(settings["session"]["spectate-battle-speed"].Float()));
  83. return static_cast<int>(std::round(settings["battle"]["speedFactor"].Float()));
  84. }
  85. int BattleOptionsTab::getQueueSizeId() const
  86. {
  87. std::string sizeText = settings["battle"]["queueSize"].String();
  88. bool visible = settings["battle"]["showQueue"].Bool();
  89. if(!visible)
  90. return -1;
  91. if(sizeText == "none")
  92. return -1;
  93. if(sizeText == "auto")
  94. return 0;
  95. if(sizeText == "small")
  96. return 1;
  97. if(sizeText == "big")
  98. return 2;
  99. return 0;
  100. }
  101. std::string BattleOptionsTab::getQueueSizeStringFromId(int value) const
  102. {
  103. switch(value)
  104. {
  105. case -1:
  106. return "none";
  107. case 0:
  108. return "auto";
  109. case 1:
  110. return "small";
  111. case 2:
  112. return "big";
  113. default:
  114. return "auto";
  115. }
  116. }
  117. void BattleOptionsTab::viewGridChangedCallback(bool value, BattleInterface * parentBattleInterface)
  118. {
  119. Settings cellBorders = settings.write["battle"]["cellBorders"];
  120. cellBorders->Bool() = value;
  121. if(parentBattleInterface)
  122. parentBattleInterface->redrawBattlefield();
  123. }
  124. void BattleOptionsTab::movementShadowChangedCallback(bool value, BattleInterface * parentBattleInterface)
  125. {
  126. Settings stackRange = settings.write["battle"]["stackRange"];
  127. stackRange->Bool() = value;
  128. if(parentBattleInterface)
  129. parentBattleInterface->redrawBattlefield();
  130. }
  131. void BattleOptionsTab::movementHighlightOnHoverChangedCallback(bool value, BattleInterface * parentBattleInterface)
  132. {
  133. Settings stackRange = settings.write["battle"]["movementHighlightOnHover"];
  134. stackRange->Bool() = value;
  135. if(parentBattleInterface)
  136. parentBattleInterface->redrawBattlefield();
  137. }
  138. void BattleOptionsTab::mouseShadowChangedCallback(bool value)
  139. {
  140. Settings shadow = settings.write["battle"]["mouseShadow"];
  141. shadow->Bool() = value;
  142. }
  143. void BattleOptionsTab::touchscreenModeChangedCallback(bool value, BattleInterface * parentBattleInterface)
  144. {
  145. Settings touchcreenMode = settings.write["battle"]["touchscreenMode"];
  146. touchcreenMode->Bool() = value;
  147. if(parentBattleInterface)
  148. parentBattleInterface->actionsController->setTouchScreenMode(value);
  149. }
  150. void BattleOptionsTab::animationSpeedChangedCallback(int value)
  151. {
  152. Settings speed = settings.write["battle"]["speedFactor"];
  153. speed->Float() = static_cast<float>(value);
  154. auto targetLabel = widget<CLabel>("animationSpeedValueLabel");
  155. int valuePercentage = value * 100 / 3; // H3 max value is "3", displaying it to be 100%
  156. if (targetLabel)
  157. targetLabel->setText(std::to_string(valuePercentage) + "%");
  158. }
  159. void BattleOptionsTab::showQueueChangedCallback(bool value, BattleInterface * parentBattleInterface)
  160. {
  161. if(!parentBattleInterface)
  162. {
  163. Settings showQueue = settings.write["battle"]["showQueue"];
  164. showQueue->Bool() = value;
  165. }
  166. else
  167. {
  168. parentBattleInterface->setBattleQueueVisibility(value);
  169. }
  170. }
  171. void BattleOptionsTab::queueSizeChangedCallback(int value, BattleInterface * parentBattleInterface)
  172. {
  173. if (value == -1)
  174. {
  175. showQueueChangedCallback(false, parentBattleInterface);
  176. return;
  177. }
  178. std::string stringifiedValue = getQueueSizeStringFromId(value);
  179. Settings size = settings.write["battle"]["queueSize"];
  180. size->String() = stringifiedValue;
  181. showQueueChangedCallback(true, parentBattleInterface);
  182. }
  183. void BattleOptionsTab::skipBattleIntroMusicChangedCallback(bool value)
  184. {
  185. Settings musicSkipSettingValue = settings.write["gameTweaks"]["skipBattleIntroMusic"];
  186. musicSkipSettingValue->Bool() = value;
  187. }