GUIClasses.cpp 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210
  1. #include "GUIClasses.h"
  2. #include "SDL_Extensions.h"
  3. #include "../stdafx.h"
  4. #include "CAdvmapInterface.h"
  5. #include "CBattleInterface.h"
  6. #include "../CCallback.h"
  7. #include "CCastleInterface.h"
  8. #include "CCursorHandler.h"
  9. #include "CGameInfo.h"
  10. #include "CHeroWindow.h"
  11. #include "CMessage.h"
  12. #include "CConfigHandler.h"
  13. #include "SDL_framerate.h"
  14. #include "CConfigHandler.h"
  15. #include "CCreatureAnimation.h"
  16. #include "Graphics.h"
  17. #include "../hch/CArtHandler.h"
  18. #include "../hch/CGeneralTextHandler.h"
  19. #include "../hch/CHeroHandler.h"
  20. #include "../hch/CLodHandler.h"
  21. #include "../hch/CObjectHandler.h"
  22. #include "../hch/CSpellHandler.h"
  23. #include "../hch/CTownHandler.h"
  24. #include "../lib/CondSh.h"
  25. #include "../lib/map.h"
  26. #include "../mapHandler.h"
  27. #include "../timeHandler.h"
  28. #include <boost/lexical_cast.hpp>
  29. #include <boost/algorithm/string.hpp>
  30. #include <boost/algorithm/string/replace.hpp>
  31. #include <boost/assign/std/vector.hpp>
  32. #include <boost/assign/list_of.hpp>
  33. #include <boost/date_time/posix_time/posix_time.hpp>
  34. //#include <boost/thread.hpp>
  35. #include <cmath>
  36. #include <queue>
  37. #include <sstream>
  38. #include "../lib/NetPacks.h"
  39. /*
  40. * GUIClasses.cpp, part of VCMI engine
  41. *
  42. * Authors: listed in file AUTHORS in main folder
  43. *
  44. * License: GNU General Public License v2.0 or later
  45. * Full text of license available in license.txt file, in main folder
  46. *
  47. */
  48. using namespace boost::assign;
  49. using namespace CSDL_Ext;
  50. extern TTF_Font * GEOR16;
  51. extern std::queue<SDL_Event*> events;
  52. extern boost::mutex eventsM;
  53. static StackState* getStackState(const CGObjectInstance *obj, int pos, bool town)
  54. {
  55. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(obj);
  56. if(!h) return NULL;
  57. StackState *pom = new StackState();
  58. pom->currentHealth = 0;
  59. pom->attackBonus = h->getPrimSkillLevel(0);
  60. pom->defenseBonus = h->getPrimSkillLevel(1);
  61. pom->luck = h->getCurrentLuck();
  62. pom->morale = h->getCurrentMorale(pos,town);
  63. pom->speedBonus = h->valOfBonuses(HeroBonus::STACKS_SPEED);
  64. return pom;
  65. }
  66. void CGarrisonSlot::hover (bool on)
  67. {
  68. Hoverable::hover(on);
  69. if(on)
  70. {
  71. std::string temp;
  72. if(creature)
  73. {
  74. if(owner->highlighted)
  75. {
  76. if(owner->highlighted == this)
  77. {
  78. temp = CGI->generaltexth->tcommands[4];
  79. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  80. }
  81. else if (owner->highlighted->creature == creature)
  82. {
  83. temp = CGI->generaltexth->tcommands[2];
  84. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  85. }
  86. else if (owner->highlighted->creature)
  87. {
  88. temp = CGI->generaltexth->tcommands[7];
  89. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  90. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  91. }
  92. else
  93. {
  94. tlog2 << "Warning - shouldn't be - highlighted void slot "<<owner->highlighted<<std::endl;
  95. tlog2 << "Highlighted set to NULL"<<std::endl;
  96. owner->highlighted = NULL;
  97. }
  98. }
  99. else
  100. {
  101. if(upg)
  102. {
  103. temp = CGI->generaltexth->tcommands[32];
  104. }
  105. else
  106. {
  107. temp = CGI->generaltexth->tcommands[12];
  108. }
  109. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  110. };
  111. }
  112. else
  113. {
  114. if(owner->highlighted)
  115. {
  116. const CArmedInstance *highl = owner->highlighted->getObj();
  117. if( highl->needsLastStack() //we are moving stack from hero's
  118. && highl->army.slots.size() == 1 //it's only stack
  119. && owner->highlighted->upg != upg //we're moving it to the other garrison
  120. )
  121. {
  122. temp = CGI->generaltexth->tcommands[5]; //cannot move last stack!
  123. }
  124. else
  125. {
  126. temp = CGI->generaltexth->tcommands[6];
  127. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  128. }
  129. }
  130. else
  131. {
  132. temp = CGI->generaltexth->tcommands[11];
  133. }
  134. }
  135. LOCPLINT->statusbar->print(temp);
  136. }
  137. else
  138. {
  139. LOCPLINT->statusbar->clear();
  140. }
  141. }
  142. const CArmedInstance * CGarrisonSlot::getObj()
  143. {
  144. return (!upg)?(owner->oup):(owner->odown);
  145. }
  146. void CGarrisonSlot::clickRight (tribool down)
  147. {
  148. StackState *pom = getStackState(getObj(),ID, LOCPLINT->topInt() == LOCPLINT->castleInt);
  149. if(down && creature)
  150. {
  151. LOCPLINT->pushInt(new CCreInfoWindow(creature->idNumber,0,count,pom,0,0,NULL));
  152. }
  153. delete pom;
  154. }
  155. void CGarrisonSlot::clickLeft(tribool down)
  156. {
  157. if(owner->ignoreEvent)
  158. {
  159. owner->ignoreEvent = false;
  160. return;
  161. }
  162. if(down)
  163. {
  164. bool refr = false;
  165. if(owner->highlighted)
  166. {
  167. if(owner->highlighted == this) //view info
  168. {
  169. StackState *pom2 = getStackState(getObj(), ID, LOCPLINT->topInt() == LOCPLINT->castleInt);
  170. UpgradeInfo pom = LOCPLINT->cb->getUpgradeInfo(getObj(), ID);
  171. CCreInfoWindow *creWindow = NULL;
  172. if(pom.oldID>=0) //upgrade is possible
  173. {
  174. creWindow = new CCreInfoWindow(
  175. creature->idNumber,1,count,pom2,
  176. boost::bind(&CCallback::upgradeCreature,LOCPLINT->cb,getObj(),ID,pom.newID[0]), //bind upgrade function
  177. boost::bind(&CCallback::dismissCreature,LOCPLINT->cb,getObj(),ID),&pom);
  178. }
  179. else
  180. {
  181. creWindow = new CCreInfoWindow(
  182. creature->idNumber,1,count,pom2,0,
  183. boost::bind(&CCallback::dismissCreature,LOCPLINT->cb,getObj(),ID), NULL);
  184. }
  185. LOCPLINT->pushInt(creWindow);
  186. owner->highlighted = NULL;
  187. show(screen2);
  188. refr = true;
  189. delete pom2;
  190. }
  191. else if((owner->splitting || LOCPLINT->shiftPressed())
  192. && (!creature
  193. || (creature == owner->highlighted->creature))
  194. ) //we want to split
  195. {
  196. owner->p2 = ID; //store the second stack pos
  197. owner->pb = upg;//store the second stack owner (up or down army)
  198. owner->splitting = false;
  199. int totalAmount = owner->highlighted->count;
  200. if(creature)
  201. totalAmount += count;
  202. int last = -1;
  203. if(upg != owner->highlighted->upg) //not splitting within same army
  204. {
  205. if(owner->highlighted->getObj()->army.slots.size() == 1
  206. && owner->highlighted->getObj()->needsLastStack() )
  207. {
  208. last = 0;
  209. }
  210. if(getObj()->army.slots.size() == 1
  211. && getObj()->needsLastStack() )
  212. {
  213. last += 2;
  214. }
  215. }
  216. CSplitWindow * spw = new CSplitWindow(owner->highlighted->creature->idNumber, totalAmount, owner, last, count);
  217. LOCPLINT->pushInt(spw);
  218. refr = true;
  219. }
  220. else if(creature != owner->highlighted->creature) //swap
  221. {
  222. LOCPLINT->cb->swapCreatures(
  223. (!upg)?(owner->oup):(owner->odown),
  224. (!owner->highlighted->upg)?(owner->oup):(owner->odown),
  225. ID,owner->highlighted->ID);
  226. }
  227. else //merge
  228. {
  229. LOCPLINT->cb->mergeStacks(
  230. (!owner->highlighted->upg)?(owner->oup):(owner->odown),
  231. (!upg)?(owner->oup):(owner->odown),
  232. owner->highlighted->ID,ID);
  233. }
  234. }
  235. else //highlight
  236. {
  237. if(creature)
  238. owner->highlighted = this;
  239. show(screen2);
  240. refr = true;
  241. }
  242. if(refr) {hover(false); hover(true); } //to refresh statusbar
  243. }
  244. }
  245. void CGarrisonSlot::activate()
  246. {
  247. if(!active) active=true;
  248. else return;
  249. ClickableL::activate();
  250. ClickableR::activate();
  251. Hoverable::activate();
  252. }
  253. void CGarrisonSlot::deactivate()
  254. {
  255. if(active) active=false;
  256. else return;
  257. ClickableL::deactivate();
  258. ClickableR::deactivate();
  259. Hoverable::deactivate();
  260. }
  261. CGarrisonSlot::CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg, const CCreature * Creature, int Count)
  262. {
  263. active = false;
  264. upg = Upg;
  265. count = Count;
  266. ID = IID;
  267. creature = Creature;
  268. pos.x = x;
  269. pos.y = y;
  270. pos.w = 58;
  271. pos.h = 64;
  272. owner = Owner;
  273. }
  274. CGarrisonSlot::~CGarrisonSlot()
  275. {
  276. if(active)
  277. deactivate();
  278. }
  279. void CGarrisonSlot::show(SDL_Surface * to)
  280. {
  281. if(creature)
  282. {
  283. char buf[15];
  284. SDL_itoa(count,buf,10);
  285. blitAt(graphics->bigImgs[creature->idNumber],pos,to);
  286. printToWR(buf,pos.x+56,pos.y+62,GEOR16,zwykly,to);
  287. if((owner->highlighted==this)
  288. || (owner->splitting && owner->highlighted->creature == creature))
  289. {
  290. blitAt(graphics->bigImgs[-1],pos,to);
  291. }
  292. }
  293. else //empty slot
  294. {
  295. SDL_Rect jakis1 = genRect(pos.h,pos.w,owner->offx+ID*(pos.w+owner->interx),owner->offy+upg*(pos.h+owner->intery)),
  296. jakis2 = pos;
  297. SDL_BlitSurface(owner->sur,&jakis1,to,&jakis2);
  298. if(owner->splitting)
  299. blitAt(graphics->bigImgs[-1],pos,to);
  300. }
  301. }
  302. CGarrisonInt::~CGarrisonInt()
  303. {
  304. if(sup)
  305. {
  306. for(size_t i=0;i<sup->size();i++)
  307. {
  308. delete (*sup)[i];
  309. }
  310. delete sup;
  311. }
  312. if(sdown)
  313. {
  314. for(size_t i=0;i<sdown->size();i++)
  315. {
  316. delete (*sdown)[i]; //XXX what about smartpointers? boost or auto_ptr from std
  317. }
  318. delete sdown;
  319. }
  320. }
  321. void CGarrisonInt::show(SDL_Surface * to)
  322. {
  323. if(sup)
  324. {
  325. for(size_t i = 0; i<sup->size(); i++)
  326. {
  327. if((*sup)[i])
  328. {
  329. (*sup)[i]->show(to);
  330. }
  331. }
  332. }
  333. if(sdown)
  334. {
  335. for(size_t i = 0; i<sdown->size(); i++)
  336. {
  337. if((*sdown)[i])
  338. {
  339. (*sdown)[i]->show(to);
  340. }
  341. }
  342. }
  343. }
  344. void CGarrisonInt::deactiveteSlots()
  345. {
  346. if(sup)
  347. {
  348. for(int i = 0; i<sup->size(); i++)
  349. {
  350. if((*sup)[i])
  351. {
  352. (*sup)[i]->deactivate();
  353. }
  354. }
  355. }
  356. if(sdown)
  357. {
  358. for(int i = 0; i<sdown->size(); i++)
  359. {
  360. if((*sdown)[i])
  361. {
  362. (*sdown)[i]->deactivate();
  363. }
  364. }
  365. }
  366. }
  367. void CGarrisonInt::activeteSlots()
  368. {
  369. if(sup)
  370. {
  371. for(int i = 0; i<sup->size(); i++)
  372. {
  373. if((*sup)[i])
  374. {
  375. (*sup)[i]->activate();
  376. }
  377. }
  378. }
  379. if(sdown)
  380. {
  381. for(int i = 0; i<sdown->size(); i++)
  382. {
  383. if((*sdown)[i])
  384. {
  385. (*sdown)[i]->activate();
  386. }
  387. }
  388. }
  389. }
  390. void CGarrisonInt::createSlots()
  391. {
  392. if(set1)
  393. {
  394. sup = new std::vector<CGarrisonSlot*>(7,(CGarrisonSlot *)(NULL));
  395. for
  396. (std::map<si32,std::pair<ui32,si32> >::const_iterator i=set1->slots.begin();
  397. i!=set1->slots.end(); i++)
  398. {
  399. (*sup)[i->first] =
  400. new CGarrisonSlot(this, pos.x + (i->first*(58+interx)), pos.y,i->first, 0,
  401. &CGI->creh->creatures[i->second.first],i->second.second);
  402. }
  403. for(int i=0; i<sup->size(); i++)
  404. if((*sup)[i] == NULL)
  405. (*sup)[i] = new CGarrisonSlot(this, pos.x + (i*(58+interx)), pos.y,i,0,NULL, 0);
  406. }
  407. if(set2)
  408. {
  409. sdown = new std::vector<CGarrisonSlot*>(7,(CGarrisonSlot *)(NULL));
  410. for
  411. (std::map<si32,std::pair<ui32,si32> >::const_iterator i=set2->slots.begin();
  412. i!=set2->slots.end(); i++)
  413. {
  414. (*sdown)[i->first] =
  415. new CGarrisonSlot(this, pos.x + (i->first*(58+interx)), pos.y + 64 + intery,i->first,1,
  416. &CGI->creh->creatures[i->second.first],i->second.second);
  417. }
  418. for(int i=0; i<sdown->size(); i++)
  419. if((*sdown)[i] == NULL)
  420. (*sdown)[i] = new CGarrisonSlot(this, pos.x + (i*(58+interx)), pos.y + 64 + intery,i,1, NULL, 0);
  421. }
  422. }
  423. void CGarrisonInt::deleteSlots()
  424. {
  425. if(sup)
  426. {
  427. for(int i = 0; i<sup->size(); i++)
  428. {
  429. if((*sup)[i])
  430. {
  431. delete (*sup)[i];
  432. }
  433. }
  434. delete sup;
  435. sup = NULL;
  436. }
  437. if(sdown)
  438. {
  439. for(int i = 0; i<sdown->size(); i++)
  440. {
  441. if((*sdown)[i])
  442. {
  443. delete (*sdown)[i];
  444. }
  445. }
  446. delete sdown;
  447. sdown = NULL;
  448. }
  449. }
  450. void CGarrisonInt::recreateSlots()
  451. {
  452. splitting = false;
  453. highlighted = NULL;
  454. if(active)
  455. {
  456. deactiveteSlots();
  457. }
  458. deleteSlots();
  459. createSlots();
  460. if(active)
  461. {
  462. //ignoreEvent = true;
  463. activeteSlots();
  464. //show(screen2);
  465. }
  466. }
  467. void CGarrisonInt::splitClick()
  468. {
  469. if(!highlighted)
  470. return;
  471. splitting = !splitting;
  472. show(screen2);
  473. }
  474. void CGarrisonInt::splitStacks(int am2)
  475. {
  476. LOCPLINT->cb->splitStack(
  477. (highlighted->upg)?(odown):(oup),
  478. (pb)?(odown):(oup),
  479. highlighted->ID,
  480. p2,
  481. am2);
  482. }
  483. CGarrisonInt::CGarrisonInt(int x, int y, int inx, int iny, SDL_Surface *&pomsur, int OX, int OY, const CArmedInstance *s1,
  484. const CArmedInstance *s2)
  485. :interx(inx),intery(iny),highlighted(NULL),sur(pomsur),offx(OX),offy(OY),sup(NULL),
  486. sdown(NULL),oup(s1),odown(s2)
  487. {
  488. active = false;
  489. splitting = false;
  490. set1 = LOCPLINT->cb->getGarrison(s1);
  491. set2 = LOCPLINT->cb->getGarrison(s2);
  492. ignoreEvent = false;
  493. update = true;
  494. pos.x=(x);
  495. pos.y=(y);
  496. pos.w=(58);
  497. pos.h=(64);
  498. createSlots();
  499. }
  500. void CGarrisonInt::activate()
  501. {
  502. active = true;
  503. if(sup)
  504. {
  505. for(int i = 0; i<sup->size(); i++)
  506. if((*sup)[i])
  507. (*sup)[i]->activate();
  508. }
  509. if(sdown)
  510. {
  511. for(int i = 0; i<sdown->size(); i++)
  512. if((*sdown)[i])
  513. (*sdown)[i]->activate();
  514. }
  515. }
  516. void CGarrisonInt::deactivate()
  517. {
  518. active = false;
  519. deactiveteSlots();
  520. }
  521. CInfoWindow::CInfoWindow(std::string text, int player, int charperline, const std::vector<SComponent*> &comps, std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons, bool delComps)
  522. {
  523. ID = -1;
  524. this->delComps = delComps;
  525. for(int i=0;i<Buttons.size();i++)
  526. {
  527. buttons.push_back(new AdventureMapButton("","",boost::bind(&CInfoWindow::close,this),0,0,Buttons[i].first));
  528. buttons[i]->callback.add(Buttons[i].second); //each button will close the window apart from call-defined actions
  529. }
  530. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  531. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  532. for(int i=0;i<comps.size();i++)
  533. {
  534. components.push_back(comps[i]);
  535. }
  536. CMessage::drawIWindow(this,text,player,charperline);
  537. }
  538. CInfoWindow::CInfoWindow()
  539. {
  540. ID = -1;
  541. delComps = false;
  542. }
  543. void CInfoWindow::close()
  544. {
  545. LOCPLINT->popIntTotally(this);
  546. LOCPLINT->showingDialog->setn(false);
  547. }
  548. void CInfoWindow::show(SDL_Surface * to)
  549. {
  550. CSimpleWindow::show(to);
  551. for(int i=0;i<buttons.size();i++)
  552. buttons[i]->show(to);
  553. }
  554. CInfoWindow::~CInfoWindow()
  555. {
  556. if(delComps)
  557. {
  558. for (int i=0;i<components.size();i++)
  559. delete components[i];
  560. }
  561. for(int i=0;i<buttons.size();i++)
  562. delete buttons[i];
  563. }
  564. void CInfoWindow::activate()
  565. {
  566. for (int i=0;i<components.size();i++)
  567. components[i]->activate();
  568. for(int i=0;i<buttons.size();i++)
  569. buttons[i]->activate();
  570. }
  571. void CInfoWindow::deactivate()
  572. {
  573. for (int i=0;i<components.size();i++)
  574. components[i]->deactivate();
  575. for(int i=0;i<buttons.size();i++)
  576. buttons[i]->deactivate();
  577. }
  578. void CRClickPopup::clickRight (tribool down)
  579. {
  580. if(down)
  581. return;
  582. close();
  583. }
  584. void CRClickPopup::activate()
  585. {
  586. ClickableR::activate();
  587. }
  588. void CRClickPopup::deactivate()
  589. {
  590. ClickableR::deactivate();
  591. }
  592. void CRClickPopup::close()
  593. {
  594. LOCPLINT->popIntTotally(this);
  595. }
  596. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  597. :free(Free),bitmap(Bitmap)
  598. {
  599. pos.x = x;
  600. pos.y = y;
  601. pos.h = bitmap->h;
  602. pos.w = bitmap->w;
  603. // Put the window back on screen if necessary
  604. amax(pos.x, 0);
  605. amax(pos.y, 0);
  606. amin(pos.x, conf.cc.resx - bitmap->w);
  607. amin(pos.y, conf.cc.resy - bitmap->h);
  608. }
  609. CInfoPopup::CInfoPopup(SDL_Surface *Bitmap, bool Free)
  610. {
  611. free=Free;
  612. bitmap=Bitmap;
  613. }
  614. void CInfoPopup::close()
  615. {
  616. if(free)
  617. SDL_FreeSurface(bitmap);
  618. LOCPLINT->popIntTotally(this);
  619. }
  620. void CInfoPopup::show(SDL_Surface * to)
  621. {
  622. blitAt(bitmap,pos.x,pos.y,to);
  623. }
  624. void SComponent::init(Etype Type, int Subtype, int Val)
  625. {
  626. std::ostringstream oss;
  627. switch (Type)
  628. {
  629. case artifact:
  630. description = CGI->arth->artifacts[Subtype].Description();
  631. subtitle = CGI->arth->artifacts[Subtype].Name();
  632. break;
  633. case primskill:
  634. oss << ((Val>0)?("+"):("-")) << Val << " ";
  635. if(Subtype < 4)
  636. {
  637. description = CGI->generaltexth->arraytxt[2+Subtype];
  638. oss << CGI->generaltexth->primarySkillNames[Subtype];
  639. }
  640. else if(Subtype == 5) //spell points
  641. {
  642. description = CGI->generaltexth->allTexts[149];
  643. oss << CGI->generaltexth->allTexts[387];
  644. }
  645. else
  646. {
  647. tlog1 << "Wrong subtype=" << Subtype << std::endl;
  648. }
  649. subtitle = oss.str();
  650. break;
  651. case secskill44: case secskill:
  652. subtitle += CGI->generaltexth->levels[Val-1] + " " + CGI->generaltexth->skillName[Subtype];
  653. description = CGI->generaltexth->skillInfoTexts[Subtype][Val-1];
  654. break;
  655. case resource:
  656. description = CGI->generaltexth->allTexts[242];
  657. oss << Val;
  658. subtitle = oss.str();
  659. break;
  660. case spell:
  661. description = CGI->spellh->spells[Subtype].descriptions[Val];
  662. subtitle = CGI->spellh->spells[Subtype].name;
  663. break;
  664. case creature:
  665. subtitle = boost::lexical_cast<std::string>(Val) + " " + CGI->creh->creatures[Subtype].*(val != 1 ? &CCreature::namePl : &CCreature::nameSing);
  666. break;
  667. case experience:
  668. description = CGI->generaltexth->allTexts[241];
  669. oss << Val ;
  670. if(Subtype && Val==1)
  671. {
  672. subtitle = CGI->generaltexth->allTexts[442];
  673. }
  674. else
  675. {
  676. subtitle = oss.str();
  677. }
  678. break;
  679. }
  680. type = Type;
  681. subtype = Subtype;
  682. val = Val;
  683. SDL_Surface * temp = this->getImg();
  684. if(!temp)
  685. {
  686. tlog1 << "Error: cannot find graphic for component with id=" << type << " subid=" << subtype << " val=" << val << std::endl;
  687. return;
  688. }
  689. pos.w = temp->w;
  690. pos.h = temp->h;
  691. }
  692. SComponent::SComponent(Etype Type, int Subtype, int Val)
  693. {
  694. init(Type,Subtype,Val);
  695. }
  696. SComponent::SComponent(const Component &c)
  697. {
  698. if(c.id==5)
  699. init(experience,c.subtype,c.val);
  700. else if(c.id == Component::SPELL)
  701. init(spell,c.subtype,c.val);
  702. else
  703. init((Etype)c.id,c.subtype,c.val);
  704. if(c.id==2 && c.when==-1)
  705. subtitle += CGI->generaltexth->allTexts[3].substr(2,CGI->generaltexth->allTexts[3].length()-2);
  706. }
  707. void SComponent::show(SDL_Surface * to)
  708. {
  709. blitAt(getImg(),pos.x,pos.y,to);
  710. }
  711. SDL_Surface * SComponent::getImg()
  712. {
  713. switch (type)
  714. {
  715. case artifact:
  716. return graphics->artDefs->ourImages[subtype].bitmap;
  717. break;
  718. case primskill:
  719. return graphics->pskillsb->ourImages[subtype].bitmap;
  720. break;
  721. case secskill44:
  722. return graphics->abils44->ourImages[subtype*3 + 3 + val - 1].bitmap;
  723. break;
  724. case secskill:
  725. return graphics->abils82->ourImages[subtype*3 + 3 + val - 1].bitmap;
  726. break;
  727. case resource:
  728. return graphics->resources->ourImages[subtype].bitmap;
  729. break;
  730. case experience:
  731. return graphics->pskillsb->ourImages[4].bitmap;
  732. break;
  733. case morale:
  734. return graphics->morale82->ourImages[val+3].bitmap;
  735. break;
  736. case luck:
  737. return graphics->luck82->ourImages[val+3].bitmap;
  738. break;
  739. case spell:
  740. return graphics->spellscr->ourImages[subtype].bitmap;
  741. break;
  742. case creature:
  743. return graphics->bigImgs[subtype];
  744. }
  745. return NULL;
  746. }
  747. void SComponent::clickRight (tribool down)
  748. {
  749. if(description.size())
  750. LOCPLINT->adventureInt->handleRightClick(description,down,this);
  751. }
  752. void SComponent::activate()
  753. {
  754. ClickableR::activate();
  755. }
  756. void SComponent::deactivate()
  757. {
  758. ClickableR::deactivate();
  759. }
  760. void CSelectableComponent::clickLeft(tribool down)
  761. {
  762. if (down)
  763. {
  764. if(onSelect)
  765. onSelect();
  766. }
  767. }
  768. void CSelectableComponent::init(SDL_Surface * Border)
  769. {
  770. SDL_Surface * symb = SComponent::getImg();
  771. myBitmap = CSDL_Ext::newSurface(symb->w+2,symb->h+2,screen);
  772. SDL_SetColorKey(myBitmap,SDL_SRCCOLORKEY,SDL_MapRGB(myBitmap->format,0,255,255));
  773. blitAt(symb,1,1,myBitmap);
  774. if (Border) //use custom border
  775. {
  776. border = Border;
  777. customB = true;
  778. }
  779. else //we need to draw border
  780. {
  781. customB = false;
  782. border = CSDL_Ext::newSurface(symb->w+2,symb->h+2,screen);
  783. SDL_FillRect(border,NULL,0x00FFFF);
  784. for (int i=0;i<border->w;i++)
  785. {
  786. SDL_PutPixelWithoutRefresh(border,i,0,239,215,123);
  787. SDL_PutPixelWithoutRefresh(border,i,(border->h)-1,239,215,123);
  788. }
  789. for (int i=0;i<border->h;i++)
  790. {
  791. SDL_PutPixelWithoutRefresh(border,0,i,239,215,123);
  792. SDL_PutPixelWithoutRefresh(border,(border->w)-1,i,239,215,123);
  793. }
  794. SDL_SetColorKey(border,SDL_SRCCOLORKEY,SDL_MapRGB(border->format,0,255,255));
  795. }
  796. selected = false;
  797. }
  798. CSelectableComponent::CSelectableComponent(const Component &c, boost::function<void()> OnSelect, SDL_Surface * Border)
  799. :SComponent(c),onSelect(OnSelect)
  800. {
  801. init(Border);
  802. }
  803. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, boost::function<void()> OnSelect, SDL_Surface * Border)
  804. :SComponent(Type,Sub,Val),onSelect(OnSelect)
  805. {
  806. init(Border);
  807. }
  808. CSelectableComponent::~CSelectableComponent()
  809. {
  810. SDL_FreeSurface(myBitmap);
  811. if (!customB)
  812. SDL_FreeSurface(border);
  813. }
  814. void CSelectableComponent::activate()
  815. {
  816. KeyInterested::activate();
  817. SComponent::activate();
  818. ClickableL::activate();
  819. }
  820. void CSelectableComponent::deactivate()
  821. {
  822. KeyInterested::deactivate();
  823. SComponent::deactivate();
  824. ClickableL::deactivate();
  825. }
  826. SDL_Surface * CSelectableComponent::getImg()
  827. {
  828. return myBitmap;
  829. }
  830. void CSelectableComponent::select(bool on)
  831. {
  832. if(on != selected)
  833. {
  834. SDL_FillRect(myBitmap,NULL,0x000000);
  835. blitAt(SComponent::getImg(),1,1,myBitmap);
  836. if (on)
  837. {
  838. blitAt(border,0,0,myBitmap);
  839. }
  840. selected = on;
  841. return;
  842. }
  843. else
  844. {
  845. return;
  846. }
  847. }
  848. void CSelectableComponent::show(SDL_Surface * to)
  849. {
  850. blitAt(myBitmap,pos.x,pos.y,to);
  851. printAtMiddleWB(subtitle,pos.x+pos.w/2,pos.y+pos.h+14,GEOR13,12,zwykly,to);
  852. }
  853. void CSimpleWindow::show(SDL_Surface * to)
  854. {
  855. blitAt(bitmap,pos.x,pos.y,to);
  856. }
  857. CSimpleWindow::~CSimpleWindow()
  858. {
  859. if (bitmap)
  860. {
  861. SDL_FreeSurface(bitmap);
  862. bitmap=NULL;
  863. }
  864. }
  865. void CSelWindow::selectionChange(unsigned to)
  866. {
  867. for (unsigned i=0;i<components.size();i++)
  868. {
  869. CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
  870. if (!pom)
  871. continue;
  872. pom->select(i==to);
  873. blitAt(pom->getImg(),pom->pos.x-pos.x,pom->pos.y-pos.y,bitmap);
  874. }
  875. }
  876. CSelWindow::CSelWindow(const std::string &text, int player, int charperline, const std::vector<CSelectableComponent*> &comps, const std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons, int askID)
  877. {
  878. ID = askID;
  879. for(int i=0;i<Buttons.size();i++)
  880. {
  881. buttons.push_back(new AdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  882. if(!i && askID >= 0)
  883. buttons.back()->callback += boost::bind(&CSelWindow::madeChoice,this);
  884. buttons[i]->callback += boost::bind(&CInfoWindow::close,this); //each button will close the window apart from call-defined actions
  885. }
  886. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  887. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  888. if(buttons.size() > 1 && askID >= 0) //cancel button functionality
  889. buttons.back()->callback += boost::bind(&ICallback::selectionMade,LOCPLINT->cb,0,askID);
  890. for(int i=0;i<comps.size();i++)
  891. {
  892. components.push_back(comps[i]);
  893. comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
  894. if(i<9)
  895. comps[i]->assignedKeys.insert(SDLK_1+i);
  896. }
  897. CMessage::drawIWindow(this,text,player,charperline);
  898. }
  899. void CSelWindow::madeChoice()
  900. {
  901. if(ID < 0)
  902. return;
  903. int ret = -1;
  904. for (int i=0;i<components.size();i++)
  905. {
  906. if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
  907. {
  908. ret = i;
  909. }
  910. }
  911. LOCPLINT->cb->selectionMade(ret+1,ID);
  912. }
  913. CStatusBar::CStatusBar(int x, int y, std::string name, int maxw)
  914. {
  915. bg=BitmapHandler::loadBitmap(name);
  916. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  917. pos.x=x;
  918. pos.y=y;
  919. if(maxw >= 0)
  920. pos.w = std::min(bg->w,maxw);
  921. else
  922. pos.w=bg->w;
  923. pos.h=bg->h;
  924. middlex=(pos.w/2)+x;
  925. middley=(bg->h/2)+y;
  926. }
  927. CStatusBar::~CStatusBar()
  928. {
  929. SDL_FreeSurface(bg);
  930. }
  931. void CStatusBar::clear()
  932. {
  933. if(LOCPLINT->cingconsole->enteredText == "") //for appropriate support for in-game console
  934. {
  935. current="";
  936. show(screen);
  937. }
  938. }
  939. void CStatusBar::print(const std::string & text)
  940. {
  941. if(LOCPLINT->cingconsole->enteredText == "" || text == LOCPLINT->cingconsole->enteredText) //for appropriate support for in-game console
  942. {
  943. current=text;
  944. show(LOCPLINT->topInt()==LOCPLINT->adventureInt ? screen : screen2); //if there are now windows opened, update statusbar on screen, else to cache surface
  945. }
  946. }
  947. void CStatusBar::show(SDL_Surface * to)
  948. {
  949. SDL_Rect pom = genRect(pos.h,pos.w,pos.x,pos.y);
  950. SDL_BlitSurface(bg,&genRect(pos.h,pos.w,0,0),to,&pom);
  951. printAtMiddle(current,middlex,middley,GEOR13,zwykly,to);
  952. }
  953. std::string CStatusBar::getCurrent()
  954. {
  955. return current;
  956. }
  957. void CList::activate()
  958. {
  959. ClickableL::activate();
  960. ClickableR::activate();
  961. Hoverable::activate();
  962. KeyInterested::activate();
  963. MotionInterested::activate();
  964. };
  965. void CList::deactivate()
  966. {
  967. ClickableL::deactivate();
  968. ClickableR::deactivate();
  969. Hoverable::deactivate();
  970. KeyInterested::deactivate();
  971. MotionInterested::deactivate();
  972. };
  973. void CList::clickLeft(tribool down)
  974. {
  975. };
  976. CList::CList(int Size)
  977. :SIZE(Size)
  978. {
  979. }
  980. CHeroList::CHeroList(int Size)
  981. :CList(Size), heroes(LOCPLINT->wanderingHeroes)
  982. {
  983. arrup = CDefHandler::giveDef(conf.go()->ac.hlistAU);
  984. arrdo = CDefHandler::giveDef(conf.go()->ac.hlistAD);
  985. mobile = CDefHandler::giveDef(conf.go()->ac.hlistMB);
  986. mana = CDefHandler::giveDef(conf.go()->ac.hlistMN);
  987. empty = BitmapHandler::loadBitmap("HPSXXX.bmp");
  988. selection = BitmapHandler::loadBitmap("HPSYYY.bmp");
  989. SDL_SetColorKey(selection,SDL_SRCCOLORKEY,SDL_MapRGB(selection->format,0,255,255));
  990. pos = genRect(32*SIZE+arrup->height+arrdo->height, std::max(arrup->width,arrdo->width), conf.go()->ac.hlistX, conf.go()->ac.hlistY);
  991. arrupp = genRect(arrup->height, arrup->width, pos.x, pos.y);
  992. arrdop = genRect(arrdo->height, arrdo->width, pos.x, pos.y+32*SIZE+arrup->height);
  993. //32px per hero
  994. posmobx = pos.x+1;
  995. posmoby = pos.y+arrup->height+1;
  996. posporx = pos.x+mobile->width+2;
  997. pospory = pos.y+arrup->height;
  998. posmanx = pos.x+1+50+mobile->width;
  999. posmany = pos.y+arrup->height+1;
  1000. from = 0;
  1001. selected = -1;
  1002. pressed = indeterminate;
  1003. }
  1004. void CHeroList::init()
  1005. {
  1006. int w = pos.w+1, h = pos.h+4;
  1007. bg = CSDL_Ext::newSurface(w,h,screen);
  1008. SDL_BlitSurface(LOCPLINT->adventureInt->bg,&genRect(w,h,pos.x,pos.y),bg,&genRect(w,h,0,0));
  1009. }
  1010. void CHeroList::genList()
  1011. {
  1012. //int howMany = LOCPLINT->cb->howManyHeroes();
  1013. //for (int i=0;i<howMany;i++)
  1014. //{
  1015. // const CGHeroInstance * h = LOCPLINT->cb->getHeroInfo(i,0);
  1016. // if(!h->inTownGarrison)
  1017. // items.push_back(std::pair<const CGHeroInstance *,CPath *>(h,NULL));
  1018. //}
  1019. }
  1020. void CHeroList::select(int which)
  1021. {
  1022. if (which<0)
  1023. {
  1024. selected = which;
  1025. LOCPLINT->adventureInt->selection = NULL;
  1026. LOCPLINT->adventureInt->terrain.currentPath = NULL;
  1027. draw(screen);
  1028. LOCPLINT->adventureInt->infoBar.draw(screen);
  1029. }
  1030. if (which>=heroes.size())
  1031. return;
  1032. selected = which;
  1033. LOCPLINT->adventureInt->select(heroes[which]);
  1034. }
  1035. void CHeroList::clickLeft(tribool down)
  1036. {
  1037. if (down)
  1038. {
  1039. /***************************ARROWS*****************************************/
  1040. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1041. {
  1042. if(from>0)
  1043. {
  1044. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  1045. pressed = true;
  1046. }
  1047. return;
  1048. }
  1049. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1050. {
  1051. if(heroes.size()-from>SIZE)
  1052. {
  1053. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  1054. pressed = false;
  1055. }
  1056. return;
  1057. }
  1058. /***************************HEROES*****************************************/
  1059. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  1060. hx-=pos.x;
  1061. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1062. int ny = hy/32;
  1063. if (ny>=SIZE || ny<0)
  1064. return;
  1065. if ( (ny+from)==selected && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  1066. LOCPLINT->openHeroWindow(heroes[selected]);//print hero screen
  1067. select(ny+from);
  1068. }
  1069. else
  1070. {
  1071. if (indeterminate(pressed))
  1072. return;
  1073. if (pressed) //up
  1074. {
  1075. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  1076. pressed = indeterminate;
  1077. if (!down)
  1078. {
  1079. from--;
  1080. if (from<0)
  1081. from=0;
  1082. draw(screen);
  1083. }
  1084. }
  1085. else if (!pressed) //down
  1086. {
  1087. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  1088. pressed = indeterminate;
  1089. if (!down)
  1090. {
  1091. from++;
  1092. //if (from<items.size()-5)
  1093. // from=items.size()-5;
  1094. draw(screen);
  1095. }
  1096. }
  1097. else
  1098. throw 0;
  1099. }
  1100. }
  1101. void CHeroList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  1102. {
  1103. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1104. {
  1105. if (from>0)
  1106. LOCPLINT->adventureInt->statusbar.print(CGI->generaltexth->zelp[303].first);
  1107. else
  1108. LOCPLINT->adventureInt->statusbar.clear();
  1109. return;
  1110. }
  1111. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1112. {
  1113. if ((heroes.size()-from) > SIZE)
  1114. LOCPLINT->adventureInt->statusbar.print(CGI->generaltexth->zelp[304].first);
  1115. else
  1116. LOCPLINT->adventureInt->statusbar.clear();
  1117. return;
  1118. }
  1119. //if not buttons then heroes
  1120. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  1121. hx-=pos.x;
  1122. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1123. int ny = hy/32;
  1124. if ((ny>SIZE || ny<0) || (from+ny>=heroes.size()))
  1125. {
  1126. LOCPLINT->adventureInt->statusbar.clear();
  1127. return;
  1128. }
  1129. std::vector<std::string> temp;
  1130. temp.push_back(heroes[from+ny]->name);
  1131. temp.push_back(heroes[from+ny]->type->heroClass->name);
  1132. LOCPLINT->adventureInt->statusbar.print( processStr(CGI->generaltexth->allTexts[15],temp) );
  1133. //select(ny+from);
  1134. }
  1135. void CHeroList::clickRight(tribool down)
  1136. {
  1137. if (down)
  1138. {
  1139. /***************************ARROWS*****************************************/
  1140. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  1141. {
  1142. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down,this);
  1143. }
  1144. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (heroes.size()-from>5))
  1145. {
  1146. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down,this);
  1147. }
  1148. else
  1149. {
  1150. //if not buttons then heroes
  1151. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  1152. hx-=pos.x;
  1153. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1154. int ny = hy/32;
  1155. if ((ny>SIZE || ny<0) || (from+ny>=heroes.size()))
  1156. {
  1157. return;
  1158. }
  1159. //show popup
  1160. CInfoPopup * ip = new CInfoPopup(graphics->heroWins[heroes[from+ny]->subID],
  1161. LOCPLINT->current->motion.x-graphics->heroWins[heroes[from+ny]->subID]->w,
  1162. LOCPLINT->current->motion.y-graphics->heroWins[heroes[from+ny]->subID]->h,
  1163. false);
  1164. LOCPLINT->pushInt(ip);
  1165. }
  1166. }
  1167. else
  1168. {
  1169. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down,this);
  1170. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down,this);
  1171. }
  1172. }
  1173. void CHeroList::hover (bool on)
  1174. {
  1175. }
  1176. void CHeroList::keyPressed (const SDL_KeyboardEvent & key)
  1177. {
  1178. }
  1179. void CHeroList::updateHList(const CGHeroInstance *toRemove)
  1180. {
  1181. if(toRemove) //remove specific hero
  1182. heroes -= toRemove;
  1183. else
  1184. LOCPLINT->recreateWanderingHeroes();
  1185. if(selected >= heroes.size())
  1186. select(heroes.size()-1);
  1187. if(heroes.size() == 0)
  1188. LOCPLINT->adventureInt->townList.select(0);
  1189. else
  1190. select(selected);
  1191. }
  1192. void CHeroList::updateMove(const CGHeroInstance* which) //draws move points bar
  1193. {
  1194. int ser = -1;
  1195. for(int i=0; i<heroes.size() && ser<0; i++)
  1196. if(heroes[i]->subID == which->subID)
  1197. ser = i;
  1198. ser -= from;
  1199. if(ser<0 || ser > SIZE) return;
  1200. int pom = std::min((which->movement)/100,(si32)mobile->ourImages.size()-1);
  1201. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+ser*32); //move point
  1202. }
  1203. void CHeroList::draw(SDL_Surface * to)
  1204. {
  1205. for (int iT=0+from;iT<SIZE+from;iT++)
  1206. {
  1207. int i = iT-from;
  1208. if (iT>=heroes.size())
  1209. {
  1210. blitAt(mobile->ourImages[0].bitmap,posmobx,posmoby+i*32,to);
  1211. blitAt(mana->ourImages[0].bitmap,posmanx,posmany+i*32,to);
  1212. blitAt(empty,posporx,pospory+i*32,to);
  1213. continue;
  1214. }
  1215. const CGHeroInstance *cur = heroes[iT];
  1216. int pom = cur->movement / 100;
  1217. if (pom>25) pom=25;
  1218. if (pom<0) pom=0;
  1219. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+i*32,to); //move point
  1220. pom = cur->mana / 5;
  1221. if (pom>25) pom=25;
  1222. if (pom<0) pom=0;
  1223. blitAt(mana->ourImages[pom].bitmap,posmanx,posmany+i*32,to); //mana
  1224. SDL_Surface * temp = graphics->portraitSmall[cur->portrait];
  1225. blitAt(temp,posporx,pospory+i*32,to);
  1226. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  1227. {
  1228. blitAt(selection,posporx,pospory+i*32,to);
  1229. }
  1230. //TODO: support for custom portraits
  1231. }
  1232. if (from>0)
  1233. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1234. else
  1235. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1236. if (heroes.size()-from > SIZE)
  1237. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1238. else
  1239. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1240. }
  1241. int CHeroList::getPosOfHero(const CGHeroInstance* h)
  1242. {
  1243. return vstd::findPos(heroes, h, std::equal_to<const CGHeroInstance*>());
  1244. }
  1245. CTownList::~CTownList()
  1246. {
  1247. delete arrup;
  1248. delete arrdo;
  1249. }
  1250. CTownList::CTownList(int Size, int x, int y, std::string arrupg, std::string arrdog)
  1251. :CList(Size)
  1252. {
  1253. arrup = CDefHandler::giveDef(arrupg);
  1254. arrdo = CDefHandler::giveDef(arrdog);
  1255. pos.x = x;
  1256. pos.y = y;
  1257. pos.w = std::max(arrdo->width, arrup->width);
  1258. pos.h = arrdo->height + arrup->height + Size*32;
  1259. arrupp.x=x;
  1260. arrupp.y=y;
  1261. arrupp.w=arrup->width;
  1262. arrupp.h=arrup->height;
  1263. arrdop.x=x;
  1264. arrdop.y=y+arrup->height+32*SIZE;
  1265. arrdop.w=arrdo->width;
  1266. arrdop.h=arrdo->height;
  1267. posporx = arrdop.x;
  1268. pospory = arrupp.y + arrupp.h;
  1269. pressed = indeterminate;
  1270. from = 0;
  1271. selected = -1;
  1272. }
  1273. void CTownList::genList()
  1274. {
  1275. int howMany = LOCPLINT->cb->howManyTowns();
  1276. for (int i=0;i<howMany;i++)
  1277. {
  1278. items.push_back(LOCPLINT->cb->getTownInfo(i,0));
  1279. }
  1280. }
  1281. void CTownList::select(int which)
  1282. {
  1283. if (which>=items.size())
  1284. return;
  1285. selected = which;
  1286. if(!fun.empty())
  1287. fun();
  1288. }
  1289. void CTownList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  1290. {
  1291. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1292. {
  1293. if (from>0)
  1294. LOCPLINT->statusbar->print(CGI->generaltexth->zelp[306].first);
  1295. else
  1296. LOCPLINT->statusbar->clear();
  1297. return;
  1298. }
  1299. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1300. {
  1301. if ((items.size()-from) > SIZE)
  1302. LOCPLINT->statusbar->print(CGI->generaltexth->zelp[307].first);
  1303. else
  1304. LOCPLINT->statusbar->clear();
  1305. return;
  1306. }
  1307. //if not buttons then towns
  1308. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  1309. hx-=pos.x;
  1310. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1311. int ny = hy/32;
  1312. if ((ny>SIZE || ny<0) || (from+ny>=items.size()))
  1313. {
  1314. LOCPLINT->statusbar->clear();
  1315. return;
  1316. };
  1317. LOCPLINT->statusbar->print(items[from+ny]->name);
  1318. }
  1319. void CTownList::clickLeft(tribool down)
  1320. {
  1321. if (down)
  1322. {
  1323. /***************************ARROWS*****************************************/
  1324. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1325. {
  1326. if(from>0)
  1327. {
  1328. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y,screenBuf);
  1329. pressed = true;
  1330. }
  1331. return;
  1332. }
  1333. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1334. {
  1335. if(items.size()-from > SIZE)
  1336. {
  1337. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y,screenBuf);
  1338. pressed = false;
  1339. }
  1340. return;
  1341. }
  1342. /***************************TOWNS*****************************************/
  1343. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  1344. hx-=pos.x;
  1345. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1346. int ny = hy/32;
  1347. if (ny>SIZE || ny<0)
  1348. return;
  1349. if(LOCPLINT->topInt() == LOCPLINT->adventureInt
  1350. && (ny+from)==selected
  1351. && LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE
  1352. )
  1353. LOCPLINT->openTownWindow(items[selected]);//print town screen
  1354. else
  1355. select(ny+from);
  1356. }
  1357. else
  1358. {
  1359. if (indeterminate(pressed))
  1360. return;
  1361. if (pressed) //up
  1362. {
  1363. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,screenBuf);
  1364. pressed = indeterminate;
  1365. if (!down)
  1366. {
  1367. from--;
  1368. if (from<0)
  1369. from=0;
  1370. draw(screenBuf);
  1371. }
  1372. }
  1373. else if (!pressed) //down
  1374. {
  1375. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,screenBuf);
  1376. pressed = indeterminate;
  1377. if (!down)
  1378. {
  1379. from++;
  1380. //if (from<items.size()-5)
  1381. // from=items.size()-5;
  1382. draw(screenBuf);
  1383. }
  1384. }
  1385. else
  1386. throw 0;
  1387. }
  1388. }
  1389. void CTownList::clickRight(tribool down)
  1390. {
  1391. if (down)
  1392. {
  1393. /***************************ARROWS*****************************************/
  1394. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  1395. {
  1396. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down,this);
  1397. }
  1398. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>5))
  1399. {
  1400. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down,this);
  1401. }
  1402. //if not buttons then towns
  1403. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  1404. hx-=pos.x;
  1405. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1406. int ny = hy/32;
  1407. if ((ny>5 || ny<0) || (from+ny>=items.size()))
  1408. {
  1409. return;
  1410. }
  1411. //show popup
  1412. CInfoPopup * ip = new CInfoPopup(
  1413. graphics->townWins[items[from+ny]->id],
  1414. LOCPLINT->current->motion.x-graphics->townWins[items[from+ny]->id]->w,
  1415. LOCPLINT->current->motion.y-graphics->townWins[items[from+ny]->id]->h,
  1416. false);
  1417. LOCPLINT->pushInt(ip);
  1418. }
  1419. else
  1420. {
  1421. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down,this);
  1422. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down,this);
  1423. }
  1424. }
  1425. void CTownList::hover (bool on)
  1426. {
  1427. }
  1428. void CTownList::keyPressed (const SDL_KeyboardEvent & key)
  1429. {
  1430. }
  1431. void CTownList::draw(SDL_Surface * to)
  1432. {
  1433. for (int iT=0+from;iT<SIZE+from;iT++)
  1434. {
  1435. int i = iT-from;
  1436. if (iT>=items.size())
  1437. {
  1438. blitAt(graphics->getPic(-1),posporx,pospory+i*32,to);
  1439. continue;
  1440. }
  1441. blitAt(graphics->getPic(items[iT]->subID,items[iT]->hasFort(),items[iT]->builded),posporx,pospory+i*32,to);
  1442. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE))
  1443. {
  1444. blitAt(graphics->getPic(-2),posporx,pospory+i*32,to);
  1445. }
  1446. }
  1447. if (from>0)
  1448. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1449. else
  1450. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1451. if (items.size()-from>SIZE)
  1452. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1453. else
  1454. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1455. }
  1456. CCreaturePic::CCreaturePic(CCreature *cre, bool Big)
  1457. :c(cre),big(Big)
  1458. {
  1459. anim = new CCreatureAnimation(cre->animDefName);
  1460. }
  1461. CCreaturePic::~CCreaturePic()
  1462. {
  1463. delete anim;
  1464. }
  1465. int CCreaturePic::blitPic(SDL_Surface *to, int x, int y, bool nextFrame)
  1466. {
  1467. SDL_Rect dst;
  1468. if(big)
  1469. {
  1470. blitAt(graphics->backgrounds[c->faction],x,y,to);//curx-50,pos.y+130-65);
  1471. dst = genRect(130,100,x,y);
  1472. }
  1473. else
  1474. {
  1475. blitAt(graphics->backgroundsm[c->faction],x,y,to);//curx-50,pos.y+130-65);
  1476. dst = genRect(120,100,x,y);
  1477. }
  1478. if(c->isDoubleWide())
  1479. x-=15;
  1480. return anim->nextFrameMiddle(to,x+78,y+(big ? 55 : 45),true,0,nextFrame,false,false,&dst);
  1481. }
  1482. SDL_Surface * CCreaturePic::getPic(bool nextFrame)
  1483. {
  1484. //TODO: write
  1485. return NULL;
  1486. }
  1487. void CRecruitmentWindow::close()
  1488. {
  1489. LOCPLINT->popIntTotally(this);
  1490. }
  1491. void CRecruitmentWindow::Max()
  1492. {
  1493. slider->moveTo(slider->amount);
  1494. }
  1495. void CRecruitmentWindow::Buy()
  1496. {
  1497. recruit(creatures[which].ID,slider->value);
  1498. close();
  1499. }
  1500. void CRecruitmentWindow::Cancel()
  1501. {
  1502. close();
  1503. }
  1504. void CRecruitmentWindow::sliderMoved(int to)
  1505. {
  1506. buy->block(!to);
  1507. }
  1508. void CRecruitmentWindow::clickLeft(tribool down)
  1509. {
  1510. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  1511. for(int i=0;i<creatures.size();i++)
  1512. {
  1513. if(isItIn(&genRect(132,102,pos.x+curx,pos.y+64),LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1514. {
  1515. which = i;
  1516. int newAmount = std::min(amounts[i],creatures[i].amount);
  1517. slider->amount = newAmount;
  1518. if(slider->value > newAmount)
  1519. slider->moveTo(newAmount);
  1520. else
  1521. slider->moveTo(slider->value);
  1522. curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  1523. for(int j=0;j<creatures.size();j++)
  1524. {
  1525. if(which==j)
  1526. drawBorder(bitmap,curx,64,102,132,int3(255,0,0));
  1527. else
  1528. drawBorder(bitmap,curx,64,102,132,int3(239,215,123));
  1529. curx += 120;
  1530. }
  1531. break;
  1532. }
  1533. curx += 120;
  1534. }
  1535. }
  1536. void CRecruitmentWindow::clickRight( boost::logic::tribool down )
  1537. {
  1538. if(down)
  1539. {
  1540. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  1541. for(int i=0;i<creatures.size();i++)
  1542. {
  1543. if(isItIn(&genRect(132,102,pos.x+curx,pos.y+64),LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1544. {
  1545. CCreInfoWindow *popup = new CCreInfoWindow(creatures[i].ID,0,0,NULL,NULL,NULL,NULL);
  1546. LOCPLINT->pushInt(popup);
  1547. break;
  1548. }
  1549. curx += 120;
  1550. }
  1551. }
  1552. }
  1553. void CRecruitmentWindow::activate()
  1554. {
  1555. ClickableL::activate();
  1556. ClickableR::activate();
  1557. buy->activate();
  1558. max->activate();
  1559. cancel->activate();
  1560. slider->activate();
  1561. LOCPLINT->statusbar = bar;
  1562. }
  1563. void CRecruitmentWindow::deactivate()
  1564. {
  1565. ClickableL::deactivate();
  1566. ClickableR::deactivate();
  1567. buy->deactivate();
  1568. max->deactivate();
  1569. cancel->deactivate();
  1570. slider->deactivate();
  1571. }
  1572. void CRecruitmentWindow::show(SDL_Surface * to)
  1573. {
  1574. static char animCounter=0; //animation counter - for determining appropriate animation frame to be shown
  1575. blitAt(bitmap,pos.x,pos.y,to);
  1576. buy->show(to);
  1577. max->show(to);
  1578. cancel->show(to);
  1579. slider->show(to);
  1580. char pom[15];
  1581. SDL_itoa(creatures[which].amount-slider->value,pom,10); //available
  1582. printAtMiddle(pom,pos.x+205,pos.y+252,GEOR13,zwykly,to);
  1583. SDL_itoa(slider->value,pom,10); //recruit
  1584. printAtMiddle(pom,pos.x+279,pos.y+252,GEOR13,zwykly,to);
  1585. printAtMiddle(CGI->generaltexth->allTexts[16] + " " + CGI->creh->creatures[creatures[which].ID].namePl,pos.x+243,pos.y+32,GEOR16,tytulowy,to); //eg "Recruit Dragon flies"
  1586. int curx = pos.x+115-creatures[which].res.size()*16;
  1587. for(int i=0;i<creatures[which].res.size();i++)
  1588. {
  1589. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx,pos.y+243,to);
  1590. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx+258,pos.y+243,to);
  1591. SDL_itoa(creatures[which].res[i].second,pom,10);
  1592. printAtMiddle(pom,curx+12,pos.y+286,GEOR13,zwykly,to);
  1593. SDL_itoa(creatures[which].res[i].second * slider->value,pom,10);
  1594. printAtMiddle(pom,curx+12+258,pos.y+286,GEOR13,zwykly,to);
  1595. curx+=32;
  1596. }
  1597. curx = pos.x + 192 + 102 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  1598. for(int i=0; i<creatures.size(); ++i)
  1599. {
  1600. creatures[i].pic->blitPic(to, curx-50, pos.y+130-65, !(animCounter%4));
  1601. curx += 120;
  1602. }
  1603. ++animCounter;
  1604. bar->show(to);
  1605. }
  1606. CRecruitmentWindow::CRecruitmentWindow(const std::vector<std::pair<int,int> > &Creatures, const boost::function<void(int,int)> &Recruit) //creatures - pairs<creature_ID,amount>
  1607. :recruit(Recruit), which(0)
  1608. {
  1609. creatures.resize(Creatures.size());
  1610. amounts.resize(Creatures.size());
  1611. for(int i=0;i<creatures.size();i++)
  1612. {
  1613. creatures[i].amount = Creatures[i].second;
  1614. creatures[i].ID = Creatures[i].first;
  1615. for(int j=0;j<CGI->creh->creatures[Creatures[i].first].cost.size();j++)
  1616. if(CGI->creh->creatures[Creatures[i].first].cost[j])
  1617. creatures[i].res.push_back(std::make_pair(j,CGI->creh->creatures[Creatures[i].first].cost[j]));
  1618. creatures[i].pic = new CCreaturePic(&CGI->creh->creatures[Creatures[i].first]);
  1619. amounts[i] = CGI->creh->creatures[Creatures[i].first].maxAmount(LOCPLINT->cb->getResourceAmount());
  1620. }
  1621. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPRCRT.bmp");
  1622. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1623. bitmap = SDL_ConvertSurface(hhlp,screen->format,0); //na 8bitowej mapie by sie psulo //it wouldn't work on 8bpp map
  1624. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1625. SDL_FreeSurface(hhlp);
  1626. pos.x = screen->w/2 - bitmap->w/2;
  1627. pos.y = screen->h/2 - bitmap->h/2;
  1628. pos.w = bitmap->w;
  1629. pos.h = bitmap->h;
  1630. bar = new CStatusBar(pos.x+8, pos.y+370, "APHLFTRT.bmp", 471);
  1631. max = new AdventureMapButton(CGI->generaltexth->zelp[553],boost::bind(&CRecruitmentWindow::Max,this),pos.x+134,pos.y+313,"IRCBTNS.DEF",SDLK_m);
  1632. buy = new AdventureMapButton(CGI->generaltexth->zelp[554],boost::bind(&CRecruitmentWindow::Buy,this),pos.x+212,pos.y+313,"IBY6432.DEF",SDLK_RETURN);
  1633. cancel = new AdventureMapButton(CGI->generaltexth->zelp[555],boost::bind(&CRecruitmentWindow::Cancel,this),pos.x+290,pos.y+313,"ICN6432.DEF",SDLK_ESCAPE);
  1634. slider = new CSlider(pos.x+176,pos.y+279,135,boost::bind(&CRecruitmentWindow::sliderMoved,this, _1),1,std::min(amounts[0],creatures[0].amount),0,true);
  1635. std::string pom;
  1636. printAtMiddle(CGI->generaltexth->allTexts[346],113,231,GEOR13,zwykly,bitmap); //cost per troop t
  1637. printAtMiddle(CGI->generaltexth->allTexts[465],205,231,GEOR13,zwykly,bitmap); //available t
  1638. printAtMiddle(CGI->generaltexth->allTexts[16],279,231,GEOR13,zwykly,bitmap); //recruit t
  1639. printAtMiddle(CGI->generaltexth->allTexts[466],373,231,GEOR13,zwykly,bitmap); //total cost t
  1640. drawBorder(bitmap,172,222,67,42,int3(239,215,123));
  1641. drawBorder(bitmap,246,222,67,42,int3(239,215,123));
  1642. drawBorder(bitmap,64,222,99,76,int3(239,215,123));
  1643. drawBorder(bitmap,322,222,99,76,int3(239,215,123));
  1644. drawBorder(bitmap,133,312,66,34,int3(173,142,66));
  1645. drawBorder(bitmap,211,312,66,34,int3(173,142,66));
  1646. drawBorder(bitmap,289,312,66,34,int3(173,142,66));
  1647. //border for creatures
  1648. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  1649. for(int i=0;i<creatures.size();i++)
  1650. {
  1651. creatures[i].pos.x = curx+1;
  1652. creatures[i].pos.y = 65;
  1653. creatures[i].pos.w = 100;
  1654. creatures[i].pos.h = 130;
  1655. if(which==i)
  1656. drawBorder(bitmap,curx,64,102,132,int3(255,0,0));
  1657. else
  1658. drawBorder(bitmap,curx,64,102,132,int3(239,215,123));
  1659. curx += 120;
  1660. }
  1661. if(!creatures[0].amount || !amounts[0])
  1662. {
  1663. max->block(true);
  1664. slider->block(true);
  1665. }
  1666. //buy->block(true); //not needed, will be blocked by initing slider on 0
  1667. }
  1668. CRecruitmentWindow::~CRecruitmentWindow()
  1669. {
  1670. for(int i=0;i<creatures.size();i++)
  1671. {
  1672. delete creatures[i].pic;
  1673. }
  1674. delete max;
  1675. delete buy;
  1676. delete cancel;
  1677. SDL_FreeSurface(bitmap);
  1678. delete slider;
  1679. delete bar;
  1680. }
  1681. CSplitWindow::CSplitWindow(int cid, int max, CGarrisonInt *Owner, int Last, int val)
  1682. {
  1683. last = Last;
  1684. which = 1;
  1685. c=cid;
  1686. slider = NULL;
  1687. gar = Owner;
  1688. bitmap = BitmapHandler::loadBitmap("GPUCRDIV.bmp");
  1689. graphics->blueToPlayersAdv(bitmap,LOCPLINT->playerID);
  1690. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1691. pos.x = screen->w/2 - bitmap->w/2;
  1692. pos.y = screen->h/2 - bitmap->h/2;
  1693. pos.w = bitmap->w;
  1694. pos.h = bitmap->h;
  1695. ok = new AdventureMapButton("","",boost::bind(&CSplitWindow::split,this),pos.x+20,pos.y+263,"IOK6432.DEF",SDLK_RETURN);
  1696. cancel = new AdventureMapButton("","",boost::bind(&CSplitWindow::close,this),pos.x+214,pos.y+263,"ICN6432.DEF",SDLK_ESCAPE);
  1697. int sliderPositions = max - (last>=0) - (last==2);
  1698. slider = new CSlider(pos.x+21,pos.y+194,257,boost::bind(&CSplitWindow::sliderMoved,this,_1),1,sliderPositions,val,true);
  1699. a1 = max-val;
  1700. a2 = val;
  1701. anim = new CCreaturePic(&CGI->creh->creatures[cid]);
  1702. anim->anim->setType(1);
  1703. std::string title = CGI->generaltexth->allTexts[256];
  1704. boost::algorithm::replace_first(title,"%s",CGI->creh->creatures[cid].namePl);
  1705. printAtMiddle(title,150,34,GEOR16,tytulowy,bitmap);
  1706. }
  1707. CSplitWindow::~CSplitWindow() //d-tor
  1708. {
  1709. SDL_FreeSurface(bitmap);
  1710. delete ok;
  1711. delete cancel;
  1712. delete slider;
  1713. delete anim;
  1714. }
  1715. void CSplitWindow::activate()
  1716. {
  1717. ClickableL::activate();
  1718. KeyInterested::activate();
  1719. ok->activate();
  1720. cancel->activate();
  1721. slider->activate();
  1722. }
  1723. void CSplitWindow::deactivate()
  1724. {
  1725. ClickableL::deactivate();
  1726. KeyInterested::deactivate();
  1727. ok->deactivate();
  1728. cancel->deactivate();
  1729. slider->deactivate();
  1730. }
  1731. void CSplitWindow::split()
  1732. {
  1733. gar->splitStacks(a2);
  1734. close();
  1735. }
  1736. void CSplitWindow::close()
  1737. {
  1738. LOCPLINT->popIntTotally(this);
  1739. }
  1740. void CSplitWindow::sliderMoved(int to)
  1741. {
  1742. int all = a1+a2;
  1743. a2 = to + (last==1 || last==2);
  1744. if(slider)
  1745. a1 = all - a2;
  1746. }
  1747. void CSplitWindow::show(SDL_Surface * to)
  1748. {
  1749. blitAt(bitmap,pos.x,pos.y,to);
  1750. ok->show(to);
  1751. cancel->show(to);
  1752. slider->show(to);
  1753. printAtMiddle(boost::lexical_cast<std::string>(a1) + (!which ? "_" : ""),pos.x+70,pos.y+237,GEOR16,zwykly,to);
  1754. printAtMiddle(boost::lexical_cast<std::string>(a2) + (which ? "_" : ""),pos.x+233,pos.y+237,GEOR16,zwykly,to);
  1755. anim->blitPic(to,pos.x+20,pos.y+54,false);
  1756. anim->blitPic(to,pos.x+177,pos.y+54,false);
  1757. }
  1758. void CSplitWindow::keyPressed (const SDL_KeyboardEvent & key)
  1759. {
  1760. if(key.state != SDL_PRESSED)
  1761. return;
  1762. int &cur = (which ? a2 : a1),
  1763. &sec = (which ? a1 : a2),
  1764. ncur = cur;
  1765. if (key.keysym.sym == SDLK_BACKSPACE)
  1766. {
  1767. ncur /= 10;
  1768. }
  1769. else if(key.keysym.sym == SDLK_TAB)
  1770. {
  1771. which = !which;
  1772. }
  1773. else
  1774. {
  1775. int number = key.keysym.sym - SDLK_0;
  1776. if (number < 0 || number > 9) //not a number pressed
  1777. {
  1778. return;
  1779. }
  1780. ncur = cur*10 + number;
  1781. }
  1782. int delta = ncur - cur;
  1783. if(delta > sec)
  1784. {
  1785. cur += sec;
  1786. sec = 0;
  1787. }
  1788. slider->moveTo(which ? ncur : a1+a2-ncur);
  1789. }
  1790. void CSplitWindow::clickLeft( boost::logic::tribool down )
  1791. {
  1792. if(down)
  1793. {
  1794. Point click(LOCPLINT->current->motion.x,LOCPLINT->current->motion.y);
  1795. click -= pos.topLeft();
  1796. if(Rect(19,216,105,40).isIn(click)) //left picture
  1797. which = 0;
  1798. else if(Rect(175,216,105,40).isIn(click)) //right picture
  1799. which = 1;
  1800. }
  1801. }
  1802. void CCreInfoWindow::show(SDL_Surface * to)
  1803. {
  1804. char pom[15];
  1805. blitAt(bitmap,pos.x,pos.y,to);
  1806. anim->blitPic(to,pos.x+21,pos.y+48,(type) && !(anf%4));
  1807. if(++anf==4)
  1808. anf=0;
  1809. if(count.size())
  1810. printTo(count.c_str(),pos.x+114,pos.y+174,GEOR16,zwykly,to);
  1811. if(upgrade)
  1812. upgrade->show(to);
  1813. if(dismiss)
  1814. dismiss->show(to);
  1815. if(ok)
  1816. ok->show(to);
  1817. }
  1818. CCreInfoWindow::CCreInfoWindow(int Cid, int Type, int creatureCount, StackState *State, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  1819. :type(Type),dsm(Dsm),dismiss(0),upgrade(0),ok(0)
  1820. {
  1821. //active = false;
  1822. anf = 0;
  1823. c = &CGI->creh->creatures[Cid];
  1824. bitmap = BitmapHandler::loadBitmap("CRSTKPU.bmp");
  1825. pos.x = screen->w/2 - bitmap->w/2;
  1826. pos.y = screen->h/2 - bitmap->h/2;
  1827. pos.w = bitmap->w;
  1828. pos.h = bitmap->h;
  1829. graphics->blueToPlayersAdv(bitmap,LOCPLINT->playerID);
  1830. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1831. anim = new CCreaturePic(c);
  1832. if(!type) anim->anim->setType(2);
  1833. char pom[75];int hlp=0;
  1834. if(creatureCount)
  1835. {
  1836. SDL_itoa(creatureCount,pom,10);
  1837. count = pom;
  1838. }
  1839. printAtMiddle(c->namePl,149,30,GEOR13,tytulowy,bitmap); //creature name
  1840. //atttack
  1841. printAt(CGI->generaltexth->primarySkillNames[0],155,48,GEOR13,zwykly,bitmap);
  1842. SDL_itoa(c->attack,pom,10);
  1843. if(State && State->attackBonus)
  1844. {
  1845. int hlp;
  1846. if(c->attack > 0)
  1847. hlp = log10f(c->attack)+2;
  1848. else
  1849. hlp = 2;
  1850. pom[hlp-1] = ' '; pom[hlp] = '(';
  1851. SDL_itoa(c->attack+State->attackBonus,pom+hlp+1,10);
  1852. hlp += 2+(int)log10f(State->attackBonus+c->attack);
  1853. pom[hlp] = ')'; pom[hlp+1] = '\0';
  1854. }
  1855. printToWR(pom,276,61,GEOR13,zwykly,bitmap);
  1856. //defense
  1857. printAt(CGI->generaltexth->primarySkillNames[1],155,67,GEOR13,zwykly,bitmap);
  1858. SDL_itoa(c->defence,pom,10);
  1859. if(State && State->defenseBonus)
  1860. {
  1861. int hlp;
  1862. if(c->defence > 0)
  1863. hlp = log10f(c->defence)+2;
  1864. else
  1865. hlp = 2;
  1866. pom[hlp-1] = ' '; pom[hlp] = '(';
  1867. SDL_itoa(c->defence+State->defenseBonus,pom+hlp+1,10);
  1868. hlp += 2+(int)log10f(State->defenseBonus+c->defence);
  1869. pom[hlp] = ')'; pom[hlp+1] = '\0';
  1870. }
  1871. printToWR(pom,276,80,GEOR13,zwykly,bitmap);
  1872. //shots
  1873. if(c->shots)
  1874. {
  1875. printAt(CGI->generaltexth->allTexts[198],155,86,GEOR13,zwykly,bitmap);
  1876. if(State)
  1877. sprintf(pom,"%d(%d)",c->shots,State->shotsLeft);
  1878. else
  1879. SDL_itoa(c->shots,pom,10);
  1880. printToWR(pom,276,99,GEOR13,zwykly,bitmap);
  1881. }
  1882. //damage
  1883. printAt(CGI->generaltexth->allTexts[199],155,105,GEOR13,zwykly,bitmap);
  1884. SDL_itoa(c->damageMin,pom,10);
  1885. if(c->damageMin > 0)
  1886. hlp = log10f(c->damageMin)+2;
  1887. else
  1888. hlp = 2;
  1889. pom[hlp-1]=' '; pom[hlp]='-'; pom[hlp+1]=' ';
  1890. SDL_itoa(c->damageMax,pom+hlp+2,10);
  1891. printToWR(pom,276,118,GEOR13,zwykly,bitmap);
  1892. //health
  1893. printAt(CGI->generaltexth->allTexts[388],155,124,GEOR13,zwykly,bitmap);
  1894. SDL_itoa(c->hitPoints,pom,10);
  1895. printToWR(pom,276,137,GEOR13,zwykly,bitmap);
  1896. //remaining health
  1897. if(State && State->currentHealth)
  1898. {
  1899. printAt(CGI->generaltexth->allTexts[200],155,143,GEOR13,zwykly,bitmap);
  1900. SDL_itoa(State->currentHealth,pom,10);
  1901. printToWR(pom,276,156,GEOR13,zwykly,bitmap);
  1902. }
  1903. //speed
  1904. printAt(CGI->generaltexth->zelp[441].first,155,162,GEOR13,zwykly,bitmap);
  1905. SDL_itoa(c->speed,pom,10);
  1906. if(State && State->speedBonus)
  1907. {
  1908. int hlp;
  1909. if(c->speed > 0)
  1910. hlp = log10f(c->speed)+2;
  1911. else
  1912. hlp = 2;
  1913. pom[hlp-1] = ' '; pom[hlp] = '(';
  1914. SDL_itoa(c->speed + State->speedBonus, pom+hlp+1, 10);
  1915. hlp += 2+(int)log10f(c->speed + State->speedBonus);
  1916. pom[hlp] = ')'; pom[hlp+1] = '\0';
  1917. }
  1918. printToWR(pom,276,175,GEOR13,zwykly,bitmap);
  1919. //luck and morale
  1920. blitAt(graphics->morale42->ourImages[(State)?(State->morale+3):(3)].bitmap,24,189,bitmap);
  1921. blitAt(graphics->luck42->ourImages[(State)?(State->luck+3):(3)].bitmap,77,189,bitmap);
  1922. //print abilities text - if r-click popup
  1923. if(type)
  1924. {
  1925. if(Upg && ui)
  1926. {
  1927. bool enough = true;
  1928. for(std::set<std::pair<int,int> >::iterator i=ui->cost[0].begin(); i!=ui->cost[0].end(); i++) //calculate upgrade cost
  1929. {
  1930. if(LOCPLINT->cb->getResourceAmount(i->first) < i->second*creatureCount)
  1931. enough = false;
  1932. upgResCost.push_back(new SComponent(SComponent::resource,i->first,i->second*creatureCount));
  1933. }
  1934. if(enough)
  1935. {
  1936. CFunctionList<void()> fs[2];
  1937. fs[0] += Upg;
  1938. fs[0] += boost::bind(&CCreInfoWindow::close,this);
  1939. CFunctionList<void()> cfl;
  1940. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[207],boost::ref(upgResCost),fs[0],fs[1],false);
  1941. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,cfl,pos.x+76,pos.y+237,"IVIEWCR.DEF",SDLK_u);
  1942. }
  1943. else
  1944. {
  1945. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,boost::function<void()>(),pos.x+76,pos.y+237,"IVIEWCR.DEF");
  1946. upgrade->callback.funcs.clear();
  1947. upgrade->bitmapOffset = 2;
  1948. }
  1949. }
  1950. if(Dsm)
  1951. {
  1952. CFunctionList<void()> fs[2];
  1953. //on dismiss confirmed
  1954. fs[0] += Dsm; //dismiss
  1955. fs[0] += boost::bind(&CCreInfoWindow::close,this);//close this window
  1956. CFunctionList<void()> cfl;
  1957. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],std::vector<SComponent*>(),fs[0],fs[1],false);
  1958. dismiss = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,cfl,pos.x+21,pos.y+237,"IVIEWCR2.DEF",SDLK_d);
  1959. }
  1960. ok = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,boost::bind(&CCreInfoWindow::close,this),pos.x+216,pos.y+237,"IOKAY.DEF",SDLK_RETURN);
  1961. }
  1962. else
  1963. {
  1964. printAtWB(c->abilityText,17,231,GEOR13,35,zwykly,bitmap);
  1965. }
  1966. //spell effects
  1967. if(State)
  1968. {
  1969. int printed=0; //how many effect pics have been printed
  1970. for(std::set<int>::const_iterator it = State->effects.begin(); it!=State->effects.end(); ++it)
  1971. {
  1972. blitAt(graphics->spellEffectsPics->ourImages[*it + 1].bitmap, 127 + 52 * printed, 186, bitmap);
  1973. ++printed;
  1974. if(printed >= 3)
  1975. {
  1976. break;
  1977. }
  1978. }
  1979. }
  1980. }
  1981. CCreInfoWindow::~CCreInfoWindow()
  1982. {
  1983. SDL_FreeSurface(bitmap);
  1984. delete anim;
  1985. delete upgrade;
  1986. delete ok;
  1987. delete dismiss;
  1988. for(int i=0; i<upgResCost.size();i++)
  1989. delete upgResCost[i];
  1990. }
  1991. void CCreInfoWindow::activate()
  1992. {
  1993. //if(active) return;
  1994. //active = true;
  1995. if(!type)
  1996. ClickableR::activate();
  1997. if(ok)
  1998. ok->activate();
  1999. if(dismiss)
  2000. dismiss->activate();
  2001. if(upgrade)
  2002. upgrade->activate();
  2003. }
  2004. void CCreInfoWindow::close()
  2005. {
  2006. LOCPLINT->popIntTotally(this);
  2007. }
  2008. void CCreInfoWindow::clickRight(boost::logic::tribool down)
  2009. {
  2010. if(down)
  2011. return;
  2012. close();
  2013. }
  2014. void CCreInfoWindow::dismissF()
  2015. {
  2016. dsm();
  2017. close();
  2018. }
  2019. void CCreInfoWindow::keyPressed (const SDL_KeyboardEvent & key)
  2020. {
  2021. }
  2022. void CCreInfoWindow::deactivate()
  2023. {
  2024. //if(!active) return;
  2025. //active = false;
  2026. if(!type)
  2027. ClickableR::deactivate();
  2028. if(ok)
  2029. ok->deactivate();
  2030. if(dismiss)
  2031. dismiss->deactivate();
  2032. if(upgrade)
  2033. upgrade->deactivate();
  2034. }
  2035. void CLevelWindow::close()
  2036. {
  2037. for(int i=0;i<comps.size();i++)
  2038. {
  2039. if(comps[i]->selected)
  2040. {
  2041. cb(i);
  2042. break;
  2043. }
  2044. }
  2045. LOCPLINT->popIntTotally(this);
  2046. LOCPLINT->showingDialog->setn(false);
  2047. }
  2048. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  2049. {
  2050. LOCPLINT->showingDialog->setn(true);
  2051. heroType = hero->subID;
  2052. cb = callback;
  2053. for(int i=0;i<skills.size();i++)
  2054. {
  2055. comps.push_back(new CSelectableComponent(SComponent::secskill44,skills[i],hero->getSecSkillLevel(skills[i])+1,boost::bind(&CLevelWindow::selectionChanged,this,i)));
  2056. comps.back()->assignedKeys.insert(SDLK_1 + i);
  2057. }
  2058. bitmap = BitmapHandler::loadBitmap("LVLUPBKG.bmp");
  2059. graphics->blueToPlayersAdv(bitmap,hero->tempOwner);
  2060. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  2061. pos.x = screen->w/2 - bitmap->w/2;
  2062. pos.y = screen->h/2 - bitmap->h/2;
  2063. pos.w = bitmap->w;
  2064. pos.h = bitmap->h;
  2065. ok = new AdventureMapButton("","",boost::bind(&CLevelWindow::close,this),pos.x+297,pos.y+413,"IOKAY.DEF",SDLK_RETURN);
  2066. //draw window
  2067. char buf[100], buf2[100];
  2068. strcpy(buf2,CGI->generaltexth->allTexts[444].c_str()); //%s has gained a level.
  2069. sprintf(buf,buf2,hero->name.c_str());
  2070. printAtMiddle(buf,192,35,GEOR16,zwykly,bitmap);
  2071. strcpy(buf2,CGI->generaltexth->allTexts[445].c_str()); //%s is now a level %d %s.
  2072. sprintf(buf,buf2,hero->name.c_str(),hero->level,hero->type->heroClass->name.c_str());
  2073. printAtMiddle(buf,192,162,GEOR16,zwykly,bitmap);
  2074. blitAt(graphics->pskillsm->ourImages[pskill].bitmap,174,190,bitmap);
  2075. printAtMiddle((CGI->generaltexth->primarySkillNames[pskill] + " +1"),192,252,GEOR16,zwykly,bitmap);
  2076. SDL_Surface * ort = TTF_RenderText_Blended(GEOR16,CGI->generaltexth->allTexts[4].c_str(),zwykly);
  2077. int curx = bitmap->w/2 - ( skills.size()*44 + (skills.size()-1)*(36+ort->w) )/2;
  2078. for(int i=0;i<comps.size();i++)
  2079. {
  2080. comps[i]->pos.x = curx+pos.x;
  2081. comps[i]->pos.y = 326+pos.y;
  2082. if( i < (comps.size()-1) )
  2083. {
  2084. curx += 44 + 18; //skill width + margin to "or"
  2085. blitAt(ort,curx,346,bitmap);
  2086. curx += ort->w + 18;
  2087. }
  2088. }
  2089. if(comps.size() > 1)
  2090. {
  2091. ok->block(true);
  2092. }
  2093. else if(comps.size() == 1)
  2094. {
  2095. comps[0]->select(true);
  2096. }
  2097. SDL_FreeSurface(ort);
  2098. }
  2099. void CLevelWindow::selectionChanged(unsigned to)
  2100. {
  2101. if(ok->blocked)
  2102. ok->block(false);
  2103. for(int i=0;i<comps.size();i++)
  2104. if(i==to)
  2105. comps[i]->select(true);
  2106. else
  2107. comps[i]->select(false);
  2108. }
  2109. CLevelWindow::~CLevelWindow()
  2110. {
  2111. delete ok;
  2112. for(int i=0;i<comps.size();i++)
  2113. delete comps[i];
  2114. SDL_FreeSurface(bitmap);
  2115. }
  2116. void CLevelWindow::activate()
  2117. {
  2118. ok->activate();
  2119. for(int i=0;i<comps.size();i++)
  2120. comps[i]->activate();
  2121. }
  2122. void CLevelWindow::deactivate()
  2123. {
  2124. ok->deactivate();
  2125. for(int i=0;i<comps.size();i++)
  2126. comps[i]->deactivate();
  2127. }
  2128. void CLevelWindow::show(SDL_Surface * to)
  2129. {
  2130. blitAt(bitmap,pos.x,pos.y,to);
  2131. blitAt(graphics->portraitLarge[heroType],170+pos.x,66+pos.y,to);
  2132. ok->show(to);
  2133. for(int i=0;i<comps.size();i++)
  2134. comps[i]->show(to);
  2135. }
  2136. void CMinorResDataBar::show(SDL_Surface * to)
  2137. {
  2138. blitAt(bg,pos.x,pos.y,to);
  2139. char buf[30];
  2140. for (int i=0;i<7;i++)
  2141. {
  2142. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  2143. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,GEOR13,zwykly,to);
  2144. }
  2145. std::vector<std::string> temp;
  2146. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  2147. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  2148. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  2149. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  2150. CGI->generaltexth->allTexts[62]
  2151. +": %s, "
  2152. + CGI->generaltexth->allTexts[63]
  2153. + ": %s, "
  2154. + CGI->generaltexth->allTexts[64]
  2155. + ": %s",temp)
  2156. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,GEOR13,zwykly,to);
  2157. }
  2158. CMinorResDataBar::CMinorResDataBar()
  2159. {
  2160. bg = BitmapHandler::loadBitmap("Z2ESBAR.bmp");
  2161. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2162. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  2163. pos.x = 7;
  2164. pos.y = 575;
  2165. pos.w = bg->w;
  2166. pos.h = bg->h;
  2167. }
  2168. CMinorResDataBar::~CMinorResDataBar()
  2169. {
  2170. SDL_FreeSurface(bg);
  2171. }
  2172. SDL_Surface * CCustomImgComponent::getImg()
  2173. {
  2174. return bmp;
  2175. }
  2176. CCustomImgComponent::CCustomImgComponent( Etype Type, int Subtype, int Val, SDL_Surface *sur, bool freeSur )
  2177. :bmp(sur), free(freeSur)
  2178. {
  2179. init(Type,Subtype,Val);
  2180. }
  2181. CCustomImgComponent::~CCustomImgComponent()
  2182. {
  2183. if(free)
  2184. SDL_FreeSurface(bmp);
  2185. }
  2186. CMarketplaceWindow::CTradeableItem::CTradeableItem( int Type, int ID, bool Left)
  2187. {
  2188. left = Left;
  2189. type = Type;
  2190. id = ID;
  2191. }
  2192. void CMarketplaceWindow::CTradeableItem::show(SDL_Surface * to)
  2193. {
  2194. SDL_Surface *hlp = getSurface();
  2195. blitAt(hlp,pos.x+19,pos.y+9,to);
  2196. }
  2197. void CMarketplaceWindow::CTradeableItem::clickLeft( boost::logic::tribool down )
  2198. {
  2199. CMarketplaceWindow *mw = dynamic_cast<CMarketplaceWindow *>(LOCPLINT->topInt());
  2200. assert(mw);
  2201. if(down)
  2202. {
  2203. if(left)
  2204. {
  2205. if(mw->hLeft != this)
  2206. mw->hLeft = this;
  2207. else
  2208. return;
  2209. }
  2210. else
  2211. {
  2212. if(mw->hRight != this)
  2213. mw->hRight = this;
  2214. else
  2215. return;
  2216. }
  2217. mw->selectionChanged(left);
  2218. }
  2219. }
  2220. void CMarketplaceWindow::CTradeableItem::activate()
  2221. {
  2222. ClickableL::activate();
  2223. }
  2224. void CMarketplaceWindow::CTradeableItem::deactivate()
  2225. {
  2226. ClickableL::deactivate();
  2227. }
  2228. SDL_Surface * CMarketplaceWindow::CTradeableItem::getSurface()
  2229. {
  2230. switch(type)
  2231. {
  2232. case 0:
  2233. return graphics->resources32->ourImages[id].bitmap;
  2234. case 1:
  2235. return graphics->artDefs->ourImages[id].bitmap;
  2236. default:
  2237. return NULL;
  2238. }
  2239. }
  2240. static void initItems( std::vector<CMarketplaceWindow::CTradeableItem*> &i, std::vector<SDL_Rect> &p, int type, int amount, bool left, std::vector<int> *ids/*=NULL*/ )
  2241. {
  2242. i.resize(amount);
  2243. for(int j=0;j<amount;j++)
  2244. {
  2245. i[j] = new CMarketplaceWindow::CTradeableItem(type,(ids && ids->size()>j) ? (*ids)[j] : j, left);
  2246. i[j]->pos = p[j];
  2247. }
  2248. }
  2249. void CMarketplaceWindow::setMode( int mode )
  2250. {
  2251. std::vector<SDL_Rect> lpos, rpos;
  2252. clear();
  2253. switch(mode)
  2254. {
  2255. case 0:
  2256. {
  2257. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPMRKRES.bmp");
  2258. SDL_SetColorKey(bg2,SDL_SRCCOLORKEY,SDL_MapRGB(bg2->format,0,255,255));
  2259. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  2260. bg = SDL_ConvertSurface(bg2,screen->format,0);
  2261. SDL_FreeSurface(bg2);
  2262. lpos += genRect(66,69,39,180), genRect(66,69,122,180), genRect(66,69,204,180),
  2263. genRect(66,69,39,259), genRect(66,69,122,259), genRect(66,69,204,259),
  2264. genRect(66,69,122,338);
  2265. for(int i=0;i<lpos.size();i++)
  2266. {
  2267. lpos[i].x += pos.x;
  2268. lpos[i].y += pos.y;
  2269. rpos.push_back(lpos[i]);
  2270. rpos[rpos.size()-1].x += 288;
  2271. }
  2272. initItems(left,lpos,0,7,true,NULL);
  2273. initItems(right,rpos,0,7,false,NULL);
  2274. printAtMiddle(CGI->generaltexth->allTexts[158],303,28,GEORXX,tytulowy,bg); //title
  2275. printAtMiddle(CGI->generaltexth->allTexts[270],158,148,GEOR13,zwykly,bg); //kingdom res.
  2276. printAtMiddle(CGI->generaltexth->allTexts[168],450,148,GEOR13,zwykly,bg); //available for trade
  2277. }
  2278. }
  2279. }
  2280. void CMarketplaceWindow::clear()
  2281. {
  2282. for(int i=0;i<left.size();i++)
  2283. delete left[i];
  2284. for(int i=0;i<right.size();i++)
  2285. delete right[i];
  2286. left.clear();
  2287. right.clear();
  2288. SDL_FreeSurface(bg);
  2289. }
  2290. CMarketplaceWindow::CMarketplaceWindow(int Mode)
  2291. {
  2292. mode = Mode;
  2293. bg = NULL;
  2294. ok = max = deal = NULL;
  2295. pos.x = screen->w/2 - 300;
  2296. pos.y = screen->h/2 - 296;
  2297. slider = new CSlider(pos.x+231,pos.y+490,137,boost::bind(&CMarketplaceWindow::sliderMoved,this,_1),0,0);
  2298. setMode(mode);
  2299. hLeft = hRight = NULL;
  2300. ok = new AdventureMapButton("","",boost::bind(&CPlayerInterface::popIntTotally,LOCPLINT,this),pos.x+516,pos.y+520,"IOK6432.DEF",SDLK_RETURN);
  2301. deal = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::makeDeal,this),pos.x+307,pos.y+520,"TPMRKB.DEF");
  2302. max = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::setMax,this),pos.x+229,pos.y+520,"IRCBTNS.DEF");
  2303. max->block(true);
  2304. deal->block(true);
  2305. }
  2306. CMarketplaceWindow::~CMarketplaceWindow()
  2307. {
  2308. clear();
  2309. delete slider;
  2310. }
  2311. void CMarketplaceWindow::show(SDL_Surface * to)
  2312. {
  2313. blitAt(bg,pos,to);
  2314. if(hRight)
  2315. CSDL_Ext::drawBorder(to,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  2316. if(hLeft)
  2317. CSDL_Ext::drawBorder(to,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  2318. ok->show(to);
  2319. deal->show(to);
  2320. max->show(to);
  2321. slider->show(to);
  2322. for(int i=0;i<left.size();i++)
  2323. left[i]->show(to);
  2324. for(int i=0;i<right.size();i++)
  2325. right[i]->show(to);
  2326. if(mode==0)
  2327. {
  2328. char buf[15];
  2329. for(int i=0;i<left.size();i++)
  2330. {
  2331. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  2332. printAtMiddle(buf,left[i]->pos.x+35,left[i]->pos.y+56,GEOR13,zwykly,to);
  2333. }
  2334. if(hLeft) //print prices
  2335. {
  2336. for(int i=0; i<right.size();i++)
  2337. {
  2338. if(right[i]->id != hLeft->id)
  2339. printAtMiddle(rSubs[i],right[i]->pos.x+35,right[i]->pos.y+56,GEOR13,zwykly,to);
  2340. else
  2341. printAtMiddle(CGI->generaltexth->allTexts[164],right[i]->pos.x+35,right[i]->pos.y+56,GEOR13,zwykly,to);
  2342. }
  2343. }
  2344. if(hLeft && hRight && hLeft->id!= hRight->id)
  2345. {
  2346. blitAt(hLeft->getSurface(),pos.x+142,pos.y+457,to);
  2347. blitAt(hRight->getSurface(),pos.x+430,pos.y+457,to);
  2348. SDL_itoa(slider->value * r1,buf,10);
  2349. printAtMiddle(buf,pos.x+158,pos.y+504,GEOR13,zwykly,to);
  2350. SDL_itoa(slider->value * r2,buf,10);
  2351. printAtMiddle(buf,pos.x+446,pos.y+504,GEOR13,zwykly,to);
  2352. }
  2353. }
  2354. }
  2355. void CMarketplaceWindow::activate()
  2356. {
  2357. for(int i=0;i<left.size();i++)
  2358. left[i]->activate();
  2359. for(int i=0;i<right.size();i++)
  2360. right[i]->activate();
  2361. ok->activate();
  2362. max->activate();
  2363. deal->activate();
  2364. slider->activate();
  2365. }
  2366. void CMarketplaceWindow::deactivate()
  2367. {
  2368. for(int i=0;i<left.size();i++)
  2369. left[i]->deactivate();
  2370. for(int i=0;i<right.size();i++)
  2371. right[i]->deactivate();
  2372. ok->deactivate();
  2373. max->deactivate();
  2374. deal->deactivate();
  2375. slider->deactivate();
  2376. }
  2377. void CMarketplaceWindow::setMax()
  2378. {
  2379. slider->moveTo(slider->amount);
  2380. }
  2381. void CMarketplaceWindow::makeDeal()
  2382. {
  2383. LOCPLINT->cb->trade(mode,hLeft->id,hRight->id,slider->value*r1);
  2384. slider->moveTo(0);
  2385. hLeft = NULL;
  2386. selectionChanged(true);
  2387. }
  2388. void CMarketplaceWindow::sliderMoved( int to )
  2389. {
  2390. }
  2391. void CMarketplaceWindow::selectionChanged(bool side)
  2392. {
  2393. if(hLeft && hRight && hLeft->id!= hRight->id)
  2394. {
  2395. LOCPLINT->cb->getMarketOffer(hLeft->id,hRight->id,r1,r2,0);
  2396. slider->amount = LOCPLINT->cb->getResourceAmount(hLeft->id) / r1;
  2397. slider->moveTo(0);
  2398. max->block(false);
  2399. deal->block(false);
  2400. }
  2401. else
  2402. {
  2403. max->block(true);
  2404. deal->block(true);
  2405. slider->amount = 0;
  2406. slider->moveTo(0);
  2407. }
  2408. if(side && hLeft) //left selection changed, recalculate offers
  2409. {
  2410. rSubs.clear();
  2411. rSubs.resize(right.size());
  2412. int h1, h2;
  2413. for(int i=0;i<right.size();i++)
  2414. {
  2415. std::ostringstream oss;
  2416. LOCPLINT->cb->getMarketOffer(hLeft->id,i,h1,h2,0);
  2417. oss << h2;
  2418. if(h1!=1)
  2419. oss << "/" << h1;
  2420. rSubs[i] = oss.str();
  2421. }
  2422. }
  2423. }
  2424. CSystemOptionsWindow::CSystemOptionsWindow(const SDL_Rect &pos, CPlayerInterface * owner)
  2425. {
  2426. this->pos = pos;
  2427. background = BitmapHandler::loadBitmap("SysOpbck.bmp", true);
  2428. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  2429. //printing texts
  2430. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[568], 240, 32, GEOR16, tytulowy, background); //window title
  2431. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[569], 122, 65, GEOR16, tytulowy, background); //hero speed
  2432. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[570], 122, 131, GEOR16, tytulowy, background); //enemy speed
  2433. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[571], 122, 197, GEOR16, tytulowy, background); //map scroll speed
  2434. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[20], 122, 263, GEOR16, tytulowy, background); //video quality
  2435. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 348, GEOR16, tytulowy, background); //music volume
  2436. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 414, GEOR16, tytulowy, background); //effects volume
  2437. CSDL_Ext::printAt(CGI->generaltexth->allTexts[572], 283, 57, GEOR16, zwykly, background); //show move path
  2438. CSDL_Ext::printAt(CGI->generaltexth->allTexts[573], 283, 89, GEOR16, zwykly, background); //show hero reminder
  2439. CSDL_Ext::printAt(CGI->generaltexth->allTexts[574], 283, 121, GEOR16, zwykly, background); //quick combat
  2440. CSDL_Ext::printAt(CGI->generaltexth->allTexts[575], 283, 153, GEOR16, zwykly, background); //video subtitles
  2441. CSDL_Ext::printAt(CGI->generaltexth->allTexts[576], 283, 185, GEOR16, zwykly, background); //town building outlines
  2442. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 283, 217, GEOR16, zwykly, background); //spell book animation
  2443. //setting up buttons
  2444. save = new AdventureMapButton (CGI->generaltexth->zelp[321].first, CGI->generaltexth->zelp[321].second, boost::bind(&CSystemOptionsWindow::bsavef, this), 516, 354, "SOSAVE.DEF", SDLK_s);
  2445. std::swap(save->imgs[0][0], save->imgs[0][1]);
  2446. quitGame = new AdventureMapButton (CGI->generaltexth->zelp[324].first, CGI->generaltexth->zelp[324].second, boost::bind(&CSystemOptionsWindow::bquitf, this), 405, 471, "soquit.def", SDLK_q);
  2447. std::swap(quitGame->imgs[0][0], quitGame->imgs[0][1]);
  2448. backToMap = new AdventureMapButton (CGI->generaltexth->zelp[325].first, CGI->generaltexth->zelp[325].second, boost::bind(&CSystemOptionsWindow::breturnf, this), 516, 471, "soretrn.def", SDLK_RETURN);
  2449. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  2450. std::swap(backToMap->imgs[0][0], backToMap->imgs[0][1]);
  2451. heroMoveSpeed = new CHighlightableButtonsGroup(0);
  2452. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[349].first),CGI->generaltexth->zelp[349].second, "sysopb1.def", 187, 134, 1);
  2453. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[350].first),CGI->generaltexth->zelp[350].second, "sysopb2.def", 235, 134, 2);
  2454. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[351].first),CGI->generaltexth->zelp[351].second, "sysopb3.def", 283, 134, 4);
  2455. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[352].first),CGI->generaltexth->zelp[352].second, "sysopb4.def", 331, 134, 8);
  2456. heroMoveSpeed->select(owner->heroMoveSpeed, 1);
  2457. heroMoveSpeed->onChange = boost::bind(&CPlayerInterface::setHeroMoveSpeed, owner, _1);
  2458. mapScrollSpeed = new CHighlightableButtonsGroup(0);
  2459. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[357].first),CGI->generaltexth->zelp[357].second, "sysopb9.def", 187, 267, 1);
  2460. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[358].first),CGI->generaltexth->zelp[358].second, "sysob10.def", 251, 267, 2);
  2461. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[359].first),CGI->generaltexth->zelp[359].second, "sysob11.def", 315, 267, 4);
  2462. mapScrollSpeed->select(owner->mapScrollingSpeed, 1);
  2463. mapScrollSpeed->onChange = boost::bind(&CPlayerInterface::setMapScrollingSpeed, owner, _1);
  2464. musicVolume = new CHighlightableButtonsGroup(0, true);
  2465. for(int i=0; i<10; ++i)
  2466. {
  2467. musicVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[326+i].first),CGI->generaltexth->zelp[326+i].second, "syslb.def", 188 + 19*i, 416, i*11);
  2468. }
  2469. musicVolume->select(CGI->musich->getVolume(), 1);
  2470. musicVolume->onChange = boost::bind(&CMusicHandler::setVolume, CGI->musich, _1);
  2471. effectsVolume = new CHighlightableButtonsGroup(0, true);
  2472. for(int i=0; i<10; ++i)
  2473. {
  2474. effectsVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[336+i].first),CGI->generaltexth->zelp[336+i].second, "syslb.def", 188 + 19*i, 482, i*11);
  2475. }
  2476. effectsVolume->select(CGI->soundh->getVolume(), 1);
  2477. effectsVolume->onChange = boost::bind(&CSoundHandler::setVolume, CGI->soundh, _1);
  2478. }
  2479. CSystemOptionsWindow::~CSystemOptionsWindow()
  2480. {
  2481. SDL_FreeSurface(background);
  2482. delete save;
  2483. delete quitGame;
  2484. delete backToMap;
  2485. delete heroMoveSpeed;
  2486. delete mapScrollSpeed;
  2487. delete musicVolume;
  2488. delete effectsVolume;
  2489. }
  2490. void CSystemOptionsWindow::bquitf()
  2491. {
  2492. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<SComponent*>(), boost::bind(exit, 0), boost::bind(&CSystemOptionsWindow::activate, this), false);
  2493. }
  2494. void CSystemOptionsWindow::breturnf()
  2495. {
  2496. LOCPLINT->popIntTotally(this);
  2497. }
  2498. void CSystemOptionsWindow::bsavef()
  2499. {
  2500. using namespace boost::posix_time;
  2501. std::ostringstream fnameStream;
  2502. fnameStream << second_clock::local_time();
  2503. std::string fname = fnameStream.str();
  2504. boost::algorithm::replace_all(fname,":","");
  2505. boost::algorithm::replace_all(fname," ","-");
  2506. LOCPLINT->showYesNoDialog("Do you want to save current game as " + fname, std::vector<SComponent*>(), boost::bind(&CCallback::save, LOCPLINT->cb, fname), boost::bind(&CSystemOptionsWindow::activate, this), false);
  2507. }
  2508. void CSystemOptionsWindow::activate()
  2509. {
  2510. save->activate();
  2511. quitGame->activate();
  2512. backToMap->activate();
  2513. heroMoveSpeed->activate();
  2514. mapScrollSpeed->activate();
  2515. musicVolume->activate();
  2516. effectsVolume->activate();
  2517. }
  2518. void CSystemOptionsWindow::deactivate()
  2519. {
  2520. save->deactivate();
  2521. quitGame->deactivate();
  2522. backToMap->deactivate();
  2523. heroMoveSpeed->deactivate();
  2524. mapScrollSpeed->deactivate();
  2525. musicVolume->deactivate();
  2526. effectsVolume->deactivate();
  2527. }
  2528. void CSystemOptionsWindow::show(SDL_Surface *to)
  2529. {
  2530. SDL_BlitSurface(background, NULL, to, &pos);
  2531. save->show(to);
  2532. quitGame->show(to);
  2533. backToMap->show(to);
  2534. heroMoveSpeed->show(to);
  2535. mapScrollSpeed->show(to);
  2536. musicVolume->show(to);
  2537. effectsVolume->show(to);
  2538. }
  2539. CTavernWindow::CTavernWindow(const CGHeroInstance *H1, const CGHeroInstance *H2, const std::string &gossip)
  2540. :h1(selected,0,72,299,H1),h2(selected,1,162,299,H2)
  2541. {
  2542. if(H1)
  2543. selected = 0;
  2544. else
  2545. selected = -1;
  2546. oldSelected = -1;
  2547. bg = BitmapHandler::loadBitmap("TPTAVERN.bmp");
  2548. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2549. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  2550. printAtMiddle(CGI->generaltexth->jktexts[37],200,35,GEOR16,tytulowy,bg);
  2551. printAtMiddle("2500",320,328,GEOR13,zwykly,bg);
  2552. printAtMiddle(CGI->generaltexth->jktexts[38],146,283,GEOR16,tytulowy,bg);
  2553. printAtMiddleWB(gossip,200,220,GEOR13,50,zwykly,bg);
  2554. pos.w = bg->w;
  2555. pos.h = bg->h;
  2556. pos.x = (screen->w-bg->w)/2;
  2557. pos.y = (screen->h-bg->h)/2;
  2558. bar = new CStatusBar(pos.x+8, pos.y+478, "APHLFTRT.bmp", 380);
  2559. h1.pos.x += pos.x;
  2560. h2.pos.x += pos.x;
  2561. h1.pos.y += pos.y;
  2562. h2.pos.y += pos.y;
  2563. cancel = new AdventureMapButton(CGI->generaltexth->tavernInfo[7],"",boost::bind(&CTavernWindow::close,this),pos.x+310,pos.y+428,"ICANCEL.DEF",SDLK_ESCAPE);
  2564. recruit = new AdventureMapButton("","",boost::bind(&CTavernWindow::recruitb,this),pos.x+272,pos.y+355,"TPTAV01.DEF",SDLK_RETURN);
  2565. thiefGuild = new AdventureMapButton(CGI->generaltexth->tavernInfo[5],"",0,pos.x+22,pos.y+428,"TPTAV02.DEF",SDLK_t);
  2566. if(LOCPLINT->cb->getResourceAmount(6) < 2500) //not enough gold
  2567. {
  2568. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[0]; //Cannot afford a Hero
  2569. recruit->block(2);
  2570. }
  2571. else if(LOCPLINT->cb->howManyHeroes(false) >= 8)
  2572. {
  2573. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  2574. boost::algorithm::replace_first(recruit->hoverTexts[0],"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes()));
  2575. recruit->block(2);
  2576. }
  2577. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  2578. {
  2579. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[2]; //Cannot recruit. You already have a Hero in this town.
  2580. recruit->block(2);
  2581. }
  2582. else
  2583. {
  2584. if(!H1)
  2585. recruit->block(1);
  2586. }
  2587. }
  2588. void CTavernWindow::recruitb()
  2589. {
  2590. const CGHeroInstance *toBuy = (selected ? h2 : h1).h;
  2591. close();
  2592. LOCPLINT->cb->recruitHero(LOCPLINT->castleInt->town,toBuy);
  2593. }
  2594. CTavernWindow::~CTavernWindow()
  2595. {
  2596. SDL_FreeSurface(bg);
  2597. delete cancel;
  2598. delete thiefGuild;
  2599. delete recruit;
  2600. delete bar;
  2601. }
  2602. void CTavernWindow::activate()
  2603. {
  2604. thiefGuild->activate();
  2605. cancel->activate();
  2606. if(h1.h)
  2607. h1.activate();
  2608. if(h2.h)
  2609. h2.activate();
  2610. recruit->activate();
  2611. LOCPLINT->statusbar = bar;
  2612. }
  2613. void CTavernWindow::deactivate()
  2614. {
  2615. thiefGuild->deactivate();
  2616. cancel->deactivate();
  2617. if(h1.h)
  2618. h1.deactivate();
  2619. if(h2.h)
  2620. h2.deactivate();
  2621. recruit->deactivate();
  2622. }
  2623. void CTavernWindow::close()
  2624. {
  2625. LOCPLINT->popIntTotally(this);
  2626. }
  2627. void CTavernWindow::show(SDL_Surface * to)
  2628. {
  2629. blitAt(bg,pos.x,pos.y,to);
  2630. if(h1.h)
  2631. h1.show(to);
  2632. if(h2.h)
  2633. h2.show(to);
  2634. thiefGuild->show(to);
  2635. cancel->show(to);
  2636. recruit->show(to);
  2637. bar->show(to);
  2638. if(selected >= 0)
  2639. {
  2640. HeroPortrait *sel = selected ? &h2 : &h1;
  2641. if (selected != oldSelected && !recruit->blocked)
  2642. {
  2643. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  2644. oldSelected = selected;
  2645. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[3]; //Recruit %s the %s
  2646. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->name);
  2647. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->type->heroClass->name);
  2648. }
  2649. printAtMiddleWB(sel->descr,pos.x+146,pos.y+389,GEOR13,40,zwykly,to);
  2650. CSDL_Ext::drawBorder(to,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  2651. }
  2652. }
  2653. void CTavernWindow::HeroPortrait::clickLeft(boost::logic::tribool down)
  2654. {
  2655. if(pressedL && !down)
  2656. as();
  2657. ClickableL::clickLeft(down);
  2658. }
  2659. void CTavernWindow::HeroPortrait::activate()
  2660. {
  2661. ClickableL::activate();
  2662. ClickableR::activate();
  2663. Hoverable::activate();
  2664. }
  2665. void CTavernWindow::HeroPortrait::deactivate()
  2666. {
  2667. ClickableL::deactivate();
  2668. ClickableR::deactivate();
  2669. Hoverable::deactivate();
  2670. }
  2671. void CTavernWindow::HeroPortrait::clickRight(boost::logic::tribool down)
  2672. {
  2673. if(down)
  2674. {
  2675. LOCPLINT->adventureInt->heroWindow->setHero(h);
  2676. LOCPLINT->pushInt(new CRClickPopupInt(LOCPLINT->adventureInt->heroWindow,false));
  2677. }
  2678. }
  2679. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  2680. :as(sel,id)
  2681. {
  2682. h = H;
  2683. pos.x = x;
  2684. pos.y = y;
  2685. pos.w = 58;
  2686. pos.h = 64;
  2687. if(H)
  2688. {
  2689. hoverName = CGI->generaltexth->tavernInfo[4];
  2690. boost::algorithm::replace_first(hoverName,"%s",H->name);
  2691. }
  2692. int artifs = h->artifWorn.size() + h->artifacts.size();
  2693. for(int i=13; i<=17; i++) //war machines and spellbook don't count
  2694. if(vstd::contains(h->artifWorn,i))
  2695. artifs--;
  2696. sprintf_s(descr, sizeof(descr),CGI->generaltexth->allTexts[215].c_str(),
  2697. h->name.c_str(), h->level, h->type->heroClass->name.c_str(), artifs);
  2698. descr[sizeof(descr)-1] = '\0';
  2699. }
  2700. void CTavernWindow::HeroPortrait::show(SDL_Surface * to)
  2701. {
  2702. blitAt(graphics->portraitLarge[h->subID],pos,to);
  2703. }
  2704. void CTavernWindow::HeroPortrait::hover( bool on )
  2705. {
  2706. Hoverable::hover(on);
  2707. if(on)
  2708. LOCPLINT->statusbar->print(hoverName);
  2709. else
  2710. LOCPLINT->statusbar->clear();
  2711. }
  2712. void CInGameConsole::activate()
  2713. {
  2714. KeyInterested::activate();
  2715. }
  2716. void CInGameConsole::deactivate()
  2717. {
  2718. KeyInterested::deactivate();
  2719. }
  2720. void CInGameConsole::show(SDL_Surface * to)
  2721. {
  2722. int number = 0;
  2723. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  2724. texts_mx.lock();
  2725. for(std::list< std::pair< std::string, int > >::iterator it = texts.begin(); it != texts.end(); ++it, ++number)
  2726. {
  2727. SDL_Color green = {0,0xff,0,0};
  2728. Point leftBottomCorner(0, screen->h);
  2729. if(LOCPLINT->battleInt)
  2730. {
  2731. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  2732. }
  2733. CSDL_Ext::printAt(it->first, leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20, TNRB16, green);
  2734. if(SDL_GetTicks() - it->second > defaultTimeout)
  2735. {
  2736. toDel.push_back(it);
  2737. }
  2738. }
  2739. for(int it=0; it<toDel.size(); ++it)
  2740. {
  2741. texts.erase(toDel[it]);
  2742. }
  2743. texts_mx.unlock();
  2744. }
  2745. void CInGameConsole::print(const std::string &txt)
  2746. {
  2747. texts_mx.lock();
  2748. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  2749. if(texts.size() > maxDisplayedTexts)
  2750. {
  2751. texts.pop_front();
  2752. }
  2753. texts_mx.unlock();
  2754. }
  2755. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  2756. {
  2757. if(key.type != SDL_KEYDOWN) return;
  2758. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  2759. switch(key.keysym.sym)
  2760. {
  2761. case SDLK_TAB:
  2762. {
  2763. if(captureAllKeys)
  2764. {
  2765. captureAllKeys = false;
  2766. endEnteringText(false);
  2767. }
  2768. else
  2769. {
  2770. captureAllKeys = true;
  2771. startEnteringText();
  2772. }
  2773. break;
  2774. }
  2775. case SDLK_RETURN: //enter key
  2776. {
  2777. if(enteredText.size() > 0 && captureAllKeys)
  2778. {
  2779. captureAllKeys = false;
  2780. endEnteringText(true);
  2781. }
  2782. break;
  2783. }
  2784. case SDLK_BACKSPACE:
  2785. {
  2786. if(enteredText.size() > 1)
  2787. {
  2788. enteredText.resize(enteredText.size()-1);
  2789. enteredText[enteredText.size()-1] = '_';
  2790. refreshEnteredText();
  2791. }
  2792. break;
  2793. }
  2794. case SDLK_UP: //up arrow
  2795. {
  2796. if(prevEntDisp == -1)
  2797. {
  2798. prevEntDisp = previouslyEntered.size() - 1;
  2799. enteredText = previouslyEntered[prevEntDisp] + "_";
  2800. refreshEnteredText();
  2801. }
  2802. else if( prevEntDisp > 0)
  2803. {
  2804. --prevEntDisp;
  2805. enteredText = previouslyEntered[prevEntDisp] + "_";
  2806. refreshEnteredText();
  2807. }
  2808. break;
  2809. }
  2810. case SDLK_DOWN: //down arrow
  2811. {
  2812. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  2813. {
  2814. ++prevEntDisp;
  2815. enteredText = previouslyEntered[prevEntDisp] + "_";
  2816. refreshEnteredText();
  2817. }
  2818. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  2819. {
  2820. prevEntDisp = -1;
  2821. enteredText = "_";
  2822. refreshEnteredText();
  2823. }
  2824. break;
  2825. }
  2826. default:
  2827. {
  2828. if(enteredText.size() > 0)
  2829. {
  2830. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  2831. {
  2832. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  2833. enteredText += "_";
  2834. refreshEnteredText();
  2835. }
  2836. }
  2837. break;
  2838. }
  2839. }
  2840. }
  2841. void CInGameConsole::startEnteringText()
  2842. {
  2843. enteredText = "_";
  2844. if(LOCPLINT->topInt() == LOCPLINT->adventureInt)
  2845. {
  2846. LOCPLINT->statusbar->print(enteredText);
  2847. }
  2848. else if(LOCPLINT->battleInt)
  2849. {
  2850. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  2851. }
  2852. }
  2853. void CInGameConsole::endEnteringText(bool printEnteredText)
  2854. {
  2855. prevEntDisp = -1;
  2856. if(printEnteredText)
  2857. {
  2858. std::string txt = enteredText.substr(0, enteredText.size()-1);
  2859. LOCPLINT->cb->sendMessage(txt);
  2860. previouslyEntered.push_back(txt);
  2861. print(txt);
  2862. }
  2863. enteredText = "";
  2864. if(LOCPLINT->topInt() == LOCPLINT->adventureInt)
  2865. {
  2866. LOCPLINT->statusbar->clear();
  2867. }
  2868. else if(LOCPLINT->battleInt)
  2869. {
  2870. LOCPLINT->battleInt->console->ingcAlter = "";
  2871. }
  2872. }
  2873. void CInGameConsole::refreshEnteredText()
  2874. {
  2875. if(LOCPLINT->topInt() == LOCPLINT->adventureInt)
  2876. {
  2877. LOCPLINT->statusbar->print(enteredText);
  2878. }
  2879. else if(LOCPLINT->battleInt)
  2880. {
  2881. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  2882. }
  2883. }
  2884. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  2885. {
  2886. }
  2887. void CGarrisonWindow::close()
  2888. {
  2889. LOCPLINT->popIntTotally(this);
  2890. }
  2891. void CGarrisonWindow::activate()
  2892. {
  2893. split->activate();
  2894. quit->activate();
  2895. garr->activate();
  2896. }
  2897. void CGarrisonWindow::deactivate()
  2898. {
  2899. split->deactivate();
  2900. quit->deactivate();
  2901. garr->deactivate();
  2902. }
  2903. void CGarrisonWindow::show(SDL_Surface * to)
  2904. {
  2905. blitAt(bg,pos,to);
  2906. split->show(to);
  2907. quit->show(to);
  2908. garr->show(to);
  2909. blitAt(graphics->flags->ourImages[garr->odown->getOwner()].bitmap,pos.x+29,pos.y+125,to);
  2910. blitAt(graphics->portraitLarge[static_cast<const CGHeroInstance*>(garr->odown)->portrait],pos.x+29,pos.y+222,to);
  2911. printAtMiddle(CGI->generaltexth->allTexts[709],pos.x+275,pos.y+30,GEOR16,tytulowy,to);
  2912. }
  2913. CGarrisonWindow::CGarrisonWindow( const CArmedInstance *up, const CGHeroInstance *down )
  2914. {
  2915. bg = BitmapHandler::loadBitmap("GARRISON.bmp");
  2916. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2917. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  2918. pos.x = screen->w/2 - bg->w/2;
  2919. pos.y = screen->h/2 - bg->h/2;
  2920. pos.w = screen->w;
  2921. pos.h = screen->h;
  2922. garr = new CGarrisonInt(pos.x+92, pos.y+129, 4, 30, bg, 92, 129, up, down);
  2923. split = new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+88,pos.y+314,"IDV6432.DEF");
  2924. quit = new AdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CGarrisonWindow::close,this),pos.x+399,pos.y+314,"IOK6432.DEF",SDLK_RETURN);
  2925. }
  2926. CGarrisonWindow::~CGarrisonWindow()
  2927. {
  2928. SDL_FreeSurface(bg);
  2929. delete split;
  2930. delete quit;
  2931. delete garr;
  2932. }
  2933. IShowActivable::IShowActivable()
  2934. {
  2935. type = 0;
  2936. }
  2937. CWindowWithGarrison::CWindowWithGarrison()
  2938. {
  2939. type |= WITH_GARRISON;
  2940. }
  2941. void CRClickPopupInt::show(SDL_Surface * to)
  2942. {
  2943. inner->show(to);
  2944. }
  2945. CRClickPopupInt::CRClickPopupInt( IShowActivable *our, bool deleteInt )
  2946. {
  2947. inner = our;
  2948. delInner = deleteInt;
  2949. }
  2950. CRClickPopupInt::~CRClickPopupInt()
  2951. {
  2952. //workaround for hero window issue - if it's our interface, call dispose to properly reset it's state
  2953. //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...?)
  2954. if(inner == LOCPLINT->adventureInt->heroWindow)
  2955. LOCPLINT->adventureInt->heroWindow->dispose();
  2956. if(delInner)
  2957. delete inner;
  2958. }