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