InfoWindows.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 "../../lib/CondSh.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_CAPTURING(255 - DISPOSE);
  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. {
  49. buttons.back()->addCallback([askID](){LOCPLINT->cb->selectionMade(0, askID);});
  50. //buttons.back()->addCallback(std::bind(&CCallback::selectionMade, LOCPLINT->cb.get(), 0, askID));
  51. }
  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. components = std::make_shared<CComponentBox>(comps, Rect(0,0,0,0));
  61. CMessage::drawIWindow(this, Text, player);
  62. }
  63. void CSelWindow::madeChoice()
  64. {
  65. if(ID.getNum() < 0)
  66. return;
  67. int ret = -1;
  68. if(components)
  69. ret = components->selectedIndex();
  70. LOCPLINT->cb->selectionMade(ret + 1, ID);
  71. }
  72. void CSelWindow::madeChoiceAndClose()
  73. {
  74. madeChoice();
  75. close();
  76. }
  77. CInfoWindow::CInfoWindow(const std::string & Text, PlayerColor player, const TCompsInfo & comps, const TButtonsInfo & Buttons)
  78. {
  79. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  80. backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DiBoxBck"), pos);
  81. ID = QueryID(-1);
  82. for(const auto & Button : Buttons)
  83. {
  84. auto button = std::make_shared<CButton>(Point(0, 0), Button.first, CButton::tooltip(), std::bind(&CInfoWindow::close, this));
  85. button->setBorderColor(Colors::METALLIC_GOLD);
  86. button->addCallback(Button.second); //each button will close the window apart from call-defined actions
  87. buttons.push_back(button);
  88. }
  89. text = std::make_shared<CTextBox>(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  90. if(!text->slider)
  91. {
  92. int finalWidth = std::min(250, text->label->textSize.x + 32);
  93. int finalHeight = text->label->textSize.y;
  94. text->resize(Point(finalWidth, finalHeight));
  95. }
  96. if(buttons.size() == 1)
  97. buttons.front()->assignedKey = EShortcut::GLOBAL_RETURN;
  98. if(buttons.size() == 2)
  99. {
  100. buttons.front()->assignedKey = EShortcut::GLOBAL_ACCEPT;
  101. buttons.back()->assignedKey = EShortcut::GLOBAL_CANCEL;
  102. }
  103. if(!comps.empty())
  104. components = std::make_shared<CComponentBox>(comps, Rect(0,0,0,0));
  105. CMessage::drawIWindow(this, Text, player);
  106. }
  107. CInfoWindow::CInfoWindow()
  108. {
  109. ID = QueryID(-1);
  110. }
  111. void CInfoWindow::close()
  112. {
  113. WindowBase::close();
  114. if(LOCPLINT)
  115. LOCPLINT->showingDialog->setn(false);
  116. }
  117. void CInfoWindow::showAll(Canvas & to)
  118. {
  119. CIntObject::showAll(to);
  120. CMessage::drawBorder(LOCPLINT ? LOCPLINT->playerID : PlayerColor(1), to, pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  121. }
  122. CInfoWindow::~CInfoWindow() = default;
  123. void CInfoWindow::showInfoDialog(const std::string & text, const TCompsInfo & components, PlayerColor player)
  124. {
  125. GH.windows().pushWindow(CInfoWindow::create(text, player, components));
  126. }
  127. void CInfoWindow::showYesNoDialog(const std::string & text, const TCompsInfo & components, const CFunctionList<void()> & onYes, const CFunctionList<void()> & onNo, PlayerColor player)
  128. {
  129. assert(!LOCPLINT || LOCPLINT->showingDialog->get());
  130. std::vector<std::pair<AnimationPath, CFunctionList<void()>>> pom;
  131. pom.emplace_back(AnimationPath::builtin("IOKAY.DEF"), nullptr);
  132. pom.emplace_back(AnimationPath::builtin("ICANCEL.DEF"), nullptr);
  133. auto temp = std::make_shared<CInfoWindow>(text, player, components, pom);
  134. temp->buttons[0]->addCallback(onYes);
  135. temp->buttons[1]->addCallback(onNo);
  136. GH.windows().pushWindow(temp);
  137. }
  138. std::shared_ptr<CInfoWindow> CInfoWindow::create(const std::string & text, PlayerColor playerID, const TCompsInfo & components)
  139. {
  140. std::vector<std::pair<AnimationPath, CFunctionList<void()>>> pom;
  141. pom.emplace_back(AnimationPath::builtin("IOKAY.DEF"), nullptr);
  142. return std::make_shared<CInfoWindow>(text, playerID, components, pom);
  143. }
  144. std::string CInfoWindow::genText(const std::string & title, const std::string & description)
  145. {
  146. return std::string("{") + title + "}" + "\n\n" + description;
  147. }
  148. bool CRClickPopup::isPopupWindow() const
  149. {
  150. return true;
  151. }
  152. void CRClickPopup::close()
  153. {
  154. WindowBase::close();
  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. {
  203. CCS->curh->hide();
  204. defActions = SHOWALL | UPDATE;
  205. our->recActions = defActions;
  206. inner = our;
  207. addChild(our.get(), false);
  208. }
  209. CRClickPopupInt::~CRClickPopupInt()
  210. {
  211. CCS->curh->show();
  212. }
  213. Point CInfoBoxPopup::toScreen(Point p)
  214. {
  215. auto bounds = adventureInt->terrainAreaPixels();
  216. vstd::abetween(p.x, bounds.top() + 100, bounds.bottom() - 100);
  217. vstd::abetween(p.y, bounds.left() + 100, bounds.right() - 100);
  218. return p;
  219. }
  220. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGTownInstance * town)
  221. : CWindowObject(RCLICK_POPUP | PLAYER_COLORED, ImagePath::builtin("TOWNQVBK"), toScreen(position))
  222. {
  223. InfoAboutTown iah;
  224. LOCPLINT->cb->getTownInfo(town, iah, LOCPLINT->localState->getCurrentTown()); //todo: should this be nearest hero?
  225. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  226. tooltip = std::make_shared<CTownTooltip>(Point(9, 10), iah);
  227. }
  228. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGHeroInstance * hero)
  229. : CWindowObject(RCLICK_POPUP | PLAYER_COLORED, ImagePath::builtin("HEROQVBK"), toScreen(position))
  230. {
  231. InfoAboutHero iah;
  232. LOCPLINT->cb->getHeroInfo(hero, iah, LOCPLINT->localState->getCurrentHero()); //todo: should this be nearest hero?
  233. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  234. tooltip = std::make_shared<CHeroTooltip>(Point(9, 10), iah);
  235. }
  236. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGGarrison * garr)
  237. : CWindowObject(RCLICK_POPUP | PLAYER_COLORED, ImagePath::builtin("TOWNQVBK"), toScreen(position))
  238. {
  239. InfoAboutTown iah;
  240. LOCPLINT->cb->getTownInfo(garr, iah);
  241. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  242. tooltip = std::make_shared<CArmyTooltip>(Point(9, 10), iah);
  243. }
  244. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGCreature * creature)
  245. : CWindowObject(RCLICK_POPUP | BORDERED, ImagePath::builtin("DIBOXBCK"), toScreen(position))
  246. {
  247. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  248. tooltip = std::make_shared<CreatureTooltip>(Point(9, 10), creature);
  249. }
  250. std::shared_ptr<WindowBase>
  251. CRClickPopup::createCustomInfoWindow(Point position, const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  252. {
  253. if(nullptr == specific)
  254. specific = LOCPLINT->localState->getCurrentArmy();
  255. if(nullptr == specific)
  256. {
  257. logGlobal->error("createCustomInfoWindow: no object to describe");
  258. return nullptr;
  259. }
  260. switch(specific->ID)
  261. {
  262. case Obj::HERO:
  263. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGHeroInstance *>(specific));
  264. case Obj::TOWN:
  265. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGTownInstance *>(specific));
  266. case Obj::MONSTER:
  267. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGCreature *>(specific));
  268. case Obj::GARRISON:
  269. case Obj::GARRISON2:
  270. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGGarrison *>(specific));
  271. default:
  272. return std::shared_ptr<WindowBase>();
  273. }
  274. }