InfoWindows.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 "../gui/CGuiHandler.h"
  17. #include "../gui/CursorHandler.h"
  18. #include "../gui/Shortcut.h"
  19. #include "../gui/WindowHandler.h"
  20. #include "../widgets/Buttons.h"
  21. #include "../widgets/CComponent.h"
  22. #include "../widgets/Images.h"
  23. #include "../widgets/MiscWidgets.h"
  24. #include "../widgets/TextControls.h"
  25. #include "../windows/CMessage.h"
  26. #include "../../CCallback.h"
  27. #include "../../lib/CConfigHandler.h"
  28. #include "../ConditionalWait.h"
  29. #include "../../lib/gameState/InfoAboutArmy.h"
  30. #include "../../lib/mapObjects/CGCreature.h"
  31. #include "../../lib/mapObjects/CGHeroInstance.h"
  32. #include "../../lib/mapObjects/CGTownInstance.h"
  33. #include "../../lib/mapObjects/MiscObjects.h"
  34. 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)
  35. {
  36. OBJECT_CONSTRUCTION;
  37. backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DiBoxBck"), pos);
  38. ID = askID;
  39. for(int i = 0; i < Buttons.size(); i++)
  40. {
  41. buttons.push_back(std::make_shared<CButton>(Point(0, 0), Buttons[i].first, CButton::tooltip(), Buttons[i].second));
  42. if(!i && askID.getNum() >= 0)
  43. buttons.back()->addCallback(std::bind(&CSelWindow::madeChoice, this));
  44. buttons[i]->addCallback(std::bind(&CInfoWindow::close, this)); //each button will close the window apart from call-defined actions
  45. }
  46. text = std::make_shared<CTextBox>(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  47. if(buttons.size() > 1 && askID.getNum() >= 0) //cancel button functionality
  48. buttons.back()->addCallback([askID](){LOCPLINT->cb->selectionMade(0, askID);});
  49. if(buttons.size() == 1)
  50. buttons.front()->assignedKey = EShortcut::GLOBAL_RETURN;
  51. if(buttons.size() == 2)
  52. {
  53. buttons.front()->assignedKey = EShortcut::GLOBAL_ACCEPT;
  54. buttons.back()->assignedKey = EShortcut::GLOBAL_CANCEL;
  55. }
  56. if(!comps.empty())
  57. {
  58. components = std::make_shared<CComponentBox>(comps, Rect(0,0,0,0));
  59. for (auto & comp : comps)
  60. comp->onChoose = [this](){ madeChoiceAndClose(); };
  61. }
  62. CMessage::drawIWindow(this, Text, player);
  63. }
  64. void CSelWindow::madeChoice()
  65. {
  66. if(ID.getNum() < 0)
  67. return;
  68. int ret = -1;
  69. if(components)
  70. ret = components->selectedIndex();
  71. LOCPLINT->cb->selectionMade(ret + 1, ID);
  72. }
  73. void CSelWindow::madeChoiceAndClose()
  74. {
  75. madeChoice();
  76. close();
  77. }
  78. CInfoWindow::CInfoWindow(const std::string & Text, PlayerColor player, const TCompsInfo & comps, const TButtonsInfo & Buttons)
  79. {
  80. OBJECT_CONSTRUCTION;
  81. backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DiBoxBck"), pos);
  82. ID = QueryID(-1);
  83. for(const auto & Button : Buttons)
  84. {
  85. auto button = std::make_shared<CButton>(Point(0, 0), Button.first, CButton::tooltip(), std::bind(&CInfoWindow::close, this));
  86. button->setBorderColor(Colors::METALLIC_GOLD);
  87. button->addCallback(Button.second); //each button will close the window apart from call-defined actions
  88. buttons.push_back(button);
  89. }
  90. text = std::make_shared<CTextBox>(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  91. if(!text->slider)
  92. {
  93. int finalWidth = std::min(250, text->label->textSize.x + 32);
  94. int finalHeight = text->label->textSize.y;
  95. text->resize(Point(finalWidth, finalHeight));
  96. }
  97. if(buttons.size() == 1)
  98. buttons.front()->assignedKey = EShortcut::GLOBAL_RETURN;
  99. if(buttons.size() == 2)
  100. {
  101. buttons.front()->assignedKey = EShortcut::GLOBAL_ACCEPT;
  102. buttons.back()->assignedKey = EShortcut::GLOBAL_CANCEL;
  103. }
  104. if(!comps.empty())
  105. components = std::make_shared<CComponentBox>(comps, Rect(0,0,0,0));
  106. CMessage::drawIWindow(this, Text, player);
  107. }
  108. CInfoWindow::CInfoWindow()
  109. {
  110. ID = QueryID(-1);
  111. }
  112. void CInfoWindow::close()
  113. {
  114. WindowBase::close();
  115. if(LOCPLINT)
  116. LOCPLINT->showingDialog->setFree();
  117. }
  118. void CInfoWindow::showAll(Canvas & to)
  119. {
  120. CIntObject::showAll(to);
  121. CMessage::drawBorder(LOCPLINT ? LOCPLINT->playerID : PlayerColor(1), to, pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  122. }
  123. CInfoWindow::~CInfoWindow() = default;
  124. void CInfoWindow::showInfoDialog(const std::string & text, const TCompsInfo & components, PlayerColor player)
  125. {
  126. GH.windows().pushWindow(CInfoWindow::create(text, player, components));
  127. }
  128. void CInfoWindow::showYesNoDialog(const std::string & text, const TCompsInfo & components, const CFunctionList<void()> & onYes, const CFunctionList<void()> & onNo, PlayerColor player)
  129. {
  130. assert(!LOCPLINT || LOCPLINT->showingDialog->isBusy());
  131. std::vector<std::pair<AnimationPath, CFunctionList<void()>>> pom;
  132. pom.emplace_back(AnimationPath::builtin("IOKAY.DEF"), nullptr);
  133. pom.emplace_back(AnimationPath::builtin("ICANCEL.DEF"), nullptr);
  134. auto temp = std::make_shared<CInfoWindow>(text, player, components, pom);
  135. temp->buttons[0]->addCallback(onYes);
  136. temp->buttons[1]->addCallback(onNo);
  137. GH.windows().pushWindow(temp);
  138. }
  139. std::shared_ptr<CInfoWindow> CInfoWindow::create(const std::string & text, PlayerColor playerID, const TCompsInfo & components)
  140. {
  141. std::vector<std::pair<AnimationPath, CFunctionList<void()>>> pom;
  142. pom.emplace_back(AnimationPath::builtin("IOKAY.DEF"), nullptr);
  143. return std::make_shared<CInfoWindow>(text, playerID, components, pom);
  144. }
  145. std::string CInfoWindow::genText(const std::string & title, const std::string & description)
  146. {
  147. return std::string("{") + title + "}" + "\n\n" + description;
  148. }
  149. bool CRClickPopup::isPopupWindow() const
  150. {
  151. return true;
  152. }
  153. void CRClickPopup::close()
  154. {
  155. WindowBase::close();
  156. }
  157. void CRClickPopup::createAndPush(const std::string & txt, const CInfoWindow::TCompsInfo & comps)
  158. {
  159. PlayerColor player = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1); //if no player, then use blue
  160. if(settings["session"]["spectate"].Bool()) //TODO: there must be better way to implement this
  161. player = PlayerColor(1);
  162. auto temp = std::make_shared<CInfoWindow>(txt, player, comps);
  163. temp->center(GH.getCursorPosition()); //center on mouse
  164. #ifdef VCMI_MOBILE
  165. temp->moveBy({0, -temp->pos.h / 2});
  166. #endif
  167. temp->fitToScreen(10);
  168. GH.windows().createAndPushWindow<CRClickPopupInt>(temp);
  169. }
  170. void CRClickPopup::createAndPush(const std::string & txt, const std::shared_ptr<CComponent> & component)
  171. {
  172. CInfoWindow::TCompsInfo intComps;
  173. intComps.push_back(component);
  174. createAndPush(txt, intComps);
  175. }
  176. void CRClickPopup::createAndPush(const CGObjectInstance * obj, const Point & p, ETextAlignment alignment)
  177. {
  178. auto iWin = createCustomInfoWindow(p, obj); //try get custom infowindow for this obj
  179. if(iWin)
  180. {
  181. GH.windows().pushWindow(iWin);
  182. }
  183. else
  184. {
  185. std::vector<Component> components;
  186. if(settings["general"]["enableUiEnhancements"].Bool())
  187. {
  188. if(LOCPLINT->localState->getCurrentHero())
  189. components = obj->getPopupComponents(LOCPLINT->localState->getCurrentHero());
  190. else
  191. components = obj->getPopupComponents(LOCPLINT->playerID);
  192. }
  193. std::vector<std::shared_ptr<CComponent>> guiComponents;
  194. for(auto & component : components)
  195. guiComponents.push_back(std::make_shared<CComponent>(component));
  196. if(LOCPLINT->localState->getCurrentHero())
  197. CRClickPopup::createAndPush(obj->getPopupText(LOCPLINT->localState->getCurrentHero()), guiComponents);
  198. else
  199. CRClickPopup::createAndPush(obj->getPopupText(LOCPLINT->playerID), guiComponents);
  200. }
  201. }
  202. CRClickPopupInt::CRClickPopupInt(const std::shared_ptr<CIntObject> & our)
  203. {
  204. CCS->curh->hide();
  205. inner = our;
  206. addChild(our.get(), false);
  207. }
  208. CRClickPopupInt::~CRClickPopupInt()
  209. {
  210. CCS->curh->show();
  211. }
  212. Point CInfoBoxPopup::toScreen(Point p)
  213. {
  214. auto bounds = adventureInt->terrainAreaPixels();
  215. vstd::abetween(p.x, bounds.top() + 100, bounds.bottom() - 100);
  216. vstd::abetween(p.y, bounds.left() + 100, bounds.right() - 100);
  217. return p;
  218. }
  219. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGTownInstance * town)
  220. : CWindowObject(RCLICK_POPUP | PLAYER_COLORED, ImagePath::builtin("TOWNQVBK"), toScreen(position))
  221. {
  222. InfoAboutTown iah;
  223. LOCPLINT->cb->getTownInfo(town, iah, LOCPLINT->localState->getCurrentTown()); //todo: should this be nearest hero?
  224. OBJECT_CONSTRUCTION;
  225. tooltip = std::make_shared<CTownTooltip>(Point(9, 10), iah);
  226. }
  227. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGHeroInstance * hero)
  228. : CWindowObject(RCLICK_POPUP | PLAYER_COLORED, ImagePath::builtin("HEROQVBK"), toScreen(position))
  229. {
  230. InfoAboutHero iah;
  231. LOCPLINT->cb->getHeroInfo(hero, iah, LOCPLINT->localState->getCurrentHero()); //todo: should this be nearest hero?
  232. OBJECT_CONSTRUCTION;
  233. tooltip = std::make_shared<CHeroTooltip>(Point(9, 10), iah);
  234. }
  235. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGGarrison * garr)
  236. : CWindowObject(RCLICK_POPUP | PLAYER_COLORED, ImagePath::builtin("TOWNQVBK"), toScreen(position))
  237. {
  238. InfoAboutTown iah;
  239. LOCPLINT->cb->getTownInfo(garr, iah);
  240. OBJECT_CONSTRUCTION;
  241. tooltip = std::make_shared<CArmyTooltip>(Point(9, 10), iah);
  242. }
  243. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGCreature * creature)
  244. : CWindowObject(RCLICK_POPUP | BORDERED, ImagePath::builtin("DIBOXBCK"), toScreen(position))
  245. {
  246. OBJECT_CONSTRUCTION;
  247. tooltip = std::make_shared<CreatureTooltip>(Point(9, 10), creature);
  248. }
  249. std::shared_ptr<WindowBase>
  250. CRClickPopup::createCustomInfoWindow(Point position, const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  251. {
  252. if(nullptr == specific)
  253. specific = LOCPLINT->localState->getCurrentArmy();
  254. if(nullptr == specific)
  255. {
  256. logGlobal->error("createCustomInfoWindow: no object to describe");
  257. return nullptr;
  258. }
  259. switch(specific->ID)
  260. {
  261. case Obj::HERO:
  262. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGHeroInstance *>(specific));
  263. case Obj::TOWN:
  264. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGTownInstance *>(specific));
  265. case Obj::MONSTER:
  266. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGCreature *>(specific));
  267. case Obj::GARRISON:
  268. case Obj::GARRISON2:
  269. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGGarrison *>(specific));
  270. default:
  271. return std::shared_ptr<WindowBase>();
  272. }
  273. }