BattleOptionsTab.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 text = settings["battle"]["queueSize"].String();
  88. if(text == "none")
  89. return -1;
  90. if(text == "auto")
  91. return 0;
  92. if(text == "small")
  93. return 1;
  94. if(text == "big")
  95. return 2;
  96. return 0;
  97. }
  98. std::string BattleOptionsTab::getQueueSizeStringFromId(int value) const
  99. {
  100. switch(value)
  101. {
  102. case -1:
  103. return "none";
  104. case 0:
  105. return "auto";
  106. case 1:
  107. return "small";
  108. case 2:
  109. return "big";
  110. default:
  111. return "auto";
  112. }
  113. }
  114. void BattleOptionsTab::viewGridChangedCallback(bool value, BattleInterface * parentBattleInterface)
  115. {
  116. Settings cellBorders = settings.write["battle"]["cellBorders"];
  117. cellBorders->Bool() = value;
  118. if(parentBattleInterface)
  119. parentBattleInterface->redrawBattlefield();
  120. }
  121. void BattleOptionsTab::movementShadowChangedCallback(bool value, BattleInterface * parentBattleInterface)
  122. {
  123. Settings stackRange = settings.write["battle"]["stackRange"];
  124. stackRange->Bool() = value;
  125. if(parentBattleInterface)
  126. parentBattleInterface->redrawBattlefield();
  127. }
  128. void BattleOptionsTab::movementHighlightOnHoverChangedCallback(bool value, BattleInterface * parentBattleInterface)
  129. {
  130. Settings stackRange = settings.write["battle"]["movementHighlightOnHover"];
  131. stackRange->Bool() = value;
  132. if(parentBattleInterface)
  133. parentBattleInterface->redrawBattlefield();
  134. }
  135. void BattleOptionsTab::mouseShadowChangedCallback(bool value)
  136. {
  137. Settings shadow = settings.write["battle"]["mouseShadow"];
  138. shadow->Bool() = value;
  139. }
  140. void BattleOptionsTab::touchscreenModeChangedCallback(bool value, BattleInterface * parentBattleInterface)
  141. {
  142. Settings touchcreenMode = settings.write["battle"]["touchscreenMode"];
  143. touchcreenMode->Bool() = value;
  144. if(parentBattleInterface)
  145. parentBattleInterface->actionsController->setTouchScreenMode(value);
  146. }
  147. void BattleOptionsTab::animationSpeedChangedCallback(int value)
  148. {
  149. Settings speed = settings.write["battle"]["speedFactor"];
  150. speed->Float() = static_cast<float>(value);
  151. auto targetLabel = widget<CLabel>("animationSpeedValueLabel");
  152. int valuePercentage = value * 100 / 3; // H3 max value is "3", displaying it to be 100%
  153. if (targetLabel)
  154. targetLabel->setText(std::to_string(valuePercentage) + "%");
  155. }
  156. void BattleOptionsTab::showQueueChangedCallback(bool value, BattleInterface * parentBattleInterface)
  157. {
  158. if(!parentBattleInterface)
  159. {
  160. Settings showQueue = settings.write["battle"]["showQueue"];
  161. showQueue->Bool() = value;
  162. }
  163. else
  164. {
  165. parentBattleInterface->setBattleQueueVisibility(value);
  166. }
  167. }
  168. void BattleOptionsTab::queueSizeChangedCallback(int value, BattleInterface * parentBattleInterface)
  169. {
  170. if (value == -1)
  171. {
  172. showQueueChangedCallback(false, parentBattleInterface);
  173. return;
  174. }
  175. std::string stringifiedValue = getQueueSizeStringFromId(value);
  176. Settings size = settings.write["battle"]["queueSize"];
  177. size->String() = stringifiedValue;
  178. showQueueChangedCallback(true, parentBattleInterface);
  179. }
  180. void BattleOptionsTab::skipBattleIntroMusicChangedCallback(bool value)
  181. {
  182. Settings musicSkipSettingValue = settings.write["gameTweaks"]["skipBattleIntroMusic"];
  183. musicSkipSettingValue->Bool() = value;
  184. }