InfoWindows.cpp 11 KB

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