From 5127061e284f329098d9b6b7f49777f08dafa407 Mon Sep 17 00:00:00 2001 From: Dydzio Date: Fri, 26 May 2017 21:58:33 +0200 Subject: [PATCH] Commander skill description support (#266) * Added commander skill description support * Add support for ZNPC00.txt not found --- client/windows/CCreatureWindow.cpp | 54 +++++++++++++++++++++++++----- client/windows/CCreatureWindow.h | 12 +++---- lib/CGeneralTextHandler.cpp | 17 ++++++++++ lib/CGeneralTextHandler.h | 2 ++ 4 files changed, 70 insertions(+), 15 deletions(-) diff --git a/client/windows/CCreatureWindow.cpp b/client/windows/CCreatureWindow.cpp index fa1c4cac7..4be09a8ba 100644 --- a/client/windows/CCreatureWindow.cpp +++ b/client/windows/CCreatureWindow.cpp @@ -435,17 +435,31 @@ void CStackWindow::CWindowSection::createCommander() return skillToFile(skillIndex, parent->info->commander->secondarySkills[skillIndex], selected); }; + auto getSkillDescription = [this](int skillIndex) -> std::string + { + if (CGI->generaltexth->znpc00.size() == 0) + return ""; + + return CGI->generaltexth->znpc00[151 + (12 * skillIndex) + (parent->info->commander->secondarySkills[skillIndex] * 2)]; + }; + for (int index = ECommander::ATTACK; index <= ECommander::SPELL_POWER; ++index) { Point skillPos = getSkillPos(index); - auto icon = new CClickableObject(new CPicture(getSkillImage(index), skillPos.x, skillPos.y), [=]{}); + auto icon = new CCommanderSkillIcon(new CPicture(getSkillImage(index), skillPos.x, skillPos.y), + [=] { LOCPLINT->showInfoDialog(getSkillDescription(index)); }); - if (parent->selectedSkill == index) + icon->text = getSkillDescription(index); //used to handle right click description via LRClickableAreaWText::ClickRight() + + if(parent->selectedSkill == index) parent->selectedIcon = icon; - if (parent->info->levelupInfo && vstd::contains(parent->info->levelupInfo->skills, index)) // can be upgraded - enable selection switch + if(parent->info->levelupInfo && vstd::contains(parent->info->levelupInfo->skills, index)) // can be upgraded - enable selection switch { + if(parent->selectedSkill == index) + parent->setSelection(index, icon); + icon->callback = [=] { parent->setSelection(index, icon); @@ -472,7 +486,7 @@ CIntObject * CStackWindow::createSkillEntry(int index) { const auto bonus = CGI->creh->skillRequirements[skillID-100].first; const CStackInstance *stack = info->commander; - CClickableObject * icon = new CClickableObject(new CPicture(stack->bonusToGraphics(bonus)), []{}); + CCommanderSkillIcon * icon = new CCommanderSkillIcon(new CPicture(stack->bonusToGraphics(bonus)), []{}); icon->callback = [=] { setSelection(skillID, icon); @@ -518,8 +532,19 @@ void CStackWindow::CWindowSection::createCommanderAbilities() } } -void CStackWindow::setSelection(si32 newSkill, CClickableObject * newIcon) +void CStackWindow::setSelection(si32 newSkill, CCommanderSkillIcon * newIcon) { + auto getSkillDescription = [this](int skillIndex, bool selected) -> std::string + { + if (CGI->generaltexth->znpc00.size() == 0) + return ""; + + if(selected) + return CGI->generaltexth->znpc00[151 + (12 * skillIndex) + ((info->commander->secondarySkills[skillIndex] + 1) * 2)]; //upgrade description + else + return CGI->generaltexth->znpc00[151 + (12 * skillIndex) + (info->commander->secondarySkills[skillIndex] * 2)]; + }; + auto getSkillImage = [this](int skillIndex) -> std::string { bool selected = ((selectedSkill == skillIndex) && info->levelupInfo ); @@ -533,9 +558,14 @@ void CStackWindow::setSelection(si32 newSkill, CClickableObject * newIcon) if (selectedIcon && oldSelection < 100) // recreate image on old selection, only for skills selectedIcon->setObject(new CPicture(getSkillImage(oldSelection))); + selectedIcon->text = getSkillDescription(oldSelection, false); //update previously selected icon's message to existing skill level + selectedIcon = newIcon; // update new selection if (newSkill < 100) + { newIcon->setObject(new CPicture(getSkillImage(newSkill))); + newIcon->text = getSkillDescription(newSkill, true); //update currently selected icon's message to show upgrade description + } } void CStackWindow::CWindowSection::createBonuses(boost::optional preferredSize) @@ -646,7 +676,7 @@ CStackWindow::CWindowSection::CWindowSection(CStackWindow * parent): { } -CClickableObject::CClickableObject(CIntObject *object, std::function callback): +CCommanderSkillIcon::CCommanderSkillIcon(CIntObject *object, std::function callback): object(nullptr), callback(callback) { @@ -654,7 +684,7 @@ CClickableObject::CClickableObject(CIntObject *object, std::function cal setObject(object); } -void CClickableObject::setObject(CIntObject *newObject) +void CCommanderSkillIcon::setObject(CIntObject *newObject) { delete object; object = newObject; @@ -663,12 +693,18 @@ void CClickableObject::setObject(CIntObject *newObject) redraw(); } -void CClickableObject::clickLeft(tribool down, bool previousState) +void CCommanderSkillIcon::clickLeft(tribool down, bool previousState) { - if (down) + if(down) callback(); } +void CCommanderSkillIcon::clickRight(tribool down, bool previousState) +{ + if(down) + LRClickableAreaWText::clickRight(down, previousState); +} + CIntObject * CStackWindow::createBonusEntry(size_t index) { auto section = new CWindowSection(this); diff --git a/client/windows/CCreatureWindow.h b/client/windows/CCreatureWindow.h index d9f465ff6..6adf4488d 100644 --- a/client/windows/CCreatureWindow.h +++ b/client/windows/CCreatureWindow.h @@ -22,16 +22,16 @@ struct UpgradeInfo; class CTabbedInt; class CButton; -class CClickableObject : public LRClickableAreaWText +class CCommanderSkillIcon : public LRClickableAreaWText //TODO: maybe bring commander skill button initialization logic inside? { CIntObject * object; // passive object that will be used to determine clickable area public: - CClickableObject(CIntObject * object, std::function callback); + CCommanderSkillIcon(CIntObject * object, std::function callback); - std::function callback; //TODO: create more generic clickable class than AdvMapButton? + std::function callback; void clickLeft(tribool down, bool previousState) override; - //void clickRight(tribool down, bool previousState){}; + void clickRight(tribool down, bool previousState) override; void setObject(CIntObject * object); }; @@ -83,8 +83,8 @@ class CStackWindow : public CWindowObject std::map switchButtons; - void setSelection(si32 newSkill, CClickableObject * newIcon); - CClickableObject * selectedIcon; + void setSelection(si32 newSkill, CCommanderSkillIcon * newIcon); + CCommanderSkillIcon * selectedIcon; si32 selectedSkill; CIntObject * createBonusEntry(size_t index); diff --git a/lib/CGeneralTextHandler.cpp b/lib/CGeneralTextHandler.cpp index 734f1e62e..4f5edef25 100644 --- a/lib/CGeneralTextHandler.cpp +++ b/lib/CGeneralTextHandler.cpp @@ -490,4 +490,21 @@ CGeneralTextHandler::CGeneralTextHandler() } while (parser.endLine()); } + if (VLC->modh->modules.COMMANDERS) + { + try + { + CLegacyConfigParser parser("DATA/ZNPC00.TXT"); + parser.endLine();//header + + do + { + znpc00.push_back(parser.readString()); + } while (parser.endLine()); + } + catch (std::runtime_error) + { + logGlobal->warn("WoG file ZNPC00.TXT containing commander texts was not found"); + } + } } diff --git a/lib/CGeneralTextHandler.h b/lib/CGeneralTextHandler.h index a0debd1b2..2dd94246c 100644 --- a/lib/CGeneralTextHandler.h +++ b/lib/CGeneralTextHandler.h @@ -135,6 +135,8 @@ public: std::vector> skillInfoTexts; //[id][level] : level 0 - basic; 2 - advanced std::vector levels; std::vector zcrexp; //more or less useful content of that file + //commanders + std::vector znpc00; //more or less useful content of that file //campaigns std::vector campaignMapNames;