GUIClasses.cpp 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889
  1. #include "StdInc.h"
  2. #include "GUIClasses.h"
  3. #include "gui/SDL_Extensions.h"
  4. #include "CAdvmapInterface.h"
  5. #include "CBitmapHandler.h"
  6. #include "CDefHandler.h"
  7. #include "battle/CBattleInterface.h"
  8. #include "battle/CBattleInterfaceClasses.h"
  9. #include "../CCallback.h"
  10. #include "CCastleInterface.h"
  11. #include "CCreatureWindow.h"
  12. #include "gui/CCursorHandler.h"
  13. #include "CGameInfo.h"
  14. #include "CHeroWindow.h"
  15. #include "CMessage.h"
  16. #include "../lib/CConfigHandler.h"
  17. #include "battle/CCreatureAnimation.h"
  18. #include "CPlayerInterface.h"
  19. #include "Graphics.h"
  20. #include "CAnimation.h"
  21. #include "../lib/CArtHandler.h"
  22. #include "../lib/CBuildingHandler.h"
  23. #include "../lib/CGeneralTextHandler.h"
  24. #include "../lib/CHeroHandler.h"
  25. #include "../lib/CModHandler.h"
  26. #include "../lib/CSpellHandler.h"
  27. #include "../lib/CTownHandler.h"
  28. #include "../lib/CondSh.h"
  29. #include "../lib/mapping/CMap.h"
  30. #include "mapHandler.h"
  31. #include "../lib/CStopWatch.h"
  32. #include "../lib/NetPacksBase.h"
  33. #include "CSpellWindow.h"
  34. #include "CHeroWindow.h"
  35. #include "CVideoHandler.h"
  36. #include "../lib/StartInfo.h"
  37. #include "CPreGame.h"
  38. #include "../lib/HeroBonus.h"
  39. #include "../lib/CCreatureHandler.h"
  40. #include "CMusicHandler.h"
  41. #include "../lib/BattleState.h"
  42. #include "../lib/CGameState.h"
  43. #include "../lib/GameConstants.h"
  44. #include "gui/CGuiHandler.h"
  45. /*
  46. * GUIClasses.cpp, part of VCMI engine
  47. *
  48. * Authors: listed in file AUTHORS in main folder
  49. *
  50. * License: GNU General Public License v2.0 or later
  51. * Full text of license available in license.txt file, in main folder
  52. *
  53. */
  54. using namespace boost::assign;
  55. using namespace CSDL_Ext;
  56. extern std::queue<SDL_Event> events;
  57. extern boost::mutex eventsM;
  58. std::list<CFocusable*> CFocusable::focusables;
  59. CFocusable * CFocusable::inputWithFocus;
  60. #undef min
  61. #undef max
  62. void CSelWindow::selectionChange(unsigned to)
  63. {
  64. for (unsigned i=0;i<components.size();i++)
  65. {
  66. CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
  67. if (!pom)
  68. continue;
  69. pom->select(i==to);
  70. }
  71. redraw();
  72. }
  73. CSelWindow::CSelWindow(const std::string &Text, PlayerColor player, int charperline, const std::vector<CSelectableComponent*> &comps, const std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons, QueryID askID)
  74. {
  75. OBJ_CONSTRUCTION_CAPTURING_ALL;
  76. ID = askID;
  77. for(int i=0;i<Buttons.size();i++)
  78. {
  79. buttons.push_back(new CAdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  80. if(!i && askID.getNum() >= 0)
  81. buttons.back()->callback += boost::bind(&CSelWindow::madeChoice,this);
  82. buttons[i]->callback += boost::bind(&CInfoWindow::close,this); //each button will close the window apart from call-defined actions
  83. }
  84. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::WHITE);
  85. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  86. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  87. if(buttons.size() > 1 && askID.getNum() >= 0) //cancel button functionality
  88. buttons.back()->callback += boost::bind(&CCallback::selectionMade,LOCPLINT->cb.get(),0,askID);
  89. for(int i=0;i<comps.size();i++)
  90. {
  91. comps[i]->recActions = 255;
  92. addChild(comps[i]);
  93. components.push_back(comps[i]);
  94. comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
  95. if(i<9)
  96. comps[i]->assignedKeys.insert(SDLK_1+i);
  97. }
  98. CMessage::drawIWindow(this, Text, player);
  99. }
  100. void CSelWindow::madeChoice()
  101. {
  102. if(ID.getNum() < 0)
  103. return;
  104. int ret = -1;
  105. for (int i=0;i<components.size();i++)
  106. {
  107. if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
  108. {
  109. ret = i;
  110. }
  111. }
  112. LOCPLINT->cb->selectionMade(ret+1,ID);
  113. }
  114. void CArmyTooltip::init(const InfoAboutArmy &army)
  115. {
  116. OBJ_CONSTRUCTION_CAPTURING_ALL;
  117. new CLabel(66, 2, FONT_SMALL, TOPLEFT, Colors::WHITE, army.name);
  118. std::vector<Point> slotsPos;
  119. slotsPos.push_back(Point(36,73));
  120. slotsPos.push_back(Point(72,73));
  121. slotsPos.push_back(Point(108,73));
  122. slotsPos.push_back(Point(18,122));
  123. slotsPos.push_back(Point(54,122));
  124. slotsPos.push_back(Point(90,122));
  125. slotsPos.push_back(Point(126,122));
  126. for(auto & slot : army.army)
  127. {
  128. if(slot.first.getNum() >= GameConstants::ARMY_SIZE)
  129. {
  130. logGlobal->warnStream() << "Warning: " << army.name << " has stack in slot " << slot.first;
  131. continue;
  132. }
  133. new CAnimImage("CPRSMALL", slot.second.type->iconIndex, 0, slotsPos[slot.first.getNum()].x, slotsPos[slot.first.getNum()].y);
  134. std::string subtitle;
  135. if(army.army.isDetailed)
  136. subtitle = boost::lexical_cast<std::string>(slot.second.count);
  137. else
  138. {
  139. //if =0 - we have no information about stack size at all
  140. if (slot.second.count)
  141. subtitle = CGI->generaltexth->arraytxt[171 + 3*(slot.second.count)];
  142. }
  143. new CLabel(slotsPos[slot.first.getNum()].x + 17, slotsPos[slot.first.getNum()].y + 41, FONT_TINY, CENTER, Colors::WHITE, subtitle);
  144. }
  145. }
  146. CArmyTooltip::CArmyTooltip(Point pos, const InfoAboutArmy &army):
  147. CIntObject(0, pos)
  148. {
  149. init(army);
  150. }
  151. CArmyTooltip::CArmyTooltip(Point pos, const CArmedInstance * army):
  152. CIntObject(0, pos)
  153. {
  154. init(InfoAboutArmy(army, true));
  155. }
  156. void CHeroTooltip::init(const InfoAboutHero &hero)
  157. {
  158. OBJ_CONSTRUCTION_CAPTURING_ALL;
  159. new CAnimImage("PortraitsLarge", hero.portrait, 0, 3, 2);
  160. if(hero.details)
  161. {
  162. for (size_t i = 0; i < hero.details->primskills.size(); i++)
  163. new CLabel(75 + 28 * i, 58, FONT_SMALL, CENTER, Colors::WHITE,
  164. boost::lexical_cast<std::string>(hero.details->primskills[i]));
  165. new CLabel(158, 98, FONT_TINY, CENTER, Colors::WHITE,
  166. boost::lexical_cast<std::string>(hero.details->mana));
  167. new CAnimImage("IMRL22", hero.details->morale + 3, 0, 5, 74);
  168. new CAnimImage("ILCK22", hero.details->luck + 3, 0, 5, 91);
  169. }
  170. }
  171. CHeroTooltip::CHeroTooltip(Point pos, const InfoAboutHero &hero):
  172. CArmyTooltip(pos, hero)
  173. {
  174. init(hero);
  175. }
  176. CHeroTooltip::CHeroTooltip(Point pos, const CGHeroInstance * hero):
  177. CArmyTooltip(pos, InfoAboutHero(hero, true))
  178. {
  179. init(InfoAboutHero(hero, true));
  180. }
  181. void CTownTooltip::init(const InfoAboutTown &town)
  182. {
  183. OBJ_CONSTRUCTION_CAPTURING_ALL;
  184. //order of icons in def: fort, citadel, castle, no fort
  185. size_t fortIndex = town.fortLevel ? town.fortLevel - 1 : 3;
  186. new CAnimImage("ITMCLS", fortIndex, 0, 105, 31);
  187. assert(town.tType);
  188. size_t iconIndex = town.tType->clientInfo.icons[town.fortLevel > 0][town.built >= CGI->modh->settings.MAX_BUILDING_PER_TURN];
  189. new CAnimImage("itpt", iconIndex, 0, 3, 2);
  190. if(town.details)
  191. {
  192. new CAnimImage("ITMTLS", town.details->hallLevel, 0, 67, 31);
  193. if (town.details->goldIncome)
  194. new CLabel(157, 58, FONT_TINY, CENTER, Colors::WHITE,
  195. boost::lexical_cast<std::string>(town.details->goldIncome));
  196. if(town.details->garrisonedHero) //garrisoned hero icon
  197. new CPicture("TOWNQKGH", 149, 76);
  198. if(town.details->customRes)//silo is built
  199. {
  200. if (town.tType->primaryRes == Res::WOOD_AND_ORE )// wood & ore
  201. {
  202. new CAnimImage("SMALRES", Res::WOOD, 0, 7, 75);
  203. new CAnimImage("SMALRES", Res::ORE , 0, 7, 88);
  204. }
  205. else
  206. new CAnimImage("SMALRES", town.tType->primaryRes, 0, 7, 81);
  207. }
  208. }
  209. }
  210. CTownTooltip::CTownTooltip(Point pos, const InfoAboutTown &town):
  211. CArmyTooltip(pos, town)
  212. {
  213. init(town);
  214. }
  215. CTownTooltip::CTownTooltip(Point pos, const CGTownInstance * town):
  216. CArmyTooltip(pos, InfoAboutTown(town, true))
  217. {
  218. init(InfoAboutTown(town, true));
  219. }
  220. CInfoWindow::CInfoWindow(std::string Text, PlayerColor player, const TCompsInfo &comps, const TButtonsInfo &Buttons, bool delComps)
  221. {
  222. OBJ_CONSTRUCTION_CAPTURING_ALL;
  223. type |= BLOCK_ADV_HOTKEYS;
  224. ID = QueryID(-1);
  225. for(auto & Button : Buttons)
  226. {
  227. CAdventureMapButton *button = new CAdventureMapButton("","",boost::bind(&CInfoWindow::close,this),0,0,Button.first);
  228. button->borderColor = Colors::METALLIC_GOLD;
  229. button->borderEnabled = true;
  230. button->callback.add(Button.second); //each button will close the window apart from call-defined actions
  231. buttons.push_back(button);
  232. }
  233. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::WHITE);
  234. if(!text->slider)
  235. {
  236. text->resize(text->label->textSize);
  237. }
  238. if(buttons.size())
  239. {
  240. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  241. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  242. }
  243. for(auto & comp : comps)
  244. {
  245. comp->recActions = 0xff;
  246. addChild(comp);
  247. comp->recActions &= ~(SHOWALL | UPDATE);
  248. components.push_back(comp);
  249. }
  250. setDelComps(delComps);
  251. CMessage::drawIWindow(this,Text,player);
  252. }
  253. CInfoWindow::CInfoWindow()
  254. {
  255. ID = QueryID(-1);
  256. setDelComps(false);
  257. text = nullptr;
  258. }
  259. void CInfoWindow::close()
  260. {
  261. GH.popIntTotally(this);
  262. if(LOCPLINT)
  263. LOCPLINT->showingDialog->setn(false);
  264. }
  265. void CInfoWindow::show(SDL_Surface * to)
  266. {
  267. CIntObject::show(to);
  268. }
  269. CInfoWindow::~CInfoWindow()
  270. {
  271. if(!delComps)
  272. {
  273. for (auto & elem : components)
  274. removeChild(elem);
  275. }
  276. }
  277. void CInfoWindow::showAll(SDL_Surface * to)
  278. {
  279. CSimpleWindow::show(to);
  280. CIntObject::showAll(to);
  281. }
  282. void CInfoWindow::showInfoDialog(const std::string &text, const std::vector<CComponent *> *components, bool DelComps, PlayerColor player)
  283. {
  284. CInfoWindow * window = CInfoWindow::create(text, player, components, DelComps);
  285. GH.pushInt(window);
  286. }
  287. void CInfoWindow::showYesNoDialog(const std::string & text, const std::vector<CComponent*> *components, const CFunctionList<void( ) > &onYes, const CFunctionList<void()> &onNo, bool DelComps, PlayerColor player)
  288. {
  289. assert(!LOCPLINT || LOCPLINT->showingDialog->get());
  290. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  291. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  292. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  293. CInfoWindow * temp = new CInfoWindow(text, player, components ? *components : std::vector<CComponent*>(), pom, DelComps);
  294. for(auto & elem : onYes.funcs)
  295. temp->buttons[0]->callback += elem;
  296. for(auto & elem : onNo.funcs)
  297. temp->buttons[1]->callback += elem;
  298. GH.pushInt(temp);
  299. }
  300. void CInfoWindow::showOkDialog(const std::string & text, const std::vector<CComponent*> *components, const boost::function<void()> & onOk, bool delComps, PlayerColor player)
  301. {
  302. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  303. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  304. CInfoWindow * temp = new CInfoWindow(text, player, *components, pom, delComps);
  305. temp->buttons[0]->callback += onOk;
  306. GH.pushInt(temp);
  307. }
  308. CInfoWindow * CInfoWindow::create(const std::string &text, PlayerColor playerID /*= 1*/, const std::vector<CComponent*> *components /*= nullptr*/, bool DelComps)
  309. {
  310. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  311. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  312. CInfoWindow * ret = new CInfoWindow(text, playerID, components ? *components : std::vector<CComponent*>(), pom, DelComps);
  313. return ret;
  314. }
  315. std::string CInfoWindow::genText(std::string title, std::string description)
  316. {
  317. return std::string("{") + title + "}" + "\n\n" + description;
  318. }
  319. void CInfoWindow::setDelComps(bool DelComps)
  320. {
  321. delComps = DelComps;
  322. for(CComponent *comp : components)
  323. {
  324. if(delComps)
  325. comp->recActions |= DISPOSE;
  326. else
  327. comp->recActions &= ~DISPOSE;
  328. }
  329. }
  330. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  331. :free(Free),bitmap(Bitmap)
  332. {
  333. init(x, y);
  334. }
  335. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, const Point &p, EAlignment alignment, bool Free/*=false*/)
  336. : free(Free),bitmap(Bitmap)
  337. {
  338. switch(alignment)
  339. {
  340. case BOTTOMRIGHT:
  341. init(p.x - Bitmap->w, p.y - Bitmap->h);
  342. break;
  343. case CENTER:
  344. init(p.x - Bitmap->w/2, p.y - Bitmap->h/2);
  345. break;
  346. case TOPLEFT:
  347. init(p.x, p.y);
  348. break;
  349. default:
  350. assert(0); //not implemented
  351. }
  352. }
  353. CInfoPopup::CInfoPopup(SDL_Surface *Bitmap, bool Free)
  354. {
  355. CCS->curh->hide();
  356. free=Free;
  357. bitmap=Bitmap;
  358. if(bitmap)
  359. {
  360. pos.x = screen->w/2 - bitmap->w/2;
  361. pos.y = screen->h/2 - bitmap->h/2;
  362. pos.h = bitmap->h;
  363. pos.w = bitmap->w;
  364. }
  365. }
  366. void CInfoPopup::close()
  367. {
  368. if(free)
  369. SDL_FreeSurface(bitmap);
  370. GH.popIntTotally(this);
  371. }
  372. void CInfoPopup::show(SDL_Surface * to)
  373. {
  374. blitAt(bitmap,pos.x,pos.y,to);
  375. }
  376. CInfoPopup::~CInfoPopup()
  377. {
  378. CCS->curh->show();
  379. }
  380. void CInfoPopup::init(int x, int y)
  381. {
  382. CCS->curh->hide();
  383. pos.x = x;
  384. pos.y = y;
  385. pos.h = bitmap->h;
  386. pos.w = bitmap->w;
  387. // Put the window back on screen if necessary
  388. vstd::amax(pos.x, 0);
  389. vstd::amax(pos.y, 0);
  390. vstd::amin(pos.x, screen->w - bitmap->w);
  391. vstd::amin(pos.y, screen->h - bitmap->h);
  392. }
  393. CCreaturePic::CCreaturePic(int x, int y, const CCreature *cre, bool Big, bool Animated)
  394. {
  395. OBJ_CONSTRUCTION_CAPTURING_ALL;
  396. pos.x+=x;
  397. pos.y+=y;
  398. TFaction faction = cre->faction;
  399. assert(CGI->townh->factions.size() > faction);
  400. if(Big)
  401. bg = new CPicture(CGI->townh->factions[faction]->creatureBg130);
  402. else
  403. bg = new CPicture(CGI->townh->factions[faction]->creatureBg120);
  404. bg->needRefresh = true;
  405. anim = new CCreatureAnim(0, 0, cre->animDefName, Rect());
  406. anim->clipRect(cre->isDoubleWide()?170:150, 155, bg->pos.w, bg->pos.h);
  407. anim->startPreview(cre->hasBonusOfType(Bonus::SIEGE_WEAPON));
  408. pos.w = bg->pos.w;
  409. pos.h = bg->pos.h;
  410. }
  411. CRecruitmentWindow::CCreatureCard::CCreatureCard(CRecruitmentWindow *window, const CCreature *crea, int totalAmount):
  412. CIntObject(LCLICK | RCLICK),
  413. parent(window),
  414. selected(false),
  415. creature(crea),
  416. amount(totalAmount)
  417. {
  418. OBJ_CONSTRUCTION_CAPTURING_ALL;
  419. pic = new CCreaturePic(1,1, creature, true, true);
  420. // 1 + 1 px for borders
  421. pos.w = pic->pos.w + 2;
  422. pos.h = pic->pos.h + 2;
  423. }
  424. void CRecruitmentWindow::CCreatureCard::select(bool on)
  425. {
  426. selected = on;
  427. redraw();
  428. }
  429. void CRecruitmentWindow::CCreatureCard::clickLeft(tribool down, bool previousState)
  430. {
  431. if (down)
  432. parent->select(this);
  433. }
  434. void CRecruitmentWindow::CCreatureCard::clickRight(tribool down, bool previousState)
  435. {
  436. if (down)
  437. GH.pushInt(createCreWindow(creature->idNumber, CCreatureWindow::OTHER, 0));
  438. }
  439. void CRecruitmentWindow::CCreatureCard::showAll(SDL_Surface * to)
  440. {
  441. CIntObject::showAll(to);
  442. if (selected)
  443. drawBorder(to, pos, int3(248, 0, 0));
  444. else
  445. drawBorder(to, pos, int3(232, 212, 120));
  446. }
  447. CRecruitmentWindow::CCostBox::CCostBox(Rect position, std::string title)
  448. {
  449. type |= REDRAW_PARENT;
  450. pos = position + pos;
  451. OBJ_CONSTRUCTION_CAPTURING_ALL;
  452. new CLabel(pos.w/2, 10, FONT_SMALL, CENTER, Colors::WHITE, title);
  453. }
  454. void CRecruitmentWindow::CCostBox::set(TResources res)
  455. {
  456. //just update values
  457. for(auto & item : resources)
  458. {
  459. item.second.first->setText(boost::lexical_cast<std::string>(res[item.first]));
  460. }
  461. }
  462. void CRecruitmentWindow::CCostBox::createItems(TResources res)
  463. {
  464. OBJ_CONSTRUCTION_CAPTURING_ALL;
  465. for(auto & curr : resources)
  466. {
  467. delete curr.second.first;
  468. delete curr.second.second;
  469. }
  470. resources.clear();
  471. TResources::nziterator iter(res);
  472. while (iter.valid())
  473. {
  474. CAnimImage * image = new CAnimImage("RESOURCE", iter->resType);
  475. CLabel * text = new CLabel(15, 43, FONT_SMALL, CENTER, Colors::WHITE, "0");
  476. resources.insert(std::make_pair(iter->resType, std::make_pair(text, image)));
  477. iter++;
  478. }
  479. if (!resources.empty())
  480. {
  481. int curx = pos.w / 2 - (16 * resources.size()) - (8 * (resources.size() - 1));
  482. //reverse to display gold as first resource
  483. for (auto & res : boost::adaptors::reverse(resources))
  484. {
  485. res.second.first->moveBy(Point(curx, 22));
  486. res.second.second->moveBy(Point(curx, 22));
  487. curx += 48;
  488. }
  489. }
  490. redraw();
  491. }
  492. void CRecruitmentWindow::select(CCreatureCard *card)
  493. {
  494. if (card == selected)
  495. return;
  496. if (selected)
  497. selected->select(false);
  498. selected = card;
  499. if (selected)
  500. selected->select(true);
  501. if (card)
  502. {
  503. si32 maxAmount = card->creature->maxAmount(LOCPLINT->cb->getResourceAmount());
  504. vstd::amin(maxAmount, card->amount);
  505. slider->setAmount(maxAmount);
  506. if(slider->value != maxAmount)
  507. slider->moveTo(maxAmount);
  508. else // if slider already at 0 - emulate call to sliderMoved()
  509. sliderMoved(maxAmount);
  510. costPerTroopValue->createItems(card->creature->cost);
  511. totalCostValue->createItems(card->creature->cost);
  512. costPerTroopValue->set(card->creature->cost);
  513. totalCostValue->set(card->creature->cost * maxAmount);
  514. //Recruit %s
  515. title->setText(boost::str(boost::format(CGI->generaltexth->tcommands[21]) % card->creature->namePl));
  516. maxButton->block(maxAmount == 0);
  517. slider->block(maxAmount == 0);
  518. }
  519. }
  520. void CRecruitmentWindow::buy()
  521. {
  522. CreatureID crid = selected->creature->idNumber;
  523. SlotID dstslot = dst-> getSlotFor(crid);
  524. if(!dstslot.validSlot() && !vstd::contains(CGI->arth->bigArtifacts,CGI->arth->creatureToMachineID(crid))) //no available slot
  525. {
  526. std::string txt;
  527. if(dst->ID == Obj::HERO)
  528. {
  529. txt = CGI->generaltexth->allTexts[425]; //The %s would join your hero, but there aren't enough provisions to support them.
  530. boost::algorithm::replace_first(txt, "%s", slider->value > 1 ? CGI->creh->creatures[crid]->namePl : CGI->creh->creatures[crid]->nameSing);
  531. }
  532. else
  533. {
  534. txt = CGI->generaltexth->allTexts[17]; //There is no room in the garrison for this army.
  535. }
  536. LOCPLINT->showInfoDialog(txt);
  537. return;
  538. }
  539. onRecruit(crid, slider->value);
  540. if(level >= 0)
  541. close();
  542. }
  543. void CRecruitmentWindow::showAll(SDL_Surface * to)
  544. {
  545. CWindowObject::showAll(to);
  546. // recruit\total values
  547. drawBorder(to, pos.x + 172, pos.y + 222, 67, 42, int3(239,215,123));
  548. drawBorder(to, pos.x + 246, pos.y + 222, 67, 42, int3(239,215,123));
  549. //cost boxes
  550. drawBorder(to, pos.x + 64, pos.y + 222, 99, 76, int3(239,215,123));
  551. drawBorder(to, pos.x + 322, pos.y + 222, 99, 76, int3(239,215,123));
  552. //buttons borders
  553. drawBorder(to, pos.x + 133, pos.y + 312, 66, 34, int3(173,142,66));
  554. drawBorder(to, pos.x + 211, pos.y + 312, 66, 34, int3(173,142,66));
  555. drawBorder(to, pos.x + 289, pos.y + 312, 66, 34, int3(173,142,66));
  556. }
  557. CRecruitmentWindow::CRecruitmentWindow(const CGDwelling *Dwelling, int Level, const CArmedInstance *Dst, const std::function<void(CreatureID,int)> &Recruit, int y_offset):
  558. CWindowObject(PLAYER_COLORED, "TPRCRT"),
  559. onRecruit(Recruit),
  560. level(Level),
  561. dst(Dst),
  562. selected(nullptr),
  563. dwelling(Dwelling)
  564. {
  565. moveBy(Point(0, y_offset));
  566. OBJ_CONSTRUCTION_CAPTURING_ALL;
  567. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  568. slider = new CSlider(176,279,135,nullptr,0,0,0,true);
  569. slider->moved = boost::bind(&CRecruitmentWindow::sliderMoved,this, _1);
  570. maxButton = new CAdventureMapButton(CGI->generaltexth->zelp[553],boost::bind(&CSlider::moveToMax,slider),134,313,"IRCBTNS.DEF",SDLK_m);
  571. buyButton = new CAdventureMapButton(CGI->generaltexth->zelp[554],boost::bind(&CRecruitmentWindow::buy,this),212,313,"IBY6432.DEF",SDLK_RETURN);
  572. cancelButton = new CAdventureMapButton(CGI->generaltexth->zelp[555],boost::bind(&CRecruitmentWindow::close,this),290,313,"ICN6432.DEF",SDLK_ESCAPE);
  573. title = new CLabel(243, 32, FONT_BIG, CENTER, Colors::YELLOW);
  574. availableValue = new CLabel(205, 253, FONT_SMALL, CENTER, Colors::WHITE);
  575. toRecruitValue = new CLabel(279, 253, FONT_SMALL, CENTER, Colors::WHITE);
  576. costPerTroopValue = new CCostBox(Rect(65, 222, 97, 74), CGI->generaltexth->allTexts[346]);
  577. totalCostValue = new CCostBox(Rect(323, 222, 97, 74), CGI->generaltexth->allTexts[466]);
  578. new CLabel(205, 233, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[465]); //available t
  579. new CLabel(279, 233, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[16]); //recruit t
  580. availableCreaturesChanged();
  581. }
  582. void CRecruitmentWindow::availableCreaturesChanged()
  583. {
  584. OBJ_CONSTRUCTION_CAPTURING_ALL;
  585. size_t selectedIndex = 0;
  586. if (!cards.empty() && selected) // find position of selected item
  587. selectedIndex = std::find(cards.begin(), cards.end(), selected) - cards.begin();
  588. //deselect card
  589. select(nullptr);
  590. //delete old cards
  591. for(auto & card : cards)
  592. delete card;
  593. cards.clear();
  594. for(int i=0; i<dwelling->creatures.size(); i++)
  595. {
  596. //find appropriate level
  597. if(level >= 0 && i != level)
  598. continue;
  599. int amount = dwelling->creatures[i].first;
  600. //create new cards
  601. for(auto & creature : boost::adaptors::reverse(dwelling->creatures[i].second))
  602. cards.push_back(new CCreatureCard(this, CGI->creh->creatures[creature], amount));
  603. }
  604. assert(!cards.empty());
  605. const int creatureWidth = 102;
  606. //normal distance between cards - 18px
  607. int requiredSpace = 18;
  608. //maximum distance we can use without reaching window borders
  609. int availableSpace = pos.w - 50 - creatureWidth * cards.size();
  610. if (cards.size() > 1) // avoid division by zero
  611. availableSpace /= cards.size() - 1;
  612. else
  613. availableSpace = 0;
  614. assert(availableSpace >= 0);
  615. const int spaceBetween = std::min(requiredSpace, availableSpace);
  616. const int totalCreatureWidth = spaceBetween + creatureWidth;
  617. //now we know total amount of cards and can move them to correct position
  618. int curx = pos.w / 2 - (creatureWidth*cards.size()/2) - (spaceBetween*(cards.size()-1)/2);
  619. for(auto & card : cards)
  620. {
  621. card->moveBy(Point(curx, 64));
  622. curx += totalCreatureWidth;
  623. }
  624. //restore selection
  625. select(cards[selectedIndex]);
  626. if(slider->value == slider->amount)
  627. slider->moveTo(slider->amount);
  628. else // if slider already at 0 - emulate call to sliderMoved()
  629. sliderMoved(slider->amount);
  630. }
  631. void CRecruitmentWindow::sliderMoved(int to)
  632. {
  633. if (!selected)
  634. return;
  635. buyButton->block(!to);
  636. availableValue->setText(boost::lexical_cast<std::string>(selected->amount - to));
  637. toRecruitValue->setText(boost::lexical_cast<std::string>(to));
  638. totalCostValue->set(selected->creature->cost * to);
  639. }
  640. CSplitWindow::CSplitWindow(const CCreature * creature, std::function<void(int, int)> callback_,
  641. int leftMin_, int rightMin_, int leftAmount_, int rightAmount_):
  642. CWindowObject(PLAYER_COLORED, "GPUCRDIV"),
  643. callback(callback_),
  644. leftAmount(leftAmount_),
  645. rightAmount(rightAmount_),
  646. leftMin(leftMin_),
  647. rightMin(rightMin_),
  648. slider(nullptr)
  649. {
  650. OBJ_CONSTRUCTION_CAPTURING_ALL;
  651. int total = leftAmount + rightAmount;
  652. int leftMax = total - rightMin;
  653. int rightMax = total - leftMin;
  654. ok = new CAdventureMapButton("", "", boost::bind(&CSplitWindow::apply, this), 20, 263, "IOK6432", SDLK_RETURN);
  655. cancel = new CAdventureMapButton("", "", boost::bind(&CSplitWindow::close, this), 214, 263, "ICN6432", SDLK_ESCAPE);
  656. int sliderPositions = total - leftMin - rightMin;
  657. leftInput = new CTextInput(Rect(20, 218, 100, 36), FONT_BIG, boost::bind(&CSplitWindow::setAmountText, this, _1, true));
  658. rightInput = new CTextInput(Rect(176, 218, 100, 36), FONT_BIG, boost::bind(&CSplitWindow::setAmountText, this, _1, false));
  659. //add filters to allow only number input
  660. leftInput->filters.add(boost::bind(&CTextInput::numberFilter, _1, _2, leftMin, leftMax));
  661. rightInput->filters.add(boost::bind(&CTextInput::numberFilter, _1, _2, rightMin, rightMax));
  662. leftInput->setText(boost::lexical_cast<std::string>(leftAmount), false);
  663. rightInput->setText(boost::lexical_cast<std::string>(rightAmount), false);
  664. animLeft = new CCreaturePic(20, 54, creature, true, false);
  665. animRight = new CCreaturePic(177, 54,creature, true, false);
  666. slider = new CSlider(21, 194, 257, boost::bind(&CSplitWindow::sliderMoved, this, _1), 0, sliderPositions, rightAmount - rightMin, true);
  667. std::string title = CGI->generaltexth->allTexts[256];
  668. boost::algorithm::replace_first(title,"%s", creature->namePl);
  669. new CLabel(150, 34, FONT_BIG, CENTER, Colors::YELLOW, title);
  670. }
  671. void CSplitWindow::setAmountText(std::string text, bool left)
  672. {
  673. try
  674. {
  675. setAmount(boost::lexical_cast<int>(text), left);
  676. slider->moveTo(rightAmount - rightMin);
  677. }
  678. catch(boost::bad_lexical_cast &)
  679. {
  680. }
  681. }
  682. void CSplitWindow::setAmount(int value, bool left)
  683. {
  684. int total = leftAmount + rightAmount;
  685. leftAmount = left ? value : total - value;
  686. rightAmount = left ? total - value : value;
  687. leftInput->setText(boost::lexical_cast<std::string>(leftAmount));
  688. rightInput->setText(boost::lexical_cast<std::string>(rightAmount));
  689. }
  690. void CSplitWindow::apply()
  691. {
  692. callback(leftAmount, rightAmount);
  693. close();
  694. }
  695. void CSplitWindow::sliderMoved(int to)
  696. {
  697. setAmount(rightMin + to, false);
  698. }
  699. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> &skills, std::function<void(ui32)> callback):
  700. CWindowObject(PLAYER_COLORED, "LVLUPBKG"),
  701. cb(callback)
  702. {
  703. OBJ_CONSTRUCTION_CAPTURING_ALL;
  704. LOCPLINT->showingDialog->setn(true);
  705. new CAnimImage("PortraitsLarge", hero->portrait, 0, 170, 66);
  706. new CAdventureMapButton("", "", boost::bind(&CLevelWindow::close, this), 297, 413, "IOKAY", SDLK_RETURN);
  707. //%s has gained a level.
  708. new CLabel(192, 33, FONT_MEDIUM, CENTER, Colors::WHITE,
  709. boost::str(boost::format(CGI->generaltexth->allTexts[444]) % hero->name));
  710. //%s is now a level %d %s.
  711. new CLabel(192, 162, FONT_MEDIUM, CENTER, Colors::WHITE,
  712. boost::str(boost::format(CGI->generaltexth->allTexts[445]) % hero->name % hero->level % hero->type->heroClass->name));
  713. new CAnimImage("PSKIL42", pskill, 0, 174, 190);
  714. new CLabel(192, 253, FONT_MEDIUM, CENTER, Colors::WHITE,
  715. CGI->generaltexth->primarySkillNames[pskill] + " +1");
  716. if (!skills.empty())
  717. {
  718. std::vector<CSelectableComponent *> comps;
  719. for(auto & skill : skills)
  720. {
  721. comps.push_back(new CSelectableComponent(
  722. CComponent::secskill,
  723. skill,
  724. hero->getSecSkillLevel( SecondarySkill(skill) )+1,
  725. CComponent::medium));
  726. }
  727. box = new CComponentBox(comps, Rect(75, 300, pos.w - 150, 100));
  728. }
  729. else
  730. box = nullptr;
  731. }
  732. CLevelWindow::~CLevelWindow()
  733. {
  734. //FIXME: call callback if there was nothing to select?
  735. if (box && box->selectedIndex() != -1)
  736. cb(box->selectedIndex());
  737. LOCPLINT->showingDialog->setn(false);
  738. }
  739. void CMinorResDataBar::show(SDL_Surface * to)
  740. {
  741. }
  742. void CMinorResDataBar::showAll(SDL_Surface * to)
  743. {
  744. blitAt(bg,pos.x,pos.y,to);
  745. for (Res::ERes i=Res::WOOD; i<=Res::GOLD; vstd::advance(i, 1))
  746. {
  747. std::string text = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(i));
  748. graphics->fonts[FONT_SMALL]->renderTextCenter(to, text, Colors::WHITE, Point(pos.x + 50 + 76 * i, pos.y + pos.h/2));
  749. }
  750. std::vector<std::string> temp;
  751. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::MONTH)));
  752. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK)));
  753. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK)));
  754. std::string datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  755. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  756. graphics->fonts[FONT_SMALL]->renderTextCenter(to, processStr(datetext,temp), Colors::WHITE, Point(pos.x+545+(pos.w-545)/2,pos.y+pos.h/2));
  757. }
  758. CMinorResDataBar::CMinorResDataBar()
  759. {
  760. bg = BitmapHandler::loadBitmap("KRESBAR.bmp");
  761. CSDL_Ext::setDefaultColorKey(bg);
  762. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  763. pos.x = 7;
  764. pos.y = 575;
  765. pos.w = bg->w;
  766. pos.h = bg->h;
  767. }
  768. CMinorResDataBar::~CMinorResDataBar()
  769. {
  770. SDL_FreeSurface(bg);
  771. }
  772. CObjectListWindow::CItem::CItem(CObjectListWindow *_parent, size_t _id, std::string _text):
  773. parent(_parent),
  774. index(_id)
  775. {
  776. OBJ_CONSTRUCTION_CAPTURING_ALL;
  777. border = new CPicture("TPGATES");
  778. pos = border->pos;
  779. addUsedEvents(LCLICK);
  780. type |= REDRAW_PARENT;
  781. text = new CLabel(pos.w/2, pos.h/2, FONT_SMALL, CENTER, Colors::WHITE, _text);
  782. select(index == parent->selected);
  783. }
  784. void CObjectListWindow::CItem::select(bool on)
  785. {
  786. if (on)
  787. border->recActions = 255;
  788. else
  789. border->recActions = ~(UPDATE | SHOWALL);
  790. redraw();
  791. }
  792. void CObjectListWindow::CItem::clickLeft(tribool down, bool previousState)
  793. {
  794. if( previousState && !down)
  795. parent->changeSelection(index);
  796. }
  797. CObjectListWindow::CObjectListWindow(const std::vector<int> &_items, CIntObject * titlePic, std::string _title, std::string _descr,
  798. std::function<void(int)> Callback):
  799. CWindowObject(PLAYER_COLORED, "TPGATE"),
  800. onSelect(Callback)
  801. {
  802. items.reserve(_items.size());
  803. for(int id : _items)
  804. {
  805. items.push_back(std::make_pair(id, CGI->mh->map->objects[id]->getObjectName()));
  806. }
  807. init(titlePic, _title, _descr);
  808. }
  809. CObjectListWindow::CObjectListWindow(const std::vector<std::string> &_items, CIntObject * titlePic, std::string _title, std::string _descr,
  810. std::function<void(int)> Callback):
  811. CWindowObject(PLAYER_COLORED, "TPGATE"),
  812. onSelect(Callback)
  813. {
  814. items.reserve(_items.size());
  815. for (size_t i=0; i<_items.size(); i++)
  816. items.push_back(std::make_pair(int(i), _items[i]));
  817. init(titlePic, _title, _descr);
  818. }
  819. void CObjectListWindow::init(CIntObject * titlePic, std::string _title, std::string _descr)
  820. {
  821. OBJ_CONSTRUCTION_CAPTURING_ALL;
  822. title = new CLabel(152, 27, FONT_BIG, CENTER, Colors::YELLOW, _title);
  823. descr = new CLabel(145, 133, FONT_SMALL, CENTER, Colors::WHITE, _descr);
  824. ok = new CAdventureMapButton("","",boost::bind(&CObjectListWindow::elementSelected, this),15,402,"IOKAY.DEF", SDLK_RETURN);
  825. ok->block(true);
  826. exit = new CAdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally,&GH, this),228,402,"ICANCEL.DEF",SDLK_ESCAPE);
  827. if (titlePic)
  828. {
  829. titleImage = titlePic;
  830. addChild(titleImage);
  831. titleImage->recActions = defActions;
  832. titleImage->pos.x = pos.w/2 + pos.x - titleImage->pos.w/2;
  833. titleImage->pos.y =75 + pos.y - titleImage->pos.h/2;
  834. }
  835. list = new CListBox(boost::bind(&CObjectListWindow::genItem, this, _1), CListBox::DestroyFunc(),
  836. Point(14, 151), Point(0, 25), 9, items.size(), 0, 1, Rect(262, -32, 256, 256) );
  837. list->type |= REDRAW_PARENT;
  838. }
  839. CIntObject * CObjectListWindow::genItem(size_t index)
  840. {
  841. if (index < items.size())
  842. return new CItem(this, index, items[index].second);
  843. return nullptr;
  844. }
  845. void CObjectListWindow::elementSelected()
  846. {
  847. std::function<void(int)> toCall = onSelect;//save
  848. int where = items[selected].first; //required variables
  849. GH.popIntTotally(this);//then destroy window
  850. toCall(where);//and send selected object
  851. }
  852. void CObjectListWindow::changeSelection(size_t which)
  853. {
  854. ok->block(false);
  855. if (selected == which)
  856. return;
  857. std::list< CIntObject * > elements = list->getItems();
  858. for(CIntObject * element : elements)
  859. {
  860. CItem *item;
  861. if ( (item = dynamic_cast<CItem*>(element)) )
  862. {
  863. if (item->index == selected)
  864. item->select(false);
  865. if (item->index == which)
  866. item->select(true);
  867. }
  868. }
  869. selected = which;
  870. }
  871. void CObjectListWindow::keyPressed (const SDL_KeyboardEvent & key)
  872. {
  873. if(key.state != SDL_PRESSED)
  874. return;
  875. int sel = selected;
  876. switch(key.keysym.sym)
  877. {
  878. break; case SDLK_UP:
  879. sel -=1;
  880. break; case SDLK_DOWN:
  881. sel +=1;
  882. break; case SDLK_PAGEUP:
  883. sel -=9;
  884. break; case SDLK_PAGEDOWN:
  885. sel +=9;
  886. break; case SDLK_HOME:
  887. sel = 0;
  888. break; case SDLK_END:
  889. sel = items.size();
  890. break; default:
  891. return;
  892. }
  893. vstd::abetween(sel, 0, items.size()-1);
  894. list->scrollTo(sel);
  895. changeSelection(sel);
  896. }
  897. void CSystemOptionsWindow::setMusicVolume( int newVolume )
  898. {
  899. Settings volume = settings.write["general"]["music"];
  900. volume->Float() = newVolume;
  901. }
  902. void CSystemOptionsWindow::setSoundVolume( int newVolume )
  903. {
  904. Settings volume = settings.write["general"]["sound"];
  905. volume->Float() = newVolume;
  906. }
  907. void CSystemOptionsWindow::setHeroMoveSpeed( int newSpeed )
  908. {
  909. Settings speed = settings.write["adventure"]["heroSpeed"];
  910. speed->Float() = newSpeed;
  911. }
  912. void CSystemOptionsWindow::setMapScrollingSpeed( int newSpeed )
  913. {
  914. Settings speed = settings.write["adventure"]["scrollSpeed"];
  915. speed->Float() = newSpeed;
  916. }
  917. CSystemOptionsWindow::CSystemOptionsWindow():
  918. CWindowObject(PLAYER_COLORED, "SysOpBck"),
  919. onFullscreenChanged(settings.listen["video"]["fullscreen"])
  920. {
  921. OBJ_CONSTRUCTION_CAPTURING_ALL;
  922. title = new CLabel(242, 32, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[568]);
  923. const JsonNode & texts = CGI->generaltexth->localizedTexts["systemOptions"];
  924. //left window section
  925. leftGroup = new CLabelGroup(FONT_MEDIUM, CENTER, Colors::YELLOW);
  926. leftGroup->add(122, 64, CGI->generaltexth->allTexts[569]);
  927. leftGroup->add(122, 130, CGI->generaltexth->allTexts[570]);
  928. leftGroup->add(122, 196, CGI->generaltexth->allTexts[571]);
  929. leftGroup->add(122, 262, texts["resolutionButton"]["label"].String());
  930. leftGroup->add(122, 347, CGI->generaltexth->allTexts[394]);
  931. leftGroup->add(122, 412, CGI->generaltexth->allTexts[395]);
  932. //right section
  933. rightGroup = new CLabelGroup(FONT_MEDIUM, TOPLEFT, Colors::WHITE);
  934. rightGroup->add(282, 57, CGI->generaltexth->allTexts[572]);
  935. rightGroup->add(282, 89, CGI->generaltexth->allTexts[573]);
  936. rightGroup->add(282, 121, CGI->generaltexth->allTexts[574]);
  937. rightGroup->add(282, 153, CGI->generaltexth->allTexts[577]);
  938. rightGroup->add(282, 185, texts["creatureWindowButton"]["label"].String());
  939. rightGroup->add(282, 217, texts["fullscreenButton"]["label"].String());
  940. //setting up buttons
  941. load = new CAdventureMapButton (CGI->generaltexth->zelp[321].first, CGI->generaltexth->zelp[321].second,
  942. boost::bind(&CSystemOptionsWindow::bloadf, this), 246, 298, "SOLOAD.DEF", SDLK_l);
  943. load->swappedImages = true;
  944. load->update();
  945. save = new CAdventureMapButton (CGI->generaltexth->zelp[322].first, CGI->generaltexth->zelp[322].second,
  946. boost::bind(&CSystemOptionsWindow::bsavef, this), 357, 298, "SOSAVE.DEF", SDLK_s);
  947. save->swappedImages = true;
  948. save->update();
  949. restart = new CAdventureMapButton (CGI->generaltexth->zelp[323].first, CGI->generaltexth->zelp[323].second,
  950. boost::bind(&CSystemOptionsWindow::brestartf, this), 246, 357, "SORSTRT", SDLK_r);
  951. restart->swappedImages = true;
  952. restart->update();
  953. mainMenu = new CAdventureMapButton (CGI->generaltexth->zelp[320].first, CGI->generaltexth->zelp[320].second,
  954. boost::bind(&CSystemOptionsWindow::bmainmenuf, this), 357, 357, "SOMAIN.DEF", SDLK_m);
  955. mainMenu->swappedImages = true;
  956. mainMenu->update();
  957. quitGame = new CAdventureMapButton (CGI->generaltexth->zelp[324].first, CGI->generaltexth->zelp[324].second,
  958. boost::bind(&CSystemOptionsWindow::bquitf, this), 246, 415, "soquit.def", SDLK_q);
  959. quitGame->swappedImages = true;
  960. quitGame->update();
  961. backToMap = new CAdventureMapButton (CGI->generaltexth->zelp[325].first, CGI->generaltexth->zelp[325].second,
  962. boost::bind(&CSystemOptionsWindow::breturnf, this), 357, 415, "soretrn.def", SDLK_RETURN);
  963. backToMap->swappedImages = true;
  964. backToMap->update();
  965. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  966. heroMoveSpeed = new CHighlightableButtonsGroup(0);
  967. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[349].second),CGI->generaltexth->zelp[349].second, "sysopb1.def", 28, 77, 1);
  968. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[350].second),CGI->generaltexth->zelp[350].second, "sysopb2.def", 76, 77, 2);
  969. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[351].second),CGI->generaltexth->zelp[351].second, "sysopb3.def", 124, 77, 4);
  970. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[352].second),CGI->generaltexth->zelp[352].second, "sysopb4.def", 172, 77, 8);
  971. heroMoveSpeed->select(settings["adventure"]["heroSpeed"].Float(), 1);
  972. heroMoveSpeed->onChange = boost::bind(&CSystemOptionsWindow::setHeroMoveSpeed, this, _1);
  973. mapScrollSpeed = new CHighlightableButtonsGroup(0);
  974. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[357].second),CGI->generaltexth->zelp[357].second, "sysopb9.def", 28, 210, 1);
  975. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[358].second),CGI->generaltexth->zelp[358].second, "sysob10.def", 92, 210, 2);
  976. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[359].second),CGI->generaltexth->zelp[359].second, "sysob11.def", 156, 210, 4);
  977. mapScrollSpeed->select(settings["adventure"]["scrollSpeed"].Float(), 1);
  978. mapScrollSpeed->onChange = boost::bind(&CSystemOptionsWindow::setMapScrollingSpeed, this, _1);
  979. musicVolume = new CHighlightableButtonsGroup(0, true);
  980. for(int i=0; i<10; ++i)
  981. musicVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[326+i].second),CGI->generaltexth->zelp[326+i].second, "syslb.def", 29 + 19*i, 359, i*11);
  982. musicVolume->select(CCS->musich->getVolume(), 1);
  983. musicVolume->onChange = boost::bind(&CSystemOptionsWindow::setMusicVolume, this, _1);
  984. effectsVolume = new CHighlightableButtonsGroup(0, true);
  985. for(int i=0; i<10; ++i)
  986. effectsVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[336+i].second),CGI->generaltexth->zelp[336+i].second, "syslb.def", 29 + 19*i, 425, i*11);
  987. effectsVolume->select(CCS->soundh->getVolume(), 1);
  988. effectsVolume->onChange = boost::bind(&CSystemOptionsWindow::setSoundVolume, this, _1);
  989. showReminder = new CHighlightableButton(
  990. boost::bind(&CSystemOptionsWindow::toggleReminder, this, true), boost::bind(&CSystemOptionsWindow::toggleReminder, this, false),
  991. std::map<int,std::string>(), CGI->generaltexth->zelp[361].second, false, "sysopchk.def", nullptr, 246, 87, false);
  992. quickCombat = new CHighlightableButton(
  993. boost::bind(&CSystemOptionsWindow::toggleQuickCombat, this, true), boost::bind(&CSystemOptionsWindow::toggleQuickCombat, this, false),
  994. std::map<int,std::string>(), CGI->generaltexth->zelp[362].second, false, "sysopchk.def", nullptr, 246, 87+32, false);
  995. spellbookAnim = new CHighlightableButton(
  996. boost::bind(&CSystemOptionsWindow::toggleSpellbookAnim, this, true), boost::bind(&CSystemOptionsWindow::toggleSpellbookAnim, this, false),
  997. std::map<int,std::string>(), CGI->generaltexth->zelp[364].second, false, "sysopchk.def", nullptr, 246, 87+64, false);
  998. newCreatureWin = new CHighlightableButton(
  999. boost::bind(&CSystemOptionsWindow::toggleCreatureWin, this, true), boost::bind(&CSystemOptionsWindow::toggleCreatureWin, this, false),
  1000. std::map<int,std::string>(), texts["creatureWindowButton"]["help"].String(), false, "sysopchk.def", nullptr, 246, 183, false);
  1001. fullscreen = new CHighlightableButton(
  1002. boost::bind(&CSystemOptionsWindow::toggleFullscreen, this, true), boost::bind(&CSystemOptionsWindow::toggleFullscreen, this, false),
  1003. std::map<int,std::string>(), texts["fullscreenButton"]["help"].String(), false, "sysopchk.def", nullptr, 246, 215, false);
  1004. showReminder->select(settings["adventure"]["heroReminder"].Bool());
  1005. quickCombat->select(settings["adventure"]["quickCombat"].Bool());
  1006. spellbookAnim->select(settings["video"]["spellbookAnimation"].Bool());
  1007. newCreatureWin->select(settings["general"]["classicCreatureWindow"].Bool());
  1008. fullscreen->select(settings["video"]["fullscreen"].Bool());
  1009. onFullscreenChanged([&](const JsonNode &newState){ fullscreen->select(newState.Bool());});
  1010. gameResButton = new CAdventureMapButton("", texts["resolutionButton"]["help"].String(),
  1011. boost::bind(&CSystemOptionsWindow::selectGameRes, this),
  1012. 28, 275,"SYSOB12", SDLK_g);
  1013. std::string resText;
  1014. resText += boost::lexical_cast<std::string>(settings["video"]["screenRes"]["width"].Float());
  1015. resText += "x";
  1016. resText += boost::lexical_cast<std::string>(settings["video"]["screenRes"]["height"].Float());
  1017. gameResLabel = new CLabel(170, 292, FONT_MEDIUM, CENTER, Colors::YELLOW, resText);
  1018. }
  1019. void CSystemOptionsWindow::selectGameRes()
  1020. {
  1021. std::vector<std::string> items;
  1022. const JsonNode & texts = CGI->generaltexth->localizedTexts["systemOptions"]["resolutionMenu"];
  1023. for( config::CConfigHandler::GuiOptionsMap::value_type& value : conf.guiOptions)
  1024. {
  1025. std::string resX = boost::lexical_cast<std::string>(value.first.first);
  1026. std::string resY = boost::lexical_cast<std::string>(value.first.second);
  1027. items.push_back(resX + 'x' + resY);
  1028. }
  1029. GH.pushInt(new CObjectListWindow(items, nullptr, texts["label"].String(), texts["help"].String(),
  1030. boost::bind(&CSystemOptionsWindow::setGameRes, this, _1)));
  1031. }
  1032. void CSystemOptionsWindow::setGameRes(int index)
  1033. {
  1034. auto iter = conf.guiOptions.begin();
  1035. std::advance(iter, index);
  1036. //do not set resolution to illegal one (0x0)
  1037. assert(iter!=conf.guiOptions.end() && iter->first.first > 0 && iter->first.second > 0);
  1038. Settings gameRes = settings.write["video"]["screenRes"];
  1039. gameRes["width"].Float() = iter->first.first;
  1040. gameRes["height"].Float() = iter->first.second;
  1041. std::string resText;
  1042. resText += boost::lexical_cast<std::string>(iter->first.first);
  1043. resText += "x";
  1044. resText += boost::lexical_cast<std::string>(iter->first.second);
  1045. gameResLabel->setText(resText);
  1046. }
  1047. void CSystemOptionsWindow::toggleReminder(bool on)
  1048. {
  1049. Settings heroReminder = settings.write["adventure"]["heroReminder"];
  1050. heroReminder->Bool() = on;
  1051. }
  1052. void CSystemOptionsWindow::toggleQuickCombat(bool on)
  1053. {
  1054. Settings quickCombat = settings.write["adventure"]["quickCombat"];
  1055. quickCombat->Bool() = on;
  1056. }
  1057. void CSystemOptionsWindow::toggleSpellbookAnim(bool on)
  1058. {
  1059. Settings quickCombat = settings.write["video"]["spellbookAnimation"];
  1060. quickCombat->Bool() = on;
  1061. }
  1062. void CSystemOptionsWindow::toggleCreatureWin(bool on)
  1063. {
  1064. Settings classicCreatureWindow = settings.write["general"]["classicCreatureWindow"];
  1065. classicCreatureWindow->Bool() = on;
  1066. }
  1067. void CSystemOptionsWindow::toggleFullscreen(bool on)
  1068. {
  1069. Settings fullscreen = settings.write["video"]["fullscreen"];
  1070. fullscreen->Bool() = on;
  1071. }
  1072. void CSystemOptionsWindow::bquitf()
  1073. {
  1074. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this]{ closeAndPushEvent(SDL_QUIT); }, 0);
  1075. }
  1076. void CSystemOptionsWindow::breturnf()
  1077. {
  1078. GH.popIntTotally(this);
  1079. }
  1080. void CSystemOptionsWindow::bmainmenuf()
  1081. {
  1082. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this]{ closeAndPushEvent(SDL_USEREVENT, RETURN_TO_MAIN_MENU); }, 0);
  1083. }
  1084. void CSystemOptionsWindow::bloadf()
  1085. {
  1086. GH.popIntTotally(this);
  1087. LOCPLINT->proposeLoadingGame();
  1088. }
  1089. void CSystemOptionsWindow::bsavef()
  1090. {
  1091. GH.popIntTotally(this);
  1092. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  1093. }
  1094. void CSystemOptionsWindow::brestartf()
  1095. {
  1096. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], [this]{ closeAndPushEvent(SDL_USEREVENT, RESTART_GAME); }, 0);
  1097. }
  1098. void CSystemOptionsWindow::closeAndPushEvent(int eventType, int code /*= 0*/)
  1099. {
  1100. GH.popIntTotally(this);
  1101. GH.pushSDLEvent(eventType, code);
  1102. }
  1103. CTavernWindow::CTavernWindow(const CGObjectInstance *TavernObj):
  1104. CWindowObject(PLAYER_COLORED, "TPTAVERN"),
  1105. tavernObj(TavernObj)
  1106. {
  1107. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1108. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(TavernObj);
  1109. if(h.size() < 2)
  1110. h.resize(2, nullptr);
  1111. h1 = new HeroPortrait(selected,0,72,299,h[0]);
  1112. h2 = new HeroPortrait(selected,1,162,299,h[1]);
  1113. selected = 0;
  1114. if (!h[0])
  1115. selected = 1;
  1116. if (!h[0] && !h[1])
  1117. selected = -1;
  1118. oldSelected = -1;
  1119. new CLabel(200, 35, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[37]);
  1120. new CLabel(320, 328, FONT_SMALL, CENTER, Colors::WHITE, "2500");
  1121. new CTextBox(LOCPLINT->cb->getTavernGossip(tavernObj), Rect(32, 190, 330, 68), 0, FONT_SMALL, CENTER, Colors::WHITE);
  1122. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1123. cancel = new CAdventureMapButton(CGI->generaltexth->tavernInfo[7],"", boost::bind(&CTavernWindow::close, this), 310, 428, "ICANCEL.DEF", SDLK_ESCAPE);
  1124. recruit = new CAdventureMapButton("", "", boost::bind(&CTavernWindow::recruitb, this), 272, 355, "TPTAV01.DEF", SDLK_RETURN);
  1125. thiefGuild = new CAdventureMapButton(CGI->generaltexth->tavernInfo[5],"", boost::bind(&CTavernWindow::thievesguildb, this), 22, 428, "TPTAV02.DEF", SDLK_t);
  1126. if(LOCPLINT->cb->getResourceAmount(Res::GOLD) < 2500) //not enough gold
  1127. {
  1128. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[0]; //Cannot afford a Hero
  1129. recruit->block(true);
  1130. }
  1131. else if(LOCPLINT->castleInt && LOCPLINT->cb->howManyHeroes(true) >= VLC->modh->settings.MAX_HEROES_AVAILABLE_PER_PLAYER)
  1132. {
  1133. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  1134. boost::algorithm::replace_first(recruit->hoverTexts[0],"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes(true)));
  1135. recruit->block(true);
  1136. }
  1137. else if((!LOCPLINT->castleInt) && LOCPLINT->cb->howManyHeroes(false) >= VLC->modh->settings.MAX_HEROES_ON_MAP_PER_PLAYER)
  1138. {
  1139. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  1140. boost::algorithm::replace_first(recruit->hoverTexts[0], "%d", boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes(false)));
  1141. recruit->block(true);
  1142. }
  1143. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  1144. {
  1145. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[2]; //Cannot recruit. You already have a Hero in this town.
  1146. recruit->block(true);
  1147. }
  1148. else
  1149. {
  1150. if(selected == -1)
  1151. recruit->block(true);
  1152. }
  1153. if (LOCPLINT->castleInt)
  1154. CCS->videoh->open(LOCPLINT->castleInt->town->town->clientInfo.tavernVideo);
  1155. else
  1156. CCS->videoh->open("TAVERN.BIK");
  1157. }
  1158. void CTavernWindow::recruitb()
  1159. {
  1160. const CGHeroInstance *toBuy = (selected ? h2 : h1)->h;
  1161. const CGObjectInstance *obj = tavernObj;
  1162. close();
  1163. LOCPLINT->cb->recruitHero(obj, toBuy);
  1164. }
  1165. void CTavernWindow::thievesguildb()
  1166. {
  1167. GH.pushInt( new CThievesGuildWindow(tavernObj) );
  1168. }
  1169. CTavernWindow::~CTavernWindow()
  1170. {
  1171. CCS->videoh->close();
  1172. }
  1173. void CTavernWindow::show(SDL_Surface * to)
  1174. {
  1175. CWindowObject::show(to);
  1176. CCS->videoh->update(pos.x+70, pos.y+56, to, true, false);
  1177. if(selected >= 0)
  1178. {
  1179. HeroPortrait *sel = selected ? h2 : h1;
  1180. if (selected != oldSelected && !recruit->isBlocked())
  1181. {
  1182. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  1183. oldSelected = selected;
  1184. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[3]; //Recruit %s the %s
  1185. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->name);
  1186. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->type->heroClass->name);
  1187. }
  1188. printAtMiddleWBLoc(sel->description, 146, 395, FONT_SMALL, 200, Colors::WHITE, to);
  1189. CSDL_Ext::drawBorder(to,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  1190. }
  1191. }
  1192. void CTavernWindow::HeroPortrait::clickLeft(tribool down, bool previousState)
  1193. {
  1194. if(previousState && !down && h)
  1195. *_sel = _id;
  1196. }
  1197. void CTavernWindow::HeroPortrait::clickRight(tribool down, bool previousState)
  1198. {
  1199. if(down && h)
  1200. {
  1201. GH.pushInt(new CRClickPopupInt(new CHeroWindow(h), true));
  1202. }
  1203. }
  1204. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  1205. : h(H), _sel(&sel), _id(id)
  1206. {
  1207. addUsedEvents(LCLICK | RCLICK | HOVER);
  1208. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1209. h = H;
  1210. pos.x += x;
  1211. pos.y += y;
  1212. pos.w = 58;
  1213. pos.h = 64;
  1214. if(H)
  1215. {
  1216. hoverName = CGI->generaltexth->tavernInfo[4];
  1217. boost::algorithm::replace_first(hoverName,"%s",H->name);
  1218. int artifs = h->artifactsWorn.size() + h->artifactsInBackpack.size();
  1219. for(int i=13; i<=17; i++) //war machines and spellbook don't count
  1220. if(vstd::contains(h->artifactsWorn, ArtifactPosition(i)))
  1221. artifs--;
  1222. description = CGI->generaltexth->allTexts[215];
  1223. boost::algorithm::replace_first(description, "%s", h->name);
  1224. boost::algorithm::replace_first(description, "%d", boost::lexical_cast<std::string>(h->level));
  1225. boost::algorithm::replace_first(description, "%s", h->type->heroClass->name);
  1226. boost::algorithm::replace_first(description, "%d", boost::lexical_cast<std::string>(artifs));
  1227. new CAnimImage("portraitsLarge", h->portrait);
  1228. }
  1229. }
  1230. void CTavernWindow::HeroPortrait::hover( bool on )
  1231. {
  1232. //Hoverable::hover(on);
  1233. if(on)
  1234. GH.statusbar->setText(hoverName);
  1235. else
  1236. GH.statusbar->clear();
  1237. }
  1238. void CInGameConsole::show(SDL_Surface * to)
  1239. {
  1240. int number = 0;
  1241. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  1242. boost::unique_lock<boost::mutex> lock(texts_mx);
  1243. for(auto it = texts.begin(); it != texts.end(); ++it, ++number)
  1244. {
  1245. Point leftBottomCorner(0, screen->h);
  1246. if(LOCPLINT->battleInt)
  1247. {
  1248. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  1249. }
  1250. graphics->fonts[FONT_MEDIUM]->renderTextLeft(to, it->first, Colors::GREEN,
  1251. Point(leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20));
  1252. if(SDL_GetTicks() - it->second > defaultTimeout)
  1253. {
  1254. toDel.push_back(it);
  1255. }
  1256. }
  1257. for(auto & elem : toDel)
  1258. {
  1259. texts.erase(elem);
  1260. }
  1261. }
  1262. void CInGameConsole::print(const std::string &txt)
  1263. {
  1264. boost::unique_lock<boost::mutex> lock(texts_mx);
  1265. int lineLen = conf.go()->ac.outputLineLength;
  1266. if(txt.size() < lineLen)
  1267. {
  1268. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  1269. if(texts.size() > maxDisplayedTexts)
  1270. {
  1271. texts.pop_front();
  1272. }
  1273. }
  1274. else
  1275. {
  1276. assert(lineLen);
  1277. for(int g=0; g<txt.size() / lineLen + 1; ++g)
  1278. {
  1279. std::string part = txt.substr(g * lineLen, lineLen);
  1280. if(part.size() == 0)
  1281. break;
  1282. texts.push_back(std::make_pair(part, SDL_GetTicks()));
  1283. if(texts.size() > maxDisplayedTexts)
  1284. {
  1285. texts.pop_front();
  1286. }
  1287. }
  1288. }
  1289. }
  1290. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  1291. {
  1292. if(key.type != SDL_KEYDOWN) return;
  1293. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  1294. switch(key.keysym.sym)
  1295. {
  1296. case SDLK_TAB:
  1297. case SDLK_ESCAPE:
  1298. {
  1299. if(captureAllKeys)
  1300. {
  1301. captureAllKeys = false;
  1302. endEnteringText(false);
  1303. }
  1304. else if(SDLK_TAB)
  1305. {
  1306. captureAllKeys = true;
  1307. startEnteringText();
  1308. }
  1309. break;
  1310. }
  1311. case SDLK_RETURN: //enter key
  1312. {
  1313. if(enteredText.size() > 0 && captureAllKeys)
  1314. {
  1315. captureAllKeys = false;
  1316. endEnteringText(true);
  1317. CCS->soundh->playSound("CHAT");
  1318. }
  1319. break;
  1320. }
  1321. case SDLK_BACKSPACE:
  1322. {
  1323. if(enteredText.size() > 1)
  1324. {
  1325. Unicode::trimRight(enteredText,2);
  1326. enteredText += '_';
  1327. refreshEnteredText();
  1328. }
  1329. break;
  1330. }
  1331. case SDLK_UP: //up arrow
  1332. {
  1333. if(previouslyEntered.size() == 0)
  1334. break;
  1335. if(prevEntDisp == -1)
  1336. {
  1337. prevEntDisp = previouslyEntered.size() - 1;
  1338. enteredText = previouslyEntered[prevEntDisp] + "_";
  1339. refreshEnteredText();
  1340. }
  1341. else if( prevEntDisp > 0)
  1342. {
  1343. --prevEntDisp;
  1344. enteredText = previouslyEntered[prevEntDisp] + "_";
  1345. refreshEnteredText();
  1346. }
  1347. break;
  1348. }
  1349. case SDLK_DOWN: //down arrow
  1350. {
  1351. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  1352. {
  1353. ++prevEntDisp;
  1354. enteredText = previouslyEntered[prevEntDisp] + "_";
  1355. refreshEnteredText();
  1356. }
  1357. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  1358. {
  1359. prevEntDisp = -1;
  1360. enteredText = "_";
  1361. refreshEnteredText();
  1362. }
  1363. break;
  1364. }
  1365. default:
  1366. {
  1367. #ifdef VCMI_SDL1
  1368. if(enteredText.size() > 0 && enteredText.size() < conf.go()->ac.inputLineLength)
  1369. {
  1370. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  1371. {
  1372. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  1373. enteredText += "_";
  1374. refreshEnteredText();
  1375. }
  1376. }
  1377. #endif // VCMI_SDL1
  1378. break;
  1379. }
  1380. }
  1381. }
  1382. #ifndef VCMI_SDL1
  1383. void CInGameConsole::textInputed(const SDL_TextInputEvent & event)
  1384. {
  1385. if(!captureAllKeys || enteredText.size() == 0)
  1386. return;
  1387. enteredText.resize(enteredText.size()-1);
  1388. enteredText += event.text;
  1389. enteredText += "_";
  1390. refreshEnteredText();
  1391. }
  1392. void CInGameConsole::textEdited(const SDL_TextEditingEvent & event)
  1393. {
  1394. //do nothing here
  1395. }
  1396. #endif // VCMI_SDL1
  1397. void CInGameConsole::startEnteringText()
  1398. {
  1399. CSDL_Ext::startTextInput(&pos);
  1400. enteredText = "_";
  1401. if(GH.topInt() == adventureInt)
  1402. {
  1403. GH.statusbar->alignment = TOPLEFT;
  1404. GH.statusbar->setText(enteredText);
  1405. //Prevent changes to the text from mouse interaction with the adventure map
  1406. GH.statusbar->lock(true);
  1407. }
  1408. else if(LOCPLINT->battleInt)
  1409. {
  1410. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  1411. }
  1412. }
  1413. void CInGameConsole::endEnteringText(bool printEnteredText)
  1414. {
  1415. CSDL_Ext::stopTextInput();
  1416. prevEntDisp = -1;
  1417. if(printEnteredText)
  1418. {
  1419. std::string txt = enteredText.substr(0, enteredText.size()-1);
  1420. LOCPLINT->cb->sendMessage(txt);
  1421. previouslyEntered.push_back(txt);
  1422. //print(txt);
  1423. }
  1424. enteredText = "";
  1425. if(GH.topInt() == adventureInt)
  1426. {
  1427. GH.statusbar->alignment = CENTER;
  1428. GH.statusbar->lock(false);
  1429. GH.statusbar->clear();
  1430. }
  1431. else if(LOCPLINT->battleInt)
  1432. {
  1433. LOCPLINT->battleInt->console->ingcAlter = "";
  1434. }
  1435. }
  1436. void CInGameConsole::refreshEnteredText()
  1437. {
  1438. if(GH.topInt() == adventureInt)
  1439. {
  1440. GH.statusbar->lock(false);
  1441. GH.statusbar->clear();
  1442. GH.statusbar->setText(enteredText);
  1443. GH.statusbar->lock(true);
  1444. }
  1445. else if(LOCPLINT->battleInt)
  1446. {
  1447. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  1448. }
  1449. }
  1450. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  1451. {
  1452. #ifdef VCMI_SDL1
  1453. addUsedEvents(KEYBOARD);
  1454. #else
  1455. addUsedEvents(KEYBOARD | TEXTINPUT);
  1456. #endif
  1457. }
  1458. void LRClickableAreaWTextComp::clickLeft(tribool down, bool previousState)
  1459. {
  1460. if((!down) && previousState)
  1461. {
  1462. std::vector<CComponent*> comp(1, createComponent());
  1463. LOCPLINT->showInfoDialog(text, comp);
  1464. }
  1465. }
  1466. LRClickableAreaWTextComp::LRClickableAreaWTextComp(const Rect &Pos, int BaseType)
  1467. : LRClickableAreaWText(Pos), baseType(BaseType), bonusValue(-1)
  1468. {
  1469. }
  1470. CComponent * LRClickableAreaWTextComp::createComponent() const
  1471. {
  1472. if(baseType >= 0)
  1473. return new CComponent(CComponent::Etype(baseType), type, bonusValue);
  1474. else
  1475. return nullptr;
  1476. }
  1477. void LRClickableAreaWTextComp::clickRight(tribool down, bool previousState)
  1478. {
  1479. if(down)
  1480. {
  1481. if(CComponent *comp = createComponent())
  1482. {
  1483. CRClickPopup::createAndPush(text, CInfoWindow::TCompsInfo(1, comp));
  1484. return;
  1485. }
  1486. }
  1487. LRClickableAreaWText::clickRight(down, previousState); //only if with-component variant not occurred
  1488. }
  1489. CHeroArea::CHeroArea(int x, int y, const CGHeroInstance * _hero):hero(_hero)
  1490. {
  1491. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1492. addUsedEvents(LCLICK | RCLICK | HOVER);
  1493. pos.x += x; pos.w = 58;
  1494. pos.y += y; pos.h = 64;
  1495. if (hero)
  1496. new CAnimImage("PortraitsLarge", hero->portrait);
  1497. }
  1498. void CHeroArea::clickLeft(tribool down, bool previousState)
  1499. {
  1500. if((!down) && previousState && hero)
  1501. LOCPLINT->openHeroWindow(hero);
  1502. }
  1503. void CHeroArea::clickRight(tribool down, bool previousState)
  1504. {
  1505. if((!down) && previousState && hero)
  1506. LOCPLINT->openHeroWindow(hero);
  1507. }
  1508. void CHeroArea::hover(bool on)
  1509. {
  1510. if (on && hero)
  1511. GH.statusbar->setText(hero->getObjectName());
  1512. else
  1513. GH.statusbar->clear();
  1514. }
  1515. void LRClickableAreaOpenTown::clickLeft(tribool down, bool previousState)
  1516. {
  1517. if((!down) && previousState && town)
  1518. {
  1519. LOCPLINT->openTownWindow(town);
  1520. if ( type == 2 )
  1521. LOCPLINT->castleInt->builds->buildingClicked(BuildingID::VILLAGE_HALL);
  1522. else if ( type == 3 && town->fortLevel() )
  1523. LOCPLINT->castleInt->builds->buildingClicked(BuildingID::FORT);
  1524. }
  1525. }
  1526. void LRClickableAreaOpenTown::clickRight(tribool down, bool previousState)
  1527. {
  1528. if((!down) && previousState && town)
  1529. LOCPLINT->openTownWindow(town);//TODO: popup?
  1530. }
  1531. LRClickableAreaOpenTown::LRClickableAreaOpenTown()
  1532. : LRClickableAreaWTextComp(Rect(0,0,0,0), -1)
  1533. {
  1534. }
  1535. void CExchangeWindow::questlog(int whichHero)
  1536. {
  1537. CCS->curh->dragAndDropCursor(nullptr);
  1538. }
  1539. void CExchangeWindow::prepareBackground()
  1540. {
  1541. //printing heroes' names and levels
  1542. auto genTitle = [](const CGHeroInstance *h)
  1543. {
  1544. return boost::str(boost::format(CGI->generaltexth->allTexts[138])
  1545. % h->name % h->level % h->type->heroClass->name);
  1546. };
  1547. new CLabel(147, 25, FONT_SMALL, CENTER, Colors::WHITE, genTitle(heroInst[0]));
  1548. new CLabel(653, 25, FONT_SMALL, CENTER, Colors::WHITE, genTitle(heroInst[1]));
  1549. //printing primary skills
  1550. for(int g=0; g<4; ++g)
  1551. new CAnimImage("PSKIL32", g, 0, 385, 19 + 36*g);
  1552. //heroes related thing
  1553. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  1554. {
  1555. CHeroWithMaybePickedArtifact heroWArt = CHeroWithMaybePickedArtifact(this, heroInst[b]);
  1556. //printing primary skills' amounts
  1557. for(int m=0; m<GameConstants::PRIMARY_SKILLS; ++m)
  1558. new CLabel(352 + 93 * b, 35 + 36 * m, FONT_SMALL, CENTER, Colors::WHITE,
  1559. boost::lexical_cast<std::string>(heroWArt.getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(m))));
  1560. //printing secondary skills
  1561. for(int m=0; m<heroInst[b]->secSkills.size(); ++m)
  1562. {
  1563. int id = heroInst[b]->secSkills[m].first;
  1564. int level = heroInst[b]->secSkills[m].second;
  1565. new CAnimImage("SECSK32", id*3 + level + 2 , 0, 32 + 36 * m + 454 * b, 88);
  1566. }
  1567. //hero's specialty
  1568. new CAnimImage("UN32", heroInst[b]->type->imageIndex, 0, 67 + 490*b, 45);
  1569. //experience
  1570. new CAnimImage("PSKIL32", 4, 0, 103 + 490*b, 45);
  1571. new CLabel(119 + 490*b, 71, FONT_SMALL, CENTER, Colors::WHITE, makeNumberShort(heroInst[b]->exp));
  1572. //mana points
  1573. new CAnimImage("PSKIL32", 5, 0, 139 + 490*b, 45);
  1574. new CLabel(155 + 490*b, 71, FONT_SMALL, CENTER, Colors::WHITE, makeNumberShort(heroInst[b]->mana));
  1575. }
  1576. //printing portraits
  1577. new CAnimImage("PortraitsLarge", heroInst[0]->portrait, 0, 257, 13);
  1578. new CAnimImage("PortraitsLarge", heroInst[1]->portrait, 0, 485, 13);
  1579. }
  1580. CExchangeWindow::CExchangeWindow(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID queryID):
  1581. CWindowObject(PLAYER_COLORED | BORDERED, "TRADE2")
  1582. {
  1583. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1584. heroInst[0] = LOCPLINT->cb->getHero(hero1);
  1585. heroInst[1] = LOCPLINT->cb->getHero(hero2);
  1586. prepareBackground();
  1587. artifs[0] = new CArtifactsOfHero(Point(-334, 150));
  1588. artifs[0]->commonInfo = new CArtifactsOfHero::SCommonPart;
  1589. artifs[0]->commonInfo->participants.insert(artifs[0]);
  1590. artifs[0]->setHero(heroInst[0]);
  1591. artifs[1] = new CArtifactsOfHero(Point(96, 150));
  1592. artifs[1]->commonInfo = artifs[0]->commonInfo;
  1593. artifs[1]->commonInfo->participants.insert(artifs[1]);
  1594. artifs[1]->setHero(heroInst[1]);
  1595. artSets.push_back(artifs[0]);
  1596. artSets.push_back(artifs[1]);
  1597. //primary skills
  1598. for(int g=0; g<4; ++g)
  1599. {
  1600. //primary skill's clickable areas
  1601. primSkillAreas.push_back(new LRClickableAreaWTextComp());
  1602. primSkillAreas[g]->pos = genRect(32, 140, pos.x + 329, pos.y + 19 + 36 * g);
  1603. primSkillAreas[g]->text = CGI->generaltexth->arraytxt[2+g];
  1604. primSkillAreas[g]->type = g;
  1605. primSkillAreas[g]->bonusValue = -1;
  1606. primSkillAreas[g]->baseType = 0;
  1607. primSkillAreas[g]->hoverText = CGI->generaltexth->heroscrn[1];
  1608. boost::replace_first(primSkillAreas[g]->hoverText, "%s", CGI->generaltexth->primarySkillNames[g]);
  1609. }
  1610. //heroes related thing
  1611. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  1612. {
  1613. //secondary skill's clickable areas
  1614. for(int g=0; g<heroInst[b]->secSkills.size(); ++g)
  1615. {
  1616. int skill = heroInst[b]->secSkills[g].first,
  1617. level = heroInst[b]->secSkills[g].second; // <1, 3>
  1618. secSkillAreas[b].push_back(new LRClickableAreaWTextComp());
  1619. secSkillAreas[b][g]->pos = genRect(32, 32, pos.x + 32 + g*36 + b*454 , pos.y + 88);
  1620. secSkillAreas[b][g]->baseType = 1;
  1621. secSkillAreas[b][g]->type = skill;
  1622. secSkillAreas[b][g]->bonusValue = level;
  1623. secSkillAreas[b][g]->text = CGI->generaltexth->skillInfoTexts[skill][level-1];
  1624. secSkillAreas[b][g]->hoverText = CGI->generaltexth->heroscrn[21];
  1625. boost::algorithm::replace_first(secSkillAreas[b][g]->hoverText, "%s", CGI->generaltexth->levels[level - 1]);
  1626. boost::algorithm::replace_first(secSkillAreas[b][g]->hoverText, "%s", CGI->generaltexth->skillName[skill]);
  1627. }
  1628. portrait[b] = new CHeroArea(257 + 228*b, 13, heroInst[b]);
  1629. specialty[b] = new LRClickableAreaWText();
  1630. specialty[b]->pos = genRect(32, 32, pos.x + 69 + 490*b, pos.y + 45);
  1631. specialty[b]->hoverText = CGI->generaltexth->heroscrn[27];
  1632. specialty[b]->text = heroInst[b]->type->specDescr;
  1633. experience[b] = new LRClickableAreaWText();
  1634. experience[b]->pos = genRect(32, 32, pos.x + 105 + 490*b, pos.y + 45);
  1635. experience[b]->hoverText = CGI->generaltexth->heroscrn[9];
  1636. experience[b]->text = CGI->generaltexth->allTexts[2].c_str();
  1637. boost::algorithm::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->level));
  1638. boost::algorithm::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(heroInst[b]->level+1)));
  1639. boost::algorithm::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->exp));
  1640. spellPoints[b] = new LRClickableAreaWText();
  1641. spellPoints[b]->pos = genRect(32, 32, pos.x + 141 + 490*b, pos.y + 45);
  1642. spellPoints[b]->hoverText = CGI->generaltexth->heroscrn[22];
  1643. spellPoints[b]->text = CGI->generaltexth->allTexts[205];
  1644. boost::algorithm::replace_first(spellPoints[b]->text, "%s", heroInst[b]->name);
  1645. boost::algorithm::replace_first(spellPoints[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->mana));
  1646. boost::algorithm::replace_first(spellPoints[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->manaLimit()));
  1647. //setting morale
  1648. morale[b] = new MoraleLuckBox(true, genRect(32, 32, 176 + 490*b, 39), true);
  1649. morale[b]->set(heroInst[b]);
  1650. //setting luck
  1651. luck[b] = new MoraleLuckBox(false, genRect(32, 32, 212 + 490*b, 39), true);
  1652. luck[b]->set(heroInst[b]);
  1653. }
  1654. //buttons
  1655. quit = new CAdventureMapButton(CGI->generaltexth->zelp[600], boost::bind(&CExchangeWindow::close, this), 732, 567, "IOKAY.DEF", SDLK_RETURN);
  1656. if(queryID.getNum() > 0)
  1657. quit->callback += [=]{ LOCPLINT->cb->selectionMade(0, queryID); };
  1658. questlogButton[0] = new CAdventureMapButton(CGI->generaltexth->heroscrn[0], "", boost::bind(&CExchangeWindow::questlog,this, 0), 10, 44, "hsbtns4.def");
  1659. questlogButton[1] = new CAdventureMapButton(CGI->generaltexth->heroscrn[0], "", boost::bind(&CExchangeWindow::questlog,this, 1), 740, 44, "hsbtns4.def");
  1660. Rect barRect(5, 578, 725, 18);
  1661. ourBar = new CGStatusBar(new CPicture(*background, barRect, 5, 578, false));
  1662. //garrison interface
  1663. garr = new CGarrisonInt(69, 131, 4, Point(418,0), *background, Point(69,131), heroInst[0],heroInst[1], true, true);
  1664. garr->addSplitBtn(new CAdventureMapButton(CGI->generaltexth->tcommands[3], "", boost::bind(&CGarrisonInt::splitClick, garr), 10, 132, "TSBTNS.DEF"));
  1665. garr->addSplitBtn(new CAdventureMapButton(CGI->generaltexth->tcommands[3], "", boost::bind(&CGarrisonInt::splitClick, garr), 740, 132, "TSBTNS.DEF"));
  1666. }
  1667. CExchangeWindow::~CExchangeWindow() //d-tor
  1668. {
  1669. delete artifs[0]->commonInfo;
  1670. artifs[0]->commonInfo = nullptr;
  1671. artifs[1]->commonInfo = nullptr;
  1672. }
  1673. CShipyardWindow::CShipyardWindow(const std::vector<si32> &cost, int state, int boatType, const std::function<void()> &onBuy):
  1674. CWindowObject(PLAYER_COLORED, "TPSHIP")
  1675. {
  1676. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1677. bgWater = new CPicture("TPSHIPBK", 100, 69);
  1678. std::string boatFilenames[3] = {"AB01_", "AB02_", "AB03_"};
  1679. Point waterCenter = Point(bgWater->pos.x+bgWater->pos.w/2, bgWater->pos.y+bgWater->pos.h/2);
  1680. bgShip = new CAnimImage(boatFilenames[boatType], 0, 7, 120, 96, CShowableAnim::USE_RLE);
  1681. bgShip->center(waterCenter);
  1682. // Create resource icons and costs.
  1683. std::string goldValue = boost::lexical_cast<std::string>(cost[Res::GOLD]);
  1684. std::string woodValue = boost::lexical_cast<std::string>(cost[Res::WOOD]);
  1685. goldCost = new CLabel(118, 294, FONT_SMALL, CENTER, Colors::WHITE, goldValue);
  1686. woodCost = new CLabel(212, 294, FONT_SMALL, CENTER, Colors::WHITE, woodValue);
  1687. goldPic = new CAnimImage("RESOURCE", Res::GOLD, 0, 100, 244);
  1688. woodPic = new CAnimImage("RESOURCE", Res::WOOD, 0, 196, 244);
  1689. quit = new CAdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CShipyardWindow::close, this), 224, 312, "ICANCEL", SDLK_RETURN);
  1690. build = new CAdventureMapButton(CGI->generaltexth->allTexts[598], "", boost::bind(&CShipyardWindow::close, this), 42, 312, "IBUY30", SDLK_RETURN);
  1691. build->callback += onBuy;
  1692. for(Res::ERes i = Res::WOOD; i <= Res::GOLD; vstd::advance(i, 1))
  1693. {
  1694. if(cost[i] > LOCPLINT->cb->getResourceAmount(i))
  1695. {
  1696. build->block(true);
  1697. break;
  1698. }
  1699. }
  1700. statusBar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1701. title = new CLabel(164, 27, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[13]);
  1702. costLabel = new CLabel(164, 220, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->jktexts[14]);
  1703. }
  1704. CPuzzleWindow::CPuzzleWindow(const int3 &GrailPos, double discoveredRatio):
  1705. CWindowObject(PLAYER_COLORED | BORDERED, "PUZZLE"),
  1706. grailPos(GrailPos),
  1707. currentAlpha(SDL_ALPHA_OPAQUE)
  1708. {
  1709. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1710. CCS->soundh->playSound(soundBase::OBELISK);
  1711. quitb = new CAdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CPuzzleWindow::close, this), 670, 538, "IOK6432.DEF", SDLK_RETURN);
  1712. quitb->assignedKeys.insert(SDLK_ESCAPE);
  1713. quitb->borderColor = Colors::METALLIC_GOLD;
  1714. quitb->borderEnabled = true;
  1715. new CPicture("PUZZLOGO", 607, 3);
  1716. new CLabel(700, 95, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[463]);
  1717. new CResDataBar("ARESBAR.bmp", 3, 575, 32, 2, 85, 85);
  1718. int faction = LOCPLINT->cb->getStartInfo()->playerInfos.find(LOCPLINT->playerID)->second.castle;
  1719. auto & puzzleMap = CGI->townh->factions[faction]->puzzleMap;
  1720. for(auto & elem : puzzleMap)
  1721. {
  1722. const SPuzzleInfo & info = elem;
  1723. auto piece = new CPicture(info.filename, info.x, info.y);
  1724. //piece that will slowly disappear
  1725. if(info.whenUncovered <= GameConstants::PUZZLE_MAP_PIECES * discoveredRatio)
  1726. {
  1727. piecesToRemove.push_back(piece);
  1728. piece->needRefresh = true;
  1729. piece->recActions = piece->recActions & ~SHOWALL;
  1730. #ifndef VCMI_SDL1
  1731. SDL_SetSurfaceBlendMode(piece->bg,SDL_BLENDMODE_BLEND);
  1732. #endif // VCMI_SDL1
  1733. }
  1734. }
  1735. }
  1736. void CPuzzleWindow::showAll(SDL_Surface * to)
  1737. {
  1738. int3 moveInt = int3(8, 9, 0);
  1739. Rect mapRect = genRect(544, 591, pos.x + 8, pos.y + 7);
  1740. CGI->mh->terrainRect
  1741. (grailPos - moveInt, adventureInt->anim,
  1742. &LOCPLINT->cb->getVisibilityMap(), true, adventureInt->heroAnim,
  1743. to, &mapRect, 0, 0, true, moveInt);
  1744. CWindowObject::showAll(to);
  1745. }
  1746. void CPuzzleWindow::show(SDL_Surface * to)
  1747. {
  1748. static int animSpeed = 2;
  1749. if (currentAlpha < animSpeed)
  1750. {
  1751. //animation done
  1752. for(auto & piece : piecesToRemove)
  1753. delete piece;
  1754. piecesToRemove.clear();
  1755. }
  1756. else
  1757. {
  1758. //update disappearing puzzles
  1759. for(auto & piece : piecesToRemove)
  1760. piece->setAlpha(currentAlpha);
  1761. currentAlpha -= animSpeed;
  1762. }
  1763. CWindowObject::show(to);
  1764. }
  1765. void CTransformerWindow::CItem::move()
  1766. {
  1767. if (left)
  1768. moveBy(Point(289, 0));
  1769. else
  1770. moveBy(Point(-289, 0));
  1771. left = !left;
  1772. }
  1773. void CTransformerWindow::CItem::clickLeft(tribool down, bool previousState)
  1774. {
  1775. if(previousState && (!down))
  1776. {
  1777. move();
  1778. parent->showAll(screen2);
  1779. }
  1780. }
  1781. void CTransformerWindow::CItem::update()
  1782. {
  1783. icon->setFrame(parent->army->getCreature(SlotID(id))->idNumber + 2);
  1784. }
  1785. CTransformerWindow::CItem::CItem(CTransformerWindow * parent, int size, int id):
  1786. id(id), size(size), parent(parent)
  1787. {
  1788. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1789. addUsedEvents(LCLICK);
  1790. left = true;
  1791. pos.w = 58;
  1792. pos.h = 64;
  1793. pos.x += 45 + (id%3)*83 + id/6*83;
  1794. pos.y += 109 + (id/3)*98;
  1795. icon = new CAnimImage("TWCRPORT", parent->army->getCreature(SlotID(id))->idNumber + 2);
  1796. new CLabel(28, 76,FONT_SMALL, CENTER, Colors::WHITE, boost::lexical_cast<std::string>(size));//stack size
  1797. }
  1798. void CTransformerWindow::makeDeal()
  1799. {
  1800. for (auto & elem : items)
  1801. if (!elem->left)
  1802. LOCPLINT->cb->trade(town, EMarketMode::CREATURE_UNDEAD, elem->id, 0, 0, hero);
  1803. }
  1804. void CTransformerWindow::addAll()
  1805. {
  1806. for (auto & elem : items)
  1807. if (elem->left)
  1808. elem->move();
  1809. showAll(screen2);
  1810. }
  1811. void CTransformerWindow::updateGarrisons()
  1812. {
  1813. for(auto & item : items)
  1814. {
  1815. item->update();
  1816. }
  1817. }
  1818. CTransformerWindow::CTransformerWindow(const CGHeroInstance * _hero, const CGTownInstance * _town):
  1819. CWindowObject(PLAYER_COLORED, "SKTRNBK"),
  1820. hero(_hero),
  1821. town(_town)
  1822. {
  1823. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1824. if (hero)
  1825. army = hero;
  1826. else
  1827. army = town;
  1828. for (int i=0; i<GameConstants::ARMY_SIZE; i++ )
  1829. if ( army->getCreature(SlotID(i)) )
  1830. items.push_back(new CItem(this, army->getStackCount(SlotID(i)), i));
  1831. all = new CAdventureMapButton(CGI->generaltexth->zelp[590],boost::bind(&CTransformerWindow::addAll,this), 146,416,"ALTARMY.DEF",SDLK_a);
  1832. convert= new CAdventureMapButton(CGI->generaltexth->zelp[591],boost::bind(&CTransformerWindow::makeDeal,this), 269,416,"ALTSACR.DEF",SDLK_RETURN);
  1833. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[592],boost::bind(&CTransformerWindow::close, this),392,416,"ICANCEL.DEF",SDLK_ESCAPE);
  1834. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1835. new CLabel(153, 29,FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[485]);//holding area
  1836. new CLabel(153+295, 29, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[486]);//transformer
  1837. new CTextBox(CGI->generaltexth->allTexts[487], Rect(26, 56, 255, 40), 0, FONT_MEDIUM, CENTER, Colors::YELLOW);//move creatures to create skeletons
  1838. new CTextBox(CGI->generaltexth->allTexts[488], Rect(320, 56, 255, 40), 0, FONT_MEDIUM, CENTER, Colors::YELLOW);//creatures here will become skeletons
  1839. }
  1840. void CUniversityWindow::CItem::clickLeft(tribool down, bool previousState)
  1841. {
  1842. if(previousState && (!down))
  1843. {
  1844. if ( state() != 2 )
  1845. return;
  1846. auto win = new CUnivConfirmWindow(parent, ID, LOCPLINT->cb->getResourceAmount(Res::GOLD) >= 2000);
  1847. GH.pushInt(win);
  1848. }
  1849. }
  1850. void CUniversityWindow::CItem::clickRight(tribool down, bool previousState)
  1851. {
  1852. if(down)
  1853. {
  1854. CRClickPopup::createAndPush(CGI->generaltexth->skillInfoTexts[ID][0],
  1855. new CComponent(CComponent::secskill, ID, 1));
  1856. }
  1857. }
  1858. void CUniversityWindow::CItem::hover(bool on)
  1859. {
  1860. if (on)
  1861. GH.statusbar->setText(CGI->generaltexth->skillName[ID]);
  1862. else
  1863. GH.statusbar->clear();
  1864. }
  1865. int CUniversityWindow::CItem::state()
  1866. {
  1867. if (parent->hero->getSecSkillLevel(SecondarySkill(ID)))//hero know this skill
  1868. return 1;
  1869. if (!parent->hero->canLearnSkill())//can't learn more skills
  1870. return 0;
  1871. if (parent->hero->type->heroClass->secSkillProbability[ID]==0)//can't learn this skill (like necromancy for most of non-necros)
  1872. return 0;
  1873. return 2;
  1874. }
  1875. void CUniversityWindow::CItem::showAll(SDL_Surface * to)
  1876. {
  1877. CPicture * bar;
  1878. switch (state())
  1879. {
  1880. case 0: bar = parent->red;
  1881. break;
  1882. case 1: bar = parent->yellow;
  1883. break;
  1884. case 2: bar = parent->green;
  1885. break;
  1886. default:bar = nullptr;
  1887. break;
  1888. }
  1889. assert(bar);
  1890. blitAtLoc(bar->bg, -28, -22, to);
  1891. blitAtLoc(bar->bg, -28, 48, to);
  1892. printAtMiddleLoc (CGI->generaltexth->skillName[ID], 22, -13, FONT_SMALL, Colors::WHITE,to);//Name
  1893. printAtMiddleLoc (CGI->generaltexth->levels[0], 22, 57, FONT_SMALL, Colors::WHITE,to);//Level(always basic)
  1894. CAnimImage::showAll(to);
  1895. }
  1896. CUniversityWindow::CItem::CItem(CUniversityWindow * _parent, int _ID, int X, int Y):
  1897. CAnimImage ("SECSKILL", _ID*3+3, 0, X, Y),
  1898. ID(_ID),
  1899. parent(_parent)
  1900. {
  1901. addUsedEvents(LCLICK | RCLICK | HOVER);
  1902. }
  1903. CUniversityWindow::CUniversityWindow(const CGHeroInstance * _hero, const IMarket * _market):
  1904. CWindowObject(PLAYER_COLORED, "UNIVERS1"),
  1905. hero(_hero),
  1906. market(_market)
  1907. {
  1908. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1909. green = new CPicture("UNIVGREN.PCX");
  1910. yellow = new CPicture("UNIVGOLD.PCX");//bars
  1911. red = new CPicture("UNIVRED.PCX");
  1912. green->recActions =
  1913. yellow->recActions =
  1914. red->recActions = DISPOSE;
  1915. CIntObject * titlePic = nullptr;
  1916. if (market->o->ID == Obj::TOWN)
  1917. titlePic = new CAnimImage(CGI->townh->factions[ETownType::CONFLUX]->town->clientInfo.buildingsIcons, BuildingID::MAGIC_UNIVERSITY);
  1918. else
  1919. titlePic = new CPicture("UNIVBLDG");
  1920. titlePic->center(Point(232 + pos.x, 76 + pos.y));
  1921. //Clerk speech
  1922. new CTextBox(CGI->generaltexth->allTexts[603], Rect(24, 129, 413, 70), 0, FONT_SMALL, CENTER, Colors::WHITE);
  1923. //University
  1924. new CLabel(231, 26, FONT_MEDIUM, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[602]);
  1925. std::vector<int> list = market->availableItemsIds(EMarketMode::RESOURCE_SKILL);
  1926. assert(list.size() == 4);
  1927. for (int i=0; i<list.size(); i++)//prepare clickable items
  1928. items.push_back(new CItem(this, list[i], 54+i*104, 234));
  1929. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[632],
  1930. boost::bind(&CUniversityWindow::close, this),200,313,"IOKAY.DEF",SDLK_RETURN);
  1931. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1932. }
  1933. CUnivConfirmWindow::CUnivConfirmWindow(CUniversityWindow * PARENT, int SKILL, bool available ):
  1934. CWindowObject(PLAYER_COLORED, "UNIVERS2.PCX"),
  1935. parent(PARENT)
  1936. {
  1937. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1938. std::string text = CGI->generaltexth->allTexts[608];
  1939. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  1940. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  1941. boost::replace_first(text, "%d", "2000");
  1942. new CTextBox(text, Rect(24, 129, 413, 70), 0, FONT_SMALL, CENTER, Colors::WHITE);//Clerk speech
  1943. new CLabel(230, 37, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth-> skillName[SKILL]);//Skill name
  1944. new CAnimImage("SECSKILL", SKILL*3+3, 0, 211, 51);//skill
  1945. new CLabel(230, 107, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->levels[1]);//Skill level
  1946. new CAnimImage("RESOURCE", Res::GOLD, 0, 210, 210);//gold
  1947. new CLabel(230, 267, FONT_SMALL, CENTER, Colors::WHITE, "2000");//Cost
  1948. std::string hoverText = CGI->generaltexth->allTexts[609];
  1949. boost::replace_first(hoverText, "%s", CGI->generaltexth->levels[0]+ " " + CGI->generaltexth->skillName[SKILL]);
  1950. text = CGI->generaltexth->zelp[633].second;
  1951. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  1952. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  1953. boost::replace_first(text, "%d", "2000");
  1954. confirm= new CAdventureMapButton(hoverText, text, boost::bind(&CUnivConfirmWindow::makeDeal, this, SKILL),
  1955. 148,299,"IBY6432.DEF",SDLK_RETURN);
  1956. confirm->block(!available);
  1957. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[631],boost::bind(&CUnivConfirmWindow::close, this),
  1958. 252,299,"ICANCEL.DEF",SDLK_ESCAPE);
  1959. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1960. }
  1961. void CUnivConfirmWindow::makeDeal(int skill)
  1962. {
  1963. LOCPLINT->cb->trade(parent->market->o, EMarketMode::RESOURCE_SKILL, 6, skill, 1, parent->hero);
  1964. close();
  1965. }
  1966. CHillFortWindow::CHillFortWindow(const CGHeroInstance *visitor, const CGObjectInstance *object):
  1967. CWindowObject(PLAYER_COLORED, "APHLFTBK"),
  1968. fort(object),
  1969. hero(visitor)
  1970. {
  1971. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1972. slotsCount=7;
  1973. resources = CDefHandler::giveDefEss("SMALRES.DEF");
  1974. new CLabel(325, 32, FONT_BIG, CENTER, Colors::YELLOW, fort->getObjectName());//Hill Fort
  1975. heroPic = new CHeroArea(30, 60, hero);
  1976. currState.resize(slotsCount+1);
  1977. costs.resize(slotsCount);
  1978. totalSumm.resize(GameConstants::RESOURCE_QUANTITY);
  1979. std::vector<std::string> files;
  1980. files += "APHLF1R.DEF", "APHLF1Y.DEF", "APHLF1G.DEF";
  1981. for (int i=0; i<slotsCount; i++)
  1982. {
  1983. currState[i] = getState(SlotID(i));
  1984. upgrade[i] = new CAdventureMapButton(getTextForSlot(SlotID(i)),"",boost::bind(&CHillFortWindow::makeDeal, this, SlotID(i)),
  1985. 107+i*76, 171, "", SDLK_1+i, &files);
  1986. upgrade[i]->block(currState[i] == -1);
  1987. }
  1988. files.clear();
  1989. files += "APHLF4R.DEF", "APHLF4Y.DEF", "APHLF4G.DEF";
  1990. currState[slotsCount] = getState(SlotID(slotsCount));
  1991. upgradeAll = new CAdventureMapButton(CGI->generaltexth->allTexts[432],"",boost::bind(&CHillFortWindow::makeDeal, this, SlotID(slotsCount)),
  1992. 30, 231, "", SDLK_0, &files);
  1993. quit = new CAdventureMapButton("","",boost::bind(&CHillFortWindow::close, this), 294, 275, "IOKAY.DEF", SDLK_RETURN);
  1994. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1995. garr = new CGarrisonInt(108, 60, 18, Point(),background->bg,Point(108,60),hero,nullptr);
  1996. updateGarrisons();
  1997. }
  1998. void CHillFortWindow::updateGarrisons()
  1999. {
  2000. for (int i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
  2001. totalSumm[i]=0;
  2002. for (int i=0; i<slotsCount; i++)
  2003. {
  2004. costs[i].clear();
  2005. int newState = getState(SlotID(i));
  2006. if (newState != -1)
  2007. {
  2008. UpgradeInfo info;
  2009. LOCPLINT->cb->getUpgradeInfo(hero, SlotID(i), info);
  2010. if (info.newID.size())//we have upgrades here - update costs
  2011. {
  2012. costs[i] = info.cost[0] * hero->getStackCount(SlotID(i));
  2013. totalSumm += costs[i];
  2014. }
  2015. }
  2016. currState[i] = newState;
  2017. upgrade[i]->setIndex(newState);
  2018. upgrade[i]->block(currState[i] == -1);
  2019. upgrade[i]->hoverTexts[0] = getTextForSlot(SlotID(i));
  2020. }
  2021. int newState = getState(SlotID(slotsCount));
  2022. currState[slotsCount] = newState;
  2023. upgradeAll->setIndex(newState);
  2024. garr->recreateSlots();
  2025. }
  2026. void CHillFortWindow::makeDeal(SlotID slot)
  2027. {
  2028. assert(slot.getNum()>=0);
  2029. int offset = (slot.getNum() == slotsCount)?2:0;
  2030. switch (currState[slot.getNum()])
  2031. {
  2032. case 0:
  2033. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[314 + offset],
  2034. std::vector<CComponent*>(), soundBase::sound_todo);
  2035. break;
  2036. case 1:
  2037. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[313 + offset],
  2038. std::vector<CComponent*>(), soundBase::sound_todo);
  2039. break;
  2040. case 2:
  2041. for (int i=0; i<slotsCount; i++)
  2042. if ( slot.getNum() ==i || ( slot.getNum() == slotsCount && currState[i] == 2 ) )//this is activated slot or "upgrade all"
  2043. {
  2044. UpgradeInfo info;
  2045. LOCPLINT->cb->getUpgradeInfo(hero, SlotID(i), info);
  2046. LOCPLINT->cb->upgradeCreature(hero, SlotID(i), info.newID[0]);
  2047. }
  2048. break;
  2049. }
  2050. }
  2051. void CHillFortWindow::showAll (SDL_Surface *to)
  2052. {
  2053. CWindowObject::showAll(to);
  2054. for ( int i=0; i<slotsCount; i++)
  2055. {
  2056. if ( currState[i] == 0 || currState[i] == 2 )
  2057. {
  2058. if ( costs[i].size() )//we have several elements
  2059. {
  2060. int curY = 128;//reverse iterator is used to display gold as first element
  2061. for(int j = costs[i].size()-1; j >= 0; j--)
  2062. {
  2063. int val = costs[i][j];
  2064. if(!val) continue;
  2065. blitAtLoc(resources->ourImages[j].bitmap, 104+76*i, curY, to);
  2066. printToLoc(boost::lexical_cast<std::string>(val), 168+76*i, curY+16, FONT_SMALL, Colors::WHITE, to);
  2067. curY += 20;
  2068. }
  2069. }
  2070. else//free upgrade - print gold image and "Free" text
  2071. {
  2072. blitAtLoc(resources->ourImages[6].bitmap, 104+76*i, 128, to);
  2073. printToLoc(CGI->generaltexth->allTexts[344], 168+76*i, 144, FONT_SMALL, Colors::WHITE, to);
  2074. }
  2075. }
  2076. }
  2077. for (int i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
  2078. {
  2079. if (totalSumm[i])//this resource is used - display it
  2080. {
  2081. blitAtLoc(resources->ourImages[i].bitmap, 104+76*i, 237, to);
  2082. printToLoc(boost::lexical_cast<std::string>(totalSumm[i]), 166+76*i, 253, FONT_SMALL, Colors::WHITE, to);
  2083. }
  2084. }
  2085. }
  2086. std::string CHillFortWindow::getTextForSlot(SlotID slot)
  2087. {
  2088. if ( !hero->getCreature(slot) )//we don`t have creature here
  2089. return "";
  2090. std::string str = CGI->generaltexth->allTexts[318];
  2091. int amount = hero->getStackCount(slot);
  2092. if ( amount == 1 )
  2093. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->nameSing);
  2094. else
  2095. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->namePl);
  2096. return str;
  2097. }
  2098. int CHillFortWindow::getState(SlotID slot)
  2099. {
  2100. TResources myRes = LOCPLINT->cb->getResourceAmount();
  2101. if ( slot.getNum() == slotsCount )//"Upgrade all" slot
  2102. {
  2103. bool allUpgraded = true;//All creatures are upgraded?
  2104. for (int i=0; i<slotsCount; i++)
  2105. allUpgraded &= currState[i] == 1 || currState[i] == -1;
  2106. if (allUpgraded)
  2107. return 1;
  2108. if(!totalSumm.canBeAfforded(myRes))
  2109. return 0;
  2110. return 2;
  2111. }
  2112. if (hero->slotEmpty(slot))//no creature here
  2113. return -1;
  2114. UpgradeInfo info;
  2115. LOCPLINT->cb->getUpgradeInfo(hero, slot, info);
  2116. if (!info.newID.size())//already upgraded
  2117. return 1;
  2118. if(!(info.cost[0] * hero->getStackCount(slot)).canBeAfforded(myRes))
  2119. return 0;
  2120. return 2;//can upgrade
  2121. }
  2122. CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner):
  2123. CWindowObject(PLAYER_COLORED | BORDERED, "TpRank"),
  2124. owner(_owner)
  2125. {
  2126. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2127. type |= BLOCK_ADV_HOTKEYS;
  2128. SThievesGuildInfo tgi; //info to be displayed
  2129. LOCPLINT->cb->getThievesGuildInfo(tgi, owner);
  2130. exitb = new CAdventureMapButton (CGI->generaltexth->allTexts[600], "", boost::bind(&CThievesGuildWindow::close,this), 748, 556, "TPMAGE1", SDLK_RETURN);
  2131. exitb->assignedKeys.insert(SDLK_ESCAPE);
  2132. statusBar = new CGStatusBar(3, 555, "TStatBar.bmp", 742);
  2133. resdatabar = new CMinorResDataBar();
  2134. resdatabar->pos.x += pos.x;
  2135. resdatabar->pos.y += pos.y;
  2136. //data for information table:
  2137. // fields[row][column] = list of id's of players for this box
  2138. static std::vector< std::vector< PlayerColor > > SThievesGuildInfo::* fields[] =
  2139. { &SThievesGuildInfo::numOfTowns, &SThievesGuildInfo::numOfHeroes, &SThievesGuildInfo::gold,
  2140. &SThievesGuildInfo::woodOre, &SThievesGuildInfo::mercSulfCrystGems, &SThievesGuildInfo::obelisks,
  2141. &SThievesGuildInfo::artifacts, &SThievesGuildInfo::army, &SThievesGuildInfo::income };
  2142. //printing texts & descriptions to background
  2143. for(int g=0; g<12; ++g)
  2144. {
  2145. int posY[] = {400, 460, 510};
  2146. int y;
  2147. if(g < 9)
  2148. y = 52 + 32*g;
  2149. else
  2150. y = posY[g-9];
  2151. std::string text = CGI->generaltexth->jktexts[24+g];
  2152. boost::algorithm::trim_if(text,boost::algorithm::is_any_of("\""));
  2153. new CLabel(135, y, FONT_MEDIUM, CENTER, Colors::YELLOW, text);
  2154. }
  2155. for(int g=1; g<tgi.playerColors.size(); ++g)
  2156. new CAnimImage("PRSTRIPS", g-1, 0, 250 + 66*g, 7);
  2157. for(int g=0; g<tgi.playerColors.size(); ++g)
  2158. new CLabel(283 + 66*g, 24, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[16+g]);
  2159. //printing flags
  2160. for(int g = 0; g < ARRAY_COUNT(fields); ++g) //by lines
  2161. {
  2162. for(int b=0; b<(tgi .* fields[g]).size(); ++b) //by places (1st, 2nd, ...)
  2163. {
  2164. std::vector<PlayerColor> &players = (tgi .* fields[g])[b]; //get players with this place in this line
  2165. //position of box
  2166. int xpos = 259 + 66 * b;
  2167. int ypos = 41 + 32 * g;
  2168. size_t rowLength[2]; //size of each row
  2169. rowLength[0] = std::min<size_t>(players.size(), 4);
  2170. rowLength[1] = players.size() - rowLength[0];
  2171. for (size_t j=0; j< 2; j++)
  2172. {
  2173. // origin of this row | offset for 2nd row| shift right for short rows
  2174. //if we have 2 rows, start either from mid or beginning (depending on count), otherwise center the flags
  2175. int rowStartX = xpos + (j ? 6 + (rowLength[j] < 3 ? 12 : 0) : 24 - 6 * rowLength[j]);
  2176. int rowStartY = ypos + (j ? 4 : 0);
  2177. for (size_t i=0; i< rowLength[j]; i++)
  2178. {
  2179. new CAnimImage("itgflags", players[i + j*4].getNum(), 0, rowStartX + i*12, rowStartY);
  2180. }
  2181. }
  2182. }
  2183. }
  2184. static const std::string colorToBox[] = {"PRRED.BMP", "PRBLUE.BMP", "PRTAN.BMP", "PRGREEN.BMP", "PRORANGE.BMP", "PRPURPLE.BMP", "PRTEAL.BMP", "PRROSE.bmp"};
  2185. //printing best hero
  2186. int counter = 0;
  2187. for(auto & iter : tgi.colorToBestHero)
  2188. {
  2189. if(iter.second.portrait >= 0)
  2190. {
  2191. new CPicture(colorToBox[iter.first.getNum()], 253 + 66 * counter, 334);
  2192. new CAnimImage("PortraitsSmall", iter.second.portrait, 0, 260 + 66 * counter, 360);
  2193. //TODO: r-click info:
  2194. // - r-click on hero
  2195. // - r-click on primary skill label
  2196. if(iter.second.details)
  2197. {
  2198. new CTextBox(CGI->generaltexth->allTexts[184], Rect(260 + 66*counter, 396, 52, 64),
  2199. 0, FONT_TINY, TOPLEFT, Colors::WHITE);
  2200. for (int i=0; i<iter.second.details->primskills.size(); ++i)
  2201. {
  2202. new CLabel(310 + 66 * counter, 407 + 11*i, FONT_TINY, BOTTOMRIGHT, Colors::WHITE,
  2203. boost::lexical_cast<std::string>(iter.second.details->primskills[i]));
  2204. }
  2205. }
  2206. }
  2207. counter++;
  2208. }
  2209. //printing best creature
  2210. counter = 0;
  2211. for(auto & it : tgi.bestCreature)
  2212. {
  2213. if(it.second >= 0)
  2214. new CAnimImage("TWCRPORT", it.second+2, 0, 255 + 66 * counter, 479);
  2215. counter++;
  2216. }
  2217. //printing personality
  2218. counter = 0;
  2219. for(auto & it : tgi.personality)
  2220. {
  2221. std::string text;
  2222. if(it.second == EAiTactic::NONE)
  2223. {
  2224. text = CGI->generaltexth->arraytxt[172];
  2225. }
  2226. else if(it.second != EAiTactic::RANDOM)
  2227. {
  2228. text = CGI->generaltexth->arraytxt[168 + it.second];
  2229. }
  2230. new CLabel(283 + 66*counter, 459, FONT_SMALL, CENTER, Colors::WHITE, text);
  2231. counter++;
  2232. }
  2233. }
  2234. void MoraleLuckBox::set(const IBonusBearer *node)
  2235. {
  2236. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2237. const int textId[] = {62, 88}; //eg %s \n\n\n {Current Luck Modifiers:}
  2238. const int noneTxtId = 108; //Russian version uses same text for neutral morale\luck
  2239. const int neutralDescr[] = {60, 86}; //eg {Neutral Morale} \n\n Neutral morale means your armies will neither be blessed with extra attacks or freeze in combat.
  2240. const int componentType[] = {CComponent::luck, CComponent::morale};
  2241. const int hoverTextBase[] = {7, 4};
  2242. const Bonus::BonusType bonusType[] = {Bonus::LUCK, Bonus::MORALE};
  2243. int (IBonusBearer::*getValue[])() const = {&IBonusBearer::LuckVal, &IBonusBearer::MoraleVal};
  2244. int mrlt = -9;
  2245. TModDescr mrl;
  2246. if (node)
  2247. {
  2248. node->getModifiersWDescr(mrl, bonusType[morale]);
  2249. bonusValue = (node->*getValue[morale])();
  2250. }
  2251. else
  2252. bonusValue = 0;
  2253. mrlt = (bonusValue>0)-(bonusValue<0); //signum: -1 - bad luck / morale, 0 - neutral, 1 - good
  2254. hoverText = CGI->generaltexth->heroscrn[hoverTextBase[morale] - mrlt];
  2255. baseType = componentType[morale];
  2256. text = CGI->generaltexth->arraytxt[textId[morale]];
  2257. boost::algorithm::replace_first(text,"%s",CGI->generaltexth->arraytxt[neutralDescr[morale]-mrlt]);
  2258. if (!mrl.size())
  2259. text += CGI->generaltexth->arraytxt[noneTxtId];
  2260. else
  2261. {
  2262. //it's a creature window
  2263. if ((morale && node->hasBonusOfType(Bonus::UNDEAD)) ||
  2264. node->hasBonusOfType(Bonus::BLOCK_MORALE) || node->hasBonusOfType(Bonus::NON_LIVING))
  2265. {
  2266. text += CGI->generaltexth->arraytxt[113]; //unaffected by morale
  2267. }
  2268. else
  2269. {
  2270. for(auto & elem : mrl)
  2271. {
  2272. if (elem.first) //no bonuses with value 0
  2273. text += "\n" + elem.second;
  2274. }
  2275. }
  2276. }
  2277. std::string imageName;
  2278. if (small)
  2279. imageName = morale ? "IMRL30": "ILCK30";
  2280. else
  2281. imageName = morale ? "IMRL42" : "ILCK42";
  2282. delete image;
  2283. image = new CAnimImage(imageName, bonusValue + 3);
  2284. image->moveBy(Point(pos.w/2 - image->pos.w/2, pos.h/2 - image->pos.h/2));//center icon
  2285. }
  2286. MoraleLuckBox::MoraleLuckBox(bool Morale, const Rect &r, bool Small):
  2287. image(nullptr),
  2288. morale(Morale),
  2289. small(Small)
  2290. {
  2291. bonusValue = 0;
  2292. pos = r + pos;
  2293. }
  2294. void CRClickPopup::clickRight(tribool down, bool previousState)
  2295. {
  2296. if(down)
  2297. return;
  2298. close();
  2299. }
  2300. void CRClickPopup::close()
  2301. {
  2302. GH.popIntTotally(this);
  2303. }
  2304. void CRClickPopup::createAndPush(const std::string &txt, const CInfoWindow::TCompsInfo &comps)
  2305. {
  2306. PlayerColor player = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1); //if no player, then use blue
  2307. CSimpleWindow * temp = new CInfoWindow(txt, player, comps);
  2308. temp->center(Point(GH.current->motion)); //center on mouse
  2309. temp->fitToScreen(10);
  2310. auto rcpi = new CRClickPopupInt(temp,true);
  2311. GH.pushInt(rcpi);
  2312. }
  2313. void CRClickPopup::createAndPush(const std::string &txt, CComponent * component)
  2314. {
  2315. CInfoWindow::TCompsInfo intComps;
  2316. intComps.push_back(component);
  2317. createAndPush(txt, intComps);
  2318. }
  2319. Point CInfoBoxPopup::toScreen(Point p)
  2320. {
  2321. vstd::abetween(p.x, adventureInt->terrain.pos.x + 100, adventureInt->terrain.pos.x + adventureInt->terrain.pos.w - 100);
  2322. vstd::abetween(p.y, adventureInt->terrain.pos.y + 100, adventureInt->terrain.pos.y + adventureInt->terrain.pos.h - 100);
  2323. return p;
  2324. }
  2325. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGTownInstance * town):
  2326. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "TOWNQVBK", toScreen(position))
  2327. {
  2328. InfoAboutTown iah;
  2329. LOCPLINT->cb->getTownInfo(town, iah);
  2330. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2331. new CTownTooltip(Point(9, 10), iah);
  2332. }
  2333. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGHeroInstance * hero):
  2334. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "HEROQVBK", toScreen(position))
  2335. {
  2336. InfoAboutHero iah;
  2337. LOCPLINT->cb->getHeroInfo(hero, iah);
  2338. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2339. new CHeroTooltip(Point(9, 10), iah);
  2340. }
  2341. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGGarrison * garr):
  2342. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "TOWNQVBK", toScreen(position))
  2343. {
  2344. InfoAboutTown iah;
  2345. LOCPLINT->cb->getTownInfo(garr, iah);
  2346. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2347. new CArmyTooltip(Point(9, 10), iah);
  2348. }
  2349. CIntObject * CRClickPopup::createInfoWin(Point position, const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  2350. {
  2351. if(!specific)
  2352. specific = adventureInt->selection;
  2353. assert(specific);
  2354. switch(specific->ID)
  2355. {
  2356. case Obj::HERO:
  2357. return new CInfoBoxPopup(position, dynamic_cast<const CGHeroInstance *>(specific));
  2358. case Obj::TOWN:
  2359. return new CInfoBoxPopup(position, dynamic_cast<const CGTownInstance *>(specific));
  2360. case Obj::GARRISON:
  2361. case Obj::GARRISON2:
  2362. return new CInfoBoxPopup(position, dynamic_cast<const CGGarrison *>(specific));
  2363. default:
  2364. return nullptr;
  2365. }
  2366. }
  2367. void CRClickPopup::createAndPush(const CGObjectInstance *obj, const Point &p, EAlignment alignment /*= BOTTOMRIGHT*/)
  2368. {
  2369. CIntObject *iWin = createInfoWin(p, obj); //try get custom infowindow for this obj
  2370. if(iWin)
  2371. GH.pushInt(iWin);
  2372. else
  2373. {
  2374. if (adventureInt->curHero())
  2375. CRClickPopup::createAndPush(obj->getHoverText(adventureInt->curHero()));
  2376. else
  2377. CRClickPopup::createAndPush(obj->getHoverText(LOCPLINT->playerID));
  2378. }
  2379. }
  2380. CRClickPopup::CRClickPopup()
  2381. {
  2382. addUsedEvents(RCLICK);
  2383. }
  2384. CRClickPopup::~CRClickPopup()
  2385. {
  2386. }
  2387. void CRClickPopupInt::show(SDL_Surface * to)
  2388. {
  2389. inner->show(to);
  2390. }
  2391. CRClickPopupInt::CRClickPopupInt( IShowActivatable *our, bool deleteInt )
  2392. {
  2393. CCS->curh->hide();
  2394. inner = our;
  2395. delInner = deleteInt;
  2396. }
  2397. CRClickPopupInt::~CRClickPopupInt()
  2398. {
  2399. // //workaround for hero window issue - if it's our interface, call dispose to properly reset it's state
  2400. // //TODO? it might be better to rewrite hero window so it will bee newed/deleted on opening / closing (not effort-worthy now, but on some day...?)
  2401. // if(LOCPLINT && inner == adventureInt->heroWindow)
  2402. // adventureInt->heroWindow->dispose();
  2403. if(delInner)
  2404. delete inner;
  2405. CCS->curh->show();
  2406. }
  2407. void CRClickPopupInt::showAll(SDL_Surface * to)
  2408. {
  2409. inner->showAll(to);
  2410. }