GUIClasses.cpp 179 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153
  1. #include "StdInc.h"
  2. #include "GUIClasses.h"
  3. #include "gui/SDL_Extensions.h"
  4. #include "CAdvmapInterface.h"
  5. #include "battle/CBattleInterface.h"
  6. #include "battle/CBattleInterfaceClasses.h"
  7. #include "../CCallback.h"
  8. #include "CCastleInterface.h"
  9. #include "CCreatureWindow.h"
  10. #include "gui/CCursorHandler.h"
  11. #include "CGameInfo.h"
  12. #include "CHeroWindow.h"
  13. #include "CMessage.h"
  14. #include "../lib/CConfigHandler.h"
  15. #include "battle/CCreatureAnimation.h"
  16. #include "CPlayerInterface.h"
  17. #include "Graphics.h"
  18. #include "CAnimation.h"
  19. #include "../lib/CArtHandler.h"
  20. #include "../lib/CBuildingHandler.h"
  21. #include "../lib/CGeneralTextHandler.h"
  22. #include "../lib/CHeroHandler.h"
  23. #include "../lib/CModHandler.h"
  24. #include "../lib/CObjectHandler.h"
  25. #include "../lib/CSpellHandler.h"
  26. #include "../lib/CTownHandler.h"
  27. #include "../lib/CondSh.h"
  28. #include "../lib/mapping/CMap.h"
  29. #include "mapHandler.h"
  30. #include "../lib/CStopWatch.h"
  31. #include "../lib/NetPacks.h"
  32. #include "CSpellWindow.h"
  33. #include "CHeroWindow.h"
  34. #include "CVideoHandler.h"
  35. #include "../lib/StartInfo.h"
  36. #include "CPreGame.h"
  37. #include "../lib/HeroBonus.h"
  38. #include "../lib/CCreatureHandler.h"
  39. #include "CMusicHandler.h"
  40. #include "../lib/BattleState.h"
  41. #include "../lib/CGameState.h"
  42. #include "../lib/GameConstants.h"
  43. #include "gui/CGuiHandler.h"
  44. /*
  45. * GUIClasses.cpp, part of VCMI engine
  46. *
  47. * Authors: listed in file AUTHORS in main folder
  48. *
  49. * License: GNU General Public License v2.0 or later
  50. * Full text of license available in license.txt file, in main folder
  51. *
  52. */
  53. using namespace boost::assign;
  54. using namespace CSDL_Ext;
  55. extern std::queue<SDL_Event> events;
  56. extern boost::mutex eventsM;
  57. std::list<CFocusable*> CFocusable::focusables;
  58. CFocusable * CFocusable::inputWithFocus;
  59. #undef min
  60. #undef max
  61. void CArmyTooltip::init(const InfoAboutArmy &army)
  62. {
  63. OBJ_CONSTRUCTION_CAPTURING_ALL;
  64. new CLabel(66, 2, FONT_SMALL, TOPLEFT, Colors::WHITE, army.name);
  65. std::vector<Point> slotsPos;
  66. slotsPos.push_back(Point(36,73));
  67. slotsPos.push_back(Point(72,73));
  68. slotsPos.push_back(Point(108,73));
  69. slotsPos.push_back(Point(18,122));
  70. slotsPos.push_back(Point(54,122));
  71. slotsPos.push_back(Point(90,122));
  72. slotsPos.push_back(Point(126,122));
  73. BOOST_FOREACH(auto & slot, army.army)
  74. {
  75. if(slot.first.getNum() >= GameConstants::ARMY_SIZE)
  76. {
  77. logGlobal->warnStream() << "Warning: " << army.name << " has stack in slot " << slot.first;
  78. continue;
  79. }
  80. new CAnimImage("CPRSMALL", slot.second.type->iconIndex, 0, slotsPos[slot.first.getNum()].x, slotsPos[slot.first.getNum()].y);
  81. std::string subtitle;
  82. if(army.army.isDetailed)
  83. subtitle = boost::lexical_cast<std::string>(slot.second.count);
  84. else
  85. {
  86. //if =0 - we have no information about stack size at all
  87. if (slot.second.count)
  88. subtitle = CGI->generaltexth->arraytxt[171 + 3*(slot.second.count)];
  89. }
  90. new CLabel(slotsPos[slot.first.getNum()].x + 17, slotsPos[slot.first.getNum()].y + 41, FONT_TINY, CENTER, Colors::WHITE, subtitle);
  91. }
  92. }
  93. CArmyTooltip::CArmyTooltip(Point pos, const InfoAboutArmy &army):
  94. CIntObject(0, pos)
  95. {
  96. init(army);
  97. }
  98. CArmyTooltip::CArmyTooltip(Point pos, const CArmedInstance * army):
  99. CIntObject(0, pos)
  100. {
  101. init(InfoAboutArmy(army, true));
  102. }
  103. void CHeroTooltip::init(const InfoAboutHero &hero)
  104. {
  105. OBJ_CONSTRUCTION_CAPTURING_ALL;
  106. new CAnimImage("PortraitsLarge", hero.portrait, 0, 3, 2);
  107. if(hero.details)
  108. {
  109. for (size_t i = 0; i < hero.details->primskills.size(); i++)
  110. new CLabel(75 + 28 * i, 58, FONT_SMALL, CENTER, Colors::WHITE,
  111. boost::lexical_cast<std::string>(hero.details->primskills[i]));
  112. new CLabel(158, 98, FONT_TINY, CENTER, Colors::WHITE,
  113. boost::lexical_cast<std::string>(hero.details->mana));
  114. new CAnimImage("IMRL22", hero.details->morale + 3, 0, 5, 74);
  115. new CAnimImage("ILCK22", hero.details->luck + 3, 0, 5, 91);
  116. }
  117. }
  118. CHeroTooltip::CHeroTooltip(Point pos, const InfoAboutHero &hero):
  119. CArmyTooltip(pos, hero)
  120. {
  121. init(hero);
  122. }
  123. CHeroTooltip::CHeroTooltip(Point pos, const CGHeroInstance * hero):
  124. CArmyTooltip(pos, InfoAboutHero(hero, true))
  125. {
  126. init(InfoAboutHero(hero, true));
  127. }
  128. void CTownTooltip::init(const InfoAboutTown &town)
  129. {
  130. OBJ_CONSTRUCTION_CAPTURING_ALL;
  131. //order of icons in def: fort, citadel, castle, no fort
  132. size_t fortIndex = town.fortLevel ? town.fortLevel - 1 : 3;
  133. new CAnimImage("ITMCLS", fortIndex, 0, 105, 31);
  134. assert(town.tType);
  135. size_t iconIndex = town.tType->clientInfo.icons[town.fortLevel > 0][town.built >= CGI->modh->settings.MAX_BUILDING_PER_TURN];
  136. new CAnimImage("itpt", iconIndex, 0, 3, 2);
  137. if(town.details)
  138. {
  139. new CAnimImage("ITMTLS", town.details->hallLevel, 0, 67, 31);
  140. if (town.details->goldIncome)
  141. new CLabel(157, 58, FONT_TINY, CENTER, Colors::WHITE,
  142. boost::lexical_cast<std::string>(town.details->goldIncome));
  143. if(town.details->garrisonedHero) //garrisoned hero icon
  144. new CPicture("TOWNQKGH", 149, 76);
  145. if(town.details->customRes)//silo is built
  146. {
  147. if (town.tType->primaryRes == Res::WOOD_AND_ORE )// wood & ore
  148. {
  149. new CAnimImage("SMALRES", Res::WOOD, 0, 7, 75);
  150. new CAnimImage("SMALRES", Res::ORE , 0, 7, 88);
  151. }
  152. else
  153. new CAnimImage("SMALRES", town.tType->primaryRes, 0, 7, 81);
  154. }
  155. }
  156. }
  157. CTownTooltip::CTownTooltip(Point pos, const InfoAboutTown &town):
  158. CArmyTooltip(pos, town)
  159. {
  160. init(town);
  161. }
  162. CTownTooltip::CTownTooltip(Point pos, const CGTownInstance * town):
  163. CArmyTooltip(pos, InfoAboutTown(town, true))
  164. {
  165. init(InfoAboutTown(town, true));
  166. }
  167. void CGarrisonSlot::setHighlight(bool on)
  168. {
  169. if (on)
  170. selectionImage->enable(); //show
  171. else
  172. selectionImage->disable(); //hide
  173. }
  174. void CGarrisonSlot::hover (bool on)
  175. {
  176. ////Hoverable::hover(on);
  177. if(on)
  178. {
  179. std::string temp;
  180. if(creature)
  181. {
  182. if(owner->getSelection())
  183. {
  184. if(owner->getSelection() == this)
  185. {
  186. temp = CGI->generaltexth->tcommands[4]; //View %s
  187. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  188. }
  189. else if (owner->getSelection()->creature == creature)
  190. {
  191. temp = CGI->generaltexth->tcommands[2]; //Combine %s armies
  192. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  193. }
  194. else if (owner->getSelection()->creature)
  195. {
  196. temp = CGI->generaltexth->tcommands[7]; //Exchange %s with %s
  197. boost::algorithm::replace_first(temp,"%s",owner->getSelection()->creature->nameSing);
  198. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  199. }
  200. else
  201. {
  202. logGlobal->warnStream() << "Warning - shouldn't be - highlighted void slot "<<owner->getSelection();
  203. logGlobal->warnStream() << "Highlighted set to NULL";
  204. owner->selectSlot(nullptr);
  205. }
  206. }
  207. else
  208. {
  209. if(upg)
  210. {
  211. temp = CGI->generaltexth->tcommands[32]; //Select %s (visiting)
  212. }
  213. else if(owner->armedObjs[0] && owner->armedObjs[0]->ID == Obj::TOWN)
  214. {
  215. temp = CGI->generaltexth->tcommands[12]; //Select %s (in garrison)
  216. }
  217. else
  218. {
  219. temp = CGI->generaltexth->allTexts[481]; //Select %s
  220. }
  221. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  222. };
  223. }
  224. else
  225. {
  226. if(owner->getSelection())
  227. {
  228. const CArmedInstance *highl = owner->getSelection()->getObj();
  229. if( highl->needsLastStack() //we are moving stack from hero's
  230. && highl->stacksCount() == 1 //it's only stack
  231. && owner->getSelection()->upg != upg //we're moving it to the other garrison
  232. )
  233. {
  234. temp = CGI->generaltexth->tcommands[5]; //Cannot move last army to garrison
  235. }
  236. else
  237. {
  238. temp = CGI->generaltexth->tcommands[6]; //Move %s
  239. boost::algorithm::replace_first(temp,"%s",owner->getSelection()->creature->nameSing);
  240. }
  241. }
  242. else
  243. {
  244. temp = CGI->generaltexth->tcommands[11]; //Empty
  245. }
  246. }
  247. GH.statusbar->print(temp);
  248. }
  249. else
  250. {
  251. GH.statusbar->clear();
  252. }
  253. }
  254. const CArmedInstance * CGarrisonSlot::getObj() const
  255. {
  256. return (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]);
  257. }
  258. bool CGarrisonSlot::our() const
  259. {
  260. return upg?(owner->owned[1]):(owner->owned[0]);
  261. }
  262. void CGarrisonSlot::clickRight(tribool down, bool previousState)
  263. {
  264. if(down && creature)
  265. {
  266. GH.pushInt(createCreWindow(myStack, CCreatureWindow::ARMY));
  267. }
  268. }
  269. void CGarrisonSlot::clickLeft(tribool down, bool previousState)
  270. {
  271. if(down)
  272. {
  273. bool refr = false;
  274. if(owner->getSelection())
  275. {
  276. if(owner->getSelection() == this) //view info
  277. {
  278. UpgradeInfo pom;
  279. LOCPLINT->cb->getUpgradeInfo(getObj(), ID, pom);
  280. bool canUpgrade = getObj()->tempOwner == LOCPLINT->playerID && pom.oldID>=0; //upgrade is possible
  281. bool canDismiss = getObj()->tempOwner == LOCPLINT->playerID && (getObj()->stacksCount()>1 || !getObj()->needsLastStack());
  282. boost::function<void()> upgr = NULL;
  283. boost::function<void()> dism = NULL;
  284. if (canUpgrade) upgr = boost::bind(&CCallback::upgradeCreature, LOCPLINT->cb.get(), getObj(), ID, pom.newID[0]);
  285. if (canDismiss) dism = boost::bind(&CCallback::dismissCreature, LOCPLINT->cb.get(), getObj(), ID);
  286. owner->selectSlot(nullptr);
  287. owner->setSplittingMode(false);
  288. for(size_t i = 0; i<owner->splitButtons.size(); i++)
  289. owner->splitButtons[i]->block(true);
  290. redraw();
  291. refr = true;
  292. CIntObject *creWindow = createCreWindow(myStack, CCreatureWindow::HERO, upgr, dism, &pom);
  293. GH.pushInt(creWindow);
  294. }
  295. else
  296. {
  297. // Only allow certain moves if troops aren't removable or not ours.
  298. if ( ( owner->getSelection()->our()//our creature is selected
  299. || owner->getSelection()->creature == creature )//or we are rebalancing army
  300. && ( owner->removableUnits
  301. || (upg == 0 && ( owner->getSelection()->upg == 1 && !creature ) )
  302. || (upg == 1 && owner->getSelection()->upg == 1 ) ) )
  303. {
  304. //we want to split
  305. if((owner->getSplittingMode() || LOCPLINT->shiftPressed())
  306. && (!creature
  307. || (creature == owner->getSelection()->creature)))
  308. {
  309. owner->p2 = ID; //store the second stack pos
  310. owner->pb = upg;//store the second stack owner (up or down army)
  311. owner->setSplittingMode(false);
  312. int minLeft=0, minRight=0;
  313. if(upg != owner->getSelection()->upg) //not splitting within same army
  314. {
  315. if(owner->getSelection()->getObj()->stacksCount() == 1 //we're splitting away the last stack
  316. && owner->getSelection()->getObj()->needsLastStack() )
  317. {
  318. minLeft = 1;
  319. }
  320. if(getObj()->stacksCount() == 1 //destination army can't be emptied, unless we're rebalancing two stacks of same creature
  321. && owner->getSelection()->creature == creature
  322. && getObj()->needsLastStack() )
  323. {
  324. minRight = 1;
  325. }
  326. }
  327. int countLeft = owner->getSelection()->myStack ? owner->getSelection()->myStack->count : 0;
  328. int countRight = myStack ? myStack->count : 0;
  329. GH.pushInt(new CSplitWindow(owner->getSelection()->creature, boost::bind(&CGarrisonInt::splitStacks, owner, _1, _2),
  330. minLeft, minRight, countLeft, countRight));
  331. refr = true;
  332. }
  333. else if(creature != owner->getSelection()->creature) //swap
  334. {
  335. LOCPLINT->cb->swapCreatures(
  336. (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  337. (!owner->getSelection()->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  338. ID,owner->getSelection()->ID);
  339. }
  340. else //merge
  341. {
  342. LOCPLINT->cb->mergeStacks(
  343. (!owner->getSelection()->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  344. (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  345. owner->getSelection()->ID,ID);
  346. }
  347. }
  348. else // Highlight
  349. {
  350. if(creature)
  351. owner->selectSlot(this);
  352. redraw();
  353. refr = true;
  354. }
  355. }
  356. }
  357. else //highlight or drop artifact
  358. {
  359. bool artSelected = false;
  360. if (CWindowWithArtifacts* chw = dynamic_cast<CWindowWithArtifacts*>(GH.topInt())) //dirty solution
  361. {
  362. const CArtifactsOfHero::SCommonPart *commonInfo = chw->artSets.front()->commonInfo;
  363. if (const CArtifactInstance *art = commonInfo->src.art)
  364. {
  365. const CGHeroInstance *srcHero = commonInfo->src.AOH->getHero();
  366. artSelected = true;
  367. ArtifactLocation src(srcHero, commonInfo->src.slotID);
  368. ArtifactLocation dst(myStack, ArtifactPosition::CREATURE_SLOT);
  369. if (art->canBePutAt(dst, true))
  370. { //equip clicked stack
  371. if(dst.getArt())
  372. {
  373. //creature can wear only one active artifact
  374. //if we are placing a new one, the old one will be returned to the hero's backpack
  375. LOCPLINT->cb->swapArtifacts(dst, ArtifactLocation(srcHero, dst.getArt()->firstBackpackSlot(srcHero)));
  376. }
  377. LOCPLINT->cb->swapArtifacts(src, dst);
  378. }
  379. }
  380. }
  381. if (!artSelected && creature)
  382. {
  383. owner->selectSlot(this);
  384. if(creature)
  385. {
  386. for(size_t i = 0; i<owner->splitButtons.size(); i++)
  387. owner->splitButtons[i]->block(false);
  388. }
  389. }
  390. redraw();
  391. refr = true;
  392. }
  393. if(refr) {hover(false); hover(true); } //to refresh statusbar
  394. }
  395. }
  396. void CGarrisonSlot::update()
  397. {
  398. if (getObj() != nullptr)
  399. {
  400. addUsedEvents(LCLICK | RCLICK | HOVER);
  401. myStack = getObj()->getStackPtr(ID);
  402. creature = myStack ? myStack->type : NULL;
  403. }
  404. else
  405. {
  406. removeUsedEvents(LCLICK | RCLICK | HOVER);
  407. myStack = nullptr;
  408. creature = nullptr;
  409. }
  410. if (creature)
  411. {
  412. creatureImage->enable();
  413. creatureImage->setFrame(creature->iconIndex);
  414. stackCount->enable();
  415. stackCount->setTxt(boost::lexical_cast<std::string>(myStack->count));
  416. }
  417. else
  418. {
  419. creatureImage->disable();
  420. stackCount->disable();
  421. }
  422. }
  423. CGarrisonSlot::CGarrisonSlot(CGarrisonInt *Owner, int x, int y, SlotID IID, int Upg, const CStackInstance * Creature):
  424. ID(IID),
  425. owner(Owner),
  426. myStack(Creature),
  427. creature(Creature ? Creature->type : nullptr),
  428. upg(Upg)
  429. {
  430. OBJ_CONSTRUCTION_CAPTURING_ALL;
  431. if (getObj())
  432. addUsedEvents(LCLICK | RCLICK | HOVER);
  433. pos.x += x;
  434. pos.y += y;
  435. std::string imgName = owner->smallIcons ? "cprsmall" : "TWCRPORT";
  436. creatureImage = new CAnimImage(imgName, creature ? creature->iconIndex : 0);
  437. if (!creature)
  438. creatureImage->disable();
  439. selectionImage = new CAnimImage(imgName, 1);
  440. selectionImage->disable();
  441. if(Owner->smallIcons)
  442. {
  443. pos.w = 32;
  444. pos.h = 32;
  445. }
  446. else
  447. {
  448. pos.w = 58;
  449. pos.h = 64;
  450. }
  451. stackCount = new CLabel(pos.w, pos.h, owner->smallIcons ? FONT_TINY : FONT_MEDIUM, BOTTOMRIGHT, Colors::WHITE);
  452. if (!creature)
  453. stackCount->disable();
  454. else
  455. stackCount->setTxt(boost::lexical_cast<std::string>(myStack->count));
  456. }
  457. void CGarrisonInt::addSplitBtn(CAdventureMapButton * button)
  458. {
  459. addChild(button);
  460. button->recActions = defActions;
  461. splitButtons.push_back(button);
  462. button->block(getSelection() == nullptr);
  463. }
  464. void CGarrisonInt::createSet(std::vector<CGarrisonSlot*> &ret, const CCreatureSet * set, int posX, int posY, int distance, int Upg )
  465. {
  466. ret.resize(7);
  467. if (set)
  468. {
  469. for(TSlots::const_iterator i=set->Slots().begin(); i!=set->Slots().end(); i++)
  470. {
  471. ret[i->first.getNum()] = new CGarrisonSlot(this, posX + (i->first.getNum()*distance), posY, i->first, Upg, i->second);
  472. }
  473. }
  474. for(int i=0; i<ret.size(); i++)
  475. if(!ret[i])
  476. ret[i] = new CGarrisonSlot(this, posX + (i*distance), posY, SlotID(i), Upg, NULL);
  477. if (twoRows)
  478. for (int i=4; i<ret.size(); i++)
  479. {
  480. ret[i]->pos.x -= 126;
  481. ret[i]->pos.y += 37;
  482. };
  483. }
  484. void CGarrisonInt::createSlots()
  485. {
  486. OBJ_CONSTRUCTION_CAPTURING_ALL;
  487. int width = smallIcons? 32 : 58;
  488. createSet(slotsUp, armedObjs[0], 0, 0, width+interx, 0);
  489. createSet(slotsDown, armedObjs[1], garOffset.x, garOffset.y, width+interx, 1);
  490. }
  491. void CGarrisonInt::recreateSlots()
  492. {
  493. selectSlot(nullptr);
  494. setSplittingMode(false);
  495. for(size_t i = 0; i<splitButtons.size(); i++)
  496. splitButtons[i]->block(true);
  497. BOOST_FOREACH(CGarrisonSlot * slot, slotsUp)
  498. slot->update();
  499. BOOST_FOREACH(CGarrisonSlot * slot, slotsDown)
  500. slot->update();
  501. }
  502. void CGarrisonInt::splitClick()
  503. {
  504. if(!getSelection())
  505. return;
  506. setSplittingMode(!getSplittingMode());
  507. redraw();
  508. }
  509. void CGarrisonInt::splitStacks(int, int amountRight)
  510. {
  511. LOCPLINT->cb->splitStack(armedObjs[getSelection()->upg], armedObjs[pb], getSelection()->ID, p2, amountRight);
  512. }
  513. CGarrisonInt::CGarrisonInt(int x, int y, int inx, const Point &garsOffset,
  514. SDL_Surface *pomsur, const Point& SurOffset,
  515. const CArmedInstance *s1, const CArmedInstance *s2,
  516. bool _removableUnits, bool smallImgs, bool _twoRows ) :
  517. highlighted(nullptr),
  518. inSplittingMode(false),
  519. interx(inx),
  520. garOffset(garsOffset),
  521. smallIcons(smallImgs),
  522. removableUnits(_removableUnits),
  523. twoRows(_twoRows)
  524. {
  525. setArmy(s1, false);
  526. setArmy(s2, true);
  527. pos.x += x;
  528. pos.y += y;
  529. createSlots();
  530. }
  531. const CGarrisonSlot * CGarrisonInt::getSelection()
  532. {
  533. return highlighted;
  534. }
  535. void CGarrisonInt::selectSlot(CGarrisonSlot *slot)
  536. {
  537. if (slot != highlighted)
  538. {
  539. if (highlighted)
  540. highlighted->setHighlight(false);
  541. highlighted = slot;
  542. BOOST_FOREACH (auto button, splitButtons)
  543. button->block(highlighted == nullptr);
  544. if (highlighted)
  545. highlighted->setHighlight(true);
  546. }
  547. }
  548. void CGarrisonInt::setSplittingMode(bool on)
  549. {
  550. assert(on == false || highlighted != nullptr); //can't be in splitting mode without selection
  551. if (inSplittingMode || on)
  552. {
  553. BOOST_FOREACH(CGarrisonSlot * slot, slotsUp)
  554. slot->setHighlight( ( on && (slot->creature == nullptr || slot->creature == getSelection()->creature)));
  555. BOOST_FOREACH(CGarrisonSlot * slot, slotsDown)
  556. slot->setHighlight( ( on && (slot->creature == nullptr || slot->creature == getSelection()->creature)));
  557. inSplittingMode = on;
  558. }
  559. }
  560. bool CGarrisonInt::getSplittingMode()
  561. {
  562. return inSplittingMode;
  563. }
  564. void CGarrisonInt::setArmy(const CArmedInstance *army, bool bottomGarrison)
  565. {
  566. owned[bottomGarrison] = army ? (army->tempOwner == LOCPLINT->playerID || army->tempOwner == PlayerColor::UNFLAGGABLE) : false;
  567. armedObjs[bottomGarrison] = army;
  568. }
  569. CInfoWindow::CInfoWindow(std::string Text, PlayerColor player, const TCompsInfo &comps, const TButtonsInfo &Buttons, bool delComps)
  570. {
  571. OBJ_CONSTRUCTION_CAPTURING_ALL;
  572. type |= BLOCK_ADV_HOTKEYS;
  573. ID = QueryID(-1);
  574. for(int i=0;i<Buttons.size();i++)
  575. {
  576. CAdventureMapButton *button = new CAdventureMapButton("","",boost::bind(&CInfoWindow::close,this),0,0,Buttons[i].first);
  577. button->borderColor = Colors::METALLIC_GOLD;
  578. button->borderEnabled = true;
  579. button->callback.add(Buttons[i].second); //each button will close the window apart from call-defined actions
  580. buttons.push_back(button);
  581. }
  582. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::WHITE);
  583. if(!text->slider)
  584. {
  585. text->pos.w = text->maxW;
  586. text->pos.h = text->maxH;
  587. }
  588. if(buttons.size())
  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. }
  593. for(int i=0;i<comps.size();i++)
  594. {
  595. comps[i]->recActions = 0xff;
  596. addChild(comps[i]);
  597. comps[i]->recActions &= ~(SHOWALL | UPDATE);
  598. components.push_back(comps[i]);
  599. }
  600. setDelComps(delComps);
  601. CMessage::drawIWindow(this,Text,player);
  602. }
  603. CInfoWindow::CInfoWindow()
  604. {
  605. ID = QueryID(-1);
  606. setDelComps(false);
  607. text = NULL;
  608. }
  609. void CInfoWindow::close()
  610. {
  611. GH.popIntTotally(this);
  612. if(LOCPLINT)
  613. LOCPLINT->showingDialog->setn(false);
  614. }
  615. void CInfoWindow::show(SDL_Surface * to)
  616. {
  617. CIntObject::show(to);
  618. }
  619. CInfoWindow::~CInfoWindow()
  620. {
  621. if(!delComps)
  622. {
  623. for (int i=0;i<components.size();i++)
  624. removeChild(components[i]);
  625. }
  626. }
  627. void CInfoWindow::showAll(SDL_Surface * to)
  628. {
  629. CSimpleWindow::show(to);
  630. CIntObject::showAll(to);
  631. }
  632. void CInfoWindow::showYesNoDialog(const std::string & text, const std::vector<CComponent*> *components, const CFunctionList<void( ) > &onYes, const CFunctionList<void()> &onNo, bool DelComps, PlayerColor player)
  633. {
  634. assert(!LOCPLINT || LOCPLINT->showingDialog->get());
  635. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  636. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  637. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  638. CInfoWindow * temp = new CInfoWindow(text, player, components ? *components : std::vector<CComponent*>(), pom, DelComps);
  639. for(int i=0;i<onYes.funcs.size();i++)
  640. temp->buttons[0]->callback += onYes.funcs[i];
  641. for(int i=0;i<onNo.funcs.size();i++)
  642. temp->buttons[1]->callback += onNo.funcs[i];
  643. GH.pushInt(temp);
  644. }
  645. CInfoWindow * CInfoWindow::create(const std::string &text, PlayerColor playerID /*= 1*/, const std::vector<CComponent*> *components /*= NULL*/, bool DelComps)
  646. {
  647. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  648. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  649. CInfoWindow * ret = new CInfoWindow(text, playerID, components ? *components : std::vector<CComponent*>(), pom, DelComps);
  650. return ret;
  651. }
  652. std::string CInfoWindow::genText(std::string title, std::string description)
  653. {
  654. return std::string("{") + title + "}" + "\n\n" + description;
  655. }
  656. void CInfoWindow::setDelComps(bool DelComps)
  657. {
  658. delComps = DelComps;
  659. BOOST_FOREACH(CComponent *comp, components)
  660. {
  661. if(delComps)
  662. comp->recActions |= DISPOSE;
  663. else
  664. comp->recActions &= ~DISPOSE;
  665. }
  666. }
  667. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  668. :free(Free),bitmap(Bitmap)
  669. {
  670. init(x, y);
  671. }
  672. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, const Point &p, EAlignment alignment, bool Free/*=false*/)
  673. : free(Free),bitmap(Bitmap)
  674. {
  675. switch(alignment)
  676. {
  677. case BOTTOMRIGHT:
  678. init(p.x - Bitmap->w, p.y - Bitmap->h);
  679. break;
  680. case CENTER:
  681. init(p.x - Bitmap->w/2, p.y - Bitmap->h/2);
  682. break;
  683. case TOPLEFT:
  684. init(p.x, p.y);
  685. break;
  686. default:
  687. assert(0); //not implemented
  688. }
  689. }
  690. CInfoPopup::CInfoPopup(SDL_Surface *Bitmap, bool Free)
  691. {
  692. CCS->curh->hide();
  693. free=Free;
  694. bitmap=Bitmap;
  695. if(bitmap)
  696. {
  697. pos.x = screen->w/2 - bitmap->w/2;
  698. pos.y = screen->h/2 - bitmap->h/2;
  699. pos.h = bitmap->h;
  700. pos.w = bitmap->w;
  701. }
  702. }
  703. void CInfoPopup::close()
  704. {
  705. if(free)
  706. SDL_FreeSurface(bitmap);
  707. GH.popIntTotally(this);
  708. }
  709. void CInfoPopup::show(SDL_Surface * to)
  710. {
  711. blitAt(bitmap,pos.x,pos.y,to);
  712. }
  713. CInfoPopup::~CInfoPopup()
  714. {
  715. CCS->curh->show();
  716. }
  717. void CInfoPopup::init(int x, int y)
  718. {
  719. CCS->curh->hide();
  720. pos.x = x;
  721. pos.y = y;
  722. pos.h = bitmap->h;
  723. pos.w = bitmap->w;
  724. // Put the window back on screen if necessary
  725. vstd::amax(pos.x, 0);
  726. vstd::amax(pos.y, 0);
  727. vstd::amin(pos.x, screen->w - bitmap->w);
  728. vstd::amin(pos.y, screen->h - bitmap->h);
  729. }
  730. CComponent::CComponent(Etype Type, int Subtype, int Val, ESize imageSize):
  731. image(nullptr),
  732. perDay(false)
  733. {
  734. addUsedEvents(RCLICK);
  735. init(Type, Subtype, Val, imageSize);
  736. }
  737. CComponent::CComponent(const Component &c):
  738. image(nullptr),
  739. perDay(false)
  740. {
  741. addUsedEvents(RCLICK);
  742. if(c.id == Component::RESOURCE && c.when==-1)
  743. perDay = true;
  744. init((Etype)c.id,c.subtype,c.val, large);
  745. }
  746. void CComponent::init(Etype Type, int Subtype, int Val, ESize imageSize)
  747. {
  748. OBJ_CONSTRUCTION_CAPTURING_ALL;
  749. compType = Type;
  750. subtype = Subtype;
  751. val = Val;
  752. size = imageSize;
  753. assert(compType < typeInvalid);
  754. assert(size < sizeInvalid);
  755. setSurface(getFileName()[size], getIndex());
  756. pos.w = image->pos.w;
  757. pos.h = image->pos.h;
  758. EFonts font = FONT_SMALL;
  759. if (imageSize < small)
  760. font = FONT_TINY; //other sizes?
  761. pos.h += 4; //distance between text and image
  762. std::vector<std::string> textLines = CMessage::breakText(getSubtitle(), std::max<int>(80, pos.w), font);
  763. BOOST_FOREACH(auto & line, textLines)
  764. {
  765. int height = graphics->fonts[font]->getLineHeight();
  766. CLabel * label = new CLabel(pos.w/2, pos.h + height/2, font, CENTER, Colors::WHITE, line);
  767. pos.h += height;
  768. if (label->pos.w > pos.w)
  769. {
  770. pos.x -= (label->pos.w - pos.w)/2;
  771. pos.w = label->pos.w;
  772. }
  773. }
  774. }
  775. const std::vector<std::string> CComponent::getFileName()
  776. {
  777. static const std::string primSkillsArr [] = {"PSKIL32", "PSKIL32", "PSKIL42", "PSKILL"};
  778. static const std::string secSkillsArr [] = {"SECSK32", "SECSK32", "SECSKILL", "SECSK82"};
  779. static const std::string resourceArr [] = {"SMALRES", "RESOURCE", "RESOUR82", "RESOUR82"};
  780. static const std::string creatureArr [] = {"CPRSMALL", "CPRSMALL", "TWCRPORT", "TWCRPORT"};
  781. static const std::string artifactArr[] = {"Artifact", "Artifact", "Artifact", "Artifact"};
  782. static const std::string spellsArr [] = {"SpellInt", "SpellInt", "SPELLSCR", "SPELLSCR"};
  783. static const std::string moraleArr [] = {"IMRL22", "IMRL30", "IMRL42", "imrl82"};
  784. static const std::string luckArr [] = {"ILCK22", "ILCK30", "ILCK42", "ilck82"};
  785. static const std::string heroArr [] = {"PortraitsSmall", "PortraitsSmall", "PortraitsLarge", "PortraitsLarge"};
  786. static const std::string flagArr [] = {"CREST58", "CREST58", "CREST58", "CREST58"};
  787. auto gen = [](const std::string * arr)
  788. {
  789. return std::vector<std::string>(arr, arr + 4);
  790. };
  791. switch(compType)
  792. {
  793. case primskill: return gen(primSkillsArr);
  794. case secskill: return gen(secSkillsArr);
  795. case resource: return gen(resourceArr);
  796. case creature: return gen(creatureArr);
  797. case artifact: return gen(artifactArr);
  798. case experience: return gen(primSkillsArr);
  799. case spell: return gen(spellsArr);
  800. case morale: return gen(moraleArr);
  801. case luck: return gen(luckArr);
  802. case building: return std::vector<std::string>(4, CGI->townh->factions[subtype]->town->clientInfo.buildingsIcons);
  803. case hero: return gen(heroArr);
  804. case flag: return gen(flagArr);
  805. }
  806. assert(0);
  807. return std::vector<std::string>();
  808. }
  809. size_t CComponent::getIndex()
  810. {
  811. switch(compType)
  812. {
  813. case primskill: return subtype;
  814. case secskill: return subtype*3 + 3 + val - 1;
  815. case resource: return subtype;
  816. case creature: return CGI->creh->creatures[subtype]->iconIndex;
  817. case artifact: return CGI->arth->artifacts[subtype]->iconIndex;
  818. case experience: return 4;
  819. case spell: return subtype;
  820. case morale: return val+3;
  821. case luck: return val+3;
  822. case building: return val;
  823. case hero: return CGI->heroh->heroes[subtype]->imageIndex;
  824. case flag: return subtype;
  825. }
  826. assert(0);
  827. return 0;
  828. }
  829. std::string CComponent::getDescription()
  830. {
  831. switch (compType)
  832. {
  833. case primskill: return (subtype < 4)? CGI->generaltexth->arraytxt[2+subtype] //Primary skill
  834. : CGI->generaltexth->allTexts[149]; //mana
  835. case secskill: return CGI->generaltexth->skillInfoTexts[subtype][val-1];
  836. case resource: return CGI->generaltexth->allTexts[242];
  837. case creature: return "";
  838. case artifact: return CGI->arth->artifacts[subtype]->Description();
  839. case experience: return CGI->generaltexth->allTexts[241];
  840. case spell: return CGI->spellh->spells[subtype]->descriptions[val];
  841. case morale: return CGI->generaltexth->heroscrn[ 4 - (val>0) + (val<0)];
  842. case luck: return CGI->generaltexth->heroscrn[ 7 - (val>0) + (val<0)];
  843. case building: return CGI->townh->factions[subtype]->town->buildings[BuildingID(val)]->Description();
  844. case hero: return CGI->heroh->heroes[subtype]->name;
  845. case flag: return "";
  846. }
  847. assert(0);
  848. return "";
  849. }
  850. std::string CComponent::getSubtitle()
  851. {
  852. if (!perDay)
  853. return getSubtitleInternal();
  854. std::string ret = CGI->generaltexth->allTexts[3];
  855. boost::replace_first(ret, "%d", getSubtitleInternal());
  856. return ret;
  857. }
  858. std::string CComponent::getSubtitleInternal()
  859. {
  860. //FIXME: some of these are horrible (e.g creature)
  861. switch(compType)
  862. {
  863. case primskill: return boost::str(boost::format("%+d %s") % val % (subtype < 4 ? CGI->generaltexth->primarySkillNames[subtype] : CGI->generaltexth->allTexts[387]));
  864. case secskill: return CGI->generaltexth->levels[val-1] + "\n" + CGI->generaltexth->skillName[subtype];
  865. case resource: return boost::lexical_cast<std::string>(val);
  866. case creature: return (val? boost::lexical_cast<std::string>(val) + " " : "") + CGI->creh->creatures[subtype]->*(val != 1 ? &CCreature::namePl : &CCreature::nameSing);
  867. case artifact: return CGI->arth->artifacts[subtype]->Name();
  868. case experience: return (subtype && val==1) ? CGI->generaltexth->allTexts[442] : boost::lexical_cast<std::string>(val);
  869. case spell: return CGI->spellh->spells[subtype]->name;
  870. case morale: return "";
  871. case luck: return "";
  872. case building: return CGI->townh->factions[subtype]->town->buildings[BuildingID(val)]->Name();
  873. case hero: return CGI->heroh->heroes[subtype]->name;
  874. case flag: return CGI->generaltexth->capColors[subtype];
  875. }
  876. assert(0);
  877. return "";
  878. }
  879. void CComponent::setSurface(std::string defName, int imgPos)
  880. {
  881. OBJ_CONSTRUCTION_CAPTURING_ALL;
  882. vstd::clear_pointer(image);
  883. image = new CAnimImage(defName, imgPos);
  884. }
  885. void CComponent::clickRight(tribool down, bool previousState)
  886. {
  887. if(!getDescription().empty())
  888. adventureInt->handleRightClick(getDescription(), down);
  889. }
  890. void CSelectableComponent::clickLeft(tribool down, bool previousState)
  891. {
  892. if (down)
  893. {
  894. if(onSelect)
  895. onSelect();
  896. }
  897. }
  898. void CSelectableComponent::init()
  899. {
  900. selected = false;
  901. }
  902. CSelectableComponent::CSelectableComponent(const Component &c, boost::function<void()> OnSelect):
  903. CComponent(c),onSelect(OnSelect)
  904. {
  905. type |= REDRAW_PARENT;
  906. addUsedEvents(LCLICK | KEYBOARD);
  907. init();
  908. }
  909. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, ESize imageSize, boost::function<void()> OnSelect):
  910. CComponent(Type,Sub,Val, imageSize),onSelect(OnSelect)
  911. {
  912. type |= REDRAW_PARENT;
  913. addUsedEvents(LCLICK | KEYBOARD);
  914. init();
  915. }
  916. void CSelectableComponent::select(bool on)
  917. {
  918. if(on != selected)
  919. {
  920. selected = on;
  921. redraw();
  922. }
  923. }
  924. void CSelectableComponent::showAll(SDL_Surface * to)
  925. {
  926. CComponent::showAll(to);
  927. if(selected)
  928. {
  929. CSDL_Ext::drawBorder(to, Rect::around(image->pos), int3(239,215,123));
  930. }
  931. }
  932. void CComponentBox::selectionChanged(CSelectableComponent * newSelection)
  933. {
  934. if (newSelection == selected)
  935. return;
  936. if (selected)
  937. selected->select(false);
  938. selected = newSelection;
  939. if (onSelect)
  940. onSelect(selectedIndex());
  941. if (selected)
  942. selected->select(true);
  943. }
  944. int CComponentBox::selectedIndex()
  945. {
  946. if (selected)
  947. return std::find(components.begin(), components.end(), selected) - components.begin();
  948. return -1;
  949. }
  950. Point CComponentBox::getOrTextPos(CComponent *left, CComponent *right)
  951. {
  952. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  953. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  954. int fullDistance = getDistance(left, right) + leftSubtitle + rightSubtitle;
  955. return Point(fullDistance/2 - leftSubtitle, (left->image->pos.h + right->image->pos.h) / 4);
  956. }
  957. int CComponentBox::getDistance(CComponent *left, CComponent *right)
  958. {
  959. static const int betweenImagesMin = 20;
  960. static const int betweenSubtitlesMin = 10;
  961. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  962. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  963. int subtitlesOffset = leftSubtitle + rightSubtitle;
  964. return std::max(betweenSubtitlesMin, betweenImagesMin - subtitlesOffset);
  965. }
  966. void CComponentBox::placeComponents(bool selectable)
  967. {
  968. static const int betweenRows = 22;
  969. OBJ_CONSTRUCTION_CAPTURING_ALL;
  970. if (components.empty())
  971. return;
  972. //prepare components
  973. BOOST_FOREACH(auto & comp, components)
  974. {
  975. addChild(comp);
  976. comp->moveTo(Point(pos.x, pos.y));
  977. }
  978. struct RowData
  979. {
  980. size_t comps;
  981. int width;
  982. int height;
  983. RowData (size_t Comps, int Width, int Height):
  984. comps(Comps), width (Width), height (Height){};
  985. };
  986. std::vector<RowData> rows;
  987. rows.push_back (RowData (0,0,0));
  988. //split components in rows
  989. CComponent * prevComp = nullptr;
  990. BOOST_FOREACH(CComponent * comp, components)
  991. {
  992. //make sure that components are smaller than our width
  993. //assert(pos.w == 0 || pos.w < comp->pos.w);
  994. const int distance = prevComp ? getDistance(prevComp, comp) : 0;
  995. //start next row
  996. if ((pos.w != 0 && rows.back().width + comp->pos.w + distance > pos.w) // row is full
  997. || rows.back().comps >= 4) // no more than 4 comps per row
  998. {
  999. prevComp = nullptr;
  1000. rows.push_back (RowData (0,0,0));
  1001. }
  1002. if (prevComp)
  1003. rows.back().width += distance;
  1004. rows.back().comps++;
  1005. rows.back().width += comp->pos.w;
  1006. vstd::amax(rows.back().height, comp->pos.h);
  1007. prevComp = comp;
  1008. }
  1009. if (pos.w == 0)
  1010. {
  1011. BOOST_FOREACH(auto & row, rows)
  1012. vstd::amax(pos.w, row.width);
  1013. }
  1014. int height = (rows.size() - 1) * betweenRows;
  1015. BOOST_FOREACH(auto & row, rows)
  1016. height += row.height;
  1017. //assert(pos.h == 0 || pos.h < height);
  1018. if (pos.h == 0)
  1019. pos.h = height;
  1020. auto iter = components.begin();
  1021. int currentY = (pos.h - height) / 2;
  1022. //move components to their positions
  1023. for (size_t row = 0; row < rows.size(); row++)
  1024. {
  1025. // amount of free space we may add on each side of every component
  1026. int freeSpace = (pos.w - rows[row].width) / (rows[row].comps * 2);
  1027. prevComp = nullptr;
  1028. int currentX = 0;
  1029. for (size_t col = 0; col < rows[row].comps; col++)
  1030. {
  1031. currentX += freeSpace;
  1032. if (prevComp)
  1033. {
  1034. if (selectable)
  1035. {
  1036. Point orPos = Point(currentX - freeSpace, currentY) + getOrTextPos(prevComp, *iter);
  1037. new CLabel(orPos.x, orPos.y, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[4]);
  1038. }
  1039. currentX += getDistance(prevComp, *iter);
  1040. }
  1041. (*iter)->moveBy(Point(currentX, currentY));
  1042. currentX += (*iter)->pos.w;
  1043. currentX += freeSpace;
  1044. prevComp = *(iter++);
  1045. }
  1046. currentY += rows[row].height + betweenRows;
  1047. }
  1048. }
  1049. CComponentBox::CComponentBox(CComponent * _components, Rect position):
  1050. components(1, _components),
  1051. selected(nullptr)
  1052. {
  1053. type |= REDRAW_PARENT;
  1054. pos = position + pos;
  1055. placeComponents(false);
  1056. }
  1057. CComponentBox::CComponentBox(std::vector<CComponent *> _components, Rect position):
  1058. components(_components),
  1059. selected(nullptr)
  1060. {
  1061. type |= REDRAW_PARENT;
  1062. pos = position + pos;
  1063. placeComponents(false);
  1064. }
  1065. CComponentBox::CComponentBox(std::vector<CSelectableComponent *> _components, Rect position, boost::function<void(int newID)> _onSelect):
  1066. components(_components.begin(), _components.end()),
  1067. selected(nullptr),
  1068. onSelect(_onSelect)
  1069. {
  1070. type |= REDRAW_PARENT;
  1071. pos = position + pos;
  1072. placeComponents(true);
  1073. assert(!components.empty());
  1074. int key = SDLK_1;
  1075. BOOST_FOREACH(auto & comp, _components)
  1076. {
  1077. comp->onSelect = boost::bind(&CComponentBox::selectionChanged, this, comp);
  1078. comp->assignedKeys.insert(key++);
  1079. }
  1080. selectionChanged(_components.front());
  1081. }
  1082. void CSelWindow::selectionChange(unsigned to)
  1083. {
  1084. for (unsigned i=0;i<components.size();i++)
  1085. {
  1086. CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
  1087. if (!pom)
  1088. continue;
  1089. pom->select(i==to);
  1090. }
  1091. redraw();
  1092. }
  1093. CSelWindow::CSelWindow(const std::string &Text, PlayerColor player, int charperline, const std::vector<CSelectableComponent*> &comps, const std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons, QueryID askID)
  1094. {
  1095. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1096. ID = askID;
  1097. for(int i=0;i<Buttons.size();i++)
  1098. {
  1099. buttons.push_back(new CAdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  1100. if(!i && askID.getNum() >= 0)
  1101. buttons.back()->callback += boost::bind(&CSelWindow::madeChoice,this);
  1102. buttons[i]->callback += boost::bind(&CInfoWindow::close,this); //each button will close the window apart from call-defined actions
  1103. }
  1104. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::WHITE);
  1105. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  1106. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  1107. if(buttons.size() > 1 && askID.getNum() >= 0) //cancel button functionality
  1108. buttons.back()->callback += boost::bind(&CCallback::selectionMade,LOCPLINT->cb.get(),0,askID);
  1109. for(int i=0;i<comps.size();i++)
  1110. {
  1111. comps[i]->recActions = 255;
  1112. addChild(comps[i]);
  1113. components.push_back(comps[i]);
  1114. comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
  1115. if(i<9)
  1116. comps[i]->assignedKeys.insert(SDLK_1+i);
  1117. }
  1118. CMessage::drawIWindow(this, Text, player);
  1119. }
  1120. void CSelWindow::madeChoice()
  1121. {
  1122. if(ID.getNum() < 0)
  1123. return;
  1124. int ret = -1;
  1125. for (int i=0;i<components.size();i++)
  1126. {
  1127. if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
  1128. {
  1129. ret = i;
  1130. }
  1131. }
  1132. LOCPLINT->cb->selectionMade(ret+1,ID);
  1133. }
  1134. CCreaturePic::CCreaturePic(int x, int y, const CCreature *cre, bool Big, bool Animated)
  1135. {
  1136. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1137. pos.x+=x;
  1138. pos.y+=y;
  1139. TFaction faction = cre->faction;
  1140. assert(CGI->townh->factions.size() > faction);
  1141. if(Big)
  1142. bg = new CPicture(CGI->townh->factions[faction]->creatureBg130);
  1143. else
  1144. bg = new CPicture(CGI->townh->factions[faction]->creatureBg120);
  1145. bg->needRefresh = true;
  1146. anim = new CCreatureAnim(0, 0, cre->animDefName, Rect());
  1147. anim->clipRect(cre->isDoubleWide()?170:150, 155, bg->pos.w, bg->pos.h);
  1148. anim->startPreview(cre->hasBonusOfType(Bonus::SIEGE_WEAPON));
  1149. pos.w = bg->pos.w;
  1150. pos.h = bg->pos.h;
  1151. }
  1152. CRecruitmentWindow::CCreatureCard::CCreatureCard(CRecruitmentWindow *window, const CCreature *crea, int totalAmount):
  1153. CIntObject(LCLICK | RCLICK),
  1154. parent(window),
  1155. selected(false),
  1156. creature(crea),
  1157. amount(totalAmount)
  1158. {
  1159. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1160. pic = new CCreaturePic(1,1, creature, true, true);
  1161. // 1 + 1 px for borders
  1162. pos.w = pic->pos.w + 2;
  1163. pos.h = pic->pos.h + 2;
  1164. }
  1165. void CRecruitmentWindow::CCreatureCard::select(bool on)
  1166. {
  1167. selected = on;
  1168. redraw();
  1169. }
  1170. void CRecruitmentWindow::CCreatureCard::clickLeft(tribool down, bool previousState)
  1171. {
  1172. if (down)
  1173. parent->select(this);
  1174. }
  1175. void CRecruitmentWindow::CCreatureCard::clickRight(tribool down, bool previousState)
  1176. {
  1177. if (down)
  1178. GH.pushInt(createCreWindow(creature->idNumber, CCreatureWindow::OTHER, 0));
  1179. }
  1180. void CRecruitmentWindow::CCreatureCard::showAll(SDL_Surface * to)
  1181. {
  1182. CIntObject::showAll(to);
  1183. if (selected)
  1184. drawBorder(to, pos, int3(248, 0, 0));
  1185. else
  1186. drawBorder(to, pos, int3(232, 212, 120));
  1187. }
  1188. CRecruitmentWindow::CCostBox::CCostBox(Rect position, std::string title)
  1189. {
  1190. type |= REDRAW_PARENT;
  1191. pos = position + pos;
  1192. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1193. new CLabel(pos.w/2, 10, FONT_SMALL, CENTER, Colors::WHITE, title);
  1194. }
  1195. void CRecruitmentWindow::CCostBox::set(TResources res)
  1196. {
  1197. //just update values
  1198. BOOST_FOREACH(auto & item, resources)
  1199. {
  1200. item.second.first->setTxt(boost::lexical_cast<std::string>(res[item.first]));
  1201. }
  1202. }
  1203. void CRecruitmentWindow::CCostBox::createItems(TResources res)
  1204. {
  1205. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1206. BOOST_FOREACH(auto & curr, resources)
  1207. {
  1208. delete curr.second.first;
  1209. delete curr.second.second;
  1210. }
  1211. resources.clear();
  1212. TResources::nziterator iter(res);
  1213. while (iter.valid())
  1214. {
  1215. CAnimImage * image = new CAnimImage("RESOURCE", iter->resType);
  1216. CLabel * text = new CLabel(15, 43, FONT_SMALL, CENTER, Colors::WHITE, "0");
  1217. resources.insert(std::make_pair(iter->resType, std::make_pair(text, image)));
  1218. iter++;
  1219. }
  1220. if (!resources.empty())
  1221. {
  1222. int curx = pos.w / 2 - (16 * resources.size()) - (8 * (resources.size() - 1));
  1223. //reverse to display gold as first resource
  1224. BOOST_REVERSE_FOREACH(auto & res, resources)
  1225. {
  1226. res.second.first->moveBy(Point(curx, 22));
  1227. res.second.second->moveBy(Point(curx, 22));
  1228. curx += 48;
  1229. }
  1230. }
  1231. redraw();
  1232. }
  1233. void CRecruitmentWindow::select(CCreatureCard *card)
  1234. {
  1235. if (card == selected)
  1236. return;
  1237. if (selected)
  1238. selected->select(false);
  1239. selected = card;
  1240. if (selected)
  1241. selected->select(true);
  1242. if (card)
  1243. {
  1244. si32 maxAmount = card->creature->maxAmount(LOCPLINT->cb->getResourceAmount());
  1245. vstd::amin(maxAmount, card->amount);
  1246. slider->setAmount(maxAmount);
  1247. if(slider->value != maxAmount)
  1248. slider->moveTo(maxAmount);
  1249. else // if slider already at 0 - emulate call to sliderMoved()
  1250. sliderMoved(maxAmount);
  1251. costPerTroopValue->createItems(card->creature->cost);
  1252. totalCostValue->createItems(card->creature->cost);
  1253. costPerTroopValue->set(card->creature->cost);
  1254. totalCostValue->set(card->creature->cost * maxAmount);
  1255. //Recruit %s
  1256. title->setTxt(boost::str(boost::format(CGI->generaltexth->tcommands[21]) % card->creature->namePl));
  1257. maxButton->block(maxAmount == 0);
  1258. slider->block(maxAmount == 0);
  1259. }
  1260. }
  1261. void CRecruitmentWindow::buy()
  1262. {
  1263. CreatureID crid = selected->creature->idNumber;
  1264. SlotID dstslot = dst-> getSlotFor(crid);
  1265. if(!dstslot.validSlot() && !vstd::contains(CGI->arth->bigArtifacts,CGI->arth->creatureToMachineID(crid))) //no available slot
  1266. {
  1267. std::string txt;
  1268. if(dst->ID == Obj::HERO)
  1269. {
  1270. txt = CGI->generaltexth->allTexts[425]; //The %s would join your hero, but there aren't enough provisions to support them.
  1271. boost::algorithm::replace_first(txt, "%s", slider->value > 1 ? CGI->creh->creatures[crid]->namePl : CGI->creh->creatures[crid]->nameSing);
  1272. }
  1273. else
  1274. {
  1275. txt = CGI->generaltexth->allTexts[17]; //There is no room in the garrison for this army.
  1276. }
  1277. LOCPLINT->showInfoDialog(txt);
  1278. return;
  1279. }
  1280. onRecruit(crid, slider->value);
  1281. if(level >= 0)
  1282. close();
  1283. }
  1284. void CRecruitmentWindow::showAll(SDL_Surface * to)
  1285. {
  1286. CWindowObject::showAll(to);
  1287. // recruit\total values
  1288. drawBorder(to, pos.x + 172, pos.y + 222, 67, 42, int3(239,215,123));
  1289. drawBorder(to, pos.x + 246, pos.y + 222, 67, 42, int3(239,215,123));
  1290. //cost boxes
  1291. drawBorder(to, pos.x + 64, pos.y + 222, 99, 76, int3(239,215,123));
  1292. drawBorder(to, pos.x + 322, pos.y + 222, 99, 76, int3(239,215,123));
  1293. //buttons borders
  1294. drawBorder(to, pos.x + 133, pos.y + 312, 66, 34, int3(173,142,66));
  1295. drawBorder(to, pos.x + 211, pos.y + 312, 66, 34, int3(173,142,66));
  1296. drawBorder(to, pos.x + 289, pos.y + 312, 66, 34, int3(173,142,66));
  1297. }
  1298. CRecruitmentWindow::CRecruitmentWindow(const CGDwelling *Dwelling, int Level, const CArmedInstance *Dst, const boost::function<void(CreatureID,int)> &Recruit, int y_offset):
  1299. CWindowObject(PLAYER_COLORED, "TPRCRT"),
  1300. onRecruit(Recruit),
  1301. level(Level),
  1302. dst(Dst),
  1303. selected(nullptr),
  1304. dwelling(Dwelling)
  1305. {
  1306. moveBy(Point(0, y_offset));
  1307. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1308. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1309. slider = new CSlider(176,279,135,0,0,0,0,true);
  1310. slider->moved = boost::bind(&CRecruitmentWindow::sliderMoved,this, _1);
  1311. maxButton = new CAdventureMapButton(CGI->generaltexth->zelp[553],boost::bind(&CSlider::moveToMax,slider),134,313,"IRCBTNS.DEF",SDLK_m);
  1312. buyButton = new CAdventureMapButton(CGI->generaltexth->zelp[554],boost::bind(&CRecruitmentWindow::buy,this),212,313,"IBY6432.DEF",SDLK_RETURN);
  1313. cancelButton = new CAdventureMapButton(CGI->generaltexth->zelp[555],boost::bind(&CRecruitmentWindow::close,this),290,313,"ICN6432.DEF",SDLK_ESCAPE);
  1314. title = new CLabel(243, 32, FONT_BIG, CENTER, Colors::YELLOW);
  1315. availableValue = new CLabel(205, 253, FONT_SMALL, CENTER, Colors::WHITE);
  1316. toRecruitValue = new CLabel(279, 253, FONT_SMALL, CENTER, Colors::WHITE);
  1317. costPerTroopValue = new CCostBox(Rect(65, 222, 97, 74), CGI->generaltexth->allTexts[346]);
  1318. totalCostValue = new CCostBox(Rect(323, 222, 97, 74), CGI->generaltexth->allTexts[466]);
  1319. new CLabel(205, 233, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[465]); //available t
  1320. new CLabel(279, 233, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[16]); //recruit t
  1321. availableCreaturesChanged();
  1322. }
  1323. void CRecruitmentWindow::availableCreaturesChanged()
  1324. {
  1325. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1326. size_t selectedIndex = 0;
  1327. if (!cards.empty() && selected) // find position of selected item
  1328. selectedIndex = std::find(cards.begin(), cards.end(), selected) - cards.begin();
  1329. //deselect card
  1330. select(nullptr);
  1331. //delete old cards
  1332. BOOST_FOREACH(auto & card, cards)
  1333. delete card;
  1334. cards.clear();
  1335. for(int i=0; i<dwelling->creatures.size(); i++)
  1336. {
  1337. //find appropriate level
  1338. if(level >= 0 && i != level)
  1339. continue;
  1340. int amount = dwelling->creatures[i].first;
  1341. //create new cards
  1342. BOOST_REVERSE_FOREACH(auto & creature, dwelling->creatures[i].second)
  1343. cards.push_back(new CCreatureCard(this, CGI->creh->creatures[creature], amount));
  1344. }
  1345. assert(!cards.empty());
  1346. const int creatureWidth = 102;
  1347. //normal distance between cards - 18px
  1348. int requiredSpace = 18;
  1349. //maximum distance we can use without reaching window borders
  1350. int availableSpace = pos.w - 50 - creatureWidth * cards.size();
  1351. if (cards.size() > 1) // avoid division by zero
  1352. availableSpace /= cards.size() - 1;
  1353. else
  1354. availableSpace = 0;
  1355. assert(availableSpace >= 0);
  1356. const int spaceBetween = std::min(requiredSpace, availableSpace);
  1357. const int totalCreatureWidth = spaceBetween + creatureWidth;
  1358. //now we know total amount of cards and can move them to correct position
  1359. int curx = pos.w / 2 - (creatureWidth*cards.size()/2) - (spaceBetween*(cards.size()-1)/2);
  1360. BOOST_FOREACH(auto & card, cards)
  1361. {
  1362. card->moveBy(Point(curx, 64));
  1363. curx += totalCreatureWidth;
  1364. }
  1365. //restore selection
  1366. select(cards[selectedIndex]);
  1367. if(slider->value == slider->amount)
  1368. slider->moveTo(slider->amount);
  1369. else // if slider already at 0 - emulate call to sliderMoved()
  1370. sliderMoved(slider->amount);
  1371. }
  1372. void CRecruitmentWindow::sliderMoved(int to)
  1373. {
  1374. if (!selected)
  1375. return;
  1376. buyButton->block(!to);
  1377. availableValue->setTxt(boost::lexical_cast<std::string>(selected->amount - to));
  1378. toRecruitValue->setTxt(boost::lexical_cast<std::string>(to));
  1379. totalCostValue->set(selected->creature->cost * to);
  1380. }
  1381. CSplitWindow::CSplitWindow(const CCreature * creature, boost::function<void(int, int)> callback_,
  1382. int leftMin_, int rightMin_, int leftAmount_, int rightAmount_):
  1383. CWindowObject(PLAYER_COLORED, "GPUCRDIV"),
  1384. callback(callback_),
  1385. leftAmount(leftAmount_),
  1386. rightAmount(rightAmount_),
  1387. leftMin(leftMin_),
  1388. rightMin(rightMin_),
  1389. slider(nullptr)
  1390. {
  1391. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1392. int total = leftAmount + rightAmount;
  1393. int leftMax = total - rightMin;
  1394. int rightMax = total - leftMin;
  1395. ok = new CAdventureMapButton("", "", boost::bind(&CSplitWindow::apply, this), 20, 263, "IOK6432", SDLK_RETURN);
  1396. cancel = new CAdventureMapButton("", "", boost::bind(&CSplitWindow::close, this), 214, 263, "ICN6432", SDLK_ESCAPE);
  1397. int sliderPositions = total - leftMin - rightMin;
  1398. leftInput = new CTextInput(Rect(20, 218, 100, 36), FONT_BIG, boost::bind(&CSplitWindow::setAmountText, this, _1, true));
  1399. rightInput = new CTextInput(Rect(176, 218, 100, 36), FONT_BIG, boost::bind(&CSplitWindow::setAmountText, this, _1, false));
  1400. //add filters to allow only number input
  1401. leftInput->filters.add(boost::bind(&CTextInput::numberFilter, _1, _2, leftMin, leftMax));
  1402. rightInput->filters.add(boost::bind(&CTextInput::numberFilter, _1, _2, rightMin, rightMax));
  1403. leftInput->setTxt(boost::lexical_cast<std::string>(leftAmount), false);
  1404. rightInput->setTxt(boost::lexical_cast<std::string>(rightAmount), false);
  1405. animLeft = new CCreaturePic(20, 54, creature, true, false);
  1406. animRight = new CCreaturePic(177, 54,creature, true, false);
  1407. slider = new CSlider(21, 194, 257, boost::bind(&CSplitWindow::sliderMoved, this, _1), 0, sliderPositions, rightAmount - rightMin, true);
  1408. std::string title = CGI->generaltexth->allTexts[256];
  1409. boost::algorithm::replace_first(title,"%s", creature->namePl);
  1410. new CLabel(150, 34, FONT_BIG, CENTER, Colors::YELLOW, title);
  1411. }
  1412. void CSplitWindow::setAmountText(std::string text, bool left)
  1413. {
  1414. try
  1415. {
  1416. setAmount(boost::lexical_cast<int>(text), left);
  1417. slider->moveTo(rightAmount - rightMin);
  1418. }
  1419. catch(boost::bad_lexical_cast &)
  1420. {
  1421. }
  1422. }
  1423. void CSplitWindow::setAmount(int value, bool left)
  1424. {
  1425. int total = leftAmount + rightAmount;
  1426. leftAmount = left ? value : total - value;
  1427. rightAmount = left ? total - value : value;
  1428. leftInput->setTxt(boost::lexical_cast<std::string>(leftAmount));
  1429. rightInput->setTxt(boost::lexical_cast<std::string>(rightAmount));
  1430. }
  1431. void CSplitWindow::apply()
  1432. {
  1433. callback(leftAmount, rightAmount);
  1434. close();
  1435. }
  1436. void CSplitWindow::sliderMoved(int to)
  1437. {
  1438. setAmount(rightMin + to, false);
  1439. }
  1440. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> &skills, boost::function<void(ui32)> callback):
  1441. CWindowObject(PLAYER_COLORED, "LVLUPBKG"),
  1442. cb(callback)
  1443. {
  1444. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1445. LOCPLINT->showingDialog->setn(true);
  1446. new CAnimImage("PortraitsLarge", hero->portrait, 0, 170, 66);
  1447. new CAdventureMapButton("", "", boost::bind(&CLevelWindow::close, this), 297, 413, "IOKAY", SDLK_RETURN);
  1448. //%s has gained a level.
  1449. new CLabel(192, 33, FONT_MEDIUM, CENTER, Colors::WHITE,
  1450. boost::str(boost::format(CGI->generaltexth->allTexts[444]) % hero->name));
  1451. //%s is now a level %d %s.
  1452. new CLabel(192, 162, FONT_MEDIUM, CENTER, Colors::WHITE,
  1453. boost::str(boost::format(CGI->generaltexth->allTexts[445]) % hero->name % hero->level % hero->type->heroClass->name));
  1454. new CAnimImage("PSKIL42", pskill, 0, 174, 190);
  1455. new CLabel(192, 253, FONT_MEDIUM, CENTER, Colors::WHITE,
  1456. CGI->generaltexth->primarySkillNames[pskill] + " +1");
  1457. if (!skills.empty())
  1458. {
  1459. std::vector<CSelectableComponent *> comps;
  1460. for(size_t i=0;i<skills.size();i++)
  1461. {
  1462. comps.push_back(new CSelectableComponent(
  1463. CComponent::secskill,
  1464. skills[i],
  1465. hero->getSecSkillLevel( SecondarySkill(skills[i]) )+1,
  1466. CComponent::medium));
  1467. }
  1468. box = new CComponentBox(comps, Rect(75, 300, pos.w - 150, 100));
  1469. }
  1470. else
  1471. box = nullptr;
  1472. }
  1473. CLevelWindow::~CLevelWindow()
  1474. {
  1475. //FIXME: call callback if there was nothing to select?
  1476. if (box && box->selectedIndex() != -1)
  1477. cb(box->selectedIndex());
  1478. LOCPLINT->showingDialog->setn(false);
  1479. }
  1480. void CMinorResDataBar::show(SDL_Surface * to)
  1481. {
  1482. }
  1483. void CMinorResDataBar::showAll(SDL_Surface * to)
  1484. {
  1485. blitAt(bg,pos.x,pos.y,to);
  1486. for (Res::ERes i=Res::WOOD; i<=Res::GOLD; vstd::advance(i, 1))
  1487. {
  1488. std::string text = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(i));
  1489. graphics->fonts[FONT_SMALL]->renderTextCenter(to, text, Colors::WHITE, Point(pos.x + 50 + 76 * i, pos.y + pos.h/2));
  1490. }
  1491. std::vector<std::string> temp;
  1492. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::MONTH)));
  1493. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK)));
  1494. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK)));
  1495. std::string datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  1496. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  1497. graphics->fonts[FONT_SMALL]->renderTextCenter(to, processStr(datetext,temp), Colors::WHITE, Point(pos.x+545+(pos.w-545)/2,pos.y+pos.h/2));
  1498. }
  1499. CMinorResDataBar::CMinorResDataBar()
  1500. {
  1501. bg = BitmapHandler::loadBitmap("KRESBAR.bmp");
  1502. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  1503. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  1504. pos.x = 7;
  1505. pos.y = 575;
  1506. pos.w = bg->w;
  1507. pos.h = bg->h;
  1508. }
  1509. CMinorResDataBar::~CMinorResDataBar()
  1510. {
  1511. SDL_FreeSurface(bg);
  1512. }
  1513. CObjectListWindow::CItem::CItem(CObjectListWindow *_parent, size_t _id, std::string _text):
  1514. parent(_parent),
  1515. index(_id)
  1516. {
  1517. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1518. border = new CPicture("TPGATES");
  1519. pos = border->pos;
  1520. addUsedEvents(LCLICK);
  1521. type |= REDRAW_PARENT;
  1522. text = new CLabel(pos.w/2, pos.h/2, FONT_SMALL, CENTER, Colors::WHITE, _text);
  1523. select(index == parent->selected);
  1524. }
  1525. void CObjectListWindow::CItem::select(bool on)
  1526. {
  1527. if (on)
  1528. border->recActions = 255;
  1529. else
  1530. border->recActions = ~(UPDATE | SHOWALL);
  1531. redraw();
  1532. }
  1533. void CObjectListWindow::CItem::clickLeft(tribool down, bool previousState)
  1534. {
  1535. if( previousState && !down)
  1536. parent->changeSelection(index);
  1537. }
  1538. CObjectListWindow::CObjectListWindow(const std::vector<int> &_items, CPicture * titlePic, std::string _title, std::string _descr,
  1539. boost::function<void(int)> Callback):
  1540. CWindowObject(PLAYER_COLORED, "TPGATE"),
  1541. onSelect(Callback)
  1542. {
  1543. items.reserve(_items.size());
  1544. BOOST_FOREACH(int id, _items)
  1545. {
  1546. items.push_back(std::make_pair(id, CGI->mh->map->objects[id]->hoverName));
  1547. }
  1548. init(titlePic, _title, _descr);
  1549. }
  1550. CObjectListWindow::CObjectListWindow(const std::vector<std::string> &_items, CPicture * titlePic, std::string _title, std::string _descr,
  1551. boost::function<void(int)> Callback):
  1552. CWindowObject(PLAYER_COLORED, "TPGATE"),
  1553. onSelect(Callback)
  1554. {
  1555. items.reserve(_items.size());
  1556. for (size_t i=0; i<_items.size(); i++)
  1557. items.push_back(std::make_pair(int(i), _items[i]));
  1558. init(titlePic, _title, _descr);
  1559. }
  1560. void CObjectListWindow::init(CPicture * titlePic, std::string _title, std::string _descr)
  1561. {
  1562. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1563. title = new CLabel(152, 27, FONT_BIG, CENTER, Colors::YELLOW, _title);
  1564. descr = new CLabel(145, 133, FONT_SMALL, CENTER, Colors::WHITE, _descr);
  1565. ok = new CAdventureMapButton("","",boost::bind(&CObjectListWindow::elementSelected, this),15,402,"IOKAY.DEF", SDLK_RETURN);
  1566. ok->block(true);
  1567. exit = new CAdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally,&GH, this),228,402,"ICANCEL.DEF",SDLK_ESCAPE);
  1568. if (titlePic)
  1569. {
  1570. titleImage = titlePic;
  1571. addChild(titleImage);
  1572. titleImage->recActions = defActions;
  1573. titleImage->pos.x = pos.w/2 + pos.x - titleImage->pos.w/2;
  1574. titleImage->pos.y =75 + pos.y - titleImage->pos.h/2;
  1575. }
  1576. list = new CListBox(boost::bind(&CObjectListWindow::genItem, this, _1), CListBox::DestroyFunc(),
  1577. Point(14, 151), Point(0, 25), 9, items.size(), 0, 1, Rect(262, -32, 256, 256) );
  1578. list->type |= REDRAW_PARENT;
  1579. }
  1580. CIntObject * CObjectListWindow::genItem(size_t index)
  1581. {
  1582. if (index < items.size())
  1583. return new CItem(this, index, items[index].second);
  1584. return NULL;
  1585. }
  1586. void CObjectListWindow::elementSelected()
  1587. {
  1588. boost::function<void(int)> toCall = onSelect;//save
  1589. int where = items[selected].first; //required variables
  1590. GH.popIntTotally(this);//then destroy window
  1591. toCall(where);//and send selected object
  1592. }
  1593. void CObjectListWindow::changeSelection(size_t which)
  1594. {
  1595. ok->block(false);
  1596. if (selected == which)
  1597. return;
  1598. std::list< CIntObject * > elements = list->getItems();
  1599. BOOST_FOREACH(CIntObject * element, elements)
  1600. {
  1601. CItem *item;
  1602. if ( (item = dynamic_cast<CItem*>(element)) )
  1603. {
  1604. if (item->index == selected)
  1605. item->select(false);
  1606. if (item->index == which)
  1607. item->select(true);
  1608. }
  1609. }
  1610. selected = which;
  1611. }
  1612. void CObjectListWindow::keyPressed (const SDL_KeyboardEvent & key)
  1613. {
  1614. if(key.state != SDL_PRESSED)
  1615. return;
  1616. int sel = selected;
  1617. switch(key.keysym.sym)
  1618. {
  1619. break; case SDLK_UP:
  1620. sel -=1;
  1621. break; case SDLK_DOWN:
  1622. sel +=1;
  1623. break; case SDLK_PAGEUP:
  1624. sel -=9;
  1625. break; case SDLK_PAGEDOWN:
  1626. sel +=9;
  1627. break; case SDLK_HOME:
  1628. sel = 0;
  1629. break; case SDLK_END:
  1630. sel = items.size();
  1631. break; default:
  1632. return;
  1633. }
  1634. vstd::abetween(sel, 0, items.size()-1);
  1635. list->scrollTo(sel);
  1636. changeSelection(sel);
  1637. }
  1638. CTradeWindow::CTradeableItem::CTradeableItem(Point pos, EType Type, int ID, bool Left, int Serial):
  1639. CIntObject(LCLICK | HOVER | RCLICK, pos),
  1640. type(EType(-1)),// set to invalid, will be corrected in setType
  1641. id(ID),
  1642. serial(Serial),
  1643. left(Left)
  1644. {
  1645. downSelection = false;
  1646. hlp = NULL;
  1647. image = nullptr;
  1648. setType(Type);
  1649. }
  1650. void CTradeWindow::CTradeableItem::setType(EType newType)
  1651. {
  1652. if (type != newType)
  1653. {
  1654. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1655. type = newType;
  1656. delete image;
  1657. if (getIndex() < 0)
  1658. {
  1659. image = new CAnimImage(getFilename(), 0);
  1660. image->disable();
  1661. }
  1662. else
  1663. image = new CAnimImage(getFilename(), getIndex());
  1664. }
  1665. }
  1666. void CTradeWindow::CTradeableItem::setID(int newID)
  1667. {
  1668. if (id != newID)
  1669. {
  1670. id = newID;
  1671. if (image)
  1672. {
  1673. int index = getIndex();
  1674. if (index < 0)
  1675. image->disable();
  1676. else
  1677. {
  1678. image->enable();
  1679. image->setFrame(index);
  1680. }
  1681. }
  1682. }
  1683. }
  1684. std::string CTradeWindow::CTradeableItem::getFilename()
  1685. {
  1686. switch(type)
  1687. {
  1688. case RESOURCE:
  1689. return "RESOURCE";
  1690. case PLAYER:
  1691. return "CREST58";
  1692. case ARTIFACT_TYPE:
  1693. case ARTIFACT_PLACEHOLDER:
  1694. case ARTIFACT_INSTANCE:
  1695. return "artifact";
  1696. case CREATURE:
  1697. return "TWCRPORT";
  1698. default:
  1699. return "";
  1700. }
  1701. }
  1702. int CTradeWindow::CTradeableItem::getIndex()
  1703. {
  1704. if (id < 0)
  1705. return -1;
  1706. switch(type)
  1707. {
  1708. case RESOURCE:
  1709. case PLAYER:
  1710. return id;
  1711. case ARTIFACT_TYPE:
  1712. case ARTIFACT_INSTANCE:
  1713. case ARTIFACT_PLACEHOLDER:
  1714. return VLC->arth->artifacts[id]->iconIndex;
  1715. case CREATURE:
  1716. return VLC->creh->creatures[id]->iconIndex;
  1717. default:
  1718. return -1;
  1719. }
  1720. }
  1721. void CTradeWindow::CTradeableItem::showAll(SDL_Surface * to)
  1722. {
  1723. Point posToBitmap;
  1724. Point posToSubCenter;
  1725. switch(type)
  1726. {
  1727. case RESOURCE:
  1728. posToBitmap = Point(19,9);
  1729. posToSubCenter = Point(36, 59);
  1730. break;
  1731. case CREATURE_PLACEHOLDER:
  1732. case CREATURE:
  1733. posToSubCenter = Point(29, 76);
  1734. if(downSelection)
  1735. posToSubCenter.y += 5;
  1736. break;
  1737. case PLAYER:
  1738. posToSubCenter = Point(31, 76);
  1739. break;
  1740. case ARTIFACT_PLACEHOLDER:
  1741. case ARTIFACT_INSTANCE:
  1742. posToSubCenter = Point(19, 55);
  1743. if(downSelection)
  1744. posToSubCenter.y += 8;
  1745. break;
  1746. case ARTIFACT_TYPE:
  1747. posToSubCenter = Point(19, 58);
  1748. break;
  1749. }
  1750. if (image)
  1751. {
  1752. image->moveTo(pos.topLeft() + posToBitmap);
  1753. CIntObject::showAll(to);
  1754. }
  1755. printAtMiddleLoc(subtitle, posToSubCenter, FONT_SMALL, Colors::WHITE, to);
  1756. }
  1757. void CTradeWindow::CTradeableItem::clickLeft(tribool down, bool previousState)
  1758. {
  1759. CTradeWindow *mw = dynamic_cast<CTradeWindow *>(parent);
  1760. assert(mw);
  1761. if(down)
  1762. {
  1763. if(type == ARTIFACT_PLACEHOLDER)
  1764. {
  1765. CAltarWindow *aw = static_cast<CAltarWindow *>(mw);
  1766. if(const CArtifactInstance *movedArt = aw->arts->commonInfo->src.art)
  1767. {
  1768. aw->moveFromSlotToAltar(aw->arts->commonInfo->src.slotID, this, movedArt);
  1769. }
  1770. else if(const CArtifactInstance *art = getArtInstance())
  1771. {
  1772. aw->arts->commonInfo->src.AOH = aw->arts;
  1773. aw->arts->commonInfo->src.art = art;
  1774. aw->arts->commonInfo->src.slotID = aw->hero->getArtPos(art);
  1775. aw->arts->markPossibleSlots(art);
  1776. //aw->arts->commonInfo->dst.AOH = aw->arts;
  1777. CCS->curh->dragAndDropCursor(new CAnimImage("artifact", art->artType->iconIndex));
  1778. aw->arts->artifactsOnAltar.erase(art);
  1779. setID(-1);
  1780. subtitle = "";
  1781. aw->deal->block(!aw->arts->artifactsOnAltar.size());
  1782. }
  1783. aw->calcTotalExp();
  1784. return;
  1785. }
  1786. if(left)
  1787. {
  1788. if(mw->hLeft != this)
  1789. mw->hLeft = this;
  1790. else
  1791. return;
  1792. }
  1793. else
  1794. {
  1795. if(mw->hRight != this)
  1796. mw->hRight = this;
  1797. else
  1798. return;
  1799. }
  1800. mw->selectionChanged(left);
  1801. }
  1802. }
  1803. void CTradeWindow::CTradeableItem::showAllAt(const Point &dstPos, const std::string &customSub, SDL_Surface * to)
  1804. {
  1805. Rect oldPos = pos;
  1806. std::string oldSub = subtitle;
  1807. downSelection = true;
  1808. moveTo(dstPos);
  1809. subtitle = customSub;
  1810. showAll(to);
  1811. downSelection = false;
  1812. moveTo(oldPos.topLeft());
  1813. subtitle = oldSub;
  1814. }
  1815. void CTradeWindow::CTradeableItem::hover(bool on)
  1816. {
  1817. if(!on)
  1818. {
  1819. GH.statusbar->clear();
  1820. return;
  1821. }
  1822. switch(type)
  1823. {
  1824. case CREATURE:
  1825. case CREATURE_PLACEHOLDER:
  1826. GH.statusbar->print(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->creatures[id]->namePl));
  1827. break;
  1828. case ARTIFACT_PLACEHOLDER:
  1829. if(id < 0)
  1830. GH.statusbar->print(CGI->generaltexth->zelp[582].first);
  1831. else
  1832. GH.statusbar->print(CGI->arth->artifacts[id]->Name());
  1833. break;
  1834. }
  1835. }
  1836. void CTradeWindow::CTradeableItem::clickRight(tribool down, bool previousState)
  1837. {
  1838. if(down)
  1839. {
  1840. switch(type)
  1841. {
  1842. case CREATURE:
  1843. case CREATURE_PLACEHOLDER:
  1844. //GH.statusbar->print(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->creatures[id]->namePl));
  1845. break;
  1846. case ARTIFACT_TYPE:
  1847. case ARTIFACT_PLACEHOLDER:
  1848. if(id >= 0)
  1849. adventureInt->handleRightClick(CGI->arth->artifacts[id]->Description(), down);
  1850. break;
  1851. }
  1852. }
  1853. }
  1854. std::string CTradeWindow::CTradeableItem::getName(int number /*= -1*/) const
  1855. {
  1856. switch(type)
  1857. {
  1858. case PLAYER:
  1859. return CGI->generaltexth->capColors[id];
  1860. case RESOURCE:
  1861. return CGI->generaltexth->restypes[id];
  1862. case CREATURE:
  1863. if(number == 1)
  1864. return CGI->creh->creatures[id]->nameSing;
  1865. else
  1866. return CGI->creh->creatures[id]->namePl;
  1867. case ARTIFACT_TYPE:
  1868. case ARTIFACT_INSTANCE:
  1869. return CGI->arth->artifacts[id]->Name();
  1870. }
  1871. assert(0);
  1872. return "";
  1873. }
  1874. const CArtifactInstance * CTradeWindow::CTradeableItem::getArtInstance() const
  1875. {
  1876. switch(type)
  1877. {
  1878. case ARTIFACT_PLACEHOLDER:
  1879. case ARTIFACT_INSTANCE:
  1880. return (const CArtifactInstance *)hlp;
  1881. default:
  1882. return NULL;
  1883. }
  1884. }
  1885. void CTradeWindow::CTradeableItem::setArtInstance(const CArtifactInstance *art)
  1886. {
  1887. assert(type == ARTIFACT_PLACEHOLDER || type == ARTIFACT_INSTANCE);
  1888. hlp = art;
  1889. if(art)
  1890. setID(art->artType->id);
  1891. else
  1892. setID(-1);
  1893. }
  1894. CTradeWindow::CTradeWindow(std::string bgName, const IMarket *Market, const CGHeroInstance *Hero, EMarketMode::EMarketMode Mode):
  1895. CWindowObject(PLAYER_COLORED, bgName),
  1896. market(Market),
  1897. hero(Hero),
  1898. arts(NULL),
  1899. hLeft(NULL),
  1900. hRight(NULL),
  1901. readyToTrade(false)
  1902. {
  1903. type |= BLOCK_ADV_HOTKEYS;
  1904. mode = Mode;
  1905. initTypes();
  1906. }
  1907. void CTradeWindow::initTypes()
  1908. {
  1909. switch(mode)
  1910. {
  1911. case EMarketMode::RESOURCE_RESOURCE:
  1912. itemsType[1] = RESOURCE;
  1913. itemsType[0] = RESOURCE;
  1914. break;
  1915. case EMarketMode::RESOURCE_PLAYER:
  1916. itemsType[1] = RESOURCE;
  1917. itemsType[0] = PLAYER;
  1918. break;
  1919. case EMarketMode::CREATURE_RESOURCE:
  1920. itemsType[1] = CREATURE;
  1921. itemsType[0] = RESOURCE;
  1922. break;
  1923. case EMarketMode::RESOURCE_ARTIFACT:
  1924. itemsType[1] = RESOURCE;
  1925. itemsType[0] = ARTIFACT_TYPE;
  1926. break;
  1927. case EMarketMode::ARTIFACT_RESOURCE:
  1928. itemsType[1] = ARTIFACT_INSTANCE;
  1929. itemsType[0] = RESOURCE;
  1930. break;
  1931. case EMarketMode::CREATURE_EXP:
  1932. itemsType[1] = CREATURE;
  1933. itemsType[0] = CREATURE_PLACEHOLDER;
  1934. break;
  1935. case EMarketMode::ARTIFACT_EXP:
  1936. itemsType[1] = ARTIFACT_TYPE;
  1937. itemsType[0] = ARTIFACT_PLACEHOLDER;
  1938. break;
  1939. }
  1940. }
  1941. void CTradeWindow::initItems(bool Left)
  1942. {
  1943. if(Left && (itemsType[1] == ARTIFACT_TYPE || itemsType[1] == ARTIFACT_INSTANCE))
  1944. {
  1945. int xOffset = 0, yOffset = 0;
  1946. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  1947. {
  1948. xOffset = -361;
  1949. yOffset = +46;
  1950. CTradeableItem *hlp = new CTradeableItem(Point(137, 469), itemsType[Left], -1, 1, 0);
  1951. hlp->recActions &= ~(UPDATE | SHOWALL);
  1952. items[Left].push_back(hlp);
  1953. }
  1954. else //ARTIFACT_EXP
  1955. {
  1956. xOffset = -363;
  1957. yOffset = -12;
  1958. }
  1959. BLOCK_CAPTURING;
  1960. arts = new CArtifactsOfHero(Point(pos.x+xOffset, pos.y+yOffset));
  1961. arts->commonInfo = new CArtifactsOfHero::SCommonPart;
  1962. arts->commonInfo->participants.insert(arts);
  1963. arts->recActions = 255;
  1964. arts->setHero(hero);
  1965. arts->allowedAssembling = false;
  1966. addChild(arts);
  1967. artSets.push_back(arts);
  1968. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  1969. arts->highlightModeCallback = boost::bind(&CTradeWindow::artifactSelected, this, _1);
  1970. return;
  1971. }
  1972. std::vector<int> *ids = getItemsIds(Left);
  1973. std::vector<Rect> pos;
  1974. int amount = -1;
  1975. getPositionsFor(pos, Left, itemsType[Left]);
  1976. if(Left || !ids)
  1977. amount = 7;
  1978. else
  1979. amount = ids->size();
  1980. if(ids)
  1981. vstd::amin(amount, ids->size());
  1982. for(int j=0; j<amount; j++)
  1983. {
  1984. int id = (ids && ids->size()>j) ? (*ids)[j] : j;
  1985. if(id < 0 && mode != EMarketMode::ARTIFACT_EXP) //when sacrificing artifacts we need to prepare empty slots
  1986. continue;
  1987. CTradeableItem *hlp = new CTradeableItem(pos[j].topLeft(), itemsType[Left], id, Left, j);
  1988. hlp->pos = pos[j] + this->pos.topLeft();
  1989. items[Left].push_back(hlp);
  1990. }
  1991. initSubs(Left);
  1992. }
  1993. std::vector<int> *CTradeWindow::getItemsIds(bool Left)
  1994. {
  1995. std::vector<int> *ids = NULL;
  1996. if(mode == EMarketMode::ARTIFACT_EXP)
  1997. return new std::vector<int>(22, -1);
  1998. if(Left)
  1999. {
  2000. switch(itemsType[1])
  2001. {
  2002. case CREATURE:
  2003. ids = new std::vector<int>;
  2004. for(int i = 0; i < 7; i++)
  2005. {
  2006. if(const CCreature *c = hero->getCreature(SlotID(i)))
  2007. ids->push_back(c->idNumber);
  2008. else
  2009. ids->push_back(-1);
  2010. }
  2011. break;
  2012. }
  2013. }
  2014. else
  2015. {
  2016. switch(itemsType[0])
  2017. {
  2018. case PLAYER:
  2019. ids = new std::vector<int>;
  2020. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  2021. if(PlayerColor(i) != LOCPLINT->playerID && LOCPLINT->cb->getPlayerStatus(PlayerColor(i)) == EPlayerStatus::INGAME)
  2022. ids->push_back(i);
  2023. break;
  2024. case ARTIFACT_TYPE:
  2025. ids = new std::vector<int>(market->availableItemsIds(mode));
  2026. break;
  2027. }
  2028. }
  2029. return ids;
  2030. }
  2031. void CTradeWindow::getPositionsFor(std::vector<Rect> &poss, bool Left, EType type) const
  2032. {
  2033. if(mode == EMarketMode::ARTIFACT_EXP && !Left)
  2034. {
  2035. //22 boxes, 5 in row, last row: two boxes centered
  2036. int h, w, x, y, dx, dy;
  2037. h = w = 44;
  2038. x = 317;
  2039. y = 53;
  2040. dx = 54;
  2041. dy = 70;
  2042. for (int i = 0; i < 4 ; i++)
  2043. for (int j = 0; j < 5 ; j++)
  2044. poss += Rect(x + dx*j, y + dy*i, w, h);
  2045. poss += Rect(x + dx*1.5, y + dy*4, w, h);
  2046. poss += Rect(x + dx*2.5, y + dy*4, w, h);
  2047. }
  2048. else
  2049. {
  2050. //seven boxes:
  2051. // X X X
  2052. // X X X
  2053. // X
  2054. int h, w, x, y, dx, dy;
  2055. int leftToRightOffset;
  2056. getBaseForPositions(type, dx, dy, x, y, h, w, !Left, leftToRightOffset);
  2057. poss += genRect(h, w, x, y), genRect(h, w, x + dx, y), genRect(h, w, x + 2*dx, y),
  2058. genRect(h, w, x, y + dy), genRect(h, w, x + dx, y + dy), genRect(h, w, x + 2*dx, y + dy),
  2059. genRect(h, w, x + dx, y + 2*dy);
  2060. if(!Left)
  2061. {
  2062. BOOST_FOREACH(Rect &r, poss)
  2063. r.x += leftToRightOffset;
  2064. }
  2065. }
  2066. }
  2067. void CTradeWindow::initSubs(bool Left)
  2068. {
  2069. BOOST_FOREACH(CTradeableItem *t, items[Left])
  2070. {
  2071. if(Left)
  2072. {
  2073. switch(itemsType[1])
  2074. {
  2075. case CREATURE:
  2076. t->subtitle = boost::lexical_cast<std::string>(hero->getStackCount(SlotID(t->serial)));
  2077. break;
  2078. case RESOURCE:
  2079. t->subtitle = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(static_cast<Res::ERes>(t->serial)));
  2080. break;
  2081. }
  2082. }
  2083. else //right side
  2084. {
  2085. if(itemsType[0] == PLAYER)
  2086. {
  2087. t->subtitle = CGI->generaltexth->capColors[t->id];
  2088. }
  2089. else if(hLeft)//artifact, creature
  2090. {
  2091. int h1, h2; //hlp variables for getting offer
  2092. market->getOffer(hLeft->id, t->id, h1, h2, mode);
  2093. if(t->id != hLeft->id || mode != EMarketMode::RESOURCE_RESOURCE) //don't allow exchanging same resources
  2094. {
  2095. std::ostringstream oss;
  2096. oss << h2;
  2097. if(h1!=1)
  2098. oss << "/" << h1;
  2099. t->subtitle = oss.str();
  2100. }
  2101. else
  2102. t->subtitle = CGI->generaltexth->allTexts[164]; // n/a
  2103. }
  2104. else
  2105. t->subtitle = "";
  2106. }
  2107. }
  2108. }
  2109. void CTradeWindow::showAll(SDL_Surface * to)
  2110. {
  2111. CWindowObject::showAll(to);
  2112. if(hRight)
  2113. CSDL_Ext::drawBorder(to,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  2114. if(hLeft && hLeft->type != ARTIFACT_INSTANCE)
  2115. CSDL_Ext::drawBorder(to,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  2116. if(readyToTrade)
  2117. {
  2118. hLeft->showAllAt(pos.topLeft() + selectionOffset(true), selectionSubtitle(true), to);
  2119. hRight->showAllAt(pos.topLeft() + selectionOffset(false), selectionSubtitle(false), to);
  2120. }
  2121. }
  2122. void CTradeWindow::removeItems(const std::set<CTradeableItem *> &toRemove)
  2123. {
  2124. BOOST_FOREACH(CTradeableItem *t, toRemove)
  2125. removeItem(t);
  2126. }
  2127. void CTradeWindow::removeItem(CTradeableItem * t)
  2128. {
  2129. items[t->left] -= t;
  2130. delete t;
  2131. if(hRight == t)
  2132. {
  2133. hRight = NULL;
  2134. selectionChanged(false);
  2135. }
  2136. }
  2137. void CTradeWindow::getEmptySlots(std::set<CTradeableItem *> &toRemove)
  2138. {
  2139. BOOST_FOREACH(CTradeableItem *t, items[1])
  2140. if(!hero->getStackCount(SlotID(t->serial)))
  2141. toRemove.insert(t);
  2142. }
  2143. void CTradeWindow::setMode(EMarketMode::EMarketMode Mode)
  2144. {
  2145. const IMarket *m = market;
  2146. const CGHeroInstance *h = hero;
  2147. CTradeWindow *nwindow = NULL;
  2148. GH.popIntTotally(this);
  2149. switch(Mode)
  2150. {
  2151. case EMarketMode::CREATURE_EXP:
  2152. case EMarketMode::ARTIFACT_EXP:
  2153. nwindow = new CAltarWindow(m, h, Mode);
  2154. break;
  2155. default:
  2156. nwindow = new CMarketplaceWindow(m, h, Mode);
  2157. break;
  2158. }
  2159. GH.pushInt(nwindow);
  2160. }
  2161. void CTradeWindow::artifactSelected(CArtPlace *slot)
  2162. {
  2163. assert(mode == EMarketMode::ARTIFACT_RESOURCE);
  2164. items[1][0]->setArtInstance(slot->ourArt);
  2165. if(slot->ourArt)
  2166. hLeft = items[1][0];
  2167. else
  2168. hLeft = NULL;
  2169. selectionChanged(true);
  2170. }
  2171. std::string CMarketplaceWindow::getBackgroundForMode(EMarketMode::EMarketMode mode)
  2172. {
  2173. switch(mode)
  2174. {
  2175. case EMarketMode::RESOURCE_RESOURCE:
  2176. return "TPMRKRES.bmp";
  2177. case EMarketMode::RESOURCE_PLAYER:
  2178. return "TPMRKPTS.bmp";
  2179. case EMarketMode::CREATURE_RESOURCE:
  2180. return "TPMRKCRS.bmp";
  2181. case EMarketMode::RESOURCE_ARTIFACT:
  2182. return "TPMRKABS.bmp";
  2183. case EMarketMode::ARTIFACT_RESOURCE:
  2184. return "TPMRKASS.bmp";
  2185. }
  2186. assert(0);
  2187. return "";
  2188. }
  2189. CMarketplaceWindow::CMarketplaceWindow(const IMarket *Market, const CGHeroInstance *Hero, EMarketMode::EMarketMode Mode)
  2190. : CTradeWindow(getBackgroundForMode(Mode), Market, Hero, Mode)
  2191. {
  2192. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2193. madeTransaction = false;
  2194. bool sliderNeeded = true;
  2195. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  2196. std::string title;
  2197. if (market->o->ID == Obj::TOWN)
  2198. {
  2199. switch (mode)
  2200. {
  2201. break; case EMarketMode::CREATURE_RESOURCE:
  2202. title = CGI->townh->factions[ETownType::STRONGHOLD]->town->buildings[BuildingID::FREELANCERS_GUILD]->Name();
  2203. break; case EMarketMode::RESOURCE_ARTIFACT:
  2204. title = CGI->townh->factions[market->o->subID]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->Name();
  2205. sliderNeeded = false;
  2206. break; case EMarketMode::ARTIFACT_RESOURCE:
  2207. title = CGI->townh->factions[market->o->subID]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->Name();
  2208. sliderNeeded = false;
  2209. break; default:
  2210. title = CGI->generaltexth->allTexts[158];
  2211. }
  2212. }
  2213. else
  2214. {
  2215. switch (market->o->ID)
  2216. {
  2217. break; case Obj::BLACK_MARKET: title = CGI->generaltexth->allTexts[349];
  2218. break; case Obj::TRADING_POST: title = CGI->generaltexth->allTexts[159];
  2219. break; case Obj::TRADING_POST_SNOW: title = CGI->generaltexth->allTexts[159];
  2220. break; default: title = market->o->getHoverText();
  2221. }
  2222. }
  2223. new CLabel(300, 27, FONT_BIG, CENTER, Colors::YELLOW, title);
  2224. initItems(false);
  2225. initItems(true);
  2226. ok = new CAdventureMapButton(CGI->generaltexth->zelp[600],boost::bind(&CGuiHandler::popIntTotally,&GH,this),516,520,"IOK6432.DEF",SDLK_RETURN);
  2227. ok->assignedKeys.insert(SDLK_ESCAPE);
  2228. deal = new CAdventureMapButton(CGI->generaltexth->zelp[595],boost::bind(&CMarketplaceWindow::makeDeal,this),307,520,"TPMRKB.DEF");
  2229. deal->block(true);
  2230. if(sliderNeeded)
  2231. {
  2232. slider = new CSlider(231,490,137,0,0,0);
  2233. slider->moved = boost::bind(&CMarketplaceWindow::sliderMoved,this,_1);
  2234. max = new CAdventureMapButton(CGI->generaltexth->zelp[596],boost::bind(&CMarketplaceWindow::setMax,this),229,520,"IRCBTNS.DEF");
  2235. max->block(true);
  2236. }
  2237. else
  2238. {
  2239. slider = NULL;
  2240. max = NULL;
  2241. deal->moveBy(Point(-30, 0));
  2242. }
  2243. Rect traderTextRect;
  2244. //left side
  2245. switch(Mode)
  2246. {
  2247. case EMarketMode::RESOURCE_RESOURCE:
  2248. case EMarketMode::RESOURCE_PLAYER:
  2249. case EMarketMode::RESOURCE_ARTIFACT:
  2250. new CLabel(154, 148, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[270]);
  2251. break;
  2252. case EMarketMode::CREATURE_RESOURCE:
  2253. //%s's Creatures
  2254. new CLabel(152, 102, FONT_SMALL, CENTER, Colors::WHITE,
  2255. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name));
  2256. break;
  2257. case EMarketMode::ARTIFACT_RESOURCE:
  2258. //%s's Artifacts
  2259. new CLabel(152, 102, FONT_SMALL, CENTER, Colors::WHITE,
  2260. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name));
  2261. break;
  2262. }
  2263. //right side
  2264. switch(Mode)
  2265. {
  2266. case EMarketMode::RESOURCE_RESOURCE:
  2267. case EMarketMode::CREATURE_RESOURCE:
  2268. case EMarketMode::RESOURCE_ARTIFACT:
  2269. case EMarketMode::ARTIFACT_RESOURCE:
  2270. new CLabel(445, 148, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[168]);
  2271. traderTextRect = Rect(316, 48, 260, 75);
  2272. break;
  2273. case EMarketMode::RESOURCE_PLAYER:
  2274. new CLabel(445, 55, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[169]);
  2275. traderTextRect = Rect(28, 48, 260, 75);
  2276. break;
  2277. }
  2278. traderText = new CTextBox("", traderTextRect, 0, FONT_SMALL, CENTER);
  2279. int specialOffset = mode == EMarketMode::ARTIFACT_RESOURCE ? 35 : 0; //in selling artifacts mode we need to move res-res and art-res buttons down
  2280. if(printButtonFor(EMarketMode::RESOURCE_PLAYER))
  2281. new CAdventureMapButton(CGI->generaltexth->zelp[612],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_PLAYER), 18, 520,"TPMRKBU1.DEF");
  2282. if(printButtonFor(EMarketMode::RESOURCE_RESOURCE))
  2283. new CAdventureMapButton(CGI->generaltexth->zelp[605],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_RESOURCE), 516, 450 + specialOffset,"TPMRKBU5.DEF");
  2284. if(printButtonFor(EMarketMode::CREATURE_RESOURCE))
  2285. new CAdventureMapButton(CGI->generaltexth->zelp[599],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::CREATURE_RESOURCE), 516, 485,"TPMRKBU4.DEF"); //was y=450, changed to not overlap res-res in some conditions
  2286. if(printButtonFor(EMarketMode::RESOURCE_ARTIFACT))
  2287. new CAdventureMapButton(CGI->generaltexth->zelp[598],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_ARTIFACT), 18, 450 + specialOffset,"TPMRKBU2.DEF");
  2288. if(printButtonFor(EMarketMode::ARTIFACT_RESOURCE))
  2289. new CAdventureMapButton(CGI->generaltexth->zelp[613],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::ARTIFACT_RESOURCE), 18, 485,"TPMRKBU3.DEF"); //was y=450, changed to not overlap res-art in some conditions
  2290. updateTraderText();
  2291. }
  2292. CMarketplaceWindow::~CMarketplaceWindow()
  2293. {
  2294. hLeft = hRight = NULL;
  2295. for(int i=0;i<items[1].size();i++)
  2296. delete items[1][i];
  2297. for(int i=0;i<items[0].size();i++)
  2298. delete items[0][i];
  2299. items[1].clear();
  2300. items[0].clear();
  2301. }
  2302. void CMarketplaceWindow::setMax()
  2303. {
  2304. slider->moveToMax();
  2305. }
  2306. void CMarketplaceWindow::makeDeal()
  2307. {
  2308. int sliderValue = 0;
  2309. if(slider)
  2310. sliderValue = slider->value;
  2311. else
  2312. sliderValue = !deal->isBlocked(); //should always be 1
  2313. if(!sliderValue)
  2314. return;
  2315. int leftIdToSend = -1;
  2316. switch (mode)
  2317. {
  2318. case EMarketMode::CREATURE_RESOURCE:
  2319. leftIdToSend = hLeft->serial;
  2320. break;
  2321. case EMarketMode::ARTIFACT_RESOURCE:
  2322. leftIdToSend = hLeft->getArtInstance()->id.getNum();
  2323. break;
  2324. default:
  2325. leftIdToSend = hLeft->id;
  2326. break;
  2327. }
  2328. if(slider)
  2329. {
  2330. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, slider->value*r1, hero);
  2331. slider->moveTo(0);
  2332. }
  2333. else
  2334. {
  2335. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, r2, hero);
  2336. }
  2337. madeTransaction = true;
  2338. hLeft = NULL;
  2339. hRight = NULL;
  2340. selectionChanged(true);
  2341. }
  2342. void CMarketplaceWindow::sliderMoved( int to )
  2343. {
  2344. redraw();
  2345. }
  2346. void CMarketplaceWindow::selectionChanged(bool side)
  2347. {
  2348. readyToTrade = hLeft && hRight;
  2349. if(mode == EMarketMode::RESOURCE_RESOURCE)
  2350. readyToTrade = readyToTrade && (hLeft->id != hRight->id); //for resource trade, two DIFFERENT resources must be selected
  2351. if(mode == EMarketMode::ARTIFACT_RESOURCE && !hLeft)
  2352. arts->unmarkSlots(false);
  2353. if(readyToTrade)
  2354. {
  2355. int soldItemId = hLeft->id;
  2356. market->getOffer(soldItemId, hRight->id, r1, r2, mode);
  2357. if(slider)
  2358. {
  2359. int newAmount = -1;
  2360. if(itemsType[1] == RESOURCE)
  2361. newAmount = LOCPLINT->cb->getResourceAmount(static_cast<Res::ERes>(soldItemId));
  2362. else if(itemsType[1] == CREATURE)
  2363. newAmount = hero->getStackCount(SlotID(hLeft->serial)) - (hero->Slots().size() == 1 && hero->needsLastStack());
  2364. else
  2365. assert(0);
  2366. slider->setAmount(newAmount / r1);
  2367. slider->moveTo(0);
  2368. max->block(false);
  2369. deal->block(false);
  2370. }
  2371. else if(itemsType[1] == RESOURCE) //buying -> check if we can afford transaction
  2372. {
  2373. deal->block(LOCPLINT->cb->getResourceAmount(static_cast<Res::ERes>(soldItemId)) < r1);
  2374. }
  2375. else
  2376. deal->block(false);
  2377. }
  2378. else
  2379. {
  2380. if(slider)
  2381. {
  2382. max->block(true);
  2383. slider->setAmount(0);
  2384. slider->moveTo(0);
  2385. }
  2386. deal->block(true);
  2387. }
  2388. if(side && itemsType[0] != PLAYER) //items[1] selection changed, recalculate offers
  2389. initSubs(false);
  2390. updateTraderText();
  2391. redraw();
  2392. }
  2393. bool CMarketplaceWindow::printButtonFor(EMarketMode::EMarketMode M) const
  2394. {
  2395. return market->allowsTrade(M) && M != mode && (hero || ( M != EMarketMode::CREATURE_RESOURCE && M != EMarketMode::RESOURCE_ARTIFACT && M != EMarketMode::ARTIFACT_RESOURCE ));
  2396. }
  2397. void CMarketplaceWindow::garrisonChanged()
  2398. {
  2399. if(mode != EMarketMode::CREATURE_RESOURCE)
  2400. return;
  2401. std::set<CTradeableItem *> toRemove;
  2402. getEmptySlots(toRemove);
  2403. removeItems(toRemove);
  2404. initSubs(true);
  2405. }
  2406. void CMarketplaceWindow::artifactsChanged(bool Left)
  2407. {
  2408. assert(!Left);
  2409. if(mode != EMarketMode::RESOURCE_ARTIFACT)
  2410. return;
  2411. std::vector<int> available = market->availableItemsIds(mode);
  2412. std::set<CTradeableItem *> toRemove;
  2413. BOOST_FOREACH(CTradeableItem *t, items[0])
  2414. if(!vstd::contains(available, t->id))
  2415. toRemove.insert(t);
  2416. removeItems(toRemove);
  2417. redraw();
  2418. }
  2419. std::string CMarketplaceWindow::selectionSubtitle(bool Left) const
  2420. {
  2421. if(Left)
  2422. {
  2423. switch(itemsType[1])
  2424. {
  2425. case RESOURCE:
  2426. case CREATURE:
  2427. {
  2428. int val = slider
  2429. ? slider->value * r1
  2430. : (((deal->isBlocked())) ? 0 : r1);
  2431. return boost::lexical_cast<std::string>(val);
  2432. }
  2433. case ARTIFACT_INSTANCE:
  2434. return ((deal->isBlocked()) ? "0" : "1");
  2435. }
  2436. }
  2437. else
  2438. {
  2439. switch(itemsType[0])
  2440. {
  2441. case RESOURCE:
  2442. if(slider)
  2443. return boost::lexical_cast<std::string>( slider->value * r2 );
  2444. else
  2445. return boost::lexical_cast<std::string>(r2);
  2446. case ARTIFACT_TYPE:
  2447. return ((deal->isBlocked()) ? "0" : "1");
  2448. case PLAYER:
  2449. return (hRight ? CGI->generaltexth->capColors[hRight->id] : "");
  2450. }
  2451. }
  2452. return "???";
  2453. }
  2454. Point CMarketplaceWindow::selectionOffset(bool Left) const
  2455. {
  2456. if(Left)
  2457. {
  2458. switch(itemsType[1])
  2459. {
  2460. case RESOURCE:
  2461. return Point(122, 446);
  2462. case CREATURE:
  2463. return Point(128, 450);
  2464. case ARTIFACT_INSTANCE:
  2465. return Point(134, 466);
  2466. }
  2467. }
  2468. else
  2469. {
  2470. switch(itemsType[0])
  2471. {
  2472. case RESOURCE:
  2473. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  2474. return Point(410, 469);
  2475. else
  2476. return Point(410, 446);
  2477. case ARTIFACT_TYPE:
  2478. return Point(425, 447);
  2479. case PLAYER:
  2480. return Point(417, 451);
  2481. }
  2482. }
  2483. assert(0);
  2484. return Point(0,0);
  2485. }
  2486. void CMarketplaceWindow::resourceChanged(int type, int val)
  2487. {
  2488. initSubs(true);
  2489. }
  2490. void CMarketplaceWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  2491. {
  2492. switch(type)
  2493. {
  2494. case RESOURCE:
  2495. dx = 82;
  2496. dy = 79;
  2497. x = 39;
  2498. y = 180;
  2499. h = 66;
  2500. w = 74;
  2501. break;
  2502. case PLAYER:
  2503. dx = 83;
  2504. dy = 118;
  2505. h = 64;
  2506. w = 58;
  2507. x = 44;
  2508. y = 83;
  2509. assert(Right);
  2510. break;
  2511. case CREATURE://45,123
  2512. x = 45;
  2513. y = 123;
  2514. w = 58;
  2515. h = 64;
  2516. dx = 83;
  2517. dy = 98;
  2518. assert(!Right);
  2519. break;
  2520. case ARTIFACT_TYPE://45,123
  2521. x = 340-289;
  2522. y = 180;
  2523. w = 44;
  2524. h = 44;
  2525. dx = 83;
  2526. dy = 79;
  2527. break;
  2528. }
  2529. leftToRightOffset = 289;
  2530. }
  2531. void CMarketplaceWindow::updateTraderText()
  2532. {
  2533. if(readyToTrade)
  2534. {
  2535. if(mode == EMarketMode::RESOURCE_PLAYER)
  2536. {
  2537. //I can give %s to the %s player.
  2538. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[165]) % hLeft->getName() % hRight->getName()));
  2539. }
  2540. else if(mode == EMarketMode::RESOURCE_ARTIFACT)
  2541. {
  2542. //I can offer you the %s for %d %s of %s.
  2543. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[267]) % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  2544. }
  2545. else if(mode == EMarketMode::RESOURCE_RESOURCE)
  2546. {
  2547. //I can offer you %d %s of %s for %d %s of %s.
  2548. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[157]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  2549. }
  2550. else if(mode == EMarketMode::CREATURE_RESOURCE)
  2551. {
  2552. //I can offer you %d %s of %s for %d %s.
  2553. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[269]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % hLeft->getName(r1)));
  2554. }
  2555. else if(mode == EMarketMode::ARTIFACT_RESOURCE)
  2556. {
  2557. //I can offer you %d %s of %s for your %s.
  2558. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[268]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % hLeft->getName(r1)));
  2559. }
  2560. return;
  2561. }
  2562. int gnrtxtnr = -1;
  2563. if(madeTransaction)
  2564. {
  2565. if(mode == EMarketMode::RESOURCE_PLAYER)
  2566. gnrtxtnr = 166; //Are there any other resources you'd like to give away?
  2567. else
  2568. gnrtxtnr = 162; //You have received quite a bargain. I expect to make no profit on the deal. Can I interest you in any of my other wares?
  2569. }
  2570. else
  2571. {
  2572. if(mode == EMarketMode::RESOURCE_PLAYER)
  2573. gnrtxtnr = 167; //If you'd like to give any of your resources to another player, click on the item you wish to give and to whom.
  2574. else
  2575. gnrtxtnr = 163; //Please inspect our fine wares. If you feel like offering a trade, click on the items you wish to trade with and for.
  2576. }
  2577. traderText->setTxt(CGI->generaltexth->allTexts[gnrtxtnr]);
  2578. }
  2579. CAltarWindow::CAltarWindow(const IMarket *Market, const CGHeroInstance *Hero /*= NULL*/, EMarketMode::EMarketMode Mode)
  2580. :CTradeWindow((Mode == EMarketMode::CREATURE_EXP ? "ALTARMON.bmp" : "ALTRART2.bmp"), Market, Hero, Mode)
  2581. {
  2582. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2583. if(Mode == EMarketMode::CREATURE_EXP)
  2584. {
  2585. //%s's Creatures
  2586. new CLabel(155, 30, FONT_SMALL, CENTER, Colors::YELLOW,
  2587. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name));
  2588. //Altar of Sacrifice
  2589. new CLabel(450, 30, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[479]);
  2590. //To sacrifice creatures, move them from your army on to the Altar and click Sacrifice
  2591. new CTextBox(CGI->generaltexth->allTexts[480], Rect(320, 56, 256, 40), 0, FONT_SMALL, CENTER, Colors::YELLOW);
  2592. slider = new CSlider(231,481,137,0,0,0);
  2593. slider->moved = boost::bind(&CAltarWindow::sliderMoved,this,_1);
  2594. max = new CAdventureMapButton(CGI->generaltexth->zelp[578],boost::bind(&CSlider::moveToMax, slider),147,520,"IRCBTNS.DEF");
  2595. sacrificedUnits.resize(GameConstants::ARMY_SIZE, 0);
  2596. sacrificeAll = new CAdventureMapButton(CGI->generaltexth->zelp[579],boost::bind(&CAltarWindow::SacrificeAll,this),393,520,"ALTARMY.DEF");
  2597. sacrificeBackpack = NULL;
  2598. initItems(true);
  2599. mimicCres();
  2600. artIcon = nullptr;
  2601. }
  2602. else
  2603. {
  2604. //Sacrifice artifacts for experience
  2605. new CLabel(450, 34, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[477]);
  2606. //%s's Creatures
  2607. new CLabel(302, 423, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[478]);
  2608. sacrificeAll = new CAdventureMapButton(CGI->generaltexth->zelp[571], boost::bind(&CAltarWindow::SacrificeAll,this),393,520,"ALTFILL.DEF");
  2609. sacrificeAll->block(hero->artifactsInBackpack.empty() && hero->artifactsWorn.empty());
  2610. sacrificeBackpack = new CAdventureMapButton(CGI->generaltexth->zelp[570],boost::bind(&CAltarWindow::SacrificeBackpack,this),147,520,"ALTEMBK.DEF");
  2611. sacrificeBackpack->block(hero->artifactsInBackpack.empty());
  2612. slider = NULL;
  2613. max = NULL;
  2614. initItems(true);
  2615. initItems(false);
  2616. artIcon = new CAnimImage("ARTIFACT", 0, 0, 281, 442);
  2617. artIcon->disable();
  2618. }
  2619. //Experience needed to reach next level
  2620. new CTextBox(CGI->generaltexth->allTexts[475], Rect(15, 415, 125, 50), 0, FONT_SMALL, CENTER, Colors::YELLOW);
  2621. //Total experience on the Altar
  2622. new CTextBox(CGI->generaltexth->allTexts[476], Rect(15, 495, 125, 40), 0, FONT_SMALL, CENTER, Colors::YELLOW);
  2623. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  2624. ok = new CAdventureMapButton(CGI->generaltexth->zelp[568],boost::bind(&CGuiHandler::popIntTotally,&GH,this),516,520,"IOK6432.DEF",SDLK_RETURN);
  2625. ok->assignedKeys.insert(SDLK_ESCAPE);
  2626. deal = new CAdventureMapButton(CGI->generaltexth->zelp[585],boost::bind(&CAltarWindow::makeDeal,this),269,520,"ALTSACR.DEF");
  2627. if(Hero->getAlignment() != ::EAlignment::EVIL && Mode == EMarketMode::CREATURE_EXP)
  2628. new CAdventureMapButton(CGI->generaltexth->zelp[580], boost::bind(&CTradeWindow::setMode,this, EMarketMode::ARTIFACT_EXP), 516, 421, "ALTART.DEF");
  2629. if(Hero->getAlignment() != ::EAlignment::GOOD && Mode == EMarketMode::ARTIFACT_EXP)
  2630. new CAdventureMapButton(CGI->generaltexth->zelp[572], boost::bind(&CTradeWindow::setMode,this, EMarketMode::CREATURE_EXP), 516, 421, "ALTSACC.DEF");
  2631. expPerUnit.resize(GameConstants::ARMY_SIZE, 0);
  2632. getExpValues();
  2633. expToLevel = new CLabel(73, 475, FONT_SMALL, CENTER);
  2634. expOnAltar = new CLabel(73, 543, FONT_SMALL, CENTER);
  2635. setExpToLevel();
  2636. calcTotalExp();
  2637. blockTrade();
  2638. }
  2639. CAltarWindow::~CAltarWindow()
  2640. {
  2641. }
  2642. void CAltarWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  2643. {
  2644. leftToRightOffset = 289;
  2645. x = 45;
  2646. y = 110;
  2647. w = 58;
  2648. h = 64;
  2649. dx = 83;
  2650. dy = 98;
  2651. }
  2652. void CAltarWindow::sliderMoved(int to)
  2653. {
  2654. sacrificedUnits[hLeft->serial] = to;
  2655. updateRight(hRight);
  2656. deal->block(!to);
  2657. calcTotalExp();
  2658. redraw();
  2659. }
  2660. void CAltarWindow::makeDeal()
  2661. {
  2662. if(mode == EMarketMode::CREATURE_EXP)
  2663. {
  2664. blockTrade();
  2665. slider->value = 0;
  2666. std::vector<int> toSacrifice = sacrificedUnits;
  2667. for (int i = 0; i < toSacrifice.size(); i++)
  2668. {
  2669. if(toSacrifice[i])
  2670. LOCPLINT->cb->trade(market->o, mode, i, 0, toSacrifice[i], hero);
  2671. }
  2672. BOOST_FOREACH(int& val, sacrificedUnits)
  2673. val = 0;
  2674. BOOST_FOREACH(CTradeableItem *t, items[0])
  2675. {
  2676. t->setType(CREATURE_PLACEHOLDER);
  2677. t->subtitle = "";
  2678. }
  2679. }
  2680. else
  2681. {
  2682. BOOST_FOREACH(const CArtifactInstance *art, arts->artifactsOnAltar) //sacrifice each artifact on the list
  2683. {
  2684. LOCPLINT->cb->trade(market->o, mode, hero->getArtPos(art), -1, 1, hero);
  2685. }
  2686. arts->artifactsOnAltar.clear();
  2687. BOOST_FOREACH(CTradeableItem *t, items[0])
  2688. {
  2689. t->setID(-1);
  2690. t->subtitle = "";
  2691. }
  2692. arts->commonInfo->reset();
  2693. //arts->scrollBackpack(0);
  2694. deal->block(true);
  2695. }
  2696. calcTotalExp();
  2697. }
  2698. void CAltarWindow::SacrificeAll()
  2699. {
  2700. if(mode == EMarketMode::CREATURE_EXP)
  2701. {
  2702. bool movedAnything = false;
  2703. BOOST_FOREACH(CTradeableItem *t, items[1])
  2704. sacrificedUnits[t->serial] = hero->getStackCount(SlotID(t->serial));
  2705. sacrificedUnits[items[1].front()->serial]--;
  2706. BOOST_FOREACH(CTradeableItem *t, items[0])
  2707. {
  2708. updateRight(t);
  2709. if(t->type == CREATURE)
  2710. movedAnything = true;
  2711. }
  2712. deal->block(!movedAnything);
  2713. calcTotalExp();
  2714. }
  2715. else
  2716. {
  2717. for(auto i = hero->artifactsWorn.cbegin(); i != hero->artifactsWorn.cend(); i++)
  2718. {
  2719. if(i->second.artifact->artType->id != ArtifactID::ART_LOCK) //ignore locks from assembled artifacts
  2720. moveFromSlotToAltar(i->first, NULL, i->second.artifact);
  2721. }
  2722. SacrificeBackpack();
  2723. }
  2724. redraw();
  2725. }
  2726. void CAltarWindow::selectionChanged(bool side)
  2727. {
  2728. if(mode != EMarketMode::CREATURE_EXP)
  2729. return;
  2730. CTradeableItem *&selected = side ? hLeft : hRight;
  2731. CTradeableItem *&theOther = side ? hRight : hLeft;
  2732. theOther = *std::find_if(items[!side].begin(), items[!side].end(), boost::bind(&CTradeableItem::serial, _1) == selected->serial);
  2733. int stackCount = 0;
  2734. for (int i = 0; i < GameConstants::ARMY_SIZE; i++)
  2735. if(hero->getStackCount(SlotID(i)) > sacrificedUnits[i])
  2736. stackCount++;
  2737. slider->setAmount(hero->getStackCount(SlotID(hLeft->serial)) - (stackCount == 1));
  2738. slider->block(!slider->amount);
  2739. slider->value = sacrificedUnits[hLeft->serial];
  2740. max->block(!slider->amount);
  2741. readyToTrade = true;
  2742. redraw();
  2743. }
  2744. void CAltarWindow::mimicCres()
  2745. {
  2746. std::vector<Rect> positions;
  2747. getPositionsFor(positions, false, CREATURE);
  2748. BOOST_FOREACH(CTradeableItem *t, items[1])
  2749. {
  2750. CTradeableItem *hlp = new CTradeableItem(positions[t->serial].topLeft(), CREATURE_PLACEHOLDER, t->id, false, t->serial);
  2751. hlp->pos = positions[t->serial] + this->pos.topLeft();
  2752. items[0].push_back(hlp);
  2753. }
  2754. }
  2755. Point CAltarWindow::selectionOffset(bool Left) const
  2756. {
  2757. if(Left)
  2758. return Point(150, 421);
  2759. else
  2760. return Point(396, 421);
  2761. }
  2762. std::string CAltarWindow::selectionSubtitle(bool Left) const
  2763. {
  2764. if(Left && slider && hLeft)
  2765. return boost::lexical_cast<std::string>(slider->value);
  2766. else if(!Left && hRight)
  2767. return hRight->subtitle;
  2768. else
  2769. return "";
  2770. }
  2771. void CAltarWindow::artifactsChanged(bool left)
  2772. {
  2773. }
  2774. void CAltarWindow::garrisonChanged()
  2775. {
  2776. if(mode != EMarketMode::CREATURE_EXP)
  2777. return;
  2778. std::set<CTradeableItem *> empty;
  2779. getEmptySlots(empty);
  2780. BOOST_FOREACH(CTradeableItem *t, empty)
  2781. {
  2782. removeItem(*std::find_if(items[0].begin(), items[0].end(), boost::bind(&CTradeableItem::serial, _1) == t->serial));
  2783. removeItem(t);
  2784. }
  2785. initSubs(true);
  2786. getExpValues();
  2787. }
  2788. void CAltarWindow::getExpValues()
  2789. {
  2790. int dump;
  2791. BOOST_FOREACH(CTradeableItem *t, items[1])
  2792. if(t->id >= 0)
  2793. market->getOffer(t->id, 0, dump, expPerUnit[t->serial], EMarketMode::CREATURE_EXP);
  2794. }
  2795. void CAltarWindow::calcTotalExp()
  2796. {
  2797. int val = 0;
  2798. if(mode == EMarketMode::CREATURE_EXP)
  2799. {
  2800. for (int i = 0; i < sacrificedUnits.size(); i++)
  2801. {
  2802. val += expPerUnit[i] * sacrificedUnits[i];
  2803. }
  2804. }
  2805. else
  2806. {
  2807. BOOST_FOREACH(const CArtifactInstance *art, arts->artifactsOnAltar)
  2808. {
  2809. int dmp, valOfArt;
  2810. market->getOffer(art->artType->id, 0, dmp, valOfArt, mode);
  2811. val += valOfArt; //WAS val += valOfArt * arts->artifactsOnAltar.count(*i);
  2812. }
  2813. }
  2814. val = hero->calculateXp(val);
  2815. expOnAltar->setTxt(boost::lexical_cast<std::string>(val));
  2816. }
  2817. void CAltarWindow::setExpToLevel()
  2818. {
  2819. expToLevel->setTxt(boost::lexical_cast<std::string>(CGI->heroh->reqExp(CGI->heroh->level(hero->exp)+1) - hero->exp));
  2820. }
  2821. void CAltarWindow::blockTrade()
  2822. {
  2823. hLeft = hRight = NULL;
  2824. readyToTrade = false;
  2825. if(slider)
  2826. {
  2827. slider->block(true);
  2828. max->block(true);
  2829. }
  2830. deal->block(true);
  2831. }
  2832. void CAltarWindow::updateRight(CTradeableItem *toUpdate)
  2833. {
  2834. int val = sacrificedUnits[toUpdate->serial];
  2835. toUpdate->setType(val ? CREATURE : CREATURE_PLACEHOLDER);
  2836. toUpdate->subtitle = val ? boost::str(boost::format(CGI->generaltexth->allTexts[122]) % boost::lexical_cast<std::string>(val * expPerUnit[toUpdate->serial])) : ""; //%s exp
  2837. }
  2838. int CAltarWindow::firstFreeSlot()
  2839. {
  2840. int ret = -1;
  2841. while(items[0][++ret]->id >= 0 && ret + 1 < items[0].size());
  2842. return ret < items[0].size() ? ret : -1;
  2843. }
  2844. void CAltarWindow::SacrificeBackpack()
  2845. {
  2846. std::multiset<const CArtifactInstance *> toOmmit = arts->artifactsOnAltar;
  2847. for (int i = 0; i < hero->artifactsInBackpack.size(); i++)
  2848. {
  2849. if(vstd::contains(toOmmit, hero->artifactsInBackpack[i].artifact))
  2850. {
  2851. toOmmit -= hero->artifactsInBackpack[i].artifact;
  2852. continue;
  2853. }
  2854. putOnAltar(NULL, hero->artifactsInBackpack[i].artifact);
  2855. }
  2856. arts->scrollBackpack(0);
  2857. calcTotalExp();
  2858. }
  2859. void CAltarWindow::artifactPicked()
  2860. {
  2861. redraw();
  2862. }
  2863. void CAltarWindow::showAll(SDL_Surface * to)
  2864. {
  2865. CTradeWindow::showAll(to);
  2866. if(mode == EMarketMode::ARTIFACT_EXP && arts && arts->commonInfo->src.art)
  2867. {
  2868. artIcon->setFrame(arts->commonInfo->src.art->artType->iconIndex);
  2869. artIcon->showAll(to);
  2870. int dmp, val;
  2871. market->getOffer(arts->commonInfo->src.art->artType->id, 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  2872. printAtMiddleLoc(boost::lexical_cast<std::string>(val), 304, 498, FONT_SMALL, Colors::WHITE, to);
  2873. }
  2874. }
  2875. bool CAltarWindow::putOnAltar(CTradeableItem* altarSlot, const CArtifactInstance *art)
  2876. {
  2877. int artID = art->artType->id;
  2878. if(artID != 1 && artID < 7) //special art
  2879. {
  2880. logGlobal->warnStream() << "Cannot put special artifact on altar!";
  2881. return false;
  2882. }
  2883. if(!altarSlot)
  2884. {
  2885. int slotIndex = firstFreeSlot();
  2886. if(slotIndex < 0)
  2887. {
  2888. logGlobal->warnStream() << "No free slots on altar!";
  2889. return false;
  2890. }
  2891. altarSlot = items[0][slotIndex];
  2892. }
  2893. int dmp, val;
  2894. market->getOffer(artID, 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  2895. arts->artifactsOnAltar.insert(art);
  2896. altarSlot->setArtInstance(art);
  2897. altarSlot->subtitle = boost::lexical_cast<std::string>(val);
  2898. deal->block(false);
  2899. return true;
  2900. }
  2901. void CAltarWindow::moveFromSlotToAltar(ArtifactPosition slotID, CTradeableItem* altarSlot, const CArtifactInstance *art)
  2902. {
  2903. auto freeBackpackSlot = ArtifactPosition(hero->artifactsInBackpack.size() + GameConstants::BACKPACK_START);
  2904. if(arts->commonInfo->src.art)
  2905. {
  2906. arts->commonInfo->dst.slotID = freeBackpackSlot;
  2907. arts->commonInfo->dst.AOH = arts;
  2908. }
  2909. if(putOnAltar(altarSlot, art))
  2910. {
  2911. if(slotID < GameConstants::BACKPACK_START)
  2912. LOCPLINT->cb->swapArtifacts(ArtifactLocation(hero, slotID), ArtifactLocation(hero, freeBackpackSlot));
  2913. else
  2914. {
  2915. arts->commonInfo->src.clear();
  2916. arts->commonInfo->dst.clear();
  2917. CCS->curh->dragAndDropCursor(NULL);
  2918. arts->unmarkSlots(false);
  2919. }
  2920. }
  2921. }
  2922. void CSystemOptionsWindow::setMusicVolume( int newVolume )
  2923. {
  2924. Settings volume = settings.write["general"]["music"];
  2925. volume->Float() = newVolume;
  2926. }
  2927. void CSystemOptionsWindow::setSoundVolume( int newVolume )
  2928. {
  2929. Settings volume = settings.write["general"]["sound"];
  2930. volume->Float() = newVolume;
  2931. }
  2932. void CSystemOptionsWindow::setHeroMoveSpeed( int newSpeed )
  2933. {
  2934. Settings speed = settings.write["adventure"]["heroSpeed"];
  2935. speed->Float() = newSpeed;
  2936. }
  2937. void CSystemOptionsWindow::setMapScrollingSpeed( int newSpeed )
  2938. {
  2939. Settings speed = settings.write["adventure"]["scrollSpeed"];
  2940. speed->Float() = newSpeed;
  2941. }
  2942. CSystemOptionsWindow::CSystemOptionsWindow():
  2943. CWindowObject(PLAYER_COLORED, "SysOpBck"),
  2944. onFullscreenChanged(settings.listen["video"]["fullscreen"])
  2945. {
  2946. //TODO: translation and\or config file
  2947. static const std::string fsLabel = "Fullscreen";
  2948. static const std::string fsHelp = "{Fullscreen}\n\n If selected, VCMI will run in fullscreen mode, othervice VCMI will run in window";
  2949. static const std::string cwLabel = "Classic creature window";
  2950. static const std::string cwHelp = "{Classic creature window}\n\n Enable original Heroes 3 creature window instead of new window from VCMI";
  2951. static const std::string rsLabel = "Resolution";
  2952. static const std::string rsHelp = "{Select resolution}\n\n Change in-game screen resolution. Will only affect adventure map. Game restart required to apply new resolution.";
  2953. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2954. title = new CLabel(242, 32, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[568]);
  2955. //left window section
  2956. leftGroup = new CLabelGroup(FONT_MEDIUM, CENTER, Colors::YELLOW);
  2957. leftGroup->add(122, 64, CGI->generaltexth->allTexts[569]);
  2958. leftGroup->add(122, 130, CGI->generaltexth->allTexts[570]);
  2959. leftGroup->add(122, 196, CGI->generaltexth->allTexts[571]);
  2960. leftGroup->add(122, 262, rsLabel); //CGI->generaltexth->allTexts[20]
  2961. leftGroup->add(122, 347, CGI->generaltexth->allTexts[394]);
  2962. leftGroup->add(122, 412, CGI->generaltexth->allTexts[395]);
  2963. //right section
  2964. rightGroup = new CLabelGroup(FONT_MEDIUM, TOPLEFT, Colors::WHITE);
  2965. rightGroup->add(282, 57, CGI->generaltexth->allTexts[572]);
  2966. rightGroup->add(282, 89, CGI->generaltexth->allTexts[573]);
  2967. rightGroup->add(282, 121, CGI->generaltexth->allTexts[574]);
  2968. rightGroup->add(282, 153, CGI->generaltexth->allTexts[575]);
  2969. rightGroup->add(282, 185, cwLabel); //CGI->generaltexth->allTexts[576]
  2970. rightGroup->add(282, 217, fsLabel); //CGI->generaltexth->allTexts[577]
  2971. //setting up buttons
  2972. load = new CAdventureMapButton (CGI->generaltexth->zelp[321].first, CGI->generaltexth->zelp[321].second,
  2973. boost::bind(&CSystemOptionsWindow::bloadf, this), 246, 298, "SOLOAD.DEF", SDLK_l);
  2974. load->swappedImages = true;
  2975. load->update();
  2976. save = new CAdventureMapButton (CGI->generaltexth->zelp[322].first, CGI->generaltexth->zelp[322].second,
  2977. boost::bind(&CSystemOptionsWindow::bsavef, this), 357, 298, "SOSAVE.DEF", SDLK_s);
  2978. save->swappedImages = true;
  2979. save->update();
  2980. restart = new CAdventureMapButton (CGI->generaltexth->zelp[323].first, CGI->generaltexth->zelp[323].second,
  2981. boost::bind(&CSystemOptionsWindow::brestartf, this), 246, 357, "SORSTRT", SDLK_r);
  2982. restart->swappedImages = true;
  2983. restart->update();
  2984. mainMenu = new CAdventureMapButton (CGI->generaltexth->zelp[320].first, CGI->generaltexth->zelp[320].second,
  2985. boost::bind(&CSystemOptionsWindow::bmainmenuf, this), 357, 357, "SOMAIN.DEF", SDLK_m);
  2986. mainMenu->swappedImages = true;
  2987. mainMenu->update();
  2988. quitGame = new CAdventureMapButton (CGI->generaltexth->zelp[324].first, CGI->generaltexth->zelp[324].second,
  2989. boost::bind(&CSystemOptionsWindow::bquitf, this), 246, 415, "soquit.def", SDLK_q);
  2990. quitGame->swappedImages = true;
  2991. quitGame->update();
  2992. backToMap = new CAdventureMapButton (CGI->generaltexth->zelp[325].first, CGI->generaltexth->zelp[325].second,
  2993. boost::bind(&CSystemOptionsWindow::breturnf, this), 357, 415, "soretrn.def", SDLK_RETURN);
  2994. backToMap->swappedImages = true;
  2995. backToMap->update();
  2996. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  2997. heroMoveSpeed = new CHighlightableButtonsGroup(0);
  2998. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[349].second),CGI->generaltexth->zelp[349].second, "sysopb1.def", 28, 77, 1);
  2999. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[350].second),CGI->generaltexth->zelp[350].second, "sysopb2.def", 76, 77, 2);
  3000. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[351].second),CGI->generaltexth->zelp[351].second, "sysopb3.def", 124, 77, 4);
  3001. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[352].second),CGI->generaltexth->zelp[352].second, "sysopb4.def", 172, 77, 8);
  3002. heroMoveSpeed->select(settings["adventure"]["heroSpeed"].Float(), 1);
  3003. heroMoveSpeed->onChange = boost::bind(&CSystemOptionsWindow::setHeroMoveSpeed, this, _1);
  3004. mapScrollSpeed = new CHighlightableButtonsGroup(0);
  3005. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[357].second),CGI->generaltexth->zelp[357].second, "sysopb9.def", 28, 210, 1);
  3006. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[358].second),CGI->generaltexth->zelp[358].second, "sysob10.def", 92, 210, 2);
  3007. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[359].second),CGI->generaltexth->zelp[359].second, "sysob11.def", 156, 210, 4);
  3008. mapScrollSpeed->select(settings["adventure"]["scrollSpeed"].Float(), 1);
  3009. mapScrollSpeed->onChange = boost::bind(&CSystemOptionsWindow::setMapScrollingSpeed, this, _1);
  3010. musicVolume = new CHighlightableButtonsGroup(0, true);
  3011. for(int i=0; i<10; ++i)
  3012. musicVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[326+i].second),CGI->generaltexth->zelp[326+i].second, "syslb.def", 29 + 19*i, 359, i*11);
  3013. musicVolume->select(CCS->musich->getVolume(), 1);
  3014. musicVolume->onChange = boost::bind(&CSystemOptionsWindow::setMusicVolume, this, _1);
  3015. effectsVolume = new CHighlightableButtonsGroup(0, true);
  3016. for(int i=0; i<10; ++i)
  3017. effectsVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[336+i].second),CGI->generaltexth->zelp[336+i].second, "syslb.def", 29 + 19*i, 425, i*11);
  3018. effectsVolume->select(CCS->soundh->getVolume(), 1);
  3019. effectsVolume->onChange = boost::bind(&CSystemOptionsWindow::setSoundVolume, this, _1);
  3020. showReminder = new CHighlightableButton(
  3021. boost::bind(&CSystemOptionsWindow::toggleReminder, this, true), boost::bind(&CSystemOptionsWindow::toggleReminder, this, false),
  3022. std::map<int,std::string>(), CGI->generaltexth->zelp[361].second, false, "sysopchk.def", NULL, 246, 87, false);
  3023. quickCombat = new CHighlightableButton(
  3024. boost::bind(&CSystemOptionsWindow::toggleQuickCombat, this, true), boost::bind(&CSystemOptionsWindow::toggleQuickCombat, this, false),
  3025. std::map<int,std::string>(), CGI->generaltexth->zelp[362].second, false, "sysopchk.def", NULL, 246, 87+32, false);
  3026. newCreatureWin = new CHighlightableButton(
  3027. boost::bind(&CSystemOptionsWindow::toggleCreatureWin, this, true), boost::bind(&CSystemOptionsWindow::toggleCreatureWin, this, false),
  3028. std::map<int,std::string>(), cwHelp, false, "sysopchk.def", NULL, 246, 183, false);
  3029. fullscreen = new CHighlightableButton(
  3030. boost::bind(&CSystemOptionsWindow::toggleFullscreen, this, true), boost::bind(&CSystemOptionsWindow::toggleFullscreen, this, false),
  3031. std::map<int,std::string>(), fsHelp, false, "sysopchk.def", NULL, 246, 215, false);
  3032. showReminder->select(settings["adventure"]["heroReminder"].Bool());
  3033. quickCombat->select(settings["adventure"]["quickCombat"].Bool());
  3034. newCreatureWin->select(settings["general"]["classicCreatureWindow"].Bool());
  3035. fullscreen->select(settings["video"]["fullscreen"].Bool());
  3036. onFullscreenChanged([&](const JsonNode &newState){ fullscreen->select(newState.Bool());});
  3037. gameResButton = new CAdventureMapButton("", rsHelp, boost::bind(&CSystemOptionsWindow::selectGameRes, this), 28, 275,"SYSOB12", SDLK_g);
  3038. std::string resText;
  3039. resText += boost::lexical_cast<std::string>(settings["video"]["screenRes"]["width"].Float());
  3040. resText += "x";
  3041. resText += boost::lexical_cast<std::string>(settings["video"]["screenRes"]["height"].Float());
  3042. gameResLabel = new CLabel(170, 292, FONT_MEDIUM, CENTER, Colors::YELLOW, resText);
  3043. }
  3044. void CSystemOptionsWindow::selectGameRes()
  3045. {
  3046. //TODO: translation and\or config file
  3047. static const std::string rsLabel = "Select resolution";
  3048. static const std::string rsHelp = "Change in-game screen resolution.";
  3049. std::vector<std::string> items;
  3050. BOOST_FOREACH( config::CConfigHandler::GuiOptionsMap::value_type& value, conf.guiOptions)
  3051. {
  3052. std::string resX = boost::lexical_cast<std::string>(value.first.first);
  3053. std::string resY = boost::lexical_cast<std::string>(value.first.second);
  3054. items.push_back(resX + 'x' + resY);
  3055. }
  3056. GH.pushInt(new CObjectListWindow(items, NULL, rsLabel, rsHelp,
  3057. boost::bind(&CSystemOptionsWindow::setGameRes, this, _1)));
  3058. }
  3059. void CSystemOptionsWindow::setGameRes(int index)
  3060. {
  3061. auto iter = conf.guiOptions.begin();
  3062. std::advance(iter, index);
  3063. //do not set resolution to illegal one (0x0)
  3064. assert(iter!=conf.guiOptions.end() && iter->first.first > 0 && iter->first.second > 0);
  3065. Settings gameRes = settings.write["video"]["screenRes"];
  3066. gameRes["width"].Float() = iter->first.first;
  3067. gameRes["height"].Float() = iter->first.second;
  3068. std::string resText;
  3069. resText += boost::lexical_cast<std::string>(iter->first.first);
  3070. resText += "x";
  3071. resText += boost::lexical_cast<std::string>(iter->first.second);
  3072. gameResLabel->setTxt(resText);
  3073. }
  3074. void CSystemOptionsWindow::toggleReminder(bool on)
  3075. {
  3076. Settings heroReminder = settings.write["adventure"]["heroReminder"];
  3077. heroReminder->Bool() = on;
  3078. }
  3079. void CSystemOptionsWindow::toggleQuickCombat(bool on)
  3080. {
  3081. Settings quickCombat = settings.write["adventure"]["quickCombat"];
  3082. quickCombat->Bool() = on;
  3083. }
  3084. void CSystemOptionsWindow::toggleCreatureWin(bool on)
  3085. {
  3086. Settings classicCreatureWindow = settings.write["general"]["classicCreatureWindow"];
  3087. classicCreatureWindow->Bool() = on;
  3088. }
  3089. void CSystemOptionsWindow::toggleFullscreen(bool on)
  3090. {
  3091. Settings fullscreen = settings.write["video"]["fullscreen"];
  3092. fullscreen->Bool() = on;
  3093. }
  3094. void CSystemOptionsWindow::bquitf()
  3095. {
  3096. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this]{ closeAndPushEvent(SDL_QUIT); }, 0);
  3097. }
  3098. void CSystemOptionsWindow::breturnf()
  3099. {
  3100. GH.popIntTotally(this);
  3101. }
  3102. void CSystemOptionsWindow::bmainmenuf()
  3103. {
  3104. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this]{ closeAndPushEvent(SDL_USEREVENT, RETURN_TO_MAIN_MENU); }, 0);
  3105. }
  3106. void CSystemOptionsWindow::bloadf()
  3107. {
  3108. GH.popIntTotally(this);
  3109. LOCPLINT->proposeLoadingGame();
  3110. }
  3111. void CSystemOptionsWindow::bsavef()
  3112. {
  3113. GH.popIntTotally(this);
  3114. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  3115. }
  3116. void CSystemOptionsWindow::brestartf()
  3117. {
  3118. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], [this]{ closeAndPushEvent(SDL_USEREVENT, RESTART_GAME); }, 0);
  3119. }
  3120. void CSystemOptionsWindow::closeAndPushEvent(int eventType, int code /*= 0*/)
  3121. {
  3122. GH.popIntTotally(this);
  3123. GH.pushSDLEvent(eventType, code);
  3124. }
  3125. CTavernWindow::CTavernWindow(const CGObjectInstance *TavernObj):
  3126. CWindowObject(PLAYER_COLORED, "TPTAVERN"),
  3127. tavernObj(TavernObj)
  3128. {
  3129. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3130. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(TavernObj);
  3131. assert(h.size() == 2);
  3132. h1 = new HeroPortrait(selected,0,72,299,h[0]);
  3133. h2 = new HeroPortrait(selected,1,162,299,h[1]);
  3134. selected = 0;
  3135. if (!h[0])
  3136. selected = 1;
  3137. if (!h[0] && !h[1])
  3138. selected = -1;
  3139. oldSelected = -1;
  3140. new CLabel(200, 35, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[37]);
  3141. new CLabel(320, 328, FONT_SMALL, CENTER, Colors::WHITE, "2500");
  3142. new CTextBox(LOCPLINT->cb->getTavernGossip(tavernObj), Rect(32, 190, 330, 68), 0, FONT_SMALL, CENTER, Colors::WHITE);
  3143. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  3144. cancel = new CAdventureMapButton(CGI->generaltexth->tavernInfo[7],"", boost::bind(&CTavernWindow::close, this), 310, 428, "ICANCEL.DEF", SDLK_ESCAPE);
  3145. recruit = new CAdventureMapButton("", "", boost::bind(&CTavernWindow::recruitb, this), 272, 355, "TPTAV01.DEF", SDLK_RETURN);
  3146. thiefGuild = new CAdventureMapButton(CGI->generaltexth->tavernInfo[5],"", boost::bind(&CTavernWindow::thievesguildb, this), 22, 428, "TPTAV02.DEF", SDLK_t);
  3147. if(LOCPLINT->cb->getResourceAmount(Res::GOLD) < 2500) //not enough gold
  3148. {
  3149. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[0]; //Cannot afford a Hero
  3150. recruit->block(true);
  3151. }
  3152. else if(LOCPLINT->cb->howManyHeroes(false) >= 8)
  3153. {
  3154. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  3155. boost::algorithm::replace_first(recruit->hoverTexts[0],"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes()));
  3156. recruit->block(true);
  3157. }
  3158. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  3159. {
  3160. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[2]; //Cannot recruit. You already have a Hero in this town.
  3161. recruit->block(true);
  3162. }
  3163. else
  3164. {
  3165. if(selected == -1)
  3166. recruit->block(true);
  3167. }
  3168. CCS->videoh->open("TAVERN.BIK");
  3169. }
  3170. void CTavernWindow::recruitb()
  3171. {
  3172. const CGHeroInstance *toBuy = (selected ? h2 : h1)->h;
  3173. const CGObjectInstance *obj = tavernObj;
  3174. close();
  3175. LOCPLINT->cb->recruitHero(obj, toBuy);
  3176. }
  3177. void CTavernWindow::thievesguildb()
  3178. {
  3179. GH.pushInt( new CThievesGuildWindow(tavernObj) );
  3180. }
  3181. CTavernWindow::~CTavernWindow()
  3182. {
  3183. CCS->videoh->close();
  3184. }
  3185. void CTavernWindow::show(SDL_Surface * to)
  3186. {
  3187. CWindowObject::show(to);
  3188. CCS->videoh->update(pos.x+70, pos.y+56, to, true, false);
  3189. if(selected >= 0)
  3190. {
  3191. HeroPortrait *sel = selected ? h2 : h1;
  3192. if (selected != oldSelected && !recruit->isBlocked())
  3193. {
  3194. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  3195. oldSelected = selected;
  3196. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[3]; //Recruit %s the %s
  3197. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->name);
  3198. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->type->heroClass->name);
  3199. }
  3200. printAtMiddleWBLoc(sel->descr, 146, 395, FONT_SMALL, 200, Colors::WHITE, to);
  3201. CSDL_Ext::drawBorder(to,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  3202. }
  3203. }
  3204. void CTavernWindow::HeroPortrait::clickLeft(tribool down, bool previousState)
  3205. {
  3206. if(previousState && !down && h)
  3207. *_sel = _id;
  3208. }
  3209. void CTavernWindow::HeroPortrait::clickRight(tribool down, bool previousState)
  3210. {
  3211. if(down && h)
  3212. {
  3213. GH.pushInt(new CRClickPopupInt(new CHeroWindow(h), true));
  3214. }
  3215. }
  3216. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  3217. : h(H), _sel(&sel), _id(id)
  3218. {
  3219. addUsedEvents(LCLICK | RCLICK | HOVER);
  3220. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3221. h = H;
  3222. pos.x += x;
  3223. pos.y += y;
  3224. pos.w = 58;
  3225. pos.h = 64;
  3226. if(H)
  3227. {
  3228. hoverName = CGI->generaltexth->tavernInfo[4];
  3229. boost::algorithm::replace_first(hoverName,"%s",H->name);
  3230. int artifs = h->artifactsWorn.size() + h->artifactsInBackpack.size();
  3231. for(int i=13; i<=17; i++) //war machines and spellbook don't count
  3232. if(vstd::contains(h->artifactsWorn, ArtifactPosition(i)))
  3233. artifs--;
  3234. sprintf_s(descr, sizeof(descr),CGI->generaltexth->allTexts[215].c_str(),
  3235. h->name.c_str(), h->level, h->type->heroClass->name.c_str(), artifs);
  3236. descr[sizeof(descr)-1] = '\0';
  3237. new CAnimImage("portraitsLarge", h->portrait);
  3238. }
  3239. }
  3240. void CTavernWindow::HeroPortrait::hover( bool on )
  3241. {
  3242. //Hoverable::hover(on);
  3243. if(on)
  3244. GH.statusbar->print(hoverName);
  3245. else
  3246. GH.statusbar->clear();
  3247. }
  3248. void CInGameConsole::show(SDL_Surface * to)
  3249. {
  3250. int number = 0;
  3251. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  3252. boost::unique_lock<boost::mutex> lock(texts_mx);
  3253. for(std::list< std::pair< std::string, int > >::iterator it = texts.begin(); it != texts.end(); ++it, ++number)
  3254. {
  3255. Point leftBottomCorner(0, screen->h);
  3256. if(LOCPLINT->battleInt)
  3257. {
  3258. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  3259. }
  3260. graphics->fonts[FONT_MEDIUM]->renderTextLeft(to, it->first, Colors::GREEN,
  3261. Point(leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20));
  3262. if(SDL_GetTicks() - it->second > defaultTimeout)
  3263. {
  3264. toDel.push_back(it);
  3265. }
  3266. }
  3267. for(int it=0; it<toDel.size(); ++it)
  3268. {
  3269. texts.erase(toDel[it]);
  3270. }
  3271. }
  3272. void CInGameConsole::print(const std::string &txt)
  3273. {
  3274. boost::unique_lock<boost::mutex> lock(texts_mx);
  3275. int lineLen = conf.go()->ac.outputLineLength;
  3276. if(txt.size() < lineLen)
  3277. {
  3278. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  3279. if(texts.size() > maxDisplayedTexts)
  3280. {
  3281. texts.pop_front();
  3282. }
  3283. }
  3284. else
  3285. {
  3286. assert(lineLen);
  3287. for(int g=0; g<txt.size() / lineLen + 1; ++g)
  3288. {
  3289. std::string part = txt.substr(g * lineLen, lineLen);
  3290. if(part.size() == 0)
  3291. break;
  3292. texts.push_back(std::make_pair(part, SDL_GetTicks()));
  3293. if(texts.size() > maxDisplayedTexts)
  3294. {
  3295. texts.pop_front();
  3296. }
  3297. }
  3298. }
  3299. }
  3300. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  3301. {
  3302. if(key.type != SDL_KEYDOWN) return;
  3303. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  3304. switch(key.keysym.sym)
  3305. {
  3306. case SDLK_TAB:
  3307. case SDLK_ESCAPE:
  3308. {
  3309. if(captureAllKeys)
  3310. {
  3311. captureAllKeys = false;
  3312. endEnteringText(false);
  3313. }
  3314. else if(SDLK_TAB)
  3315. {
  3316. captureAllKeys = true;
  3317. startEnteringText();
  3318. }
  3319. break;
  3320. }
  3321. case SDLK_RETURN: //enter key
  3322. {
  3323. if(enteredText.size() > 0 && captureAllKeys)
  3324. {
  3325. captureAllKeys = false;
  3326. endEnteringText(true);
  3327. }
  3328. break;
  3329. }
  3330. case SDLK_BACKSPACE:
  3331. {
  3332. if(enteredText.size() > 1)
  3333. {
  3334. enteredText.resize(enteredText.size()-1);
  3335. enteredText[enteredText.size()-1] = '_';
  3336. refreshEnteredText();
  3337. }
  3338. break;
  3339. }
  3340. case SDLK_UP: //up arrow
  3341. {
  3342. if(previouslyEntered.size() == 0)
  3343. break;
  3344. if(prevEntDisp == -1)
  3345. {
  3346. prevEntDisp = previouslyEntered.size() - 1;
  3347. enteredText = previouslyEntered[prevEntDisp] + "_";
  3348. refreshEnteredText();
  3349. }
  3350. else if( prevEntDisp > 0)
  3351. {
  3352. --prevEntDisp;
  3353. enteredText = previouslyEntered[prevEntDisp] + "_";
  3354. refreshEnteredText();
  3355. }
  3356. break;
  3357. }
  3358. case SDLK_DOWN: //down arrow
  3359. {
  3360. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  3361. {
  3362. ++prevEntDisp;
  3363. enteredText = previouslyEntered[prevEntDisp] + "_";
  3364. refreshEnteredText();
  3365. }
  3366. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  3367. {
  3368. prevEntDisp = -1;
  3369. enteredText = "_";
  3370. refreshEnteredText();
  3371. }
  3372. break;
  3373. }
  3374. default:
  3375. {
  3376. if(enteredText.size() > 0 && enteredText.size() < conf.go()->ac.inputLineLength)
  3377. {
  3378. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  3379. {
  3380. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  3381. enteredText += "_";
  3382. refreshEnteredText();
  3383. }
  3384. }
  3385. break;
  3386. }
  3387. }
  3388. }
  3389. void CInGameConsole::startEnteringText()
  3390. {
  3391. enteredText = "_";
  3392. if(GH.topInt() == adventureInt)
  3393. {
  3394. GH.statusbar->alignment = TOPLEFT;
  3395. GH.statusbar->calcOffset();
  3396. GH.statusbar->print(enteredText);
  3397. //Prevent changes to the text from mouse interaction with the adventure map
  3398. GH.statusbar->lock(true);
  3399. }
  3400. else if(LOCPLINT->battleInt)
  3401. {
  3402. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3403. }
  3404. }
  3405. void CInGameConsole::endEnteringText(bool printEnteredText)
  3406. {
  3407. prevEntDisp = -1;
  3408. if(printEnteredText)
  3409. {
  3410. std::string txt = enteredText.substr(0, enteredText.size()-1);
  3411. LOCPLINT->cb->sendMessage(txt);
  3412. previouslyEntered.push_back(txt);
  3413. //print(txt);
  3414. }
  3415. enteredText = "";
  3416. if(GH.topInt() == adventureInt)
  3417. {
  3418. GH.statusbar->alignment = CENTER;
  3419. GH.statusbar->calcOffset();
  3420. GH.statusbar->lock(false);
  3421. GH.statusbar->clear();
  3422. }
  3423. else if(LOCPLINT->battleInt)
  3424. {
  3425. LOCPLINT->battleInt->console->ingcAlter = "";
  3426. }
  3427. }
  3428. void CInGameConsole::refreshEnteredText()
  3429. {
  3430. if(GH.topInt() == adventureInt)
  3431. {
  3432. GH.statusbar->lock(false);
  3433. GH.statusbar->clear();
  3434. GH.statusbar->print(enteredText);
  3435. GH.statusbar->lock(true);
  3436. }
  3437. else if(LOCPLINT->battleInt)
  3438. {
  3439. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3440. }
  3441. }
  3442. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  3443. {
  3444. addUsedEvents(KEYBOARD);
  3445. }
  3446. CGarrisonWindow::CGarrisonWindow( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits ):
  3447. CWindowObject(PLAYER_COLORED, "GARRISON")
  3448. {
  3449. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3450. garr = new CGarrisonInt(92, 127, 4, Point(0,96), background->bg, Point(93,127), up, down, removableUnits);
  3451. {
  3452. CAdventureMapButton *split = new CAdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),88,314,"IDV6432.DEF");
  3453. removeChild(split);
  3454. garr->addSplitBtn(split);
  3455. }
  3456. quit = new CAdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CGarrisonWindow::close,this),399,314,"IOK6432.DEF",SDLK_RETURN);
  3457. std::string titleText;
  3458. if (garr->armedObjs[1]->tempOwner == garr->armedObjs[0]->tempOwner)
  3459. titleText = CGI->generaltexth->allTexts[709];
  3460. else
  3461. {
  3462. titleText = CGI->generaltexth->allTexts[35];
  3463. boost::algorithm::replace_first(titleText, "%s", garr->armedObjs[0]->Slots().begin()->second->type->namePl);
  3464. }
  3465. new CLabel(275, 30, FONT_BIG, CENTER, Colors::YELLOW, titleText);
  3466. new CAnimImage("CREST58", garr->armedObjs[0]->getOwner().getNum(), 0, 28, 124);
  3467. new CAnimImage("PortraitsLarge", dynamic_cast<const CGHeroInstance*>(garr->armedObjs[1])->portrait, 0, 29, 222);
  3468. }
  3469. IShowActivatable::IShowActivatable()
  3470. {
  3471. type = 0;
  3472. }
  3473. CGarrisonHolder::CGarrisonHolder()
  3474. {
  3475. }
  3476. void CWindowWithGarrison::updateGarrisons()
  3477. {
  3478. garr->recreateSlots();
  3479. }
  3480. CArtPlace::CArtPlace(Point position, const CArtifactInstance * Art):
  3481. locked(false), picked(false), marked(false), ourArt(Art)
  3482. {
  3483. pos += position;
  3484. pos.w = pos.h = 44;
  3485. createImage();
  3486. }
  3487. void CArtPlace::createImage()
  3488. {
  3489. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3490. int graphic = 0;
  3491. if (ourArt)
  3492. graphic = ourArt->artType->iconIndex;
  3493. if (locked)
  3494. graphic = ArtifactID::ART_LOCK;
  3495. image = new CAnimImage("artifact", graphic);
  3496. if (!ourArt)
  3497. image->disable();
  3498. selection = new CAnimImage("artifact", ArtifactID::ART_SELECTION);
  3499. selection->disable();
  3500. }
  3501. void CArtPlace::lockSlot(bool on)
  3502. {
  3503. if (locked == on)
  3504. return;
  3505. locked = on;
  3506. if (on)
  3507. image->setFrame(ArtifactID::ART_LOCK);
  3508. else
  3509. image->setFrame(ourArt->artType->iconIndex);
  3510. }
  3511. void CArtPlace::pickSlot(bool on)
  3512. {
  3513. if (picked == on)
  3514. return;
  3515. picked = on;
  3516. if (on)
  3517. image->disable();
  3518. else
  3519. image->enable();
  3520. }
  3521. void CArtPlace::selectSlot(bool on)
  3522. {
  3523. if (marked == on)
  3524. return;
  3525. marked = on;
  3526. if (on)
  3527. selection->enable();
  3528. else
  3529. selection->disable();
  3530. }
  3531. void CArtPlace::clickLeft(tribool down, bool previousState)
  3532. {
  3533. //LRClickableAreaWTextComp::clickLeft(down);
  3534. bool inBackpack = slotID >= GameConstants::BACKPACK_START,
  3535. srcInBackpack = ourOwner->commonInfo->src.slotID >= GameConstants::BACKPACK_START,
  3536. srcInSameHero = ourOwner->commonInfo->src.AOH == ourOwner;
  3537. if(ourOwner->highlightModeCallback && ourArt)
  3538. {
  3539. if(down)
  3540. {
  3541. if(ourArt->artType->id < 7) //War Machine or Spellbook
  3542. {
  3543. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]); //This item can't be traded.
  3544. }
  3545. else
  3546. {
  3547. ourOwner->unmarkSlots(false);
  3548. selectSlot(true);
  3549. ourOwner->highlightModeCallback(this);
  3550. }
  3551. }
  3552. return;
  3553. }
  3554. // If clicked on spellbook, open it only if no artifact is held at the moment.
  3555. if(ourArt && !down && previousState && !ourOwner->commonInfo->src.AOH)
  3556. {
  3557. if(ourArt->artType->id == 0)
  3558. {
  3559. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (screen->w - 620)/2, (screen->h - 595)/2), ourOwner->curHero, LOCPLINT, LOCPLINT->battleInt);
  3560. GH.pushInt(spellWindow);
  3561. }
  3562. }
  3563. if (!down && previousState)
  3564. {
  3565. if(ourArt && ourArt->artType->id == 0) //spellbook
  3566. return; //this is handled separately
  3567. if(!ourOwner->commonInfo->src.AOH) //nothing has been clicked
  3568. {
  3569. if(ourArt //to prevent selecting empty slots (bugfix to what GrayFace reported)
  3570. && ourOwner->curHero->tempOwner == LOCPLINT->playerID)//can't take art from another player
  3571. {
  3572. if(ourArt->artType->id == 3) //catapult cannot be highlighted
  3573. {
  3574. std::vector<CComponent *> catapult(1, new CComponent(CComponent::artifact, 3, 0));
  3575. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312], catapult); //The Catapult must be equipped.
  3576. return;
  3577. }
  3578. select();
  3579. }
  3580. }
  3581. else if(ourArt == ourOwner->commonInfo->src.art) //restore previously picked artifact
  3582. {
  3583. deselect();
  3584. }
  3585. else //perform artifact transition
  3586. {
  3587. if(inBackpack) // Backpack destination.
  3588. {
  3589. if(srcInBackpack && slotID == ourOwner->commonInfo->src.slotID + 1) //next slot (our is not visible, so visually same as "old" place) to the art -> make nothing, return artifact to slot
  3590. {
  3591. deselect();
  3592. }
  3593. else
  3594. {
  3595. const CArtifact * const cur = ourOwner->commonInfo->src.art->artType;
  3596. switch(cur->id)
  3597. {
  3598. case ArtifactID::CATAPULT:
  3599. //should not happen, catapult cannot be selected
  3600. assert(cur->id != ArtifactID::CATAPULT);
  3601. break;
  3602. case ArtifactID::BALLISTA: case ArtifactID::AMMO_CART: case ArtifactID::FIRST_AID_TENT: //war machines cannot go to backpack
  3603. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[153]) % cur->Name()));
  3604. break;
  3605. default:
  3606. setMeAsDest();
  3607. vstd::amin(ourOwner->commonInfo->dst.slotID, ArtifactPosition(
  3608. ourOwner->curHero->artifactsInBackpack.size() + GameConstants::BACKPACK_START));
  3609. if(srcInBackpack && srcInSameHero)
  3610. {
  3611. if(!ourArt //cannot move from backpack to AFTER backpack -> combined with vstd::amin above it will guarantee that dest is at most the last artifact
  3612. || ourOwner->commonInfo->src.slotID < ourOwner->commonInfo->dst.slotID) //rearranging arts in backpack after taking src artifact, the dest id will be shifted
  3613. vstd::advance(ourOwner->commonInfo->dst.slotID, -1);
  3614. }
  3615. if(srcInSameHero && ourOwner->commonInfo->dst.slotID == ourOwner->commonInfo->src.slotID) //we came to src == dst
  3616. deselect();
  3617. else
  3618. ourOwner->realizeCurrentTransaction();
  3619. break;
  3620. }
  3621. }
  3622. }
  3623. //check if swap is possible
  3624. else if (fitsHere(ourOwner->commonInfo->src.art) &&
  3625. (!ourArt || ourOwner->curHero->tempOwner == LOCPLINT->playerID))
  3626. {
  3627. setMeAsDest();
  3628. //
  3629. // // Special case when the dest artifact can't be fit into the src slot.
  3630. // //CGI->arth->unequipArtifact(ourOwner->curHero->artifWorn, slotID);
  3631. // const CArtifactsOfHero* srcAOH = ourOwner->commonInfo->src.AOH;
  3632. // ui16 srcSlotID = ourOwner->commonInfo->src.slotID;
  3633. // if (ourArt && srcSlotID < 19 && !ourArt->canBePutAt(ArtifactLocation(srcAOH->curHero, srcSlotID)))
  3634. // {
  3635. // // Put dest artifact into owner's backpack.
  3636. // ourOwner->commonInfo->src.AOH = ourOwner;
  3637. // ourOwner->commonInfo->src.slotID = ourOwner->curHero->artifacts.size() + 19;
  3638. // }
  3639. ourOwner->realizeCurrentTransaction();
  3640. }
  3641. }
  3642. }
  3643. }
  3644. void CArtPlace::clickRight(tribool down, bool previousState)
  3645. {
  3646. if(down && ourArt && !locked && text.size() && !picked) //if there is no description or it's a lock, do nothing ;]
  3647. {
  3648. if (slotID < GameConstants::BACKPACK_START)
  3649. {
  3650. if(ourOwner->allowedAssembling)
  3651. {
  3652. std::vector<const CArtifact *> assemblyPossibilities = ourArt->assemblyPossibilities(ourOwner->curHero);
  3653. // If the artifact can be assembled, display dialog.
  3654. BOOST_FOREACH(const CArtifact *combination, assemblyPossibilities)
  3655. {
  3656. LOCPLINT->showArtifactAssemblyDialog(
  3657. ourArt->artType->id,
  3658. combination->id,
  3659. true,
  3660. boost::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), ourOwner->curHero, slotID, true, combination->id),
  3661. 0);
  3662. if(assemblyPossibilities.size() > 2)
  3663. {
  3664. logGlobal->warnStream() << "More than one possibility of assembling... taking only first";
  3665. break;
  3666. }
  3667. return;
  3668. }
  3669. // Otherwise if the artifact can be diasassembled, display dialog.
  3670. if(ourArt->canBeDisassembled())
  3671. {
  3672. LOCPLINT->showArtifactAssemblyDialog(
  3673. ourArt->artType->id,
  3674. 0,
  3675. false,
  3676. boost::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), ourOwner->curHero, slotID, false, ArtifactID()),
  3677. 0);
  3678. return;
  3679. }
  3680. }
  3681. }
  3682. // Lastly just show the artifact description.
  3683. LRClickableAreaWTextComp::clickRight(down, previousState);
  3684. }
  3685. }
  3686. /**
  3687. * Selects artifact slot so that the containing artifact looks like it's picked up.
  3688. */
  3689. void CArtPlace::select ()
  3690. {
  3691. if (locked)
  3692. return;
  3693. selectSlot(true);
  3694. pickSlot(true);
  3695. if(ourArt->canBeDisassembled() && slotID < GameConstants::BACKPACK_START) //worn combined artifact -> locks have to disappear
  3696. {
  3697. for(int i = 0; i < GameConstants::BACKPACK_START; i++)
  3698. {
  3699. CArtPlace *ap = ourOwner->getArtPlace(i);
  3700. ap->pickSlot(ourArt->isPart(ap->ourArt));
  3701. }
  3702. }
  3703. //int backpackCorrection = -(slotID - Arts::BACKPACK_START < ourOwner->backpackPos);
  3704. CCS->curh->dragAndDropCursor(new CAnimImage("artifact", ourArt->artType->iconIndex));
  3705. ourOwner->commonInfo->src.setTo(this, false);
  3706. ourOwner->markPossibleSlots(ourArt);
  3707. if(slotID >= GameConstants::BACKPACK_START)
  3708. ourOwner->scrollBackpack(0); //will update slots
  3709. ourOwner->updateParentWindow();
  3710. ourOwner->safeRedraw();
  3711. }
  3712. /**
  3713. * Deselects the artifact slot. FIXME: Not used. Maybe it should?
  3714. */
  3715. void CArtPlace::deselect ()
  3716. {
  3717. pickSlot(false);
  3718. if(ourArt && ourArt->canBeDisassembled()) //combined art returned to its slot -> restore locks
  3719. {
  3720. for(int i = 0; i < GameConstants::BACKPACK_START; i++)
  3721. ourOwner->getArtPlace(i)->pickSlot(false);
  3722. }
  3723. CCS->curh->dragAndDropCursor(NULL);
  3724. ourOwner->unmarkSlots();
  3725. ourOwner->commonInfo->src.clear();
  3726. if(slotID >= GameConstants::BACKPACK_START)
  3727. ourOwner->scrollBackpack(0); //will update slots
  3728. ourOwner->updateParentWindow();
  3729. ourOwner->safeRedraw();
  3730. }
  3731. void CArtPlace::showAll(SDL_Surface * to)
  3732. {
  3733. if (ourArt && !picked && ourArt == ourOwner->curHero->getArt(slotID, false)) //last condition is needed for disassembling -> artifact may be gone, but we don't know yet TODO: real, nice solution
  3734. {
  3735. CIntObject::showAll(to);
  3736. }
  3737. if(marked && active)
  3738. {
  3739. // Draw vertical bars.
  3740. for (int i = 0; i < pos.h; ++i)
  3741. {
  3742. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x, pos.y + i, 240, 220, 120);
  3743. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + pos.w - 1, pos.y + i, 240, 220, 120);
  3744. }
  3745. // Draw horizontal bars.
  3746. for (int i = 0; i < pos.w; ++i)
  3747. {
  3748. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y, 240, 220, 120);
  3749. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y + pos.h - 1, 240, 220, 120);
  3750. }
  3751. }
  3752. }
  3753. bool CArtPlace::fitsHere(const CArtifactInstance * art) const
  3754. {
  3755. // You can place 'no artifact' anywhere.
  3756. if(!art)
  3757. return true;
  3758. // Anything can but War Machines can be placed in backpack.
  3759. if (slotID >= GameConstants::BACKPACK_START)
  3760. return !CGI->arth->isBigArtifact(art->artType->id);
  3761. return art->canBePutAt(ArtifactLocation(ourOwner->curHero, slotID), true);
  3762. }
  3763. void CArtPlace::setMeAsDest(bool backpackAsVoid /*= true*/)
  3764. {
  3765. ourOwner->commonInfo->dst.setTo(this, backpackAsVoid);
  3766. }
  3767. void CArtPlace::setArtifact(const CArtifactInstance *art)
  3768. {
  3769. baseType = -1; //by default we don't store any component
  3770. ourArt = art;
  3771. if(!art)
  3772. {
  3773. image->disable();
  3774. text = std::string();
  3775. hoverText = CGI->generaltexth->allTexts[507];
  3776. }
  3777. else
  3778. {
  3779. image->enable();
  3780. image->setFrame(locked ? ArtifactID::ART_LOCK : art->artType->iconIndex);
  3781. std::string artDesc = ourArt->artType->Description();
  3782. if (vstd::contains (artDesc, '{'))
  3783. text = artDesc;
  3784. else
  3785. text = '{' + ourArt->artType->Name() + "}\n\n" + artDesc; //workaround for new artifacts with single name, turns it to H3-style
  3786. if(art->artType->id == 1) //spell scroll
  3787. {
  3788. // we expect scroll description to be like this: This scroll contains the [spell name] spell which is added into your spell book for as long as you carry the scroll.
  3789. // so we want to replace text in [...] with a spell name
  3790. // however other language versions don't have name placeholder at all, so we have to be careful
  3791. int spellID = art->getGivenSpellID();
  3792. size_t nameStart = text.find_first_of('[');
  3793. size_t nameEnd = text.find_first_of(']', nameStart);
  3794. if(spellID >= 0)
  3795. {
  3796. if(nameStart != std::string::npos && nameEnd != std::string::npos)
  3797. text = text.replace(nameStart, nameEnd - nameStart + 1, CGI->spellh->spells[spellID]->name);
  3798. //add spell component info (used to provide a pic in r-click popup)
  3799. baseType = CComponent::spell;
  3800. type = spellID;
  3801. bonusValue = 0;
  3802. }
  3803. }
  3804. else
  3805. {
  3806. baseType = CComponent::artifact;
  3807. type = art->artType->iconIndex;
  3808. bonusValue = 0;
  3809. }
  3810. if (locked) // Locks should appear as empty.
  3811. hoverText = CGI->generaltexth->allTexts[507];
  3812. else
  3813. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->Name());
  3814. }
  3815. }
  3816. void LRClickableAreaWTextComp::clickLeft(tribool down, bool previousState)
  3817. {
  3818. if((!down) && previousState)
  3819. {
  3820. std::vector<CComponent*> comp(1, createComponent());
  3821. LOCPLINT->showInfoDialog(text, comp);
  3822. }
  3823. }
  3824. LRClickableAreaWTextComp::LRClickableAreaWTextComp(const Rect &Pos, int BaseType)
  3825. : LRClickableAreaWText(Pos), baseType(BaseType), bonusValue(-1)
  3826. {
  3827. }
  3828. CComponent * LRClickableAreaWTextComp::createComponent() const
  3829. {
  3830. if(baseType >= 0)
  3831. return new CComponent(CComponent::Etype(baseType), type, bonusValue);
  3832. else
  3833. return NULL;
  3834. }
  3835. void LRClickableAreaWTextComp::clickRight(tribool down, bool previousState)
  3836. {
  3837. if(down)
  3838. {
  3839. if(CComponent *comp = createComponent())
  3840. {
  3841. CRClickPopup::createAndPush(text, CInfoWindow::TCompsInfo(1, comp));
  3842. return;
  3843. }
  3844. }
  3845. LRClickableAreaWText::clickRight(down, previousState); //only if with-component variant not occurred
  3846. }
  3847. CHeroArea::CHeroArea(int x, int y, const CGHeroInstance * _hero):hero(_hero)
  3848. {
  3849. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3850. addUsedEvents(LCLICK | RCLICK | HOVER);
  3851. pos.x += x; pos.w = 58;
  3852. pos.y += y; pos.h = 64;
  3853. if (hero)
  3854. new CAnimImage("PortraitsLarge", hero->portrait);
  3855. }
  3856. void CHeroArea::clickLeft(tribool down, bool previousState)
  3857. {
  3858. if((!down) && previousState && hero)
  3859. LOCPLINT->openHeroWindow(hero);
  3860. }
  3861. void CHeroArea::clickRight(tribool down, bool previousState)
  3862. {
  3863. if((!down) && previousState && hero)
  3864. LOCPLINT->openHeroWindow(hero);
  3865. }
  3866. void CHeroArea::hover(bool on)
  3867. {
  3868. if (on && hero)
  3869. GH.statusbar->print(hero->hoverName);
  3870. else
  3871. GH.statusbar->clear();
  3872. }
  3873. void LRClickableAreaOpenTown::clickLeft(tribool down, bool previousState)
  3874. {
  3875. if((!down) && previousState && town)
  3876. {
  3877. LOCPLINT->openTownWindow(town);
  3878. if ( type == 2 )
  3879. LOCPLINT->castleInt->builds->buildingClicked(BuildingID::VILLAGE_HALL);
  3880. else if ( type == 3 && town->fortLevel() )
  3881. LOCPLINT->castleInt->builds->buildingClicked(BuildingID::FORT);
  3882. }
  3883. }
  3884. void LRClickableAreaOpenTown::clickRight(tribool down, bool previousState)
  3885. {
  3886. if((!down) && previousState && town)
  3887. LOCPLINT->openTownWindow(town);//TODO: popup?
  3888. }
  3889. LRClickableAreaOpenTown::LRClickableAreaOpenTown()
  3890. : LRClickableAreaWTextComp(Rect(0,0,0,0), -1)
  3891. {
  3892. }
  3893. void CArtifactsOfHero::SCommonPart::reset()
  3894. {
  3895. src.clear();
  3896. dst.clear();
  3897. CCS->curh->dragAndDropCursor(NULL);
  3898. }
  3899. void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
  3900. {
  3901. // // An update is made, rather than initialization.
  3902. // if (curHero && curHero->id == hero->id)
  3903. // {
  3904. // if(curHero != hero)
  3905. // {
  3906. // //delete curHero;
  3907. // curHero = hero; //was: creating a copy
  3908. // }
  3909. //
  3910. // // Compensate backpack pos if an artifact was insertad before it.
  3911. // if (commonInfo->dst.slotID >= 19 && commonInfo->destAOH == this
  3912. // && commonInfo->dst.slotID - 19 < backpackPos)
  3913. // {
  3914. // backpackPos++;
  3915. // }
  3916. //
  3917. // if (updateState && commonInfo->srcAOH == this)
  3918. // {
  3919. // // A swap was made, make the replaced artifact the current selected.
  3920. // if (commonInfo->dst.slotID < 19 && commonInfo->destArtifact)
  3921. // {
  3922. // // // Temporarily remove artifact from hero.
  3923. // // if (commonInfo->srcSlotID < 19)
  3924. // // CGI->arth->unequipArtifact(curHero->artifWorn, commonInfo->srcSlotID);
  3925. // // else
  3926. // // curHero->artifacts.erase(curHero->artifacts.begin() + (commonInfo->srcSlotID - 19));
  3927. //
  3928. // updateParentWindow(); //TODO: evil! but does the thing
  3929. //
  3930. // // Source <- Dest
  3931. // commonInfo->srcArtifact = commonInfo->destArtifact;
  3932. //
  3933. // // Reset destination parameters.
  3934. // commonInfo->dst.clear();
  3935. //
  3936. // CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[commonInfo->srcArtifact->id].bitmap);
  3937. // markPossibleSlots(commonInfo->srcArtifact);
  3938. // }
  3939. // else if (commonInfo->destAOH != NULL)
  3940. // {
  3941. // // Reset all parameters.
  3942. // commonInfo->reset();
  3943. // unmarkSlots();
  3944. // }
  3945. // }
  3946. // }
  3947. // else
  3948. // {
  3949. // commonInfo->reset();
  3950. // }
  3951. //
  3952. // if(hero != curHero)
  3953. // {
  3954. // // delete curHero;
  3955. // // curHero = new CGHeroInstance(*hero);
  3956. // curHero = hero; //was: creating a copy
  3957. // }
  3958. curHero = hero;
  3959. if (curHero->artifactsInBackpack.size() > 0)
  3960. backpackPos %= curHero->artifactsInBackpack.size();
  3961. else
  3962. backpackPos = 0;
  3963. // Fill the slots for worn artifacts and backpack.
  3964. for (int g = 0; g < artWorn.size() ; g++)
  3965. setSlotData(artWorn[g], ArtifactPosition(g));
  3966. scrollBackpack(0);
  3967. }
  3968. void CArtifactsOfHero::dispose()
  3969. {
  3970. //vstd::clear_pointer(curHero);
  3971. //unmarkSlots(false);
  3972. CCS->curh->dragAndDropCursor(NULL);
  3973. }
  3974. void CArtifactsOfHero::scrollBackpack(int dir)
  3975. {
  3976. int artsInBackpack = curHero->artifactsInBackpack.size();
  3977. backpackPos += dir;
  3978. if(backpackPos < 0)// No guarantee of modulus behavior with negative operands -> we keep it positive
  3979. backpackPos += artsInBackpack;
  3980. if(artsInBackpack)
  3981. backpackPos %= artsInBackpack;
  3982. std::multiset<const CArtifactInstance *> toOmit = artifactsOnAltar;
  3983. if(commonInfo->src.art) //if we picked an art from backapck, its slot has to be omitted
  3984. toOmit.insert(commonInfo->src.art);
  3985. int omitedSoFar = 0;
  3986. //set new data
  3987. size_t s = 0;
  3988. for( ; s < artsInBackpack; ++s)
  3989. {
  3990. if (s < artsInBackpack)
  3991. {
  3992. auto slotID = ArtifactPosition(GameConstants::BACKPACK_START + (s + backpackPos)%artsInBackpack);
  3993. const CArtifactInstance *art = curHero->getArt(slotID);
  3994. assert(art);
  3995. if(!vstd::contains(toOmit, art))
  3996. {
  3997. if(s - omitedSoFar < backpack.size())
  3998. setSlotData(backpack[s-omitedSoFar], slotID);
  3999. }
  4000. else
  4001. {
  4002. toOmit -= art;
  4003. omitedSoFar++;
  4004. continue;
  4005. }
  4006. }
  4007. }
  4008. for( ; s - omitedSoFar < backpack.size(); s++)
  4009. eraseSlotData(backpack[s-omitedSoFar], ArtifactPosition(GameConstants::BACKPACK_START + s));
  4010. //in artifact merchant selling artifacts we may have highlight on one of backpack artifacts -> market needs update, cause artifact under highlight changed
  4011. if(highlightModeCallback)
  4012. {
  4013. for(int i = 0; i < backpack.size(); i++)
  4014. {
  4015. if(backpack[i]->marked)
  4016. {
  4017. highlightModeCallback(backpack[i]);
  4018. break;
  4019. }
  4020. }
  4021. }
  4022. //blocking scrolling if there is not enough artifacts to scroll
  4023. bool scrollingPossible = artsInBackpack - omitedSoFar > backpack.size();
  4024. leftArtRoll->block(!scrollingPossible);
  4025. rightArtRoll->block(!scrollingPossible);
  4026. safeRedraw();
  4027. }
  4028. /**
  4029. * Marks possible slots where a given artifact can be placed, except backpack.
  4030. *
  4031. * @param art Artifact checked against.
  4032. */
  4033. void CArtifactsOfHero::markPossibleSlots(const CArtifactInstance* art)
  4034. {
  4035. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  4036. BOOST_FOREACH(CArtPlace *place, aoh->artWorn)
  4037. place->selectSlot(art->canBePutAt(ArtifactLocation(aoh->curHero, place->slotID), true));
  4038. safeRedraw();
  4039. }
  4040. /**
  4041. * Unamarks all slots.
  4042. */
  4043. void CArtifactsOfHero::unmarkSlots(bool withRedraw /*= true*/)
  4044. {
  4045. if(commonInfo)
  4046. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  4047. aoh->unmarkLocalSlots(false);
  4048. else
  4049. unmarkLocalSlots(false);\
  4050. if(withRedraw)
  4051. safeRedraw();
  4052. }
  4053. void CArtifactsOfHero::unmarkLocalSlots(bool withRedraw /*= true*/)
  4054. {
  4055. BOOST_FOREACH(CArtPlace *place, artWorn)
  4056. place->selectSlot(false);
  4057. BOOST_FOREACH(CArtPlace *place, backpack)
  4058. place->selectSlot(false);
  4059. if(withRedraw)
  4060. safeRedraw();
  4061. }
  4062. /**
  4063. * Assigns an artifacts to an artifact place depending on it's new slot ID.
  4064. */
  4065. void CArtifactsOfHero::setSlotData(CArtPlace* artPlace, ArtifactPosition slotID)
  4066. {
  4067. if(!artPlace && slotID >= GameConstants::BACKPACK_START) //spurious call from artifactMoved in attempt to update hidden backpack slot
  4068. {
  4069. return;
  4070. }
  4071. artPlace->pickSlot(false);
  4072. artPlace->slotID = slotID;
  4073. if(const ArtSlotInfo *asi = curHero->getSlot(slotID))
  4074. {
  4075. artPlace->setArtifact(asi->artifact);
  4076. artPlace->lockSlot(asi->locked);
  4077. }
  4078. else
  4079. artPlace->setArtifact(NULL);
  4080. }
  4081. /**
  4082. * Makes given artifact slot appear as empty with a certain slot ID.
  4083. */
  4084. void CArtifactsOfHero::eraseSlotData (CArtPlace* artPlace, ArtifactPosition slotID)
  4085. {
  4086. artPlace->pickSlot(false);
  4087. artPlace->slotID = slotID;
  4088. artPlace->setArtifact(NULL);
  4089. }
  4090. CArtifactsOfHero::CArtifactsOfHero(std::vector<CArtPlace *> ArtWorn, std::vector<CArtPlace *> Backpack,
  4091. CAdventureMapButton *leftScroll, CAdventureMapButton *rightScroll, bool createCommonPart):
  4092. curHero(NULL),
  4093. artWorn(ArtWorn), backpack(Backpack),
  4094. backpackPos(0), commonInfo(NULL), updateState(false),
  4095. leftArtRoll(leftScroll), rightArtRoll(rightScroll),
  4096. allowedAssembling(true), highlightModeCallback(0)
  4097. {
  4098. if(createCommonPart)
  4099. {
  4100. commonInfo = new CArtifactsOfHero::SCommonPart;
  4101. commonInfo->participants.insert(this);
  4102. }
  4103. // Init slots for worn artifacts.
  4104. for (size_t g = 0; g < artWorn.size() ; g++)
  4105. {
  4106. artWorn[g]->ourOwner = this;
  4107. eraseSlotData(artWorn[g], ArtifactPosition(g));
  4108. }
  4109. // Init slots for the backpack.
  4110. for(size_t s=0; s<backpack.size(); ++s)
  4111. {
  4112. backpack[s]->ourOwner = this;
  4113. eraseSlotData(backpack[s], ArtifactPosition(GameConstants::BACKPACK_START + s));
  4114. }
  4115. leftArtRoll->callback += boost::bind(&CArtifactsOfHero::scrollBackpack,this,-1);
  4116. rightArtRoll->callback += boost::bind(&CArtifactsOfHero::scrollBackpack,this,+1);
  4117. }
  4118. CArtifactsOfHero::CArtifactsOfHero(const Point& position, bool createCommonPart /*= false*/)
  4119. : curHero(NULL), backpackPos(0), commonInfo(NULL), updateState(false), allowedAssembling(true), highlightModeCallback(0)
  4120. {
  4121. if(createCommonPart)
  4122. {
  4123. commonInfo = new CArtifactsOfHero::SCommonPart;
  4124. commonInfo->participants.insert(this);
  4125. }
  4126. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4127. pos += position;
  4128. artWorn.resize(19);
  4129. std::vector<Point> slotPos;
  4130. slotPos += Point(509,30), Point(567,240), Point(509,80),
  4131. Point(383,68), Point(564,183), Point(509,130),
  4132. Point(431,68), Point(610,183), Point(515,295),
  4133. Point(383,143), Point(399,194), Point(415,245),
  4134. Point(431,296), Point(564,30), Point(610,30),
  4135. Point(610,76), Point(610,122), Point(610,310),
  4136. Point(381,296);
  4137. // Create slots for worn artifacts.
  4138. for (size_t g = 0; g < GameConstants::BACKPACK_START ; g++)
  4139. {
  4140. artWorn[g] = new CArtPlace(slotPos[g]);
  4141. artWorn[g]->ourOwner = this;
  4142. eraseSlotData(artWorn[g], ArtifactPosition(g));
  4143. }
  4144. // Create slots for the backpack.
  4145. for(size_t s=0; s<5; ++s)
  4146. {
  4147. CArtPlace * add = new CArtPlace(Point(403 + 46 * s, 365));
  4148. add->ourOwner = this;
  4149. eraseSlotData(add, ArtifactPosition(GameConstants::BACKPACK_START + s));
  4150. backpack.push_back(add);
  4151. }
  4152. leftArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,-1), 379, 364, "hsbtns3.def", SDLK_LEFT);
  4153. rightArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,+1), 632, 364, "hsbtns5.def", SDLK_RIGHT);
  4154. }
  4155. CArtifactsOfHero::~CArtifactsOfHero()
  4156. {
  4157. dispose();
  4158. }
  4159. void CArtifactsOfHero::updateParentWindow()
  4160. {
  4161. if (CHeroWindow* chw = dynamic_cast<CHeroWindow*>(GH.topInt()))
  4162. {
  4163. if(updateState)
  4164. chw->curHero = curHero;
  4165. else
  4166. chw->update(curHero, true);
  4167. }
  4168. else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt()))
  4169. {
  4170. //use our copy of hero to draw window
  4171. if(cew->heroInst[0]->id == curHero->id)
  4172. cew->heroInst[0] = curHero;
  4173. else
  4174. cew->heroInst[1] = curHero;
  4175. if(!updateState)
  4176. {
  4177. cew->deactivate();
  4178. // for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  4179. // {
  4180. // if(cew->heroInst[g] == curHero)
  4181. // {
  4182. // cew->artifs[g]->setHero(curHero);
  4183. // }
  4184. // }
  4185. cew->prepareBackground();
  4186. cew->redraw();
  4187. cew->activate();
  4188. }
  4189. }
  4190. }
  4191. void CArtifactsOfHero::safeRedraw()
  4192. {
  4193. if (active)
  4194. {
  4195. if(parent)
  4196. parent->redraw();
  4197. else
  4198. redraw();
  4199. }
  4200. }
  4201. void CArtifactsOfHero::realizeCurrentTransaction()
  4202. {
  4203. assert(commonInfo->src.AOH);
  4204. assert(commonInfo->dst.AOH);
  4205. LOCPLINT->cb->swapArtifacts(ArtifactLocation(commonInfo->src.AOH->curHero, commonInfo->src.slotID),
  4206. ArtifactLocation(commonInfo->dst.AOH->curHero, commonInfo->dst.slotID));
  4207. }
  4208. void CArtifactsOfHero::artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst)
  4209. {
  4210. bool isCurHeroSrc = src.isHolder(curHero),
  4211. isCurHeroDst = dst.isHolder(curHero);
  4212. if(isCurHeroSrc && src.slot >= GameConstants::BACKPACK_START)
  4213. updateSlot(src.slot);
  4214. if(isCurHeroDst && dst.slot >= GameConstants::BACKPACK_START)
  4215. updateSlot(dst.slot);
  4216. if(isCurHeroSrc || isCurHeroDst) //we need to update all slots, artifact might be combined and affect more slots
  4217. updateWornSlots(false);
  4218. if (!src.isHolder(curHero) && !isCurHeroDst)
  4219. return;
  4220. if(commonInfo->src == src) //artifact was taken from us
  4221. {
  4222. assert(commonInfo->dst == dst //expected movement from slot ot slot
  4223. || dst.slot == dst.getHolderArtSet()->artifactsInBackpack.size() + GameConstants::BACKPACK_START //artifact moved back to backpack (eg. to make place for art we are moving)
  4224. || dst.getHolderArtSet()->bearerType() != ArtBearer::HERO);
  4225. commonInfo->reset();
  4226. unmarkSlots();
  4227. }
  4228. else if(commonInfo->dst == src) //the dest artifact was moved -> we are picking it
  4229. {
  4230. assert(dst.slot >= GameConstants::BACKPACK_START);
  4231. commonInfo->reset();
  4232. CArtPlace *ap = NULL;
  4233. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  4234. {
  4235. if(dst.isHolder(aoh->curHero))
  4236. {
  4237. commonInfo->src.AOH = aoh;
  4238. if((ap = aoh->getArtPlace(dst.slot)))
  4239. break;
  4240. }
  4241. }
  4242. if(ap)
  4243. {
  4244. ap->select();
  4245. }
  4246. else
  4247. {
  4248. commonInfo->src.art = dst.getArt();
  4249. commonInfo->src.slotID = dst.slot;
  4250. assert(commonInfo->src.AOH);
  4251. CCS->curh->dragAndDropCursor(new CAnimImage("artifact", dst.getArt()->artType->iconIndex));
  4252. markPossibleSlots(dst.getArt());
  4253. }
  4254. }
  4255. else if(src.slot >= GameConstants::BACKPACK_START &&
  4256. src.slot < commonInfo->src.slotID &&
  4257. src.isHolder(commonInfo->src.AOH->curHero)) //artifact taken from before currently picked one
  4258. {
  4259. //int fixedSlot = src.hero->getArtPos(commonInfo->src.art);
  4260. vstd::advance(commonInfo->src.slotID, -1);
  4261. assert(commonInfo->src.valid());
  4262. }
  4263. else
  4264. {
  4265. //when moving one artifact onto another it leads to two art movements: dst->backapck; src->dst
  4266. // however after first movement we pick the art from backpack and the second movement coming when
  4267. // we have a different artifact may look surprising... but it's valid.
  4268. }
  4269. updateParentWindow();
  4270. int shift = 0;
  4271. // if(dst.slot >= Arts::BACKPACK_START && dst.slot - Arts::BACKPACK_START < backpackPos)
  4272. // shift++;
  4273. //
  4274. if(src.slot < GameConstants::BACKPACK_START && dst.slot - GameConstants::BACKPACK_START < backpackPos)
  4275. shift++;
  4276. if(dst.slot < GameConstants::BACKPACK_START && src.slot - GameConstants::BACKPACK_START < backpackPos)
  4277. shift--;
  4278. if( (isCurHeroSrc && src.slot >= GameConstants::BACKPACK_START)
  4279. || (isCurHeroDst && dst.slot >= GameConstants::BACKPACK_START) )
  4280. scrollBackpack(shift); //update backpack slots
  4281. }
  4282. void CArtifactsOfHero::artifactRemoved(const ArtifactLocation &al)
  4283. {
  4284. if(al.isHolder(curHero))
  4285. {
  4286. if(al.slot < GameConstants::BACKPACK_START)
  4287. updateWornSlots(0);
  4288. else
  4289. scrollBackpack(0); //update backpack slots
  4290. }
  4291. }
  4292. CArtPlace * CArtifactsOfHero::getArtPlace(int slot)
  4293. {
  4294. if(slot < GameConstants::BACKPACK_START)
  4295. {
  4296. return artWorn[slot];
  4297. }
  4298. else
  4299. {
  4300. BOOST_FOREACH(CArtPlace *ap, backpack)
  4301. if(ap->slotID == slot)
  4302. return ap;
  4303. }
  4304. return NULL;
  4305. }
  4306. void CArtifactsOfHero::artifactAssembled(const ArtifactLocation &al)
  4307. {
  4308. if(al.isHolder(curHero))
  4309. updateWornSlots();
  4310. }
  4311. void CArtifactsOfHero::artifactDisassembled(const ArtifactLocation &al)
  4312. {
  4313. if(al.isHolder(curHero))
  4314. updateWornSlots();
  4315. }
  4316. void CArtifactsOfHero::updateWornSlots(bool redrawParent /*= true*/)
  4317. {
  4318. for(int i = 0; i < artWorn.size(); i++)
  4319. updateSlot(ArtifactPosition(i));
  4320. if(redrawParent)
  4321. updateParentWindow();
  4322. }
  4323. const CGHeroInstance * CArtifactsOfHero::getHero() const
  4324. {
  4325. return curHero;
  4326. }
  4327. void CArtifactsOfHero::updateSlot(ArtifactPosition slotID)
  4328. {
  4329. setSlotData(getArtPlace(slotID), slotID);
  4330. }
  4331. void CExchangeWindow::questlog(int whichHero)
  4332. {
  4333. CCS->curh->dragAndDropCursor(NULL);
  4334. }
  4335. void CExchangeWindow::prepareBackground()
  4336. {
  4337. //printing heroes' names and levels
  4338. auto genTitle = [](const CGHeroInstance *h)
  4339. {
  4340. return boost::str(boost::format(CGI->generaltexth->allTexts[138])
  4341. % h->name % h->level % h->type->heroClass->name);
  4342. };
  4343. new CLabel(147, 25, FONT_SMALL, CENTER, Colors::WHITE, genTitle(heroInst[0]));
  4344. new CLabel(653, 25, FONT_SMALL, CENTER, Colors::WHITE, genTitle(heroInst[1]));
  4345. //printing primary skills
  4346. for(int g=0; g<4; ++g)
  4347. new CAnimImage("PSKIL32", g, 0, 385, 19 + 36*g);
  4348. //heroes related thing
  4349. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  4350. {
  4351. CHeroWithMaybePickedArtifact heroWArt = CHeroWithMaybePickedArtifact(this, heroInst[b]);
  4352. //printing primary skills' amounts
  4353. for(int m=0; m<GameConstants::PRIMARY_SKILLS; ++m)
  4354. new CLabel(352 + 93 * b, 35 + 36 * m, FONT_SMALL, CENTER, Colors::WHITE,
  4355. boost::lexical_cast<std::string>(heroWArt.getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(m))));
  4356. //printing secondary skills
  4357. for(int m=0; m<heroInst[b]->secSkills.size(); ++m)
  4358. {
  4359. int id = heroInst[b]->secSkills[m].first;
  4360. int level = heroInst[b]->secSkills[m].second;
  4361. new CAnimImage("SECSK32", id*3 + level + 2 , 0, 32 + 36 * m + 454 * b, 88);
  4362. }
  4363. //hero's specialty
  4364. new CAnimImage("UN32", heroInst[b]->type->imageIndex, 0, 67 + 490*b, 45);
  4365. //experience
  4366. new CAnimImage("PSKIL32", 4, 0, 103 + 490*b, 45);
  4367. new CLabel(119 + 490*b, 71, FONT_SMALL, CENTER, Colors::WHITE, makeNumberShort(heroInst[b]->exp));
  4368. //mana points
  4369. new CAnimImage("PSKIL32", 5, 0, 139 + 490*b, 45);
  4370. new CLabel(155 + 490*b, 71, FONT_SMALL, CENTER, Colors::WHITE, makeNumberShort(heroInst[b]->mana));
  4371. }
  4372. //printing portraits
  4373. new CAnimImage("PortraitsLarge", heroInst[0]->portrait, 0, 257, 13);
  4374. new CAnimImage("PortraitsLarge", heroInst[1]->portrait, 0, 485, 13);
  4375. }
  4376. CExchangeWindow::CExchangeWindow(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID queryID):
  4377. CWindowObject(PLAYER_COLORED | BORDERED, "TRADE2")
  4378. {
  4379. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4380. char bufor[400];
  4381. heroInst[0] = LOCPLINT->cb->getHero(hero1);
  4382. heroInst[1] = LOCPLINT->cb->getHero(hero2);
  4383. prepareBackground();
  4384. artifs[0] = new CArtifactsOfHero(Point(-334, 150));
  4385. artifs[0]->commonInfo = new CArtifactsOfHero::SCommonPart;
  4386. artifs[0]->commonInfo->participants.insert(artifs[0]);
  4387. artifs[0]->setHero(heroInst[0]);
  4388. artifs[1] = new CArtifactsOfHero(Point(96, 150));
  4389. artifs[1]->commonInfo = artifs[0]->commonInfo;
  4390. artifs[1]->commonInfo->participants.insert(artifs[1]);
  4391. artifs[1]->setHero(heroInst[1]);
  4392. artSets.push_back(artifs[0]);
  4393. artSets.push_back(artifs[1]);
  4394. //primary skills
  4395. for(int g=0; g<4; ++g)
  4396. {
  4397. //primary skill's clickable areas
  4398. primSkillAreas.push_back(new LRClickableAreaWTextComp());
  4399. primSkillAreas[g]->pos = genRect(32, 140, pos.x + 329, pos.y + 19 + 36 * g);
  4400. primSkillAreas[g]->text = CGI->generaltexth->arraytxt[2+g];
  4401. primSkillAreas[g]->type = g;
  4402. primSkillAreas[g]->bonusValue = -1;
  4403. primSkillAreas[g]->baseType = 0;
  4404. sprintf(bufor, CGI->generaltexth->heroscrn[1].c_str(), CGI->generaltexth->primarySkillNames[g].c_str());
  4405. primSkillAreas[g]->hoverText = std::string(bufor);
  4406. }
  4407. //heroes related thing
  4408. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  4409. {
  4410. //secondary skill's clickable areas
  4411. for(int g=0; g<heroInst[b]->secSkills.size(); ++g)
  4412. {
  4413. int skill = heroInst[b]->secSkills[g].first,
  4414. level = heroInst[b]->secSkills[g].second; // <1, 3>
  4415. secSkillAreas[b].push_back(new LRClickableAreaWTextComp());
  4416. secSkillAreas[b][g]->pos = genRect(32, 32, pos.x + 32 + g*36 + b*454 , pos.y + 88);
  4417. secSkillAreas[b][g]->baseType = 1;
  4418. secSkillAreas[b][g]->type = skill;
  4419. secSkillAreas[b][g]->bonusValue = level;
  4420. secSkillAreas[b][g]->text = CGI->generaltexth->skillInfoTexts[skill][level-1];
  4421. sprintf(bufor, CGI->generaltexth->heroscrn[21].c_str(), CGI->generaltexth->levels[level - 1].c_str(), CGI->generaltexth->skillName[skill].c_str());
  4422. secSkillAreas[b][g]->hoverText = std::string(bufor);
  4423. }
  4424. portrait[b] = new CHeroArea(257 + 228*b, 13, heroInst[b]);
  4425. specialty[b] = new LRClickableAreaWText();
  4426. specialty[b]->pos = genRect(32, 32, pos.x + 69 + 490*b, pos.y + 45);
  4427. specialty[b]->hoverText = CGI->generaltexth->heroscrn[27];
  4428. specialty[b]->text = heroInst[b]->type->specDescr;
  4429. experience[b] = new LRClickableAreaWText();
  4430. experience[b]->pos = genRect(32, 32, pos.x + 105 + 490*b, pos.y + 45);
  4431. experience[b]->hoverText = CGI->generaltexth->heroscrn[9];
  4432. experience[b]->text = CGI->generaltexth->allTexts[2].c_str();
  4433. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->level));
  4434. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(heroInst[b]->level+1)));
  4435. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->exp));
  4436. spellPoints[b] = new LRClickableAreaWText();
  4437. spellPoints[b]->pos = genRect(32, 32, pos.x + 141 + 490*b, pos.y + 45);
  4438. spellPoints[b]->hoverText = CGI->generaltexth->heroscrn[22];
  4439. sprintf(bufor, CGI->generaltexth->allTexts[205].c_str(), heroInst[b]->name.c_str(), heroInst[b]->mana, heroInst[b]->manaLimit());
  4440. spellPoints[b]->text = std::string(bufor);
  4441. //setting morale
  4442. morale[b] = new MoraleLuckBox(true, genRect(32, 32, 176 + 490*b, 39), true);
  4443. morale[b]->set(heroInst[b]);
  4444. //setting luck
  4445. luck[b] = new MoraleLuckBox(false, genRect(32, 32, 212 + 490*b, 39), true);
  4446. luck[b]->set(heroInst[b]);
  4447. }
  4448. //buttons
  4449. quit = new CAdventureMapButton(CGI->generaltexth->zelp[600], boost::bind(&CExchangeWindow::close, this), 732, 567, "IOKAY.DEF", SDLK_RETURN);
  4450. if(queryID.getNum() > 0)
  4451. quit->callback += [=]{ LOCPLINT->cb->selectionMade(0, queryID); };
  4452. questlogButton[0] = new CAdventureMapButton(CGI->generaltexth->heroscrn[0], "", boost::bind(&CExchangeWindow::questlog,this, 0), 10, 44, "hsbtns4.def");
  4453. questlogButton[1] = new CAdventureMapButton(CGI->generaltexth->heroscrn[0], "", boost::bind(&CExchangeWindow::questlog,this, 1), 740, 44, "hsbtns4.def");
  4454. Rect barRect(5, 578, 725, 18);
  4455. ourBar = new CGStatusBar(new CPicture(*background, barRect, 5, 578, false));
  4456. //garrison interface
  4457. garr = new CGarrisonInt(69, 131, 4, Point(418,0), *background, Point(69,131), heroInst[0],heroInst[1], true, true);
  4458. garr->addSplitBtn(new CAdventureMapButton(CGI->generaltexth->tcommands[3], "", boost::bind(&CGarrisonInt::splitClick, garr), 10, 132, "TSBTNS.DEF"));
  4459. garr->addSplitBtn(new CAdventureMapButton(CGI->generaltexth->tcommands[3], "", boost::bind(&CGarrisonInt::splitClick, garr), 740, 132, "TSBTNS.DEF"));
  4460. }
  4461. CExchangeWindow::~CExchangeWindow() //d-tor
  4462. {
  4463. delete artifs[0]->commonInfo;
  4464. artifs[0]->commonInfo = NULL;
  4465. artifs[1]->commonInfo = NULL;
  4466. }
  4467. CShipyardWindow::CShipyardWindow(const std::vector<si32> &cost, int state, int boatType, const boost::function<void()> &onBuy):
  4468. CWindowObject(PLAYER_COLORED, "TPSHIP")
  4469. {
  4470. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4471. bgWater = new CPicture("TPSHIPBK", 100, 69);
  4472. std::string boatFilenames[3] = {"AB01_", "AB02_", "AB03_"};
  4473. Point waterCenter = Point(bgWater->pos.x+bgWater->pos.w/2, bgWater->pos.y+bgWater->pos.h/2);
  4474. bgShip = new CAnimImage(boatFilenames[boatType], 0, 7, 120, 96, CShowableAnim::USE_RLE);
  4475. bgShip->center(waterCenter);
  4476. // Create resource icons and costs.
  4477. std::string goldValue = boost::lexical_cast<std::string>(cost[Res::GOLD]);
  4478. std::string woodValue = boost::lexical_cast<std::string>(cost[Res::WOOD]);
  4479. goldCost = new CLabel(118, 294, FONT_SMALL, CENTER, Colors::WHITE, goldValue);
  4480. woodCost = new CLabel(212, 294, FONT_SMALL, CENTER, Colors::WHITE, woodValue);
  4481. goldPic = new CAnimImage("RESOURCE", Res::GOLD, 0, 100, 244);
  4482. woodPic = new CAnimImage("RESOURCE", Res::WOOD, 0, 196, 244);
  4483. quit = new CAdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CShipyardWindow::close, this), 224, 312, "ICANCEL", SDLK_RETURN);
  4484. build = new CAdventureMapButton(CGI->generaltexth->allTexts[598], "", boost::bind(&CShipyardWindow::close, this), 42, 312, "IBUY30", SDLK_RETURN);
  4485. build->callback += onBuy;
  4486. for(Res::ERes i = Res::WOOD; i <= Res::GOLD; vstd::advance(i, 1))
  4487. {
  4488. if(cost[i] > LOCPLINT->cb->getResourceAmount(i))
  4489. {
  4490. build->block(true);
  4491. break;
  4492. }
  4493. }
  4494. statusBar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  4495. title = new CLabel(164, 27, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[13]);
  4496. costLabel = new CLabel(164, 220, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->jktexts[14]);
  4497. }
  4498. CPuzzleWindow::CPuzzleWindow(const int3 &GrailPos, double discoveredRatio):
  4499. CWindowObject(PLAYER_COLORED | BORDERED, "PUZZLE"),
  4500. grailPos(GrailPos),
  4501. currentAlpha(SDL_ALPHA_OPAQUE)
  4502. {
  4503. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4504. CCS->soundh->playSound(soundBase::OBELISK);
  4505. quitb = new CAdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CPuzzleWindow::close, this), 670, 538, "IOK6432.DEF", SDLK_RETURN);
  4506. quitb->assignedKeys.insert(SDLK_ESCAPE);
  4507. quitb->borderColor = Colors::METALLIC_GOLD;
  4508. quitb->borderEnabled = true;
  4509. new CPicture("PUZZLOGO", 607, 3);
  4510. new CLabel(700, 95, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[463]);
  4511. new CResDataBar("ARESBAR.bmp", 3, 575, 32, 2, 85, 85);
  4512. int faction = LOCPLINT->cb->getStartInfo()->playerInfos.find(LOCPLINT->playerID)->second.castle;
  4513. auto & puzzleMap = CGI->townh->factions[faction]->puzzleMap;
  4514. for(int g=0; g<puzzleMap.size(); ++g)
  4515. {
  4516. const SPuzzleInfo & info = puzzleMap[g];
  4517. auto piece = new CPicture(info.filename, info.x, info.y);
  4518. //piece that will slowly disappear
  4519. if(info.whenUncovered <= GameConstants::PUZZLE_MAP_PIECES * discoveredRatio)
  4520. {
  4521. piecesToRemove.push_back(piece);
  4522. piece->needRefresh = true;
  4523. piece->recActions = piece->recActions & ~SHOWALL;
  4524. }
  4525. }
  4526. }
  4527. void CPuzzleWindow::showAll(SDL_Surface * to)
  4528. {
  4529. int3 moveInt = int3(8, 9, 0);
  4530. Rect mapRect = genRect(544, 591, pos.x + 8, pos.y + 7);
  4531. CGI->mh->terrainRect
  4532. (grailPos - moveInt, adventureInt->anim,
  4533. &LOCPLINT->cb->getVisibilityMap(), true, adventureInt->heroAnim,
  4534. to, &mapRect, 0, 0, true, moveInt);
  4535. CWindowObject::showAll(to);
  4536. }
  4537. void CPuzzleWindow::show(SDL_Surface * to)
  4538. {
  4539. static int animSpeed = 2;
  4540. if (currentAlpha < animSpeed)
  4541. {
  4542. //animation done
  4543. BOOST_FOREACH(auto & piece, piecesToRemove)
  4544. delete piece;
  4545. piecesToRemove.clear();
  4546. }
  4547. else
  4548. {
  4549. //update disappearing puzzles
  4550. BOOST_FOREACH(auto & piece, piecesToRemove)
  4551. piece->setAlpha(currentAlpha);
  4552. currentAlpha -= animSpeed;
  4553. }
  4554. CWindowObject::show(to);
  4555. }
  4556. void CTransformerWindow::CItem::move()
  4557. {
  4558. if (left)
  4559. moveBy(Point(289, 0));
  4560. else
  4561. moveBy(Point(-289, 0));
  4562. left = !left;
  4563. }
  4564. void CTransformerWindow::CItem::clickLeft(tribool down, bool previousState)
  4565. {
  4566. if(previousState && (!down))
  4567. {
  4568. move();
  4569. parent->showAll(screen2);
  4570. }
  4571. }
  4572. void CTransformerWindow::CItem::update()
  4573. {
  4574. icon->setFrame(parent->army->getCreature(SlotID(id))->idNumber + 2);
  4575. }
  4576. CTransformerWindow::CItem::CItem(CTransformerWindow * parent, int size, int id):
  4577. id(id), size(size), parent(parent)
  4578. {
  4579. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4580. addUsedEvents(LCLICK);
  4581. left = true;
  4582. pos.w = 58;
  4583. pos.h = 64;
  4584. pos.x += 45 + (id%3)*83 + id/6*83;
  4585. pos.y += 109 + (id/3)*98;
  4586. icon = new CAnimImage("TWCRPORT", parent->army->getCreature(SlotID(id))->idNumber + 2);
  4587. new CLabel(28, 76,FONT_SMALL, CENTER, Colors::WHITE, boost::lexical_cast<std::string>(size));//stack size
  4588. }
  4589. void CTransformerWindow::makeDeal()
  4590. {
  4591. for (int i=0; i<items.size(); i++)
  4592. if (!items[i]->left)
  4593. LOCPLINT->cb->trade(town, EMarketMode::CREATURE_UNDEAD, items[i]->id, 0, 0, hero);
  4594. }
  4595. void CTransformerWindow::addAll()
  4596. {
  4597. for (int i=0; i<items.size(); i++)
  4598. if (items[i]->left)
  4599. items[i]->move();
  4600. showAll(screen2);
  4601. }
  4602. void CTransformerWindow::updateGarrisons()
  4603. {
  4604. BOOST_FOREACH(auto & item, items)
  4605. {
  4606. item->update();
  4607. }
  4608. }
  4609. CTransformerWindow::CTransformerWindow(const CGHeroInstance * _hero, const CGTownInstance * _town):
  4610. CWindowObject(PLAYER_COLORED, "SKTRNBK"),
  4611. hero(_hero),
  4612. town(_town)
  4613. {
  4614. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4615. if (hero)
  4616. army = hero;
  4617. else
  4618. army = town;
  4619. for (int i=0; i<GameConstants::ARMY_SIZE; i++ )
  4620. if ( army->getCreature(SlotID(i)) )
  4621. items.push_back(new CItem(this, army->getStackCount(SlotID(i)), i));
  4622. all = new CAdventureMapButton(CGI->generaltexth->zelp[590],boost::bind(&CTransformerWindow::addAll,this), 146,416,"ALTARMY.DEF",SDLK_a);
  4623. convert= new CAdventureMapButton(CGI->generaltexth->zelp[591],boost::bind(&CTransformerWindow::makeDeal,this), 269,416,"ALTSACR.DEF",SDLK_RETURN);
  4624. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[592],boost::bind(&CTransformerWindow::close, this),392,416,"ICANCEL.DEF",SDLK_ESCAPE);
  4625. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  4626. new CLabel(153, 29,FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[485]);//holding area
  4627. new CLabel(153+295, 29, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[486]);//transformer
  4628. new CTextBox(CGI->generaltexth->allTexts[487], Rect(26, 56, 255, 40), 0, FONT_MEDIUM, CENTER, Colors::YELLOW);//move creatures to create skeletons
  4629. new CTextBox(CGI->generaltexth->allTexts[488], Rect(320, 56, 255, 40), 0, FONT_MEDIUM, CENTER, Colors::YELLOW);//creatures here will become skeletons
  4630. }
  4631. void CUniversityWindow::CItem::clickLeft(tribool down, bool previousState)
  4632. {
  4633. if(previousState && (!down))
  4634. {
  4635. if ( state() != 2 )
  4636. return;
  4637. CUnivConfirmWindow *win = new CUnivConfirmWindow(parent, ID, LOCPLINT->cb->getResourceAmount(Res::GOLD) >= 2000);
  4638. GH.pushInt(win);
  4639. }
  4640. }
  4641. void CUniversityWindow::CItem::clickRight(tribool down, bool previousState)
  4642. {
  4643. if(down)
  4644. {
  4645. CRClickPopup::createAndPush(CGI->generaltexth->skillInfoTexts[ID][0],
  4646. new CComponent(CComponent::secskill, ID, 1));
  4647. }
  4648. }
  4649. void CUniversityWindow::CItem::hover(bool on)
  4650. {
  4651. if (on)
  4652. GH.statusbar->print(CGI->generaltexth->skillName[ID]);
  4653. else
  4654. GH.statusbar->clear();
  4655. }
  4656. int CUniversityWindow::CItem::state()
  4657. {
  4658. if (parent->hero->getSecSkillLevel(SecondarySkill(ID)))//hero know this skill
  4659. return 1;
  4660. if (!parent->hero->canLearnSkill())//can't learn more skills
  4661. return 0;
  4662. if (parent->hero->type->heroClass->secSkillProbability[ID]==0)//can't learn this skill (like necromancy for most of non-necros)
  4663. return 0;
  4664. return 2;
  4665. }
  4666. void CUniversityWindow::CItem::showAll(SDL_Surface * to)
  4667. {
  4668. CPicture * bar;
  4669. switch (state())
  4670. {
  4671. case 0: bar = parent->red;
  4672. break;
  4673. case 1: bar = parent->yellow;
  4674. break;
  4675. case 2: bar = parent->green;
  4676. break;
  4677. default:bar = NULL;
  4678. break;
  4679. }
  4680. assert(bar);
  4681. blitAtLoc(bar->bg, -28, -22, to);
  4682. blitAtLoc(bar->bg, -28, 48, to);
  4683. printAtMiddleLoc (CGI->generaltexth->skillName[ID], 22, -13, FONT_SMALL, Colors::WHITE,to);//Name
  4684. printAtMiddleLoc (CGI->generaltexth->levels[0], 22, 57, FONT_SMALL, Colors::WHITE,to);//Level(always basic)
  4685. CAnimImage::showAll(to);
  4686. }
  4687. CUniversityWindow::CItem::CItem(CUniversityWindow * _parent, int _ID, int X, int Y):
  4688. CAnimImage ("SECSKILL", _ID*3+3, 0, X, Y),
  4689. ID(_ID),
  4690. parent(_parent)
  4691. {
  4692. addUsedEvents(LCLICK | RCLICK | HOVER);
  4693. }
  4694. CUniversityWindow::CUniversityWindow(const CGHeroInstance * _hero, const IMarket * _market):
  4695. CWindowObject(PLAYER_COLORED, "UNIVERS1"),
  4696. hero(_hero),
  4697. market(_market)
  4698. {
  4699. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4700. green = new CPicture("UNIVGREN.PCX");
  4701. yellow = new CPicture("UNIVGOLD.PCX");//bars
  4702. red = new CPicture("UNIVRED.PCX");
  4703. green->recActions =
  4704. yellow->recActions =
  4705. red->recActions = DISPOSE;
  4706. CIntObject * titlePic = nullptr;
  4707. if (market->o->ID == Obj::TOWN)
  4708. titlePic = new CAnimImage(CGI->townh->factions[ETownType::CONFLUX]->town->clientInfo.buildingsIcons, BuildingID::MAGIC_UNIVERSITY);
  4709. else
  4710. titlePic = new CPicture("UNIVBLDG");
  4711. titlePic->center(Point(232 + pos.x, 76 + pos.y));
  4712. //Clerk speech
  4713. new CTextBox(CGI->generaltexth->allTexts[603], Rect(24, 129, 413, 70), 0, FONT_SMALL, CENTER, Colors::WHITE);
  4714. //University
  4715. new CLabel(231, 26, FONT_MEDIUM, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[602]);
  4716. std::vector<int> list = market->availableItemsIds(EMarketMode::RESOURCE_SKILL);
  4717. assert(list.size() == 4);
  4718. for (int i=0; i<list.size(); i++)//prepare clickable items
  4719. items.push_back(new CItem(this, list[i], 54+i*104, 234));
  4720. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[632],
  4721. boost::bind(&CUniversityWindow::close, this),200,313,"IOKAY.DEF",SDLK_RETURN);
  4722. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  4723. }
  4724. CUnivConfirmWindow::CUnivConfirmWindow(CUniversityWindow * PARENT, int SKILL, bool available ):
  4725. CWindowObject(PLAYER_COLORED, "UNIVERS2.PCX"),
  4726. parent(PARENT)
  4727. {
  4728. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4729. std::string text = CGI->generaltexth->allTexts[608];
  4730. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  4731. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  4732. boost::replace_first(text, "%d", "2000");
  4733. new CTextBox(text, Rect(24, 129, 413, 70), 0, FONT_SMALL, CENTER, Colors::WHITE);//Clerk speech
  4734. new CLabel(230, 37, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth-> skillName[SKILL]);//Skill name
  4735. new CAnimImage("SECSKILL", SKILL*3+3, 0, 211, 51);//skill
  4736. new CLabel(230, 107, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->levels[1]);//Skill level
  4737. new CAnimImage("RESOURCE", Res::GOLD, 0, 210, 210);//gold
  4738. new CLabel(230, 267, FONT_SMALL, CENTER, Colors::WHITE, "2000");//Cost
  4739. std::string hoverText = CGI->generaltexth->allTexts[609];
  4740. boost::replace_first(hoverText, "%s", CGI->generaltexth->levels[0]+ " " + CGI->generaltexth->skillName[SKILL]);
  4741. text = CGI->generaltexth->zelp[633].second;
  4742. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  4743. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  4744. boost::replace_first(text, "%d", "2000");
  4745. confirm= new CAdventureMapButton(hoverText, text, boost::bind(&CUnivConfirmWindow::makeDeal, this, SKILL),
  4746. 148,299,"IBY6432.DEF",SDLK_RETURN);
  4747. confirm->block(!available);
  4748. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[631],boost::bind(&CUnivConfirmWindow::close, this),
  4749. 252,299,"ICANCEL.DEF",SDLK_ESCAPE);
  4750. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  4751. }
  4752. void CUnivConfirmWindow::makeDeal(int skill)
  4753. {
  4754. LOCPLINT->cb->trade(parent->market->o, EMarketMode::RESOURCE_SKILL, 6, skill, 1, parent->hero);
  4755. close();
  4756. }
  4757. CHillFortWindow::CHillFortWindow(const CGHeroInstance *visitor, const CGObjectInstance *object):
  4758. CWindowObject(PLAYER_COLORED, "APHLFTBK"),
  4759. fort(object),
  4760. hero(visitor)
  4761. {
  4762. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4763. slotsCount=7;
  4764. resources = CDefHandler::giveDefEss("SMALRES.DEF");
  4765. new CLabel(325, 32, FONT_BIG, CENTER, Colors::YELLOW, fort->hoverName);//Hill Fort
  4766. heroPic = new CHeroArea(30, 60, hero);
  4767. currState.resize(slotsCount+1);
  4768. costs.resize(slotsCount);
  4769. totalSumm.resize(GameConstants::RESOURCE_QUANTITY);
  4770. std::vector<std::string> files;
  4771. files += "APHLF1R.DEF", "APHLF1Y.DEF", "APHLF1G.DEF";
  4772. for (int i=0; i<slotsCount; i++)
  4773. {
  4774. currState[i] = getState(SlotID(i));
  4775. upgrade[i] = new CAdventureMapButton(getTextForSlot(SlotID(i)),"",boost::bind(&CHillFortWindow::makeDeal, this, SlotID(i)),
  4776. 107+i*76, 171, "", SDLK_1+i, &files);
  4777. upgrade[i]->block(currState[i] == -1);
  4778. }
  4779. files.clear();
  4780. files += "APHLF4R.DEF", "APHLF4Y.DEF", "APHLF4G.DEF";
  4781. currState[slotsCount] = getState(SlotID(slotsCount));
  4782. upgradeAll = new CAdventureMapButton(CGI->generaltexth->allTexts[432],"",boost::bind(&CHillFortWindow::makeDeal, this, SlotID(slotsCount)),
  4783. 30, 231, "", SDLK_0, &files);
  4784. quit = new CAdventureMapButton("","",boost::bind(&CHillFortWindow::close, this), 294, 275, "IOKAY.DEF", SDLK_RETURN);
  4785. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  4786. garr = new CGarrisonInt(108, 60, 18, Point(),background->bg,Point(108,60),hero,NULL);
  4787. updateGarrisons();
  4788. }
  4789. void CHillFortWindow::updateGarrisons()
  4790. {
  4791. for (int i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
  4792. totalSumm[i]=0;
  4793. for (int i=0; i<slotsCount; i++)
  4794. {
  4795. costs[i].clear();
  4796. int newState = getState(SlotID(i));
  4797. if (newState != -1)
  4798. {
  4799. UpgradeInfo info;
  4800. LOCPLINT->cb->getUpgradeInfo(hero, SlotID(i), info);
  4801. if (info.newID.size())//we have upgrades here - update costs
  4802. {
  4803. costs[i] = info.cost[0] * hero->getStackCount(SlotID(i));
  4804. totalSumm += costs[i];
  4805. }
  4806. }
  4807. currState[i] = newState;
  4808. upgrade[i]->setIndex(newState);
  4809. upgrade[i]->block(currState[i] == -1);
  4810. upgrade[i]->hoverTexts[0] = getTextForSlot(SlotID(i));
  4811. }
  4812. int newState = getState(SlotID(slotsCount));
  4813. currState[slotsCount] = newState;
  4814. upgradeAll->setIndex(newState);
  4815. garr->recreateSlots();
  4816. }
  4817. void CHillFortWindow::makeDeal(SlotID slot)
  4818. {
  4819. assert(slot.getNum()>=0);
  4820. int offset = (slot.getNum() == slotsCount)?2:0;
  4821. switch (currState[slot.getNum()])
  4822. {
  4823. case 0:
  4824. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[314 + offset],
  4825. std::vector<CComponent*>(), soundBase::sound_todo);
  4826. break;
  4827. case 1:
  4828. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[313 + offset],
  4829. std::vector<CComponent*>(), soundBase::sound_todo);
  4830. break;
  4831. case 2:
  4832. for (int i=0; i<slotsCount; i++)
  4833. if ( slot.getNum() ==i || ( slot.getNum() == slotsCount && currState[i] == 2 ) )//this is activated slot or "upgrade all"
  4834. {
  4835. UpgradeInfo info;
  4836. LOCPLINT->cb->getUpgradeInfo(hero, SlotID(i), info);
  4837. LOCPLINT->cb->upgradeCreature(hero, SlotID(i), info.newID[0]);
  4838. }
  4839. break;
  4840. }
  4841. }
  4842. void CHillFortWindow::showAll (SDL_Surface *to)
  4843. {
  4844. CWindowObject::showAll(to);
  4845. for ( int i=0; i<slotsCount; i++)
  4846. {
  4847. if ( currState[i] == 0 || currState[i] == 2 )
  4848. {
  4849. if ( costs[i].size() )//we have several elements
  4850. {
  4851. int curY = 128;//reverse iterator is used to display gold as first element
  4852. for(int j = costs[i].size()-1; j >= 0; j--)
  4853. {
  4854. int val = costs[i][j];
  4855. if(!val) continue;
  4856. blitAtLoc(resources->ourImages[j].bitmap, 104+76*i, curY, to);
  4857. printToLoc(boost::lexical_cast<std::string>(val), 168+76*i, curY+16, FONT_SMALL, Colors::WHITE, to);
  4858. curY += 20;
  4859. }
  4860. }
  4861. else//free upgrade - print gold image and "Free" text
  4862. {
  4863. blitAtLoc(resources->ourImages[6].bitmap, 104+76*i, 128, to);
  4864. printToLoc(CGI->generaltexth->allTexts[344], 168+76*i, 144, FONT_SMALL, Colors::WHITE, to);
  4865. }
  4866. }
  4867. }
  4868. for (int i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
  4869. {
  4870. if (totalSumm[i])//this resource is used - display it
  4871. {
  4872. blitAtLoc(resources->ourImages[i].bitmap, 104+76*i, 237, to);
  4873. printToLoc(boost::lexical_cast<std::string>(totalSumm[i]), 166+76*i, 253, FONT_SMALL, Colors::WHITE, to);
  4874. }
  4875. }
  4876. }
  4877. std::string CHillFortWindow::getTextForSlot(SlotID slot)
  4878. {
  4879. if ( !hero->getCreature(slot) )//we dont have creature here
  4880. return "";
  4881. std::string str = CGI->generaltexth->allTexts[318];
  4882. int amount = hero->getStackCount(slot);
  4883. if ( amount == 1 )
  4884. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->nameSing);
  4885. else
  4886. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->namePl);
  4887. return str;
  4888. }
  4889. int CHillFortWindow::getState(SlotID slot)
  4890. {
  4891. TResources myRes = LOCPLINT->cb->getResourceAmount();
  4892. if ( slot.getNum() == slotsCount )//"Upgrade all" slot
  4893. {
  4894. bool allUpgraded = true;//All creatures are upgraded?
  4895. for (int i=0; i<slotsCount; i++)
  4896. allUpgraded &= currState[i] == 1 || currState[i] == -1;
  4897. if (allUpgraded)
  4898. return 1;
  4899. if(!totalSumm.canBeAfforded(myRes))
  4900. return 0;
  4901. return 2;
  4902. }
  4903. if (hero->slotEmpty(slot))//no creature here
  4904. return -1;
  4905. UpgradeInfo info;
  4906. LOCPLINT->cb->getUpgradeInfo(hero, slot, info);
  4907. if (!info.newID.size())//already upgraded
  4908. return 1;
  4909. if(!(info.cost[0] * hero->getStackCount(slot)).canBeAfforded(myRes))
  4910. return 0;
  4911. return 2;//can upgrade
  4912. }
  4913. CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner):
  4914. CWindowObject(PLAYER_COLORED | BORDERED, "TpRank"),
  4915. owner(_owner)
  4916. {
  4917. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4918. type |= BLOCK_ADV_HOTKEYS;
  4919. SThievesGuildInfo tgi; //info to be displayed
  4920. LOCPLINT->cb->getThievesGuildInfo(tgi, owner);
  4921. exitb = new CAdventureMapButton (CGI->generaltexth->allTexts[600], "", boost::bind(&CThievesGuildWindow::close,this), 748, 556, "TPMAGE1", SDLK_RETURN);
  4922. exitb->assignedKeys.insert(SDLK_ESCAPE);
  4923. statusBar = new CGStatusBar(3, 555, "TStatBar.bmp", 742);
  4924. resdatabar = new CMinorResDataBar();
  4925. resdatabar->pos.x += pos.x;
  4926. resdatabar->pos.y += pos.y;
  4927. //data for information table:
  4928. // fields[row][column] = list of id's of players for this box
  4929. static std::vector< std::vector< PlayerColor > > SThievesGuildInfo::* fields[] =
  4930. { &SThievesGuildInfo::numOfTowns, &SThievesGuildInfo::numOfHeroes, &SThievesGuildInfo::gold,
  4931. &SThievesGuildInfo::woodOre, &SThievesGuildInfo::mercSulfCrystGems, &SThievesGuildInfo::obelisks,
  4932. &SThievesGuildInfo::artifacts, &SThievesGuildInfo::army, &SThievesGuildInfo::income };
  4933. //printing texts & descriptions to background
  4934. for(int g=0; g<12; ++g)
  4935. {
  4936. int posY[] = {400, 460, 510};
  4937. int y;
  4938. if(g < 9)
  4939. y = 52 + 32*g;
  4940. else
  4941. y = posY[g-9];
  4942. std::string text = CGI->generaltexth->jktexts[24+g];
  4943. boost::algorithm::trim_if(text,boost::algorithm::is_any_of("\""));
  4944. new CLabel(135, y, FONT_MEDIUM, CENTER, Colors::YELLOW, text);
  4945. }
  4946. for(int g=1; g<tgi.playerColors.size(); ++g)
  4947. new CAnimImage("PRSTRIPS", g-1, 0, 250 + 66*g, 7);
  4948. for(int g=0; g<tgi.playerColors.size(); ++g)
  4949. new CLabel(283 + 66*g, 24, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[16+g]);
  4950. //printing flags
  4951. for(int g = 0; g < ARRAY_COUNT(fields); ++g) //by lines
  4952. {
  4953. for(int b=0; b<(tgi .* fields[g]).size(); ++b) //by places (1st, 2nd, ...)
  4954. {
  4955. std::vector<PlayerColor> &players = (tgi .* fields[g])[b]; //get players with this place in this line
  4956. //position of box
  4957. int xpos = 259 + 66 * b;
  4958. int ypos = 41 + 32 * g;
  4959. size_t rowLength[2]; //size of each row
  4960. rowLength[0] = std::min<size_t>(players.size(), 4);
  4961. rowLength[1] = players.size() - rowLength[0];
  4962. for (size_t j=0; j< 2; j++)
  4963. {
  4964. // origin of this row | offset for 2nd row| shift right for short rows
  4965. //if we have 2 rows, start either from mid or beginning (depending on count), otherwise center the flags
  4966. int rowStartX = xpos + (j ? 6 + (rowLength[j] < 3 ? 12 : 0) : 24 - 6 * rowLength[j]);
  4967. int rowStartY = ypos + (j ? 4 : 0);
  4968. for (size_t i=0; i< rowLength[j]; i++)
  4969. {
  4970. new CAnimImage("itgflags", players[i + j*4].getNum(), 0, rowStartX + i*12, rowStartY);
  4971. }
  4972. }
  4973. }
  4974. }
  4975. static const std::string colorToBox[] = {"PRRED.BMP", "PRBLUE.BMP", "PRTAN.BMP", "PRGREEN.BMP", "PRORANGE.BMP", "PRPURPLE.BMP", "PRTEAL.BMP", "PRROSE.bmp"};
  4976. //printing best hero
  4977. int counter = 0;
  4978. BOOST_FOREACH(auto & iter, tgi.colorToBestHero)
  4979. {
  4980. if(iter.second.portrait >= 0)
  4981. {
  4982. new CPicture(colorToBox[iter.first.getNum()], 253 + 66 * counter, 334);
  4983. new CAnimImage("PortraitsSmall", iter.second.portrait, 0, 260 + 66 * counter, 360);
  4984. //TODO: r-click info:
  4985. // - r-click on hero
  4986. // - r-click on primary skill label
  4987. if(iter.second.details)
  4988. {
  4989. new CTextBox(CGI->generaltexth->allTexts[184], Rect(260 + 66*counter, 396, 52, 64),
  4990. 0, FONT_TINY, TOPLEFT, Colors::WHITE);
  4991. for (int i=0; i<iter.second.details->primskills.size(); ++i)
  4992. {
  4993. new CLabel(310 + 66 * counter, 407 + 11*i, FONT_TINY, BOTTOMRIGHT, Colors::WHITE,
  4994. boost::lexical_cast<std::string>(iter.second.details->primskills[i]));
  4995. }
  4996. }
  4997. }
  4998. counter++;
  4999. }
  5000. //printing best creature
  5001. counter = 0;
  5002. BOOST_FOREACH(auto & it, tgi.bestCreature)
  5003. {
  5004. if(it.second >= 0)
  5005. new CAnimImage("TWCRPORT", it.second+2, 0, 255 + 66 * counter, 479);
  5006. counter++;
  5007. }
  5008. //printing personality
  5009. counter = 0;
  5010. BOOST_FOREACH(auto & it, tgi.personality)
  5011. {
  5012. std::string text;
  5013. if(it.second == EAiTactic::NONE)
  5014. {
  5015. text = CGI->generaltexth->arraytxt[172];
  5016. }
  5017. else if(it.second != EAiTactic::RANDOM)
  5018. {
  5019. text = CGI->generaltexth->arraytxt[168 + it.second];
  5020. }
  5021. new CLabel(283 + 66*counter, 459, FONT_SMALL, CENTER, Colors::WHITE, text);
  5022. counter++;
  5023. }
  5024. }
  5025. void MoraleLuckBox::set(const IBonusBearer *node)
  5026. {
  5027. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5028. const int textId[] = {62, 88}; //eg %s \n\n\n {Current Luck Modifiers:}
  5029. const int noneTxtId = 108; //Russian version uses same text for neutral morale\luck
  5030. const int neutralDescr[] = {60, 86}; //eg {Neutral Morale} \n\n Neutral morale means your armies will neither be blessed with extra attacks or freeze in combat.
  5031. const int componentType[] = {CComponent::luck, CComponent::morale};
  5032. const int hoverTextBase[] = {7, 4};
  5033. const Bonus::BonusType bonusType[] = {Bonus::LUCK, Bonus::MORALE};
  5034. int (IBonusBearer::*getValue[])() const = {&IBonusBearer::LuckVal, &IBonusBearer::MoraleVal};
  5035. int mrlt = -9;
  5036. TModDescr mrl;
  5037. if (node)
  5038. {
  5039. node->getModifiersWDescr(mrl, bonusType[morale]);
  5040. bonusValue = (node->*getValue[morale])();
  5041. }
  5042. else
  5043. bonusValue = 0;
  5044. mrlt = (bonusValue>0)-(bonusValue<0); //signum: -1 - bad luck / morale, 0 - neutral, 1 - good
  5045. hoverText = CGI->generaltexth->heroscrn[hoverTextBase[morale] - mrlt];
  5046. baseType = componentType[morale];
  5047. text = CGI->generaltexth->arraytxt[textId[morale]];
  5048. boost::algorithm::replace_first(text,"%s",CGI->generaltexth->arraytxt[neutralDescr[morale]-mrlt]);
  5049. if (!mrl.size())
  5050. text += CGI->generaltexth->arraytxt[noneTxtId];
  5051. else
  5052. {
  5053. if (node->hasBonusOfType (Bonus::UNDEAD) || node->hasBonusOfType(Bonus::BLOCK_MORALE) || node->hasBonusOfType(Bonus::NON_LIVING)) //it's a creature window
  5054. {
  5055. text += CGI->generaltexth->arraytxt[113]; //unaffected by morale
  5056. }
  5057. else
  5058. {
  5059. for(int it=0; it < mrl.size(); it++)
  5060. {
  5061. if (mrl[it].first) //no bonuses with value 0
  5062. text += "\n" + mrl[it].second;
  5063. }
  5064. }
  5065. }
  5066. std::string imageName;
  5067. if (small)
  5068. imageName = morale ? "IMRL30": "ILCK30";
  5069. else
  5070. imageName = morale ? "IMRL42" : "ILCK42";
  5071. delete image;
  5072. image = new CAnimImage(imageName, bonusValue + 3);
  5073. image->moveBy(Point(pos.w/2 - image->pos.w/2, pos.h/2 - image->pos.h/2));//center icon
  5074. }
  5075. MoraleLuckBox::MoraleLuckBox(bool Morale, const Rect &r, bool Small):
  5076. image(NULL),
  5077. morale(Morale),
  5078. small(Small)
  5079. {
  5080. bonusValue = 0;
  5081. pos = r + pos;
  5082. }
  5083. CArtifactHolder::CArtifactHolder()
  5084. {
  5085. }
  5086. void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation &artLoc)
  5087. {
  5088. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  5089. aoh->artifactRemoved(artLoc);
  5090. }
  5091. void CWindowWithArtifacts::artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc)
  5092. {
  5093. CArtifactsOfHero *destaoh = NULL;
  5094. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  5095. {
  5096. aoh->artifactMoved(artLoc, destLoc);
  5097. aoh->redraw();
  5098. if(destLoc.isHolder(aoh->getHero()))
  5099. destaoh = aoh;
  5100. }
  5101. //Make sure the status bar is updated so it does not display old text
  5102. if(destaoh != NULL && destaoh->getArtPlace(destLoc.slot) != NULL)
  5103. {
  5104. destaoh->getArtPlace(destLoc.slot)->hover(true);
  5105. }
  5106. }
  5107. void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation &artLoc)
  5108. {
  5109. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  5110. aoh->artifactDisassembled(artLoc);
  5111. }
  5112. void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation &artLoc)
  5113. {
  5114. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  5115. aoh->artifactAssembled(artLoc);
  5116. }
  5117. void CArtifactsOfHero::SCommonPart::Artpos::clear()
  5118. {
  5119. slotID = ArtifactPosition::PRE_FIRST;
  5120. AOH = NULL;
  5121. art = NULL;
  5122. }
  5123. CArtifactsOfHero::SCommonPart::Artpos::Artpos()
  5124. {
  5125. clear();
  5126. }
  5127. void CArtifactsOfHero::SCommonPart::Artpos::setTo(const CArtPlace *place, bool dontTakeBackpack)
  5128. {
  5129. slotID = place->slotID;
  5130. AOH = place->ourOwner;
  5131. if(slotID >= 19 && dontTakeBackpack)
  5132. art = NULL;
  5133. else
  5134. art = place->ourArt;
  5135. }
  5136. bool CArtifactsOfHero::SCommonPart::Artpos::operator==(const ArtifactLocation &al) const
  5137. {
  5138. if(!AOH)
  5139. return false;
  5140. bool ret = al.isHolder(AOH->curHero) && al.slot == slotID;
  5141. //assert(al.getArt() == art);
  5142. return ret;
  5143. }
  5144. bool CArtifactsOfHero::SCommonPart::Artpos::valid()
  5145. {
  5146. assert(AOH && art);
  5147. return art == AOH->curHero->getArt(slotID);
  5148. }
  5149. void CRClickPopup::clickRight(tribool down, bool previousState)
  5150. {
  5151. if(down)
  5152. return;
  5153. close();
  5154. }
  5155. void CRClickPopup::close()
  5156. {
  5157. GH.popIntTotally(this);
  5158. }
  5159. void CRClickPopup::createAndPush(const std::string &txt, const CInfoWindow::TCompsInfo &comps)
  5160. {
  5161. PlayerColor player = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1); //if no player, then use blue
  5162. CSimpleWindow * temp = new CInfoWindow(txt, player, comps);
  5163. temp->center(Point(GH.current->motion)); //center on mouse
  5164. temp->fitToScreen(10);
  5165. CRClickPopupInt *rcpi = new CRClickPopupInt(temp,true);
  5166. GH.pushInt(rcpi);
  5167. }
  5168. void CRClickPopup::createAndPush(const std::string &txt, CComponent * component)
  5169. {
  5170. CInfoWindow::TCompsInfo intComps;
  5171. intComps.push_back(component);
  5172. createAndPush(txt, intComps);
  5173. }
  5174. Point CInfoBoxPopup::toScreen(Point p)
  5175. {
  5176. vstd::abetween(p.x, adventureInt->terrain.pos.x + 100, adventureInt->terrain.pos.x + adventureInt->terrain.pos.w - 100);
  5177. vstd::abetween(p.y, adventureInt->terrain.pos.y + 100, adventureInt->terrain.pos.y + adventureInt->terrain.pos.h - 100);
  5178. return p;
  5179. }
  5180. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGTownInstance * town):
  5181. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "TOWNQVBK", toScreen(position))
  5182. {
  5183. InfoAboutTown iah;
  5184. LOCPLINT->cb->getTownInfo(town, iah);
  5185. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5186. new CTownTooltip(Point(9, 10), iah);
  5187. }
  5188. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGHeroInstance * hero):
  5189. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "HEROQVBK", toScreen(position))
  5190. {
  5191. InfoAboutHero iah;
  5192. LOCPLINT->cb->getHeroInfo(hero, iah);
  5193. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5194. new CHeroTooltip(Point(9, 10), iah);
  5195. }
  5196. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGGarrison * garr):
  5197. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "TOWNQVBK", toScreen(position))
  5198. {
  5199. InfoAboutTown iah;
  5200. LOCPLINT->cb->getTownInfo(garr, iah);
  5201. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5202. new CArmyTooltip(Point(9, 10), iah);
  5203. }
  5204. CIntObject * CRClickPopup::createInfoWin(Point position, const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  5205. {
  5206. if(!specific)
  5207. specific = adventureInt->selection;
  5208. assert(specific);
  5209. switch(specific->ID)
  5210. {
  5211. case Obj::HERO:
  5212. return new CInfoBoxPopup(position, dynamic_cast<const CGHeroInstance *>(specific));
  5213. case Obj::TOWN:
  5214. return new CInfoBoxPopup(position, dynamic_cast<const CGTownInstance *>(specific));
  5215. case Obj::GARRISON:
  5216. case Obj::GARRISON2:
  5217. return new CInfoBoxPopup(position, dynamic_cast<const CGGarrison *>(specific));
  5218. default:
  5219. return NULL;
  5220. }
  5221. }
  5222. void CRClickPopup::createAndPush(const CGObjectInstance *obj, const Point &p, EAlignment alignment /*= BOTTOMRIGHT*/)
  5223. {
  5224. CIntObject *iWin = createInfoWin(p, obj); //try get custom infowindow for this obj
  5225. if(iWin)
  5226. GH.pushInt(iWin);
  5227. else
  5228. CRClickPopup::createAndPush(obj->getHoverText());
  5229. }
  5230. CRClickPopup::CRClickPopup()
  5231. {
  5232. addUsedEvents(RCLICK);
  5233. }
  5234. CRClickPopup::~CRClickPopup()
  5235. {
  5236. }
  5237. void CRClickPopupInt::show(SDL_Surface * to)
  5238. {
  5239. inner->show(to);
  5240. }
  5241. CRClickPopupInt::CRClickPopupInt( IShowActivatable *our, bool deleteInt )
  5242. {
  5243. CCS->curh->hide();
  5244. inner = our;
  5245. delInner = deleteInt;
  5246. }
  5247. CRClickPopupInt::~CRClickPopupInt()
  5248. {
  5249. // //workaround for hero window issue - if it's our interface, call dispose to properly reset it's state
  5250. // //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...?)
  5251. // if(LOCPLINT && inner == adventureInt->heroWindow)
  5252. // adventureInt->heroWindow->dispose();
  5253. if(delInner)
  5254. delete inner;
  5255. CCS->curh->show();
  5256. }
  5257. void CRClickPopupInt::showAll(SDL_Surface * to)
  5258. {
  5259. inner->showAll(to);
  5260. }