InfoWindows.cpp 12 KB

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