2
0

InfoWindows.cpp 11 KB

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