QuickSpellPanel.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * QuickSpellPanel.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 "QuickSpellPanel.h"
  12. #include "BattleInterface.h"
  13. #include "../GameEngine.h"
  14. #include "../eventsSDL/InputHandler.h"
  15. #include "../gui/WindowHandler.h"
  16. #include "../widgets/Buttons.h"
  17. #include "../widgets/GraphicalPrimitiveCanvas.h"
  18. #include "../widgets/Images.h"
  19. #include "../widgets/TextControls.h"
  20. #include "../windows/CSpellWindow.h"
  21. #include "../../lib/CConfigHandler.h"
  22. #include "../../lib/GameLibrary.h"
  23. #include "../../lib/battle/CPlayerBattleCallback.h"
  24. #include "../../lib/json/JsonUtils.h"
  25. #include "../../lib/mapObjects/CGHeroInstance.h"
  26. #include "../../lib/spells/CSpellHandler.h"
  27. QuickSpellPanel::QuickSpellPanel(BattleInterface & owner)
  28. : CIntObject(0)
  29. , owner(owner)
  30. , isEnabled(true)
  31. {
  32. OBJECT_CONSTRUCTION;
  33. addUsedEvents(LCLICK | SHOW_POPUP | MOVE | INPUT_MODE_CHANGE);
  34. pos = Rect(0, 0, 52, 600);
  35. background = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), pos);
  36. rect = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w + 1, pos.h + 1), ColorRGBA(0, 0, 0, 0), ColorRGBA(241, 216, 120, 255));
  37. create();
  38. }
  39. std::vector<std::tuple<SpellID, bool>> QuickSpellPanel::getSpells() const
  40. {
  41. std::vector<SpellID> spellIds;
  42. std::vector<bool> spellIdsFromSetting;
  43. for(int i = 0; i < QUICKSPELL_SLOTS; i++)
  44. {
  45. std::string spellIdentifier = persistentStorage["quickSpell"][std::to_string(i)].String();
  46. SpellID id;
  47. try
  48. {
  49. id = SpellID::decode(spellIdentifier);
  50. }
  51. catch(const IdentifierResolutionException & e)
  52. {
  53. id = SpellID::NONE;
  54. }
  55. spellIds.push_back(id);
  56. spellIdsFromSetting.push_back(id != SpellID::NONE);
  57. }
  58. // autofill empty slots with spells if possible
  59. const auto * hero = owner.getBattle()->battleGetMyHero();
  60. for(int i = 0; i < QUICKSPELL_SLOTS; i++)
  61. {
  62. if(spellIds[i] != SpellID::NONE)
  63. continue;
  64. for(const auto & availableSpellID : LIBRARY->spellh->getDefaultAllowed())
  65. {
  66. const auto * availableSpell = availableSpellID.toSpell();
  67. if(!availableSpell->isAdventure() && !availableSpell->isCreatureAbility() && hero->canCastThisSpell(availableSpell) && !vstd::contains(spellIds, availableSpell->getId()))
  68. {
  69. spellIds[i] = availableSpell->getId();
  70. break;
  71. }
  72. }
  73. }
  74. std::vector<std::tuple<SpellID, bool>> ret;
  75. for(int i = 0; i < QUICKSPELL_SLOTS; i++)
  76. ret.push_back(std::make_tuple(spellIds[i], spellIdsFromSetting[i]));
  77. return ret;
  78. }
  79. void QuickSpellPanel::create()
  80. {
  81. OBJECT_CONSTRUCTION;
  82. const JsonNode config = JsonUtils::assembleFromFiles("config/shortcutsConfig");
  83. labels.clear();
  84. buttons.clear();
  85. buttonsDisabled.clear();
  86. const auto * hero = owner.getBattle()->battleGetMyHero();
  87. if(!hero)
  88. return;
  89. auto spells = getSpells();
  90. for(int i = 0; i < QUICKSPELL_SLOTS; i++)
  91. {
  92. SpellID id;
  93. bool fromSettings;
  94. std::tie(id, fromSettings) = spells[i];
  95. auto button = std::make_shared<CButton>(Point(2, 7 + 50 * i), AnimationPath::builtin("spellint"), CButton::tooltip(), [this, id, hero](){
  96. if(id.hasValue() && id.toSpell()->canBeCast(owner.getBattle().get(), spells::Mode::HERO, hero))
  97. {
  98. owner.castThisSpell(id);
  99. }
  100. });
  101. button->setOverlay(std::make_shared<CAnimImage>(AnimationPath::builtin("spellint"), id != SpellID::NONE ? id.num + 1 : 0));
  102. button->addPopupCallback([this, i, hero](){
  103. ENGINE->input().hapticFeedback();
  104. ENGINE->windows().createAndPushWindow<CSpellWindow>(hero, owner.curInt.get(), true, [this, i](SpellID spell){
  105. Settings configID = persistentStorage.write["quickSpell"][std::to_string(i)];
  106. configID->String() = spell == SpellID::NONE ? "" : spell.toSpell()->identifier;
  107. create();
  108. });
  109. });
  110. if(fromSettings)
  111. buttonsIsAutoGenerated.push_back(std::make_shared<TransparentFilledRectangle>(Rect(45, 37 + 50 * i, 5, 5), Colors::ORANGE));
  112. if(!id.hasValue() || !id.toSpell()->canBeCast(owner.getBattle().get(), spells::Mode::HERO, hero))
  113. {
  114. buttonsDisabled.push_back(std::make_shared<TransparentFilledRectangle>(Rect(2, 7 + 50 * i, 48, 36), ColorRGBA(0, 0, 0, 172)));
  115. }
  116. if(ENGINE->input().getCurrentInputMode() == InputMode::KEYBOARD_AND_MOUSE)
  117. labels.push_back(std::make_shared<CLabel>(7, 10 + 50 * i, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, config["keyboard"]["battleSpellShortcut" + std::to_string(i)].String()));
  118. buttons.push_back(button);
  119. }
  120. }
  121. void QuickSpellPanel::show(Canvas & to)
  122. {
  123. showAll(to);
  124. CIntObject::show(to);
  125. }
  126. void QuickSpellPanel::inputModeChanged(InputMode modi)
  127. {
  128. create();
  129. redraw();
  130. }