GUIClasses.cpp 143 KB

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