123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- #include "StdInc.h"
- #include "InfoWindows.h"
- #include "../CBitmapHandler.h"
- #include "../Graphics.h"
- #include "../CGameInfo.h"
- #include "../CPlayerInterface.h"
- #include "../CMessage.h"
- #include "../CMusicHandler.h"
- #include "../windows/CAdvmapInterface.h"
- #include "../widgets/CComponent.h"
- #include "../widgets/MiscWidgets.h"
- #include "../gui/SDL_Pixels.h"
- #include "../gui/SDL_Extensions.h"
- #include "../gui/CGuiHandler.h"
- #include "../gui/CCursorHandler.h"
- #include "../battle/CBattleInterface.h"
- #include "../battle/CBattleInterfaceClasses.h"
- #include "../../CCallback.h"
- #include "../../lib/CGameState.h"
- #include "../../lib/CConfigHandler.h"
- #include "../../lib/CondSh.h"
- #include "../../lib/CGeneralTextHandler.h" //for Unicode related stuff
- #include "../../lib/mapObjects/CGHeroInstance.h"
- #include "../../lib/mapObjects/CGTownInstance.h"
- #include "../../lib/mapObjects/MiscObjects.h"
- /*
- * InfoWindows.cpp, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
- void CSimpleWindow::show(SDL_Surface * to)
- {
- if(bitmap)
- blitAt(bitmap,pos.x,pos.y,to);
- }
- CSimpleWindow::~CSimpleWindow()
- {
- if (bitmap)
- {
- SDL_FreeSurface(bitmap);
- bitmap=nullptr;
- }
- }
- void CSelWindow::selectionChange(unsigned to)
- {
- for (unsigned i=0;i<components.size();i++)
- {
- CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
- if (!pom)
- continue;
- pom->select(i==to);
- }
- redraw();
- }
- CSelWindow::CSelWindow(const std::string &Text, PlayerColor player, int charperline, const std::vector<CSelectableComponent*> &comps, const std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons, QueryID askID)
- {
- OBJ_CONSTRUCTION_CAPTURING_ALL;
- ID = askID;
- for(int i=0;i<Buttons.size();i++)
- {
- buttons.push_back(new CButton(Point(0,0), Buttons[i].first, CButton::tooltip(), Buttons[i].second));
- if(!i && askID.getNum() >= 0)
- buttons.back()->addCallback(boost::bind(&CSelWindow::madeChoice,this));
- buttons[i]->addCallback(boost::bind(&CInfoWindow::close,this)); //each button will close the window apart from call-defined actions
- }
- text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::WHITE);
- buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
- buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
- if(buttons.size() > 1 && askID.getNum() >= 0) //cancel button functionality
- buttons.back()->addCallback(boost::bind(&CCallback::selectionMade,LOCPLINT->cb.get(),0,askID));
- for(int i=0;i<comps.size();i++)
- {
- comps[i]->recActions = 255;
- addChild(comps[i]);
- components.push_back(comps[i]);
- comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
- if(i<9)
- comps[i]->assignedKeys.insert(SDLK_1+i);
- }
- CMessage::drawIWindow(this, Text, player);
- }
- void CSelWindow::madeChoice()
- {
- if(ID.getNum() < 0)
- return;
- int ret = -1;
- for (int i=0;i<components.size();i++)
- {
- if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
- {
- ret = i;
- }
- }
- LOCPLINT->cb->selectionMade(ret+1,ID);
- }
- CInfoWindow::CInfoWindow(std::string Text, PlayerColor player, const TCompsInfo &comps, const TButtonsInfo &Buttons, bool delComps)
- {
- OBJ_CONSTRUCTION_CAPTURING_ALL;
- type |= BLOCK_ADV_HOTKEYS;
- ID = QueryID(-1);
- for(auto & Button : Buttons)
- {
- CButton *button = new CButton(Point(0,0), Button.first, CButton::tooltip(), boost::bind(&CInfoWindow::close,this));
- button->borderColor = Colors::METALLIC_GOLD;
- button->addCallback(Button.second); //each button will close the window apart from call-defined actions
- buttons.push_back(button);
- }
- text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::WHITE);
- if(!text->slider)
- {
- text->resize(text->label->textSize);
- }
- if(buttons.size())
- {
- buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
- buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
- }
- for(auto & comp : comps)
- {
- comp->recActions = 0xff;
- addChild(comp);
- comp->recActions &= ~(SHOWALL | UPDATE);
- components.push_back(comp);
- }
- setDelComps(delComps);
- CMessage::drawIWindow(this,Text,player);
- }
- CInfoWindow::CInfoWindow()
- {
- ID = QueryID(-1);
- setDelComps(false);
- text = nullptr;
- }
- void CInfoWindow::close()
- {
- GH.popIntTotally(this);
- if(LOCPLINT)
- LOCPLINT->showingDialog->setn(false);
- }
- void CInfoWindow::show(SDL_Surface * to)
- {
- CIntObject::show(to);
- }
- CInfoWindow::~CInfoWindow()
- {
- if(!delComps)
- {
- for (auto & elem : components)
- removeChild(elem);
- }
- }
- void CInfoWindow::showAll(SDL_Surface * to)
- {
- CSimpleWindow::show(to);
- CIntObject::showAll(to);
- }
- void CInfoWindow::showInfoDialog(const std::string &text, const std::vector<CComponent *> *components, bool DelComps, PlayerColor player)
- {
- CInfoWindow * window = CInfoWindow::create(text, player, components, DelComps);
- GH.pushInt(window);
- }
- void CInfoWindow::showYesNoDialog(const std::string & text, const std::vector<CComponent*> *components, const CFunctionList<void( ) > &onYes, const CFunctionList<void()> &onNo, bool DelComps, PlayerColor player)
- {
- assert(!LOCPLINT || LOCPLINT->showingDialog->get());
- std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
- pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
- pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
- CInfoWindow * temp = new CInfoWindow(text, player, components ? *components : std::vector<CComponent*>(), pom, DelComps);
- for(auto & elem : onYes.funcs)
- temp->buttons[0]->addCallback(elem);
- for(auto & elem : onNo.funcs)
- temp->buttons[1]->addCallback(elem);
- GH.pushInt(temp);
- }
- void CInfoWindow::showOkDialog(const std::string & text, const std::vector<CComponent*> *components, const boost::function<void()> & onOk, bool delComps, PlayerColor player)
- {
- std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
- pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
- CInfoWindow * temp = new CInfoWindow(text, player, *components, pom, delComps);
- temp->buttons[0]->addCallback(onOk);
- GH.pushInt(temp);
- }
- CInfoWindow * CInfoWindow::create(const std::string &text, PlayerColor playerID /*= 1*/, const std::vector<CComponent*> *components /*= nullptr*/, bool DelComps)
- {
- std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
- pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
- CInfoWindow * ret = new CInfoWindow(text, playerID, components ? *components : std::vector<CComponent*>(), pom, DelComps);
- return ret;
- }
- std::string CInfoWindow::genText(std::string title, std::string description)
- {
- return std::string("{") + title + "}" + "\n\n" + description;
- }
- void CInfoWindow::setDelComps(bool DelComps)
- {
- delComps = DelComps;
- for(CComponent *comp : components)
- {
- if(delComps)
- comp->recActions |= DISPOSE;
- else
- comp->recActions &= ~DISPOSE;
- }
- }
- CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
- :free(Free),bitmap(Bitmap)
- {
- init(x, y);
- }
- CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, const Point &p, EAlignment alignment, bool Free/*=false*/)
- : free(Free),bitmap(Bitmap)
- {
- switch(alignment)
- {
- case BOTTOMRIGHT:
- init(p.x - Bitmap->w, p.y - Bitmap->h);
- break;
- case CENTER:
- init(p.x - Bitmap->w/2, p.y - Bitmap->h/2);
- break;
- case TOPLEFT:
- init(p.x, p.y);
- break;
- default:
- assert(0); //not implemented
- }
- }
- CInfoPopup::CInfoPopup(SDL_Surface *Bitmap, bool Free)
- {
- CCS->curh->hide();
- free=Free;
- bitmap=Bitmap;
- if(bitmap)
- {
- pos.x = screen->w/2 - bitmap->w/2;
- pos.y = screen->h/2 - bitmap->h/2;
- pos.h = bitmap->h;
- pos.w = bitmap->w;
- }
- }
- void CInfoPopup::close()
- {
- if(free)
- SDL_FreeSurface(bitmap);
- GH.popIntTotally(this);
- }
- void CInfoPopup::show(SDL_Surface * to)
- {
- blitAt(bitmap,pos.x,pos.y,to);
- }
- CInfoPopup::~CInfoPopup()
- {
- CCS->curh->show();
- }
- void CInfoPopup::init(int x, int y)
- {
- CCS->curh->hide();
- pos.x = x;
- pos.y = y;
- pos.h = bitmap->h;
- pos.w = bitmap->w;
- // Put the window back on screen if necessary
- vstd::amax(pos.x, 0);
- vstd::amax(pos.y, 0);
- vstd::amin(pos.x, screen->w - bitmap->w);
- vstd::amin(pos.y, screen->h - bitmap->h);
- }
- void CRClickPopup::clickRight(tribool down, bool previousState)
- {
- if(down)
- return;
- close();
- }
- void CRClickPopup::close()
- {
- GH.popIntTotally(this);
- }
- void CRClickPopup::createAndPush(const std::string &txt, const CInfoWindow::TCompsInfo &comps)
- {
- PlayerColor player = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1); //if no player, then use blue
- CSimpleWindow * temp = new CInfoWindow(txt, player, comps);
- temp->center(Point(GH.current->motion)); //center on mouse
- temp->fitToScreen(10);
- auto rcpi = new CRClickPopupInt(temp,true);
- GH.pushInt(rcpi);
- }
- void CRClickPopup::createAndPush(const std::string &txt, CComponent * component)
- {
- CInfoWindow::TCompsInfo intComps;
- intComps.push_back(component);
- createAndPush(txt, intComps);
- }
- void CRClickPopup::createAndPush(const CGObjectInstance *obj, const Point &p, EAlignment alignment /*= BOTTOMRIGHT*/)
- {
- CIntObject *iWin = createInfoWin(p, obj); //try get custom infowindow for this obj
- if(iWin)
- GH.pushInt(iWin);
- else
- {
- if (adventureInt->curHero())
- CRClickPopup::createAndPush(obj->getHoverText(adventureInt->curHero()));
- else
- CRClickPopup::createAndPush(obj->getHoverText(LOCPLINT->playerID));
- }
- }
- CRClickPopup::CRClickPopup()
- {
- addUsedEvents(RCLICK);
- }
- CRClickPopup::~CRClickPopup()
- {
- }
- void CRClickPopupInt::show(SDL_Surface * to)
- {
- inner->show(to);
- }
- CRClickPopupInt::CRClickPopupInt( IShowActivatable *our, bool deleteInt )
- {
- CCS->curh->hide();
- inner = our;
- delInner = deleteInt;
- }
- CRClickPopupInt::~CRClickPopupInt()
- {
- if(delInner)
- delete inner;
- CCS->curh->show();
- }
- void CRClickPopupInt::showAll(SDL_Surface * to)
- {
- inner->showAll(to);
- }
- Point CInfoBoxPopup::toScreen(Point p)
- {
- vstd::abetween(p.x, adventureInt->terrain.pos.x + 100, adventureInt->terrain.pos.x + adventureInt->terrain.pos.w - 100);
- vstd::abetween(p.y, adventureInt->terrain.pos.y + 100, adventureInt->terrain.pos.y + adventureInt->terrain.pos.h - 100);
- return p;
- }
- CInfoBoxPopup::CInfoBoxPopup(Point position, const CGTownInstance * town):
- CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "TOWNQVBK", toScreen(position))
- {
- InfoAboutTown iah;
- LOCPLINT->cb->getTownInfo(town, iah);
- OBJ_CONSTRUCTION_CAPTURING_ALL;
- new CTownTooltip(Point(9, 10), iah);
- }
- CInfoBoxPopup::CInfoBoxPopup(Point position, const CGHeroInstance * hero):
- CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "HEROQVBK", toScreen(position))
- {
- InfoAboutHero iah;
- LOCPLINT->cb->getHeroInfo(hero, iah);
- OBJ_CONSTRUCTION_CAPTURING_ALL;
- new CHeroTooltip(Point(9, 10), iah);
- }
- CInfoBoxPopup::CInfoBoxPopup(Point position, const CGGarrison * garr):
- CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "TOWNQVBK", toScreen(position))
- {
- InfoAboutTown iah;
- LOCPLINT->cb->getTownInfo(garr, iah);
- OBJ_CONSTRUCTION_CAPTURING_ALL;
- new CArmyTooltip(Point(9, 10), iah);
- }
- CIntObject * CRClickPopup::createInfoWin(Point position, const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
- {
- if(!specific)
- specific = adventureInt->selection;
- assert(specific);
- switch(specific->ID)
- {
- case Obj::HERO:
- return new CInfoBoxPopup(position, dynamic_cast<const CGHeroInstance *>(specific));
- case Obj::TOWN:
- return new CInfoBoxPopup(position, dynamic_cast<const CGTownInstance *>(specific));
- case Obj::GARRISON:
- case Obj::GARRISON2:
- return new CInfoBoxPopup(position, dynamic_cast<const CGGarrison *>(specific));
- default:
- return nullptr;
- }
- }
|