InfoWindows.cpp 12 KB

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