From f039b2065344fd1046277cb1c65615a0fb7e05e6 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Thu, 2 Nov 2023 15:49:21 +0200 Subject: [PATCH] Improvement for wandering monster tooltip/hover text: - show Visions information only on right-click (H3 logic) - show threat level only on right-click and only if UI tweaks are on --- client/widgets/MiscWidgets.cpp | 10 +++-- lib/mapObjects/CGCreature.cpp | 71 +++++++++++++++++++++++----------- lib/mapObjects/CGCreature.h | 3 ++ 3 files changed, 58 insertions(+), 26 deletions(-) diff --git a/client/widgets/MiscWidgets.cpp b/client/widgets/MiscWidgets.cpp index 0e8d8c353..d2b879a01 100644 --- a/client/widgets/MiscWidgets.cpp +++ b/client/widgets/MiscWidgets.cpp @@ -515,14 +515,16 @@ CreatureTooltip::CreatureTooltip(Point pos, const CGCreature * creature) { OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE; - auto creatureData = (*CGI->creh)[creature->stacks.begin()->second->getCreatureID()].get(); - creatureImage = std::make_shared(graphics->getAnimation(AnimationPath::builtin("TWCRPORT")), creatureData->getIconIndex()); + auto creatureID = creature->getCreature(); + int32_t creatureIconIndex = CGI->creatures()->getById(creatureID)->getIconIndex(); + + creatureImage = std::make_shared(graphics->getAnimation(AnimationPath::builtin("TWCRPORT")), creatureIconIndex); creatureImage->center(Point(parent->pos.x + parent->pos.w / 2, parent->pos.y + creatureImage->pos.h / 2 + 11)); bool isHeroSelected = LOCPLINT->localState->getCurrentHero() != nullptr; std::string textContent = isHeroSelected - ? creature->getHoverText(LOCPLINT->localState->getCurrentHero()) - : creature->getHoverText(LOCPLINT->playerID); + ? creature->getPopupText(LOCPLINT->localState->getCurrentHero()) + : creature->getPopupText(LOCPLINT->playerID); //TODO: window is bigger than OH3 //TODO: vertical alignment does not match H3. Commented below example that matches H3 for creatures count but supports only 1 line: diff --git a/lib/mapObjects/CGCreature.cpp b/lib/mapObjects/CGCreature.cpp index 04af6f8ad..cd5d90e82 100644 --- a/lib/mapObjects/CGCreature.cpp +++ b/lib/mapObjects/CGCreature.cpp @@ -32,7 +32,6 @@ std::string CGCreature::getHoverText(PlayerColor player) const return "INVALID_STACK"; } - std::string hoverName; MetaString ms; CCreature::CreatureQuantityId monsterQuantityId = stacks.begin()->second->getQuantityID(); int quantityTextIndex = 172 + 3 * (int)monsterQuantityId; @@ -42,20 +41,33 @@ std::string CGCreature::getHoverText(PlayerColor player) const ms.appendLocalString(EMetaText::ARRAY_TXT, quantityTextIndex); ms.appendRawString(" "); ms.appendLocalString(EMetaText::CRE_PL_NAMES, getCreature()); - hoverName = ms.toString(); - return hoverName; + + return ms.toString(); } std::string CGCreature::getHoverText(const CGHeroInstance * hero) const { - std::string hoverName; if(hero->hasVisions(this, BonusCustomSubtype::visionsMonsters)) { MetaString ms; ms.appendNumber(stacks.begin()->second->count); ms.appendRawString(" "); ms.appendLocalString(EMetaText::CRE_PL_NAMES, getCreature()); + return ms.toString(); + } + else + { + return getHoverText(hero->tempOwner); + } +} +std::string CGCreature::getPopupText(const CGHeroInstance * hero) const +{ + std::string hoverName; + if(hero->hasVisions(this, BonusCustomSubtype::visionsMonsters)) + { + MetaString ms; + ms.appendRawString(getHoverText(hero)); ms.appendRawString("\n\n"); int decision = takenAction(hero, true); @@ -72,10 +84,10 @@ std::string CGCreature::getHoverText(const CGHeroInstance * hero) const ms.appendLocalString(EMetaText::GENERAL_TXT,243); break; default: //decision = cost in gold - ms.appendRawString(boost::str(boost::format(VLC->generaltexth->allTexts[244]) % decision)); + ms.appendLocalString(EMetaText::GENERAL_TXT,244); + ms.replaceNumber(decision); break; } - hoverName = ms.toString(); } else @@ -83,27 +95,42 @@ std::string CGCreature::getHoverText(const CGHeroInstance * hero) const hoverName = getHoverText(hero->tempOwner); } - hoverName += VLC->generaltexth->translate("vcmi.adventureMap.monsterThreat.title"); + if (settings["general"]["enableUiEnhancements"].Bool()) + { + hoverName += VLC->generaltexth->translate("vcmi.adventureMap.monsterThreat.title"); - int choice; - double ratio = (static_cast(getArmyStrength()) / hero->getTotalStrength()); - if (ratio < 0.1) choice = 0; - else if (ratio < 0.25) choice = 1; - else if (ratio < 0.6) choice = 2; - else if (ratio < 0.9) choice = 3; - else if (ratio < 1.1) choice = 4; - else if (ratio < 1.3) choice = 5; - else if (ratio < 1.8) choice = 6; - else if (ratio < 2.5) choice = 7; - else if (ratio < 4) choice = 8; - else if (ratio < 8) choice = 9; - else if (ratio < 20) choice = 10; - else choice = 11; + int choice; + double ratio = (static_cast(getArmyStrength()) / hero->getTotalStrength()); + if (ratio < 0.1) choice = 0; + else if (ratio < 0.25) choice = 1; + else if (ratio < 0.6) choice = 2; + else if (ratio < 0.9) choice = 3; + else if (ratio < 1.1) choice = 4; + else if (ratio < 1.3) choice = 5; + else if (ratio < 1.8) choice = 6; + else if (ratio < 2.5) choice = 7; + else if (ratio < 4) choice = 8; + else if (ratio < 8) choice = 9; + else if (ratio < 20) choice = 10; + else choice = 11; - hoverName += VLC->generaltexth->translate("vcmi.adventureMap.monsterThreat.levels." + std::to_string(choice)); + hoverName += VLC->generaltexth->translate("vcmi.adventureMap.monsterThreat.levels." + std::to_string(choice)); + } return hoverName; } +std::string CGCreature::getPopupText(PlayerColor player) const +{ + return getHoverText(player); +} + +std::vector CGCreature::getPopupComponents(PlayerColor player) const +{ + return { + Component(ComponentType::CREATURE, getCreature()) + }; +} + void CGCreature::onHeroVisit( const CGHeroInstance * h ) const { //show message diff --git a/lib/mapObjects/CGCreature.h b/lib/mapObjects/CGCreature.h index 13ff02f30..f2101d3b0 100644 --- a/lib/mapObjects/CGCreature.h +++ b/lib/mapObjects/CGCreature.h @@ -40,6 +40,9 @@ public: void onHeroVisit(const CGHeroInstance * h) const override; std::string getHoverText(PlayerColor player) const override; std::string getHoverText(const CGHeroInstance * hero) const override; + std::string getPopupText(PlayerColor player) const override; + std::string getPopupText(const CGHeroInstance * hero) const override; + std::vector getPopupComponents(PlayerColor player) const override; void initObj(CRandomGenerator & rand) override; void pickRandomObject(CRandomGenerator & rand) override; void newTurn(CRandomGenerator & rand) const override;