GUIClasses.cpp 104 KB

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