InfoWindows.cpp 11 KB

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