diff --git a/client/battle/BattleInterfaceClasses.cpp b/client/battle/BattleInterfaceClasses.cpp index d10956326..b9775eba8 100644 --- a/client/battle/BattleInterfaceClasses.cpp +++ b/client/battle/BattleInterfaceClasses.cpp @@ -433,6 +433,49 @@ QuickSpellPanel::QuickSpellPanel(BattleInterface & owner) create(); } +std::vector> QuickSpellPanel::getSpells() +{ + std::vector spellIds; + std::vector spellIdsFromSetting; + for(int i = 0; i < QUICKSPELL_SLOTS; i++) + { + std::string spellIdentifier = persistentStorage["quickSpell"][std::to_string(i)].String(); + SpellID id; + try + { + id = SpellID::decode(spellIdentifier); + } + catch(const IdentifierResolutionException& e) + { + id = SpellID::NONE; + } + spellIds.push_back(id); + spellIdsFromSetting.push_back(id != SpellID::NONE); + } + + // autofill empty slots with spells if possible + auto hero = owner.getBattle()->battleGetMyHero(); + for(int i = 0; i < QUICKSPELL_SLOTS; i++) + { + if(spellIds[i] != SpellID::NONE) + continue; + + for(auto & availableSpell : CGI->spellh->objects) + { + if(!availableSpell->isAdventure() && !availableSpell->isCreatureAbility() && hero->canCastThisSpell(availableSpell.get()) && !vstd::contains(spellIds, availableSpell->getId())) + { + spellIds[i] = availableSpell->getId(); + break; + } + } + } + + std::vector> ret; + for(int i = 0; i < QUICKSPELL_SLOTS; i++) + ret.push_back(std::make_tuple(spellIds[i], spellIdsFromSetting[i])); + return ret; +} + void QuickSpellPanel::create() { OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE); @@ -447,18 +490,11 @@ void QuickSpellPanel::create() if(!hero) return; - for(int i = 0; i < 12; i++) { - std::string spellIdentifier = persistentStorage["quickSpell"][std::to_string(i)].String(); - + auto spells = getSpells(); + for(int i = 0; i < QUICKSPELL_SLOTS; i++) { SpellID id; - try - { - id = SpellID::decode(spellIdentifier); - } - catch(const IdentifierResolutionException& e) - { - id = SpellID::NONE; - } + bool fromSettings; + std::tie(id, fromSettings) = spells[i]; auto button = std::make_shared(Point(2, 7 + 50 * i), AnimationPath::builtin("spellint"), CButton::tooltip(), [this, id, hero](){ if(id.hasValue() && id.toSpell()->canBeCast(owner.getBattle().get(), spells::Mode::HERO, hero)) @@ -466,16 +502,19 @@ void QuickSpellPanel::create() owner.castThisSpell(id); } }); - button->setOverlay(std::make_shared(AnimationPath::builtin("spellint"), !spellIdentifier.empty() ? id.num + 1 : 0)); + button->setOverlay(std::make_shared(AnimationPath::builtin("spellint"), id != SpellID::NONE ? id.num + 1 : 0)); button->addPopupCallback([this, i, hero](){ GH.input().hapticFeedback(); GH.windows().createAndPushWindow(hero, owner.curInt.get(), true, [this, i](SpellID spell){ Settings configID = persistentStorage.write["quickSpell"][std::to_string(i)]; - configID->String() = spell.toSpell()->identifier; + configID->String() = spell == SpellID::NONE ? "" : spell.toSpell()->identifier; create(); }); }); + if(fromSettings) + buttonsIsAutoGenerated.push_back(std::make_shared(Rect(45, 37 + 50 * i, 5, 5), Colors::ORANGE)); + if(!id.hasValue() || !id.toSpell()->canBeCast(owner.getBattle().get(), spells::Mode::HERO, hero)) { buttonsDisabled.push_back(std::make_shared(Rect(2, 7 + 50 * i, 48, 36), ColorRGBA(0, 0, 0, 172))); diff --git a/client/battle/BattleInterfaceClasses.h b/client/battle/BattleInterfaceClasses.h index a1a16121a..2af8c6bbd 100644 --- a/client/battle/BattleInterfaceClasses.h +++ b/client/battle/BattleInterfaceClasses.h @@ -155,17 +155,22 @@ private: std::shared_ptr background; std::shared_ptr rect; std::vector> buttons; + std::vector> buttonsIsAutoGenerated; std::vector> buttonsDisabled; std::vector> labels; BattleInterface & owner; public: + int QUICKSPELL_SLOTS = 12; + bool isEnabled; // isActive() is not working on multiple conditions, because of this we need a seperate flag QuickSpellPanel(BattleInterface & owner); void create(); + std::vector> getSpells(); + void show(Canvas & to) override; void inputModeChanged(InputMode modi) override; }; diff --git a/client/battle/BattleWindow.cpp b/client/battle/BattleWindow.cpp index 15216c811..26330cfda 100644 --- a/client/battle/BattleWindow.cpp +++ b/client/battle/BattleWindow.cpp @@ -273,16 +273,13 @@ std::shared_ptr BattleWindow::buildBattleConsole(const JsonNode & void BattleWindow::useSpellIfPossible(int slot) { - std::string spellIdentifier = persistentStorage["quickSpell"][std::to_string(slot)].String(); SpellID id; - try - { - id = SpellID::decode(spellIdentifier); - } - catch(const IdentifierResolutionException& e) - { + bool fromSettings; + std::tie(id, fromSettings) = quickSpellWindow->getSpells()[slot]; + + if(id == SpellID::NONE) return; - } + if(id.hasValue() && owner.getBattle()->battleGetMyHero() && id.toSpell()->canBeCast(owner.getBattle().get(), spells::Mode::HERO, owner.getBattle()->battleGetMyHero())) { owner.castThisSpell(id); diff --git a/client/windows/CSpellWindow.cpp b/client/windows/CSpellWindow.cpp index 6c562e191..ad2f4e4bb 100644 --- a/client/windows/CSpellWindow.cpp +++ b/client/windows/CSpellWindow.cpp @@ -29,6 +29,7 @@ #include "../widgets/CComponent.h" #include "../widgets/CTextInput.h" #include "../widgets/TextControls.h" +#include "../widgets/Buttons.h" #include "../adventureMap/AdventureMapInterface.h" #include "../render/IRenderHandler.h" #include "../render/IImage.h" @@ -130,9 +131,9 @@ CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _m pos = background->center(Point(pos.w/2 + pos.x, pos.h/2 + pos.y)); + Rect r(90, isBigSpellbook ? 480 : 420, isBigSpellbook ? 160 : 110, 16); if(settings["general"]["enableUiEnhancements"].Bool()) { - Rect r(90, isBigSpellbook ? 480 : 420, isBigSpellbook ? 160 : 110, 16); const ColorRGBA rectangleColor = ColorRGBA(0, 0, 0, 75); const ColorRGBA borderColor = ColorRGBA(128, 100, 75); const ColorRGBA grayedColor = ColorRGBA(158, 130, 105); @@ -143,6 +144,13 @@ CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _m searchBox->setCallback(std::bind(&CSpellWindow::searchInput, this)); } + if(onSpellSelect) + { + Point boxPos = r.bottomLeft() + Point(-2, 5); + showAllSpells = std::make_shared(boxPos, AnimationPath::builtin("sysopchk.def"), CButton::tooltip(CGI->generaltexth->translate("core.help.458.hover"), CGI->generaltexth->translate("core.help.458.hover")), [this](bool state){ searchInput(); }); + showAllSpellsDescription = std::make_shared(boxPos.x + 40, boxPos.y + 12, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, CGI->generaltexth->translate("core.help.458.hover")); + } + processSpells(); //numbers of spell pages computed @@ -288,7 +296,7 @@ void CSpellWindow::processSpells() if(onSpellSelect) { - if(spell->isCombat() == openOnBattleSpells && !spell->isSpecial() && !spell->isCreatureAbility() && searchTextFound) + if(spell->isCombat() == openOnBattleSpells && !spell->isSpecial() && !spell->isCreatureAbility() && searchTextFound && (showAllSpells->isSelected() || myHero->canCastThisSpell(spell.get()))) mySpells.push_back(spell.get()); continue; } @@ -359,6 +367,9 @@ void CSpellWindow::fexitb() (myInt->battleInt ? myInt->localState->spellbookSettings.spellbookLastTabBattle : myInt->localState->spellbookSettings.spellbookLastTabAdvmap) = selectedTab; (myInt->battleInt ? myInt->localState->spellbookSettings.spellbookLastPageBattle : myInt->localState->spellbookSettings.spellbookLastPageAdvmap) = currentPage; + if(onSpellSelect) + onSpellSelect(SpellID::NONE); + close(); } @@ -605,7 +616,7 @@ void CSpellWindow::SpellArea::clickPressed(const Point & cursorPosition) if(owner->onSpellSelect) { owner->onSpellSelect(mySpell->id); - owner->fexitb(); + owner->close(); return; } diff --git a/client/windows/CSpellWindow.h b/client/windows/CSpellWindow.h index 73dee4e4c..3119860ca 100644 --- a/client/windows/CSpellWindow.h +++ b/client/windows/CSpellWindow.h @@ -27,6 +27,7 @@ class CPlayerInterface; class CSpellWindow; class CTextInput; class TransparentFilledRectangle; +class CToggleButton; /// The spell window class CSpellWindow : public CWindowObject @@ -82,6 +83,9 @@ class CSpellWindow : public CWindowObject std::shared_ptr searchBoxRectangle; std::shared_ptr searchBoxDescription; + std::shared_ptr showAllSpells; + std::shared_ptr showAllSpellsDescription; + bool isBigSpellbook; int spellsPerPage; int offL;