InfoWindows.cpp 12 KB

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