InfoWindows.cpp 13 KB

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