InfoWindows.cpp 11 KB

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