InfoWindows.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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 "../CPlayerInterface.h"
  14. #include "../PlayerLocalState.h"
  15. #include "../adventureMap/AdventureMapInterface.h"
  16. #include "../adventureMap/CMinimap.h"
  17. #include "../gui/CGuiHandler.h"
  18. #include "../gui/CursorHandler.h"
  19. #include "../gui/Shortcut.h"
  20. #include "../gui/WindowHandler.h"
  21. #include "../widgets/Buttons.h"
  22. #include "../widgets/CComponent.h"
  23. #include "../widgets/GraphicalPrimitiveCanvas.h"
  24. #include "../widgets/Images.h"
  25. #include "../widgets/MiscWidgets.h"
  26. #include "../widgets/TextControls.h"
  27. #include "../windows/CMessage.h"
  28. #include "../../CCallback.h"
  29. #include "../../lib/CConfigHandler.h"
  30. #include "../../lib/gameState/InfoAboutArmy.h"
  31. #include "../../lib/mapObjects/CGCreature.h"
  32. #include "../../lib/mapObjects/CGHeroInstance.h"
  33. #include "../../lib/mapObjects/CGTownInstance.h"
  34. #include "../../lib/mapObjects/CQuest.h"
  35. #include "../../lib/mapObjects/MiscObjects.h"
  36. #include "../ConditionalWait.h"
  37. 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)
  38. {
  39. OBJECT_CONSTRUCTION;
  40. backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DiBoxBck"), pos);
  41. ID = askID;
  42. for(int i = 0; i < Buttons.size(); i++)
  43. {
  44. buttons.push_back(std::make_shared<CButton>(Point(0, 0), Buttons[i].first, CButton::tooltip(), Buttons[i].second));
  45. if(!i && askID.getNum() >= 0)
  46. buttons.back()->addCallback(std::bind(&CSelWindow::madeChoice, this));
  47. buttons[i]->addCallback(std::bind(&CInfoWindow::close, this)); //each button will close the window apart from call-defined actions
  48. }
  49. text = std::make_shared<CTextBox>(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  50. if(buttons.size() > 1 && askID.getNum() >= 0) //cancel button functionality
  51. buttons.back()->addCallback([askID](){LOCPLINT->cb->selectionMade(0, askID);});
  52. if(buttons.size() == 1)
  53. buttons.front()->assignedKey = EShortcut::GLOBAL_RETURN;
  54. if(buttons.size() == 2)
  55. {
  56. buttons.front()->assignedKey = EShortcut::GLOBAL_ACCEPT;
  57. buttons.back()->assignedKey = EShortcut::GLOBAL_CANCEL;
  58. }
  59. if(!comps.empty())
  60. {
  61. components = std::make_shared<CComponentBox>(comps, Rect(0,0,0,0));
  62. for (auto & comp : comps)
  63. comp->onChoose = [this](){ madeChoiceAndClose(); };
  64. }
  65. CMessage::drawIWindow(this, Text, player);
  66. }
  67. void CSelWindow::madeChoice()
  68. {
  69. if(ID.getNum() < 0)
  70. return;
  71. int ret = -1;
  72. if(components)
  73. ret = components->selectedIndex();
  74. LOCPLINT->cb->selectionMade(ret + 1, ID);
  75. }
  76. void CSelWindow::madeChoiceAndClose()
  77. {
  78. madeChoice();
  79. close();
  80. }
  81. CInfoWindow::CInfoWindow(const std::string & Text, PlayerColor player, const TCompsInfo & comps, const TButtonsInfo & Buttons)
  82. {
  83. OBJECT_CONSTRUCTION;
  84. backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DiBoxBck"), pos);
  85. ID = QueryID(-1);
  86. for(const auto & Button : Buttons)
  87. {
  88. auto button = std::make_shared<CButton>(Point(0, 0), Button.first, CButton::tooltip(), std::bind(&CInfoWindow::close, this));
  89. button->setBorderColor(Colors::METALLIC_GOLD);
  90. button->addCallback(Button.second); //each button will close the window apart from call-defined actions
  91. buttons.push_back(button);
  92. }
  93. text = std::make_shared<CTextBox>(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  94. if(!text->slider)
  95. {
  96. int finalWidth = std::min(250, text->label->textSize.x + 32);
  97. int finalHeight = text->label->textSize.y;
  98. text->resize(Point(finalWidth, finalHeight));
  99. }
  100. if(buttons.size() == 1)
  101. buttons.front()->assignedKey = EShortcut::GLOBAL_RETURN;
  102. if(buttons.size() == 2)
  103. {
  104. buttons.front()->assignedKey = EShortcut::GLOBAL_ACCEPT;
  105. buttons.back()->assignedKey = EShortcut::GLOBAL_CANCEL;
  106. }
  107. if(!comps.empty())
  108. components = std::make_shared<CComponentBox>(comps, Rect(0,0,0,0));
  109. CMessage::drawIWindow(this, Text, player);
  110. }
  111. CInfoWindow::CInfoWindow()
  112. {
  113. ID = QueryID(-1);
  114. }
  115. void CInfoWindow::close()
  116. {
  117. WindowBase::close();
  118. if(LOCPLINT)
  119. LOCPLINT->showingDialog->setFree();
  120. }
  121. void CInfoWindow::showAll(Canvas & to)
  122. {
  123. CIntObject::showAll(to);
  124. CMessage::drawBorder(LOCPLINT ? LOCPLINT->playerID : PlayerColor(1), to, pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  125. }
  126. CInfoWindow::~CInfoWindow() = default;
  127. void CInfoWindow::showInfoDialog(const std::string & text, const TCompsInfo & components, PlayerColor player)
  128. {
  129. GH.windows().pushWindow(CInfoWindow::create(text, player, components));
  130. }
  131. void CInfoWindow::showYesNoDialog(const std::string & text, const TCompsInfo & components, const CFunctionList<void()> & onYes, const CFunctionList<void()> & onNo, PlayerColor player)
  132. {
  133. assert(!LOCPLINT || LOCPLINT->showingDialog->isBusy());
  134. std::vector<std::pair<AnimationPath, CFunctionList<void()>>> pom;
  135. pom.emplace_back(AnimationPath::builtin("IOKAY.DEF"), nullptr);
  136. pom.emplace_back(AnimationPath::builtin("ICANCEL.DEF"), nullptr);
  137. auto temp = std::make_shared<CInfoWindow>(text, player, components, pom);
  138. temp->buttons[0]->addCallback(onYes);
  139. temp->buttons[1]->addCallback(onNo);
  140. GH.windows().pushWindow(temp);
  141. }
  142. std::shared_ptr<CInfoWindow> CInfoWindow::create(const std::string & text, PlayerColor playerID, const TCompsInfo & components)
  143. {
  144. std::vector<std::pair<AnimationPath, CFunctionList<void()>>> pom;
  145. pom.emplace_back(AnimationPath::builtin("IOKAY.DEF"), nullptr);
  146. return std::make_shared<CInfoWindow>(text, playerID, components, pom);
  147. }
  148. std::string CInfoWindow::genText(const std::string & title, const std::string & description)
  149. {
  150. return std::string("{") + title + "}" + "\n\n" + description;
  151. }
  152. bool CRClickPopup::isPopupWindow() const
  153. {
  154. return true;
  155. }
  156. void CRClickPopup::createAndPush(const std::string & txt, const CInfoWindow::TCompsInfo & comps)
  157. {
  158. PlayerColor player = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1); //if no player, then use blue
  159. if(settings["session"]["spectate"].Bool()) //TODO: there must be better way to implement this
  160. player = PlayerColor(1);
  161. auto temp = std::make_shared<CInfoWindow>(txt, player, comps);
  162. temp->center(GH.getCursorPosition()); //center on mouse
  163. #ifdef VCMI_MOBILE
  164. temp->moveBy({0, -temp->pos.h / 2});
  165. #endif
  166. temp->fitToScreen(10);
  167. GH.windows().createAndPushWindow<CRClickPopupInt>(temp);
  168. }
  169. void CRClickPopup::createAndPush(const std::string & txt, const std::shared_ptr<CComponent> & component)
  170. {
  171. CInfoWindow::TCompsInfo intComps;
  172. intComps.push_back(component);
  173. createAndPush(txt, intComps);
  174. }
  175. void CRClickPopup::createAndPush(const CGObjectInstance * obj, const Point & p, ETextAlignment alignment)
  176. {
  177. auto iWin = createCustomInfoWindow(p, obj); //try get custom infowindow for this obj
  178. if(iWin)
  179. {
  180. GH.windows().pushWindow(iWin);
  181. }
  182. else
  183. {
  184. std::vector<Component> components;
  185. if(settings["general"]["enableUiEnhancements"].Bool())
  186. {
  187. if(LOCPLINT->localState->getCurrentHero())
  188. components = obj->getPopupComponents(LOCPLINT->localState->getCurrentHero());
  189. else
  190. components = obj->getPopupComponents(LOCPLINT->playerID);
  191. }
  192. std::vector<std::shared_ptr<CComponent>> guiComponents;
  193. for(auto & component : components)
  194. guiComponents.push_back(std::make_shared<CComponent>(component));
  195. if(LOCPLINT->localState->getCurrentHero())
  196. CRClickPopup::createAndPush(obj->getPopupText(LOCPLINT->localState->getCurrentHero()), guiComponents);
  197. else
  198. CRClickPopup::createAndPush(obj->getPopupText(LOCPLINT->playerID), guiComponents);
  199. }
  200. }
  201. CRClickPopupInt::CRClickPopupInt(const std::shared_ptr<CIntObject> & our) :
  202. dragDistance(Point(0, 0))
  203. {
  204. addUsedEvents(DRAG_POPUP);
  205. CCS->curh->hide();
  206. inner = our;
  207. addChild(our.get(), false);
  208. }
  209. CRClickPopupInt::~CRClickPopupInt()
  210. {
  211. CCS->curh->show();
  212. }
  213. void CRClickPopupInt::mouseDraggedPopup(const Point & cursorPosition, const Point & lastUpdateDistance)
  214. {
  215. if(!settings["adventure"]["rightButtonDrag"].Bool())
  216. return;
  217. dragDistance += lastUpdateDistance;
  218. if(dragDistance.length() > 16)
  219. close();
  220. }
  221. void CInfoBoxPopup::mouseDraggedPopup(const Point & cursorPosition, const Point & lastUpdateDistance)
  222. {
  223. if(!settings["adventure"]["rightButtonDrag"].Bool())
  224. return;
  225. dragDistance += lastUpdateDistance;
  226. if(dragDistance.length() > 16)
  227. close();
  228. }
  229. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGTownInstance * town)
  230. : CWindowObject(RCLICK_POPUP | PLAYER_COLORED, ImagePath::builtin("TOWNQVBK"), position)
  231. {
  232. InfoAboutTown iah;
  233. LOCPLINT->cb->getTownInfo(town, iah, LOCPLINT->localState->getCurrentArmy()); //todo: should this be nearest hero?
  234. OBJECT_CONSTRUCTION;
  235. tooltip = std::make_shared<CTownTooltip>(Point(9, 10), iah);
  236. addUsedEvents(DRAG_POPUP);
  237. fitToScreen(10);
  238. }
  239. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGHeroInstance * hero)
  240. : CWindowObject(RCLICK_POPUP | PLAYER_COLORED, ImagePath::builtin("HEROQVBK"), position)
  241. {
  242. InfoAboutHero iah;
  243. LOCPLINT->cb->getHeroInfo(hero, iah, LOCPLINT->localState->getCurrentArmy()); //todo: should this be nearest hero?
  244. OBJECT_CONSTRUCTION;
  245. tooltip = std::make_shared<CHeroTooltip>(Point(9, 10), iah);
  246. addUsedEvents(DRAG_POPUP);
  247. fitToScreen(10);
  248. }
  249. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGGarrison * garr)
  250. : CWindowObject(RCLICK_POPUP | PLAYER_COLORED, ImagePath::builtin("TOWNQVBK"), position)
  251. {
  252. InfoAboutTown iah;
  253. LOCPLINT->cb->getTownInfo(garr, iah);
  254. OBJECT_CONSTRUCTION;
  255. tooltip = std::make_shared<CArmyTooltip>(Point(9, 10), iah);
  256. addUsedEvents(DRAG_POPUP);
  257. fitToScreen(10);
  258. }
  259. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGCreature * creature)
  260. : CWindowObject(RCLICK_POPUP | BORDERED, ImagePath::builtin("DIBOXBCK"), position)
  261. {
  262. OBJECT_CONSTRUCTION;
  263. tooltip = std::make_shared<CreatureTooltip>(Point(9, 10), creature);
  264. addUsedEvents(DRAG_POPUP);
  265. fitToScreen(10);
  266. }
  267. MinimapWithIcons::MinimapWithIcons(const Point & position)
  268. {
  269. OBJECT_CONSTRUCTION;
  270. pos += position;
  271. Rect areaSurface(11, 41, 144, 144);
  272. Rect areaUnderground(167, 41, 144, 144);
  273. Rect borderSurface(10, 40, 147, 147);
  274. Rect borderUnderground(166, 40, 147, 147);
  275. bool singleLevelMap = LOCPLINT->cb->getMapSize().z == 1;
  276. if (singleLevelMap)
  277. {
  278. areaSurface.x += 78;
  279. borderSurface.x += 78;
  280. }
  281. backgroundSurface = std::make_shared<TransparentFilledRectangle>(borderSurface, Colors::TRANSPARENCY, Colors::YELLOW);
  282. surface = std::make_shared<CMinimapInstance>(areaSurface.topLeft(), areaSurface.dimensions(), 0);
  283. if (!singleLevelMap)
  284. {
  285. backgroundUnderground = std::make_shared<TransparentFilledRectangle>(borderUnderground, Colors::TRANSPARENCY, Colors::YELLOW);
  286. undergroud = std::make_shared<CMinimapInstance>(areaUnderground.topLeft(), areaUnderground.dimensions(), 1);
  287. }
  288. }
  289. void MinimapWithIcons::addIcon(const int3 & coordinates, const ImagePath & image )
  290. {
  291. OBJECT_CONSTRUCTION;
  292. Rect areaSurface(11, 41, 144, 144);
  293. Rect areaUnderground(167, 41, 144, 144);
  294. bool singleLevelMap = LOCPLINT->cb->getMapSize().z == 1;
  295. if (singleLevelMap)
  296. areaSurface.x += 78;
  297. int positionX = 144 * coordinates.x / LOCPLINT->cb->getMapSize().x;
  298. int positionY = 144 * coordinates.y / LOCPLINT->cb->getMapSize().y;
  299. Point iconPosition(positionX, positionY);
  300. iconPosition -= Point(8,8); // compensate for 16x16 icon half-size
  301. if (coordinates.z == 0)
  302. iconPosition += areaSurface.topLeft();
  303. else
  304. iconPosition += areaUnderground.topLeft();
  305. iconsOverlay.push_back(std::make_shared<CPicture>(image, iconPosition));
  306. }
  307. TeleporterPopup::TeleporterPopup(const Point & position, const CGTeleport * teleporter)
  308. : CWindowObject(BORDERED | RCLICK_POPUP)
  309. {
  310. OBJECT_CONSTRUCTION;
  311. pos.w = 322;
  312. pos.h = 200;
  313. filledBackground = std::make_shared<FilledTexturePlayerColored>(Rect(0, 0, pos.w, pos.h));
  314. labelTitle = std::make_shared<CLabel>(pos.w / 2, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, teleporter->getPopupText(LOCPLINT->playerID));
  315. minimap = std::make_shared<MinimapWithIcons>(Point(0,0));
  316. const auto & entrances = teleporter->getAllEntrances();
  317. const auto & exits = teleporter->getAllExits();
  318. std::set<ObjectInstanceID> allTeleporters;
  319. allTeleporters.insert(entrances.begin(), entrances.end());
  320. allTeleporters.insert(exits.begin(), exits.end());
  321. for (const auto exit : allTeleporters)
  322. {
  323. const auto * exitObject = LOCPLINT->cb->getObj(exit, false);
  324. if (!exitObject)
  325. continue;
  326. int3 position = exitObject->visitablePos();
  327. ImagePath image;
  328. if (!vstd::contains(entrances, exit))
  329. image = ImagePath::builtin("minimapIcons/portalExit");
  330. else if (!vstd::contains(exits, exit))
  331. image = ImagePath::builtin("minimapIcons/portalEntrance");
  332. else
  333. image = ImagePath::builtin("minimapIcons/portalBidirectional");
  334. minimap->addIcon(position, image);
  335. }
  336. center(position);
  337. fitToScreen(10);
  338. }
  339. KeymasterPopup::KeymasterPopup(const Point & position, const CGKeys * keymasterOrGuard)
  340. : CWindowObject(BORDERED | RCLICK_POPUP)
  341. {
  342. OBJECT_CONSTRUCTION;
  343. pos.w = 322;
  344. pos.h = 220;
  345. filledBackground = std::make_shared<FilledTexturePlayerColored>(Rect(0, 0, pos.w, pos.h));
  346. labelTitle = std::make_shared<CLabel>(pos.w / 2, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, keymasterOrGuard->getObjectName());
  347. labelDescription = std::make_shared<CLabel>(pos.w / 2, 40, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, keymasterOrGuard->getObjectDescription(LOCPLINT->playerID));
  348. minimap = std::make_shared<MinimapWithIcons>(Point(0,20));
  349. const auto allObjects = LOCPLINT->cb->getAllVisitableObjs();
  350. for (const auto mapObject : allObjects)
  351. {
  352. if (!mapObject)
  353. continue;
  354. switch (mapObject->ID)
  355. {
  356. case Obj::KEYMASTER:
  357. if (mapObject->subID == keymasterOrGuard->subID)
  358. minimap->addIcon(mapObject->visitablePos(), ImagePath::builtin("minimapIcons/keymaster"));
  359. break;
  360. case Obj::BORDERGUARD:
  361. if (mapObject->subID == keymasterOrGuard->subID)
  362. minimap->addIcon(mapObject->visitablePos(), ImagePath::builtin("minimapIcons/borderguard"));
  363. break;
  364. case Obj::BORDER_GATE:
  365. if (mapObject->subID == keymasterOrGuard->subID)
  366. minimap->addIcon(mapObject->visitablePos(), ImagePath::builtin("minimapIcons/bordergate"));
  367. break;
  368. }
  369. }
  370. center(position);
  371. fitToScreen(10);
  372. }
  373. ObeliskPopup::ObeliskPopup(const Point & position, const CGObelisk * obelisk)
  374. : CWindowObject(BORDERED | RCLICK_POPUP)
  375. {
  376. OBJECT_CONSTRUCTION;
  377. pos.w = 322;
  378. pos.h = 220;
  379. filledBackground = std::make_shared<FilledTexturePlayerColored>(Rect(0, 0, pos.w, pos.h));
  380. labelTitle = std::make_shared<CLabel>(pos.w / 2, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, obelisk->getObjectName());
  381. labelDescription = std::make_shared<CLabel>(pos.w / 2, 40, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, obelisk->getObjectDescription(LOCPLINT->playerID));
  382. minimap = std::make_shared<MinimapWithIcons>(Point(0,20));
  383. const auto allObjects = LOCPLINT->cb->getAllVisitableObjs();
  384. for (const auto mapObject : allObjects)
  385. {
  386. if (!mapObject)
  387. continue;
  388. if (mapObject->ID != Obj::OBELISK)
  389. continue;
  390. if (mapObject->wasVisited(LOCPLINT->playerID))
  391. minimap->addIcon(mapObject->visitablePos(), ImagePath::builtin("minimapIcons/obeliskVisited"));
  392. else
  393. minimap->addIcon(mapObject->visitablePos(), ImagePath::builtin("minimapIcons/obelisk"));
  394. }
  395. center(position);
  396. fitToScreen(10);
  397. }
  398. std::shared_ptr<WindowBase>
  399. CRClickPopup::createCustomInfoWindow(Point position, const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  400. {
  401. if(nullptr == specific)
  402. specific = LOCPLINT->localState->getCurrentArmy();
  403. if(nullptr == specific)
  404. {
  405. logGlobal->error("createCustomInfoWindow: no object to describe");
  406. return nullptr;
  407. }
  408. switch(specific->ID)
  409. {
  410. case Obj::HERO:
  411. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGHeroInstance *>(specific));
  412. case Obj::TOWN:
  413. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGTownInstance *>(specific));
  414. case Obj::MONSTER:
  415. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGCreature *>(specific));
  416. case Obj::GARRISON:
  417. case Obj::GARRISON2:
  418. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGGarrison *>(specific));
  419. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  420. case Obj::MONOLITH_ONE_WAY_EXIT:
  421. case Obj::MONOLITH_TWO_WAY:
  422. case Obj::SUBTERRANEAN_GATE:
  423. case Obj::WHIRLPOOL:
  424. return std::make_shared<TeleporterPopup>(position, dynamic_cast<const CGTeleport *>(specific));
  425. case Obj::KEYMASTER:
  426. case Obj::BORDERGUARD:
  427. case Obj::BORDER_GATE:
  428. return std::make_shared<KeymasterPopup>(position, dynamic_cast<const CGKeys *>(specific));
  429. case Obj::OBELISK:
  430. return std::make_shared<ObeliskPopup>(position, dynamic_cast<const CGObelisk *>(specific));
  431. default:
  432. return std::shared_ptr<WindowBase>();
  433. }
  434. }