InfoWindows.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 "../widgets/CComponent.h"
  17. #include "../widgets/MiscWidgets.h"
  18. #include "../widgets/Buttons.h"
  19. #include "../widgets/TextControls.h"
  20. #include "../gui/CGuiHandler.h"
  21. #include "../gui/WindowHandler.h"
  22. #include "../battle/BattleInterface.h"
  23. #include "../battle/BattleInterfaceClasses.h"
  24. #include "../adventureMap/AdventureMapInterface.h"
  25. #include "../windows/CMessage.h"
  26. #include "../render/Canvas.h"
  27. #include "../renderSDL/SDL_Extensions.h"
  28. #include "../gui/CursorHandler.h"
  29. #include "../gui/Shortcut.h"
  30. #include "../../CCallback.h"
  31. #include "../../lib/CConfigHandler.h"
  32. #include "../../lib/CondSh.h"
  33. #include "../../lib/CGeneralTextHandler.h" //for Unicode related stuff
  34. #include "../../lib/mapObjects/CGCreature.h"
  35. #include "../../lib/mapObjects/CGHeroInstance.h"
  36. #include "../../lib/mapObjects/CGTownInstance.h"
  37. #include "../../lib/mapObjects/MiscObjects.h"
  38. #include "../../lib/gameState/InfoAboutArmy.h"
  39. #include <SDL_surface.h>
  40. void CSimpleWindow::show(Canvas & to)
  41. {
  42. if(bitmap)
  43. CSDL_Ext::blitAt(bitmap, pos.x, pos.y, to.getInternalSurface());
  44. }
  45. CSimpleWindow::~CSimpleWindow()
  46. {
  47. if (bitmap)
  48. {
  49. SDL_FreeSurface(bitmap);
  50. bitmap=nullptr;
  51. }
  52. }
  53. void CSelWindow::selectionChange(unsigned to)
  54. {
  55. for (unsigned i=0;i<components.size();i++)
  56. {
  57. auto pom = std::dynamic_pointer_cast<CSelectableComponent>(components[i]);
  58. if (!pom)
  59. continue;
  60. pom->select(i==to);
  61. }
  62. redraw();
  63. }
  64. 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)
  65. {
  66. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  67. ID = askID;
  68. for (int i = 0; i < Buttons.size(); i++)
  69. {
  70. buttons.push_back(std::make_shared<CButton>(Point(0, 0), Buttons[i].first, CButton::tooltip(), Buttons[i].second));
  71. if (!i && askID.getNum() >= 0)
  72. buttons.back()->addCallback(std::bind(&CSelWindow::madeChoice, this));
  73. buttons[i]->addCallback(std::bind(&CInfoWindow::close, this)); //each button will close the window apart from call-defined actions
  74. }
  75. text = std::make_shared<CTextBox>(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  76. if (buttons.size() > 1 && askID.getNum() >= 0) //cancel button functionality
  77. {
  78. buttons.back()->addCallback([askID]() {
  79. LOCPLINT->cb.get()->selectionMade(0, askID);
  80. });
  81. //buttons.back()->addCallback(std::bind(&CCallback::selectionMade, LOCPLINT->cb.get(), 0, askID));
  82. }
  83. if(buttons.size() == 1)
  84. buttons.front()->assignedKey = EShortcut::GLOBAL_RETURN;
  85. if(buttons.size() == 2)
  86. {
  87. buttons.front()->assignedKey = EShortcut::GLOBAL_ACCEPT;
  88. buttons.back()->assignedKey = EShortcut::GLOBAL_CANCEL;
  89. }
  90. for(int i=0;i<comps.size();i++)
  91. {
  92. comps[i]->recActions = 255-DISPOSE;
  93. addChild(comps[i].get());
  94. components.push_back(comps[i]);
  95. comps[i]->onSelect = std::bind(&CSelWindow::selectionChange,this,i);
  96. comps[i]->onChoose = std::bind(&CSelWindow::madeChoiceAndClose,this);
  97. if(i<8)
  98. comps[i]->assignedKey = vstd::next(EShortcut::SELECT_INDEX_1,i);
  99. }
  100. CMessage::drawIWindow(this, Text, player);
  101. }
  102. void CSelWindow::madeChoice()
  103. {
  104. if(ID.getNum() < 0)
  105. return;
  106. int ret = -1;
  107. for (int i=0;i<components.size();i++)
  108. {
  109. if(std::dynamic_pointer_cast<CSelectableComponent>(components[i])->selected)
  110. {
  111. ret = i;
  112. }
  113. }
  114. LOCPLINT->cb->selectionMade(ret+1,ID);
  115. }
  116. void CSelWindow::madeChoiceAndClose()
  117. {
  118. madeChoice();
  119. close();
  120. }
  121. CInfoWindow::CInfoWindow(std::string Text, PlayerColor player, const TCompsInfo & comps, const TButtonsInfo & Buttons)
  122. {
  123. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  124. ID = QueryID(-1);
  125. for(auto & Button : Buttons)
  126. {
  127. std::shared_ptr<CButton> button = std::make_shared<CButton>(Point(0,0), Button.first, CButton::tooltip(), std::bind(&CInfoWindow::close, this));
  128. button->setBorderColor(Colors::METALLIC_GOLD);
  129. button->addCallback(Button.second); //each button will close the window apart from call-defined actions
  130. buttons.push_back(button);
  131. }
  132. text = std::make_shared<CTextBox>(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  133. if(!text->slider)
  134. {
  135. int finalWidth = std::min(250, text->label->textSize.x + 32);
  136. int finalHeight = text->label->textSize.y;
  137. text->resize(Point(finalWidth, finalHeight));
  138. }
  139. if(buttons.size() == 1)
  140. buttons.front()->assignedKey = EShortcut::GLOBAL_RETURN;
  141. if(buttons.size() == 2)
  142. {
  143. buttons.front()->assignedKey = EShortcut::GLOBAL_ACCEPT;
  144. buttons.back()->assignedKey = EShortcut::GLOBAL_CANCEL;
  145. }
  146. for(auto & comp : comps)
  147. {
  148. comp->recActions = 0xff & ~DISPOSE;
  149. addChild(comp.get());
  150. comp->recActions &= ~(SHOWALL | UPDATE);
  151. components.push_back(comp);
  152. }
  153. CMessage::drawIWindow(this,Text,player);
  154. }
  155. CInfoWindow::CInfoWindow()
  156. {
  157. ID = QueryID(-1);
  158. }
  159. void CInfoWindow::close()
  160. {
  161. WindowBase::close();
  162. if(LOCPLINT)
  163. LOCPLINT->showingDialog->setn(false);
  164. }
  165. void CInfoWindow::show(Canvas & to)
  166. {
  167. CIntObject::show(to);
  168. }
  169. CInfoWindow::~CInfoWindow() = default;
  170. void CInfoWindow::showAll(Canvas & to)
  171. {
  172. CSimpleWindow::show(to);
  173. CIntObject::showAll(to);
  174. }
  175. void CInfoWindow::showInfoDialog(const std::string &text, const TCompsInfo & components, PlayerColor player)
  176. {
  177. GH.windows().pushWindow(CInfoWindow::create(text, player, components));
  178. }
  179. void CInfoWindow::showYesNoDialog(const std::string & text, const TCompsInfo & components, const CFunctionList<void( ) > &onYes, const CFunctionList<void()> &onNo, PlayerColor player)
  180. {
  181. assert(!LOCPLINT || LOCPLINT->showingDialog->get());
  182. std::vector<std::pair<AnimationPath,CFunctionList<void()> > > pom;
  183. pom.push_back( { AnimationPath::builtin("IOKAY.DEF"), 0 });
  184. pom.push_back( { AnimationPath::builtin("ICANCEL.DEF"), 0 });
  185. std::shared_ptr<CInfoWindow> temp = std::make_shared<CInfoWindow>(text, player, components, pom);
  186. temp->buttons[0]->addCallback( onYes );
  187. temp->buttons[1]->addCallback( onNo );
  188. GH.windows().pushWindow(temp);
  189. }
  190. std::shared_ptr<CInfoWindow> CInfoWindow::create(const std::string &text, PlayerColor playerID, const TCompsInfo & components)
  191. {
  192. std::vector<std::pair<AnimationPath,CFunctionList<void()> > > pom;
  193. pom.push_back({AnimationPath::builtin("IOKAY.DEF"), 0});
  194. return std::make_shared<CInfoWindow>(text, playerID, components, pom);
  195. }
  196. std::string CInfoWindow::genText(std::string title, std::string description)
  197. {
  198. return std::string("{") + title + "}" + "\n\n" + description;
  199. }
  200. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  201. :free(Free),bitmap(Bitmap)
  202. {
  203. init(x, y);
  204. }
  205. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, const Point &p, ETextAlignment alignment, bool Free)
  206. : free(Free),bitmap(Bitmap)
  207. {
  208. switch(alignment)
  209. {
  210. case ETextAlignment::BOTTOMRIGHT:
  211. init(p.x - Bitmap->w, p.y - Bitmap->h);
  212. break;
  213. case ETextAlignment::CENTER:
  214. init(p.x - Bitmap->w/2, p.y - Bitmap->h/2);
  215. break;
  216. case ETextAlignment::TOPLEFT:
  217. init(p.x, p.y);
  218. break;
  219. case ETextAlignment::TOPCENTER:
  220. init(p.x - Bitmap->w/2, p.y);
  221. break;
  222. default:
  223. assert(0); //not implemented
  224. }
  225. }
  226. CInfoPopup::CInfoPopup(SDL_Surface *Bitmap, bool Free)
  227. {
  228. CCS->curh->hide();
  229. free=Free;
  230. bitmap=Bitmap;
  231. if(bitmap)
  232. {
  233. pos.x = GH.screenDimensions().x / 2 - bitmap->w / 2;
  234. pos.y = GH.screenDimensions().y / 2 - bitmap->h / 2;
  235. pos.h = bitmap->h;
  236. pos.w = bitmap->w;
  237. }
  238. }
  239. void CInfoPopup::close()
  240. {
  241. if(free)
  242. SDL_FreeSurface(bitmap);
  243. WindowBase::close();
  244. }
  245. void CInfoPopup::show(Canvas & to)
  246. {
  247. CSDL_Ext::blitAt(bitmap,pos.x,pos.y,to.getInternalSurface());
  248. }
  249. CInfoPopup::~CInfoPopup()
  250. {
  251. CCS->curh->show();
  252. }
  253. void CInfoPopup::init(int x, int y)
  254. {
  255. CCS->curh->hide();
  256. pos.x = x;
  257. pos.y = y;
  258. pos.h = bitmap->h;
  259. pos.w = bitmap->w;
  260. // Put the window back on screen if necessary
  261. vstd::amax(pos.x, 0);
  262. vstd::amax(pos.y, 0);
  263. vstd::amin(pos.x, GH.screenDimensions().x - bitmap->w);
  264. vstd::amin(pos.y, GH.screenDimensions().y - bitmap->h);
  265. }
  266. bool CRClickPopup::isPopupWindow() const
  267. {
  268. return true;
  269. }
  270. void CRClickPopup::close()
  271. {
  272. WindowBase::close();
  273. }
  274. void CRClickPopup::createAndPush(const std::string &txt, const CInfoWindow::TCompsInfo &comps)
  275. {
  276. PlayerColor player = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1); //if no player, then use blue
  277. if(settings["session"]["spectate"].Bool())//TODO: there must be better way to implement this
  278. player = PlayerColor(1);
  279. auto temp = std::make_shared<CInfoWindow>(txt, player, comps);
  280. temp->center(GH.getCursorPosition()); //center on mouse
  281. #ifdef VCMI_MOBILE
  282. temp->moveBy({0, -temp->pos.h / 2});
  283. #endif
  284. temp->fitToScreen(10);
  285. GH.windows().createAndPushWindow<CRClickPopupInt>(temp);
  286. }
  287. void CRClickPopup::createAndPush(const std::string & txt, std::shared_ptr<CComponent> component)
  288. {
  289. CInfoWindow::TCompsInfo intComps;
  290. intComps.push_back(component);
  291. createAndPush(txt, intComps);
  292. }
  293. void CRClickPopup::createAndPush(const CGObjectInstance * obj, const Point & p, ETextAlignment alignment)
  294. {
  295. auto iWin = createCustomInfoWindow(p, obj); //try get custom infowindow for this obj
  296. if(iWin)
  297. {
  298. GH.windows().pushWindow(iWin);
  299. }
  300. else
  301. {
  302. std::vector<Component> components;
  303. if (settings["general"]["enableUiEnhancements"].Bool())
  304. {
  305. if(LOCPLINT->localState->getCurrentHero())
  306. components = obj->getPopupComponents(LOCPLINT->localState->getCurrentHero());
  307. else
  308. components = obj->getPopupComponents(LOCPLINT->playerID);
  309. }
  310. std::vector<std::shared_ptr<CComponent>> guiComponents;
  311. for (auto & component : components)
  312. guiComponents.push_back(std::make_shared<CComponent>(component));
  313. if(LOCPLINT->localState->getCurrentHero())
  314. CRClickPopup::createAndPush(obj->getPopupText(LOCPLINT->localState->getCurrentHero()), guiComponents);
  315. else
  316. CRClickPopup::createAndPush(obj->getPopupText(LOCPLINT->playerID), guiComponents);
  317. }
  318. }
  319. CRClickPopupInt::CRClickPopupInt(std::shared_ptr<CIntObject> our)
  320. {
  321. CCS->curh->hide();
  322. defActions = SHOWALL | UPDATE;
  323. our->recActions = defActions;
  324. inner = our;
  325. addChild(our.get(), false);
  326. }
  327. CRClickPopupInt::~CRClickPopupInt()
  328. {
  329. CCS->curh->show();
  330. }
  331. Point CInfoBoxPopup::toScreen(Point p)
  332. {
  333. auto bounds = adventureInt->terrainAreaPixels();
  334. vstd::abetween(p.x, bounds.top() + 100, bounds.bottom() - 100);
  335. vstd::abetween(p.y, bounds.left() + 100, bounds.right() - 100);
  336. return p;
  337. }
  338. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGTownInstance * town)
  339. : CWindowObject(RCLICK_POPUP | PLAYER_COLORED, ImagePath::builtin("TOWNQVBK"), toScreen(position))
  340. {
  341. InfoAboutTown iah;
  342. LOCPLINT->cb->getTownInfo(town, iah, LOCPLINT->localState->getCurrentTown()); //todo: should this be nearest hero?
  343. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  344. tooltip = std::make_shared<CTownTooltip>(Point(9, 10), iah);
  345. }
  346. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGHeroInstance * hero)
  347. : CWindowObject(RCLICK_POPUP | PLAYER_COLORED, ImagePath::builtin("HEROQVBK"), toScreen(position))
  348. {
  349. InfoAboutHero iah;
  350. LOCPLINT->cb->getHeroInfo(hero, iah, LOCPLINT->localState->getCurrentHero());//todo: should this be nearest hero?
  351. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  352. tooltip = std::make_shared<CHeroTooltip>(Point(9, 10), iah);
  353. }
  354. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGGarrison * garr)
  355. : CWindowObject(RCLICK_POPUP | PLAYER_COLORED, ImagePath::builtin("TOWNQVBK"), toScreen(position))
  356. {
  357. InfoAboutTown iah;
  358. LOCPLINT->cb->getTownInfo(garr, iah);
  359. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  360. tooltip = std::make_shared<CArmyTooltip>(Point(9, 10), iah);
  361. }
  362. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGCreature * creature)
  363. : CWindowObject(RCLICK_POPUP | BORDERED, ImagePath::builtin("DIBOXBCK"), toScreen(position))
  364. {
  365. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  366. tooltip = std::make_shared<CreatureTooltip>(Point(9, 10), creature);
  367. }
  368. std::shared_ptr<WindowBase> CRClickPopup::createCustomInfoWindow(Point position, const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  369. {
  370. if(nullptr == specific)
  371. specific = LOCPLINT->localState->getCurrentArmy();
  372. if(nullptr == specific)
  373. {
  374. logGlobal->error("createCustomInfoWindow: no object to describe");
  375. return nullptr;
  376. }
  377. switch(specific->ID)
  378. {
  379. case Obj::HERO:
  380. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGHeroInstance *>(specific));
  381. case Obj::TOWN:
  382. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGTownInstance *>(specific));
  383. case Obj::MONSTER:
  384. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGCreature *>(specific));
  385. case Obj::GARRISON:
  386. case Obj::GARRISON2:
  387. return std::make_shared<CInfoBoxPopup>(position, dynamic_cast<const CGGarrison *>(specific));
  388. default:
  389. return std::shared_ptr<WindowBase>();
  390. }
  391. }