InfoWindows.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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::createAndPush(const std::string & txt, const CInfoWindow::TCompsInfo & comps)
  154. {
  155. PlayerColor player = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1); //if no player, then use blue
  156. if(settings["session"]["spectate"].Bool()) //TODO: there must be better way to implement this
  157. player = PlayerColor(1);
  158. auto temp = std::make_shared<CInfoWindow>(txt, player, comps);
  159. temp->center(GH.getCursorPosition()); //center on mouse
  160. #ifdef VCMI_MOBILE
  161. temp->moveBy({0, -temp->pos.h / 2});
  162. #endif
  163. temp->fitToScreen(10);
  164. GH.windows().createAndPushWindow<CRClickPopupInt>(temp);
  165. }
  166. void CRClickPopup::createAndPush(const std::string & txt, const std::shared_ptr<CComponent> & component)
  167. {
  168. CInfoWindow::TCompsInfo intComps;
  169. intComps.push_back(component);
  170. createAndPush(txt, intComps);
  171. }
  172. void CRClickPopup::createAndPush(const CGObjectInstance * obj, const Point & p, ETextAlignment alignment)
  173. {
  174. auto iWin = createCustomInfoWindow(p, obj); //try get custom infowindow for this obj
  175. if(iWin)
  176. {
  177. GH.windows().pushWindow(iWin);
  178. }
  179. else
  180. {
  181. std::vector<Component> components;
  182. if(settings["general"]["enableUiEnhancements"].Bool())
  183. {
  184. if(LOCPLINT->localState->getCurrentHero())
  185. components = obj->getPopupComponents(LOCPLINT->localState->getCurrentHero());
  186. else
  187. components = obj->getPopupComponents(LOCPLINT->playerID);
  188. }
  189. std::vector<std::shared_ptr<CComponent>> guiComponents;
  190. for(auto & component : components)
  191. guiComponents.push_back(std::make_shared<CComponent>(component));
  192. if(LOCPLINT->localState->getCurrentHero())
  193. CRClickPopup::createAndPush(obj->getPopupText(LOCPLINT->localState->getCurrentHero()), guiComponents);
  194. else
  195. CRClickPopup::createAndPush(obj->getPopupText(LOCPLINT->playerID), guiComponents);
  196. }
  197. }
  198. CRClickPopupInt::CRClickPopupInt(const std::shared_ptr<CIntObject> & our) :
  199. dragDistance(Point(0, 0))
  200. {
  201. addUsedEvents(DRAG_POPUP);
  202. CCS->curh->hide();
  203. inner = our;
  204. addChild(our.get(), false);
  205. }
  206. CRClickPopupInt::~CRClickPopupInt()
  207. {
  208. CCS->curh->show();
  209. }
  210. void CRClickPopupInt::mouseDraggedPopup(const Point & cursorPosition, const Point & lastUpdateDistance)
  211. {
  212. if(!settings["adventure"]["rightButtonDrag"].Bool())
  213. return;
  214. dragDistance += lastUpdateDistance;
  215. if(dragDistance.length() > 16)
  216. close();
  217. }
  218. Point CInfoBoxPopup::toScreen(Point p)
  219. {
  220. auto bounds = adventureInt->terrainAreaPixels();
  221. vstd::abetween(p.x, bounds.top() + 100, bounds.bottom() - 100);
  222. vstd::abetween(p.y, bounds.left() + 100, bounds.right() - 100);
  223. return p;
  224. }
  225. void CInfoBoxPopup::mouseDraggedPopup(const Point & cursorPosition, const Point & lastUpdateDistance)
  226. {
  227. if(!settings["adventure"]["rightButtonDrag"].Bool())
  228. return;
  229. dragDistance += lastUpdateDistance;
  230. if(dragDistance.length() > 16)
  231. close();
  232. }
  233. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGTownInstance * town)
  234. : CWindowObject(RCLICK_POPUP | PLAYER_COLORED, ImagePath::builtin("TOWNQVBK"), toScreen(position))
  235. {
  236. InfoAboutTown iah;
  237. LOCPLINT->cb->getTownInfo(town, iah, LOCPLINT->localState->getCurrentArmy()); //todo: should this be nearest hero?
  238. OBJECT_CONSTRUCTION;
  239. tooltip = std::make_shared<CTownTooltip>(Point(9, 10), iah);
  240. addUsedEvents(DRAG_POPUP);
  241. }
  242. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGHeroInstance * hero)
  243. : CWindowObject(RCLICK_POPUP | PLAYER_COLORED, ImagePath::builtin("HEROQVBK"), toScreen(position))
  244. {
  245. InfoAboutHero iah;
  246. LOCPLINT->cb->getHeroInfo(hero, iah, LOCPLINT->localState->getCurrentArmy()); //todo: should this be nearest hero?
  247. OBJECT_CONSTRUCTION;
  248. tooltip = std::make_shared<CHeroTooltip>(Point(9, 10), iah);
  249. addUsedEvents(DRAG_POPUP);
  250. }
  251. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGGarrison * garr)
  252. : CWindowObject(RCLICK_POPUP | PLAYER_COLORED, ImagePath::builtin("TOWNQVBK"), toScreen(position))
  253. {
  254. InfoAboutTown iah;
  255. LOCPLINT->cb->getTownInfo(garr, iah);
  256. OBJECT_CONSTRUCTION;
  257. tooltip = std::make_shared<CArmyTooltip>(Point(9, 10), iah);
  258. addUsedEvents(DRAG_POPUP);
  259. }
  260. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGCreature * creature)
  261. : CWindowObject(RCLICK_POPUP | BORDERED, ImagePath::builtin("DIBOXBCK"), toScreen(position))
  262. {
  263. OBJECT_CONSTRUCTION;
  264. tooltip = std::make_shared<CreatureTooltip>(Point(9, 10), creature);
  265. addUsedEvents(DRAG_POPUP);
  266. }
  267. std::shared_ptr<WindowBase>
  268. CRClickPopup::createCustomInfoWindow(Point position, const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  269. {
  270. if(nullptr == specific)
  271. specific = LOCPLINT->localState->getCurrentArmy();
  272. if(nullptr == specific)
  273. {
  274. logGlobal->error("createCustomInfoWindow: no object to describe");
  275. return nullptr;
  276. }
  277. switch(specific->ID)
  278. {
  279. case Obj::HERO:
  280. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGHeroInstance *>(specific));
  281. case Obj::TOWN:
  282. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGTownInstance *>(specific));
  283. case Obj::MONSTER:
  284. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGCreature *>(specific));
  285. case Obj::GARRISON:
  286. case Obj::GARRISON2:
  287. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGGarrison *>(specific));
  288. default:
  289. return std::shared_ptr<WindowBase>();
  290. }
  291. }