1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

Finish encapsulation of PlayerLocalState class

This commit is contained in:
Ivan Savenko
2024-10-08 14:17:04 +00:00
parent 25311b3464
commit 9492eab7c5
3 changed files with 42 additions and 14 deletions

View File

@@ -23,6 +23,16 @@ PlayerLocalState::PlayerLocalState(CPlayerInterface & owner)
{ {
} }
const PlayerSpellbookSetting & PlayerLocalState::getSpellbookSettings()
{
return spellbookSettings;
}
void PlayerLocalState::setSpellbookSettings(const PlayerSpellbookSetting & newSettings)
{
spellbookSettings = newSettings;
}
void PlayerLocalState::saveHeroPaths(std::map<const CGHeroInstance *, int3> & pathsMap) void PlayerLocalState::saveHeroPaths(std::map<const CGHeroInstance *, int3> & pathsMap)
{ {
for(auto & p : paths) for(auto & p : paths)

View File

@@ -21,6 +21,15 @@ VCMI_LIB_NAMESPACE_END
class CPlayerInterface; class CPlayerInterface;
struct PlayerSpellbookSetting
{
//on which page we left spellbook
int spellbookLastPageBattle = 0;
int spellbookLastPageAdvmap = 0;
int spellbookLastTabBattle = 4;
int spellbookLastTabAdvmap = 4;
};
/// Class that contains potentially serializeable state of a local player /// Class that contains potentially serializeable state of a local player
class PlayerLocalState class PlayerLocalState
{ {
@@ -34,18 +43,12 @@ class PlayerLocalState
std::vector<const CGHeroInstance *> wanderingHeroes; //our heroes on the adventure map (not the garrisoned ones) std::vector<const CGHeroInstance *> wanderingHeroes; //our heroes on the adventure map (not the garrisoned ones)
std::vector<const CGTownInstance *> ownedTowns; //our towns on the adventure map std::vector<const CGTownInstance *> ownedTowns; //our towns on the adventure map
PlayerSpellbookSetting spellbookSettings;
void saveHeroPaths(std::map<const CGHeroInstance *, int3> & paths); void saveHeroPaths(std::map<const CGHeroInstance *, int3> & paths);
void loadHeroPaths(std::map<const CGHeroInstance *, int3> & paths); void loadHeroPaths(std::map<const CGHeroInstance *, int3> & paths);
public: public:
struct SpellbookLastSetting
{
//on which page we left spellbook
int spellbookLastPageBattle = 0;
int spellbookLastPageAdvmap = 0;
int spellbookLastTabBattle = 4;
int spellbookLastTabAdvmap = 4;
} spellbookSettings;
explicit PlayerLocalState(CPlayerInterface & owner); explicit PlayerLocalState(CPlayerInterface & owner);
@@ -53,6 +56,9 @@ public:
void setHeroAsleep(const CGHeroInstance * hero); void setHeroAsleep(const CGHeroInstance * hero);
void setHeroAwaken(const CGHeroInstance * hero); void setHeroAwaken(const CGHeroInstance * hero);
const PlayerSpellbookSetting & getSpellbookSettings();
void setSpellbookSettings(const PlayerSpellbookSetting & newSettings);
const std::vector<const CGTownInstance *> & getOwnedTowns(); const std::vector<const CGTownInstance *> & getOwnedTowns();
const CGTownInstance * getOwnedTown(size_t index); const CGTownInstance * getOwnedTown(size_t index);
void addOwnedTown(const CGTownInstance * hero); void addOwnedTown(const CGTownInstance * hero);

View File

@@ -205,9 +205,9 @@ CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _m
} }
} }
selectedTab = battleSpellsOnly ? myInt->localState->spellbookSettings.spellbookLastTabBattle : myInt->localState->spellbookSettings.spellbookLastTabAdvmap; selectedTab = battleSpellsOnly ? myInt->localState->getSpellbookSettings().spellbookLastTabBattle : myInt->localState->getSpellbookSettings().spellbookLastTabAdvmap;
schoolTab->setFrame(selectedTab, 0); schoolTab->setFrame(selectedTab, 0);
int cp = battleSpellsOnly ? myInt->localState->spellbookSettings.spellbookLastPageBattle : myInt->localState->spellbookSettings.spellbookLastPageAdvmap; int cp = battleSpellsOnly ? myInt->localState->getSpellbookSettings().spellbookLastPageBattle : myInt->localState->getSpellbookSettings().spellbookLastPageAdvmap;
// spellbook last page battle index is not reset after battle, so this needs to stay here // spellbook last page battle index is not reset after battle, so this needs to stay here
vstd::abetween(cp, 0, std::max(0, pagesWithinCurrentTab() - 1)); vstd::abetween(cp, 0, std::max(0, pagesWithinCurrentTab() - 1));
setCurrentPage(cp); setCurrentPage(cp);
@@ -313,8 +313,18 @@ void CSpellWindow::processSpells()
void CSpellWindow::fexitb() void CSpellWindow::fexitb()
{ {
(myInt->battleInt ? myInt->localState->spellbookSettings.spellbookLastTabBattle : myInt->localState->spellbookSettings.spellbookLastTabAdvmap) = selectedTab; auto spellBookState = myInt->localState->getSpellbookSettings();
(myInt->battleInt ? myInt->localState->spellbookSettings.spellbookLastPageBattle : myInt->localState->spellbookSettings.spellbookLastPageAdvmap) = currentPage; if(myInt->battleInt)
{
spellBookState.spellbookLastTabBattle = selectedTab;
spellBookState.spellbookLastPageBattle = currentPage;
}
else
{
spellBookState.spellbookLastTabAdvmap = selectedTab;
spellBookState.spellbookLastPageAdvmap = currentPage;
}
myInt->localState->setSpellbookSettings(spellBookState);
if(onSpellSelect) if(onSpellSelect)
onSpellSelect(SpellID::NONE); onSpellSelect(SpellID::NONE);
@@ -619,8 +629,10 @@ void CSpellWindow::SpellArea::clickPressed(const Point & cursorPosition)
auto guard = vstd::makeScopeGuard([this]() auto guard = vstd::makeScopeGuard([this]()
{ {
owner->myInt->localState->spellbookSettings.spellbookLastTabAdvmap = owner->selectedTab; auto spellBookState = owner->myInt->localState->getSpellbookSettings();
owner->myInt->localState->spellbookSettings.spellbookLastPageAdvmap = owner->currentPage; spellBookState.spellbookLastTabAdvmap = owner->selectedTab;
spellBookState.spellbookLastPageAdvmap = owner->currentPage;
owner->myInt->localState->setSpellbookSettings(spellBookState);
}); });
spells::detail::ProblemImpl problem; spells::detail::ProblemImpl problem;