InfoWindows.cpp 12 KB

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