InfoWindows.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 CAdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  66. if(!i && askID.getNum() >= 0)
  67. buttons.back()->callback += boost::bind(&CSelWindow::madeChoice,this);
  68. buttons[i]->callback += 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()->callback += 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. CAdventureMapButton *button = new CAdventureMapButton("","",boost::bind(&CInfoWindow::close,this),0,0,Button.first);
  108. button->borderColor = Colors::METALLIC_GOLD;
  109. button->borderEnabled = true;
  110. button->callback.add(Button.second); //each button will close the window apart from call-defined actions
  111. buttons.push_back(button);
  112. }
  113. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::WHITE);
  114. if(!text->slider)
  115. {
  116. text->resize(text->label->textSize);
  117. }
  118. if(buttons.size())
  119. {
  120. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  121. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  122. }
  123. for(auto & comp : comps)
  124. {
  125. comp->recActions = 0xff;
  126. addChild(comp);
  127. comp->recActions &= ~(SHOWALL | UPDATE);
  128. components.push_back(comp);
  129. }
  130. setDelComps(delComps);
  131. CMessage::drawIWindow(this,Text,player);
  132. }
  133. CInfoWindow::CInfoWindow()
  134. {
  135. ID = QueryID(-1);
  136. setDelComps(false);
  137. text = nullptr;
  138. }
  139. void CInfoWindow::close()
  140. {
  141. GH.popIntTotally(this);
  142. if(LOCPLINT)
  143. LOCPLINT->showingDialog->setn(false);
  144. }
  145. void CInfoWindow::show(SDL_Surface * to)
  146. {
  147. CIntObject::show(to);
  148. }
  149. CInfoWindow::~CInfoWindow()
  150. {
  151. if(!delComps)
  152. {
  153. for (auto & elem : components)
  154. removeChild(elem);
  155. }
  156. }
  157. void CInfoWindow::showAll(SDL_Surface * to)
  158. {
  159. CSimpleWindow::show(to);
  160. CIntObject::showAll(to);
  161. }
  162. void CInfoWindow::showInfoDialog(const std::string &text, const std::vector<CComponent *> *components, bool DelComps, PlayerColor player)
  163. {
  164. CInfoWindow * window = CInfoWindow::create(text, player, components, DelComps);
  165. GH.pushInt(window);
  166. }
  167. void CInfoWindow::showYesNoDialog(const std::string & text, const std::vector<CComponent*> *components, const CFunctionList<void( ) > &onYes, const CFunctionList<void()> &onNo, bool DelComps, PlayerColor player)
  168. {
  169. assert(!LOCPLINT || LOCPLINT->showingDialog->get());
  170. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  171. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  172. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  173. CInfoWindow * temp = new CInfoWindow(text, player, components ? *components : std::vector<CComponent*>(), pom, DelComps);
  174. for(auto & elem : onYes.funcs)
  175. temp->buttons[0]->callback += elem;
  176. for(auto & elem : onNo.funcs)
  177. temp->buttons[1]->callback += elem;
  178. GH.pushInt(temp);
  179. }
  180. void CInfoWindow::showOkDialog(const std::string & text, const std::vector<CComponent*> *components, const boost::function<void()> & onOk, bool delComps, PlayerColor player)
  181. {
  182. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  183. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  184. CInfoWindow * temp = new CInfoWindow(text, player, *components, pom, delComps);
  185. temp->buttons[0]->callback += onOk;
  186. GH.pushInt(temp);
  187. }
  188. CInfoWindow * CInfoWindow::create(const std::string &text, PlayerColor playerID /*= 1*/, const std::vector<CComponent*> *components /*= nullptr*/, bool DelComps)
  189. {
  190. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  191. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  192. CInfoWindow * ret = new CInfoWindow(text, playerID, components ? *components : std::vector<CComponent*>(), pom, DelComps);
  193. return ret;
  194. }
  195. std::string CInfoWindow::genText(std::string title, std::string description)
  196. {
  197. return std::string("{") + title + "}" + "\n\n" + description;
  198. }
  199. void CInfoWindow::setDelComps(bool DelComps)
  200. {
  201. delComps = DelComps;
  202. for(CComponent *comp : components)
  203. {
  204. if(delComps)
  205. comp->recActions |= DISPOSE;
  206. else
  207. comp->recActions &= ~DISPOSE;
  208. }
  209. }
  210. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  211. :free(Free),bitmap(Bitmap)
  212. {
  213. init(x, y);
  214. }
  215. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, const Point &p, EAlignment alignment, bool Free/*=false*/)
  216. : free(Free),bitmap(Bitmap)
  217. {
  218. switch(alignment)
  219. {
  220. case BOTTOMRIGHT:
  221. init(p.x - Bitmap->w, p.y - Bitmap->h);
  222. break;
  223. case CENTER:
  224. init(p.x - Bitmap->w/2, p.y - Bitmap->h/2);
  225. break;
  226. case TOPLEFT:
  227. init(p.x, p.y);
  228. break;
  229. default:
  230. assert(0); //not implemented
  231. }
  232. }
  233. CInfoPopup::CInfoPopup(SDL_Surface *Bitmap, bool Free)
  234. {
  235. CCS->curh->hide();
  236. free=Free;
  237. bitmap=Bitmap;
  238. if(bitmap)
  239. {
  240. pos.x = screen->w/2 - bitmap->w/2;
  241. pos.y = screen->h/2 - bitmap->h/2;
  242. pos.h = bitmap->h;
  243. pos.w = bitmap->w;
  244. }
  245. }
  246. void CInfoPopup::close()
  247. {
  248. if(free)
  249. SDL_FreeSurface(bitmap);
  250. GH.popIntTotally(this);
  251. }
  252. void CInfoPopup::show(SDL_Surface * to)
  253. {
  254. blitAt(bitmap,pos.x,pos.y,to);
  255. }
  256. CInfoPopup::~CInfoPopup()
  257. {
  258. CCS->curh->show();
  259. }
  260. void CInfoPopup::init(int x, int y)
  261. {
  262. CCS->curh->hide();
  263. pos.x = x;
  264. pos.y = y;
  265. pos.h = bitmap->h;
  266. pos.w = bitmap->w;
  267. // Put the window back on screen if necessary
  268. vstd::amax(pos.x, 0);
  269. vstd::amax(pos.y, 0);
  270. vstd::amin(pos.x, screen->w - bitmap->w);
  271. vstd::amin(pos.y, screen->h - bitmap->h);
  272. }
  273. void CRClickPopup::clickRight(tribool down, bool previousState)
  274. {
  275. if(down)
  276. return;
  277. close();
  278. }
  279. void CRClickPopup::close()
  280. {
  281. GH.popIntTotally(this);
  282. }
  283. void CRClickPopup::createAndPush(const std::string &txt, const CInfoWindow::TCompsInfo &comps)
  284. {
  285. PlayerColor player = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1); //if no player, then use blue
  286. CSimpleWindow * temp = new CInfoWindow(txt, player, comps);
  287. temp->center(Point(GH.current->motion)); //center on mouse
  288. temp->fitToScreen(10);
  289. auto rcpi = new CRClickPopupInt(temp,true);
  290. GH.pushInt(rcpi);
  291. }
  292. void CRClickPopup::createAndPush(const std::string &txt, CComponent * component)
  293. {
  294. CInfoWindow::TCompsInfo intComps;
  295. intComps.push_back(component);
  296. createAndPush(txt, intComps);
  297. }
  298. void CRClickPopup::createAndPush(const CGObjectInstance *obj, const Point &p, EAlignment alignment /*= BOTTOMRIGHT*/)
  299. {
  300. CIntObject *iWin = createInfoWin(p, obj); //try get custom infowindow for this obj
  301. if(iWin)
  302. GH.pushInt(iWin);
  303. else
  304. {
  305. if (adventureInt->curHero())
  306. CRClickPopup::createAndPush(obj->getHoverText(adventureInt->curHero()));
  307. else
  308. CRClickPopup::createAndPush(obj->getHoverText(LOCPLINT->playerID));
  309. }
  310. }
  311. CRClickPopup::CRClickPopup()
  312. {
  313. addUsedEvents(RCLICK);
  314. }
  315. CRClickPopup::~CRClickPopup()
  316. {
  317. }
  318. void CRClickPopupInt::show(SDL_Surface * to)
  319. {
  320. inner->show(to);
  321. }
  322. CRClickPopupInt::CRClickPopupInt( IShowActivatable *our, bool deleteInt )
  323. {
  324. CCS->curh->hide();
  325. inner = our;
  326. delInner = deleteInt;
  327. }
  328. CRClickPopupInt::~CRClickPopupInt()
  329. {
  330. if(delInner)
  331. delete inner;
  332. CCS->curh->show();
  333. }
  334. void CRClickPopupInt::showAll(SDL_Surface * to)
  335. {
  336. inner->showAll(to);
  337. }
  338. Point CInfoBoxPopup::toScreen(Point p)
  339. {
  340. vstd::abetween(p.x, adventureInt->terrain.pos.x + 100, adventureInt->terrain.pos.x + adventureInt->terrain.pos.w - 100);
  341. vstd::abetween(p.y, adventureInt->terrain.pos.y + 100, adventureInt->terrain.pos.y + adventureInt->terrain.pos.h - 100);
  342. return p;
  343. }
  344. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGTownInstance * town):
  345. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "TOWNQVBK", toScreen(position))
  346. {
  347. InfoAboutTown iah;
  348. LOCPLINT->cb->getTownInfo(town, iah);
  349. OBJ_CONSTRUCTION_CAPTURING_ALL;
  350. new CTownTooltip(Point(9, 10), iah);
  351. }
  352. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGHeroInstance * hero):
  353. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "HEROQVBK", toScreen(position))
  354. {
  355. InfoAboutHero iah;
  356. LOCPLINT->cb->getHeroInfo(hero, iah);
  357. OBJ_CONSTRUCTION_CAPTURING_ALL;
  358. new CHeroTooltip(Point(9, 10), iah);
  359. }
  360. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGGarrison * garr):
  361. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "TOWNQVBK", toScreen(position))
  362. {
  363. InfoAboutTown iah;
  364. LOCPLINT->cb->getTownInfo(garr, iah);
  365. OBJ_CONSTRUCTION_CAPTURING_ALL;
  366. new CArmyTooltip(Point(9, 10), iah);
  367. }
  368. CIntObject * CRClickPopup::createInfoWin(Point position, const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  369. {
  370. if(!specific)
  371. specific = adventureInt->selection;
  372. assert(specific);
  373. switch(specific->ID)
  374. {
  375. case Obj::HERO:
  376. return new CInfoBoxPopup(position, dynamic_cast<const CGHeroInstance *>(specific));
  377. case Obj::TOWN:
  378. return new CInfoBoxPopup(position, dynamic_cast<const CGTownInstance *>(specific));
  379. case Obj::GARRISON:
  380. case Obj::GARRISON2:
  381. return new CInfoBoxPopup(position, dynamic_cast<const CGGarrison *>(specific));
  382. default:
  383. return nullptr;
  384. }
  385. }