InfoWindows.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #include "StdInc.h"
  2. #include "InfoWindows.h"
  3. #include "../CBitmapHandler.h"
  4. #include "../Graphics.h"
  5. #include "../CGameInfo.h"
  6. #include "../CPlayerInterface.h"
  7. #include "../CMessage.h"
  8. #include "../CMusicHandler.h"
  9. #include "../windows/CAdvmapInterface.h"
  10. #include "../widgets/CComponent.h"
  11. #include "../widgets/MiscWidgets.h"
  12. #include "../gui/SDL_Pixels.h"
  13. #include "../gui/SDL_Extensions.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../gui/CCursorHandler.h"
  16. #include "../battle/CBattleInterface.h"
  17. #include "../battle/CBattleInterfaceClasses.h"
  18. #include "../../CCallback.h"
  19. #include "../../lib/CGameState.h"
  20. #include "../../lib/CConfigHandler.h"
  21. #include "../../lib/CondSh.h"
  22. #include "../../lib/CGeneralTextHandler.h" //for Unicode related stuff
  23. #include "../../lib/mapObjects/CGHeroInstance.h"
  24. #include "../../lib/mapObjects/CGTownInstance.h"
  25. #include "../../lib/mapObjects/MiscObjects.h"
  26. /*
  27. * InfoWindows.cpp, part of VCMI engine
  28. *
  29. * Authors: listed in file AUTHORS in main folder
  30. *
  31. * License: GNU General Public License v2.0 or later
  32. * Full text of license available in license.txt file, in main folder
  33. *
  34. */
  35. void CSimpleWindow::show(SDL_Surface * to)
  36. {
  37. if(bitmap)
  38. blitAt(bitmap,pos.x,pos.y,to);
  39. }
  40. CSimpleWindow::~CSimpleWindow()
  41. {
  42. if (bitmap)
  43. {
  44. SDL_FreeSurface(bitmap);
  45. bitmap=nullptr;
  46. }
  47. }
  48. void CSelWindow::selectionChange(unsigned to)
  49. {
  50. for (unsigned i=0;i<components.size();i++)
  51. {
  52. CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
  53. if (!pom)
  54. continue;
  55. pom->select(i==to);
  56. }
  57. redraw();
  58. }
  59. 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)
  60. {
  61. OBJ_CONSTRUCTION_CAPTURING_ALL;
  62. ID = askID;
  63. for(int i=0;i<Buttons.size();i++)
  64. {
  65. buttons.push_back(new CButton(Point(0,0), Buttons[i].first, CButton::tooltip(), Buttons[i].second));
  66. if(!i && askID.getNum() >= 0)
  67. buttons.back()->addCallback(boost::bind(&CSelWindow::madeChoice,this));
  68. buttons[i]->addCallback(boost::bind(&CInfoWindow::close,this)); //each button will close the window apart from call-defined actions
  69. }
  70. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::WHITE);
  71. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  72. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  73. if(buttons.size() > 1 && askID.getNum() >= 0) //cancel button functionality
  74. buttons.back()->addCallback(boost::bind(&CCallback::selectionMade,LOCPLINT->cb.get(),0,askID));
  75. for(int i=0;i<comps.size();i++)
  76. {
  77. comps[i]->recActions = 255;
  78. addChild(comps[i]);
  79. components.push_back(comps[i]);
  80. comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
  81. if(i<9)
  82. comps[i]->assignedKeys.insert(SDLK_1+i);
  83. }
  84. CMessage::drawIWindow(this, Text, player);
  85. }
  86. void CSelWindow::madeChoice()
  87. {
  88. if(ID.getNum() < 0)
  89. return;
  90. int ret = -1;
  91. for (int i=0;i<components.size();i++)
  92. {
  93. if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
  94. {
  95. ret = i;
  96. }
  97. }
  98. LOCPLINT->cb->selectionMade(ret+1,ID);
  99. }
  100. CInfoWindow::CInfoWindow(std::string Text, PlayerColor player, const TCompsInfo &comps, const TButtonsInfo &Buttons, bool delComps)
  101. {
  102. OBJ_CONSTRUCTION_CAPTURING_ALL;
  103. type |= BLOCK_ADV_HOTKEYS;
  104. ID = QueryID(-1);
  105. for(auto & Button : Buttons)
  106. {
  107. CButton *button = new CButton(Point(0,0), Button.first, CButton::tooltip(), boost::bind(&CInfoWindow::close,this));
  108. button->borderColor = Colors::METALLIC_GOLD;
  109. button->addCallback(Button.second); //each button will close the window apart from call-defined actions
  110. buttons.push_back(button);
  111. }
  112. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::WHITE);
  113. if(!text->slider)
  114. {
  115. text->resize(text->label->textSize);
  116. }
  117. if(buttons.size())
  118. {
  119. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  120. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  121. }
  122. for(auto & comp : comps)
  123. {
  124. comp->recActions = 0xff;
  125. addChild(comp);
  126. comp->recActions &= ~(SHOWALL | UPDATE);
  127. components.push_back(comp);
  128. }
  129. setDelComps(delComps);
  130. CMessage::drawIWindow(this,Text,player);
  131. }
  132. CInfoWindow::CInfoWindow()
  133. {
  134. ID = QueryID(-1);
  135. setDelComps(false);
  136. text = nullptr;
  137. }
  138. void CInfoWindow::close()
  139. {
  140. GH.popIntTotally(this);
  141. if(LOCPLINT)
  142. LOCPLINT->showingDialog->setn(false);
  143. }
  144. void CInfoWindow::show(SDL_Surface * to)
  145. {
  146. CIntObject::show(to);
  147. }
  148. CInfoWindow::~CInfoWindow()
  149. {
  150. if(!delComps)
  151. {
  152. for (auto & elem : components)
  153. removeChild(elem);
  154. }
  155. }
  156. void CInfoWindow::showAll(SDL_Surface * to)
  157. {
  158. CSimpleWindow::show(to);
  159. CIntObject::showAll(to);
  160. }
  161. void CInfoWindow::showInfoDialog(const std::string &text, const std::vector<CComponent *> *components, bool DelComps, PlayerColor player)
  162. {
  163. CInfoWindow * window = CInfoWindow::create(text, player, components, DelComps);
  164. GH.pushInt(window);
  165. }
  166. void CInfoWindow::showYesNoDialog(const std::string & text, const std::vector<CComponent*> *components, const CFunctionList<void( ) > &onYes, const CFunctionList<void()> &onNo, bool DelComps, PlayerColor player)
  167. {
  168. assert(!LOCPLINT || LOCPLINT->showingDialog->get());
  169. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  170. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  171. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  172. CInfoWindow * temp = new CInfoWindow(text, player, components ? *components : std::vector<CComponent*>(), pom, DelComps);
  173. for(auto & elem : onYes.funcs)
  174. temp->buttons[0]->addCallback(elem);
  175. for(auto & elem : onNo.funcs)
  176. temp->buttons[1]->addCallback(elem);
  177. GH.pushInt(temp);
  178. }
  179. void CInfoWindow::showOkDialog(const std::string & text, const std::vector<CComponent*> *components, const boost::function<void()> & onOk, bool delComps, PlayerColor player)
  180. {
  181. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  182. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  183. CInfoWindow * temp = new CInfoWindow(text, player, *components, pom, delComps);
  184. temp->buttons[0]->addCallback(onOk);
  185. GH.pushInt(temp);
  186. }
  187. CInfoWindow * CInfoWindow::create(const std::string &text, PlayerColor playerID /*= 1*/, const std::vector<CComponent*> *components /*= nullptr*/, bool DelComps)
  188. {
  189. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  190. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  191. CInfoWindow * ret = new CInfoWindow(text, playerID, components ? *components : std::vector<CComponent*>(), pom, DelComps);
  192. return ret;
  193. }
  194. std::string CInfoWindow::genText(std::string title, std::string description)
  195. {
  196. return std::string("{") + title + "}" + "\n\n" + description;
  197. }
  198. void CInfoWindow::setDelComps(bool DelComps)
  199. {
  200. delComps = DelComps;
  201. for(CComponent *comp : components)
  202. {
  203. if(delComps)
  204. comp->recActions |= DISPOSE;
  205. else
  206. comp->recActions &= ~DISPOSE;
  207. }
  208. }
  209. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  210. :free(Free),bitmap(Bitmap)
  211. {
  212. init(x, y);
  213. }
  214. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, const Point &p, EAlignment alignment, bool Free/*=false*/)
  215. : free(Free),bitmap(Bitmap)
  216. {
  217. switch(alignment)
  218. {
  219. case BOTTOMRIGHT:
  220. init(p.x - Bitmap->w, p.y - Bitmap->h);
  221. break;
  222. case CENTER:
  223. init(p.x - Bitmap->w/2, p.y - Bitmap->h/2);
  224. break;
  225. case TOPLEFT:
  226. init(p.x, p.y);
  227. break;
  228. default:
  229. assert(0); //not implemented
  230. }
  231. }
  232. CInfoPopup::CInfoPopup(SDL_Surface *Bitmap, bool Free)
  233. {
  234. CCS->curh->hide();
  235. free=Free;
  236. bitmap=Bitmap;
  237. if(bitmap)
  238. {
  239. pos.x = screen->w/2 - bitmap->w/2;
  240. pos.y = screen->h/2 - bitmap->h/2;
  241. pos.h = bitmap->h;
  242. pos.w = bitmap->w;
  243. }
  244. }
  245. void CInfoPopup::close()
  246. {
  247. if(free)
  248. SDL_FreeSurface(bitmap);
  249. GH.popIntTotally(this);
  250. }
  251. void CInfoPopup::show(SDL_Surface * to)
  252. {
  253. blitAt(bitmap,pos.x,pos.y,to);
  254. }
  255. CInfoPopup::~CInfoPopup()
  256. {
  257. CCS->curh->show();
  258. }
  259. void CInfoPopup::init(int x, int y)
  260. {
  261. CCS->curh->hide();
  262. pos.x = x;
  263. pos.y = y;
  264. pos.h = bitmap->h;
  265. pos.w = bitmap->w;
  266. // Put the window back on screen if necessary
  267. vstd::amax(pos.x, 0);
  268. vstd::amax(pos.y, 0);
  269. vstd::amin(pos.x, screen->w - bitmap->w);
  270. vstd::amin(pos.y, screen->h - bitmap->h);
  271. }
  272. void CRClickPopup::clickRight(tribool down, bool previousState)
  273. {
  274. if(down)
  275. return;
  276. close();
  277. }
  278. void CRClickPopup::close()
  279. {
  280. GH.popIntTotally(this);
  281. }
  282. void CRClickPopup::createAndPush(const std::string &txt, const CInfoWindow::TCompsInfo &comps)
  283. {
  284. PlayerColor player = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1); //if no player, then use blue
  285. CSimpleWindow * temp = new CInfoWindow(txt, player, comps);
  286. temp->center(Point(GH.current->motion)); //center on mouse
  287. temp->fitToScreen(10);
  288. auto rcpi = new CRClickPopupInt(temp,true);
  289. GH.pushInt(rcpi);
  290. }
  291. void CRClickPopup::createAndPush(const std::string &txt, CComponent * component)
  292. {
  293. CInfoWindow::TCompsInfo intComps;
  294. intComps.push_back(component);
  295. createAndPush(txt, intComps);
  296. }
  297. void CRClickPopup::createAndPush(const CGObjectInstance *obj, const Point &p, EAlignment alignment /*= BOTTOMRIGHT*/)
  298. {
  299. CIntObject *iWin = createInfoWin(p, obj); //try get custom infowindow for this obj
  300. if(iWin)
  301. GH.pushInt(iWin);
  302. else
  303. {
  304. if (adventureInt->curHero())
  305. CRClickPopup::createAndPush(obj->getHoverText(adventureInt->curHero()));
  306. else
  307. CRClickPopup::createAndPush(obj->getHoverText(LOCPLINT->playerID));
  308. }
  309. }
  310. CRClickPopup::CRClickPopup()
  311. {
  312. addUsedEvents(RCLICK);
  313. }
  314. CRClickPopup::~CRClickPopup()
  315. {
  316. }
  317. void CRClickPopupInt::show(SDL_Surface * to)
  318. {
  319. inner->show(to);
  320. }
  321. CRClickPopupInt::CRClickPopupInt( IShowActivatable *our, bool deleteInt )
  322. {
  323. CCS->curh->hide();
  324. inner = our;
  325. delInner = deleteInt;
  326. }
  327. CRClickPopupInt::~CRClickPopupInt()
  328. {
  329. if(delInner)
  330. delete inner;
  331. CCS->curh->show();
  332. }
  333. void CRClickPopupInt::showAll(SDL_Surface * to)
  334. {
  335. inner->showAll(to);
  336. }
  337. Point CInfoBoxPopup::toScreen(Point p)
  338. {
  339. vstd::abetween(p.x, adventureInt->terrain.pos.x + 100, adventureInt->terrain.pos.x + adventureInt->terrain.pos.w - 100);
  340. vstd::abetween(p.y, adventureInt->terrain.pos.y + 100, adventureInt->terrain.pos.y + adventureInt->terrain.pos.h - 100);
  341. return p;
  342. }
  343. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGTownInstance * town):
  344. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "TOWNQVBK", toScreen(position))
  345. {
  346. InfoAboutTown iah;
  347. LOCPLINT->cb->getTownInfo(town, iah);
  348. OBJ_CONSTRUCTION_CAPTURING_ALL;
  349. new CTownTooltip(Point(9, 10), iah);
  350. }
  351. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGHeroInstance * hero):
  352. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "HEROQVBK", toScreen(position))
  353. {
  354. InfoAboutHero iah;
  355. LOCPLINT->cb->getHeroInfo(hero, iah);
  356. OBJ_CONSTRUCTION_CAPTURING_ALL;
  357. new CHeroTooltip(Point(9, 10), iah);
  358. }
  359. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGGarrison * garr):
  360. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "TOWNQVBK", toScreen(position))
  361. {
  362. InfoAboutTown iah;
  363. LOCPLINT->cb->getTownInfo(garr, iah);
  364. OBJ_CONSTRUCTION_CAPTURING_ALL;
  365. new CArmyTooltip(Point(9, 10), iah);
  366. }
  367. CIntObject * CRClickPopup::createInfoWin(Point position, const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  368. {
  369. if(!specific)
  370. specific = adventureInt->selection;
  371. assert(specific);
  372. switch(specific->ID)
  373. {
  374. case Obj::HERO:
  375. return new CInfoBoxPopup(position, dynamic_cast<const CGHeroInstance *>(specific));
  376. case Obj::TOWN:
  377. return new CInfoBoxPopup(position, dynamic_cast<const CGTownInstance *>(specific));
  378. case Obj::GARRISON:
  379. case Obj::GARRISON2:
  380. return new CInfoBoxPopup(position, dynamic_cast<const CGGarrison *>(specific));
  381. default:
  382. return nullptr;
  383. }
  384. }