InfoWindows.cpp 11 KB

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