GUIClasses.cpp 142 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259
  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/CBuildingHandler.h"
  19. #include "../hch/CGeneralTextHandler.h"
  20. #include "../hch/CHeroHandler.h"
  21. #include "../hch/CLodHandler.h"
  22. #include "../hch/CObjectHandler.h"
  23. #include "../hch/CSpellHandler.h"
  24. #include "../hch/CTownHandler.h"
  25. #include "../lib/CondSh.h"
  26. #include "../lib/map.h"
  27. #include "../mapHandler.h"
  28. #include "../timeHandler.h"
  29. #include <boost/foreach.hpp>
  30. #include <boost/lexical_cast.hpp>
  31. #include <boost/format.hpp>
  32. #include <boost/algorithm/string.hpp>
  33. #include <boost/algorithm/string/replace.hpp>
  34. #include <boost/assign/std/vector.hpp>
  35. #include <boost/assign/list_of.hpp>
  36. #include <boost/date_time/posix_time/posix_time.hpp>
  37. //#include <boost/thread.hpp>
  38. #include <cmath>
  39. #include <queue>
  40. #include <sstream>
  41. #include "../lib/NetPacks.h"
  42. #include "CSpellWindow.h"
  43. #include "CHeroWindow.h"
  44. #include "../hch/CVideoHandler.h"
  45. #include "../StartInfo.h"
  46. #include "CPreGame.h"
  47. /*
  48. * GUIClasses.cpp, part of VCMI engine
  49. *
  50. * Authors: listed in file AUTHORS in main folder
  51. *
  52. * License: GNU General Public License v2.0 or later
  53. * Full text of license available in license.txt file, in main folder
  54. *
  55. */
  56. using namespace boost::assign;
  57. using namespace CSDL_Ext;
  58. extern std::queue<SDL_Event*> events;
  59. extern boost::mutex eventsM;
  60. CTextInput * CTextInput::inputWithFocus;
  61. #undef min
  62. #undef max
  63. static StackState* getStackState(const CGObjectInstance *obj, int pos, bool town)
  64. {
  65. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(obj);
  66. if(!h) return NULL;
  67. StackState *pom = new StackState();
  68. pom->shotsLeft = -1;
  69. pom->healthBonus = h->valOfBonuses(HeroBonus::STACK_HEALTH);
  70. pom->currentHealth = 0;
  71. pom->attackBonus = h->getPrimSkillLevel(0);
  72. pom->defenseBonus = h->getPrimSkillLevel(1);
  73. pom->luck = h->getCurrentLuck();
  74. pom->morale = h->getCurrentMorale(pos,town);
  75. pom->speedBonus = h->valOfBonuses(HeroBonus::STACKS_SPEED);
  76. pom->dmgMultiplier = 1;
  77. return pom;
  78. }
  79. void CGarrisonSlot::hover (bool on)
  80. {
  81. ////Hoverable::hover(on);
  82. if(on)
  83. {
  84. std::string temp;
  85. if(creature)
  86. {
  87. if(owner->highlighted)
  88. {
  89. if(owner->highlighted == this)
  90. {
  91. temp = CGI->generaltexth->tcommands[4]; //View %s
  92. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  93. }
  94. else if (owner->highlighted->creature == creature)
  95. {
  96. temp = CGI->generaltexth->tcommands[2]; //Combine %s armies
  97. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  98. }
  99. else if (owner->highlighted->creature)
  100. {
  101. temp = CGI->generaltexth->tcommands[7]; //Exchange %s with %s
  102. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  103. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  104. }
  105. else
  106. {
  107. tlog2 << "Warning - shouldn't be - highlighted void slot "<<owner->highlighted<<std::endl;
  108. tlog2 << "Highlighted set to NULL"<<std::endl;
  109. owner->highlighted = NULL;
  110. }
  111. }
  112. else
  113. {
  114. if(upg)
  115. {
  116. temp = CGI->generaltexth->tcommands[32]; //Select %s (visiting)
  117. }
  118. else if(owner->oup && owner->oup->ID == TOWNI_TYPE)
  119. {
  120. temp = CGI->generaltexth->tcommands[12]; //Select %s (in garrison)
  121. }
  122. else
  123. {
  124. temp = CGI->generaltexth->allTexts[481]; //Select %s
  125. }
  126. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  127. };
  128. }
  129. else
  130. {
  131. if(owner->highlighted)
  132. {
  133. const CArmedInstance *highl = owner->highlighted->getObj();
  134. if( highl->needsLastStack() //we are moving stack from hero's
  135. && highl->army.slots.size() == 1 //it's only stack
  136. && owner->highlighted->upg != upg //we're moving it to the other garrison
  137. )
  138. {
  139. temp = CGI->generaltexth->tcommands[5]; //Cannot move last army to garrison
  140. }
  141. else
  142. {
  143. temp = CGI->generaltexth->tcommands[6]; //Move %s
  144. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  145. }
  146. }
  147. else
  148. {
  149. temp = CGI->generaltexth->tcommands[11]; //Empty
  150. }
  151. }
  152. GH.statusbar->print(temp);
  153. }
  154. else
  155. {
  156. GH.statusbar->clear();
  157. }
  158. }
  159. const CArmedInstance * CGarrisonSlot::getObj()
  160. {
  161. return (!upg)?(owner->oup):(owner->odown);
  162. }
  163. void CGarrisonSlot::clickRight(tribool down, bool previousState)
  164. {
  165. StackState *pom = getStackState(getObj(),ID, GH.topInt() == LOCPLINT->castleInt);
  166. if(down && creature)
  167. {
  168. GH.pushInt(new CCreInfoWindow(creature->idNumber, 0, count, pom, 0, 0, NULL));
  169. }
  170. delete pom;
  171. }
  172. void CGarrisonSlot::clickLeft(tribool down, bool previousState)
  173. {
  174. if(owner->ignoreEvent)
  175. {
  176. owner->ignoreEvent = false;
  177. return;
  178. }
  179. if(down)
  180. {
  181. bool refr = false;
  182. if(owner->highlighted)
  183. {
  184. if(owner->highlighted == this) //view info
  185. {
  186. StackState *pom2 = getStackState(getObj(), ID, GH.topInt() == LOCPLINT->castleInt);
  187. UpgradeInfo pom = LOCPLINT->cb->getUpgradeInfo(getObj(), ID);
  188. CCreInfoWindow *creWindow = NULL;
  189. if(pom.oldID>=0) //upgrade is possible
  190. {
  191. creWindow = new CCreInfoWindow(
  192. creature->idNumber, 1, count, pom2,
  193. boost::bind(&CCallback::upgradeCreature, LOCPLINT->cb, getObj(), ID, pom.newID[0]), //bind upgrade function
  194. boost::bind(&CCallback::dismissCreature, LOCPLINT->cb, getObj(), ID), &pom);
  195. }
  196. else
  197. {
  198. creWindow = new CCreInfoWindow(
  199. creature->idNumber, 1, count, pom2, 0,
  200. boost::bind(&CCallback::dismissCreature, LOCPLINT->cb, getObj(), ID), NULL);
  201. }
  202. GH.pushInt(creWindow);
  203. owner->highlighted = NULL;
  204. owner->splitting = false;
  205. for(size_t i = 0; i<owner->splitButtons.size(); i++)
  206. owner->splitButtons[i]->block(true);
  207. show(screen2);
  208. refr = true;
  209. delete pom2;
  210. }
  211. else
  212. {
  213. // Only allow certain moves if troops aren't removable.
  214. if (owner->removableUnits
  215. || (upg == 0 && (owner->highlighted->upg == 1 && !creature))
  216. || (upg == 1 && owner->highlighted->upg == 1))
  217. {
  218. //we want to split
  219. if((owner->splitting || LOCPLINT->shiftPressed())
  220. && (!creature
  221. || (creature == owner->highlighted->creature)))
  222. {
  223. owner->p2 = ID; //store the second stack pos
  224. owner->pb = upg;//store the second stack owner (up or down army)
  225. owner->splitting = false;
  226. int totalAmount = owner->highlighted->count;
  227. if(creature)
  228. totalAmount += count;
  229. int last = -1;
  230. if(upg != owner->highlighted->upg) //not splitting within same army
  231. {
  232. if(owner->highlighted->getObj()->army.slots.size() == 1 //we're splitting away the last stack
  233. && owner->highlighted->getObj()->needsLastStack() )
  234. {
  235. last = 0;
  236. }
  237. if(getObj()->army.slots.size() == 1 //destination army can't be emptied, unless we're rebalancing two stacks of same creature
  238. && owner->highlighted->creature == creature
  239. && getObj()->needsLastStack() )
  240. {
  241. last += 2;
  242. }
  243. }
  244. CSplitWindow * spw = new CSplitWindow(owner->highlighted->creature->idNumber, totalAmount, owner, last, count);
  245. GH.pushInt(spw);
  246. refr = true;
  247. }
  248. else if(creature != owner->highlighted->creature) //swap
  249. {
  250. LOCPLINT->cb->swapCreatures(
  251. (!upg)?(owner->oup):(owner->odown),
  252. (!owner->highlighted->upg)?(owner->oup):(owner->odown),
  253. ID,owner->highlighted->ID);
  254. }
  255. else //merge
  256. {
  257. LOCPLINT->cb->mergeStacks(
  258. (!owner->highlighted->upg)?(owner->oup):(owner->odown),
  259. (!upg)?(owner->oup):(owner->odown),
  260. owner->highlighted->ID,ID);
  261. }
  262. }
  263. else // Highlight
  264. {
  265. if(creature)
  266. owner->highlighted = this;
  267. show(screen2);
  268. refr = true;
  269. }
  270. }
  271. }
  272. else //highlight
  273. {
  274. if(creature)
  275. {
  276. owner->highlighted = this;
  277. for(size_t i = 0; i<owner->splitButtons.size(); i++)
  278. owner->splitButtons[i]->block(false);
  279. }
  280. show(screen2);
  281. refr = true;
  282. }
  283. if(refr) {hover(false); hover(true); } //to refresh statusbar
  284. }
  285. }
  286. void CGarrisonSlot::activate()
  287. {
  288. if(!active) active=true;
  289. else return;
  290. activateLClick();
  291. activateRClick();
  292. activateHover();
  293. }
  294. void CGarrisonSlot::deactivate()
  295. {
  296. if(active) active=false;
  297. else return;
  298. deactivateLClick();
  299. deactivateRClick();
  300. deactivateHover();
  301. }
  302. CGarrisonSlot::CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg, const CCreature * Creature, int Count)
  303. {
  304. active = false;
  305. upg = Upg;
  306. count = Count;
  307. ID = IID;
  308. creature = Creature;
  309. pos.x = x;
  310. pos.y = y;
  311. if(Owner->smallIcons)
  312. {
  313. pos.w = 32;
  314. pos.h = 32;
  315. }
  316. else
  317. {
  318. pos.w = 58;
  319. pos.h = 64;
  320. }
  321. owner = Owner;
  322. }
  323. CGarrisonSlot::~CGarrisonSlot()
  324. {
  325. if(active)
  326. deactivate();
  327. }
  328. void CGarrisonSlot::show(SDL_Surface * to)
  329. {
  330. std::map<int,SDL_Surface*> &imgs = (owner->smallIcons ? graphics->smallImgs : graphics->bigImgs);
  331. if(creature)
  332. {
  333. char buf[15];
  334. SDL_itoa(count,buf,10);
  335. blitAt(imgs[creature->idNumber],pos,to);
  336. printTo(buf, pos.x+pos.w, pos.y+pos.h+1, owner->smallIcons ? FONT_TINY : FONT_MEDIUM, zwykly, to);
  337. if((owner->highlighted==this)
  338. || (owner->splitting && owner->highlighted->creature == creature))
  339. {
  340. blitAt(imgs[-1],pos,to);
  341. }
  342. }
  343. else//empty slot
  344. {
  345. Rect pos1 = pos, pos2 = pos; //positions on the garr bg sur and scren
  346. pos1.x = owner->surOffset.x+ pos.x-owner->pos.x;
  347. pos1.y = owner->surOffset.y+ pos.y-owner->pos.y;
  348. SDL_BlitSurface(owner->sur,&pos1,to,&pos2);
  349. if(owner->splitting)
  350. blitAt(imgs[-1],pos,to);
  351. }
  352. }
  353. CGarrisonInt::~CGarrisonInt()
  354. {
  355. if(sup)
  356. {
  357. for(size_t i=0;i<sup->size();i++)
  358. {
  359. delete (*sup)[i];
  360. }
  361. delete sup;
  362. }
  363. if(sdown)
  364. {
  365. for(size_t i=0;i<sdown->size();i++)
  366. {
  367. delete (*sdown)[i]; //XXX what about smartpointers? boost or auto_ptr from std
  368. }
  369. delete sdown;
  370. }
  371. for(size_t i = 0; i<splitButtons.size(); i++)
  372. delete splitButtons[i];
  373. }
  374. void CGarrisonInt::show(SDL_Surface * to)
  375. {
  376. if(sup)
  377. {
  378. for(size_t i = 0; i<sup->size(); i++)
  379. {
  380. if((*sup)[i])
  381. {
  382. (*sup)[i]->show(to);
  383. }
  384. }
  385. }
  386. if(sdown)
  387. {
  388. for(size_t i = 0; i<sdown->size(); i++)
  389. {
  390. if((*sdown)[i])
  391. {
  392. (*sdown)[i]->show(to);
  393. }
  394. }
  395. }
  396. for(size_t i = 0; i<splitButtons.size(); i++)
  397. splitButtons[i]->show(to);
  398. }
  399. void CGarrisonInt::deactiveteSlots()
  400. {
  401. if(sup)
  402. {
  403. for(int i = 0; i<sup->size(); i++)
  404. {
  405. if((*sup)[i])
  406. {
  407. (*sup)[i]->deactivate();
  408. }
  409. }
  410. }
  411. if(sdown)
  412. {
  413. for(int i = 0; i<sdown->size(); i++)
  414. {
  415. if((*sdown)[i])
  416. {
  417. (*sdown)[i]->deactivate();
  418. }
  419. }
  420. }
  421. }
  422. void CGarrisonInt::activeteSlots()
  423. {
  424. if(sup)
  425. {
  426. for(int i = 0; i<sup->size(); i++)
  427. {
  428. if((*sup)[i])
  429. {
  430. (*sup)[i]->activate();
  431. }
  432. }
  433. }
  434. if(sdown)
  435. {
  436. for(int i = 0; i<sdown->size(); i++)
  437. {
  438. if((*sdown)[i])
  439. {
  440. (*sdown)[i]->activate();
  441. }
  442. }
  443. }
  444. }
  445. void CGarrisonInt::createSlots()
  446. {
  447. int h, w; //height and width of slot
  448. if(smallIcons)
  449. {
  450. h = w = 32;
  451. }
  452. else
  453. {
  454. h = 64;
  455. w = 58;
  456. }
  457. if(set1)
  458. {
  459. sup = new std::vector<CGarrisonSlot*>(7,(CGarrisonSlot *)(NULL));
  460. for
  461. (std::map<si32,std::pair<ui32,si32> >::const_iterator i=set1->slots.begin();
  462. i!=set1->slots.end(); i++)
  463. {
  464. (*sup)[i->first] =
  465. new CGarrisonSlot(this, pos.x + (i->first*(w+interx)), pos.y,i->first, 0,
  466. &CGI->creh->creatures[i->second.first],i->second.second);
  467. }
  468. for(int i=0; i<sup->size(); i++)
  469. if((*sup)[i] == NULL)
  470. (*sup)[i] = new CGarrisonSlot(this, pos.x + (i*(w+interx)), pos.y,i,0,NULL, 0);
  471. if (shiftPos)
  472. for (int i=shiftPos; i<sup->size(); i++)
  473. {
  474. (*sup)[i]->pos.x += shiftPoint.x;
  475. (*sup)[i]->pos.y += shiftPoint.y;
  476. };
  477. }
  478. if(set2)
  479. {
  480. sdown = new std::vector<CGarrisonSlot*>(7,(CGarrisonSlot *)(NULL));
  481. for
  482. (std::map<si32,std::pair<ui32,si32> >::const_iterator i=set2->slots.begin();
  483. i!=set2->slots.end(); i++)
  484. {
  485. (*sdown)[i->first] =
  486. new CGarrisonSlot(this, pos.x + (i->first*(w+interx)) + garOffset.x, pos.y + garOffset.y,i->first,1,
  487. &CGI->creh->creatures[i->second.first],i->second.second);
  488. }
  489. for(int i=0; i<sdown->size(); i++)
  490. if((*sdown)[i] == NULL)
  491. (*sdown)[i] = new CGarrisonSlot(this, pos.x + (i*(w+interx)) + garOffset.x, pos.y + garOffset.y,i,1, NULL, 0);
  492. if (shiftPos)
  493. for (int i=shiftPos; i<sup->size(); i++)
  494. {
  495. (*sdown)[i]->pos.x += shiftPoint.x;
  496. (*sdown)[i]->pos.y += shiftPoint.y;
  497. };
  498. }
  499. }
  500. void CGarrisonInt::deleteSlots()
  501. {
  502. if(sup)
  503. {
  504. for(int i = 0; i<sup->size(); i++)
  505. {
  506. if((*sup)[i])
  507. {
  508. delete (*sup)[i];
  509. }
  510. }
  511. delete sup;
  512. sup = NULL;
  513. }
  514. if(sdown)
  515. {
  516. for(int i = 0; i<sdown->size(); i++)
  517. {
  518. if((*sdown)[i])
  519. {
  520. delete (*sdown)[i];
  521. }
  522. }
  523. delete sdown;
  524. sdown = NULL;
  525. }
  526. }
  527. void CGarrisonInt::recreateSlots()
  528. {
  529. splitting = false;
  530. highlighted = NULL;
  531. for(size_t i = 0; i<splitButtons.size(); i++)
  532. splitButtons[i]->block(true);
  533. if(active)
  534. {
  535. deactiveteSlots();
  536. }
  537. deleteSlots();
  538. createSlots();
  539. if(active)
  540. {
  541. //ignoreEvent = true;
  542. activeteSlots();
  543. //show(screen2);
  544. }
  545. }
  546. void CGarrisonInt::splitClick()
  547. {
  548. if(!highlighted)
  549. return;
  550. splitting = !splitting;
  551. show(screen2);
  552. }
  553. void CGarrisonInt::splitStacks(int am2)
  554. {
  555. LOCPLINT->cb->splitStack(
  556. (highlighted->upg)?(odown):(oup),
  557. (pb)?(odown):(oup),
  558. highlighted->ID,
  559. p2,
  560. am2);
  561. }
  562. CGarrisonInt::CGarrisonInt(int x, int y, int inx, const Point &garsOffset, SDL_Surface *&pomsur, const Point& SurOffset,
  563. const CArmedInstance *s1, const CArmedInstance *s2, bool _removableUnits, bool smallImgs, int _shiftPos, const Point &_shiftPoint)
  564. :interx(inx),garOffset(garsOffset),highlighted(NULL),sur(pomsur),surOffset(SurOffset),sup(NULL),
  565. sdown(NULL),oup(s1),odown(s2), removableUnits(_removableUnits), smallIcons(smallImgs), shiftPos(_shiftPos), shiftPoint(_shiftPoint)
  566. {
  567. active = false;
  568. splitting = false;
  569. set1 = LOCPLINT->cb->getGarrison(s1);
  570. set2 = LOCPLINT->cb->getGarrison(s2);
  571. ignoreEvent = false;
  572. update = true;
  573. pos.x=(x);
  574. pos.y=(y);
  575. pos.w=(58);
  576. pos.h=(64);
  577. createSlots();
  578. }
  579. void CGarrisonInt::activate()
  580. {
  581. for(size_t i = 0; i<splitButtons.size(); i++)
  582. if(splitButtons[i]->blocked != !highlighted)
  583. splitButtons[i]->block(!highlighted);
  584. active = true;
  585. if(sup)
  586. {
  587. for(int i = 0; i<sup->size(); i++)
  588. if((*sup)[i])
  589. (*sup)[i]->activate();
  590. }
  591. if(sdown)
  592. {
  593. for(int i = 0; i<sdown->size(); i++)
  594. if((*sdown)[i])
  595. (*sdown)[i]->activate();
  596. }
  597. for(size_t i = 0; i<splitButtons.size(); i++)
  598. splitButtons[i]->activate();
  599. }
  600. void CGarrisonInt::deactivate()
  601. {
  602. active = false;
  603. deactiveteSlots();
  604. for(size_t i = 0; i<splitButtons.size(); i++)
  605. splitButtons[i]->deactivate();
  606. }
  607. 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)
  608. {
  609. slider = NULL;
  610. ID = -1;
  611. this->delComps = delComps;
  612. for(int i=0;i<Buttons.size();i++)
  613. {
  614. buttons.push_back(new AdventureMapButton("","",boost::bind(&CInfoWindow::close,this),0,0,Buttons[i].first));
  615. buttons[i]->callback.add(Buttons[i].second); //each button will close the window apart from call-defined actions
  616. }
  617. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  618. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  619. for(int i=0;i<comps.size();i++)
  620. {
  621. components.push_back(comps[i]);
  622. }
  623. CMessage::drawIWindow(this,text,player,charperline);
  624. }
  625. CInfoWindow::CInfoWindow()
  626. {
  627. ID = -1;
  628. delComps = false;
  629. }
  630. void CInfoWindow::close()
  631. {
  632. GH.popIntTotally(this);
  633. if(LOCPLINT)
  634. LOCPLINT->showingDialog->setn(false);
  635. }
  636. void CInfoWindow::show(SDL_Surface * to)
  637. {
  638. CSimpleWindow::show(to);
  639. for(int i=0;i<buttons.size();i++)
  640. buttons[i]->show(to);
  641. if (slider)
  642. slider->show(to);
  643. }
  644. CInfoWindow::~CInfoWindow()
  645. {
  646. if(delComps)
  647. {
  648. for (int i=0;i<components.size();i++)
  649. delete components[i];
  650. }
  651. for(int i=0;i<buttons.size();i++)
  652. delete buttons[i];
  653. if (slider)
  654. delete slider;
  655. }
  656. void CInfoWindow::activate()
  657. {
  658. for (int i=0;i<components.size();i++)
  659. components[i]->activate();
  660. for(int i=0;i<buttons.size();i++)
  661. buttons[i]->activate();
  662. if (slider)
  663. slider->activate();
  664. }
  665. void CInfoWindow::sliderMoved(int to)
  666. {
  667. /*slider->moveTo(to);
  668. if(!slider) return; //ignore spurious call when slider is being created
  669. */
  670. redraw();
  671. }
  672. void CInfoWindow::deactivate()
  673. {
  674. for (int i=0;i<components.size();i++)
  675. components[i]->deactivate();
  676. for(int i=0;i<buttons.size();i++)
  677. buttons[i]->deactivate();
  678. if (slider)
  679. slider->deactivate();
  680. }
  681. void CInfoWindow::showAll( SDL_Surface * to )
  682. {
  683. show(to);
  684. }
  685. void CInfoWindow::showYesNoDialog(const std::string & text, const std::vector<SComponent*> *components, const CFunctionList<void( ) > &onYes, const CFunctionList<void()> &onNo, bool DelComps, int player)
  686. {
  687. assert(!LOCPLINT || LOCPLINT->showingDialog->get());
  688. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  689. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  690. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  691. CInfoWindow * temp = new CInfoWindow(text, player, 0, components ? *components : std::vector<SComponent*>(), pom, DelComps);
  692. temp->delComps = DelComps;
  693. for(int i=0;i<onYes.funcs.size();i++)
  694. temp->buttons[0]->callback += onYes.funcs[i];
  695. for(int i=0;i<onNo.funcs.size();i++)
  696. temp->buttons[1]->callback += onNo.funcs[i];
  697. GH.pushInt(temp);
  698. }
  699. void CRClickPopup::clickRight(tribool down, bool previousState)
  700. {
  701. if(down)
  702. return;
  703. close();
  704. }
  705. void CRClickPopup::activate()
  706. {
  707. activateRClick();
  708. }
  709. void CRClickPopup::deactivate()
  710. {
  711. deactivateRClick();
  712. }
  713. void CRClickPopup::close()
  714. {
  715. GH.popIntTotally(this);
  716. }
  717. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  718. :free(Free),bitmap(Bitmap)
  719. {
  720. CGI->curh->hide();
  721. pos.x = x;
  722. pos.y = y;
  723. pos.h = bitmap->h;
  724. pos.w = bitmap->w;
  725. // Put the window back on screen if necessary
  726. amax(pos.x, 0);
  727. amax(pos.y, 0);
  728. amin(pos.x, conf.cc.resx - bitmap->w);
  729. amin(pos.y, conf.cc.resy - bitmap->h);
  730. }
  731. CInfoPopup::CInfoPopup(SDL_Surface *Bitmap, bool Free)
  732. {
  733. CGI->curh->hide();
  734. free=Free;
  735. bitmap=Bitmap;
  736. if(bitmap)
  737. {
  738. pos.x = screen->w/2 - bitmap->w/2;
  739. pos.y = screen->h/2 - bitmap->h/2;
  740. pos.h = bitmap->h;
  741. pos.w = bitmap->w;
  742. }
  743. }
  744. void CInfoPopup::close()
  745. {
  746. if(free)
  747. SDL_FreeSurface(bitmap);
  748. GH.popIntTotally(this);
  749. }
  750. void CInfoPopup::show(SDL_Surface * to)
  751. {
  752. blitAt(bitmap,pos.x,pos.y,to);
  753. }
  754. CInfoPopup::~CInfoPopup()
  755. {
  756. CGI->curh->show();
  757. }
  758. void SComponent::init(Etype Type, int Subtype, int Val)
  759. {
  760. std::ostringstream oss;
  761. switch (Type)
  762. {
  763. case artifact:
  764. description = CGI->arth->artifacts[Subtype].Description();
  765. subtitle = CGI->arth->artifacts[Subtype].Name();
  766. break;
  767. case primskill:
  768. oss << std::showpos << Val << " ";
  769. if(Subtype < 4)
  770. {
  771. description = CGI->generaltexth->arraytxt[2+Subtype];
  772. oss << CGI->generaltexth->primarySkillNames[Subtype];
  773. }
  774. else if(Subtype == 5) //spell points
  775. {
  776. description = CGI->generaltexth->allTexts[149];
  777. oss << CGI->generaltexth->allTexts[387];
  778. }
  779. else
  780. {
  781. tlog1 << "Wrong subtype=" << Subtype << std::endl;
  782. }
  783. subtitle = oss.str();
  784. break;
  785. case building:
  786. description = CGI->buildh->buildings[Subtype][Val]->Description();
  787. subtitle = CGI->buildh->buildings[Subtype][Val]->Name();
  788. break;
  789. case secskill44: case secskill:
  790. subtitle += CGI->generaltexth->levels[Val-1] + " " + CGI->generaltexth->skillName[Subtype];
  791. description = CGI->generaltexth->skillInfoTexts[Subtype][Val-1];
  792. break;
  793. case resource:
  794. description = CGI->generaltexth->allTexts[242];
  795. oss << Val;
  796. subtitle = oss.str();
  797. break;
  798. case spell:
  799. description = CGI->spellh->spells[Subtype].descriptions[Val];
  800. subtitle = CGI->spellh->spells[Subtype].name;
  801. break;
  802. case creature:
  803. subtitle = boost::lexical_cast<std::string>(Val) + " " + CGI->creh->creatures[Subtype].*(Val != 1 ? &CCreature::namePl : &CCreature::nameSing);
  804. break;
  805. case experience:
  806. description = CGI->generaltexth->allTexts[241];
  807. oss << Val ;
  808. if(Subtype && Val==1)
  809. {
  810. subtitle = CGI->generaltexth->allTexts[442];
  811. }
  812. else
  813. {
  814. subtitle = oss.str();
  815. }
  816. break;
  817. case hero:
  818. subtitle = description = CGI->heroh->heroes[Subtype]->name;
  819. break;
  820. case flag:
  821. subtitle = CGI->generaltexth->capColors[Subtype];
  822. break;
  823. }
  824. type = Type;
  825. subtype = Subtype;
  826. val = Val;
  827. SDL_Surface * temp = this->getImg();
  828. if(!temp)
  829. {
  830. tlog1 << "Error: cannot find graphic for component with id=" << type << " subid=" << subtype << " val=" << val << std::endl;
  831. return;
  832. }
  833. pos.w = temp->w;
  834. pos.h = temp->h;
  835. }
  836. SComponent::SComponent(Etype Type, int Subtype, int Val)
  837. {
  838. init(Type,Subtype,Val);
  839. }
  840. SComponent::SComponent(const Component &c)
  841. {
  842. if(c.id==5)
  843. init(experience,c.subtype,c.val);
  844. else if(c.id == Component::SPELL)
  845. init(spell,c.subtype,c.val);
  846. else
  847. init((Etype)c.id,c.subtype,c.val);
  848. if(c.id==2 && c.when==-1)
  849. subtitle += CGI->generaltexth->allTexts[3].substr(2,CGI->generaltexth->allTexts[3].length()-2);
  850. }
  851. void SComponent::show(SDL_Surface * to)
  852. {
  853. blitAt(getImg(),pos.x,pos.y,to);
  854. }
  855. SDL_Surface * SComponent::getImg()
  856. {
  857. switch (type)
  858. {
  859. case artifact:
  860. return graphics->artDefs->ourImages[subtype].bitmap;
  861. break;
  862. case primskill:
  863. return graphics->pskillsb->ourImages[subtype].bitmap;
  864. break;
  865. case secskill44:
  866. return graphics->abils44->ourImages[subtype*3 + 3 + val - 1].bitmap;
  867. break;
  868. case secskill:
  869. return graphics->abils82->ourImages[subtype*3 + 3 + val - 1].bitmap;
  870. break;
  871. case resource:
  872. return graphics->resources->ourImages[subtype].bitmap;
  873. break;
  874. case experience:
  875. return graphics->pskillsb->ourImages[4].bitmap;
  876. break;
  877. case morale:
  878. return graphics->morale82->ourImages[val+3].bitmap;
  879. break;
  880. case luck:
  881. return graphics->luck82->ourImages[val+3].bitmap;
  882. break;
  883. case spell:
  884. return graphics->spellscr->ourImages[subtype].bitmap;
  885. break;
  886. case creature:
  887. return graphics->bigImgs[subtype];
  888. break;
  889. case hero:
  890. return graphics->portraitLarge[subtype];
  891. case flag:
  892. return graphics->flags->ourImages[subtype].bitmap;
  893. }
  894. return NULL;
  895. }
  896. void SComponent::clickRight(tribool down, bool previousState)
  897. {
  898. if(description.size())
  899. LOCPLINT->adventureInt->handleRightClick(description,down,this);
  900. }
  901. void SComponent::activate()
  902. {
  903. activateRClick();
  904. }
  905. void SComponent::deactivate()
  906. {
  907. deactivateRClick();
  908. }
  909. void CSelectableComponent::clickLeft(tribool down, bool previousState)
  910. {
  911. if (down)
  912. {
  913. if(onSelect)
  914. onSelect();
  915. }
  916. }
  917. void CSelectableComponent::init(SDL_Surface * Border)
  918. {
  919. SDL_Surface * symb = SComponent::getImg();
  920. myBitmap = CSDL_Ext::newSurface(symb->w+2,symb->h+2,screen);
  921. SDL_SetColorKey(myBitmap,SDL_SRCCOLORKEY,SDL_MapRGB(myBitmap->format,0,255,255));
  922. blitAt(symb,1,1,myBitmap);
  923. if (Border) //use custom border
  924. {
  925. border = Border;
  926. customB = true;
  927. }
  928. else //we need to draw border
  929. {
  930. customB = false;
  931. border = CSDL_Ext::newSurface(symb->w+2,symb->h+2,screen);
  932. SDL_FillRect(border,NULL,0x00FFFF);
  933. for (int i=0;i<border->w;i++)
  934. {
  935. SDL_PutPixelWithoutRefresh(border,i,0,239,215,123);
  936. SDL_PutPixelWithoutRefresh(border,i,(border->h)-1,239,215,123);
  937. }
  938. for (int i=0;i<border->h;i++)
  939. {
  940. SDL_PutPixelWithoutRefresh(border,0,i,239,215,123);
  941. SDL_PutPixelWithoutRefresh(border,(border->w)-1,i,239,215,123);
  942. }
  943. SDL_SetColorKey(border,SDL_SRCCOLORKEY,SDL_MapRGB(border->format,0,255,255));
  944. }
  945. selected = false;
  946. }
  947. CSelectableComponent::CSelectableComponent(const Component &c, boost::function<void()> OnSelect, SDL_Surface * Border)
  948. :SComponent(c),onSelect(OnSelect)
  949. {
  950. init(Border);
  951. }
  952. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, boost::function<void()> OnSelect, SDL_Surface * Border)
  953. :SComponent(Type,Sub,Val),onSelect(OnSelect)
  954. {
  955. init(Border);
  956. }
  957. CSelectableComponent::~CSelectableComponent()
  958. {
  959. SDL_FreeSurface(myBitmap);
  960. if (!customB)
  961. SDL_FreeSurface(border);
  962. }
  963. void CSelectableComponent::activate()
  964. {
  965. activateKeys();
  966. SComponent::activate();
  967. activateLClick();
  968. }
  969. void CSelectableComponent::deactivate()
  970. {
  971. deactivateKeys();
  972. SComponent::deactivate();
  973. deactivateLClick();
  974. }
  975. SDL_Surface * CSelectableComponent::getImg()
  976. {
  977. return myBitmap;
  978. }
  979. void CSelectableComponent::select(bool on)
  980. {
  981. if(on != selected)
  982. {
  983. SDL_FillRect(myBitmap,NULL,0x000000);
  984. blitAt(SComponent::getImg(),1,1,myBitmap);
  985. if (on)
  986. {
  987. blitAt(border,0,0,myBitmap);
  988. }
  989. selected = on;
  990. return;
  991. }
  992. else
  993. {
  994. return;
  995. }
  996. }
  997. void CSelectableComponent::show(SDL_Surface * to)
  998. {
  999. blitAt(myBitmap,pos.x,pos.y,to);
  1000. printAtMiddleWB(subtitle,pos.x+pos.w/2,pos.y+pos.h+25,FONT_SMALL,12,zwykly,to);
  1001. }
  1002. void CSimpleWindow::show(SDL_Surface * to)
  1003. {
  1004. blitAt(bitmap,pos.x,pos.y,to);
  1005. }
  1006. CSimpleWindow::~CSimpleWindow()
  1007. {
  1008. if (bitmap)
  1009. {
  1010. SDL_FreeSurface(bitmap);
  1011. bitmap=NULL;
  1012. }
  1013. }
  1014. void CSelWindow::selectionChange(unsigned to)
  1015. {
  1016. for (unsigned i=0;i<components.size();i++)
  1017. {
  1018. CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
  1019. if (!pom)
  1020. continue;
  1021. pom->select(i==to);
  1022. blitAt(pom->getImg(),pom->pos.x-pos.x,pom->pos.y-pos.y,bitmap);
  1023. }
  1024. }
  1025. 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)
  1026. {
  1027. ID = askID;
  1028. for(int i=0;i<Buttons.size();i++)
  1029. {
  1030. buttons.push_back(new AdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  1031. if(!i && askID >= 0)
  1032. buttons.back()->callback += boost::bind(&CSelWindow::madeChoice,this);
  1033. buttons[i]->callback += boost::bind(&CInfoWindow::close,this); //each button will close the window apart from call-defined actions
  1034. }
  1035. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  1036. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  1037. if(buttons.size() > 1 && askID >= 0) //cancel button functionality
  1038. buttons.back()->callback += boost::bind(&ICallback::selectionMade,LOCPLINT->cb,0,askID);
  1039. for(int i=0;i<comps.size();i++)
  1040. {
  1041. components.push_back(comps[i]);
  1042. comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
  1043. if(i<9)
  1044. comps[i]->assignedKeys.insert(SDLK_1+i);
  1045. }
  1046. CMessage::drawIWindow(this,text,player,charperline);
  1047. }
  1048. void CSelWindow::madeChoice()
  1049. {
  1050. if(ID < 0)
  1051. return;
  1052. int ret = -1;
  1053. for (int i=0;i<components.size();i++)
  1054. {
  1055. if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
  1056. {
  1057. ret = i;
  1058. }
  1059. }
  1060. LOCPLINT->cb->selectionMade(ret+1,ID);
  1061. }
  1062. CStatusBar::CStatusBar(int x, int y, std::string name, int maxw)
  1063. {
  1064. bg=BitmapHandler::loadBitmap(name);
  1065. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  1066. pos.x += x;
  1067. pos.y += y;
  1068. if(maxw >= 0)
  1069. pos.w = std::min(bg->w,maxw);
  1070. else
  1071. pos.w=bg->w;
  1072. pos.h=bg->h;
  1073. middlex=(pos.w/2)+pos.x;
  1074. middley=(bg->h/2)+pos.y;
  1075. }
  1076. CStatusBar::~CStatusBar()
  1077. {
  1078. SDL_FreeSurface(bg);
  1079. }
  1080. void CStatusBar::clear()
  1081. {
  1082. if(LOCPLINT->cingconsole->enteredText == "") //for appropriate support for in-game console
  1083. {
  1084. current="";
  1085. show(screen);
  1086. }
  1087. }
  1088. void CStatusBar::print(const std::string & text)
  1089. {
  1090. if(LOCPLINT->cingconsole->enteredText == "" || text == LOCPLINT->cingconsole->enteredText) //for appropriate support for in-game console
  1091. {
  1092. current=text;
  1093. show(GH.topInt()==LOCPLINT->adventureInt ? screen : screen2); //if there are now windows opened, update statusbar on screen, else to cache surface
  1094. }
  1095. }
  1096. void CStatusBar::show(SDL_Surface * to)
  1097. {
  1098. SDL_Rect pom = genRect(pos.h,pos.w,pos.x,pos.y);
  1099. SDL_BlitSurface(bg,&genRect(pos.h,pos.w,0,0),to,&pom);
  1100. printAtMiddle(current,middlex,middley,FONT_SMALL,zwykly,to);
  1101. }
  1102. std::string CStatusBar::getCurrent()
  1103. {
  1104. return current;
  1105. }
  1106. void CList::activate()
  1107. {
  1108. activateLClick();
  1109. activateRClick();
  1110. activateHover();
  1111. activateKeys();
  1112. activateMouseMove();
  1113. };
  1114. void CList::deactivate()
  1115. {
  1116. deactivateLClick();
  1117. deactivateRClick();
  1118. deactivateHover();
  1119. deactivateKeys();
  1120. deactivateMouseMove();
  1121. };
  1122. void CList::clickLeft(tribool down, bool previousState)
  1123. {
  1124. };
  1125. CList::CList(int Size)
  1126. :SIZE(Size)
  1127. {
  1128. }
  1129. void CList::fixPos()
  1130. {
  1131. int oldFrom = from;
  1132. if(selected < 0) //no selection, do nothing
  1133. return;
  1134. if(selected < from) //scroll up
  1135. from = selected;
  1136. else if(from + SIZE <= selected)
  1137. from = selected - SIZE + 1;
  1138. amin(from, size() - SIZE);
  1139. amax(from, 0);
  1140. }
  1141. CHeroList::CHeroList(int Size)
  1142. :CList(Size), heroes(LOCPLINT->wanderingHeroes)
  1143. {
  1144. arrup = CDefHandler::giveDef(conf.go()->ac.hlistAU);
  1145. arrdo = CDefHandler::giveDef(conf.go()->ac.hlistAD);
  1146. mobile = CDefHandler::giveDef(conf.go()->ac.hlistMB);
  1147. mana = CDefHandler::giveDef(conf.go()->ac.hlistMN);
  1148. empty = BitmapHandler::loadBitmap("HPSXXX.bmp");
  1149. selection = BitmapHandler::loadBitmap("HPSYYY.bmp");
  1150. SDL_SetColorKey(selection,SDL_SRCCOLORKEY,SDL_MapRGB(selection->format,0,255,255));
  1151. pos = genRect(32*SIZE+arrup->height+arrdo->height, std::max(arrup->width,arrdo->width), conf.go()->ac.hlistX, conf.go()->ac.hlistY);
  1152. arrupp = genRect(arrup->height, arrup->width, pos.x, pos.y);
  1153. arrdop = genRect(arrdo->height, arrdo->width, pos.x, pos.y+32*SIZE+arrup->height);
  1154. //32px per hero
  1155. posmobx = pos.x+1;
  1156. posmoby = pos.y+arrup->height+1;
  1157. posporx = pos.x+mobile->width+2;
  1158. pospory = pos.y+arrup->height;
  1159. posmanx = pos.x+1+50+mobile->width;
  1160. posmany = pos.y+arrup->height+1;
  1161. from = 0;
  1162. selected = -1;
  1163. pressed = indeterminate;
  1164. }
  1165. void CHeroList::init()
  1166. {
  1167. int w = pos.w+1, h = pos.h+4;
  1168. bg = CSDL_Ext::newSurface(w,h,screen);
  1169. SDL_BlitSurface(LOCPLINT->adventureInt->bg,&genRect(w,h,pos.x,pos.y),bg,&genRect(w,h,0,0));
  1170. }
  1171. void CHeroList::genList()
  1172. {
  1173. //int howMany = LOCPLINT->cb->howManyHeroes();
  1174. //for (int i=0;i<howMany;i++)
  1175. //{
  1176. // const CGHeroInstance * h = LOCPLINT->cb->getHeroInfo(i,0);
  1177. // if(!h->inTownGarrison)
  1178. // items.push_back(std::pair<const CGHeroInstance *,CPath *>(h,NULL));
  1179. //}
  1180. }
  1181. void CHeroList::select(int which)
  1182. {
  1183. if (which<0)
  1184. {
  1185. selected = which;
  1186. LOCPLINT->adventureInt->selection = NULL;
  1187. LOCPLINT->adventureInt->terrain.currentPath = NULL;
  1188. draw(screen);
  1189. LOCPLINT->adventureInt->infoBar.draw(screen);
  1190. }
  1191. if (which>=heroes.size())
  1192. return;
  1193. selected = which;
  1194. LOCPLINT->adventureInt->select(heroes[which]);
  1195. }
  1196. void CHeroList::clickLeft(tribool down, bool previousState)
  1197. {
  1198. if (down)
  1199. {
  1200. /***************************ARROWS*****************************************/
  1201. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1202. {
  1203. if(from>0)
  1204. {
  1205. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  1206. pressed = true;
  1207. }
  1208. return;
  1209. }
  1210. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1211. {
  1212. if(heroes.size()-from>SIZE)
  1213. {
  1214. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  1215. pressed = false;
  1216. }
  1217. return;
  1218. }
  1219. /***************************HEROES*****************************************/
  1220. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1221. hx-=pos.x;
  1222. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1223. int ny = hy/32;
  1224. if (ny>=SIZE || ny<0)
  1225. return;
  1226. if ( (ny+from)==selected && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  1227. LOCPLINT->openHeroWindow(heroes[selected]);//print hero screen
  1228. select(ny+from);
  1229. }
  1230. else
  1231. {
  1232. if (indeterminate(pressed))
  1233. return;
  1234. if (pressed) //up
  1235. {
  1236. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  1237. pressed = indeterminate;
  1238. if (!down)
  1239. {
  1240. from--;
  1241. if (from<0)
  1242. from=0;
  1243. draw(screen);
  1244. }
  1245. }
  1246. else if (!pressed) //down
  1247. {
  1248. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  1249. pressed = indeterminate;
  1250. if (!down)
  1251. {
  1252. from++;
  1253. //if (from<items.size()-5)
  1254. // from=items.size()-5;
  1255. draw(screen);
  1256. }
  1257. }
  1258. else
  1259. throw 0;
  1260. }
  1261. }
  1262. void CHeroList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  1263. {
  1264. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1265. {
  1266. if (from>0)
  1267. LOCPLINT->adventureInt->statusbar.print(CGI->generaltexth->zelp[303].first);
  1268. else
  1269. LOCPLINT->adventureInt->statusbar.clear();
  1270. return;
  1271. }
  1272. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1273. {
  1274. if ((heroes.size()-from) > SIZE)
  1275. LOCPLINT->adventureInt->statusbar.print(CGI->generaltexth->zelp[304].first);
  1276. else
  1277. LOCPLINT->adventureInt->statusbar.clear();
  1278. return;
  1279. }
  1280. //if not buttons then heroes
  1281. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1282. hx-=pos.x;
  1283. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1284. int ny = hy/32;
  1285. if ((ny>SIZE || ny<0) || (from+ny>=heroes.size()))
  1286. {
  1287. LOCPLINT->adventureInt->statusbar.clear();
  1288. return;
  1289. }
  1290. std::vector<std::string> temp;
  1291. temp.push_back(heroes[from+ny]->name);
  1292. temp.push_back(heroes[from+ny]->type->heroClass->name);
  1293. LOCPLINT->adventureInt->statusbar.print( processStr(CGI->generaltexth->allTexts[15],temp) );
  1294. //select(ny+from);
  1295. }
  1296. void CHeroList::clickRight(tribool down, bool previousState)
  1297. {
  1298. if (down)
  1299. {
  1300. /***************************ARROWS*****************************************/
  1301. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y) && from>0)
  1302. {
  1303. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down,this);
  1304. }
  1305. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y) && (heroes.size()-from>5))
  1306. {
  1307. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down,this);
  1308. }
  1309. else
  1310. {
  1311. //if not buttons then heroes
  1312. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1313. hx-=pos.x;
  1314. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1315. int ny = hy/32;
  1316. if ((ny>SIZE || ny<0) || (from+ny>=heroes.size()))
  1317. {
  1318. return;
  1319. }
  1320. //show popup
  1321. CInfoPopup * ip = new CInfoPopup(graphics->heroWins[heroes[from+ny]->subID],
  1322. GH.current->motion.x-graphics->heroWins[heroes[from+ny]->subID]->w,
  1323. GH.current->motion.y-graphics->heroWins[heroes[from+ny]->subID]->h,
  1324. false);
  1325. GH.pushInt(ip);
  1326. }
  1327. }
  1328. else
  1329. {
  1330. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down,this);
  1331. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down,this);
  1332. }
  1333. }
  1334. void CHeroList::hover (bool on)
  1335. {
  1336. }
  1337. void CHeroList::keyPressed (const SDL_KeyboardEvent & key)
  1338. {
  1339. }
  1340. void CHeroList::updateHList(const CGHeroInstance *toRemove)
  1341. {
  1342. if(toRemove) //remove specific hero
  1343. heroes -= toRemove;
  1344. else
  1345. LOCPLINT->recreateWanderingHeroes();
  1346. if(selected >= heroes.size())
  1347. select(heroes.size()-1);
  1348. if(toRemove)
  1349. {
  1350. if(heroes.size() == 0)
  1351. LOCPLINT->adventureInt->townList.select(0);
  1352. else
  1353. select(selected);
  1354. }
  1355. }
  1356. void CHeroList::updateMove(const CGHeroInstance* which) //draws move points bar
  1357. {
  1358. int ser = -1;
  1359. for(int i=0; i<heroes.size() && ser<0; i++)
  1360. if(heroes[i]->subID == which->subID)
  1361. ser = i;
  1362. ser -= from;
  1363. if(ser<0 || ser >= SIZE) return;
  1364. int pom = std::min((which->movement)/100,(si32)mobile->ourImages.size()-1);
  1365. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+ser*32); //move point
  1366. }
  1367. void CHeroList::draw(SDL_Surface * to)
  1368. {
  1369. for (int iT=0+from;iT<SIZE+from;iT++)
  1370. {
  1371. int i = iT-from;
  1372. if (iT>=heroes.size())
  1373. {
  1374. blitAt(mobile->ourImages[0].bitmap,posmobx,posmoby+i*32,to);
  1375. blitAt(mana->ourImages[0].bitmap,posmanx,posmany+i*32,to);
  1376. blitAt(empty,posporx,pospory+i*32,to);
  1377. continue;
  1378. }
  1379. const CGHeroInstance *cur = heroes[iT];
  1380. int pom = cur->movement / 100;
  1381. if (pom>25) pom=25;
  1382. if (pom<0) pom=0;
  1383. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+i*32,to); //move point
  1384. pom = cur->mana / 5;
  1385. if (pom>25) pom=25;
  1386. if (pom<0) pom=0;
  1387. blitAt(mana->ourImages[pom].bitmap,posmanx,posmany+i*32,to); //mana
  1388. SDL_Surface * temp = graphics->portraitSmall[cur->portrait];
  1389. blitAt(temp,posporx,pospory+i*32,to);
  1390. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  1391. {
  1392. blitAt(selection,posporx,pospory+i*32,to);
  1393. }
  1394. //TODO: support for custom portraits
  1395. }
  1396. if (from>0)
  1397. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1398. else
  1399. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1400. if (heroes.size()-from > SIZE)
  1401. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1402. else
  1403. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1404. }
  1405. int CHeroList::getPosOfHero(const CGHeroInstance* h)
  1406. {
  1407. return vstd::findPos(heroes, h, std::equal_to<const CGHeroInstance*>());
  1408. }
  1409. void CHeroList::show( SDL_Surface * to )
  1410. {
  1411. }
  1412. int CHeroList::size()
  1413. {
  1414. return heroes.size();
  1415. }
  1416. CTownList::~CTownList()
  1417. {
  1418. delete arrup;
  1419. delete arrdo;
  1420. }
  1421. CTownList::CTownList(int Size, int x, int y, std::string arrupg, std::string arrdog)
  1422. :CList(Size)
  1423. {
  1424. arrup = CDefHandler::giveDef(arrupg);
  1425. arrdo = CDefHandler::giveDef(arrdog);
  1426. pos.x = x;
  1427. pos.y = y;
  1428. pos.w = std::max(arrdo->width, arrup->width);
  1429. pos.h = arrdo->height + arrup->height + Size*32;
  1430. arrupp.x=x;
  1431. arrupp.y=y;
  1432. arrupp.w=arrup->width;
  1433. arrupp.h=arrup->height;
  1434. arrdop.x=x;
  1435. arrdop.y=y+arrup->height+32*SIZE;
  1436. arrdop.w=arrdo->width;
  1437. arrdop.h=arrdo->height;
  1438. posporx = arrdop.x;
  1439. pospory = arrupp.y + arrupp.h;
  1440. pressed = indeterminate;
  1441. from = 0;
  1442. selected = -1;
  1443. }
  1444. void CTownList::genList()
  1445. {
  1446. items.clear();
  1447. int howMany = LOCPLINT->cb->howManyTowns();
  1448. for (int i=0;i<howMany;i++)
  1449. {
  1450. items.push_back(LOCPLINT->cb->getTownInfo(i,0));
  1451. }
  1452. }
  1453. void CTownList::select(int which)
  1454. {
  1455. if (which>=items.size())
  1456. return;
  1457. selected = which;
  1458. if(!fun.empty())
  1459. fun();
  1460. }
  1461. void CTownList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  1462. {
  1463. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1464. {
  1465. if (from>0)
  1466. GH.statusbar->print(CGI->generaltexth->zelp[306].first);
  1467. else
  1468. GH.statusbar->clear();
  1469. return;
  1470. }
  1471. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1472. {
  1473. if ((items.size()-from) > SIZE)
  1474. GH.statusbar->print(CGI->generaltexth->zelp[307].first);
  1475. else
  1476. GH.statusbar->clear();
  1477. return;
  1478. }
  1479. //if not buttons then towns
  1480. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1481. hx-=pos.x;
  1482. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1483. int ny = hy/32;
  1484. if ((ny>=SIZE || ny<0) || (from+ny>=items.size()))
  1485. {
  1486. GH.statusbar->clear();
  1487. return;
  1488. };
  1489. std::string temp = CGI->generaltexth->tcommands[4];
  1490. boost::algorithm::replace_first(temp,"%s",items[from+ny]->name);
  1491. temp += ", "+items[from+ny]->town->Name();
  1492. GH.statusbar->print(temp);
  1493. }
  1494. void CTownList::clickLeft(tribool down, bool previousState)
  1495. {
  1496. if (down)
  1497. {
  1498. /***************************ARROWS*****************************************/
  1499. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1500. {
  1501. if(from>0)
  1502. {
  1503. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y,screenBuf);
  1504. pressed = true;
  1505. }
  1506. return;
  1507. }
  1508. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1509. {
  1510. if(items.size()-from > SIZE)
  1511. {
  1512. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y,screenBuf);
  1513. pressed = false;
  1514. }
  1515. return;
  1516. }
  1517. /***************************TOWNS*****************************************/
  1518. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1519. hx-=pos.x;
  1520. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1521. int ny = hy/32;
  1522. if (ny>=SIZE || ny<0)
  1523. return;
  1524. if(GH.topInt() == LOCPLINT->adventureInt
  1525. && (ny+from)==selected
  1526. && LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE
  1527. )
  1528. LOCPLINT->openTownWindow(items[selected]);//print town screen
  1529. else
  1530. select(ny+from);
  1531. }
  1532. else
  1533. {
  1534. if (indeterminate(pressed))
  1535. return;
  1536. if (pressed) //up
  1537. {
  1538. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,screenBuf);
  1539. pressed = indeterminate;
  1540. if (!down)
  1541. {
  1542. from--;
  1543. if (from<0)
  1544. from=0;
  1545. draw(screenBuf);
  1546. }
  1547. }
  1548. else if (!pressed) //down
  1549. {
  1550. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,screenBuf);
  1551. pressed = indeterminate;
  1552. if (!down)
  1553. {
  1554. from++;
  1555. //if (from<items.size()-5)
  1556. // from=items.size()-5;
  1557. draw(screenBuf);
  1558. }
  1559. }
  1560. else
  1561. throw 0;
  1562. }
  1563. }
  1564. void CTownList::clickRight(tribool down, bool previousState)
  1565. {
  1566. if (down)
  1567. {
  1568. /***************************ARROWS*****************************************/
  1569. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y) && from>0)
  1570. {
  1571. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down,this);
  1572. }
  1573. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y) && (items.size()-from>5))
  1574. {
  1575. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down,this);
  1576. }
  1577. //if not buttons then towns
  1578. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1579. hx-=pos.x;
  1580. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1581. int ny = hy/32;
  1582. if ((ny>=SIZE || ny<0) || (from+ny>=items.size()))
  1583. {
  1584. return;
  1585. }
  1586. //show popup
  1587. CInfoPopup * ip = new CInfoPopup(
  1588. graphics->townWins[items[from+ny]->id],
  1589. GH.current->motion.x-graphics->townWins[items[from+ny]->id]->w,
  1590. GH.current->motion.y-graphics->townWins[items[from+ny]->id]->h,
  1591. false);
  1592. GH.pushInt(ip);
  1593. }
  1594. else
  1595. {
  1596. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down,this);
  1597. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down,this);
  1598. }
  1599. }
  1600. void CTownList::hover (bool on)
  1601. {
  1602. }
  1603. void CTownList::keyPressed (const SDL_KeyboardEvent & key)
  1604. {
  1605. }
  1606. void CTownList::draw(SDL_Surface * to)
  1607. {
  1608. for (int iT=0+from;iT<SIZE+from;iT++)
  1609. {
  1610. int i = iT-from;
  1611. if (iT>=items.size())
  1612. {
  1613. blitAt(graphics->getPic(-1),posporx,pospory+i*32,to);
  1614. continue;
  1615. }
  1616. blitAt(graphics->getPic(items[iT]->subID,items[iT]->hasFort(),items[iT]->builded),posporx,pospory+i*32,to);
  1617. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE))
  1618. {
  1619. blitAt(graphics->getPic(-2),posporx,pospory+i*32,to);
  1620. }
  1621. }
  1622. if (from>0)
  1623. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1624. else
  1625. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1626. if (items.size()-from>SIZE)
  1627. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1628. else
  1629. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1630. }
  1631. void CTownList::show( SDL_Surface * to )
  1632. {
  1633. }
  1634. int CTownList::size()
  1635. {
  1636. return items.size();
  1637. }
  1638. CCreaturePic::CCreaturePic(const CCreature *cre, bool Big)
  1639. :c(cre),big(Big)
  1640. {
  1641. anim = new CCreatureAnimation(cre->animDefName);
  1642. }
  1643. CCreaturePic::~CCreaturePic()
  1644. {
  1645. delete anim;
  1646. }
  1647. int CCreaturePic::blitPic(SDL_Surface *to, int x, int y, bool nextFrame)
  1648. {
  1649. SDL_Rect dst;
  1650. if(big)
  1651. {
  1652. blitAt(graphics->backgrounds[c->faction],x,y,to);//curx-50,pos.y+130-65);
  1653. dst = genRect(130,100,x,y);
  1654. }
  1655. else
  1656. {
  1657. blitAt(graphics->backgroundsm[c->faction],x,y,to);//curx-50,pos.y+130-65);
  1658. dst = genRect(120,100,x,y);
  1659. }
  1660. if(c->isDoubleWide())
  1661. x-=15;
  1662. return anim->nextFrameMiddle(to,x+78,y+(big ? 55 : 45),true,0,nextFrame,false,false,&dst);
  1663. }
  1664. SDL_Surface * CCreaturePic::getPic(bool nextFrame)
  1665. {
  1666. //TODO: write
  1667. return NULL;
  1668. }
  1669. void CRecruitmentWindow::close()
  1670. {
  1671. GH.popIntTotally(this);
  1672. }
  1673. void CRecruitmentWindow::Max()
  1674. {
  1675. slider->moveTo(slider->amount);
  1676. }
  1677. void CRecruitmentWindow::Buy()
  1678. {
  1679. int crid = creatures[which].ID,
  1680. dstslot = dst->army.getSlotFor(crid);
  1681. if(dstslot < 0) //no available slot
  1682. {
  1683. std::string txt;
  1684. if(dst->ID == HEROI_TYPE)
  1685. {
  1686. txt = CGI->generaltexth->allTexts[425]; //The %s would join your hero, but there aren't enough provisions to support them.
  1687. boost::algorithm::replace_first(txt, "%s", slider->value > 1 ? CGI->creh->creatures[crid].namePl : CGI->creh->creatures[crid].nameSing);
  1688. }
  1689. else
  1690. {
  1691. txt = CGI->generaltexth->allTexts[17]; //There is no room in the garrison for this army.
  1692. }
  1693. LOCPLINT->showInfoDialog(txt);
  1694. return;
  1695. }
  1696. recruit(crid, slider->value);
  1697. if(level >= 0)
  1698. close();
  1699. else
  1700. slider->moveTo(0);
  1701. }
  1702. void CRecruitmentWindow::Cancel()
  1703. {
  1704. close();
  1705. }
  1706. void CRecruitmentWindow::sliderMoved(int to)
  1707. {
  1708. buy->block(!to);
  1709. }
  1710. void CRecruitmentWindow::clickLeft(tribool down, bool previousState)
  1711. {
  1712. int curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1713. for(int i=0;i<creatures.size();i++)
  1714. {
  1715. const int sCREATURE_WIDTH = CREATURE_WIDTH; // gcc -O0 workaround
  1716. if(isItIn(&genRect(132,sCREATURE_WIDTH,pos.x+curx,pos.y+64),GH.current->motion.x,GH.current->motion.y))
  1717. {
  1718. which = i;
  1719. int newAmount = std::min(amounts[i],creatures[i].amount);
  1720. slider->setAmount(newAmount);
  1721. if(slider->value > newAmount)
  1722. slider->moveTo(newAmount);
  1723. else
  1724. slider->moveTo(slider->value);
  1725. curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1726. for(int j=0;j<creatures.size();j++)
  1727. {
  1728. if(which==j)
  1729. drawBorder(bitmap,curx,64,CREATURE_WIDTH,132,int3(255,0,0));
  1730. else
  1731. drawBorder(bitmap,curx,64,CREATURE_WIDTH,132,int3(239,215,123));
  1732. curx += TOTAL_CREATURE_WIDTH;
  1733. }
  1734. break;
  1735. }
  1736. curx += TOTAL_CREATURE_WIDTH;
  1737. }
  1738. }
  1739. void CRecruitmentWindow::clickRight(tribool down, bool previousState)
  1740. {
  1741. if(down)
  1742. {
  1743. int curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1744. for(int i=0;i<creatures.size();i++)
  1745. {
  1746. const int sCREATURE_WIDTH = CREATURE_WIDTH; // gcc -O0 workaround
  1747. if(isItIn(&genRect(132,sCREATURE_WIDTH,pos.x+curx,pos.y+64),GH.current->motion.x,GH.current->motion.y))
  1748. {
  1749. CCreInfoWindow *popup = new CCreInfoWindow(creatures[i].ID, 0, 0, NULL, NULL, NULL, NULL);
  1750. GH.pushInt(popup);
  1751. break;
  1752. }
  1753. curx += TOTAL_CREATURE_WIDTH;
  1754. }
  1755. }
  1756. }
  1757. void CRecruitmentWindow::activate()
  1758. {
  1759. activateLClick();
  1760. activateRClick();
  1761. buy->activate();
  1762. max->activate();
  1763. cancel->activate();
  1764. slider->activate();
  1765. GH.statusbar = bar;
  1766. }
  1767. void CRecruitmentWindow::deactivate()
  1768. {
  1769. deactivateLClick();
  1770. deactivateRClick();
  1771. buy->deactivate();
  1772. max->deactivate();
  1773. cancel->deactivate();
  1774. slider->deactivate();
  1775. }
  1776. void CRecruitmentWindow::show(SDL_Surface * to)
  1777. {
  1778. static char animCounter=0; //animation counter - for determining appropriate animation frame to be shown
  1779. blitAt(bitmap,pos.x,pos.y,to);
  1780. buy->show(to);
  1781. max->show(to);
  1782. cancel->show(to);
  1783. slider->show(to);
  1784. char pom[15];
  1785. SDL_itoa(creatures[which].amount-slider->value,pom,10); //available
  1786. printAtMiddle(pom,pos.x+205,pos.y+254,FONT_SMALL,zwykly,to);
  1787. SDL_itoa(slider->value,pom,10); //recruit
  1788. printAtMiddle(pom,pos.x+279,pos.y+254,FONT_SMALL,zwykly,to);
  1789. printAtMiddle(CGI->generaltexth->allTexts[16] + " " + CGI->creh->creatures[creatures[which].ID].namePl,pos.x+243,pos.y+32,FONT_BIG,tytulowy,to); //eg "Recruit Dragon flies"
  1790. int curx = pos.x+115-creatures[which].res.size()*16;
  1791. for(int i=0;i<creatures[which].res.size();i++)
  1792. {
  1793. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx,pos.y+243,to);
  1794. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx+258,pos.y+243,to);
  1795. SDL_itoa(creatures[which].res[i].second,pom,10);
  1796. printAtMiddle(pom,curx+14,pos.y+288,FONT_SMALL,zwykly,to);
  1797. SDL_itoa(creatures[which].res[i].second * slider->value,pom,10);
  1798. printAtMiddle(pom,curx+12+258,pos.y+286,FONT_SMALL,zwykly,to);
  1799. curx+=32;
  1800. }
  1801. curx = pos.x + 192 + CREATURE_WIDTH - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1802. for(int i=0; i<creatures.size(); ++i)
  1803. {
  1804. creatures[i].pic->blitPic(to, curx-50, pos.y+130-65, !(animCounter%4));
  1805. curx += TOTAL_CREATURE_WIDTH;
  1806. }
  1807. ++animCounter;
  1808. bar->show(to);
  1809. }
  1810. CRecruitmentWindow::CRecruitmentWindow(const CGDwelling *Dwelling, int Level, const CArmedInstance *Dst, const boost::function<void(int,int)> &Recruit)
  1811. :recruit(Recruit), dwelling(Dwelling), dst(Dst), level(Level)
  1812. {
  1813. which = 0;
  1814. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPRCRT.bmp");
  1815. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1816. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  1817. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1818. SDL_FreeSurface(hhlp);
  1819. pos.x = screen->w/2 - bitmap->w/2;
  1820. pos.y = screen->h/2 - bitmap->h/2;
  1821. pos.w = bitmap->w;
  1822. pos.h = bitmap->h;
  1823. bar = new CStatusBar(pos.x+8, pos.y+370, "APHLFTRT.bmp", 471);
  1824. max = new AdventureMapButton(CGI->generaltexth->zelp[553],boost::bind(&CRecruitmentWindow::Max,this),pos.x+134,pos.y+313,"IRCBTNS.DEF",SDLK_m);
  1825. buy = new AdventureMapButton(CGI->generaltexth->zelp[554],boost::bind(&CRecruitmentWindow::Buy,this),pos.x+212,pos.y+313,"IBY6432.DEF",SDLK_RETURN);
  1826. cancel = new AdventureMapButton(CGI->generaltexth->zelp[555],boost::bind(&CRecruitmentWindow::Cancel,this),pos.x+290,pos.y+313,"ICN6432.DEF",SDLK_ESCAPE);
  1827. slider = new CSlider(pos.x+176,pos.y+279,135,boost::bind(&CRecruitmentWindow::sliderMoved,this, _1),0,0,0,true);
  1828. initCres();
  1829. printAtMiddle(CGI->generaltexth->allTexts[346],113,233,FONT_SMALL,zwykly,bitmap); //cost per troop t
  1830. printAtMiddle(CGI->generaltexth->allTexts[465],205,233,FONT_SMALL,zwykly,bitmap); //available t
  1831. printAtMiddle(CGI->generaltexth->allTexts[16],279,233,FONT_SMALL,zwykly,bitmap); //recruit t
  1832. printAtMiddle(CGI->generaltexth->allTexts[466],373,233,FONT_SMALL,zwykly,bitmap); //total cost t
  1833. drawBorder(bitmap,172,222,67,42,int3(239,215,123));
  1834. drawBorder(bitmap,246,222,67,42,int3(239,215,123));
  1835. drawBorder(bitmap,64,222,99,76,int3(239,215,123));
  1836. drawBorder(bitmap,322,222,99,76,int3(239,215,123));
  1837. drawBorder(bitmap,133,312,66,34,int3(173,142,66));
  1838. drawBorder(bitmap,211,312,66,34,int3(173,142,66));
  1839. drawBorder(bitmap,289,312,66,34,int3(173,142,66));
  1840. //border for creatures
  1841. int curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1842. for(int i=0;i<creatures.size();i++)
  1843. {
  1844. creatures[i].pos.x = curx+1;
  1845. creatures[i].pos.y = 65;
  1846. creatures[i].pos.w = 100;
  1847. creatures[i].pos.h = 130;
  1848. if(which==i)
  1849. drawBorder(bitmap,curx,64,CREATURE_WIDTH,132,int3(255,0,0));
  1850. else
  1851. drawBorder(bitmap,curx,64,CREATURE_WIDTH,132,int3(239,215,123));
  1852. curx += TOTAL_CREATURE_WIDTH;
  1853. }
  1854. if(!creatures[0].amount || !amounts[0])
  1855. {
  1856. max->block(true);
  1857. slider->block(true);
  1858. }
  1859. //buy->block(true); //not needed, will be blocked by initing slider on 0
  1860. }
  1861. CRecruitmentWindow::~CRecruitmentWindow()
  1862. {
  1863. cleanCres();
  1864. delete max;
  1865. delete buy;
  1866. delete cancel;
  1867. SDL_FreeSurface(bitmap);
  1868. delete slider;
  1869. delete bar;
  1870. }
  1871. void CRecruitmentWindow::initCres()
  1872. {
  1873. cleanCres();
  1874. for(int i=0; i<dwelling->creatures.size(); i++)
  1875. {
  1876. if(level >= 0 && i != level)
  1877. continue;
  1878. for(int j = dwelling->creatures[i].second.size() - 1; j >= 0 ; j--)
  1879. {
  1880. creatures.resize(creatures.size()+1);
  1881. creinfo &cur = creatures.back();
  1882. cur.amount = dwelling->creatures[i].first;
  1883. cur.ID = dwelling->creatures[i].second[j];
  1884. const CCreature *cre = &CGI->creh->creatures[cur.ID];
  1885. cur.pic = new CCreaturePic(cre);
  1886. for(int k=0; k<cre->cost.size(); k++)
  1887. if(cre->cost[k])
  1888. cur.res.push_back(std::make_pair(k,cre->cost[k]));
  1889. amounts.push_back(cre->maxAmount(LOCPLINT->cb->getResourceAmount()));
  1890. }
  1891. }
  1892. slider->setAmount(std::min(amounts[which],creatures[which].amount));
  1893. }
  1894. void CRecruitmentWindow::cleanCres()
  1895. {
  1896. for(int i=0;i<creatures.size();i++)
  1897. {
  1898. delete creatures[i].pic;
  1899. }
  1900. creatures.clear();
  1901. amounts.clear();
  1902. }
  1903. CSplitWindow::CSplitWindow(int cid, int max, CGarrisonInt *Owner, int Last, int val)
  1904. {
  1905. last = Last;
  1906. which = 1;
  1907. c=cid;
  1908. slider = NULL;
  1909. gar = Owner;
  1910. SDL_Surface *hhlp = BitmapHandler::loadBitmap("GPUCRDIV.bmp");
  1911. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1912. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  1913. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1914. SDL_FreeSurface(hhlp);
  1915. pos.x = screen->w/2 - bitmap->w/2;
  1916. pos.y = screen->h/2 - bitmap->h/2;
  1917. pos.w = bitmap->w;
  1918. pos.h = bitmap->h;
  1919. ok = new AdventureMapButton("","",boost::bind(&CSplitWindow::split,this),pos.x+20,pos.y+263,"IOK6432.DEF",SDLK_RETURN);
  1920. cancel = new AdventureMapButton("","",boost::bind(&CSplitWindow::close,this),pos.x+214,pos.y+263,"ICN6432.DEF",SDLK_ESCAPE);
  1921. int sliderPositions = max - (last>=0) - (last==2);
  1922. slider = new CSlider(pos.x+21,pos.y+194,257,boost::bind(&CSplitWindow::sliderMoved,this,_1),0,sliderPositions,val,true);
  1923. a1 = max-val;
  1924. a2 = val;
  1925. anim = new CCreaturePic(&CGI->creh->creatures[cid]);
  1926. anim->anim->setType(1);
  1927. std::string title = CGI->generaltexth->allTexts[256];
  1928. boost::algorithm::replace_first(title,"%s",CGI->creh->creatures[cid].namePl);
  1929. printAtMiddle(title,150,34,FONT_BIG,tytulowy,bitmap);
  1930. }
  1931. CSplitWindow::~CSplitWindow() //d-tor
  1932. {
  1933. SDL_FreeSurface(bitmap);
  1934. delete ok;
  1935. delete cancel;
  1936. delete slider;
  1937. delete anim;
  1938. }
  1939. void CSplitWindow::activate()
  1940. {
  1941. activateLClick();
  1942. activateKeys();
  1943. ok->activate();
  1944. cancel->activate();
  1945. slider->activate();
  1946. }
  1947. void CSplitWindow::deactivate()
  1948. {
  1949. deactivateLClick();
  1950. deactivateKeys();
  1951. ok->deactivate();
  1952. cancel->deactivate();
  1953. slider->deactivate();
  1954. }
  1955. void CSplitWindow::split()
  1956. {
  1957. gar->splitStacks(a2);
  1958. close();
  1959. }
  1960. void CSplitWindow::close()
  1961. {
  1962. GH.popIntTotally(this);
  1963. }
  1964. void CSplitWindow::sliderMoved(int to)
  1965. {
  1966. int all = a1+a2;
  1967. a2 = to + (last==1 || last==2);
  1968. if(slider)
  1969. a1 = all - a2;
  1970. }
  1971. void CSplitWindow::show(SDL_Surface * to)
  1972. {
  1973. blitAt(bitmap,pos.x,pos.y,to);
  1974. ok->show(to);
  1975. cancel->show(to);
  1976. slider->show(to);
  1977. printAtMiddle(boost::lexical_cast<std::string>(a1) + (!which ? "_" : ""),pos.x+70,pos.y+237,FONT_BIG,zwykly,to);
  1978. printAtMiddle(boost::lexical_cast<std::string>(a2) + (which ? "_" : ""),pos.x+233,pos.y+237,FONT_BIG,zwykly,to);
  1979. anim->blitPic(to,pos.x+20,pos.y+54,false);
  1980. anim->blitPic(to,pos.x+177,pos.y+54,false);
  1981. }
  1982. void CSplitWindow::keyPressed (const SDL_KeyboardEvent & key)
  1983. {
  1984. SDLKey k = key.keysym.sym;
  1985. if (isNumKey(k)) //convert numpad number to normal digit
  1986. k = numToDigit(k);
  1987. if(key.state != SDL_PRESSED)
  1988. return;
  1989. int &cur = (which ? a2 : a1),
  1990. &sec = (which ? a1 : a2),
  1991. ncur = cur;
  1992. if (k == SDLK_BACKSPACE)
  1993. {
  1994. ncur /= 10;
  1995. }
  1996. else if(k == SDLK_TAB)
  1997. {
  1998. which = !which;
  1999. }
  2000. else
  2001. {
  2002. int number = k - SDLK_0;
  2003. if (number < 0 || number > 9) //not a number pressed
  2004. {
  2005. return;
  2006. }
  2007. ncur = cur*10 + number;
  2008. }
  2009. int delta = ncur - cur;
  2010. if(delta > sec)
  2011. {
  2012. cur += sec;
  2013. sec = 0;
  2014. }
  2015. slider->moveTo(which ? ncur : a1+a2-ncur);
  2016. }
  2017. void CSplitWindow::clickLeft(tribool down, bool previousState)
  2018. {
  2019. if(down)
  2020. {
  2021. Point click(GH.current->motion.x,GH.current->motion.y);
  2022. click = click - pos.topLeft();
  2023. if(Rect(19,216,105,40).isIn(click)) //left picture
  2024. which = 0;
  2025. else if(Rect(175,216,105,40).isIn(click)) //right picture
  2026. which = 1;
  2027. }
  2028. }
  2029. void CCreInfoWindow::show(SDL_Surface * to)
  2030. {
  2031. char pom[15];
  2032. blitAt(bitmap,pos.x,pos.y,to);
  2033. anim->blitPic(to,pos.x+21,pos.y+48,(type) && !(anf%4));
  2034. if(++anf==4)
  2035. anf=0;
  2036. if(count.size())
  2037. printTo(count.c_str(),pos.x+114,pos.y+174,FONT_TIMES,zwykly,to);
  2038. if(upgrade)
  2039. upgrade->show(to);
  2040. if(dismiss)
  2041. dismiss->show(to);
  2042. if(ok)
  2043. ok->show(to);
  2044. }
  2045. CCreInfoWindow::CCreInfoWindow(int Cid, int Type, int creatureCount, StackState *State, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  2046. : type(Type), dsm(Dsm), dismiss(0), upgrade(0), ok(0)
  2047. {
  2048. //active = false;
  2049. anf = 0;
  2050. c = &CGI->creh->creatures[Cid];
  2051. SDL_Surface *hhlp = BitmapHandler::loadBitmap("CRSTKPU.bmp");
  2052. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  2053. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  2054. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  2055. SDL_FreeSurface(hhlp);
  2056. pos.x = screen->w/2 - bitmap->w/2;
  2057. pos.y = screen->h/2 - bitmap->h/2;
  2058. pos.w = bitmap->w;
  2059. pos.h = bitmap->h;
  2060. anim = new CCreaturePic(c);
  2061. if(!type) anim->anim->setType(2);
  2062. char pom[75];int hlp=0;
  2063. if(creatureCount)
  2064. {
  2065. SDL_itoa(creatureCount,pom,10);
  2066. count = pom;
  2067. }
  2068. printAtMiddle(c->namePl,149,30,FONT_SMALL,tytulowy,bitmap); //creature name
  2069. //atttack
  2070. printAt(CGI->generaltexth->primarySkillNames[0],155,48,FONT_SMALL,zwykly,bitmap);
  2071. SDL_itoa(c->attack,pom,10);
  2072. if(State && State->attackBonus)
  2073. {
  2074. int hlp;
  2075. if(c->attack > 0)
  2076. hlp = log10f(c->attack)+2;
  2077. else
  2078. hlp = 2;
  2079. pom[hlp-1] = ' '; pom[hlp] = '(';
  2080. SDL_itoa(c->attack+State->attackBonus,pom+hlp+1,10);
  2081. hlp += 2+(int)log10f(State->attackBonus+c->attack);
  2082. pom[hlp] = ')'; pom[hlp+1] = '\0';
  2083. }
  2084. printTo(pom,276,61,FONT_SMALL,zwykly,bitmap);
  2085. //defense
  2086. printAt(CGI->generaltexth->primarySkillNames[1],155,67,FONT_SMALL,zwykly,bitmap);
  2087. SDL_itoa(c->defence,pom,10);
  2088. if(State && State->defenseBonus)
  2089. {
  2090. int hlp;
  2091. if(c->defence > 0)
  2092. hlp = log10f(c->defence)+2;
  2093. else
  2094. hlp = 2;
  2095. pom[hlp-1] = ' '; pom[hlp] = '(';
  2096. SDL_itoa(c->defence+State->defenseBonus,pom+hlp+1,10);
  2097. hlp += 2+(int)log10f(State->defenseBonus+c->defence);
  2098. pom[hlp] = ')'; pom[hlp+1] = '\0';
  2099. }
  2100. printTo(pom,276,80,FONT_SMALL,zwykly,bitmap);
  2101. //shots
  2102. if(c->shots)
  2103. {
  2104. printAt(CGI->generaltexth->allTexts[198], 155, 86, FONT_SMALL, zwykly, bitmap);
  2105. if(State && State->shotsLeft >= 0)
  2106. sprintf(pom,"%d (%d)", c->shots, State->shotsLeft);
  2107. else
  2108. SDL_itoa(c->shots, pom, 10);
  2109. printTo(pom, 276, 99, FONT_SMALL, zwykly, bitmap);
  2110. }
  2111. //damage
  2112. int dmgMin = c->damageMin * (State ? State->dmgMultiplier : 1);
  2113. int dmgMax = c->damageMax * (State ? State->dmgMultiplier : 1);
  2114. printAt(CGI->generaltexth->allTexts[199], 155, 105, FONT_SMALL, zwykly, bitmap);
  2115. SDL_itoa(dmgMin, pom, 10);
  2116. if(dmgMin > 0)
  2117. hlp = log10f(dmgMin) + 2;
  2118. else
  2119. hlp = 2;
  2120. pom[hlp-1]=' '; pom[hlp]='-'; pom[hlp+1]=' ';
  2121. SDL_itoa(dmgMax, pom+hlp+2, 10);
  2122. printTo(pom, 276, 118, FONT_SMALL, zwykly, bitmap);
  2123. //health
  2124. printAt(CGI->generaltexth->allTexts[388],155,124,FONT_SMALL,zwykly,bitmap);
  2125. if(State && State->healthBonus)
  2126. sprintf(pom,"%d (%d)",c->hitPoints, c->hitPoints + State->healthBonus);
  2127. else
  2128. SDL_itoa(c->hitPoints,pom,10);
  2129. printTo(pom,276,137,FONT_SMALL,zwykly,bitmap);
  2130. //remaining health
  2131. if(State && State->currentHealth)
  2132. {
  2133. printAt(CGI->generaltexth->allTexts[200],155,143,FONT_SMALL,zwykly,bitmap);
  2134. SDL_itoa(State->currentHealth,pom,10);
  2135. printTo(pom,276,156,FONT_SMALL,zwykly,bitmap);
  2136. }
  2137. //speed
  2138. printAt(CGI->generaltexth->zelp[441].first,155,162,FONT_SMALL,zwykly,bitmap);
  2139. SDL_itoa(c->speed,pom,10);
  2140. if(State && State->speedBonus)
  2141. {
  2142. int hlp;
  2143. if(c->speed > 0)
  2144. hlp = log10f(c->speed)+2;
  2145. else
  2146. hlp = 2;
  2147. pom[hlp-1] = ' '; pom[hlp] = '(';
  2148. SDL_itoa(c->speed + State->speedBonus, pom+hlp+1, 10);
  2149. hlp += 2+(int)log10f(c->speed + State->speedBonus);
  2150. pom[hlp] = ')'; pom[hlp+1] = '\0';
  2151. }
  2152. printTo(pom,276,175,FONT_SMALL,zwykly,bitmap);
  2153. //luck and morale
  2154. blitAt(graphics->morale42->ourImages[(State)?(State->morale+3):(3)].bitmap,24,189,bitmap);
  2155. blitAt(graphics->luck42->ourImages[(State)?(State->luck+3):(3)].bitmap,77,189,bitmap);
  2156. //print abilities text - if r-click popup
  2157. if(type)
  2158. {
  2159. if(Upg && ui)
  2160. {
  2161. bool enough = true;
  2162. for(std::set<std::pair<int,int> >::iterator i=ui->cost[0].begin(); i!=ui->cost[0].end(); i++) //calculate upgrade cost
  2163. {
  2164. if(LOCPLINT->cb->getResourceAmount(i->first) < i->second*creatureCount)
  2165. enough = false;
  2166. upgResCost.push_back(new SComponent(SComponent::resource,i->first,i->second*creatureCount));
  2167. }
  2168. if(enough)
  2169. {
  2170. CFunctionList<void()> fs[2];
  2171. fs[0] += Upg;
  2172. fs[0] += boost::bind(&CCreInfoWindow::close,this);
  2173. CFunctionList<void()> cfl;
  2174. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[207],boost::ref(upgResCost),fs[0],fs[1],false);
  2175. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,cfl,pos.x+76,pos.y+237,"IVIEWCR.DEF",SDLK_u);
  2176. }
  2177. else
  2178. {
  2179. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,boost::function<void()>(),pos.x+76,pos.y+237,"IVIEWCR.DEF");
  2180. upgrade->callback.funcs.clear();
  2181. upgrade->bitmapOffset = 2;
  2182. }
  2183. }
  2184. if(Dsm)
  2185. {
  2186. CFunctionList<void()> fs[2];
  2187. //on dismiss confirmed
  2188. fs[0] += Dsm; //dismiss
  2189. fs[0] += boost::bind(&CCreInfoWindow::close,this);//close this window
  2190. CFunctionList<void()> cfl;
  2191. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],std::vector<SComponent*>(),fs[0],fs[1],false);
  2192. dismiss = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,cfl,pos.x+21,pos.y+237,"IVIEWCR2.DEF",SDLK_d);
  2193. }
  2194. ok = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,boost::bind(&CCreInfoWindow::close,this),pos.x+216,pos.y+237,"IOKAY.DEF",SDLK_RETURN);
  2195. }
  2196. else
  2197. {
  2198. printAtWB(c->abilityText,17,231,FONT_SMALL,35,zwykly,bitmap);
  2199. }
  2200. //spell effects
  2201. if(State)
  2202. {
  2203. int printed=0; //how many effect pics have been printed
  2204. for(std::set<int>::const_iterator it = State->effects.begin(); it!=State->effects.end(); ++it)
  2205. {
  2206. blitAt(graphics->spellEffectsPics->ourImages[*it + 1].bitmap, 127 + 52 * printed, 186, bitmap);
  2207. ++printed;
  2208. if(printed >= 3)
  2209. {
  2210. break;
  2211. }
  2212. }
  2213. }
  2214. }
  2215. CCreInfoWindow::~CCreInfoWindow()
  2216. {
  2217. SDL_FreeSurface(bitmap);
  2218. delete anim;
  2219. delete upgrade;
  2220. delete ok;
  2221. delete dismiss;
  2222. for(int i=0; i<upgResCost.size();i++)
  2223. delete upgResCost[i];
  2224. }
  2225. void CCreInfoWindow::activate()
  2226. {
  2227. //if(active) return;
  2228. //active = true;
  2229. if(!type)
  2230. activateRClick();
  2231. if(ok)
  2232. ok->activate();
  2233. if(dismiss)
  2234. dismiss->activate();
  2235. if(upgrade)
  2236. upgrade->activate();
  2237. }
  2238. void CCreInfoWindow::close()
  2239. {
  2240. GH.popIntTotally(this);
  2241. }
  2242. void CCreInfoWindow::clickRight(tribool down, bool previousState)
  2243. {
  2244. if(down)
  2245. return;
  2246. close();
  2247. }
  2248. void CCreInfoWindow::dismissF()
  2249. {
  2250. dsm();
  2251. close();
  2252. }
  2253. void CCreInfoWindow::keyPressed (const SDL_KeyboardEvent & key)
  2254. {
  2255. }
  2256. void CCreInfoWindow::deactivate()
  2257. {
  2258. //if(!active) return;
  2259. //active = false;
  2260. if(!type)
  2261. deactivateRClick();
  2262. if(ok)
  2263. ok->deactivate();
  2264. if(dismiss)
  2265. dismiss->deactivate();
  2266. if(upgrade)
  2267. upgrade->deactivate();
  2268. }
  2269. void CLevelWindow::close()
  2270. {
  2271. for(int i=0;i<comps.size();i++)
  2272. {
  2273. if(comps[i]->selected)
  2274. {
  2275. cb(i);
  2276. break;
  2277. }
  2278. }
  2279. GH.popIntTotally(this);
  2280. LOCPLINT->showingDialog->setn(false);
  2281. }
  2282. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  2283. {
  2284. LOCPLINT->showingDialog->setn(true);
  2285. heroPortrait = hero->portrait;
  2286. cb = callback;
  2287. for(int i=0;i<skills.size();i++)
  2288. {
  2289. comps.push_back(new CSelectableComponent(SComponent::secskill44,skills[i],hero->getSecSkillLevel(skills[i])+1,boost::bind(&CLevelWindow::selectionChanged,this,i)));
  2290. comps.back()->assignedKeys.insert(SDLK_1 + i);
  2291. }
  2292. SDL_Surface *hhlp = BitmapHandler::loadBitmap("LVLUPBKG.bmp");
  2293. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  2294. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  2295. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  2296. SDL_FreeSurface(hhlp);
  2297. pos.x = screen->w/2 - bitmap->w/2;
  2298. pos.y = screen->h/2 - bitmap->h/2;
  2299. pos.w = bitmap->w;
  2300. pos.h = bitmap->h;
  2301. ok = new AdventureMapButton("","",boost::bind(&CLevelWindow::close,this),pos.x+297,pos.y+413,"IOKAY.DEF",SDLK_RETURN);
  2302. //draw window
  2303. char buf[100], buf2[100];
  2304. strcpy(buf2,CGI->generaltexth->allTexts[444].c_str()); //%s has gained a level.
  2305. sprintf(buf,buf2,hero->name.c_str());
  2306. printAtMiddle(buf,192,33,FONT_MEDIUM,zwykly,bitmap);
  2307. strcpy(buf2,CGI->generaltexth->allTexts[445].c_str()); //%s is now a level %d %s.
  2308. sprintf(buf,buf2,hero->name.c_str(),hero->level,hero->type->heroClass->name.c_str());
  2309. printAtMiddle(buf,192,162,FONT_MEDIUM,zwykly,bitmap);
  2310. blitAt(graphics->pskillsm->ourImages[pskill].bitmap,174,190,bitmap);
  2311. printAtMiddle((CGI->generaltexth->primarySkillNames[pskill] + " +1"),192,253,FONT_MEDIUM,zwykly,bitmap);
  2312. const Font *f = graphics->fonts[FONT_MEDIUM];
  2313. std::string text = CGI->generaltexth->allTexts[4];
  2314. int fontWidth = f->getWidth(text.c_str())/2;
  2315. int curx = bitmap->w/2 - ( skills.size()*44 + (skills.size()-1)*(36+fontWidth) )/2;
  2316. for(int i=0;i<comps.size();i++)
  2317. {
  2318. comps[i]->pos.x = curx+pos.x;
  2319. comps[i]->pos.y = 326+pos.y;
  2320. if( i < (comps.size()-1) )
  2321. {
  2322. curx += 44+21; //skill width + margin to "or"
  2323. printAtMiddle(text ,curx,346,FONT_MEDIUM,zwykly,bitmap);
  2324. curx += fontWidth+15;
  2325. }
  2326. }
  2327. if(comps.size() > 1)
  2328. {
  2329. ok->block(true);
  2330. }
  2331. else if(comps.size() == 1)
  2332. {
  2333. comps[0]->select(true);
  2334. }
  2335. }
  2336. void CLevelWindow::selectionChanged(unsigned to)
  2337. {
  2338. if(ok->blocked)
  2339. ok->block(false);
  2340. for(int i=0;i<comps.size();i++)
  2341. if(i==to)
  2342. comps[i]->select(true);
  2343. else
  2344. comps[i]->select(false);
  2345. }
  2346. CLevelWindow::~CLevelWindow()
  2347. {
  2348. delete ok;
  2349. for(int i=0;i<comps.size();i++)
  2350. delete comps[i];
  2351. SDL_FreeSurface(bitmap);
  2352. }
  2353. void CLevelWindow::activate()
  2354. {
  2355. ok->activate();
  2356. for(int i=0;i<comps.size();i++)
  2357. comps[i]->activate();
  2358. }
  2359. void CLevelWindow::deactivate()
  2360. {
  2361. ok->deactivate();
  2362. for(int i=0;i<comps.size();i++)
  2363. comps[i]->deactivate();
  2364. }
  2365. void CLevelWindow::show(SDL_Surface * to)
  2366. {
  2367. blitAt(bitmap,pos.x,pos.y,to);
  2368. blitAt(graphics->portraitLarge[heroPortrait],170+pos.x,66+pos.y,to);
  2369. ok->show(to);
  2370. for(int i=0;i<comps.size();i++)
  2371. comps[i]->show(to);
  2372. }
  2373. void CMinorResDataBar::show(SDL_Surface * to)
  2374. {
  2375. blitAt(bg,pos.x,pos.y,to);
  2376. char buf[30];
  2377. for (int i=0;i<7;i++)
  2378. {
  2379. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  2380. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,FONT_SMALL,zwykly,to);
  2381. }
  2382. std::vector<std::string> temp;
  2383. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  2384. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  2385. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  2386. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  2387. CGI->generaltexth->allTexts[62]
  2388. +": %s, "
  2389. + CGI->generaltexth->allTexts[63]
  2390. + ": %s, "
  2391. + CGI->generaltexth->allTexts[64]
  2392. + ": %s",temp)
  2393. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,FONT_SMALL,zwykly,to);
  2394. }
  2395. CMinorResDataBar::CMinorResDataBar()
  2396. {
  2397. bg = BitmapHandler::loadBitmap("Z2ESBAR.bmp");
  2398. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2399. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  2400. pos.x = 7;
  2401. pos.y = 575;
  2402. pos.w = bg->w;
  2403. pos.h = bg->h;
  2404. }
  2405. CMinorResDataBar::~CMinorResDataBar()
  2406. {
  2407. SDL_FreeSurface(bg);
  2408. }
  2409. SDL_Surface * CCustomImgComponent::getImg()
  2410. {
  2411. return bmp;
  2412. }
  2413. CCustomImgComponent::CCustomImgComponent( Etype Type, int Subtype, int Val, SDL_Surface *sur, bool freeSur )
  2414. :bmp(sur), free(freeSur)
  2415. {
  2416. init(Type,Subtype,Val);
  2417. }
  2418. CCustomImgComponent::~CCustomImgComponent()
  2419. {
  2420. if(free)
  2421. SDL_FreeSurface(bmp);
  2422. }
  2423. CMarketplaceWindow::CTradeableItem::CTradeableItem( int Type, int ID, bool Left)
  2424. {
  2425. left = Left;
  2426. type = Type;
  2427. id = ID;
  2428. }
  2429. void CMarketplaceWindow::CTradeableItem::show(SDL_Surface * to)
  2430. {
  2431. SDL_Surface *hlp = getSurface();
  2432. blitAt(hlp,pos.x+19,pos.y+9,to);
  2433. }
  2434. void CMarketplaceWindow::CTradeableItem::clickLeft(tribool down, bool previousState)
  2435. {
  2436. CMarketplaceWindow *mw = dynamic_cast<CMarketplaceWindow *>(GH.topInt());
  2437. assert(mw);
  2438. if(down)
  2439. {
  2440. if(left)
  2441. {
  2442. if(mw->hLeft != this)
  2443. mw->hLeft = this;
  2444. else
  2445. return;
  2446. }
  2447. else
  2448. {
  2449. if(mw->hRight != this)
  2450. mw->hRight = this;
  2451. else
  2452. return;
  2453. }
  2454. mw->selectionChanged(left);
  2455. }
  2456. }
  2457. void CMarketplaceWindow::CTradeableItem::activate()
  2458. {
  2459. activateLClick();
  2460. }
  2461. void CMarketplaceWindow::CTradeableItem::deactivate()
  2462. {
  2463. deactivateLClick();
  2464. }
  2465. SDL_Surface * CMarketplaceWindow::CTradeableItem::getSurface()
  2466. {
  2467. switch(type)
  2468. {
  2469. case 0:
  2470. return graphics->resources32->ourImages[id].bitmap;
  2471. case 1:
  2472. return graphics->artDefs->ourImages[id].bitmap;
  2473. default:
  2474. return NULL;
  2475. }
  2476. }
  2477. static void initItems( std::vector<CMarketplaceWindow::CTradeableItem*> &i, std::vector<SDL_Rect> &p, int type, int amount, bool left, std::vector<int> *ids/*=NULL*/ )
  2478. {
  2479. i.resize(amount);
  2480. for(int j=0;j<amount;j++)
  2481. {
  2482. i[j] = new CMarketplaceWindow::CTradeableItem(type,(ids && ids->size()>j) ? (*ids)[j] : j, left);
  2483. i[j]->pos = p[j];
  2484. }
  2485. }
  2486. void CMarketplaceWindow::setMode( int mode )
  2487. {
  2488. std::vector<SDL_Rect> lpos, rpos;
  2489. clear();
  2490. switch(mode)
  2491. {
  2492. case 0:
  2493. {
  2494. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPMRKRES.bmp");
  2495. SDL_SetColorKey(bg2,SDL_SRCCOLORKEY,SDL_MapRGB(bg2->format,0,255,255));
  2496. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  2497. bg = SDL_ConvertSurface(bg2,screen->format,0);
  2498. SDL_FreeSurface(bg2);
  2499. lpos += genRect(66,74,39,180), genRect(66,74,122,180), genRect(66,74,204,180),
  2500. genRect(66,74,39,259), genRect(66,74,122,259), genRect(66,74,204,259),
  2501. genRect(66,74,122,338);
  2502. for(int i=0;i<lpos.size();i++)
  2503. {
  2504. lpos[i].x += pos.x;
  2505. lpos[i].y += pos.y;
  2506. rpos.push_back(lpos[i]);
  2507. rpos[rpos.size()-1].x += 288;
  2508. }
  2509. initItems(left,lpos,0,7,true,NULL);
  2510. initItems(right,rpos,0,7,false,NULL);
  2511. printAtMiddle(CGI->generaltexth->allTexts[158],300,27,FONT_BIG,tytulowy,bg); //title
  2512. printAtMiddle(CGI->generaltexth->allTexts[270],154,148,FONT_SMALL,zwykly,bg); //kingdom res.
  2513. printAtMiddle(CGI->generaltexth->allTexts[168],445,147,FONT_SMALL,zwykly,bg); //available for trade
  2514. }
  2515. }
  2516. }
  2517. void CMarketplaceWindow::clear()
  2518. {
  2519. for(int i=0;i<left.size();i++)
  2520. delete left[i];
  2521. for(int i=0;i<right.size();i++)
  2522. delete right[i];
  2523. left.clear();
  2524. right.clear();
  2525. SDL_FreeSurface(bg);
  2526. }
  2527. CMarketplaceWindow::CMarketplaceWindow(int Mode)
  2528. {
  2529. type = BLOCK_ADV_HOTKEYS;
  2530. mode = Mode;
  2531. bg = NULL;
  2532. ok = max = deal = NULL;
  2533. pos.x = screen->w/2 - 300;
  2534. pos.y = screen->h/2 - 296;
  2535. slider = new CSlider(pos.x+231,pos.y+490,137,boost::bind(&CMarketplaceWindow::sliderMoved,this,_1),0,0);
  2536. setMode(mode);
  2537. hLeft = hRight = NULL;
  2538. ok = new AdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally,&GH,this),pos.x+516,pos.y+520,"IOK6432.DEF",SDLK_RETURN);
  2539. ok->assignedKeys.insert(SDLK_ESCAPE);
  2540. deal = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::makeDeal,this),pos.x+307,pos.y+520,"TPMRKB.DEF");
  2541. max = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::setMax,this),pos.x+229,pos.y+520,"IRCBTNS.DEF");
  2542. max->block(true);
  2543. deal->block(true);
  2544. }
  2545. CMarketplaceWindow::~CMarketplaceWindow()
  2546. {
  2547. clear();
  2548. delete slider;
  2549. }
  2550. void CMarketplaceWindow::show(SDL_Surface * to)
  2551. {
  2552. blitAt(bg,pos,to);
  2553. if(hRight)
  2554. CSDL_Ext::drawBorder(to,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  2555. if(hLeft)
  2556. CSDL_Ext::drawBorder(to,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  2557. ok->show(to);
  2558. deal->show(to);
  2559. max->show(to);
  2560. slider->show(to);
  2561. for(int i=0;i<left.size();i++)
  2562. left[i]->show(to);
  2563. for(int i=0;i<right.size();i++)
  2564. right[i]->show(to);
  2565. if(mode==0)
  2566. {
  2567. char buf[15];
  2568. for(int i=0;i<left.size();i++)
  2569. {
  2570. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  2571. printAtMiddle(buf,left[i]->pos.x+36,left[i]->pos.y+57,FONT_SMALL,zwykly,to);
  2572. }
  2573. if(hLeft) //print prices
  2574. {
  2575. for(int i=0; i<right.size();i++)
  2576. {
  2577. if(right[i]->id != hLeft->id)
  2578. printAtMiddle(rSubs[i],right[i]->pos.x+36,right[i]->pos.y+57,FONT_SMALL,zwykly,to);
  2579. else
  2580. printAtMiddle(CGI->generaltexth->allTexts[164],right[i]->pos.x+36,right[i]->pos.y+57,FONT_SMALL,zwykly,to);
  2581. }
  2582. }
  2583. if(hLeft && hRight && hLeft->id!= hRight->id)
  2584. {
  2585. blitAt(hLeft->getSurface(),pos.x+141,pos.y+457,to);
  2586. blitAt(hRight->getSurface(),pos.x+429,pos.y+457,to);
  2587. SDL_itoa(slider->value * r1,buf,10);
  2588. printAtMiddle(buf,pos.x+156,pos.y+505,FONT_SMALL,zwykly,to);
  2589. SDL_itoa(slider->value * r2,buf,10);
  2590. printAtMiddle(buf,pos.x+443,pos.y+505,FONT_SMALL,zwykly,to);
  2591. }
  2592. }
  2593. }
  2594. void CMarketplaceWindow::activate()
  2595. {
  2596. for(int i=0;i<left.size();i++)
  2597. left[i]->activate();
  2598. for(int i=0;i<right.size();i++)
  2599. right[i]->activate();
  2600. ok->activate();
  2601. max->activate();
  2602. deal->activate();
  2603. slider->activate();
  2604. }
  2605. void CMarketplaceWindow::deactivate()
  2606. {
  2607. for(int i=0;i<left.size();i++)
  2608. left[i]->deactivate();
  2609. for(int i=0;i<right.size();i++)
  2610. right[i]->deactivate();
  2611. ok->deactivate();
  2612. max->deactivate();
  2613. deal->deactivate();
  2614. slider->deactivate();
  2615. }
  2616. void CMarketplaceWindow::setMax()
  2617. {
  2618. slider->moveTo(slider->amount);
  2619. }
  2620. void CMarketplaceWindow::makeDeal()
  2621. {
  2622. LOCPLINT->cb->trade(mode,hLeft->id,hRight->id,slider->value*r1);
  2623. slider->moveTo(0);
  2624. hLeft = NULL;
  2625. selectionChanged(true);
  2626. }
  2627. void CMarketplaceWindow::sliderMoved( int to )
  2628. {
  2629. }
  2630. void CMarketplaceWindow::selectionChanged(bool side)
  2631. {
  2632. if(hLeft && hRight && hLeft->id!= hRight->id)
  2633. {
  2634. LOCPLINT->cb->getMarketOffer(hLeft->id,hRight->id,r1,r2,0);
  2635. slider->setAmount(LOCPLINT->cb->getResourceAmount(hLeft->id) / r1);
  2636. slider->moveTo(0);
  2637. max->block(false);
  2638. deal->block(false);
  2639. }
  2640. else
  2641. {
  2642. max->block(true);
  2643. deal->block(true);
  2644. slider->setAmount(0);
  2645. slider->moveTo(0);
  2646. }
  2647. if(side && hLeft) //left selection changed, recalculate offers
  2648. {
  2649. rSubs.clear();
  2650. rSubs.resize(right.size());
  2651. int h1, h2;
  2652. for(int i=0;i<right.size();i++)
  2653. {
  2654. std::ostringstream oss;
  2655. LOCPLINT->cb->getMarketOffer(hLeft->id,i,h1,h2,0);
  2656. oss << h2;
  2657. if(h1!=1)
  2658. oss << "/" << h1;
  2659. rSubs[i] = oss.str();
  2660. }
  2661. }
  2662. }
  2663. CSystemOptionsWindow::CSystemOptionsWindow(const SDL_Rect &pos, CPlayerInterface * owner)
  2664. {
  2665. this->pos = pos;
  2666. SDL_Surface *hhlp = BitmapHandler::loadBitmap("SysOpbck.bmp", true);
  2667. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  2668. background = SDL_ConvertSurface(hhlp,screen->format,0);
  2669. SDL_SetColorKey(background,SDL_SRCCOLORKEY,SDL_MapRGB(background->format,0,255,255));
  2670. SDL_FreeSurface(hhlp);
  2671. //printing texts
  2672. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[568], 242, 32, FONT_BIG, tytulowy, background); //window title
  2673. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[569], 122, 64, FONT_MEDIUM, tytulowy, background); //hero speed
  2674. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[570], 122, 130, FONT_MEDIUM, tytulowy, background); //enemy speed
  2675. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[571], 122, 196, FONT_MEDIUM, tytulowy, background); //map scroll speed
  2676. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[20], 122, 262, FONT_MEDIUM, tytulowy, background); //video quality
  2677. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 347, FONT_MEDIUM, tytulowy, background); //music volume
  2678. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 412, FONT_MEDIUM, tytulowy, background); //effects volume
  2679. CSDL_Ext::printAt(CGI->generaltexth->allTexts[572], 282, 57, FONT_MEDIUM, zwykly, background); //show move path
  2680. CSDL_Ext::printAt(CGI->generaltexth->allTexts[573], 282, 89, FONT_MEDIUM, zwykly, background); //show hero reminder
  2681. CSDL_Ext::printAt(CGI->generaltexth->allTexts[574], 282, 121, FONT_MEDIUM, zwykly, background); //quick combat
  2682. CSDL_Ext::printAt(CGI->generaltexth->allTexts[575], 282, 153, FONT_MEDIUM, zwykly, background); //video subtitles
  2683. CSDL_Ext::printAt(CGI->generaltexth->allTexts[576], 282, 185, FONT_MEDIUM, zwykly, background); //town building outlines
  2684. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 282, 217, FONT_MEDIUM, zwykly, background); //spell book animation
  2685. //setting up buttons
  2686. // load = new AdventureMapButton (CGI->generaltexth->zelp[321].first, CGI->generaltexth->zelp[321].second, boost::bind(&CSystemOptionsWindow::loadf, this), pos.x+246, pos.y+298, "SOLOAD.DEF", SDLK_l);
  2687. // std::swap(save->imgs[0][0], load->imgs[0][1]);
  2688. save = new AdventureMapButton (CGI->generaltexth->zelp[322].first, CGI->generaltexth->zelp[322].second, boost::bind(&CSystemOptionsWindow::bsavef, this), pos.x+357, pos.y+298, "SOSAVE.DEF", SDLK_s);
  2689. std::swap(save->imgs[0][0], save->imgs[0][1]);
  2690. // restart = new AdventureMapButton (CGI->generaltexth->zelp[323].first, CGI->generaltexth->zelp[323].second, boost::bind(&CSystemOptionsWindow::bmainmenuf, this), pos.x+346, pos.y+357, "SORSTRT", SDLK_r);
  2691. // std::swap(save->imgs[0][0], restart->imgs[0][1]);
  2692. mainMenu = new AdventureMapButton (CGI->generaltexth->zelp[320].first, CGI->generaltexth->zelp[320].second, boost::bind(&CSystemOptionsWindow::bmainmenuf, this), pos.x+357, pos.y+357, "SOMAIN.DEF", SDLK_m);
  2693. std::swap(mainMenu->imgs[0][0], mainMenu->imgs[0][1]);
  2694. quitGame = new AdventureMapButton (CGI->generaltexth->zelp[324].first, CGI->generaltexth->zelp[324].second, boost::bind(&CSystemOptionsWindow::bquitf, this), pos.x+246, pos.y+415, "soquit.def", SDLK_q);
  2695. std::swap(quitGame->imgs[0][0], quitGame->imgs[0][1]);
  2696. backToMap = new AdventureMapButton (CGI->generaltexth->zelp[325].first, CGI->generaltexth->zelp[325].second, boost::bind(&CSystemOptionsWindow::breturnf, this), pos.x+357, pos.y+415, "soretrn.def", SDLK_RETURN);
  2697. std::swap(backToMap->imgs[0][0], backToMap->imgs[0][1]);
  2698. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  2699. heroMoveSpeed = new CHighlightableButtonsGroup(0);
  2700. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[349].second),CGI->generaltexth->zelp[349].second, "sysopb1.def", pos.x+28, pos.y+77, 1);
  2701. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[350].second),CGI->generaltexth->zelp[350].second, "sysopb2.def", pos.x+76, pos.y+77, 2);
  2702. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[351].second),CGI->generaltexth->zelp[351].second, "sysopb3.def", pos.x+124, pos.y+77, 4);
  2703. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[352].second),CGI->generaltexth->zelp[352].second, "sysopb4.def", pos.x+172, pos.y+77, 8);
  2704. heroMoveSpeed->select(owner->sysOpts.heroMoveSpeed, 1);
  2705. heroMoveSpeed->onChange = boost::bind(&SystemOptions::setHeroMoveSpeed, &owner->sysOpts, _1);
  2706. mapScrollSpeed = new CHighlightableButtonsGroup(0);
  2707. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[357].second),CGI->generaltexth->zelp[357].second, "sysopb9.def", pos.x+28, pos.y+210, 1);
  2708. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[358].second),CGI->generaltexth->zelp[358].second, "sysob10.def", pos.x+92, pos.y+210, 2);
  2709. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[359].second),CGI->generaltexth->zelp[359].second, "sysob11.def", pos.x+156, pos.y+210, 4);
  2710. mapScrollSpeed->select(owner->sysOpts.mapScrollingSpeed, 1);
  2711. mapScrollSpeed->onChange = boost::bind(&SystemOptions::setMapScrollingSpeed, &owner->sysOpts, _1);
  2712. musicVolume = new CHighlightableButtonsGroup(0, true);
  2713. for(int i=0; i<10; ++i)
  2714. {
  2715. musicVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[326+i].second),CGI->generaltexth->zelp[326+i].second, "syslb.def", pos.x+29 + 19*i, pos.y+359, i*11);
  2716. }
  2717. musicVolume->select(CGI->musich->getVolume(), 1);
  2718. musicVolume->onChange = boost::bind(&SystemOptions::setMusicVolume, &owner->sysOpts, _1);
  2719. effectsVolume = new CHighlightableButtonsGroup(0, true);
  2720. for(int i=0; i<10; ++i)
  2721. {
  2722. effectsVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[336+i].second),CGI->generaltexth->zelp[336+i].second, "syslb.def", pos.x+29 + 19*i, pos.y+425, i*11);
  2723. }
  2724. effectsVolume->select(CGI->soundh->getVolume(), 1);
  2725. effectsVolume->onChange = boost::bind(&SystemOptions::setSoundVolume, &owner->sysOpts, _1);
  2726. }
  2727. CSystemOptionsWindow::~CSystemOptionsWindow()
  2728. {
  2729. SDL_FreeSurface(background);
  2730. delete save;
  2731. delete quitGame;
  2732. delete backToMap;
  2733. delete mainMenu;
  2734. delete heroMoveSpeed;
  2735. delete mapScrollSpeed;
  2736. delete musicVolume;
  2737. delete effectsVolume;
  2738. }
  2739. void CSystemOptionsWindow::pushSDLEvent(int type, int usercode)
  2740. {
  2741. GH.popIntTotally(this);
  2742. SDL_Event event;
  2743. event.type = type;
  2744. event.user.code = usercode; // not necessarily used
  2745. SDL_PushEvent(&event);
  2746. }
  2747. void CSystemOptionsWindow::bquitf()
  2748. {
  2749. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<SComponent*>(), boost::bind(&CSystemOptionsWindow::pushSDLEvent, this, SDL_QUIT, 0), 0, false);
  2750. }
  2751. void CSystemOptionsWindow::breturnf()
  2752. {
  2753. GH.popIntTotally(this);
  2754. }
  2755. void CSystemOptionsWindow::bmainmenuf()
  2756. {
  2757. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<SComponent*>(), boost::bind(&CSystemOptionsWindow::pushSDLEvent, this, SDL_USEREVENT, 2), 0, false);
  2758. }
  2759. void CSystemOptionsWindow::bsavef()
  2760. {
  2761. GH.popIntTotally(this);
  2762. GH.pushInt(new CSelectionScreen(CMenuScreen::saveGame));
  2763. /*using namespace boost::posix_time;
  2764. std::ostringstream fnameStream;
  2765. fnameStream << second_clock::local_time();
  2766. std::string fname = fnameStream.str();
  2767. boost::algorithm::replace_all(fname,":","");
  2768. boost::algorithm::replace_all(fname," ","-");
  2769. 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);*/
  2770. }
  2771. void CSystemOptionsWindow::activate()
  2772. {
  2773. save->activate();
  2774. quitGame->activate();
  2775. backToMap->activate();
  2776. mainMenu->activate();
  2777. heroMoveSpeed->activate();
  2778. mapScrollSpeed->activate();
  2779. musicVolume->activate();
  2780. effectsVolume->activate();
  2781. }
  2782. void CSystemOptionsWindow::deactivate()
  2783. {
  2784. save->deactivate();
  2785. quitGame->deactivate();
  2786. backToMap->deactivate();
  2787. mainMenu->deactivate();
  2788. heroMoveSpeed->deactivate();
  2789. mapScrollSpeed->deactivate();
  2790. musicVolume->deactivate();
  2791. effectsVolume->deactivate();
  2792. }
  2793. void CSystemOptionsWindow::show(SDL_Surface *to)
  2794. {
  2795. SDL_BlitSurface(background, NULL, to, &pos);
  2796. save->show(to);
  2797. quitGame->show(to);
  2798. backToMap->show(to);
  2799. mainMenu->show(to);
  2800. heroMoveSpeed->show(to);
  2801. mapScrollSpeed->show(to);
  2802. musicVolume->show(to);
  2803. effectsVolume->show(to);
  2804. }
  2805. CTavernWindow::CTavernWindow(const CGHeroInstance *H1, const CGHeroInstance *H2, const std::string &gossip)
  2806. :h1(selected,0,72,299,H1),h2(selected,1,162,299,H2)
  2807. {
  2808. if(H1)
  2809. selected = 0;
  2810. else
  2811. selected = -1;
  2812. oldSelected = -1;
  2813. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPTAVERN.bmp");
  2814. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  2815. bg = SDL_ConvertSurface(hhlp,screen->format,0);
  2816. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2817. SDL_FreeSurface(hhlp);
  2818. printAtMiddle(CGI->generaltexth->jktexts[37],200,35,FONT_BIG,tytulowy,bg);
  2819. printAtMiddle("2500",320,328,FONT_SMALL,zwykly,bg);
  2820. // printAtMiddle(CGI->generaltexth->jktexts[38],146,283,FONT_BIG,tytulowy,bg); //what is this???
  2821. printAtMiddleWB(gossip,200,220,FONT_SMALL,50,zwykly,bg);
  2822. pos.w = bg->w;
  2823. pos.h = bg->h;
  2824. pos.x = (screen->w-bg->w)/2;
  2825. pos.y = (screen->h-bg->h)/2;
  2826. bar = new CStatusBar(pos.x+8, pos.y+478, "APHLFTRT.bmp", 380);
  2827. h1.pos.x += pos.x;
  2828. h2.pos.x += pos.x;
  2829. h1.pos.y += pos.y;
  2830. h2.pos.y += pos.y;
  2831. cancel = new AdventureMapButton(CGI->generaltexth->tavernInfo[7],"", boost::bind(&CTavernWindow::close, this), pos.x+310,pos.y+428, "ICANCEL.DEF", SDLK_ESCAPE);
  2832. recruit = new AdventureMapButton("", "", boost::bind(&CTavernWindow::recruitb, this), pos.x+272, pos.y+355, "TPTAV01.DEF", SDLK_RETURN);
  2833. thiefGuild = new AdventureMapButton(CGI->generaltexth->tavernInfo[5],"", boost::bind(&CTavernWindow::thievesguildb, this), pos.x+22, pos.y+428, "TPTAV02.DEF", SDLK_t);
  2834. if(LOCPLINT->cb->getResourceAmount(6) < 2500) //not enough gold
  2835. {
  2836. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[0]; //Cannot afford a Hero
  2837. recruit->block(2);
  2838. }
  2839. else if(LOCPLINT->cb->howManyHeroes(false) >= 8)
  2840. {
  2841. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  2842. boost::algorithm::replace_first(recruit->hoverTexts[0],"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes()));
  2843. recruit->block(2);
  2844. }
  2845. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  2846. {
  2847. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[2]; //Cannot recruit. You already have a Hero in this town.
  2848. recruit->block(2);
  2849. }
  2850. else
  2851. {
  2852. if(!H1)
  2853. recruit->block(1);
  2854. }
  2855. #ifdef _WIN32
  2856. CGI->videoh->open("TAVERN.BIK");
  2857. #else
  2858. CGI->videoh->open("tavern.mjpg", true, false);
  2859. #endif
  2860. }
  2861. void CTavernWindow::recruitb()
  2862. {
  2863. const CGHeroInstance *toBuy = (selected ? h2 : h1).h;
  2864. close();
  2865. LOCPLINT->cb->recruitHero(LOCPLINT->castleInt->town,toBuy);
  2866. }
  2867. void CTavernWindow::thievesguildb()
  2868. {
  2869. GH.pushInt( new CThievesGuildWindow(LOCPLINT->castleInt->town) );
  2870. }
  2871. CTavernWindow::~CTavernWindow()
  2872. {
  2873. CGI->videoh->close();
  2874. SDL_FreeSurface(bg);
  2875. delete cancel;
  2876. delete thiefGuild;
  2877. delete recruit;
  2878. delete bar;
  2879. }
  2880. void CTavernWindow::activate()
  2881. {
  2882. thiefGuild->activate();
  2883. cancel->activate();
  2884. if(h1.h)
  2885. h1.activate();
  2886. if(h2.h)
  2887. h2.activate();
  2888. recruit->activate();
  2889. GH.statusbar = bar;
  2890. }
  2891. void CTavernWindow::deactivate()
  2892. {
  2893. thiefGuild->deactivate();
  2894. cancel->deactivate();
  2895. if(h1.h)
  2896. h1.deactivate();
  2897. if(h2.h)
  2898. h2.deactivate();
  2899. recruit->deactivate();
  2900. }
  2901. void CTavernWindow::close()
  2902. {
  2903. GH.popIntTotally(this);
  2904. }
  2905. void CTavernWindow::show(SDL_Surface * to)
  2906. {
  2907. blitAt(bg,pos.x,pos.y,to);
  2908. CGI->videoh->update(pos.x+70, pos.y+56, to, true, false);
  2909. if(h1.h)
  2910. h1.show(to);
  2911. if(h2.h)
  2912. h2.show(to);
  2913. thiefGuild->show(to);
  2914. cancel->show(to);
  2915. recruit->show(to);
  2916. bar->show(to);
  2917. if(selected >= 0)
  2918. {
  2919. HeroPortrait *sel = selected ? &h2 : &h1;
  2920. if (selected != oldSelected && !recruit->blocked)
  2921. {
  2922. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  2923. oldSelected = selected;
  2924. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[3]; //Recruit %s the %s
  2925. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->name);
  2926. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->type->heroClass->name);
  2927. }
  2928. printAtMiddleWB(sel->descr,pos.x+146,pos.y+389,FONT_SMALL,40,zwykly,to);
  2929. CSDL_Ext::drawBorder(to,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  2930. }
  2931. }
  2932. void CTavernWindow::HeroPortrait::clickLeft(tribool down, bool previousState)
  2933. {
  2934. if(previousState && !down)
  2935. as();
  2936. //ClickableL::clickLeft(down);
  2937. }
  2938. void CTavernWindow::HeroPortrait::activate()
  2939. {
  2940. activateLClick();
  2941. activateRClick();
  2942. activateHover();
  2943. }
  2944. void CTavernWindow::HeroPortrait::deactivate()
  2945. {
  2946. deactivateLClick();
  2947. deactivateRClick();
  2948. deactivateHover();
  2949. }
  2950. void CTavernWindow::HeroPortrait::clickRight(tribool down, bool previousState)
  2951. {
  2952. if(down)
  2953. {
  2954. LOCPLINT->adventureInt->heroWindow->setHero(h);
  2955. GH.pushInt(new CRClickPopupInt(LOCPLINT->adventureInt->heroWindow,false));
  2956. }
  2957. }
  2958. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  2959. :as(sel,id)
  2960. {
  2961. h = H;
  2962. pos.x = x;
  2963. pos.y = y;
  2964. pos.w = 58;
  2965. pos.h = 64;
  2966. if(H)
  2967. {
  2968. hoverName = CGI->generaltexth->tavernInfo[4];
  2969. boost::algorithm::replace_first(hoverName,"%s",H->name);
  2970. int artifs = h->artifWorn.size() + h->artifacts.size();
  2971. for(int i=13; i<=17; i++) //war machines and spellbook don't count
  2972. if(vstd::contains(h->artifWorn,i))
  2973. artifs--;
  2974. sprintf_s(descr, sizeof(descr),CGI->generaltexth->allTexts[215].c_str(),
  2975. h->name.c_str(), h->level, h->type->heroClass->name.c_str(), artifs);
  2976. descr[sizeof(descr)-1] = '\0';
  2977. }
  2978. }
  2979. void CTavernWindow::HeroPortrait::show(SDL_Surface * to)
  2980. {
  2981. blitAt(graphics->portraitLarge[h->subID],pos,to);
  2982. }
  2983. void CTavernWindow::HeroPortrait::hover( bool on )
  2984. {
  2985. //Hoverable::hover(on);
  2986. if(on)
  2987. GH.statusbar->print(hoverName);
  2988. else
  2989. GH.statusbar->clear();
  2990. }
  2991. void CInGameConsole::activate()
  2992. {
  2993. activateKeys();
  2994. }
  2995. void CInGameConsole::deactivate()
  2996. {
  2997. deactivateKeys();
  2998. }
  2999. void CInGameConsole::show(SDL_Surface * to)
  3000. {
  3001. int number = 0;
  3002. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  3003. texts_mx.lock();
  3004. for(std::list< std::pair< std::string, int > >::iterator it = texts.begin(); it != texts.end(); ++it, ++number)
  3005. {
  3006. SDL_Color green = {0,0xff,0,0};
  3007. Point leftBottomCorner(0, screen->h);
  3008. if(LOCPLINT->battleInt)
  3009. {
  3010. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  3011. }
  3012. CSDL_Ext::printAt(it->first, leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20, FONT_MEDIUM, green);
  3013. if(SDL_GetTicks() - it->second > defaultTimeout)
  3014. {
  3015. toDel.push_back(it);
  3016. }
  3017. }
  3018. for(int it=0; it<toDel.size(); ++it)
  3019. {
  3020. texts.erase(toDel[it]);
  3021. }
  3022. texts_mx.unlock();
  3023. }
  3024. void CInGameConsole::print(const std::string &txt)
  3025. {
  3026. texts_mx.lock();
  3027. int lineLen = conf.go()->ac.outputLineLength;
  3028. if(txt.size() < lineLen)
  3029. {
  3030. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  3031. if(texts.size() > maxDisplayedTexts)
  3032. {
  3033. texts.pop_front();
  3034. }
  3035. }
  3036. else
  3037. {
  3038. assert(lineLen);
  3039. for(int g=0; g<txt.size() / lineLen + 1; ++g)
  3040. {
  3041. std::string part = txt.substr(g * lineLen, lineLen);
  3042. if(part.size() == 0)
  3043. break;
  3044. texts.push_back(std::make_pair(part, SDL_GetTicks()));
  3045. if(texts.size() > maxDisplayedTexts)
  3046. {
  3047. texts.pop_front();
  3048. }
  3049. }
  3050. }
  3051. texts_mx.unlock();
  3052. }
  3053. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  3054. {
  3055. if(key.type != SDL_KEYDOWN) return;
  3056. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  3057. switch(key.keysym.sym)
  3058. {
  3059. case SDLK_TAB:
  3060. case SDLK_ESCAPE:
  3061. {
  3062. if(captureAllKeys)
  3063. {
  3064. captureAllKeys = false;
  3065. endEnteringText(false);
  3066. }
  3067. else if(SDLK_TAB)
  3068. {
  3069. captureAllKeys = true;
  3070. startEnteringText();
  3071. }
  3072. break;
  3073. }
  3074. case SDLK_RETURN: //enter key
  3075. {
  3076. if(enteredText.size() > 0 && captureAllKeys)
  3077. {
  3078. captureAllKeys = false;
  3079. endEnteringText(true);
  3080. }
  3081. break;
  3082. }
  3083. case SDLK_BACKSPACE:
  3084. {
  3085. if(enteredText.size() > 1)
  3086. {
  3087. enteredText.resize(enteredText.size()-1);
  3088. enteredText[enteredText.size()-1] = '_';
  3089. refreshEnteredText();
  3090. }
  3091. break;
  3092. }
  3093. case SDLK_UP: //up arrow
  3094. {
  3095. if(previouslyEntered.size() == 0)
  3096. break;
  3097. if(prevEntDisp == -1)
  3098. {
  3099. prevEntDisp = previouslyEntered.size() - 1;
  3100. enteredText = previouslyEntered[prevEntDisp] + "_";
  3101. refreshEnteredText();
  3102. }
  3103. else if( prevEntDisp > 0)
  3104. {
  3105. --prevEntDisp;
  3106. enteredText = previouslyEntered[prevEntDisp] + "_";
  3107. refreshEnteredText();
  3108. }
  3109. break;
  3110. }
  3111. case SDLK_DOWN: //down arrow
  3112. {
  3113. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  3114. {
  3115. ++prevEntDisp;
  3116. enteredText = previouslyEntered[prevEntDisp] + "_";
  3117. refreshEnteredText();
  3118. }
  3119. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  3120. {
  3121. prevEntDisp = -1;
  3122. enteredText = "_";
  3123. refreshEnteredText();
  3124. }
  3125. break;
  3126. }
  3127. default:
  3128. {
  3129. if(enteredText.size() > 0 && enteredText.size() < conf.go()->ac.inputLineLength)
  3130. {
  3131. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  3132. {
  3133. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  3134. enteredText += "_";
  3135. refreshEnteredText();
  3136. }
  3137. }
  3138. break;
  3139. }
  3140. }
  3141. }
  3142. void CInGameConsole::startEnteringText()
  3143. {
  3144. enteredText = "_";
  3145. if(GH.topInt() == LOCPLINT->adventureInt)
  3146. {
  3147. GH.statusbar->print(enteredText);
  3148. }
  3149. else if(LOCPLINT->battleInt)
  3150. {
  3151. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3152. }
  3153. }
  3154. void CInGameConsole::endEnteringText(bool printEnteredText)
  3155. {
  3156. prevEntDisp = -1;
  3157. if(printEnteredText)
  3158. {
  3159. std::string txt = enteredText.substr(0, enteredText.size()-1);
  3160. LOCPLINT->cb->sendMessage(txt);
  3161. previouslyEntered.push_back(txt);
  3162. print(txt);
  3163. }
  3164. enteredText = "";
  3165. if(GH.topInt() == LOCPLINT->adventureInt)
  3166. {
  3167. GH.statusbar->clear();
  3168. }
  3169. else if(LOCPLINT->battleInt)
  3170. {
  3171. LOCPLINT->battleInt->console->ingcAlter = "";
  3172. }
  3173. }
  3174. void CInGameConsole::refreshEnteredText()
  3175. {
  3176. if(GH.topInt() == LOCPLINT->adventureInt)
  3177. {
  3178. GH.statusbar->print(enteredText);
  3179. }
  3180. else if(LOCPLINT->battleInt)
  3181. {
  3182. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3183. }
  3184. }
  3185. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  3186. {
  3187. }
  3188. void CGarrisonWindow::close()
  3189. {
  3190. GH.popIntTotally(this);
  3191. }
  3192. void CGarrisonWindow::activate()
  3193. {
  3194. quit->activate();
  3195. garr->activate();
  3196. }
  3197. void CGarrisonWindow::deactivate()
  3198. {
  3199. quit->deactivate();
  3200. garr->deactivate();
  3201. }
  3202. void CGarrisonWindow::show(SDL_Surface * to)
  3203. {
  3204. blitAt(bg,pos,to);
  3205. quit->show(to);
  3206. garr->show(to);
  3207. blitAt(graphics->flags->ourImages[garr->odown->getOwner()].bitmap,pos.x+28,pos.y+124,to);
  3208. blitAt(graphics->portraitLarge[static_cast<const CGHeroInstance*>(garr->odown)->portrait],pos.x+29,pos.y+222,to);
  3209. printAtMiddle(CGI->generaltexth->allTexts[709],pos.x+275,pos.y+30,FONT_BIG,tytulowy,to);
  3210. }
  3211. CGarrisonWindow::CGarrisonWindow( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits )
  3212. {
  3213. bg = BitmapHandler::loadBitmap("GARRISON.bmp");
  3214. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  3215. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  3216. pos.x = screen->w/2 - bg->w/2;
  3217. pos.y = screen->h/2 - bg->h/2;
  3218. pos.w = screen->w;
  3219. pos.h = screen->h;
  3220. garr = new CGarrisonInt(pos.x+92, pos.y+127, 4, Point(0,96), bg, Point(93,127), up, down, removableUnits);
  3221. garr->splitButtons.push_back(new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+88,pos.y+314,"IDV6432.DEF"));
  3222. quit = new AdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CGarrisonWindow::close,this),pos.x+399,pos.y+314,"IOK6432.DEF",SDLK_RETURN);
  3223. }
  3224. CGarrisonWindow::~CGarrisonWindow()
  3225. {
  3226. SDL_FreeSurface(bg);
  3227. delete quit;
  3228. delete garr;
  3229. }
  3230. IShowActivable::IShowActivable()
  3231. {
  3232. type = 0;
  3233. }
  3234. CWindowWithGarrison::CWindowWithGarrison()
  3235. {
  3236. type |= WITH_GARRISON;
  3237. }
  3238. void CRClickPopupInt::show(SDL_Surface * to)
  3239. {
  3240. inner->show(to);
  3241. }
  3242. CRClickPopupInt::CRClickPopupInt( IShowActivable *our, bool deleteInt )
  3243. {
  3244. CGI->curh->hide();
  3245. inner = our;
  3246. delInner = deleteInt;
  3247. }
  3248. CRClickPopupInt::~CRClickPopupInt()
  3249. {
  3250. //workaround for hero window issue - if it's our interface, call dispose to properly reset it's state
  3251. //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...?)
  3252. if(LOCPLINT && inner == LOCPLINT->adventureInt->heroWindow)
  3253. LOCPLINT->adventureInt->heroWindow->dispose();
  3254. if(delInner)
  3255. delete inner;
  3256. CGI->curh->show();
  3257. }
  3258. CArtPlace::CArtPlace(const CArtifact* Art): active(false), marked(false), ourArt(Art)/*,
  3259. spellBook(false), warMachine1(false), warMachine2(false), warMachine3(false),
  3260. warMachine4(false),misc1(false), misc2(false), misc3(false), misc4(false),
  3261. misc5(false), feet(false), lRing(false), rRing(false), torso(false),
  3262. lHand(false), rHand(false), neck(false), shoulders(false), head(false) */
  3263. {
  3264. }
  3265. void CArtPlace::activate()
  3266. {
  3267. if(!active)
  3268. {
  3269. //activateLClick();
  3270. LRClickableAreaWTextComp::activate();
  3271. active = true;
  3272. }
  3273. }
  3274. void CArtPlace::clickLeft(tribool down, bool previousState)
  3275. {
  3276. //LRClickableAreaWTextComp::clickLeft(down);
  3277. // If clicked on spellbook, open it only if no artifact is held at the moment.
  3278. if(ourArt && !down && previousState && !ourOwner->commonInfo->srcAOH)
  3279. {
  3280. if(ourArt->id == 0)
  3281. {
  3282. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), ourOwner->curHero);
  3283. GH.pushInt(spellWindow);
  3284. }
  3285. }
  3286. if (!down && previousState)
  3287. {
  3288. if(ourArt && ourArt->id == 0)
  3289. return; //this is handled separately
  3290. if(!ourOwner->commonInfo->srcAOH) //nothing has been clicked
  3291. {
  3292. if(ourArt) //to prevent selecting empty slots (bugfix to what GrayFace reported)
  3293. {
  3294. if(ourArt->id == 3) //catapult cannot be highlighted
  3295. {
  3296. std::vector<SComponent *> catapult(1, new SComponent(SComponent::artifact, 3, 0));
  3297. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312], catapult); //The Catapult must be equipped.
  3298. return;
  3299. }
  3300. select();
  3301. }
  3302. }
  3303. else //perform artifact substitution
  3304. {
  3305. if (slotID >= 19) // Backpack destination.
  3306. {
  3307. const CArtifact * cur = ourOwner->commonInfo->srcArtifact;
  3308. switch(cur->id)
  3309. {
  3310. case 3:
  3311. //should not happen, catapult cannot be selected
  3312. assert(cur->id != 3);
  3313. break;
  3314. case 4: case 5: case 6:
  3315. {
  3316. std::string text = CGI->generaltexth->allTexts[153];
  3317. boost::algorithm::replace_first(text, "%s", cur->Name());
  3318. LOCPLINT->showInfoDialog(text);
  3319. }
  3320. break;
  3321. default:
  3322. ourOwner->commonInfo->destAOH = ourOwner;
  3323. ourOwner->commonInfo->destSlotID = slotID;
  3324. ourOwner->commonInfo->destArtifact = NULL;
  3325. // Correction for backpack position when src lies before dest.
  3326. ourOwner->commonInfo->destSlotID +=
  3327. (ourOwner->commonInfo->srcAOH == ourOwner
  3328. && ourOwner->commonInfo->srcSlotID >= 19
  3329. && ourOwner->commonInfo->srcSlotID <= slotID);
  3330. LOCPLINT->cb->swapArtifacts(
  3331. ourOwner->commonInfo->srcAOH->curHero,
  3332. ourOwner->commonInfo->srcSlotID,
  3333. ourOwner->curHero,
  3334. ourOwner->commonInfo->destSlotID);
  3335. break;
  3336. }
  3337. }
  3338. //check if swap is possible
  3339. else if (this->fitsHere(ourOwner->commonInfo->srcArtifact))
  3340. {
  3341. ourOwner->commonInfo->destAOH = ourOwner;
  3342. ourOwner->commonInfo->destSlotID = slotID;
  3343. ourOwner->commonInfo->destArtifact = ourArt;
  3344. // Special case when the dest artifact can't be fit into the src slot.
  3345. CGHeroInstance *destHero = const_cast<CGHeroInstance *>(ourOwner->curHero);
  3346. CGI->arth->unequipArtifact(destHero->artifWorn, slotID);
  3347. const CArtifactsOfHero* srcAOH = ourOwner->commonInfo->srcAOH;
  3348. ui16 srcSlotID = ourOwner->commonInfo->srcSlotID;
  3349. if (ourArt && srcSlotID < 19 && !ourArt->fitsAt(srcAOH->curHero->artifWorn, srcSlotID)) {
  3350. // Put dest artifact into owner's backpack.
  3351. ourOwner->commonInfo->srcAOH = ourOwner;
  3352. ourOwner->commonInfo->srcSlotID = ourOwner->curHero->artifacts.size() + 19;
  3353. }
  3354. LOCPLINT->cb->swapArtifacts(
  3355. srcAOH->curHero,
  3356. srcSlotID,
  3357. ourOwner->curHero,
  3358. slotID);
  3359. }
  3360. }
  3361. }
  3362. /*else if(!down && clicked)
  3363. {
  3364. if(ourArt && ourArt->id == 0)
  3365. return; //this is handled separately
  3366. deselect();
  3367. }*/
  3368. //ClickableL::clickLeft(down);
  3369. }
  3370. void CArtPlace::clickRight(tribool down, bool previousState)
  3371. {
  3372. if(ourArt && !locked() && text.size()) { //if there is no description or it's a lock, do nothing ;]
  3373. LRClickableAreaWTextComp::clickRight(down, previousState);
  3374. /*if (ourArt->constituentOf != NULL) {
  3375. BOOST_FOREACH(ui32 combination, *ourArt->constituentOf) {
  3376. if (ourArt->canBeAssembledTo(ourOwner->curHero->artifWorn, combination)) {
  3377. LOCPLINT->cb->assembleArtifacts(ourOwner->curHero, slotID, true, combination);
  3378. return;
  3379. }
  3380. }
  3381. }
  3382. if (ourArt->constituents != NULL) {
  3383. LOCPLINT->cb->assembleArtifacts(ourOwner->curHero, slotID, false, 0);
  3384. return;
  3385. }*/
  3386. }
  3387. }
  3388. /**
  3389. * Selects artifact slot so that the containing artifact looks like it's picked up.
  3390. */
  3391. void CArtPlace::select ()
  3392. {
  3393. if (locked())
  3394. return;
  3395. CGI->curh->dragAndDropCursor(graphics->artDefs->ourImages[ourArt->id].bitmap);
  3396. ourOwner->commonInfo->srcArtifact = ourArt;
  3397. ourOwner->commonInfo->srcSlotID = slotID;
  3398. ourOwner->commonInfo->srcAOH = ourOwner;
  3399. // Temporarily remove artifact from hero.
  3400. CGHeroInstance* hero = const_cast<CGHeroInstance*>(ourOwner->curHero);
  3401. if (slotID < 19)
  3402. CGI->arth->unequipArtifact(hero->artifWorn, slotID);
  3403. else
  3404. hero->artifacts.erase(hero->artifacts.begin() + (slotID - 19));
  3405. ourOwner->markPossibleSlots(ourArt);
  3406. hero->recreateArtBonuses();
  3407. // Update the hero bonuses.
  3408. CHeroWindow* chw = dynamic_cast<CHeroWindow*>(GH.topInt());
  3409. if (chw != NULL) {
  3410. chw->deactivate();
  3411. chw->setHero(hero);
  3412. chw->activate();
  3413. } else {
  3414. CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt());
  3415. assert(cew); // Either an exchange- or hero window should be active if an artifact slot is selected.
  3416. cew->deactivate();
  3417. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  3418. {
  3419. if(cew->heroInst[g] == hero)
  3420. {
  3421. cew->artifs[g]->setHero(hero);
  3422. }
  3423. }
  3424. cew->prepareBackground();
  3425. cew->activate();
  3426. }
  3427. if (slotID >= 19) {
  3428. // Correcting position in backpack.
  3429. ourOwner->scrollBackpack(-(slotID - 19 < ourOwner->backpackPos));
  3430. } else {
  3431. ourOwner->eraseSlotData(this, slotID);
  3432. }
  3433. }
  3434. /**
  3435. * Deselects the artifact slot.
  3436. */
  3437. void CArtPlace::deselect ()
  3438. {
  3439. CGI->curh->dragAndDropCursor(NULL);
  3440. ourOwner->unmarkSlots();
  3441. }
  3442. void CArtPlace::deactivate()
  3443. {
  3444. if(active)
  3445. {
  3446. active = false;
  3447. //deactivateLClick();
  3448. LRClickableAreaWTextComp::deactivate();
  3449. }
  3450. }
  3451. void CArtPlace::show(SDL_Surface *to)
  3452. {
  3453. if (ourArt)
  3454. blitAt(graphics->artDefs->ourImages[ourArt->id].bitmap, pos.x, pos.y, to);
  3455. if(marked && active)
  3456. {
  3457. // Draw vertical bars.
  3458. for (int i = 0; i < pos.h; ++i) {
  3459. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x, pos.y + i, 240, 220, 120);
  3460. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + pos.w - 1, pos.y + i, 240, 220, 120);
  3461. }
  3462. // Draw horizontal bars.
  3463. for (int i = 0; i < pos.w; ++i) {
  3464. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y, 240, 220, 120);
  3465. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y + pos.h - 1, 240, 220, 120);
  3466. }
  3467. }
  3468. }
  3469. bool CArtPlace::fitsHere(const CArtifact * art)
  3470. {
  3471. // You can place 'no artifact' anywhere.
  3472. if(!art)
  3473. return true;
  3474. // Anything can but War Machines can be placed in backpack.
  3475. if (slotID >= 19)
  3476. return !CGI->arth->isBigArtifact(art->id);
  3477. return art->fitsAt(ourOwner->curHero->artifWorn, slotID);
  3478. }
  3479. CArtPlace::~CArtPlace()
  3480. {
  3481. deactivate();
  3482. }
  3483. void HoverableArea::hover (bool on)
  3484. {
  3485. if (on)
  3486. GH.statusbar->print(hoverText);
  3487. else if (GH.statusbar->getCurrent()==hoverText)
  3488. GH.statusbar->clear();
  3489. }
  3490. void HoverableArea::activate()
  3491. {
  3492. activateHover();
  3493. }
  3494. void HoverableArea::deactivate()
  3495. {
  3496. deactivateHover();
  3497. }
  3498. void LClickableArea::activate()
  3499. {
  3500. activateLClick();
  3501. }
  3502. void LClickableArea::deactivate()
  3503. {
  3504. deactivateLClick();
  3505. }
  3506. void LClickableArea::clickLeft(tribool down, bool previousState)
  3507. {
  3508. //if(!down)
  3509. //{
  3510. // LOCPLINT->showInfoDialog("TEST TEST AAA", std::vector<SComponent*>());
  3511. //}
  3512. }
  3513. void RClickableArea::activate()
  3514. {
  3515. activateRClick();
  3516. }
  3517. void RClickableArea::deactivate()
  3518. {
  3519. deactivateRClick();
  3520. }
  3521. void RClickableArea::clickRight(tribool down, bool previousState)
  3522. {
  3523. //if(!down)
  3524. //{
  3525. // LOCPLINT->showInfoDialog("TEST TEST AAA", std::vector<SComponent*>());
  3526. //}
  3527. }
  3528. void LRClickableAreaWText::clickLeft(tribool down, bool previousState)
  3529. {
  3530. if(!down && previousState)
  3531. {
  3532. LOCPLINT->showInfoDialog(text, std::vector<SComponent*>(), soundBase::sound_todo);
  3533. }
  3534. //ClickableL::clickLeft(down);
  3535. }
  3536. void LRClickableAreaWText::clickRight(tribool down, bool previousState)
  3537. {
  3538. LOCPLINT->adventureInt->handleRightClick(text, down, this);
  3539. }
  3540. void LRClickableAreaWText::activate()
  3541. {
  3542. LClickableArea::activate();
  3543. RClickableArea::activate();
  3544. activateHover();
  3545. }
  3546. void LRClickableAreaWText::deactivate()
  3547. {
  3548. LClickableArea::deactivate();
  3549. RClickableArea::deactivate();
  3550. deactivateHover();
  3551. }
  3552. void LClickableAreaHero::clickLeft(tribool down, bool previousState)
  3553. {
  3554. if(!down)
  3555. {
  3556. owner->deactivate();
  3557. const CGHeroInstance * buf = LOCPLINT->getWHero(id);
  3558. owner->setHero(buf);
  3559. owner->redrawCurBack();
  3560. owner->activate();
  3561. }
  3562. }
  3563. void LRClickableAreaWTextComp::clickLeft(tribool down, bool previousState)
  3564. {
  3565. if((!down) && previousState)
  3566. {
  3567. std::vector<SComponent*> comp(1, new SComponent(SComponent::Etype(baseType), type, bonus));
  3568. LOCPLINT->showInfoDialog(text, comp, soundBase::sound_todo);
  3569. }
  3570. //ClickableL::clickLeft(down);
  3571. }
  3572. void LRClickableAreaWTextComp::clickRight(tribool down, bool previousState)
  3573. {
  3574. LOCPLINT->adventureInt->handleRightClick(text, down, this);
  3575. }
  3576. void LRClickableAreaWTextComp::activate()
  3577. {
  3578. LClickableArea::activate();
  3579. RClickableArea::activate();
  3580. activateHover();
  3581. }
  3582. void LRClickableAreaWTextComp::deactivate()
  3583. {
  3584. LClickableArea::deactivate();
  3585. RClickableArea::deactivate();
  3586. deactivateHover();
  3587. }
  3588. void LRClickableAreaOpenHero::clickLeft(tribool down, bool previousState)
  3589. {
  3590. if((!down) && previousState && hero)
  3591. LOCPLINT->openHeroWindow(hero);
  3592. }
  3593. void LRClickableAreaOpenHero::clickRight(tribool down, bool previousState)
  3594. {
  3595. if((!down) && previousState && hero)
  3596. LOCPLINT->openHeroWindow(hero);
  3597. }
  3598. void LRClickableAreaOpenTown::clickLeft(tribool down, bool previousState)
  3599. {
  3600. if((!down) && previousState && town)
  3601. LOCPLINT->openTownWindow(town);
  3602. }
  3603. void LRClickableAreaOpenTown::clickRight(tribool down, bool previousState)
  3604. {
  3605. if((!down) && previousState && town)
  3606. LOCPLINT->openTownWindow(town);
  3607. }
  3608. void CArtifactsOfHero::activate()
  3609. {
  3610. for(size_t f=0; f<artWorn.size(); ++f)
  3611. {
  3612. if(artWorn[f])
  3613. artWorn[f]->activate();
  3614. }
  3615. for(size_t f=0; f<backpack.size(); ++f)
  3616. {
  3617. if(backpack[f])
  3618. backpack[f]->activate();
  3619. }
  3620. leftArtRoll->activate();
  3621. rightArtRoll->activate();
  3622. }
  3623. void CArtifactsOfHero::deactivate()
  3624. {
  3625. for(size_t f=0; f<artWorn.size(); ++f)
  3626. {
  3627. if(artWorn[f])
  3628. artWorn[f]->deactivate();
  3629. }
  3630. for(size_t f=0; f<backpack.size(); ++f)
  3631. {
  3632. if(backpack[f])
  3633. backpack[f]->deactivate();
  3634. }
  3635. leftArtRoll->deactivate();
  3636. rightArtRoll->deactivate();
  3637. }
  3638. void CArtifactsOfHero::show(SDL_Surface * to)
  3639. {
  3640. for(size_t d=0; d<artWorn.size(); ++d)
  3641. {
  3642. artWorn[d]->show(to);
  3643. }
  3644. for(size_t d=0; d<backpack.size(); ++d)
  3645. {
  3646. backpack[d]->show(to);
  3647. }
  3648. leftArtRoll->show(to);
  3649. rightArtRoll->show(to);
  3650. }
  3651. void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
  3652. {
  3653. // An update is made, rather than initialization.
  3654. if (curHero == hero) {
  3655. // Compensate backpack pos if an artifact was insertad before it.
  3656. if (commonInfo->destSlotID >= 19 && commonInfo->destAOH == this
  3657. && commonInfo->destSlotID - 19 < backpackPos)
  3658. {
  3659. backpackPos++;
  3660. }
  3661. if (updateState && commonInfo->srcAOH == this) {
  3662. curHero = hero;
  3663. // A swap was made, make the replaced artifact the current selected.
  3664. if (commonInfo->destSlotID < 19 && commonInfo->destArtifact) {
  3665. // Temporarily remove artifact from hero.
  3666. CGHeroInstance * nonconstCurHero = const_cast<CGHeroInstance *>(curHero);
  3667. if (commonInfo->srcSlotID < 19)
  3668. CGI->arth->unequipArtifact(nonconstCurHero->artifWorn, commonInfo->srcSlotID);
  3669. else
  3670. nonconstCurHero->artifacts.erase(nonconstCurHero->artifacts.begin() + (commonInfo->srcSlotID - 19));
  3671. nonconstCurHero->recreateArtBonuses();
  3672. // Source <- Dest
  3673. commonInfo->srcArtifact = commonInfo->destArtifact;
  3674. // Reset destination parameters.
  3675. commonInfo->destAOH = NULL;
  3676. commonInfo->destArtifact = NULL;
  3677. commonInfo->destSlotID = -1;
  3678. CGI->curh->dragAndDropCursor(
  3679. graphics->artDefs->ourImages[commonInfo->srcArtifact->id].bitmap);
  3680. markPossibleSlots(commonInfo->srcArtifact);
  3681. } else if (commonInfo->destAOH != NULL) {
  3682. // Reset all parameters.
  3683. commonInfo->srcAOH = NULL;
  3684. commonInfo->srcArtifact = NULL;
  3685. commonInfo->srcSlotID = -1;
  3686. commonInfo->destAOH = NULL;
  3687. commonInfo->destArtifact = NULL;
  3688. commonInfo->destSlotID = -1;
  3689. CGI->curh->dragAndDropCursor(NULL);
  3690. unmarkSlots();
  3691. }
  3692. }
  3693. } else {
  3694. rollback();
  3695. }
  3696. curHero = hero;
  3697. if (curHero->artifacts.size() > 0)
  3698. backpackPos %= curHero->artifacts.size();
  3699. else
  3700. backpackPos = 0;
  3701. // Fill the slots for worn artifacts and backpack.
  3702. for (int g = 0; g < 19 ; g++)
  3703. setSlotData(artWorn[g], g);
  3704. scrollBackpack(0);
  3705. //blocking scrolling if there is not enough artifacts to scroll
  3706. leftArtRoll->block(curHero->artifacts.size() <= backpack.size());
  3707. rightArtRoll->block(curHero->artifacts.size() <= backpack.size());
  3708. }
  3709. /**
  3710. * Any held artifacts, marked slots etc. will be restored to it's original way.
  3711. */
  3712. void CArtifactsOfHero::rollback()
  3713. {
  3714. if (curHero != NULL) {
  3715. // Restore any held artifact to it's original position.
  3716. if (commonInfo->srcArtifact && commonInfo->srcAOH == this) {
  3717. CGHeroInstance * hero = const_cast<CGHeroInstance *>(curHero);
  3718. if (commonInfo->srcSlotID != -1) {
  3719. // Put a held artifact back to it's spot.
  3720. if (commonInfo->srcSlotID < 19)
  3721. CGI->arth->equipArtifact(hero->artifWorn, commonInfo->srcSlotID, commonInfo->srcArtifact->id);
  3722. else
  3723. hero->artifacts.insert(hero->artifacts.begin() + (commonInfo->srcSlotID - 19), commonInfo->srcArtifact->id);
  3724. } else { // Held swapped artifact.
  3725. // Wear the artifact in a suitable spot.
  3726. ui16 i = 0;
  3727. for (; i < 19; i++) {
  3728. if (artWorn[i]->fitsHere(commonInfo->srcArtifact)
  3729. && curHero->artifWorn.find(i) == curHero->artifWorn.end())
  3730. {
  3731. CGI->arth->equipArtifact(hero->artifWorn, i, commonInfo->srcArtifact->id);
  3732. break;
  3733. }
  3734. }
  3735. // If it can't be worn, put it in the backpack.
  3736. if (i == 19)
  3737. hero->artifacts.push_back(commonInfo->srcArtifact->id);
  3738. }
  3739. hero->recreateArtBonuses();
  3740. }
  3741. }
  3742. unmarkSlots();
  3743. backpackPos = 0;
  3744. commonInfo->srcAOH = NULL;
  3745. commonInfo->srcArtifact = NULL;
  3746. commonInfo->srcSlotID = -1;
  3747. commonInfo->destAOH = NULL;
  3748. commonInfo->destArtifact = NULL;
  3749. commonInfo->destSlotID = -1;
  3750. CGI->curh->dragAndDropCursor(NULL);
  3751. }
  3752. void CArtifactsOfHero::dispose()
  3753. {
  3754. curHero = NULL;
  3755. }
  3756. void CArtifactsOfHero::scrollBackpack(int dir)
  3757. {
  3758. backpackPos += dir;
  3759. if (curHero->artifacts.size() > 0) {
  3760. if (backpackPos < 0) { // No guarantee of modulus behavior with negative operands.
  3761. do {
  3762. backpackPos += curHero->artifacts.size();
  3763. } while (backpackPos < 0);
  3764. } else {
  3765. backpackPos %= curHero->artifacts.size();
  3766. }
  3767. }
  3768. //set new data
  3769. for (size_t s = 0; s < backpack.size(); ++s) {
  3770. if (s < curHero->artifacts.size())
  3771. setSlotData(backpack[s], 19 + (s + backpackPos)%curHero->artifacts.size());
  3772. else
  3773. eraseSlotData(backpack[s], 19 + s);
  3774. }
  3775. }
  3776. /**
  3777. * Marks possible slots where a given artifact can be placed, except backpack.
  3778. *
  3779. * @param art Artifact checked against.
  3780. */
  3781. void CArtifactsOfHero::markPossibleSlots (const CArtifact* art)
  3782. {
  3783. for (std::set<CArtifactsOfHero *>::iterator it = commonInfo->participants.begin();
  3784. it != commonInfo->participants.end();
  3785. ++it)
  3786. {
  3787. for (int i = 0; i < (*it)->artWorn.size(); i++) {
  3788. if ((*it)->artWorn[i]->fitsHere(art))
  3789. (*it)->artWorn[i]->marked = true;
  3790. else
  3791. (*it)->artWorn[i]->marked = false;
  3792. }
  3793. }
  3794. }
  3795. /**
  3796. * Unamarks all slots.
  3797. */
  3798. void CArtifactsOfHero::unmarkSlots ()
  3799. {
  3800. for (std::set<CArtifactsOfHero *>::iterator it = commonInfo->participants.begin();
  3801. it != commonInfo->participants.end();
  3802. ++it)
  3803. {
  3804. for (int i = 0; i < (*it)->artWorn.size(); i++) {
  3805. (*it)->artWorn[i]->marked = false;
  3806. }
  3807. }
  3808. }
  3809. /**
  3810. * Assigns an artifacts to an artifact place depending on it's new slot ID.
  3811. */
  3812. void CArtifactsOfHero::setSlotData (CArtPlace* artPlace, int slotID)
  3813. {
  3814. artPlace->slotID = slotID;
  3815. artPlace->ourArt = curHero->getArt(slotID);
  3816. if (artPlace->ourArt) {
  3817. artPlace->text = artPlace->ourArt->Description();
  3818. if (artPlace->locked()) // Locks should appear as empty.
  3819. artPlace->hoverText = CGI->generaltexth->allTexts[507];
  3820. else
  3821. artPlace->hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1].c_str()) % artPlace->ourArt->Name().c_str());
  3822. } else {
  3823. eraseSlotData(artPlace, slotID);
  3824. }
  3825. }
  3826. /**
  3827. * Makes given artifact slot appear as empty with a certain slot ID.
  3828. */
  3829. void CArtifactsOfHero::eraseSlotData (CArtPlace* artPlace, int slotID)
  3830. {
  3831. artPlace->slotID = slotID;
  3832. artPlace->ourArt = NULL;
  3833. artPlace->text = std::string();
  3834. artPlace->hoverText = CGI->generaltexth->allTexts[507];
  3835. }
  3836. CArtifactsOfHero::CArtifactsOfHero(const SDL_Rect & position) :
  3837. backpackPos(0), updateState(false)
  3838. {
  3839. pos = position;
  3840. artWorn.resize(19);
  3841. std::vector<SDL_Rect> slotPos;
  3842. 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),
  3843. 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),
  3844. 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),
  3845. 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),
  3846. 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),
  3847. 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),
  3848. genRect(44,44,pos.x+381,pos.y+296);
  3849. // Create slots for worn artifacts.
  3850. for (int g = 0; g < 19 ; g++)
  3851. {
  3852. artWorn[g] = new CArtPlace(NULL);
  3853. artWorn[g]->pos = slotPos[g];
  3854. artWorn[g]->ourOwner = this;
  3855. eraseSlotData(artWorn[g], g);
  3856. }
  3857. // Create slots for the backpack.
  3858. for(size_t s=0; s<5; ++s)
  3859. {
  3860. CArtPlace * add = new CArtPlace(NULL);
  3861. add->ourOwner = this;
  3862. add->pos.x = pos.x + 403 + 46*s;
  3863. add->pos.y = pos.y + 365;
  3864. add->pos.h = add->pos.w = 44;
  3865. eraseSlotData(add, 19 + s);
  3866. backpack.push_back(add);
  3867. }
  3868. leftArtRoll = new AdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,-1), pos.x+379, pos.y+364, "hsbtns3.def", SDLK_LEFT);
  3869. rightArtRoll = new AdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,+1), pos.x+632, pos.y+364, "hsbtns5.def", SDLK_RIGHT);
  3870. }
  3871. CArtifactsOfHero::~CArtifactsOfHero()
  3872. {
  3873. dispose();
  3874. for(size_t g=0; g<artWorn.size(); ++g)
  3875. {
  3876. delete artWorn[g];
  3877. artWorn[g] = NULL;
  3878. }
  3879. for(size_t g=0; g<backpack.size(); ++g)
  3880. {
  3881. delete backpack[g];
  3882. backpack[g] = NULL;
  3883. }
  3884. backpack.clear();
  3885. artWorn.clear();
  3886. delete leftArtRoll;
  3887. delete rightArtRoll;
  3888. }
  3889. void CExchangeWindow::close()
  3890. {
  3891. GH.popIntTotally(this);
  3892. }
  3893. void CExchangeWindow::activate()
  3894. {
  3895. quit->activate();
  3896. garr->activate();
  3897. artifs[0]->setHero(heroInst[0]);
  3898. artifs[0]->activate();
  3899. artifs[1]->setHero(heroInst[1]);
  3900. artifs[1]->activate();
  3901. for(int g=0; g<ARRAY_COUNT(secSkillAreas); g++)
  3902. {
  3903. for(int b=0; b<secSkillAreas[g].size(); ++b)
  3904. {
  3905. secSkillAreas[g][b]->activate();
  3906. }
  3907. }
  3908. for(int b=0; b<primSkillAreas.size(); ++b)
  3909. primSkillAreas[b]->activate();
  3910. GH.statusbar = ourBar;
  3911. for(int g=0; g<ARRAY_COUNT(questlogButton); g++)
  3912. questlogButton[g]->activate();
  3913. for(int g=0; g<ARRAY_COUNT(morale); g++)
  3914. morale[g]->activate();
  3915. for(int g=0; g<ARRAY_COUNT(luck); g++)
  3916. luck[g]->activate();
  3917. for(int g=0; g<ARRAY_COUNT(portrait); g++)
  3918. portrait[g]->activate();
  3919. for(int g=0; g<ARRAY_COUNT(spellPoints); g++)
  3920. spellPoints[g]->activate();
  3921. for(int g=0; g<ARRAY_COUNT(experience); g++)
  3922. experience[g]->activate();
  3923. for(int g=0; g<ARRAY_COUNT(speciality); g++)
  3924. speciality[g]->activate();
  3925. }
  3926. void CExchangeWindow::deactivate()
  3927. {
  3928. quit->deactivate();
  3929. garr->deactivate();
  3930. artifs[0]->deactivate();
  3931. artifs[1]->deactivate();
  3932. for(int g=0; g<ARRAY_COUNT(secSkillAreas); g++)
  3933. {
  3934. for(int b=0; b<secSkillAreas[g].size(); ++b)
  3935. {
  3936. secSkillAreas[g][b]->deactivate();
  3937. }
  3938. }
  3939. for(int b=0; b<primSkillAreas.size(); ++b)
  3940. primSkillAreas[b]->deactivate();
  3941. for(int g=0; g<ARRAY_COUNT(questlogButton); g++)
  3942. questlogButton[g]->deactivate();
  3943. for(int g=0; g<ARRAY_COUNT(morale); g++)
  3944. morale[g]->deactivate();
  3945. for(int g=0; g<ARRAY_COUNT(luck); g++)
  3946. luck[g]->deactivate();
  3947. for(int g=0; g<ARRAY_COUNT(portrait); g++)
  3948. portrait[g]->deactivate();
  3949. for(int g=0; g<ARRAY_COUNT(spellPoints); g++)
  3950. spellPoints[g]->deactivate();
  3951. for(int g=0; g<ARRAY_COUNT(experience); g++)
  3952. experience[g]->deactivate();
  3953. for(int g=0; g<ARRAY_COUNT(speciality); g++)
  3954. speciality[g]->deactivate();
  3955. }
  3956. void CExchangeWindow::show(SDL_Surface * to)
  3957. {
  3958. blitAt(bg, pos, to);
  3959. quit->show(to);
  3960. //printing border around window
  3961. if(screen->w != 800 || screen->h !=600)
  3962. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  3963. artifs[0]->show(to);
  3964. artifs[1]->show(to);
  3965. ourBar->show(to);
  3966. for(int g=0; g<ARRAY_COUNT(secSkillAreas); g++)
  3967. {
  3968. questlogButton[g]->show(to);
  3969. }
  3970. garr->show(to);
  3971. }
  3972. void CExchangeWindow::questlog(int whichHero)
  3973. {
  3974. CGI->curh->dragAndDropCursor(NULL);
  3975. }
  3976. void CExchangeWindow::prepareBackground()
  3977. {
  3978. if(bg)
  3979. SDL_FreeSurface(bg);
  3980. SDL_Surface * bgtemp; //loaded as 8bpp surface
  3981. bgtemp = BitmapHandler::loadBitmap("TRADE2.BMP");
  3982. graphics->blueToPlayersAdv(bgtemp, heroInst[0]->tempOwner);
  3983. bg = SDL_ConvertSurface(bgtemp, screen->format, screen->flags); //to 24 bpp
  3984. SDL_FreeSurface(bgtemp);
  3985. //printing heroes' names and levels
  3986. std::ostringstream os, os2;
  3987. os<<heroInst[0]->name<<", Level "<<heroInst[0]->level<<" "<<heroInst[0]->type->heroClass->name;
  3988. CSDL_Ext::printAtMiddle(os.str(), 147, 25, FONT_SMALL, zwykly, bg);
  3989. os2<<heroInst[1]->name<<", Level "<<heroInst[1]->level<<" "<<heroInst[1]->type->heroClass->name;
  3990. CSDL_Ext::printAtMiddle(os2.str(), 653, 25, FONT_SMALL, zwykly, bg);
  3991. //printing primary skills
  3992. CDefHandler * skilldef = CDefHandler::giveDef("PSKIL32.DEF");
  3993. for(int g=0; g<4; ++g)
  3994. {
  3995. //graphics
  3996. blitAt(skilldef->ourImages[g].bitmap, genRect(32, 32, 385, 19 + 36 * g), bg);
  3997. }
  3998. CDefHandler * un32 = CDefHandler::giveDef("UN32.DEF");
  3999. //heroes related thing
  4000. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  4001. {
  4002. //printing primary skills' amounts
  4003. for(int m=0; m<4; ++m)
  4004. {
  4005. std::ostringstream primarySkill;
  4006. primarySkill<<heroInst[b]->getPrimSkillLevel(m);
  4007. CSDL_Ext::printAtMiddle(primarySkill.str(), 352 + 93 * b, 35 + 36 * m, FONT_SMALL, zwykly, bg);
  4008. }
  4009. //printing secondary skills
  4010. for(int m=0; m<heroInst[b]->secSkills.size(); ++m)
  4011. {
  4012. blitAt(graphics->abils32->ourImages[heroInst[b]->secSkills[m].first * 3 + heroInst[b]->secSkills[m].second + 2].bitmap, genRect(32, 32, 32 + 36 * m + 454 * b, 88), bg);
  4013. }
  4014. //hero's specialty
  4015. blitAt(un32->ourImages[heroInst[b]->subID].bitmap, 67 + 490*b, 45, bg);
  4016. //experience
  4017. blitAt(skilldef->ourImages[4].bitmap, 103 + 490*b, 45, bg);
  4018. printAtMiddle( makeNumberShort(heroInst[b]->exp), 119 + 490*b, 71, FONT_SMALL, zwykly, bg );
  4019. //mana points
  4020. blitAt(skilldef->ourImages[5].bitmap, 139 + 490*b, 45, bg);
  4021. printAtMiddle( makeNumberShort(heroInst[b]->mana), 155 + 490*b, 71, FONT_SMALL, zwykly, bg );
  4022. //setting morale
  4023. blitAt(graphics->morale30->ourImages[heroInst[b]->getCurrentMorale()+3].bitmap, 177 + 490*b, 45, bg);
  4024. //setting luck
  4025. blitAt(graphics->luck30->ourImages[heroInst[b]->getCurrentLuck()+3].bitmap, 213 + 490*b, 45, bg);
  4026. }
  4027. //printing portraits
  4028. blitAt(graphics->portraitLarge[heroInst[0]->portrait], 257, 13, bg);
  4029. blitAt(graphics->portraitLarge[heroInst[1]->portrait], 485, 13, bg);
  4030. delete un32;
  4031. delete skilldef;
  4032. }
  4033. CExchangeWindow::CExchangeWindow(si32 hero1, si32 hero2) : bg(NULL)
  4034. {
  4035. char bufor[400];
  4036. heroInst[0] = LOCPLINT->cb->getHeroInfo(hero1, 2);
  4037. heroInst[1] = LOCPLINT->cb->getHeroInfo(hero2, 2);
  4038. prepareBackground();
  4039. pos.x = screen->w/2 - bg->w/2;
  4040. pos.y = screen->h/2 - bg->h/2;
  4041. pos.w = screen->w;
  4042. pos.h = screen->h;
  4043. artifs[0] = new CArtifactsOfHero(genRect(600, 800, pos.x + -334, pos.y + 150));
  4044. artifs[0]->commonInfo = new CArtifactsOfHero::SCommonPart;
  4045. artifs[0]->commonInfo->participants.insert(artifs[0]);
  4046. artifs[0]->setHero(heroInst[0]);
  4047. artifs[1] = new CArtifactsOfHero(genRect(600, 800, pos.x + 96, pos.y + 150));
  4048. artifs[1]->commonInfo = artifs[0]->commonInfo;
  4049. artifs[1]->commonInfo->participants.insert(artifs[1]);
  4050. artifs[1]->setHero(heroInst[1]);
  4051. //primary skills
  4052. for(int g=0; g<4; ++g)
  4053. {
  4054. //primary skill's clickable areas
  4055. primSkillAreas.push_back(new LRClickableAreaWTextComp());
  4056. primSkillAreas[g]->pos = genRect(32, 140, pos.x+329, pos.y + 19 + 36 * g);
  4057. primSkillAreas[g]->text = CGI->generaltexth->arraytxt[2+g];
  4058. primSkillAreas[g]->type = g;
  4059. primSkillAreas[g]->bonus = -1;
  4060. primSkillAreas[g]->baseType = 0;
  4061. sprintf(bufor, CGI->generaltexth->heroscrn[1].c_str(), CGI->generaltexth->primarySkillNames[g].c_str());
  4062. primSkillAreas[g]->hoverText = std::string(bufor);
  4063. }
  4064. //heroes related thing
  4065. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  4066. {
  4067. //secondary skill's clickable areas
  4068. for(int g=0; g<heroInst[b]->secSkills.size(); ++g)
  4069. {
  4070. int skill = heroInst[b]->secSkills[g].first,
  4071. level = heroInst[b]->secSkills[g].second; // <1, 3>
  4072. secSkillAreas[b].push_back(new LRClickableAreaWTextComp());
  4073. secSkillAreas[b][g]->pos = genRect(32, 32, pos.x + 32 + g*36 + b*454 , pos.y + 88);
  4074. secSkillAreas[b][g]->baseType = 1;
  4075. secSkillAreas[b][g]->type = skill;
  4076. secSkillAreas[b][g]->bonus = level;
  4077. secSkillAreas[b][g]->text = CGI->generaltexth->skillInfoTexts[skill][level-1];
  4078. sprintf(bufor, CGI->generaltexth->heroscrn[21].c_str(), CGI->generaltexth->levels[level - 1].c_str(), CGI->generaltexth->skillName[skill].c_str());
  4079. secSkillAreas[b][g]->hoverText = std::string(bufor);
  4080. }
  4081. portrait[b] = new LRClickableAreaOpenHero();
  4082. portrait[b]->pos = genRect(64, 58, pos.x + 257 + 228*b, pos.y + 13);
  4083. portrait[b]->hero = heroInst[b];
  4084. sprintf(bufor, CGI->generaltexth->allTexts[15].c_str(), heroInst[b]->name.c_str(), heroInst[b]->type->heroClass->name.c_str());
  4085. portrait[b]->hoverText = std::string(bufor);
  4086. speciality[b] = new LRClickableAreaWText();
  4087. speciality[b]->pos = genRect(32, 32, pos.x + 69 + 490*b, pos.y + 45);
  4088. speciality[b]->hoverText = CGI->generaltexth->heroscrn[27];
  4089. speciality[b]->text = CGI->generaltexth->hTxts[heroInst[b]->subID].longBonus;
  4090. experience[b] = new LRClickableAreaWText();
  4091. experience[b]->pos = genRect(32, 32, pos.x + 105 + 490*b, pos.y + 45);
  4092. experience[b]->hoverText = CGI->generaltexth->heroscrn[9];
  4093. experience[b]->text = CGI->generaltexth->allTexts[2].c_str();
  4094. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->level));
  4095. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(heroInst[b]->level+1)));
  4096. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->exp));
  4097. spellPoints[b] = new LRClickableAreaWText();
  4098. spellPoints[b]->pos = genRect(32, 32, pos.x + 141 + 490*b, pos.y + 45);
  4099. spellPoints[b]->hoverText = CGI->generaltexth->heroscrn[22];
  4100. sprintf(bufor, CGI->generaltexth->allTexts[205].c_str(), heroInst[b]->name.c_str(), heroInst[b]->mana, heroInst[b]->manaLimit());
  4101. spellPoints[b]->text = std::string(bufor);
  4102. //setting morale
  4103. morale[b] = new LRClickableAreaWTextComp();
  4104. morale[b]->pos = genRect(32, 32, pos.x + 177 + 490*b, pos.y + 45);
  4105. std::vector<std::pair<int,std::string> > mrl = heroInst[b]->getCurrentMoraleModifiers();
  4106. int mrlv = heroInst[b]->getCurrentMorale();
  4107. int mrlt = (mrlv>0)-(mrlv<0); //signum: -1 - bad morale[b], 0 - neutral, 1 - good
  4108. morale[b]->hoverText = CGI->generaltexth->heroscrn[4 - mrlt];
  4109. morale[b]->baseType = SComponent::morale;
  4110. morale[b]->bonus = mrlv;
  4111. morale[b]->text = CGI->generaltexth->arraytxt[88];
  4112. boost::algorithm::replace_first(morale[b]->text,"%s",CGI->generaltexth->arraytxt[86-mrlt]);
  4113. if (!mrl.size())
  4114. morale[b]->text += CGI->generaltexth->arraytxt[108];
  4115. else
  4116. for(int it=0; it < mrl.size(); it++)
  4117. morale[b]->text += "\n" + mrl[it].second;
  4118. //setting luck
  4119. luck[b] = new LRClickableAreaWTextComp();
  4120. luck[b]->pos = genRect(32, 32, pos.x + 213 + 490*b, pos.y + 45);
  4121. mrl = heroInst[b]->getCurrentLuckModifiers();
  4122. mrlv = heroInst[b]->getCurrentLuck();
  4123. mrlt = (mrlv>0)-(mrlv<0); //signum: -1 - bad luck[b], 0 - neutral, 1 - good
  4124. luck[b]->hoverText = CGI->generaltexth->heroscrn[7 - mrlt];
  4125. luck[b]->baseType = SComponent::luck;
  4126. luck[b]->bonus = mrlv;
  4127. luck[b]->text = CGI->generaltexth->arraytxt[62];
  4128. boost::algorithm::replace_first(luck[b]->text,"%s",CGI->generaltexth->arraytxt[60-mrlt]);
  4129. if (!mrl.size())
  4130. luck[b]->text += CGI->generaltexth->arraytxt[77];
  4131. else
  4132. for(int it=0; it < mrl.size(); it++)
  4133. luck[b]->text += "\n" + mrl[it].second;
  4134. }
  4135. //buttons
  4136. quit = new AdventureMapButton(CGI->generaltexth->tcommands[8], "", boost::bind(&CExchangeWindow::close, this), pos.x+732, pos.y+567, "IOKAY.DEF", SDLK_RETURN);
  4137. questlogButton[0] = new AdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CExchangeWindow::questlog,this, 0), pos.x+10, pos.y+44, "hsbtns4.def");
  4138. questlogButton[1] = new AdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CExchangeWindow::questlog,this, 1), pos.x+740, pos.y+44, "hsbtns4.def");
  4139. //statusbar
  4140. ourBar = new CStatusBar(pos.x + 3, pos.y + 577, "TSTATBAR.bmp", 726);
  4141. //garrison interface
  4142. garr = new CGarrisonInt(pos.x + 69, pos.y + 131, 4, Point(418,0), bg, Point(69,131), heroInst[0],heroInst[1], true, true);
  4143. garr->splitButtons.push_back(new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+10,pos.y+132,"TSBTNS.DEF"));
  4144. garr->splitButtons.push_back(new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+740,pos.y+132,"TSBTNS.DEF"));
  4145. }
  4146. CExchangeWindow::~CExchangeWindow() //d-tor
  4147. {
  4148. SDL_FreeSurface(bg);
  4149. delete quit;
  4150. //warning: don't experiment with these =NULL lines, they prevent heap corruption!
  4151. artifs[0]->rollback();
  4152. artifs[1]->rollback();
  4153. delete artifs[0]->commonInfo;
  4154. artifs[0]->commonInfo = NULL;
  4155. delete artifs[0];
  4156. artifs[1]->commonInfo = NULL;
  4157. delete artifs[1];
  4158. delete garr;
  4159. delete ourBar;
  4160. for(int g=0; g<ARRAY_COUNT(secSkillAreas); g++)
  4161. {
  4162. for(int b=0; b<secSkillAreas[g].size(); ++b)
  4163. {
  4164. delete secSkillAreas[g][b];
  4165. }
  4166. }
  4167. for(int b=0; b<primSkillAreas.size(); ++b)
  4168. {
  4169. delete primSkillAreas[b];
  4170. }
  4171. for(int g=0; g<ARRAY_COUNT(questlogButton); g++)
  4172. {
  4173. delete questlogButton[g];
  4174. }
  4175. for(int g=0; g<ARRAY_COUNT(morale); g++)
  4176. delete morale[g];
  4177. for(int g=0; g<ARRAY_COUNT(luck); g++)
  4178. delete luck[g];
  4179. for(int g=0; g<ARRAY_COUNT(portrait); g++)
  4180. delete portrait[g];
  4181. for(int g=0; g<ARRAY_COUNT(spellPoints); g++)
  4182. delete spellPoints[g];
  4183. for(int g=0; g<ARRAY_COUNT(experience); g++)
  4184. delete experience[g];
  4185. for(int g=0; g<ARRAY_COUNT(speciality); g++)
  4186. delete speciality[g];
  4187. }
  4188. void CShipyardWindow::activate()
  4189. {
  4190. build->activate();
  4191. quit->activate();
  4192. }
  4193. void CShipyardWindow::deactivate()
  4194. {
  4195. build->deactivate();
  4196. quit->deactivate();
  4197. }
  4198. void CShipyardWindow::show( SDL_Surface * to )
  4199. {
  4200. blitAt(bg,pos,to);
  4201. CSDL_Ext::blit8bppAlphaTo24bpp(graphics->boatAnims[boat]->ourImages[21 + frame++/8%8].bitmap, NULL, to, &genRect(64, 96, pos.x+110, pos.y+85));
  4202. build->show(to);
  4203. quit->show(to);
  4204. }
  4205. CShipyardWindow::~CShipyardWindow()
  4206. {
  4207. delete build;
  4208. delete quit;
  4209. }
  4210. CShipyardWindow::CShipyardWindow(const std::vector<si32> &cost, int state, int boatType, const boost::function<void()> &onBuy)
  4211. {
  4212. boat = boatType;
  4213. frame = 0;
  4214. SDL_Surface * bgtemp; //loaded as 8bpp surface
  4215. bgtemp = BitmapHandler::loadBitmap("TPSHIP.bmp");
  4216. pos.x = screen->w/2 - bgtemp->w/2;
  4217. pos.y = screen->h/2 - bgtemp->h/2;
  4218. pos.w = bgtemp->w;
  4219. pos.h = bgtemp->h;
  4220. SDL_SetColorKey(bgtemp,SDL_SRCCOLORKEY,SDL_MapRGB(bgtemp->format,0,255,255));
  4221. graphics->blueToPlayersAdv(bgtemp, LOCPLINT->playerID);
  4222. bg = SDL_ConvertSurface(bgtemp, screen->format, screen->flags); //to 24 bpp
  4223. SDL_FreeSurface(bgtemp);
  4224. bgtemp = BitmapHandler::loadBitmap("TPSHIPBK.bmp");
  4225. blitAt(bgtemp, 100, 69, bg);
  4226. SDL_FreeSurface(bgtemp);
  4227. // Draw resource icons and costs.
  4228. std::string goldCost = boost::lexical_cast<std::string>(1000);
  4229. std::string woodCost = boost::lexical_cast<std::string>(10);
  4230. blitAt(graphics->resources32->ourImages[6].bitmap, 100, 244, bg);
  4231. printAtMiddle(goldCost.c_str(), 118, 294, FONT_SMALL, zwykly, bg);
  4232. blitAt(graphics->resources32->ourImages[0].bitmap, 196, 244, bg);
  4233. printAtMiddle(woodCost.c_str(), 212, 294, FONT_SMALL, zwykly, bg);
  4234. bool affordable = true;
  4235. for(int i = 0; i < cost.size(); i++)
  4236. {
  4237. if(cost[i] > LOCPLINT->cb->getResourceAmount(i))
  4238. {
  4239. affordable = false;
  4240. break;
  4241. }
  4242. }
  4243. quit = new AdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), pos.x+224, pos.y+312, "ICANCEL.DEF", SDLK_RETURN);
  4244. build = new AdventureMapButton(CGI->generaltexth->allTexts[598], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), pos.x+42, pos.y+312, "IBY6432.DEF", SDLK_RETURN);
  4245. build->callback += onBuy;
  4246. if(!affordable)
  4247. build->block(true);
  4248. printAtMiddle(CGI->generaltexth->jktexts[13], 164, 27, FONT_BIG, tytulowy, bg); //Build A New Ship
  4249. printAtMiddle(CGI->generaltexth->jktexts[14], 164, 220, FONT_MEDIUM, zwykly, bg); //Resource cost:
  4250. }
  4251. CPuzzleWindow::CPuzzleWindow(const int3 &grailPos, float discoveredRatio)
  4252. :animCount(0)
  4253. {
  4254. SDL_Surface * back = BitmapHandler::loadBitmap("PUZZLE.BMP", false);
  4255. graphics->blueToPlayersAdv(back, LOCPLINT->playerID);
  4256. //make transparency black
  4257. back->format->palette->colors[0].b = back->format->palette->colors[0].r = back->format->palette->colors[0].g = 0;
  4258. //the rest
  4259. background = SDL_ConvertSurface(back, screen->format, back->flags);
  4260. SDL_FreeSurface(back);
  4261. pos = genRect(background->h, background->w, (conf.cc.resx - background->w) / 2, (conf.cc.resy - background->h) / 2);
  4262. quitb = new AdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), pos.x+670, pos.y+538, "IOK6432.DEF", SDLK_RETURN);
  4263. resdatabar = new CResDataBar("ZRESBAR.bmp", pos.x+3, pos.y+575, 32, 2, 85, 85);
  4264. resdatabar->pos.x = pos.x+3; resdatabar->pos.y = pos.y+575;
  4265. //printing necessary thinks to background
  4266. CGI->mh->terrainRect
  4267. (grailPos, LOCPLINT->adventureInt->anim,
  4268. &LOCPLINT->cb->getVisibilityMap(), true, LOCPLINT->adventureInt->heroAnim,
  4269. background, &genRect(544, 591, 8, 8), 0, 0, true);
  4270. int faction = LOCPLINT->cb->getStartInfo()->playerInfos[LOCPLINT->serialID].castle;
  4271. std::vector<SPuzzleInfo> puzzlesToPrint;
  4272. for(int g=0; g<PUZZLES_PER_FACTION; ++g)
  4273. {
  4274. if(CGI->heroh->puzzleInfo[faction][g].whenUncovered > PUZZLES_PER_FACTION * discoveredRatio)
  4275. {
  4276. puzzlesToPrint.push_back(CGI->heroh->puzzleInfo[faction][g]);
  4277. }
  4278. else
  4279. {
  4280. SDL_Surface *buf = BitmapHandler::loadBitmap(CGI->heroh->puzzleInfo[faction][g].filename);
  4281. puzzlesToPullBack.push_back( std::make_pair(buf, &CGI->heroh->puzzleInfo[faction][g]) );
  4282. }
  4283. }
  4284. for(int b = 0; b < puzzlesToPrint.size(); ++b)
  4285. {
  4286. SDL_Surface * puzzle = BitmapHandler::loadBitmap(puzzlesToPrint[b].filename);
  4287. blitAt(puzzle, puzzlesToPrint[b].x, puzzlesToPrint[b].y, background);
  4288. SDL_FreeSurface(puzzle);
  4289. }
  4290. }
  4291. CPuzzleWindow::~CPuzzleWindow()
  4292. {
  4293. delete quitb;
  4294. delete resdatabar;
  4295. SDL_FreeSurface(background);
  4296. for(int g = 0; g < puzzlesToPullBack.size(); ++g)
  4297. SDL_FreeSurface( puzzlesToPullBack[g].first );
  4298. }
  4299. void CPuzzleWindow::activate()
  4300. {
  4301. quitb->activate();
  4302. }
  4303. void CPuzzleWindow::deactivate()
  4304. {
  4305. quitb->deactivate();
  4306. }
  4307. void CPuzzleWindow::show(SDL_Surface * to)
  4308. {
  4309. blitAt(background, pos.x, pos.y, to);
  4310. quitb->show(to);
  4311. resdatabar->draw(to);
  4312. //blitting disappearing puzzles
  4313. for(int g=0; g<2; ++g)
  4314. if(animCount != 255)
  4315. ++animCount;
  4316. if(animCount != 255)
  4317. {
  4318. for(int b = 0; b < puzzlesToPullBack.size(); ++b)
  4319. {
  4320. int xPos = puzzlesToPullBack[b].second->x + pos.x,
  4321. yPos = puzzlesToPullBack[b].second->y + pos.y;
  4322. SDL_Surface *from = puzzlesToPullBack[b].first;
  4323. SDL_SetAlpha(from, SDL_SRCALPHA, 255 - animCount);
  4324. blitAt(from, xPos, yPos, to);
  4325. }
  4326. }
  4327. //disappearing puzzles blitted
  4328. //printing border around window
  4329. if(screen->w != 800 || screen->h !=600)
  4330. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  4331. }
  4332. bool CShopWindow::swapItem (ui16 which, bool choose)
  4333. {
  4334. bool itemFound = false;
  4335. if (choose == true) //choose item
  4336. {
  4337. if (chosen.count(which))
  4338. {
  4339. itemFound = true;
  4340. chosen[which] = available[which];
  4341. available.erase(which);
  4342. }
  4343. }
  4344. else //return item to available list
  4345. {
  4346. if (available.count(which))
  4347. {
  4348. itemFound = true;
  4349. available[which] = chosen[which];
  4350. chosen.erase(which);
  4351. }
  4352. }
  4353. return itemFound;
  4354. }
  4355. CArtMerchantWindow::CArtMerchantWindow()
  4356. {
  4357. }
  4358. CArtMerchantWindow::~CArtMerchantWindow(){}
  4359. void CArtMerchantWindow::activate() {}
  4360. void CArtMerchantWindow::deactivate() {}
  4361. void CArtMerchantWindow::show(SDL_Surface * to) {}
  4362. void CArtMerchantWindow::Buy() {}
  4363. void CThievesGuildWindow::activate()
  4364. {
  4365. CIntObject::activate();
  4366. GH.statusbar = statusBar;
  4367. }
  4368. void CThievesGuildWindow::show(SDL_Surface * to)
  4369. {
  4370. blitAt(background, pos.x, pos.y, to);
  4371. statusBar->show(to);
  4372. exitb->show(to);
  4373. resdatabar->show(to);
  4374. //showing border around window
  4375. if(screen->w != 800 || screen->h !=600)
  4376. {
  4377. CMessage::drawBorder(LOCPLINT->playerID, to, pos.w + 28, pos.h + 28, pos.x-14, pos.y-15);
  4378. }
  4379. }
  4380. void CThievesGuildWindow::bexitf()
  4381. {
  4382. GH.popIntTotally(this);
  4383. }
  4384. CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner)
  4385. :owner(_owner)
  4386. {
  4387. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4388. SThievesGuildInfo tgi; //info to be displayed
  4389. LOCPLINT->cb->getThievesGuildInfo(tgi, owner);
  4390. pos = center(Rect(0,0,800,600));// Rect( (conf.cc.resx - 800) / 2, (conf.cc.resy - 600) / 2, 800, 600 );
  4391. //loading backround and converting to more bpp form
  4392. SDL_Surface * bg = background = BitmapHandler::loadBitmap("TpRank.bmp", false);
  4393. background = newSurface(bg->w, bg->h);
  4394. blitAt(bg, 0, 0, background);
  4395. SDL_FreeSurface(bg);
  4396. exitb = new AdventureMapButton (std::string(), std::string(), boost::bind(&CThievesGuildWindow::bexitf,this), 748, 556, "HSBTNS.def", SDLK_RETURN);
  4397. statusBar = new CStatusBar(3, 555, "TStatBar.bmp", 742);
  4398. resdatabar = new CMinorResDataBar();
  4399. resdatabar->pos.x += pos.x - 3;
  4400. resdatabar->pos.y += pos.y;
  4401. static std::vector< std::list< ui8 > > SThievesGuildInfo::* fields[] = { &SThievesGuildInfo::numOfTowns, &SThievesGuildInfo::numOfHeroes, &SThievesGuildInfo::gold,
  4402. &SThievesGuildInfo::woodOre, &SThievesGuildInfo::mercSulfCrystGems, &SThievesGuildInfo::obelisks, &SThievesGuildInfo::artifacts, &SThievesGuildInfo::army,
  4403. &SThievesGuildInfo::income};
  4404. //printing texts & descriptions to background
  4405. for(int g=0; g<12; ++g)
  4406. {
  4407. int y;
  4408. if(g == 9) //best hero
  4409. {
  4410. y = 400;
  4411. }
  4412. else if(g == 10) //personality
  4413. {
  4414. y = 460;
  4415. }
  4416. else if(g == 11) //best monster
  4417. {
  4418. y = 510;
  4419. }
  4420. else
  4421. {
  4422. y = 52 + 32*g;
  4423. }
  4424. printAtMiddle(CGI->generaltexth->jktexts[24+g], 135, y, FONT_MEDIUM, tytulowy, background);
  4425. }
  4426. CDefHandler * strips = CDefHandler::giveDef("PRSTRIPS.DEF");
  4427. static const std::string colorToBox[] = {"PRRED.BMP", "PRBLUE.BMP", "PRTAN.BMP", "PRGREEN.BMP", "PRORANGE.BMP", "PRPURPLE.BMP", "PRTEAL.BMP", "PRPINK.BMP"};
  4428. for(int g=0; g<tgi.playerColors.size(); ++g)
  4429. {
  4430. if(g > 0)
  4431. {
  4432. blitAt(strips->ourImages[g-1].bitmap, 250 + 66*g, 7, background);
  4433. }
  4434. printAtMiddle(CGI->generaltexth->jktexts[16+g], 283 + 66*g, 20, FONT_MEDIUM, tytulowy, background);
  4435. SDL_Surface * box = BitmapHandler::loadBitmap(colorToBox[tgi.playerColors[g]]);
  4436. blitAt(box, 253 + 66*g, 334, background);
  4437. SDL_FreeSurface(box);
  4438. }
  4439. delete strips;
  4440. CDefHandler * flagPictures = CDefHandler::giveDef("itgflags.def");
  4441. //printing flags
  4442. for(int g=0; g<ARRAY_COUNT(fields); ++g) //by lines
  4443. {
  4444. for(int b=0; b<(tgi .* fields[g]).size(); ++b) //by places (1st, 2nd, ...)
  4445. {
  4446. std::list<ui8> players = (tgi .* fields[g])[b]; //get players with this place in this line
  4447. //std::sort(players.begin(), players.end());
  4448. int counter = 0;
  4449. for(std::list<ui8>::const_iterator it = players.begin(); it != players.end(); ++it)
  4450. {
  4451. int xpos = 259 + 66 * b + 12 * (counter % 4) + 6 * (counter / 4);
  4452. int ypos = 41 + 32 * g + 4 * (counter / 4);
  4453. blitAt(flagPictures->ourImages[*it].bitmap, xpos, ypos, background);
  4454. counter++;
  4455. }
  4456. }
  4457. }
  4458. delete flagPictures;
  4459. flagPictures = NULL;
  4460. //printing best hero
  4461. int counter = 0;
  4462. for(std::map<ui8, InfoAboutHero>::const_iterator it = tgi.colorToBestHero.begin(); it != tgi.colorToBestHero.end(); ++it)
  4463. {
  4464. blitAt(graphics->portraitSmall[it->second.portrait], 260 + 66 * counter, 360, background);
  4465. counter++;
  4466. //printing stats
  4467. if(it->second.details)
  4468. {
  4469. for (int i=0; i<it->second.details->primskills.size(); ++i)
  4470. {
  4471. printAt(CGI->generaltexth->allTexts[380+i], 191 + 66*counter, 394 + 11*i, FONT_SMALL, zwykly, background);
  4472. std::ostringstream skill;
  4473. skill << it->second.details->primskills[i];
  4474. printTo(skill.str(), 244 + 66 * counter, 410 + 11*i, FONT_SMALL, zwykly, background);
  4475. }
  4476. }
  4477. }
  4478. //printing best creature
  4479. counter = 0;
  4480. for(std::map<ui8, si32>::const_iterator it = tgi.bestCreature.begin(); it != tgi.bestCreature.end(); ++it)
  4481. {
  4482. blitAt(graphics->bigImgs[it->second], 255 + 66 * counter, 479, background);
  4483. counter++;
  4484. }
  4485. //printing personality
  4486. counter = 0;
  4487. for(std::map<ui8, si8>::const_iterator it = tgi.personality.begin(); it != tgi.personality.end(); ++it)
  4488. {
  4489. int toPrint = 0;
  4490. if(it->second == -1)
  4491. {
  4492. toPrint = 172;
  4493. }
  4494. else
  4495. {
  4496. toPrint = 168 + it->second;
  4497. }
  4498. printAtMiddle(CGI->generaltexth->arraytxt[toPrint], 283 + 66*counter, 459, FONT_SMALL, zwykly, background);
  4499. counter++;
  4500. }
  4501. }
  4502. CThievesGuildWindow::~CThievesGuildWindow()
  4503. {
  4504. SDL_FreeSurface(background);
  4505. // delete exitb;
  4506. // delete statusBar;
  4507. // delete resdatabar;
  4508. }
  4509. void MoraleLuckBox::set( bool morale, const CGHeroInstance *hero, int slot /*= -1*/ )
  4510. {
  4511. int mrlv = -9, mrlt = -9;
  4512. std::vector<std::pair<int,std::string> > mrl;
  4513. if(morale)
  4514. {
  4515. //setting morale
  4516. mrl = hero->getCurrentMoraleModifiers();
  4517. mrlv = hero->getCurrentMorale();
  4518. mrlt = (mrlv>0)-(mrlv<0); //signum: -1 - bad morale, 0 - neutral, 1 - good
  4519. hoverText = CGI->generaltexth->heroscrn[4 - mrlt];
  4520. baseType = SComponent::morale;
  4521. bonus = mrlv;
  4522. text = CGI->generaltexth->arraytxt[88];
  4523. boost::algorithm::replace_first(text,"%s",CGI->generaltexth->arraytxt[86-mrlt]);
  4524. if (!mrl.size())
  4525. text += CGI->generaltexth->arraytxt[108];
  4526. else
  4527. for(int it=0; it < mrl.size(); it++)
  4528. text += "\n" + mrl[it].second;
  4529. }
  4530. else
  4531. {
  4532. //setting luck
  4533. mrl = hero->getCurrentLuckModifiers();
  4534. mrlv = hero->getCurrentLuck();
  4535. mrlt = (mrlv>0)-(mrlv<0); //signum: -1 - bad luck, 0 - neutral, 1 - good
  4536. hoverText = CGI->generaltexth->heroscrn[7 - mrlt];
  4537. baseType = SComponent::luck;
  4538. bonus = mrlv;
  4539. text = CGI->generaltexth->arraytxt[62];
  4540. boost::algorithm::replace_first(text,"%s",CGI->generaltexth->arraytxt[60-mrlt]);
  4541. if (!mrl.size())
  4542. text += CGI->generaltexth->arraytxt[77];
  4543. else
  4544. for(int it=0; it < mrl.size(); it++)
  4545. text += "\n" + mrl[it].second;
  4546. }
  4547. }
  4548. void CLabel::showAll(SDL_Surface * to)
  4549. {
  4550. CIntObject::showAll(to);
  4551. if(!text.length())
  4552. return;
  4553. static void (*printer[3])(const std::string &, int, int, EFonts, SDL_Color, SDL_Surface *, bool) = {&CSDL_Ext::printAt, &CSDL_Ext::printAtMiddle, &CSDL_Ext::printTo}; //array of printing functions
  4554. printer[alignment](text, pos.x + textOffset.x, pos.y + textOffset.y, font, color, to, false);
  4555. }
  4556. CLabel::CLabel(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= zwykly*/, const std::string &Text /*= ""*/)
  4557. :font(Font), color(Color), text(Text), alignment(Align)
  4558. {
  4559. autoRedraw = true;
  4560. pos.x += x;
  4561. pos.y += y;
  4562. pos.w = pos.h = 0;
  4563. bg = NULL;
  4564. }
  4565. void CLabel::setTxt(const std::string &Txt)
  4566. {
  4567. text = Txt;
  4568. if(autoRedraw)
  4569. {
  4570. if(bg || !parent)
  4571. redraw();
  4572. else
  4573. parent->redraw();
  4574. }
  4575. }
  4576. void CGStatusBar::print(const std::string & Text)
  4577. {
  4578. setTxt(Text);
  4579. }
  4580. void CGStatusBar::clear()
  4581. {
  4582. setTxt("");
  4583. }
  4584. std::string CGStatusBar::getCurrent()
  4585. {
  4586. return text;
  4587. }
  4588. CGStatusBar::CGStatusBar(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= zwykly*/, const std::string &Text /*= ""*/)
  4589. : CLabel(x, y, Font, Align, Color, Text)
  4590. {
  4591. init();
  4592. }
  4593. CGStatusBar::CGStatusBar(CPicture *BG, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= CENTER*/, const SDL_Color &Color /*= zwykly*/)
  4594. : CLabel(BG->pos.x, BG->pos.y, Font, Align, Color, "")
  4595. {
  4596. init();
  4597. bg = BG;
  4598. moveChildren(bg, bg->parent, this);
  4599. pos = bg->pos;
  4600. switch(Align)
  4601. {
  4602. case CENTER:
  4603. textOffset = Point(pos.w/2, pos.h/2);
  4604. break;
  4605. case BOTTOMRIGHT:
  4606. textOffset = Point(pos.w, pos.h);
  4607. break;
  4608. }
  4609. }
  4610. CGStatusBar::~CGStatusBar()
  4611. {
  4612. GH.statusbar = oldStatusBar;
  4613. }
  4614. void CGStatusBar::show(SDL_Surface * to)
  4615. {
  4616. }
  4617. void CGStatusBar::init()
  4618. {
  4619. oldStatusBar = GH.statusbar;
  4620. GH.statusbar = this;
  4621. }
  4622. CTextInput::CTextInput( const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB )
  4623. :cb(CB), focus(false)
  4624. {
  4625. pos += Pos;
  4626. OBJ_CONSTRUCTION;
  4627. bg = new CPicture(bgName, bgOffset.x, bgOffset.y);
  4628. used = LCLICK | KEYBOARD;
  4629. giveFocus();
  4630. }
  4631. CTextInput::CTextInput(const Rect &Pos, SDL_Surface *srf)
  4632. : focus(false)
  4633. {
  4634. pos += Pos;
  4635. OBJ_CONSTRUCTION;
  4636. bg = new CPicture(Pos, 0, true);
  4637. Rect hlp = Pos;
  4638. SDL_BlitSurface(srf, &hlp, *bg, NULL);
  4639. pos.w = bg->pos.w;
  4640. pos.h = bg->pos.h;
  4641. bg->pos = pos;
  4642. used = LCLICK | KEYBOARD;
  4643. giveFocus();
  4644. }
  4645. void CTextInput::showAll( SDL_Surface * to )
  4646. {
  4647. CIntObject::showAll(to);
  4648. const std::string toPrint = focus ? text + "_" : text;
  4649. CSDL_Ext::printAt(toPrint, pos.x, pos.y, FONT_SMALL, zwykly, to);
  4650. }
  4651. void CTextInput::clickLeft( tribool down, bool previousState )
  4652. {
  4653. if(down && !focus)
  4654. giveFocus();
  4655. }
  4656. void CTextInput::keyPressed( const SDL_KeyboardEvent & key )
  4657. {
  4658. if(!focus || key.state != SDL_PRESSED) return;
  4659. switch(key.keysym.sym)
  4660. {
  4661. case SDLK_BACKSPACE:
  4662. if(text.size())
  4663. text.resize(text.size()-1);
  4664. break;
  4665. default:
  4666. char c = key.keysym.unicode; //TODO 16-/>8
  4667. static const std::string forbiddenChars = "<>:\"/\\|?*"; //if we are entering a filename, some special characters won't be allowed
  4668. if(!vstd::contains(forbiddenChars,c) && std::isprint(c))
  4669. text += c;
  4670. break;
  4671. }
  4672. redraw();
  4673. cb(text);
  4674. }
  4675. void CTextInput::setText( const std::string &nText, bool callCb )
  4676. {
  4677. text = nText;
  4678. redraw();
  4679. if(callCb)
  4680. cb(text);
  4681. }
  4682. CTextInput::~CTextInput()
  4683. {
  4684. if(inputWithFocus == this)
  4685. inputWithFocus = NULL;
  4686. }
  4687. void CTextInput::giveFocus()
  4688. {
  4689. if(inputWithFocus)
  4690. {
  4691. inputWithFocus->focus = false;
  4692. inputWithFocus->redraw();
  4693. }
  4694. focus = true;
  4695. inputWithFocus = this;
  4696. redraw();
  4697. }