1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-26 22:57:00 +02:00

get rid of CDefHandler in CSpellWindow

This commit is contained in:
AlexVinS 2016-10-16 15:27:35 +03:00
parent 2a6bab119b
commit ba5b65ff93
4 changed files with 60 additions and 60 deletions

View File

@ -78,6 +78,7 @@ CPicture::CPicture(const Rect &r, ui32 color, bool screenFormat /*= false*/)
CPicture::CPicture(SDL_Surface *BG, const Rect &SrcRect, int x /*= 0*/, int y /*= 0*/, bool free /*= false*/) CPicture::CPicture(SDL_Surface *BG, const Rect &SrcRect, int x /*= 0*/, int y /*= 0*/, bool free /*= false*/)
{ {
visible = true;
needRefresh = false; needRefresh = false;
srcRect = new Rect(SrcRect); srcRect = new Rect(SrcRect);
pos.x += x; pos.x += x;
@ -112,19 +113,20 @@ CPicture::~CPicture()
void CPicture::init() void CPicture::init()
{ {
visible = true;
needRefresh = false; needRefresh = false;
srcRect = nullptr; srcRect = nullptr;
} }
void CPicture::show(SDL_Surface * to) void CPicture::show(SDL_Surface * to)
{ {
if (needRefresh) if (visible && needRefresh)
showAll(to); showAll(to);
} }
void CPicture::showAll(SDL_Surface * to) void CPicture::showAll(SDL_Surface * to)
{ {
if(bg) if(bg && visible)
{ {
if(srcRect) if(srcRect)
{ {

View File

@ -28,7 +28,7 @@ public:
Rect * srcRect; //if nullptr then whole surface will be used Rect * srcRect; //if nullptr then whole surface will be used
bool freeSurf; //whether surface will be freed upon CPicture destruction bool freeSurf; //whether surface will be freed upon CPicture destruction
bool needRefresh;//Surface needs to be displayed each frame bool needRefresh;//Surface needs to be displayed each frame
bool visible;
operator SDL_Surface*() operator SDL_Surface*()
{ {
return bg; return bg;

View File

@ -8,8 +8,6 @@
#include "InfoWindows.h" #include "InfoWindows.h"
#include "CCastleInterface.h" #include "CCastleInterface.h"
#include "../CBitmapHandler.h"
#include "../CDefHandler.h"
#include "../CGameInfo.h" #include "../CGameInfo.h"
#include "../CMessage.h" #include "../CMessage.h"
#include "../CMT.h" #include "../CMT.h"
@ -171,17 +169,21 @@ CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _m
//numbers of spell pages computed //numbers of spell pages computed
leftCorner = BitmapHandler::loadBitmap("SpelTrnL.bmp", true); leftCorner = new CPicture("SpelTrnL.bmp", 97, 77);
rightCorner = BitmapHandler::loadBitmap("SpelTrnR.bmp", true); rightCorner = new CPicture("SpelTrnR.bmp", 487, 72);
spells = std::make_shared<CAnimation>("Spells"); spells = std::make_shared<CAnimation>("Spells");
spellTab = CDefHandler::giveDef("SpelTab.def"); spellTab = new CAnimImage("SpelTab", selectedTab, 0, 524, 88);
schools = CDefHandler::giveDef("Schools.def"); schools = new CAnimImage("Schools",0,0,117,74);
schoolBorders[0] = CDefHandler::giveDef("SplevA.def");
schoolBorders[1] = CDefHandler::giveDef("SplevF.def"); schoolBorders[0] = std::make_shared<CAnimation>("SplevA.def");
schoolBorders[2] = CDefHandler::giveDef("SplevW.def"); schoolBorders[1] = std::make_shared<CAnimation>("SplevF.def");
schoolBorders[3] = CDefHandler::giveDef("SplevE.def"); schoolBorders[2] = std::make_shared<CAnimation>("SplevW.def");
schoolBorders[3] = std::make_shared<CAnimation>("SplevE.def");
for(auto item : schoolBorders)
item->load();
statusBar = new CGStatusBar(7, 569, "Spelroll.bmp"); statusBar = new CGStatusBar(7, 569, "Spelroll.bmp");
@ -205,10 +207,10 @@ CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _m
temp_rect = genRect(36, 56, 549 + pos.x, 330 + pos.y); temp_rect = genRect(36, 56, 549 + pos.x, 330 + pos.y);
new InteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 4), 458, this); new InteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 4), 458, this);
lCorner = genRect(leftCorner->h, leftCorner->w, 97 + pos.x, 77 + pos.y); temp_rect = genRect(leftCorner->bg->h, leftCorner->bg->w, 97 + pos.x, 77 + pos.y);
new InteractiveArea(lCorner, std::bind(&CSpellWindow::fLcornerb, this), 450, this); new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fLcornerb, this), 450, this);
rCorner = genRect(rightCorner->h, rightCorner->w, 487 + pos.x, 72 + pos.y); temp_rect = genRect(rightCorner->bg->h, rightCorner->bg->w, 487 + pos.x, 72 + pos.y);
new InteractiveArea(rCorner, std::bind(&CSpellWindow::fRcornerb, this), 451, this); new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fRcornerb, this), 451, this);
//areas for spells //areas for spells
int xpos = 117 + pos.x, ypos = 90 + pos.y; int xpos = 117 + pos.x, ypos = 90 + pos.y;
@ -236,24 +238,19 @@ CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _m
} }
selectedTab = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap; selectedTab = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap;
currentPage = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap; int cp = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap;
// 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(currentPage, 0, std::max(0, pagesWithinCurrentTab() - 1)); vstd::abetween(cp, 0, std::max(0, pagesWithinCurrentTab() - 1));
setCurrentPage(cp);
computeSpellsPerArea(); computeSpellsPerArea();
addUsedEvents(KEYBOARD); addUsedEvents(KEYBOARD);
} }
CSpellWindow::~CSpellWindow() CSpellWindow::~CSpellWindow()
{ {
for(auto item : schoolBorders)
item->unload();
spells->unload(); spells->unload();
SDL_FreeSurface(leftCorner);
SDL_FreeSurface(rightCorner);
delete spellTab;
delete schools;
for(auto & elem : schoolBorders)
delete elem;
} }
void CSpellWindow::fexitb() void CSpellWindow::fexitb()
@ -270,7 +267,7 @@ void CSpellWindow::fadvSpellsb()
{ {
turnPageRight(); turnPageRight();
battleSpellsOnly = false; battleSpellsOnly = false;
currentPage = 0; setCurrentPage(0);
} }
computeSpellsPerArea(); computeSpellsPerArea();
} }
@ -281,7 +278,7 @@ void CSpellWindow::fbattleSpellsb()
{ {
turnPageLeft(); turnPageLeft();
battleSpellsOnly = true; battleSpellsOnly = true;
currentPage = 0; setCurrentPage(0);
} }
computeSpellsPerArea(); computeSpellsPerArea();
} }
@ -299,7 +296,8 @@ void CSpellWindow::selectSchool(int school)
else else
turnPageRight(); turnPageRight();
selectedTab = school; selectedTab = school;
currentPage = 0; spellTab->setFrame(selectedTab, 0);
setCurrentPage(0);
} }
computeSpellsPerArea(); computeSpellsPerArea();
} }
@ -309,7 +307,7 @@ void CSpellWindow::fLcornerb()
if(currentPage>0) if(currentPage>0)
{ {
turnPageLeft(); turnPageLeft();
--currentPage; setCurrentPage(currentPage - 1);
} }
computeSpellsPerArea(); computeSpellsPerArea();
GH.breakEventHandling(); GH.breakEventHandling();
@ -320,7 +318,7 @@ void CSpellWindow::fRcornerb()
if((currentPage + 1) < (pagesWithinCurrentTab())) if((currentPage + 1) < (pagesWithinCurrentTab()))
{ {
turnPageRight(); turnPageRight();
++currentPage; setCurrentPage(currentPage + 1);
} }
computeSpellsPerArea(); computeSpellsPerArea();
GH.breakEventHandling(); GH.breakEventHandling();
@ -329,25 +327,8 @@ void CSpellWindow::fRcornerb()
void CSpellWindow::showAll(SDL_Surface * to) void CSpellWindow::showAll(SDL_Surface * to)
{ {
CWindowObject::showAll(to); CWindowObject::showAll(to);
blitAt(spellTab->ourImages[selectedTab].bitmap, 524 + pos.x, 88 + pos.y, to);
printAtMiddleLoc(boost::lexical_cast<std::string>(myHero->mana), 435, 426, FONT_SMALL, Colors::YELLOW, to); printAtMiddleLoc(boost::lexical_cast<std::string>(myHero->mana), 435, 426, FONT_SMALL, Colors::YELLOW, to);
//printing school images
if(selectedTab!=4 && currentPage == 0)
{
blitAt(schools->ourImages[selectedTab].bitmap, 117 + pos.x, 74 + pos.y, to);
}
//printing corners
if(currentPage!=0)
{
blitAt(leftCorner, lCorner.x, lCorner.y, to);
}
if((currentPage+1) < (pagesWithinCurrentTab()) )
{
blitAt(rightCorner, rCorner.x, rCorner.y, to);
}
} }
void CSpellWindow::show(SDL_Surface * to) void CSpellWindow::show(SDL_Surface * to)
@ -428,6 +409,16 @@ void CSpellWindow::computeSpellsPerArea()
redraw(); redraw();
} }
void CSpellWindow::setCurrentPage(int value)
{
currentPage = value;
schools->visible = selectedTab!=4 && currentPage == 0;
if(selectedTab != 4)
schools->setFrame(selectedTab, 0);
leftCorner->visible = currentPage != 0;
rightCorner->visible = (currentPage+1) < pagesWithinCurrentTab();
}
void CSpellWindow::turnPageLeft() void CSpellWindow::turnPageLeft()
{ {
if (settings["video"]["spellbookAnimation"].Bool()) if (settings["video"]["spellbookAnimation"].Bool())
@ -519,6 +510,7 @@ CSpellWindow::SpellArea::SpellArea(SDL_Rect pos, CSpellWindow * owner)
spellCost = whichSchool = schoolLevel = -1; spellCost = whichSchool = schoolLevel = -1;
mySpell = nullptr; mySpell = nullptr;
schoolBorder = nullptr;
OBJ_CONSTRUCTION_CAPTURING_ALL; OBJ_CONSTRUCTION_CAPTURING_ALL;
image = new CAnimImage(owner->spells, 0, 0); image = new CAnimImage(owner->spells, 0, 0);
@ -527,7 +519,8 @@ CSpellWindow::SpellArea::SpellArea(SDL_Rect pos, CSpellWindow * owner)
CSpellWindow::SpellArea::~SpellArea() CSpellWindow::SpellArea::~SpellArea()
{ {
if(schoolBorder)
schoolBorder->decreaseRef();
} }
void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState) void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState)
@ -787,9 +780,11 @@ void CSpellWindow::SpellArea::showAll(SDL_Surface * to)
{ {
if(!mySpell) if(!mySpell)
return; return;
schoolBorder->draw(to, pos.x, pos.y);
CIntObject::showAll(to); CIntObject::showAll(to);
blitAt(owner->schoolBorders[owner->selectedTab >= 4 ? whichSchool : owner->selectedTab]->ourImages[schoolLevel].bitmap, pos.x, pos.y, to); //printing border (indicates level of magic school) // blitAt(owner->schoolBorders[owner->selectedTab >= 4 ? whichSchool : owner->selectedTab]->ourImages[schoolLevel].bitmap, pos.x, pos.y, to); //printing border (indicates level of magic school)
SDL_Color firstLineColor, secondLineColor; SDL_Color firstLineColor, secondLineColor;
if(spellCost > owner->myHero->mana) //hero cannot cast this spell if(spellCost > owner->myHero->mana) //hero cannot cast this spell
@ -823,6 +818,8 @@ void CSpellWindow::SpellArea::showAll(SDL_Surface * to)
void CSpellWindow::SpellArea::setSpell(const CSpell * spell) void CSpellWindow::SpellArea::setSpell(const CSpell * spell)
{ {
if(schoolBorder)
schoolBorder->decreaseRef();
image->visible = false; image->visible = false;
mySpell = spell; mySpell = spell;
if(mySpell) if(mySpell)
@ -832,5 +829,6 @@ void CSpellWindow::SpellArea::setSpell(const CSpell * spell)
image->setFrame(mySpell->id); image->setFrame(mySpell->id);
image->visible = true; image->visible = true;
schoolBorder = owner->schoolBorders[whichSchool]->getImage(schoolLevel,0);
} }
} }

View File

@ -13,9 +13,10 @@
*/ */
struct SDL_Surface; struct SDL_Surface;
class CDefHandler;
struct SDL_Rect; struct SDL_Rect;
class IImage;
class CAnimImage; class CAnimImage;
class CPicture;
class CGHeroInstance; class CGHeroInstance;
class CGStatusBar; class CGStatusBar;
class CPlayerInterface; class CPlayerInterface;
@ -35,6 +36,7 @@ private:
int spellCost; int spellCost;
CSpellWindow * owner; CSpellWindow * owner;
CAnimImage * image; CAnimImage * image;
IImage * schoolBorder;
SpellArea(SDL_Rect pos, CSpellWindow * owner); SpellArea(SDL_Rect pos, CSpellWindow * owner);
~SpellArea(); ~SpellArea();
@ -62,15 +64,13 @@ private:
InteractiveArea(const SDL_Rect & myRect, std::function<void()> funcL, int helpTextId, CSpellWindow * _owner);//c-tor InteractiveArea(const SDL_Rect & myRect, std::function<void()> funcL, int helpTextId, CSpellWindow * _owner);//c-tor
}; };
SDL_Surface * leftCorner, * rightCorner; CPicture * leftCorner, * rightCorner;
SDL_Rect lCorner, rCorner;
std::shared_ptr<CAnimation> spells; //pictures of spells std::shared_ptr<CAnimation> spells; //pictures of spells
CDefHandler * spellTab, //school select CAnimImage * spellTab; //school select
* schools, //schools' pictures CAnimImage * schools; //schools' pictures
* schoolBorders [4]; //schools' 'borders': [0]: air, [1]: fire, [2]: water, [3]: earth std::array< std::shared_ptr<CAnimation>, 4> schoolBorders; //schools' 'borders': [0]: air, [1]: fire, [2]: water, [3]: earth
SpellArea * spellAreas[12]; SpellArea * spellAreas[12];
CGStatusBar * statusBar; CGStatusBar * statusBar;
@ -84,14 +84,14 @@ private:
std::vector<const CSpell *> mySpells; //all spels in this spellbook std::vector<const CSpell *> mySpells; //all spels in this spellbook
const CGHeroInstance * myHero; //hero whose spells are presented const CGHeroInstance * myHero; //hero whose spells are presented
CPlayerInterface * myInt;
void computeSpellsPerArea(); //recalculates spellAreas::mySpell void computeSpellsPerArea(); //recalculates spellAreas::mySpell
void setCurrentPage(int value);
void turnPageLeft(); void turnPageLeft();
void turnPageRight(); void turnPageRight();
CPlayerInterface * myInt;
public: public:
CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _myInt, bool openOnBattleSpells = true); //c-tor CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _myInt, bool openOnBattleSpells = true); //c-tor