QuickSpellPanel.cpp 4.7 KB

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